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.

131 lines
5.5KB

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