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

  1. using System;
  2. using System.Linq.Expressions;
  3. using System.Reflection;
  4. namespace Svelto.ECS.Internal
  5. {
  6. static class SetEGIDWithoutBoxing<T> where T : struct, IEntityStruct
  7. {
  8. internal delegate void ActionCast(ref T target, EGID egid);
  9. public static readonly ActionCast SetIDWithoutBoxing = MakeSetter();
  10. static ActionCast MakeSetter()
  11. {
  12. if (EntityBuilder<T>.HAS_EGID)
  13. {
  14. #if !ENABLE_IL2CPP
  15. Type myTypeA = typeof(T);
  16. PropertyInfo myFieldInfo = myTypeA.GetProperty("ID");
  17. ParameterExpression targetExp = Expression.Parameter(typeof(T).MakeByRefType(), "target");
  18. ParameterExpression valueExp = Expression.Parameter(typeof(EGID), "value");
  19. MemberExpression fieldExp = Expression.Property(targetExp, myFieldInfo);
  20. BinaryExpression assignExp = Expression.Assign(fieldExp, valueExp);
  21. var setter = Expression.Lambda<ActionCast>(assignExp, targetExp, valueExp).Compile();
  22. return setter;
  23. #else
  24. return (ref T target, EGID value) =>
  25. {
  26. var needEgid = (target as INeedEGID);
  27. needEgid.ID = value;
  28. target = (T) needEgid;
  29. };
  30. #endif
  31. }
  32. return null;
  33. }
  34. public static void Warmup()
  35. {
  36. }
  37. }
  38. }