Mirror of Svelto.ECS because we're a fan of it
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

43 lines
1.3KB

  1. using System;
  2. namespace Svelto.ECS.Serialization
  3. {
  4. public class ComposedSerializer<T, X, Y> : ISerializer<T>
  5. where T : unmanaged, IEntityStruct
  6. where X : class, ISerializer<T>, new()
  7. where Y : class, ISerializer<T>, new()
  8. {
  9. public ComposedSerializer()
  10. {
  11. _serializers = new ISerializer<T>[2];
  12. _serializers[0] = new X();
  13. _serializers[1] = new Y();
  14. }
  15. public bool Serialize(in T value, ISerializationData serializationData)
  16. {
  17. foreach (ISerializer<T> s in _serializers)
  18. {
  19. serializationData.data.ExpandBy(s.size);
  20. if (s.SerializeSafe(value, serializationData))
  21. return true;
  22. }
  23. throw new Exception($"ComposedSerializer for {typeof(T)} did not serialize any data!");
  24. }
  25. public bool Deserialize(ref T value, ISerializationData serializationData)
  26. {
  27. foreach (ISerializer<T> s in _serializers)
  28. {
  29. if (s.DeserializeSafe(ref value, serializationData))
  30. return true;
  31. }
  32. throw new Exception($"ComposedSerializer for {typeof(T)} did not deserialize any data!");
  33. }
  34. public uint size => 0;
  35. ISerializer<T>[] _serializers;
  36. }
  37. }