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.2KB

  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. public delegate void INetMsgServerSender_SendMessage<T>(NetworkDispatcherCode code, ref T value, int playerId) where T : struct, ISerializedNetData;
  23. public delegate void INetMsgServerListener_RegisterListener<T>(NetworkDispatcherCode code, NetCBServer<T> proc) where T: struct, ISerializedNetData;
  24. // useful reflection functions
  25. public static TFuncProto BuildDelegate<TFuncProto>(MethodInfo method) where TFuncProto : Delegate
  26. {
  27. return (TFuncProto) Delegate.CreateDelegate(typeof(TFuncProto), method);
  28. }
  29. public static Delegate BuildDelegateRaw(MethodInfo method, Type TFuncProto)
  30. {
  31. return Delegate.CreateDelegate(TFuncProto, method);
  32. }
  33. public static TFuncProto BuildDelegate<TFuncProto>(MethodInfo method, object instance) where TFuncProto : Delegate
  34. {
  35. return (TFuncProto) Delegate.CreateDelegate(typeof(TFuncProto), instance, method, true);
  36. }
  37. public static Delegate BuildDelegateRaw(MethodInfo method, object instance, Type TFuncProto)
  38. {
  39. return Delegate.CreateDelegate(TFuncProto, instance, method, true);
  40. }
  41. public static TFuncProto MethodAsDelegate<TFuncProto>(Type class_, string methodName, Type[] parameters = null, Type[] generics = null, object instance = null) where TFuncProto : Delegate
  42. {
  43. MethodInfo method = AccessTools.Method(class_, methodName, parameters, generics);
  44. if (instance != null)
  45. {
  46. return BuildDelegate<TFuncProto>(method, instance);
  47. }
  48. return BuildDelegate<TFuncProto>(method);
  49. }
  50. public static Delegate MethodAsDelegateRaw(Type class_, Type TFuncProto, string methodName, Type[] parameters = null, Type[] generics = null, object instance = null)
  51. {
  52. MethodInfo method = AccessTools.Method(class_, methodName, parameters, generics);
  53. if (instance != null)
  54. {
  55. return BuildDelegateRaw(method, instance, TFuncProto);
  56. }
  57. return BuildDelegateRaw(method, TFuncProto);
  58. }
  59. public static TFuncProto MethodAsDelegate<TFuncProto>(string typeColonName, Type[] parameters = null, Type[] generics = null, object instance = null) where TFuncProto : Delegate
  60. {
  61. MethodInfo method = AccessTools.Method(typeColonName, parameters, generics);
  62. if (instance != null)
  63. {
  64. return BuildDelegate<TFuncProto>(method, instance);
  65. }
  66. return BuildDelegate<TFuncProto>(method);
  67. }
  68. public static Delegate MethodAsDelegateRaw(string typeColonName, Type TFuncProto, Type[] parameters = null, Type[] generics = null, object instance = null)
  69. {
  70. MethodInfo method = AccessTools.Method(typeColonName, parameters, generics);
  71. if (instance != null)
  72. {
  73. return BuildDelegateRaw(method, instance, TFuncProto);
  74. }
  75. return BuildDelegateRaw(method, TFuncProto);
  76. }
  77. 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)
  78. {
  79. foreach (PropertyInfo p in type.GetProperties(bindingFlags))
  80. {
  81. if (arguments == null && p.GetIndexParameters().Length != 0) return p;
  82. if (arguments != null)
  83. {
  84. uint count = 0;
  85. foreach (ParameterInfo param in p.GetIndexParameters())
  86. {
  87. if (param.ParameterType != arguments[count])
  88. {
  89. break;
  90. }
  91. count++;
  92. }
  93. if (count == arguments.Length)
  94. {
  95. return p;
  96. }
  97. }
  98. }
  99. return null;
  100. }
  101. }
  102. }