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.

70 lines
2.6KB

  1. #if SLOW_SVELTO_SUBMISSION
  2. namespace Svelto.ECS.Internal
  3. {
  4. delegate void SetEGIDWithoutBoxingActionCast<T>(ref T target, EGID egid) where T : struct, _IInternalEntityComponent;
  5. delegate void SetReferenceWithoutBoxingActionCast<T>(ref T target, EntityReference egid) where T : struct, _IInternalEntityComponent;
  6. static class SetEGIDWithoutBoxing<T> where T : struct, _IInternalEntityComponent
  7. {
  8. public static readonly SetEGIDWithoutBoxingActionCast<T> SetIDWithoutBoxing = MakeSetter();
  9. public static readonly SetReferenceWithoutBoxingActionCast<T> SetRefWithoutBoxing = MakeSetterReference();
  10. public static void Warmup() { }
  11. static SetEGIDWithoutBoxingActionCast<T> MakeSetter()
  12. {
  13. if (ComponentBuilder<T>.HAS_EGID)
  14. {
  15. #if !ENABLE_IL2CPP
  16. var method = typeof(Trick).GetMethod(nameof(Trick.SetEGIDImpl)).MakeGenericMethod(typeof(T));
  17. return (SetEGIDWithoutBoxingActionCast<T>) System.Delegate.CreateDelegate(
  18. typeof(SetEGIDWithoutBoxingActionCast<T>), method);
  19. #else
  20. return (ref T target, EGID egid) =>
  21. {
  22. var needEgid = (target as INeedEGID);
  23. needEgid.ID = egid;
  24. target = (T) needEgid;
  25. };
  26. #endif
  27. }
  28. return null;
  29. }
  30. static SetReferenceWithoutBoxingActionCast<T> MakeSetterReference()
  31. {
  32. if (ComponentBuilder<T>.HAS_REFERENCE)
  33. {
  34. #if !ENABLE_IL2CPP
  35. var method = typeof(Trick).GetMethod(nameof(Trick.SetEGIDImplRef)).MakeGenericMethod(typeof(T));
  36. return (SetReferenceWithoutBoxingActionCast<T>) System.Delegate.CreateDelegate(
  37. typeof(SetReferenceWithoutBoxingActionCast<T>), method);
  38. #else
  39. return (ref T target, EntityReference reference) =>
  40. {
  41. var needEgid = (target as INeedEntityReference);
  42. needEgid.selfReference = reference;
  43. target = (T) needEgid;
  44. };
  45. #endif
  46. }
  47. return null;
  48. }
  49. static class Trick
  50. {
  51. public static void SetEGIDImpl<U>(ref U target, EGID egid) where U : struct, INeedEGID
  52. {
  53. target.ID = egid;
  54. }
  55. public static void SetEGIDImplRef<U>(ref U target, EntityReference reference) where U : struct, INeedEntityReference
  56. {
  57. target.selfReference = reference;
  58. }
  59. }
  60. }
  61. }
  62. #endif