From 7ba4295e22a414cccad473a5625527c73737b4db Mon Sep 17 00:00:00 2001 From: Anairkoen Schno Date: Fri, 10 Aug 2018 21:14:14 -0500 Subject: [PATCH] Added Umbra's CallNonStaticFunction as CallNonStaticMethod (and 2 overloads) --- IllusionPlugin/Utils/ReflectionUtil.cs | 102 ++++++++++++++++++++++++- 1 file changed, 98 insertions(+), 4 deletions(-) diff --git a/IllusionPlugin/Utils/ReflectionUtil.cs b/IllusionPlugin/Utils/ReflectionUtil.cs index c2f9cc94..cf8fdde1 100644 --- a/IllusionPlugin/Utils/ReflectionUtil.cs +++ b/IllusionPlugin/Utils/ReflectionUtil.cs @@ -57,7 +57,7 @@ namespace IllusionPlugin.Utils /// the return value public static object InvokePrivateMethod(this object obj, string methodName, params object[] methodParams) { - MethodInfo dynMethod = obj.GetType().GetMethod(methodName, BindingFlags.NonPublic | BindingFlags.Instance); + MethodInfo dynMethod = obj.GetType().GetMethod(methodName, BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance); return dynMethod.Invoke(obj, methodParams); } @@ -78,13 +78,39 @@ namespace IllusionPlugin.Utils /// Copies a component of type originalType to a component of overridingType on the destination GameObject. /// /// the original component - /// the original component's type /// the new component's type /// the destination GameObject - /// - public static Component CopyComponent(Component original, Type originalType, Type overridingType, GameObject destination) + /// overrides the source component type (for example, to a superclass) + /// the copied component + public static Component CopyComponent(this Component original, Type overridingType, GameObject destination, Type originalTypeOverride = null) { var copy = destination.AddComponent(overridingType); + var originalType = originalTypeOverride ?? original.GetType(); + + Type type = originalType; + while (type != typeof(MonoBehaviour)) + { + CopyForType(type, original, copy); + type = type.BaseType; + } + + return copy; + } + + /// + /// A generic version of CopyComponent. + /// + /// + /// the overriding type + /// the original component + /// the destination game object + /// overrides the source component type (for example, to a superclass) + /// the copied component + public static T CopyComponent(this Component original, GameObject destination, Type originalTypeOverride = null) + where T : Component + { + var copy = destination.AddComponent(); + var originalType = originalTypeOverride ?? original.GetType(); Type type = originalType; while (type != typeof(MonoBehaviour)) @@ -105,5 +131,73 @@ namespace IllusionPlugin.Utils fi.SetValue(destination, fi.GetValue(source)); } } + + /// + /// Calls an instance method on a type specified by functionClass and dependency. + /// + /// + /// the type name + /// the assembly the type is in + /// the name of the method to call + /// the type signature of the method + /// the method parameters + /// the result of the call + public static object CallNonStaticMethod(string functionClass, string dependency, string function, Type[] methodSig, params object[] parameters) + { + return CallNonStaticMethod(Type.GetType(string.Format("{0},{1}", functionClass, dependency)), function, methodSig, parameters); + } + + /// + /// Calls an instance method on a new object. + /// + /// the object type + /// the name of the method to call + /// the type signature + /// the parameters + /// the result of the call + public static object CallNonStaticMethod(this Type type, /*string functionClass, string dependency,*/ string function, Type[] methodSig, params object[] parameters) + { + //Type FunctionClass = Type.GetType(string.Format("{0},{1}", functionClass, dependency)); + if (type != null) + { + object instance = Activator.CreateInstance(type); + if (instance != null) + { + Type instType = instance.GetType(); + MethodInfo methodInfo = instType.GetMethod(function, methodSig); + if (methodInfo != null) + { + return methodInfo.Invoke(instance, parameters); + } + else + { + throw new Exception("Method not found"); + } + } + else + { + throw new Exception("Unable to instantiate object of type"); + } + } + else + { + throw new ArgumentNullException("type"); + } + } + + /// + /// Calls an instance method on a new object. + /// + /// + /// the return type + /// the object type + /// the name of the method to call + /// the type signature + /// the parameters + /// the result of the call + public static T CallNonStaticMethod(this Type type, string function, Type[] methodSig, params object[] parameters) + { + return (T)CallNonStaticMethod(type, function, methodSig, parameters); + } } }