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.

124 lines
5.3KB

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