| | 1 | | using DCL.Controllers; |
| | 2 | | using DCL.CRDT; |
| | 3 | | using DCL.Models; |
| | 4 | | using System; |
| | 5 | | using System.Collections.Generic; |
| | 6 | | using UnityEngine; |
| | 7 | |
|
| | 8 | | namespace DCL.ECSRuntime |
| | 9 | | { |
| | 10 | | public class ECSComponentWriter : IECSComponentWriter |
| | 11 | | { |
| | 12 | | private abstract class Serializer |
| | 13 | | { |
| | 14 | | private readonly Type targetType; |
| | 15 | |
|
| 38 | 16 | | protected Serializer(Type targetType) |
| | 17 | | { |
| 38 | 18 | | this.targetType = targetType; |
| 38 | 19 | | } |
| | 20 | |
|
| | 21 | | public bool CheckType(Type expectedType) => |
| 1 | 22 | | targetType == expectedType; |
| | 23 | |
|
| | 24 | | public abstract byte[] Serialize(object obj); |
| | 25 | | } |
| | 26 | |
|
| | 27 | | private class Serializer<T> : Serializer |
| | 28 | | { |
| | 29 | | private readonly Func<T, byte[]> serialize; |
| | 30 | |
|
| | 31 | | public Serializer(Func<T, byte[]> serialize) : base(typeof(T)) |
| | 32 | | { |
| | 33 | | this.serialize = serialize; |
| | 34 | | } |
| | 35 | |
|
| | 36 | | public override byte[] Serialize(object obj) => |
| | 37 | | serialize((T)obj); |
| | 38 | | } |
| | 39 | |
|
| | 40 | | public delegate void WriteComponent(int sceneNumber, long entityId, int componentId, byte[] data, |
| | 41 | | int minTimeStamp, ECSComponentWriteType writeType, CrdtMessageType messageType); |
| | 42 | |
|
| 3 | 43 | | private readonly Dictionary<int, Serializer> serializers = new (); |
| | 44 | | private WriteComponent writeComponent; |
| | 45 | |
|
| 2 | 46 | | public ECSComponentWriter(WriteComponent writeComponent) |
| | 47 | | { |
| 2 | 48 | | this.writeComponent = writeComponent; |
| 2 | 49 | | } |
| | 50 | |
|
| 2 | 51 | | public ECSComponentWriter() { } |
| | 52 | |
|
| | 53 | | public void AddOrReplaceComponentSerializer<T>(int componentId, Func<T, byte[]> serializer) |
| | 54 | | { |
| 38 | 55 | | serializers[componentId] = new Serializer<T>(serializer); |
| 38 | 56 | | } |
| | 57 | |
|
| | 58 | | public void RemoveComponentSerializer(int componentId) |
| | 59 | | { |
| 36 | 60 | | serializers.Remove(componentId); |
| 36 | 61 | | } |
| | 62 | |
|
| | 63 | | public void PutComponent<T>(IParcelScene scene, IDCLEntity entity, int componentId, T model, ECSComponentWriteTy |
| | 64 | | { |
| 1 | 65 | | PutComponent(scene.sceneData.sceneNumber, entity.entityId, componentId, model, -1, writeType); |
| 1 | 66 | | } |
| | 67 | |
|
| | 68 | | public void PutComponent<T>(int sceneNumber, long entityId, int componentId, T model, ECSComponentWriteType writ |
| | 69 | | { |
| 0 | 70 | | PutComponent(sceneNumber, entityId, componentId, model, -1, writeType); |
| 0 | 71 | | } |
| | 72 | |
|
| | 73 | | public void PutComponent<T>(int sceneNumber, long entityId, int componentId, T model, int minTimeStamp, |
| | 74 | | ECSComponentWriteType writeType) |
| | 75 | | { |
| 1 | 76 | | PutComponent(typeof(T), sceneNumber, entityId, componentId, model, minTimeStamp, writeType); |
| 1 | 77 | | } |
| | 78 | |
|
| | 79 | | public void PutComponent(Type componentType, int sceneNumber, long entityId, int componentId, object model, |
| | 80 | | ECSComponentWriteType writeType) |
| | 81 | | { |
| 0 | 82 | | PutComponent(componentType, sceneNumber, entityId, componentId, model, -1, writeType); |
| 0 | 83 | | } |
| | 84 | |
|
| | 85 | | public void PutComponent(Type componentType, int sceneNumber, long entityId, int componentId, object model, |
| | 86 | | int minTimeStamp, ECSComponentWriteType writeType) |
| | 87 | | { |
| 1 | 88 | | if (!serializers.TryGetValue(componentId, out Serializer serializer)) |
| | 89 | | { |
| 0 | 90 | | Debug.LogError($"Trying to write component but no serializer was found for {componentId}"); |
| 0 | 91 | | return; |
| | 92 | | } |
| | 93 | |
|
| 1 | 94 | | if (serializer.CheckType(componentType)) |
| | 95 | | { |
| 1 | 96 | | writeComponent(sceneNumber, entityId, componentId, serializer.Serialize(model), minTimeStamp, writeType, |
| | 97 | | } |
| | 98 | | else |
| | 99 | | { |
| 0 | 100 | | Debug.LogError($"Trying to write component but serializer for {componentId} does not match {componentTyp |
| | 101 | | } |
| 0 | 102 | | } |
| | 103 | |
|
| | 104 | | public void RemoveComponent(int sceneNumber, long entityId, int componentId, ECSComponentWriteType writeType) |
| | 105 | | { |
| 1 | 106 | | RemoveComponent(sceneNumber, entityId, componentId, -1, writeType); |
| 1 | 107 | | } |
| | 108 | |
|
| | 109 | | public void RemoveComponent(int sceneNumber, long entityId, int componentId, int minTimeStamp, ECSComponentWrite |
| | 110 | | { |
| 1 | 111 | | writeComponent(sceneNumber, entityId, componentId, null, minTimeStamp, writeType, CrdtMessageType.DELETE_COM |
| 1 | 112 | | } |
| | 113 | |
|
| | 114 | | public void AppendComponent(Type componentType, int sceneNumber, long entityId, int componentId, object model, |
| | 115 | | ECSComponentWriteType writeType) |
| | 116 | | { |
| 0 | 117 | | if (!serializers.TryGetValue(componentId, out Serializer serializer)) |
| | 118 | | { |
| 0 | 119 | | Debug.LogError($"Trying to write an append component but no serializer was found for {componentId}"); |
| 0 | 120 | | return; |
| | 121 | | } |
| | 122 | |
|
| 0 | 123 | | if (serializer.CheckType(componentType)) |
| | 124 | | { |
| 0 | 125 | | writeComponent(sceneNumber, entityId, componentId, serializer.Serialize(model), -1, writeType, CrdtMessa |
| | 126 | | } |
| | 127 | | else |
| | 128 | | { |
| 0 | 129 | | Debug.LogError($"Trying to write component but serializer for {componentId} does not match {componentTyp |
| | 130 | | } |
| 0 | 131 | | } |
| | 132 | |
|
| | 133 | | public void AppendComponent<T>(int sceneNumber, long entityId, int componentId, T model, ECSComponentWriteType w |
| | 134 | | { |
| 0 | 135 | | AppendComponent(typeof(T), sceneNumber, entityId, componentId, model, writeType); |
| 0 | 136 | | } |
| | 137 | |
|
| | 138 | | public void Dispose() |
| | 139 | | { |
| 1 | 140 | | serializers.Clear(); |
| 1 | 141 | | writeComponent = null; |
| 1 | 142 | | } |
| | 143 | | } |
| | 144 | | } |