static bool AddMethod(Type objType, ConsoleCommand consoleCommand, MemberInfo member, MethodInfo method, MemberType memberType, string methodName, bool logFailed = true)
        {
            if (method != null)
            {
                ParameterInfo[] paramInfos = method.GetParameters();

                if (!ValidParams(objType, method, paramInfos))
                {
                    return(true);
                }

                // Debug.Log("Add Method " + methodName);
                string syntax = method.ToString();
                AddCommand(member, consoleCommand, methodName, new CommandData(consoleCommand, objType, syntax, memberType, member, paramInfos, method.IsStatic), logFailed);
                return(true);
            }
            return(false);
        }
        static void AddCommand(MemberInfo member, ConsoleCommand consoleCommand, string commandName, CommandData commandData, bool logFailed = true)
        {
            if (instance.ignoreCasesInCommands)
            {
                commandName = commandName.ToLower();
            }

            if (consoleCommand.command == string.Empty)
            {
                consoleCommand.command = commandName;
            }
            else
            {
                commandName = consoleCommand.command;
                if (instance.ignoreCasesInCommands)
                {
                    commandName = commandName.ToLower();
                }
            }

            if (consoleCommand.alias != string.Empty)
            {
                if (!commandsTable.lookup.ContainsKey(consoleCommand.alias))
                {
                    commandsTable.lookup[consoleCommand.alias] = CommandData.empty;
                }
                commandName = consoleCommand.alias + "." + consoleCommand.command;
            }

            // Debug.Log("=> " + commandName + " " + (commandData.paramInfos == null ? "null " : commandData.paramInfos.Length.ToString()));

            if (commandsTable.lookup.ContainsKey(commandName))
            {
                if (logFailed)
                {
                    Debug.LogError("Duplicate command: `" + commandName + "` on " + member.Name + " in " + commandData.obj.ToString() + " class ");
                }
                return;
            }

            commandsTable.lookup[commandName] = commandData;
        }
Example #3
0
            public CommandData(ConsoleCommand consoleCommand, object obj, string syntax, MemberType memberType, MemberInfo member, ParameterInfo[] paramInfos, bool isStatic)
            {
                this.consoleCommand = consoleCommand;
                this.obj            = obj;
                this.syntax         = syntax;
                this.memberType     = memberType;
                this.member         = member;
                this.isStatic       = isStatic;

                if (paramInfos == null || (paramInfos != null && paramInfos.Length == 0))
                {
                    this.paramInfos = null;
                    args            = null;
                }
                else
                {
                    this.paramInfos = paramInfos;
                    args            = new object[paramInfos.Length];
                }
            }
        static public void RegisterStaticType(Type objType)
        {
            if (instance == null)
            {
                return;
            }

            var consoleAlias = (ConsoleAlias[])objType.GetCustomAttributes(typeof(ConsoleAlias), true);

            if (consoleAlias.Length > 0)
            {
                commandsTable.lookup[consoleAlias[0].alias] = CommandData.empty;
            }

            const BindingFlags registerBindingFlags = BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static | BindingFlags.Default;// | BindingFlags.FlattenHierarchy;

            MemberInfo[] members = objType.GetMembers(registerBindingFlags);

            for (int i = 0; i < members.Length; i++)
            {
                MemberInfo member = members[i];

                var consoleCommands = (ConsoleCommand[])member.GetCustomAttributes(typeof(ConsoleCommand), false);

                if (consoleCommands.Length == 0)
                {
                    continue;
                }

                ConsoleCommand consoleCommand = consoleCommands[0];

                if (consoleAlias.Length > 0)
                {
                    consoleCommand.alias = consoleAlias[0].alias + (consoleCommand.alias.Length > 0 ? "." + consoleCommand.alias : string.Empty);
                }
                // Debug.Log("Register " + objType.ToString() + " " + members.Length);

                if (AddMethod(objType, consoleCommand, member, member as MethodInfo, MemberType.Method, member.Name))
                {
                    continue;
                }

                FieldInfo field = member as FieldInfo;
                if (field != null)
                {
                    Type type = field.FieldType;

                    if (typeof(Delegate).IsAssignableFrom(type))
                    {
                        AddMethod(objType, consoleCommand, member, type.GetMethod("Invoke"), MemberType.Delegate, field.Name);
                        continue;
                    }

                    if (!ValidParam(objType, field, type))
                    {
                        continue;
                    }

                    if (type.IsEnum && consoleCommand.description == string.Empty)
                    {
                        consoleCommand.description = string.Join(", ", Enum.GetNames(type));
                    }

                    string syntax = field.Name + " " + type.Name;
                    AddCommand(member, consoleCommand, member.Name, new CommandData(consoleCommand, objType, syntax, MemberType.Field, member, null, field.IsStatic));
                    continue;
                }

                PropertyInfo prop = member as PropertyInfo;
                if (prop != null)
                {
                    Type type = prop.PropertyType;

                    // Debug.Log(consoleCommand.alias + "." + consoleCommand.command);
                    if (!ValidParam(objType, prop, type))
                    {
                        continue;
                    }

                    MethodInfo getMethod = prop.GetGetMethod(true);
                    MethodInfo setMethod = prop.GetSetMethod(true);

                    bool hasGet = getMethod != null;
                    bool hasSet = setMethod != null;
                    bool isStatic;

                    if (hasGet)
                    {
                        isStatic = getMethod.IsStatic;
                    }
                    else if (hasSet)
                    {
                        isStatic = setMethod.IsStatic;
                    }
                    else
                    {
                        Debug.Log("Property has no getter or setter");
                        continue;
                    }

                    string syntax = prop.Name + " " + type.Name;
                    if (hasGet && !hasSet)
                    {
                        syntax += " (get)";
                    }
                    else if (hasGet && hasSet)
                    {
                        syntax += " (get set)";
                    }
                    else
                    {
                        syntax += " (set)";
                    }

                    AddCommand(member, consoleCommand, member.Name, new CommandData(consoleCommand, objType, syntax, MemberType.Property, member, null, isStatic));
                    continue;
                }
            }
        }