Ejemplo n.º 1
0
        private void RegisterAttributes()
        {
#if !NETFX_CORE
            foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies())
            {
                // HACK: IL2CPP crashes if you attempt to get the methods of some classes in these assemblies.
                if (assembly.FullName.StartsWith("System") || assembly.FullName.StartsWith("mscorlib"))
                {
                    continue;
                }
                foreach (Type type in assembly.GetTypes())
                {
                    // FIXME add support for non-static methods (FindObjectByType?)
                    foreach (MethodInfo method in type.GetMethods(BindingFlags.Public | BindingFlags.Static))
                    {
                        CommandAttribute[] attrs = method.GetCustomAttributes(typeof(CommandAttribute), true) as CommandAttribute[];
                        if (attrs.Length == 0)
                        {
                            continue;
                        }

                        CommandAttribute.Callback cb = Delegate.CreateDelegate(typeof(CommandAttribute.Callback), method, false) as CommandAttribute.Callback;
                        if (cb == null)
                        {
                            CommandAttribute.CallbackSimple cbs = Delegate.CreateDelegate(typeof(CommandAttribute.CallbackSimple), method, false) as CommandAttribute.CallbackSimple;
                            if (cbs != null)
                            {
                                cb = delegate(string[] args) {
                                    cbs();
                                };
                            }
                        }

                        if (cb == null)
                        {
                            Debug.LogError(string.Format("Method {0}.{1} takes the wrong arguments for a console command.", type, method.Name));
                            continue;
                        }

                        // try with a bare action
                        foreach (CommandAttribute cmd in attrs)
                        {
                            if (string.IsNullOrEmpty(cmd.m_command))
                            {
                                Debug.LogError(string.Format("Method {0}.{1} needs a valid command name.", type, method.Name));
                                continue;
                            }

                            cmd.m_callback = cb;
                            m_commands.Add(cmd);
                        }
                    }
                }
            }
#endif
        }
Ejemplo n.º 2
0
        private void RegisterAttributes()
        {
            foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies())
            {
                foreach (Type type in assembly.GetTypes())
                {
                    // FIXME add support for non-static methods (FindObjectByType?)
                    foreach (MethodInfo method in type.GetMethods(BindingFlags.Public | BindingFlags.Static))
                    {
                        CommandAttribute[] attrs = method.GetCustomAttributes(typeof(CommandAttribute), true) as CommandAttribute[];
                        if (attrs.Length == 0)
                        {
                            continue;
                        }

                        CommandAttribute.Callback cb = Delegate.CreateDelegate(typeof(CommandAttribute.Callback), method, false) as CommandAttribute.Callback;
                        if (cb == null)
                        {
                            CommandAttribute.CallbackSimple cbs = Delegate.CreateDelegate(typeof(CommandAttribute.CallbackSimple), method, false) as CommandAttribute.CallbackSimple;
                            if (cbs != null)
                            {
                                cb = delegate(string[] args) {
                                    cbs();
                                };
                            }
                        }

                        if (cb == null)
                        {
                            Debug.LogError(string.Format("Method {0}.{1} takes the wrong arguments for a console command.", type, method.Name));
                            continue;
                        }

                        // try with a bare action
                        foreach (CommandAttribute cmd in attrs)
                        {
                            if (string.IsNullOrEmpty(cmd.m_command))
                            {
                                Debug.LogError(string.Format("Method {0}.{1} needs a valid command name.", type, method.Name));
                                continue;
                            }

                            cmd.m_callback = cb;
                            m_commands.Add(cmd);
                            m_help += string.Format("\n{0} : {1}", cmd.m_command, cmd.m_help);
                        }
                    }
                }
            }
        }
Ejemplo n.º 3
0
        public static bool AddCommandByMethodInfo(MethodInfo method)
        {
            CommandAttribute[] attrs = method.GetCustomAttributes(typeof(CommandAttribute), true) as CommandAttribute[];
            if (attrs.Length == 0)
            {
                return(false);
            }

            CommandAttribute.Callback cb = Delegate.CreateDelegate(typeof(CommandAttribute.Callback), method, false) as CommandAttribute.Callback;
            if (cb == null)
            {
                CommandAttribute.CallbackSimple cbs = Delegate.CreateDelegate(typeof(CommandAttribute.CallbackSimple), method, false) as CommandAttribute.CallbackSimple;
                if (cbs != null)
                {
                    cb = delegate
                    {
                        cbs();
                    };
                }
            }

            if (cb == null)
            {
                Debug.LogError($"Method {method.DeclaringType}.{method.Name} takes the wrong arguments for a console command.");
                return(false);
            }

            // try with a bare action
            foreach (CommandAttribute cmd in attrs)
            {
                if (string.IsNullOrEmpty(cmd.m_command))
                {
                    Debug.LogError($"Method {method.DeclaringType}.{method.Name} needs a valid command name.");
                    continue;
                }

                cmd.m_callback = cb;
                Instance.m_commands.Add(cmd);
                Instance.m_help += $"\n{cmd.m_command} : {cmd.m_help}";
            }
            return(true);
        }