Пример #1
0
        private static List <CommandParameterInfo> FindParameters(MethodInfo methodInfo)
        {
            if (DebugLog)
            {
                Debug.LogFormat("Checking parameters for method {0}", methodInfo.Name);
            }

            ParameterInfo[]    parameterInfos      = methodInfo.GetParameters();
            CommandParameter[] parameterAttributes =
                (CommandParameter[])methodInfo.GetCustomAttributes(typeof(CommandParameter),
                                                                   false);

            List <CommandParameterInfo> commandInfos
                = new List <CommandParameterInfo>(parameterInfos.Length);

            for (int i = 0; i < parameterInfos.Length; i++)
            {
                commandInfos.Add(null);
            }

            for (int i = 0; i < parameterInfos.Length; i++)
            {
                CommandParameter[] parameterCommandAttributes =
                    (CommandParameter[])parameterInfos[i].
                    GetCustomAttributes(typeof(CommandParameter), false);
                if (parameterCommandAttributes.Length > 0)
                {
                    parameterCommandAttributes[0].Order = i;
                    AddParameterInfo(methodInfo, parameterCommandAttributes[0],
                                     parameterInfos, commandInfos);
                }
            }

            foreach (var attribute in parameterAttributes)
            {
                AddParameterInfo(methodInfo, attribute, parameterInfos, commandInfos);
            }

            for (int i = 0; i < commandInfos.Count; i++)
            {
                if (commandInfos[i] == null)
                {
                    commandInfos[i] = new CommandParameterInfo(parameterInfos[i], i);
                }
            }
            return(commandInfos);
        }
Пример #2
0
        private static void AddParameterInfo(MethodInfo methodInfo, CommandParameter attribute,
                                             ParameterInfo[] parameterInfos, List <CommandParameterInfo> commandInfos)
        {
            if (DebugLog)
            {
                Debug.LogFormat("Checking parameter info for method {0}", methodInfo.Name);
            }

            if (attribute == null || methodInfo == null)
            {
                Debug.LogWarning("MonKey | Error when parsing a command : no attribute or method info found");
                return;
            }

            if (parameterInfos == null || attribute.Order >= parameterInfos.Length)
            {
                Debug.LogWarningFormat("Monkey Commander Warning:" +
                                       " A command Parameter for the Command '{0}'" +
                                       " was associated with the order '{1}', " +
                                       "but the method does not have that many" +
                                       " parameters", methodInfo.Name, attribute.Order);
                return;
            }

            MethodInfo autoCompleteMethod = null;

            if (attribute.HasAutoCompleteMethod)
            {
                if (methodInfo.DeclaringType != null)
                {
                    autoCompleteMethod =
                        methodInfo.DeclaringType.GetMethod(attribute.AutoCompleteMethodName,
                                                           BindingFlags.Public | BindingFlags.Default
                                                           | BindingFlags.NonPublic | BindingFlags.Static);
                }

                if (autoCompleteMethod != null &&
                    (!autoCompleteMethod.ReturnType.IsSubclassOf(
                         typeof(GenericCommandParameterAutoComplete)) &&
                     autoCompleteMethod.ReturnType != typeof(GenericCommandParameterAutoComplete) ||
                     autoCompleteMethod.GetParameters().Length != 0))
                {
                    autoCompleteMethod = null;
                }

                if (autoCompleteMethod == null)
                {
                    Debug.LogWarningFormat("Monkey Commander Warning:A parameter for the " +
                                           "command '{0}' was linked to an auto complete method named '{1}'," +
                                           " but no static method of such name could be found, " +
                                           "or the method does not return a CommandParameterAutoComplete",
                                           methodInfo.Name, attribute.AutoCompleteMethodName);
                }
            }

            MethodInfo defaultValueMethod = null;

            if (attribute.HasDefaultValueMethod)
            {
                if (methodInfo.DeclaringType != null)
                {
                    defaultValueMethod = methodInfo.DeclaringType.GetMethod(attribute.DefaultValueMethod,
                                                                            BindingFlags.Public | BindingFlags.Default
                                                                            | BindingFlags.NonPublic | BindingFlags.Static);
                }

                if (defaultValueMethod != null)
                {
                    //default values for array members must be done differently

                    /*if (parameterInfos[attribute.Order].ParameterType.IsArray)
                     * {
                     *  if(defaultValueMethod.ReturnType
                     *     != parameterInfos[attribute.Order].ParameterType.GetElementType())
                     *      defaultValueMethod = null;
                     *
                     * }else*/
                    if (defaultValueMethod.ReturnType !=
                        parameterInfos[attribute.Order].ParameterType ||
                        defaultValueMethod.GetParameters().Length != 0)
                    {
                        defaultValueMethod = null;
                    }
                }

                if (defaultValueMethod == null)
                {
                    Debug.LogWarningFormat("Monkey Commander Warning:A parameter for the " +
                                           "command '{0}' was linked to a default value method named '{1}'," +
                                           " but no static method of such name could be found, " +
                                           "or the method does not return the same type than the specified parameter",
                                           methodInfo.Name, attribute.DefaultValueMethod);
                }
            }

            CommandParameterInfo info =
                new CommandParameterInfo(
                    attribute, parameterInfos[attribute.Order], autoCompleteMethod, defaultValueMethod);

            commandInfos[attribute.Order] = info;
        }
