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.

127 lines
5.4KB

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