Unofficial CardLife revival project, pronounced like "celery"
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.

Reflection.cs 4.9KB

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