| | 1 | | using DCL.ECS7.ComponentWrapper.Generic; |
| | 2 | | using Google.Protobuf; |
| | 3 | | using System; |
| | 4 | | using System.IO; |
| | 5 | |
|
| | 6 | | namespace DCL.ECS7.ComponentWrapper |
| | 7 | | { |
| | 8 | | public record ProtobufWrappedComponent<T> : IWrappedComponent<T> where T: class, IMessage<T> |
| | 9 | | { |
| | 10 | | private readonly T model; |
| | 11 | |
|
| 125 | 12 | | public T Model => model; |
| | 13 | |
|
| | 14 | | public static implicit operator T(ProtobufWrappedComponent<T> wrapped) => |
| 0 | 15 | | wrapped.model; |
| | 16 | |
|
| 80 | 17 | | public ProtobufWrappedComponent(T model) |
| | 18 | | { |
| 80 | 19 | | this.model = model; |
| 80 | 20 | | } |
| | 21 | |
|
| | 22 | | public void SerializeTo(MemoryStream buffer, CodedOutputStream stream) |
| | 23 | | { |
| 0 | 24 | | buffer.SetLength(0); |
| 0 | 25 | | model.WriteTo(stream); |
| 0 | 26 | | stream.Flush(); |
| 0 | 27 | | } |
| | 28 | |
|
| | 29 | | public void DeserializeFrom(ReadOnlySpan<byte> bytes) |
| | 30 | | { |
| 0 | 31 | | ClearFields(); |
| 0 | 32 | | model.MergeFrom(bytes); |
| 0 | 33 | | } |
| | 34 | |
|
| | 35 | | public void ClearFields() |
| | 36 | | { |
| 3 | 37 | | var fields = model.Descriptor.Fields.InDeclarationOrder(); |
| | 38 | |
|
| 42 | 39 | | for (int i = 0; i < fields.Count; i++) |
| | 40 | | { |
| 18 | 41 | | fields[i].Accessor.Clear(model); |
| | 42 | | } |
| 3 | 43 | | } |
| | 44 | | } |
| | 45 | | } |