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.

44 lines
1.5KB

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