Example #1
0
        private void RegisterAttributes()
        {
            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);
                        }
                    }
                }
            }
        }
Example #2
0
        private void RegisterAttributes()
        {
            foreach (Type type in Assembly.GetExecutingAssembly().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
                            {
                                cbs();
                            };
                        }
                    }

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

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

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