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.

45 lines
1.6KB

  1. namespace Svelto.ECS.Serialization
  2. {
  3. public class DefaultSerializer<T> : IComponentSerializer<T> where T : unmanaged, IEntityComponent
  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 (_type.GetProperties().Length > (ComponentBuilder<T>.HAS_EGID ? 1 : 0))
  17. throw new ECSException("serializable entity struct must be property less ".FastConcat(_type.FullName));
  18. }
  19. public uint size => SIZEOFT;
  20. public bool Serialize(in T value, ISerializationData serializationData)
  21. {
  22. DefaultSerializerUtils.CopyToByteArray(value, serializationData.data.ToArrayFast(out _),
  23. serializationData.dataPos);
  24. serializationData.dataPos += SIZEOFT;
  25. return true;
  26. }
  27. public bool Deserialize(ref T value, ISerializationData serializationData)
  28. {
  29. value = DefaultSerializerUtils.CopyFromByteArray<T>(serializationData.data.ToArrayFast(out _),
  30. serializationData.dataPos);
  31. serializationData.dataPos += SIZEOFT;
  32. return true;
  33. }
  34. }
  35. }