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.9KB

  1. namespace Svelto.ECS.Serialization
  2. {
  3. public static class SerializerExt
  4. {
  5. public static bool SerializeSafe<T>
  6. (this IComponentSerializer<T> componentSerializer, in T value, ISerializationData serializationData)
  7. where T : unmanaged, IBaseEntityComponent
  8. {
  9. #if DEBUG && !PROFILE_SVELTO
  10. uint posBefore = serializationData.dataPos;
  11. #endif
  12. bool res = componentSerializer.Serialize(value, serializationData);
  13. #if DEBUG && !PROFILE_SVELTO
  14. // size == 0 is a special case when we don't know the size in advance
  15. if (componentSerializer.size != 0 && serializationData.dataPos != posBefore + componentSerializer.size)
  16. {
  17. throw new System.IndexOutOfRangeException(
  18. $"Size mismatch when serializing {typeof(T).FullName} using {componentSerializer.GetType().FullName}, "
  19. + $"expected offset {posBefore + componentSerializer.size}, got {serializationData.dataPos}");
  20. }
  21. #endif
  22. return res;
  23. }
  24. public static bool DeserializeSafe<T>
  25. (this IComponentSerializer<T> componentSerializer, ref T value, ISerializationData serializationData)
  26. where T : unmanaged, IBaseEntityComponent
  27. {
  28. #if DEBUG && !PROFILE_SVELTO
  29. uint posBefore = serializationData.dataPos;
  30. #endif
  31. bool res = componentSerializer.Deserialize(ref value, serializationData);
  32. #if DEBUG && !PROFILE_SVELTO
  33. if (componentSerializer.size != 0 && serializationData.dataPos != posBefore + componentSerializer.size)
  34. {
  35. throw new System.IndexOutOfRangeException(
  36. $"Size mismatch when deserializing {typeof(T).FullName} using {componentSerializer.GetType().FullName}, "
  37. + $"expected offset {posBefore + componentSerializer.size}, got {serializationData.dataPos}");
  38. }
  39. #endif
  40. return res;
  41. }
  42. }
  43. }