Mirror of Svelto.ECS because we're a fan of it
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

43 рядки
1.5KB

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