Example #1
0
        private void Awake()
        {
            try
            {
                Debug.Log($"Creating a scene controller of type {SceneControllerClass}");

                gameObject.SetActive(false);

                Type controllerType = CoreUtils.GetLoadedTypes()
                                      .Where(t => t.Name == SceneControllerClass)
                                      //.Where(t => t.Name.EndsWith(SceneControllerClass))
                                      .Single();
                MonoBehaviour controller = (MonoBehaviour)gameObject.AddComponent(controllerType);

                ProxyUtils.SetProxyFields(this, controller); //this call does 90% of the magic

                //fires awake, probably
                gameObject.SetActive(true);

                //it will also fire OnEnable and OnDisable
                //that _shouldn't_ be a problem but if we need to we can re-root the world
            }
            catch (Exception e)
            {
                Debug.LogError($"Error setting up proxied scene controller {e.GetType().Name}");
                Debug.LogException(e);
            }
            finally
            {
                gameObject.SetActive(true);
            }
        }
Example #2
0
        /// <summary>
        /// Invokes a static method on a type
        /// </summary>
        public static object InvokeStaticProxiedEx(string typeName, string methodName, InvokeStaticProxyOptions options, params object[] args)
        {
            if (args == null)
            {
                args = new object[0];
            }

            IEnumerable <Type> types;

            if (options.UseAddonTypes)
            {
                types = CoreUtils.GetLoadedTypes();
            }
            else
            {
                types = CCBase.BaseGameTypes;
            }

            Type type = types
                        .Where(t => t.FullName == typeName)
                        .Single();

            MethodInfo method;

            if (options.MatchParameterTypes)
            {
                var signature = options.ParameterMatchTypes != null?options.ParameterMatchTypes.ToArray() : args.Select(a => a.GetType()).ToArray();

                method = type.GetMethod(methodName, BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic, null, signature, null);
            }
            else
            {
                method = type.GetMethods(BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic)
                         .Where(m => m.Name == methodName)
                         .Where(m => m.GetParameters().Length == args.Length)
                         .Single();
            }

            object[] coercedArgs = args;
            if (options.CoerceInputTypes)
            {
                var mParams = method.GetParameters();
                for (int i = 0; i < args.Length; i++)
                {
                    coercedArgs[i] = TypeUtils.CoerceValue(args[i], mParams[i].ParameterType);
                }
            }

            object result = method.Invoke(null, coercedArgs);

            if (options.OutputType != null)
            {
                result = TypeUtils.CoerceValue(result, options.OutputType);
            }

            return(result);
        }