| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using DCL.Controllers; |
| | 4 | | using DCL.Models; |
| | 5 | | using UnityEngine; |
| | 6 | |
|
| | 7 | | namespace DCL.ECSRuntime |
| | 8 | | { |
| | 9 | | public class ECSComponentWriter : IECSComponentWriter |
| | 10 | | { |
| | 11 | | public delegate void WriteComponent(string sceneId, long entityId, int componentId, byte[] data, |
| | 12 | | long minTimeStamp, ECSComponentWriteType writeType); |
| | 13 | |
|
| 3 | 14 | | private readonly Dictionary<int, object> serializers = new Dictionary<int, object>(); |
| | 15 | | private WriteComponent writeComponent; |
| | 16 | |
|
| 3 | 17 | | public ECSComponentWriter(WriteComponent writeComponent) |
| | 18 | | { |
| 3 | 19 | | this.writeComponent = writeComponent; |
| 3 | 20 | | } |
| | 21 | |
|
| | 22 | | public void AddOrReplaceComponentSerializer<T>(int componentId, Func<T, byte[]> serializer) |
| | 23 | | { |
| 26 | 24 | | serializers[componentId] = serializer; |
| 26 | 25 | | } |
| | 26 | |
|
| | 27 | | public void RemoveComponentSerializer(int componentId) |
| | 28 | | { |
| 24 | 29 | | serializers.Remove(componentId); |
| 24 | 30 | | } |
| | 31 | |
|
| | 32 | | public void PutComponent<T>(IParcelScene scene, IDCLEntity entity, int componentId, T model, ECSComponentWriteTy |
| | 33 | | { |
| 1 | 34 | | PutComponent(scene.sceneData.id, entity.entityId, componentId, model, -1, writeType); |
| 1 | 35 | | } |
| | 36 | |
|
| | 37 | | public void PutComponent<T>(string sceneId, long entityId, int componentId, T model, ECSComponentWriteType write |
| | 38 | | { |
| 0 | 39 | | PutComponent(sceneId, entityId, componentId, model, -1, writeType); |
| 0 | 40 | | } |
| | 41 | |
|
| | 42 | | public void PutComponent<T>(string sceneId, long entityId, int componentId, T model, long minTimeStamp, ECSCompo |
| | 43 | | { |
| 1 | 44 | | if (!serializers.TryGetValue(componentId, out object serializer)) |
| | 45 | | { |
| 0 | 46 | | Debug.LogError($"Trying to write component but no serializer was found for {componentId}"); |
| 0 | 47 | | return; |
| | 48 | | } |
| | 49 | |
|
| 1 | 50 | | if (serializer is Func<T, byte[]> typedSerializer) |
| | 51 | | { |
| 1 | 52 | | writeComponent(sceneId, entityId, componentId, typedSerializer(model), minTimeStamp, writeType); |
| 1 | 53 | | } |
| | 54 | | else |
| | 55 | | { |
| 0 | 56 | | Debug.LogError($"Trying to write component but serializer for {componentId} does not match {nameof(T)}") |
| | 57 | | } |
| 0 | 58 | | } |
| | 59 | |
|
| | 60 | | public void RemoveComponent(IParcelScene scene, IDCLEntity entity, int componentId, ECSComponentWriteType writeT |
| | 61 | | { |
| 1 | 62 | | RemoveComponent(scene.sceneData.id, entity.entityId, componentId, writeType); |
| 1 | 63 | | } |
| | 64 | |
|
| | 65 | | public void RemoveComponent(string sceneId, long entityId, int componentId, ECSComponentWriteType writeType) |
| | 66 | | { |
| 1 | 67 | | RemoveComponent(sceneId, entityId, componentId, -1, writeType); |
| 1 | 68 | | } |
| | 69 | |
|
| | 70 | | public void RemoveComponent(string sceneId, long entityId, int componentId, long minTimeStamp, ECSComponentWrite |
| | 71 | | { |
| 1 | 72 | | writeComponent(sceneId, entityId, componentId, null, minTimeStamp, writeType); |
| 1 | 73 | | } |
| | 74 | |
|
| | 75 | | public void Dispose() |
| | 76 | | { |
| 1 | 77 | | serializers.Clear(); |
| 1 | 78 | | writeComponent = null; |
| 1 | 79 | | } |
| | 80 | | } |
| | 81 | | } |