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