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

  1. using System;
  2. using Svelto.ECS;
  3. public static class DefaultSerializerUtils
  4. {
  5. public static unsafe void CopyToByteArray<T>(in T src, byte[] data, uint offsetDst) where T : unmanaged, IEntityStruct
  6. {
  7. fixed (void* dstPtr = data)
  8. {
  9. void* dstOffsetPtr;
  10. if (IntPtr.Size == sizeof(int))
  11. {
  12. dstOffsetPtr = (void*) (((IntPtr) dstPtr).ToInt32() + ((IntPtr) offsetDst).ToInt32());
  13. }
  14. else
  15. {
  16. dstOffsetPtr = (void*) (((IntPtr) dstPtr).ToInt64() + ((IntPtr) offsetDst).ToInt64());
  17. }
  18. *(T*) dstOffsetPtr = src;
  19. }
  20. }
  21. public static unsafe T CopyFromByteArray<T>(byte[] data, uint offsetSrc) where T : unmanaged, IEntityStruct
  22. {
  23. T dst = new T();
  24. void* dstPtr = &dst;
  25. fixed (void* srcPtr = data)
  26. {
  27. void* srcOffsetPtr;
  28. if (IntPtr.Size == sizeof(int))
  29. {
  30. srcOffsetPtr = (void*) (((IntPtr) srcPtr).ToInt32() + ((IntPtr) offsetSrc).ToInt32());
  31. }
  32. else
  33. {
  34. srcOffsetPtr = (void*) (((IntPtr) srcPtr).ToInt64() + ((IntPtr) offsetSrc).ToInt64());
  35. }
  36. *(T*) dstPtr = *(T*) srcOffsetPtr;
  37. }
  38. return dst;
  39. }
  40. }