| | 1 | | using System.Collections.Generic; |
| | 2 | | using DCL.Controllers; |
| | 3 | | using DCL.ECSRuntime; |
| | 4 | | using DCL.Models; |
| | 5 | | using UnityEngine; |
| | 6 | |
|
| | 7 | | namespace DCL.CRDT |
| | 8 | | { |
| | 9 | | public class CRDTExecutor : ICRDTExecutor |
| | 10 | | { |
| | 11 | | private readonly IParcelScene ownerScene; |
| | 12 | | private readonly ECSComponentsManager ecsManager; |
| | 13 | |
|
| | 14 | | private bool sceneAdded = false; |
| | 15 | | private bool disposed = false; |
| | 16 | | private readonly IList<IParcelScene> loadedScenes; |
| | 17 | |
|
| 3 | 18 | | public CRDTProtocol crdtProtocol { get; } |
| | 19 | |
|
| 147 | 20 | | public CRDTExecutor(IParcelScene scene, ECSComponentsManager componentsManager) |
| | 21 | | { |
| 147 | 22 | | ownerScene = scene; |
| 147 | 23 | | crdtProtocol = new CRDTProtocol(); |
| 147 | 24 | | ecsManager = componentsManager; |
| 147 | 25 | | loadedScenes = DataStore.i.ecs7.scenes; |
| 147 | 26 | | } |
| | 27 | |
|
| | 28 | | public void Dispose() |
| | 29 | | { |
| | 30 | | #if UNITY_EDITOR |
| 4 | 31 | | if (disposed) |
| 0 | 32 | | Debug.LogWarning("CRDTExecutor::Dispose Called while disposed"); |
| | 33 | | #endif |
| | 34 | |
|
| 4 | 35 | | if (disposed) |
| 0 | 36 | | return; |
| | 37 | |
|
| 4 | 38 | | disposed = true; |
| 4 | 39 | | loadedScenes.Remove(ownerScene); |
| 4 | 40 | | using (var entities = ownerScene.entities.Values.GetEnumerator()) |
| | 41 | | { |
| 4 | 42 | | while (entities.MoveNext()) |
| | 43 | | { |
| 0 | 44 | | var entity = entities.Current; |
| 0 | 45 | | entity.OnRemoved -= OnEntityRemoved; |
| 0 | 46 | | ecsManager.RemoveAllComponents(ownerScene, entity); |
| | 47 | | } |
| 4 | 48 | | } |
| 4 | 49 | | } |
| | 50 | |
|
| | 51 | | public void Execute(CRDTMessage crdtMessage) |
| | 52 | | { |
| | 53 | | #if UNITY_EDITOR |
| 12 | 54 | | if (disposed) |
| 0 | 55 | | Debug.LogWarning("CRDTExecutor::Execute Called while disposed"); |
| | 56 | | #endif |
| 12 | 57 | | if (!sceneAdded) |
| | 58 | | { |
| 6 | 59 | | sceneAdded = true; |
| 6 | 60 | | loadedScenes.Add(ownerScene); |
| | 61 | | } |
| | 62 | |
|
| 12 | 63 | | CRDTMessage storedMessage = crdtProtocol.GetState(crdtMessage.key1, crdtMessage.key2); |
| 12 | 64 | | CRDTMessage resultMessage = crdtProtocol.ProcessMessage(crdtMessage); |
| | 65 | |
|
| | 66 | | // messages are the same so state didn't change |
| 12 | 67 | | if (storedMessage == resultMessage) |
| | 68 | | { |
| 0 | 69 | | return; |
| | 70 | | } |
| | 71 | |
|
| | 72 | |
|
| 12 | 73 | | ExecuteWithoutStoringState(crdtMessage.key1, crdtMessage.key2, crdtMessage.data); |
| 12 | 74 | | } |
| | 75 | |
|
| | 76 | | public void ExecuteWithoutStoringState(long entityId, int componentId, object data) |
| | 77 | | { |
| | 78 | | #if UNITY_EDITOR |
| 342 | 79 | | if (disposed) |
| 0 | 80 | | Debug.LogWarning("CRDTExecutor::ExecuteWithoutStoringState Called while disposed"); |
| | 81 | | #endif |
| | 82 | |
|
| 342 | 83 | | if (disposed) |
| 0 | 84 | | return; |
| | 85 | |
|
| | 86 | | // null data means to remove component, not null data means to update or create |
| 342 | 87 | | if (data != null) |
| | 88 | | { |
| 271 | 89 | | PutComponent(ownerScene, entityId, componentId, data); |
| 271 | 90 | | } |
| | 91 | | else |
| | 92 | | { |
| 71 | 93 | | RemoveComponent(ownerScene, entityId, componentId); |
| | 94 | | } |
| 71 | 95 | | } |
| | 96 | |
|
| | 97 | | private void PutComponent(IParcelScene scene, long entityId, int componentId, object data) |
| | 98 | | { |
| 271 | 99 | | IDCLEntity entity = GetOrCreateEntity(scene, entityId); |
| 271 | 100 | | ecsManager.DeserializeComponent(componentId, scene, entity, data); |
| 271 | 101 | | } |
| | 102 | |
|
| | 103 | | private void RemoveComponent(IParcelScene scene, long entityId, int componentId) |
| | 104 | | { |
| 71 | 105 | | if (!scene.entities.TryGetValue(entityId, out IDCLEntity entity)) |
| | 106 | | { |
| 0 | 107 | | return; |
| | 108 | | } |
| | 109 | |
|
| 71 | 110 | | ecsManager.RemoveComponent(componentId, scene, entity); |
| | 111 | |
|
| | 112 | | // there is no component for this entity so we remove it |
| | 113 | | // from scene |
| 71 | 114 | | if (!ecsManager.HasAnyComponent(scene, entity)) |
| | 115 | | { |
| 8 | 116 | | RemoveEntity(scene, entityId); |
| | 117 | | } |
| 71 | 118 | | } |
| | 119 | |
|
| | 120 | | private IDCLEntity GetOrCreateEntity(IParcelScene scene, long entityId) |
| | 121 | | { |
| 271 | 122 | | if (scene.entities.TryGetValue(entityId, out IDCLEntity entity)) |
| | 123 | | { |
| 234 | 124 | | return entity; |
| | 125 | | } |
| | 126 | |
|
| | 127 | | // CreateEntity internally adds entity to `scene.entities` |
| 37 | 128 | | entity = scene.CreateEntity(entityId); |
| 37 | 129 | | entity.OnRemoved += OnEntityRemoved; |
| 37 | 130 | | return entity; |
| | 131 | | } |
| | 132 | |
|
| | 133 | | private void RemoveEntity(IParcelScene scene, long entityId) |
| | 134 | | { |
| 8 | 135 | | if (!scene.entities.TryGetValue(entityId, out IDCLEntity entity)) |
| | 136 | | { |
| 0 | 137 | | return; |
| | 138 | | } |
| | 139 | |
|
| | 140 | | // we unsubscribe since this methods means that entity has no components |
| | 141 | | // so there is no need to try to clean them up on removed event |
| 8 | 142 | | entity.OnRemoved -= OnEntityRemoved; |
| 8 | 143 | | scene.RemoveEntity(entityId); |
| 8 | 144 | | } |
| | 145 | |
|
| | 146 | | private void OnEntityRemoved(IDCLEntity entity) |
| | 147 | | { |
| 1 | 148 | | entity.OnRemoved -= OnEntityRemoved; |
| 1 | 149 | | ecsManager.RemoveAllComponents(ownerScene, entity); |
| 1 | 150 | | } |
| | 151 | | } |
| | 152 | | } |