A stable modding interface between Techblox and mods https://mod.exmods.org/
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.

52 lines
2.0KB

  1. using System;
  2. using System.Linq.Expressions;
  3. using Svelto.DataStructures;
  4. using Svelto.ECS;
  5. using Svelto.ECS.Internal;
  6. namespace GamecraftModdingAPI.Blocks
  7. {
  8. public partial class BlockEngine
  9. {
  10. /// <summary>
  11. /// Holds information needed to construct a component initializer
  12. /// </summary>
  13. internal struct BlockInitData
  14. {
  15. public FasterDictionary<RefWrapperType, ITypeSafeDictionary> Group;
  16. }
  17. internal delegate FasterDictionary<RefWrapperType, ITypeSafeDictionary> GetInitGroup(
  18. EntityComponentInitializer initializer);
  19. /// <summary>
  20. /// Accesses the group field of the initializer
  21. /// </summary>
  22. internal GetInitGroup InitGroup = CreateAccessor<GetInitGroup>("_group");
  23. //https://stackoverflow.com/questions/55878525/unit-testing-ref-structs-with-private-fields-via-reflection
  24. internal static TDelegate CreateAccessor<TDelegate>(string memberName) where TDelegate : Delegate
  25. {
  26. var invokeMethod = typeof(TDelegate).GetMethod("Invoke");
  27. if (invokeMethod == null)
  28. throw new InvalidOperationException($"{typeof(TDelegate)} signature could not be determined.");
  29. var delegateParameters = invokeMethod.GetParameters();
  30. if (delegateParameters.Length != 1)
  31. throw new InvalidOperationException("Delegate must have a single parameter.");
  32. var paramType = delegateParameters[0].ParameterType;
  33. var objParam = Expression.Parameter(paramType, "obj");
  34. var memberExpr = Expression.PropertyOrField(objParam, memberName);
  35. Expression returnExpr = memberExpr;
  36. if (invokeMethod.ReturnType != memberExpr.Type)
  37. returnExpr = Expression.ConvertChecked(memberExpr, invokeMethod.ReturnType);
  38. var lambda =
  39. Expression.Lambda<TDelegate>(returnExpr, $"Access{paramType.Name}_{memberName}", new[] {objParam});
  40. return lambda.Compile();
  41. }
  42. }
  43. }