Exemplo n.º 1
0
 public void OnTabPressed()
 {
     if (AutoCompList.Count != 0)
     {
         OnEnterPressed(); return;
     }
     AutoCompIndex = 0;
     AutoCompList.Clear();
     AutoCompList.AddRange(terminalMethods.GetCommandsContaining(InputText));
 }
Exemplo n.º 2
0
 internal void OnEnterPressed()
 {
     if (AutoCompList.Count > 0)
     {
         InputText = AutoCompList[AutoCompIndex];
         AutoCompList.Clear();
     }
     else
     {
         PreExecute();
     }
 }
Exemplo n.º 3
0
    private string ExecuteCommand(string inputText)
    {
        AutoCompList.Clear();
        bool          registered        = false;
        string        result            = null;
        string        insideParentheses = Regex.Match(inputText, @"\(([^)]*)\)").Groups[1].Value;
        List <string> args = new List <string>();
        string        command;

        if (!string.IsNullOrEmpty(insideParentheses))
        {
            args    = insideParentheses.Split(new char[] { ',' }).ToList();
            command = inputText.Replace(insideParentheses, "").Replace("(", "").Replace(")", "").Replace(";", "");
        }
        else
        {
            command = inputText.Replace("(", "").Replace(")", "").Replace(";", "");
        }
        foreach (var method in TerminalMethods.Methods)
        {
            foreach (object attribute in method.GetCustomAttributes(true)) // Returns all 3 of my attributes.
            {
                if (attribute is TerminalCommandAttribute)
                {
                    TerminalCommandAttribute attr = (TerminalCommandAttribute)attribute;
                    if (attr.commandName == command)
                    {
                        if (registered)
                        {
                            Debug.LogError(TerminalStrings.MULTIPLE_COMMAND_NAMES + command);
                        }
                        Type            type             = (method.DeclaringType);
                        ParameterInfo[] methodParameters = method.GetParameters();
                        List <object>   argList          = new List <object>();

                        // check if method parameters and arguments count are equal
                        if (methodParameters.Length != args.Count)
                        {
                            result = string.Format(TerminalStrings.ARGUMENT_COUNT_MISSMATCH, command, methodParameters.Length, args.Count);
                            Debug.Log(result);
                            return(result);
                        }

                        // Cast Arguments if there is any
                        if (args.Count != 0)
                        {
                            if (methodParameters.Length == args.Count)
                            {
                                // Cast string arguments to input objects types
                                for (int i = 0; i < methodParameters.Length; i++)
                                {
                                    try
                                    {
                                        var a = Convert.ChangeType(args[i], methodParameters[i].ParameterType);
                                        argList.Add(a);
                                    }
                                    catch
                                    {
                                        result = string.Format("Counld not convert {0} to Type {1}", args[i], methodParameters[i].ParameterType);
                                        Debug.LogError(result);
                                        return(result);
                                    }
                                }
                            }
                        }
                        if (type.IsSubclassOf(typeof(UnityEngine.Object)))
                        {
                            var instance_classes = GameObject.FindObjectsOfType(type);
                            if (instance_classes != null)
                            {
                                foreach (var instance_class in instance_classes)
                                {
                                    result = (string)method.Invoke(instance_class, argList.ToArray());
                                }
                            }
                        }
                        else
                        {
                            var instance_class = Activator.CreateInstance(type);
                            result = (string)method.Invoke(instance_class, argList.ToArray());
                        }
                        registered = true;
                        break;
                    }
                }
            }
        }
        if (!string.IsNullOrEmpty(result))
        {
            return(result);
        }
        if (registered)
        {
            return(null);
        }
        return(TerminalStrings.COMMAND_NOT_FOUND);
    }