ReflectionUtil.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Reflection;
  5. using UnityEngine;
  6. namespace SongCore.Utilities
  7. {
  8. public static class ReflectionUtil
  9. {
  10. private const BindingFlags _allBindingFlags = BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic;
  11. //Sets the value of a (static?) field in object "obj" with name "fieldName"
  12. public static void SetField(this object obj, string fieldName, object value)
  13. {
  14. (obj is Type ? (Type)obj : obj.GetType())
  15. .GetField(fieldName, _allBindingFlags)
  16. .SetValue(obj, value);
  17. }
  18. //Gets the value of a (static?) field in object "obj" with name "fieldName"
  19. public static object GetField(this object obj, string fieldName)
  20. {
  21. return (obj is Type ? (Type)obj : obj.GetType())
  22. .GetField(fieldName, _allBindingFlags)
  23. .GetValue(obj);
  24. }
  25. //Gets the value of a (static?) field in object "obj" with name "fieldName" (TYPED)
  26. public static T GetField<T>(this object obj, string fieldName) => (T)GetField(obj, fieldName);
  27. //Sets the value of a (static?) Property specified by the object "obj" and the name "propertyName"
  28. public static void SetProperty(this object obj, string propertyName, object value)
  29. {
  30. (obj is Type ? (Type)obj : obj.GetType())
  31. .GetProperty(propertyName, _allBindingFlags)
  32. .SetValue(obj, value, null);
  33. }
  34. //Gets the value of a (static?) Property specified by the object "obj" and the name "propertyName"
  35. public static object GetProperty(this object obj, string propertyName)
  36. {
  37. return (obj is Type ? (Type)obj : obj.GetType())
  38. .GetProperty(propertyName, _allBindingFlags)
  39. .GetValue(obj);
  40. }
  41. //Gets the value of a (static?) Property specified by the object "obj" and the name "propertyName" (TYPED)
  42. public static T GetProperty<T>(this object obj, string propertyName) => (T)GetProperty(obj, propertyName);
  43. //Invokes a (static?) private method with name "methodName" and params "methodParams", returns an object of the specified type
  44. public static T InvokeMethod<T>(this object obj, string methodName, params object[] methodParams) => (T)InvokeMethod(obj, methodName, methodParams);
  45. //Invokes a (static?) private method with name "methodName" and params "methodParams"
  46. public static object InvokeMethod(this object obj, string methodName, params object[] methodParams)
  47. {
  48. return (obj is Type ? (Type)obj : obj.GetType())
  49. .GetMethod(methodName, _allBindingFlags)
  50. .Invoke(obj, methodParams);
  51. }
  52. //Returns a constructor with the specified parameters to the specified type or object
  53. public static object InvokeConstructor(this object obj, params object[] constructorParams)
  54. {
  55. Type[] types = new Type[constructorParams.Length];
  56. for (int i = 0; i < constructorParams.Length; i++) types[i] = constructorParams[i].GetType();
  57. return (obj is Type ? (Type)obj : obj.GetType())
  58. .GetConstructor(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, types, null)
  59. .Invoke(constructorParams);
  60. }
  61. //Returns a Type object which can be used to invoke static methods with the above helpers
  62. public static Type GetStaticType(string clazz)
  63. {
  64. return Type.GetType(clazz);
  65. }
  66. //Returns a list (of strings) of the names of all loaded assemblies
  67. public static IEnumerable<Assembly> ListLoadedAssemblies()
  68. {
  69. return AppDomain.CurrentDomain.GetAssemblies();
  70. }
  71. //Returns a list of all loaded namespaces
  72. //TODO: Check up on time complexity here, could potentially be parallelized
  73. public static IEnumerable<string> ListNamespacesInAssembly(Assembly assembly)
  74. {
  75. IEnumerable<string> ret = Enumerable.Empty<string>();
  76. ret = ret.Concat(assembly.GetTypes()
  77. .Select(t => t.Namespace)
  78. .Distinct()
  79. .Where(n => n != null));
  80. return ret.Distinct();
  81. }
  82. //Returns a list of classes in a namespace
  83. //TODO: Check up on time complexity here, could potentially be parallelized
  84. public static IEnumerable<string> ListClassesInNamespace(string ns)
  85. {
  86. //For each loaded assembly
  87. foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
  88. {
  89. //If the assembly contains the desired namespace
  90. if (assembly.GetTypes().Where(t => t.Namespace == ns).Any())
  91. {
  92. //Select the types we want from the namespace and return them
  93. return assembly.GetTypes()
  94. .Where(t => t.IsClass)
  95. .Select(t => t.Name);
  96. }
  97. }
  98. return null;
  99. //Code to list reflectable classes
  100. /*
  101. ReflectionUtil.ListLoadedAssemblies().ToList().ForEach(x => {
  102. if (x.GetName().Name == "BeatSaberMultiplayer")
  103. {
  104. Logger.Success($"ASSEMBLY: {x.GetName().Name}");
  105. ReflectionUtil.ListNamespacesInAssembly(x).ToList().ForEach(y =>
  106. {
  107. Logger.Warning($"NAMESPACE: {y}");
  108. ReflectionUtil.ListClassesInNamespace(y).ToList().ForEach(z =>
  109. {
  110. Logger.Warning($"CLASS: {z} : {((ReflectionUtil.GetStaticType(y + "." + z + "," + x) != null) ? "REFLECTABLE" : "NOT")}");
  111. });
  112. });
  113. }
  114. });
  115. */
  116. }
  117. //(Created by taz?) Copies a component to a destination object, keeping all its field values?
  118. public static Behaviour CopyComponent(Behaviour original, Type originalType, Type overridingType, GameObject destination)
  119. {
  120. Behaviour copy = null;
  121. try
  122. {
  123. copy = destination.AddComponent(overridingType) as Behaviour;
  124. }
  125. catch (Exception)
  126. {
  127. }
  128. copy.enabled = false;
  129. //Copy types of super classes as well as our class
  130. Type type = originalType;
  131. while (type != typeof(MonoBehaviour))
  132. {
  133. CopyForType(type, original, copy);
  134. type = type.BaseType;
  135. }
  136. copy.enabled = true;
  137. return copy;
  138. }
  139. public static void SetPrivateField(this object obj, string fieldName, object value)
  140. {
  141. var field = obj.GetType().GetField(fieldName, BindingFlags.Instance | BindingFlags.NonPublic);
  142. field.SetValue(obj, value);
  143. }
  144. public static T GetPrivateField<T>(this object obj, string fieldName)
  145. {
  146. var field = obj.GetType().GetField(fieldName, BindingFlags.Instance | BindingFlags.NonPublic);
  147. var value = field.GetValue(obj);
  148. return (T)value;
  149. }
  150. public static object GetPrivateField(Type type, object obj, string fieldName)
  151. {
  152. var field = obj.GetType().GetField(fieldName, BindingFlags.Instance | BindingFlags.NonPublic);
  153. var value = field.GetValue(obj);
  154. return value;
  155. }
  156. public static void InvokePrivateMethod(this object obj, string methodName, object[] methodParams)
  157. {
  158. var method = obj.GetType().GetMethod(methodName, BindingFlags.Instance | BindingFlags.NonPublic);
  159. method.Invoke(obj, methodParams);
  160. }
  161. //(Created by taz?) Copies a Component of Type type, and all its fields
  162. private static void CopyForType(Type type, Component source, Component destination)
  163. {
  164. FieldInfo[] myObjectFields = type.GetFields(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance | BindingFlags.GetField | BindingFlags.SetField);
  165. foreach (FieldInfo fi in myObjectFields)
  166. {
  167. fi.SetValue(destination, fi.GetValue(source));
  168. }
  169. }
  170. }
  171. }