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.

42 lines
1.5KB

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