Ejemplo n.º 1
0
        private CLISuggestion[] GetSuggestions(string path)
        {
            List <CLISuggestion> suggestions = new List <CLISuggestion>();

            if (path == "")
            {
                return(suggestions.ToArray());
            }

            string[] split = path.Split('.');

            int lastSeparatorIndex = path.LastIndexOf('.');

            if (lastSeparatorIndex < 0)
            {
                lastSeparatorIndex = 0;
            }

            string parentPath = path.Substring(0, lastSeparatorIndex);

            CLINode lastCompleteNode = FindNode(parentPath);

            List <CLINode> currentNodes = new List <CLINode>();

            if (lastCompleteNode == null)
            {
                // Only use the whole node tree if there is no seperator ('.') present
                if (!path.Contains("."))
                {
                    currentNodes = nodes;
                }
            }
            else
            {
                currentNodes = lastCompleteNode.children;
            }

            foreach (var node in currentNodes)
            {
                if (/*split[split.Length - 1] != "" &&*/ node.name.ToLower().StartsWith(split[split.Length - 1].ToLower()))
                {
                    string suggestion = parentPath + ((parentPath == "") ? "" : ".") + node.name;
                    string typeText   = "";

                    if (node.method != null)
                    {
                        var parameters = node.method.GetParameters();

                        if (parameters.Length > 0)
                        {
                            typeText = "(";

                            for (int i = 0; i < parameters.Length; i++)
                            {
                                typeText += TypeAliases[parameters[i].ParameterType];
                                if (i < parameters.Length - 1)
                                {
                                    typeText += ", ";
                                }
                            }

                            typeText += ")";
                        }
                    }


                    suggestions.Add(new CLISuggestion(suggestion, typeText, node));
                }
            }


            return(suggestions.ToArray());
        }
Ejemplo n.º 2
0
        private void ConstructNodeTree()
        {
            //List<MethodInfo> methods = new List<MethodInfo>();
            //for (int i = 0; i < methodData.Length; i++)
            //{
            //    methods.AddRange(methodData[i].methods);
            //}

            //MethodInfo[] me = GetMethodData();

            for (int m = 0; m < methodData.Length; m++)
            {
                for (int i = 0; i < methodData[m].methods.Count; i++)
                {
                    ConsoleCommand command = GetConsoleCommand(methodData[m].methods[i]);

                    if (command != null)
                    {
                        // Custom path

                        if (command.customPath != "")
                        {
                            string[] split = command.customPath.Split('.');

                            List <CLINode> currentNodeList = nodes;

                            for (int splitIndex = 0; splitIndex < split.Length; splitIndex++)
                            {
                                CLINode n = new CLINode();
                                n = GetNode(currentNodeList, split[splitIndex]);

                                if (n == null)
                                {
                                    n      = new CLINode();
                                    n.name = split[splitIndex];
                                    currentNodeList.Add(n);
                                }

                                currentNodeList = n.children;

                                if (splitIndex == split.Length - 1)
                                {
                                    CLINode finalNode = new CLINode();
                                    finalNode.name          = methodData[m].methods[i].Name;
                                    finalNode.method        = methodData[m].methods[i];
                                    finalNode.monoBehaviour = methodData[m].monoBehaviour;
                                    n.children.Add(finalNode);
                                }
                            }
                        }

                        // Type path

                        CLINode childNode = new CLINode();

                        childNode.name          = methodData[m].methods[i].Name;
                        childNode.method        = methodData[m].methods[i];
                        childNode.monoBehaviour = methodData[m].monoBehaviour;

                        string baseNodeName = methodData[m].methods[i].DeclaringType.ToString();

                        CLINode baseNode = GetNode(nodes, baseNodeName);

                        if (baseNode != null)
                        {
                            // Base node exists
                            baseNode.children.Add(childNode);
                        }
                        else
                        {
                            // Base node doesn't exist
                            baseNode = new CLINode();

                            baseNode.name = methodData[m].methods[i].DeclaringType.ToString();
                            baseNode.children.Add(childNode);

                            nodes.Add(baseNode);
                        }
                    }
                }
            }
        }
Ejemplo n.º 3
0
        private void Update()
        {
            if (Input.GetKeyDown(KeyCode.F1))
            {
                Debug.Log("Toggled CLI");
                m_CLIPanel.gameObject.SetActive(!m_CLIPanel.gameObject.activeInHierarchy);

                if (m_CLIPanel.gameObject.activeInHierarchy)
                {
                    m_InputField.Select();
                    m_InputField.ActivateInputField();
                }
            }

            if (Input.GetKeyDown(KeyCode.Tab))
            {
                string input = m_InputField.text;

                CLISuggestion[] suggestions = GetSuggestions(input);

                if (suggestions.Length > 0)
                {
                    m_InputField.text = suggestions[0].path;
                    m_InputField.MoveTextEnd(false);
                }
            }

            if (Input.GetKeyDown(KeyCode.UpArrow))
            {
                m_InputField.text          = lastCommand;
                m_InputField.caretPosition = m_InputField.text.Length;
            }

            if (Input.GetKeyDown(KeyCode.Return) || Input.GetKeyDown(KeyCode.KeypadEnter))
            {
                lastCommand = m_InputField.text;

                string[] split = m_InputField.text.Split(' ');

                CLINode node = FindNode(split[0]);

                if (node != null && node.method != null)
                {
                    // Valid command

                    List <object> parameters = new List <object>();

                    var parameterTypes = node.method.GetParameters();

                    for (int i = 1; i < split.Length; i++)
                    {
                        //int x = Convert.ToInt32(split[i]);
                        //print(parameterTypes[i-1].ParameterType.ToString() + (parameterTypes[i - 1].ParameterType == typeof(int)));

                        try
                        {
                            var x = Convert.ChangeType(split[i], parameterTypes[i - 1].ParameterType);
                            parameters.Add(x);
                        }
                        catch (Exception e)
                        {
                            Debug.LogError(e);
                        }
                    }

                    Log(m_InputField.text);
                    object obj = node.method.Invoke(node.monoBehaviour, parameters.ToArray());
                    if (obj == null)
                    {
                        Log("Return value: null\n");
                    }
                    else
                    {
                        Log("Return value: " + obj.ToString() + "\n");
                    }
                }
                else
                {
                    Log("'" + m_InputField.text + "' is not a valid command!\n");
                }

                m_InputField.text = "";
                m_InputField.ActivateInputField();
            }
        }