Unofficial CardLife revival project, pronounced like "celery"
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

Reflection.cs 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. using System;
  2. using System.Reflection;
  3. using HarmonyLib;
  4. using Svelto.DataStructures;
  5. using Svelto.ECS;
  6. namespace CLre.API.Utility
  7. {
  8. public static class Reflection
  9. {
  10. // useful function & method prototypes
  11. public delegate T Getter<T>();
  12. public delegate bool ExistsV1(EGID egid);
  13. public delegate bool ExistsV2(int id, int groupid);
  14. public delegate T QueryEntityViewV1<T>(EGID egid);
  15. public delegate T QueryEntityViewV2<T>(int id, ExclusiveGroup.ExclusiveGroupStruct groupid);
  16. public delegate ReadOnlyCollectionStruct<T> QueryEntityViews<T>(int group) where T : class, IEntityViewStruct;
  17. public delegate T[] QueryEntitiesV2<T>(int group, out int count) where T : IEntityStruct;
  18. public delegate T[] QueryEntitiesV1<T>(ExclusiveGroup.ExclusiveGroupStruct group, out int count) where T : IEntityStruct;
  19. // useful reflection functions
  20. public static TFuncProto BuildDelegate<TFuncProto>(MethodInfo method) where TFuncProto : Delegate
  21. {
  22. return (TFuncProto) Delegate.CreateDelegate(typeof(TFuncProto), method);
  23. }
  24. public static Delegate BuildDelegateRaw(MethodInfo method, Type TFuncProto)
  25. {
  26. return Delegate.CreateDelegate(TFuncProto, method);
  27. }
  28. public static TFuncProto BuildDelegate<TFuncProto>(MethodInfo method, object instance) where TFuncProto : Delegate
  29. {
  30. return (TFuncProto) Delegate.CreateDelegate(typeof(TFuncProto), instance, method, true);
  31. }
  32. public static Delegate BuildDelegateRaw(MethodInfo method, object instance, Type TFuncProto)
  33. {
  34. return Delegate.CreateDelegate(TFuncProto, instance, method, true);
  35. }
  36. public static TFuncProto MethodAsDelegate<TFuncProto>(Type class_, string methodName, Type[] parameters = null, Type[] generics = null, object instance = null) where TFuncProto : Delegate
  37. {
  38. MethodInfo method = AccessTools.Method(class_, methodName, parameters, generics);
  39. if (instance != null)
  40. {
  41. return BuildDelegate<TFuncProto>(method, instance);
  42. }
  43. return BuildDelegate<TFuncProto>(method);
  44. }
  45. public static Delegate MethodAsDelegateRaw(Type class_, Type TFuncProto, string methodName, Type[] parameters = null, Type[] generics = null, object instance = null)
  46. {
  47. MethodInfo method = AccessTools.Method(class_, methodName, parameters, generics);
  48. if (instance != null)
  49. {
  50. return BuildDelegateRaw(method, instance, TFuncProto);
  51. }
  52. return BuildDelegateRaw(method, TFuncProto);
  53. }
  54. public static TFuncProto MethodAsDelegate<TFuncProto>(string typeColonName, Type[] parameters = null, Type[] generics = null, object instance = null) where TFuncProto : Delegate
  55. {
  56. MethodInfo method = AccessTools.Method(typeColonName, parameters, generics);
  57. if (instance != null)
  58. {
  59. return BuildDelegate<TFuncProto>(method, instance);
  60. }
  61. return BuildDelegate<TFuncProto>(method);
  62. }
  63. public static Delegate MethodAsDelegateRaw(string typeColonName, Type TFuncProto, Type[] parameters = null, Type[] generics = null, object instance = null)
  64. {
  65. MethodInfo method = AccessTools.Method(typeColonName, parameters, generics);
  66. if (instance != null)
  67. {
  68. return BuildDelegateRaw(method, instance, TFuncProto);
  69. }
  70. return BuildDelegateRaw(method, TFuncProto);
  71. }
  72. public static PropertyInfo GetIndexer(this Type type, Type[] arguments = null, BindingFlags bindingFlags = BindingFlags.GetField | BindingFlags.GetProperty | BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.SetField | BindingFlags.SetProperty | BindingFlags.Static)
  73. {
  74. foreach (PropertyInfo p in type.GetProperties(bindingFlags))
  75. {
  76. if (arguments == null && p.GetIndexParameters().Length != 0) return p;
  77. if (arguments != null)
  78. {
  79. uint count = 0;
  80. foreach (ParameterInfo param in p.GetIndexParameters())
  81. {
  82. if (param.ParameterType != arguments[count])
  83. {
  84. break;
  85. }
  86. count++;
  87. }
  88. if (count == arguments.Length)
  89. {
  90. return p;
  91. }
  92. }
  93. }
  94. return null;
  95. }
  96. }
  97. }