Mirror of Svelto.ECS because we're a fan of it
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

46 linhas
2.0KB

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