A stable modding interface between Techblox and mods https://mod.exmods.org/
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

53 行
2.1KB

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