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.

46 lines
1.6KB

  1. namespace Svelto.ECS.Serialization
  2. {
  3. public class DefaultSerializer<T> : IComponentSerializer<T> where T : unmanaged, IBaseEntityComponent
  4. {
  5. static readonly uint SIZEOFT = SerializableComponentBuilder<T>.SIZE;
  6. static DefaultSerializer()
  7. {
  8. var _type = typeof(T);
  9. foreach (var field in _type.GetFields())
  10. {
  11. var fieldFieldType = field.FieldType;
  12. if (fieldFieldType.ContainsCustomAttribute(typeof(DoNotSerializeAttribute)) &&
  13. field.IsPrivate == false)
  14. throw new ECSException($"field cannot be serialised {fieldFieldType} in {_type.FullName}");
  15. }
  16. #if SLOW_SVELTO_SUBMISSION
  17. if (_type.GetProperties().Length > (ComponentBuilder<T>.HAS_EGID ? 1 : 0))
  18. throw new ECSException("serializable entity struct must be property less ".FastConcat(_type.FullName));
  19. #endif
  20. }
  21. public uint size => SIZEOFT;
  22. public bool Serialize(in T value, ISerializationData serializationData)
  23. {
  24. DefaultSerializerUtils.CopyToByteArray(value, serializationData.data.ToArrayFast(out _),
  25. serializationData.dataPos);
  26. serializationData.dataPos += SIZEOFT;
  27. return true;
  28. }
  29. public bool Deserialize(ref T value, ISerializationData serializationData)
  30. {
  31. value = DefaultSerializerUtils.CopyFromByteArray<T>(serializationData.data.ToArrayFast(out _),
  32. serializationData.dataPos);
  33. serializationData.dataPos += SIZEOFT;
  34. return true;
  35. }
  36. }
  37. }