Пример #3
0
        private void PrepareParameterEdition()
        {
            CommandParameterInfo paramInfo = Info.CommandParameterInfo[CurrentParameterID];

            currentInterpreter = CommandParameterInterpreter.
                                 GetInterpreter(paramInfo.ParameterType);
            CurrentTextEntered = TextEntered[CurrentParameterID];

            CurrentAutoComplete = paramInfo.HasAutoComplete
                ? paramInfo.AutoComplete
                : AutoCompleteManager.GetDefaultAutoComplete(IsArray ?
                                                             paramInfo.ParameterType.GetElementType() :
                                                             paramInfo.ParameterType);

            CurrentAutoComplete?.InitializeAutoComplete();

            CurrentAutoCompleteID = -1;

            if (!CurrentParameterInfo.PreventDefaultValueUsage && CurrentParameterInfo.HadDefaultValue && CurrentAutoComplete != null)
            {
                if (CurrentAutoComplete.Count == 0)
                {
                    CurrentAutoComplete.GenerateAndSortAutoComplete("");
                }

                for (int i = 0; i < CurrentAutoComplete.Count; i++)
                {
                    if (CurrentAutoComplete.GetValue(i).Equals(CurrentParameterInfo.DefaultValue))
                    //   if (CurrentAutoComplete.GetValue(i).ToString() == CurrentParameterInfo.DefaultValue.ToString())
                    {
                        CurrentAutoCompleteID = i;
                        break;
                    }
                }
            }

            if (IsArray)
            {
                CurrentArrayTextEntered  = arrayTextEntered[CurrentParameterID];
                CurrentArrayValuesParsed = arrayValuesParsed[CurrentParameterID];
                currentArrayIDEdited     = CurrentArrayTextEntered.Count - 1;
                if (CurrentArrayIDEdited >= 0)
                {
                    CurrentTextEntered = CurrentArrayTextEntered[CurrentArrayIDEdited];
                }
            }
            else
            {
                CurrentTextEntered = TextEntered[CurrentParameterID];
            }

            if (!CurrentTextEntered.IsNullOrEmpty())
            {
                RegenerateAutoComplete();
            }
            else
            {
                CurrentAutoComplete?.InitializeAutoComplete();
            }

            CommandConsoleWindow.CurrentPanel.SearchTerms         = CurrentTextEntered;
            CommandConsoleWindow.CurrentPanel.PreviousSearchTerms = CurrentTextEntered;
        }