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