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