| | 1 | | using DCL.Controllers; |
| | 2 | | using DCL.CRDT; |
| | 3 | | using DCL.ECS7.InternalComponents; |
| | 4 | | using DCL.ECSRuntime; |
| | 5 | | using DCL.Models; |
| | 6 | | using System; |
| | 7 | | using System.Collections.Generic; |
| | 8 | |
|
| | 9 | | public class InternalECSComponent<T> : IInternalECSComponent<T> where T: struct, IInternalComponent |
| | 10 | | { |
| | 11 | | internal readonly struct DirtyData |
| | 12 | | { |
| | 13 | | public readonly T Data; |
| | 14 | | public readonly bool IsDelayedRemoval; |
| | 15 | |
|
| | 16 | | public DirtyData(T data, bool isDelayedRemoval) |
| | 17 | | { |
| 1122 | 18 | | Data = data; |
| 1122 | 19 | | IsDelayedRemoval = isDelayedRemoval; |
| 1122 | 20 | | } |
| | 21 | | } |
| | 22 | |
|
| | 23 | | private readonly ECSComponentsFactory componentsFactory; |
| | 24 | | private readonly int componentId; |
| | 25 | | private readonly ECSComponent<T> component; |
| | 26 | | private readonly IReadOnlyDictionary<int, ICRDTExecutor> crdtExecutors; |
| | 27 | | private readonly IComponentDirtySystem dirtySystem; |
| 4711 | 28 | | internal readonly DualKeyValueSet<int, long, DirtyData> markAsDirtyComponents = new DualKeyValueSet<int, long, Dirty |
| 4711 | 29 | | internal readonly DualKeyValueSet<int, long, DirtyData> removeAsDirtyComponents = new DualKeyValueSet<int, long, Dir |
| | 30 | |
|
| 4711 | 31 | | public InternalECSComponent(InternalECSComponentsId id, |
| | 32 | | ECSComponentsManager componentsManager, |
| | 33 | | ECSComponentsFactory componentsFactory, |
| | 34 | | Func<IECSComponentHandler<T>> handlerBuilder, |
| | 35 | | IReadOnlyDictionary<int, ICRDTExecutor> crdtExecutors, |
| | 36 | | IComponentDirtySystem dirtySystem) |
| | 37 | | { |
| 4711 | 38 | | this.componentId = (int)id; |
| 4711 | 39 | | this.componentsFactory = componentsFactory; |
| 4711 | 40 | | this.crdtExecutors = crdtExecutors; |
| 4711 | 41 | | this.dirtySystem = dirtySystem; |
| | 42 | |
|
| 4711 | 43 | | componentsFactory.AddOrReplaceComponent<T>(componentId, x => (T)x, handlerBuilder); |
| 4711 | 44 | | component = (ECSComponent<T>)componentsManager.GetOrCreateComponent(componentId); |
| | 45 | |
|
| 4711 | 46 | | dirtySystem.MarkComponentsAsDirty += MarkDirtyComponentData; |
| 4711 | 47 | | dirtySystem.RemoveComponentsAsDirty += ResetDirtyComponentData; |
| 4711 | 48 | | } |
| | 49 | |
|
| | 50 | | public void Dispose() |
| | 51 | | { |
| 21 | 52 | | componentsFactory.RemoveComponent(componentId); |
| 21 | 53 | | dirtySystem.MarkComponentsAsDirty -= MarkDirtyComponentData; |
| 21 | 54 | | dirtySystem.RemoveComponentsAsDirty -= ResetDirtyComponentData; |
| 21 | 55 | | } |
| | 56 | |
|
| 0 | 57 | | public int ComponentId => componentId; |
| | 58 | |
|
| | 59 | | public void PutFor(IParcelScene scene, IDCLEntity entity, T model) |
| | 60 | | { |
| 737 | 61 | | PutFor(scene, entity.entityId, model); |
| 737 | 62 | | } |
| | 63 | |
|
| | 64 | | public void PutFor(IParcelScene scene, long entityId, T model) |
| | 65 | | { |
| 947 | 66 | | PutFor(scene.sceneData.sceneNumber, entityId, model); |
| 947 | 67 | | } |
| | 68 | |
|
| | 69 | | public void PutFor(int sceneNumber, long entityId, T model) |
| | 70 | | { |
| 947 | 71 | | if (crdtExecutors.TryGetValue(sceneNumber, out ICRDTExecutor crdtExecutor)) |
| | 72 | | { |
| 941 | 73 | | markAsDirtyComponents[sceneNumber, entityId] = new DirtyData(model, false); |
| | 74 | |
|
| 941 | 75 | | crdtExecutor.PutComponent(entityId, component, model); |
| | 76 | | } |
| 947 | 77 | | } |
| | 78 | |
|
| | 79 | | public void RemoveFor(IParcelScene scene, IDCLEntity entity, T defaultModel) => |
| 97 | 80 | | RemoveFor(scene, entity.entityId, defaultModel); |
| | 81 | |
|
| | 82 | | public void RemoveFor(IParcelScene scene, long entityId, T defaultModel) => |
| 97 | 83 | | RemoveFor(scene.sceneData.sceneNumber, entityId, defaultModel); |
| | 84 | |
|
| | 85 | | public void RemoveFor(int sceneNumber, long entityId, T defaultModel) |
| | 86 | | { |
| 97 | 87 | | if (!crdtExecutors.TryGetValue(sceneNumber, out ICRDTExecutor crdtExecutor)) |
| 0 | 88 | | return; |
| | 89 | |
|
| 97 | 90 | | markAsDirtyComponents[sceneNumber, entityId] = new DirtyData(defaultModel, true); |
| | 91 | |
|
| 97 | 92 | | crdtExecutor.PutComponent(entityId, component, defaultModel); |
| 97 | 93 | | } |
| | 94 | |
|
| | 95 | | public void RemoveFor(IParcelScene scene, IDCLEntity entity) => |
| 174 | 96 | | RemoveFor(scene, entity.entityId); |
| | 97 | |
|
| | 98 | | public void RemoveFor(IParcelScene scene, long entityId) => |
| 177 | 99 | | RemoveFor(scene.sceneData.sceneNumber, entityId); |
| | 100 | |
|
| | 101 | | public void RemoveFor(int sceneNumber, long entityId) |
| | 102 | | { |
| 177 | 103 | | markAsDirtyComponents.Remove(sceneNumber, entityId); |
| | 104 | |
|
| 177 | 105 | | if (!crdtExecutors.TryGetValue(sceneNumber, out ICRDTExecutor crdtExecutor)) |
| 0 | 106 | | return; |
| | 107 | |
|
| 177 | 108 | | crdtExecutor.RemoveComponent(entityId, componentId); |
| 177 | 109 | | } |
| | 110 | |
|
| | 111 | | public ECSComponentData<T>? GetFor(IParcelScene scene, IDCLEntity entity) |
| | 112 | | { |
| 723 | 113 | | return GetFor(scene, entity.entityId); |
| | 114 | | } |
| | 115 | |
|
| | 116 | | public ECSComponentData<T>? GetFor(IParcelScene scene, long entityId) |
| | 117 | | { |
| 927 | 118 | | if (component.TryGet(scene, entityId, out var data)) |
| | 119 | | { |
| 469 | 120 | | if (markAsDirtyComponents.TryGetValue(scene.sceneData.sceneNumber, entityId, out var dirtyData)) |
| | 121 | | { |
| 429 | 122 | | T model = dirtyData.Data; |
| 429 | 123 | | model.dirty = true; |
| 429 | 124 | | return data.With(model); |
| | 125 | | } |
| | 126 | |
|
| 40 | 127 | | return data; |
| | 128 | | } |
| | 129 | |
|
| 458 | 130 | | return null; |
| | 131 | | } |
| | 132 | |
|
| | 133 | | public IReadOnlyList<KeyValueSetTriplet<IParcelScene, long, ECSComponentData<T>>> GetForAll() |
| | 134 | | { |
| 178 | 135 | | return component.Get(); |
| | 136 | | } |
| | 137 | |
|
| | 138 | | private void MarkDirtyComponentData() |
| | 139 | | { |
| 1265 | 140 | | var markAsDirty = markAsDirtyComponents.Pairs; |
| | 141 | |
|
| 2698 | 142 | | for (int i = 0; i < markAsDirty.Count; i++) |
| | 143 | | { |
| 84 | 144 | | int sceneNumber = markAsDirty[i].key1; |
| 84 | 145 | | long entityId = markAsDirty[i].key2; |
| 84 | 146 | | T data = markAsDirty[i].value.Data; |
| 84 | 147 | | bool isRemoval = markAsDirty[i].value.IsDelayedRemoval; |
| | 148 | |
|
| 84 | 149 | | if (!crdtExecutors.TryGetValue(sceneNumber, out ICRDTExecutor crdtExecutor)) |
| | 150 | | { |
| | 151 | | continue; |
| | 152 | | } |
| | 153 | |
|
| 84 | 154 | | data.dirty = true; |
| 84 | 155 | | crdtExecutor.PutComponent(entityId, component, data); |
| 84 | 156 | | removeAsDirtyComponents[sceneNumber, entityId] = new DirtyData(data, isRemoval); |
| | 157 | | } |
| | 158 | |
|
| 1265 | 159 | | markAsDirtyComponents.Clear(); |
| 1265 | 160 | | } |
| | 161 | |
|
| | 162 | | private void ResetDirtyComponentData() |
| | 163 | | { |
| 1219 | 164 | | var resetDirtyComponents = removeAsDirtyComponents.Pairs; |
| | 165 | |
|
| 2594 | 166 | | for (int i = 0; i < resetDirtyComponents.Count; i++) |
| | 167 | | { |
| 78 | 168 | | int sceneNumber = resetDirtyComponents[i].key1; |
| 78 | 169 | | long entityId = resetDirtyComponents[i].key2; |
| 78 | 170 | | T data = resetDirtyComponents[i].value.Data; |
| 78 | 171 | | bool isRemoval = resetDirtyComponents[i].value.IsDelayedRemoval; |
| | 172 | |
|
| 78 | 173 | | if (!crdtExecutors.TryGetValue(sceneNumber, out ICRDTExecutor crdtExecutor)) |
| | 174 | | { |
| | 175 | | continue; |
| | 176 | | } |
| | 177 | |
|
| 78 | 178 | | if (isRemoval) |
| | 179 | | { |
| 3 | 180 | | crdtExecutor.RemoveComponent(entityId, componentId); |
| | 181 | | } |
| | 182 | | else |
| | 183 | | { |
| 75 | 184 | | data.dirty = false; |
| 75 | 185 | | crdtExecutor.PutComponent(entityId, component, data); |
| | 186 | | } |
| | 187 | | } |
| | 188 | |
|
| 1219 | 189 | | removeAsDirtyComponents.Clear(); |
| 1219 | 190 | | } |
| | 191 | | } |