| | 1 | | using DCL.Controllers; |
| | 2 | | using DCL.ECS7; |
| | 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 disposed = false; |
| | 15 | |
|
| 21 | 16 | | public CRDTProtocol crdtProtocol { get; } |
| | 17 | |
|
| 336 | 18 | | public CRDTExecutor(IParcelScene scene, ECSComponentsManager componentsManager) |
| | 19 | | { |
| 336 | 20 | | ownerScene = scene; |
| 336 | 21 | | crdtProtocol = new CRDTProtocol(); |
| 336 | 22 | | ecsManager = componentsManager; |
| 336 | 23 | | } |
| | 24 | |
|
| | 25 | | public void Dispose() |
| | 26 | | { |
| | 27 | | #if UNITY_EDITOR |
| 327 | 28 | | if (disposed) |
| 0 | 29 | | Debug.LogWarning("CRDTExecutor::Dispose Called while disposed"); |
| | 30 | | #endif |
| | 31 | |
|
| 327 | 32 | | if (disposed) |
| 0 | 33 | | return; |
| | 34 | |
|
| 327 | 35 | | disposed = true; |
| | 36 | |
|
| 327 | 37 | | using (var entities = ownerScene.entities.Values.GetEnumerator()) |
| | 38 | | { |
| 840 | 39 | | while (entities.MoveNext()) |
| | 40 | | { |
| 513 | 41 | | var entity = entities.Current; |
| 513 | 42 | | entity.OnRemoved -= OnEntityRemoved; |
| 513 | 43 | | ecsManager.RemoveAllComponents(ownerScene, entity); |
| | 44 | | } |
| 327 | 45 | | } |
| 327 | 46 | | } |
| | 47 | |
|
| | 48 | | public void Execute(CrdtMessage crdtMessage) |
| | 49 | | { |
| | 50 | | #if UNITY_EDITOR |
| 14 | 51 | | if (disposed) |
| 0 | 52 | | Debug.LogWarning("CRDTExecutor::Execute Called while disposed"); |
| | 53 | | #endif |
| | 54 | |
|
| 14 | 55 | | CRDTProtocol.ProcessMessageResultType resultType = crdtProtocol.ProcessMessage(crdtMessage); |
| | 56 | |
|
| | 57 | | // If the message change the state |
| 14 | 58 | | if (resultType == CRDTProtocol.ProcessMessageResultType.StateUpdatedData || |
| | 59 | | resultType == CRDTProtocol.ProcessMessageResultType.StateUpdatedTimestamp || |
| | 60 | | resultType == CRDTProtocol.ProcessMessageResultType.EntityWasDeleted) |
| | 61 | | { |
| 14 | 62 | | ExecuteWithoutStoringState(crdtMessage.EntityId, crdtMessage.ComponentId, crdtMessage.Data); |
| | 63 | | } |
| 14 | 64 | | } |
| | 65 | |
|
| | 66 | | public void ExecuteWithoutStoringState(long entityId, int componentId, object data) |
| | 67 | | { |
| | 68 | | #if UNITY_EDITOR |
| 19 | 69 | | if (disposed) |
| 0 | 70 | | Debug.LogWarning($"CRDTExecutor::ExecuteWithoutStoringState Called while disposed scene {ownerScene.scen |
| | 71 | | #endif |
| | 72 | |
|
| 19 | 73 | | if (disposed) |
| 0 | 74 | | return; |
| | 75 | |
|
| | 76 | | // null data means to remove component, not null data means to update or create |
| 19 | 77 | | if (data != null) |
| | 78 | | { |
| 15 | 79 | | DeserializeComponent(ownerScene, entityId, componentId, data); |
| | 80 | | } |
| | 81 | | else |
| | 82 | | { |
| 4 | 83 | | RemoveComponent(entityId, componentId); |
| | 84 | | } |
| 4 | 85 | | } |
| | 86 | |
|
| | 87 | | public void GenerateInitialEntities() |
| | 88 | | { |
| 4 | 89 | | var cameraEntity = GetOrCreateEntity(ownerScene, SpecialEntityId.CAMERA_ENTITY); |
| 4 | 90 | | var playerEntity = GetOrCreateEntity(ownerScene, SpecialEntityId.PLAYER_ENTITY); |
| 4 | 91 | | ecsManager.GetOrCreateComponent(ComponentID.TRANSFORM, ownerScene, cameraEntity); |
| 4 | 92 | | ecsManager.GetOrCreateComponent(ComponentID.TRANSFORM, ownerScene, playerEntity); |
| 4 | 93 | | } |
| | 94 | |
|
| | 95 | | public void PutComponent<T>(long entityId, ECSComponent<T> component, T model) |
| | 96 | | { |
| | 97 | | #if UNITY_EDITOR |
| 1197 | 98 | | if (disposed) |
| 4 | 99 | | Debug.LogWarning($"CRDTExecutor::PutComponent Called while disposed scene {ownerScene.sceneData.sceneNum |
| | 100 | | #endif |
| | 101 | |
|
| 1197 | 102 | | if (disposed) |
| 4 | 103 | | return; |
| | 104 | |
|
| 1193 | 105 | | IDCLEntity entity = GetOrCreateEntity(ownerScene, entityId); |
| | 106 | |
|
| 1193 | 107 | | if (!component.HasComponent(ownerScene, entity)) |
| | 108 | | { |
| 700 | 109 | | component.Create(ownerScene, entity); |
| 700 | 110 | | ecsManager.SignalComponentCreated(ownerScene, entity, component); |
| | 111 | | } |
| | 112 | |
|
| 1193 | 113 | | component.SetModel(ownerScene, entity, model); |
| 1193 | 114 | | ecsManager.SignalComponentUpdated(ownerScene, entity, component); |
| 1193 | 115 | | } |
| | 116 | |
|
| | 117 | | public void RemoveComponent(long entityId, int componentId) |
| | 118 | | { |
| | 119 | | #if UNITY_EDITOR |
| 184 | 120 | | if (disposed) |
| 4 | 121 | | Debug.LogWarning($"CRDTExecutor::RemoveComponent Called while disposed scene {ownerScene.sceneData.scene |
| | 122 | | #endif |
| | 123 | |
|
| 184 | 124 | | if (disposed) |
| 4 | 125 | | return; |
| | 126 | |
|
| 180 | 127 | | if (!ownerScene.entities.TryGetValue(entityId, out IDCLEntity entity)) |
| | 128 | | { |
| 0 | 129 | | return; |
| | 130 | | } |
| | 131 | |
|
| 180 | 132 | | ecsManager.RemoveComponent(componentId, ownerScene, entity); |
| | 133 | |
|
| | 134 | | // there is no component for this entity so we remove it |
| | 135 | | // from scene |
| 180 | 136 | | if (!ecsManager.HasAnyComponent(ownerScene, entity)) |
| | 137 | | { |
| 38 | 138 | | RemoveEntity(ownerScene, entityId); |
| | 139 | | } |
| 180 | 140 | | } |
| | 141 | |
|
| | 142 | | private void DeserializeComponent(IParcelScene scene, long entityId, int componentId, object data) |
| | 143 | | { |
| 15 | 144 | | IDCLEntity entity = GetOrCreateEntity(scene, entityId); |
| 15 | 145 | | ecsManager.DeserializeComponent(componentId, scene, entity, data); |
| 15 | 146 | | } |
| | 147 | |
|
| | 148 | | private IDCLEntity GetOrCreateEntity(IParcelScene scene, long entityId) |
| | 149 | | { |
| 1216 | 150 | | if (scene.entities.TryGetValue(entityId, out IDCLEntity entity)) |
| | 151 | | { |
| 1098 | 152 | | return entity; |
| | 153 | | } |
| | 154 | |
|
| | 155 | | // CreateEntity internally adds entity to `scene.entities` |
| 118 | 156 | | entity = scene.CreateEntity(entityId); |
| 118 | 157 | | entity.OnRemoved += OnEntityRemoved; |
| 118 | 158 | | return entity; |
| | 159 | | } |
| | 160 | |
|
| | 161 | | private void RemoveEntity(IParcelScene scene, long entityId) |
| | 162 | | { |
| 38 | 163 | | if (!scene.entities.TryGetValue(entityId, out IDCLEntity entity)) |
| | 164 | | { |
| 0 | 165 | | return; |
| | 166 | | } |
| | 167 | |
|
| | 168 | | // we unsubscribe since this methods means that entity has no components |
| | 169 | | // so there is no need to try to clean them up on removed event |
| 38 | 170 | | entity.OnRemoved -= OnEntityRemoved; |
| 38 | 171 | | scene.RemoveEntity(entityId); |
| 38 | 172 | | } |
| | 173 | |
|
| | 174 | | private void OnEntityRemoved(IDCLEntity entity) |
| | 175 | | { |
| 1 | 176 | | entity.OnRemoved -= OnEntityRemoved; |
| 1 | 177 | | ecsManager.RemoveAllComponents(ownerScene, entity); |
| 1 | 178 | | } |
| | 179 | | } |
| | 180 | | } |