示例#1
0
        private static void termPool(object _)
        {
            int sleepTime = 50;

            Draw(":", 0, Console.WindowHeight - 1, ConsoleColor.Cyan);
            Log("Welcome!", ConsoleColor.Magenta);

            try
            {
                while (isRunning)
                {
                    Thread.Sleep(sleepTime);

                    if (sleepTime < 500)
                    {
                        sleepTime++;
                    }

                    if (Pause)
                    {
                        continue;
                    }

                    #region INPUT

                    if (Console.KeyAvailable)
                    {
                        sleepTime = 1;
                        var k = Console.ReadKey(true);
                        if (k.Key == ConsoleKey.Tab)
                        {
                            if (commands.Count != 0 && inputText.Length != 0 && !inputText.Contains(' '))
                            {
                                // подбор
                                if (skip == 0)
                                {
                                    searchText = inputText;
                                }
                                using (IEnumerator <string> keyEnum = commands.Keys.GetEnumerator())
                                {
                                    while (keyEnum.MoveNext())
                                    {
                                        for (int i = 0; i < skip; i++)
                                        {
                                            if (!keyEnum.MoveNext())
                                            {
                                                keyEnum.Reset();
                                            }
                                        }

                                        if (keyEnum.Current == null)
                                        {
                                            break; // for safe
                                        }
                                        if (keyEnum.Current.Contains(searchText))
                                        {
                                            // set
                                            inputText = keyEnum.Current;
                                            //Console.CursorLeft = inputText.Length + 1;
                                            skip++;
                                            break;
                                        }
                                    }
                                }
                            }
                        }
                        else if (k.Key == ConsoleKey.Backspace)
                        {
                            inputText = inputText.Length > 1 ? inputText.Remove(inputText.Length - 1) : string.Empty;
                        }
                        else if (k.Key == ConsoleKey.Spacebar)
                        {
                            inputText += " ";
                        }
                        else if (k.Key == ConsoleKey.Escape)
                        {
                            Draw(inputText, 1, Console.WindowHeight - 1, ConsoleColor.Black); // clear
                            inputText = string.Empty;
                        }
                        else
                        {
                            skip = 0;
                            if (k.Key == ConsoleKey.Enter && inputText.Split().Length > 0)
                            {
                                // ENTER
                                string args = null;
                                if (inputText.Contains(' '))
                                {
                                    var spl = inputText.Split(new char[1] {
                                        ' '
                                    }, 2);
                                    inputText = spl[0].Trim();
                                    args      = spl[1].Trim();
                                }

                                if (inputText.Trim().Length > 0)
                                {
                                    OnCommandEnter?.Invoke(inputText, args);

                                    if (commands.ContainsKey(inputText))
                                    {
                                        Log("> " + inputText, ConsoleColor.Gray);
                                        commands[inputText].Item1.Invoke(args);
                                    }
                                    else
                                    {
                                        Log($"Unknown command \'{inputText}\'", ConsoleColor.Gray);
                                    }

                                    //Draw(inputText, 1, Console.WindowHeight - 1, ConsoleColor.Black); // clear
                                    ClearLine(Console.WindowHeight - 1);
                                }
                                inputText = string.Empty;
                            }
                            else if (inputText.Length < Console.BufferWidth)
                            {
                                //if ((k.Key >= ConsoleKey.D0 && k.Key <= ConsoleKey.Z) ||
                                //    (k.Key >= ConsoleKey.NumPad0 && k.Key <= ConsoleKey.Divide))
                                inputText += char.ToString(k.KeyChar);
                            }
                        }
                        Draw(":", 0, Console.WindowHeight - 1, ConsoleColor.Cyan);
                        Draw(inputText + "  ", 1, Console.WindowHeight - 1, ConsoleColor.White);
                    }

                    #endregion

                    #region DRAW

                    if (writeData.Count != 0)
                    {
                        lock (writeLock)
                        {
                            // clear terminal log border
                            //Console.SetCursorPosition(0, startLogY);
                            //Console.Write(new string(' ', (Console.BufferWidth - 1) * Console.WindowHeight));

                            sleepTime = 1;
                            Flush();
                            // подсветка курсора
                            Console.SetCursorPosition(inputText.Length + 1, Console.WindowHeight - 1);
                            Console.BackgroundColor = ConsoleColor.Gray;
                            Console.Write(' ');
                            Console.BackgroundColor = ConsoleColor.Black;

                            //OnDraw?.Invoke();
                        } // end lock
                    }

                    #endregion
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("\nTerm pool exception:\n" + ex.ToString());
            }

            Console.Write("\nTerminal pool stoped.");
        }
示例#2
0
        private void Submit(string text)
        {
            if (!Input.GetKeyDown(KeyCode.Return) && !Input.GetKeyDown(KeyCode.KeypadEnter))
            {
                return;
            }
            _commandInput.ActivateInputField();
            var command = new Entry();

            command.Command = text;
            _historyList.Add(command);
            _pendingOutput = true;
            _lua.DoString(text);
            OnCommandEnter?.Invoke(text);
            UpdateHistoryText();
            //command.Output = .ToString();
            //_commandInput.text = "";
            //_historyList.Add(
            //    new Entry {
            //        Command = text
            //    });
            //_commandIndex = _historyList.Count;
            //StartCoroutine(SetFocus(true));

            //string entityStr = string.Empty;
            //string componentStr = string.Empty;
            //string methodStr = string.Empty;
            //var args = text.Split(' ');
            //List<object> parameters = new List<object>();
            //for (int i = 0; i < args.Length; i++) {
            //    var arg = args[i];
            //    if (arg == EntityOption) {
            //        i++;
            //        int parsed;
            //        string str = ParseString(text, i, out parsed);
            //        if (parsed < 0) {
            //            Application.logMessageReceived -= InterceptDebugLog;
            //            return;
            //        }
            //        entityStr = str;
            //        i += parsed;
            //    }
            //    else if (arg == ComponentOption) {
            //        componentStr = args[++i];
            //    }
            //    else if (arg == MethodOption) {
            //        methodStr = args[++i];
            //        string next;
            //        while (i < args.Length - 1 && (next = args[++i]) != EntityOption && next != ComponentOption && next != MethodOption) {
            //            int iParse;
            //            float fParse;
            //            bool bParse;
            //            if (int.TryParse(next, out iParse)) {
            //                parameters.Add(iParse);
            //            }
            //            else if (float.TryParse(next, out fParse)) {
            //                parameters.Add(fParse);
            //            }
            //            else if (bool.TryParse(next, out bParse)) {
            //                parameters.Add(bParse);
            //            }
            //            else {
            //                int parsed;
            //                string str = ParseString(text, i, out parsed);
            //                if (parsed < 0) {
            //                    Application.logMessageReceived -= InterceptDebugLog;
            //                    return;
            //                }
            //                i += parsed;
            //                parameters.Add(str);
            //            }
            //        }
            //        i--;
            //    }
            //}
            //Entity targetEntity = null;
            //Component targetComponent = null;
            //MethodInfo targetMethod = null;

            //if (entityStr != string.Empty) {
            //    if (int.TryParse(entityStr, out int entityNum)) {
            //        targetEntity = EntityController.GetEntity(entityNum);
            //    }
            //    else {

            //    }
            //    targetEntity = GameObject.Find(entityStr);
            //}
            //if (targetEntity == null) {
            //    targetEntity = Player.SelectedActor?.Entity;
            //}
            //if (targetEntity == null) {
            //    LogError("You must either specify a GameObject with " + EntityOption + " or select one in the hierarchy.");
            //    Application.logMessageReceived -= InterceptDebugLog;
            //    return;
            //}

            //// Target Component
            //{
            //    if (componentStr != string.Empty) {
            //        targetComponent = targetEntity.GetComponent(componentStr);
            //        if (targetComponent == null) {
            //            Debug.LogError("Could not find component '" + componentStr + "'.");
            //            Application.logMessageReceived -= InterceptDebugLog;
            //            return;
            //        }
            //    }
            //}

            //// Target Method
            //{
            //    if (methodStr == string.Empty) {
            //        Debug.LogError("You must specify a method to call with " + MethodOption + ".");
            //        Application.logMessageReceived -= InterceptDebugLog;
            //        return;
            //    }
            //    if (targetComponent == null) {
            //        foreach (var component in targetEntity.GetComponents<Component>()) {
            //            var method = GetMethod(component, methodStr);
            //            if (method != null) {
            //                targetComponent = component;
            //                break;
            //            }
            //        }
            //        if (targetComponent == null) {
            //            if (string.IsNullOrEmpty(componentStr)) {
            //                Debug.LogError("Could not find a component with the method '" + methodStr + "'.");
            //            }
            //            else {
            //                Debug.LogError("Could not find component '" + componentStr + "'.");
            //            }
            //            Application.logMessageReceived -= InterceptDebugLog;
            //            return;
            //        }
            //    }
            //    targetMethod = GetMethod(targetComponent, methodStr);
            //    if (targetMethod == null) {
            //        Debug.LogError("Could not find method '" + methodStr + "'.");
            //        Application.logMessageReceived -= InterceptDebugLog;
            //        return;
            //    }
            //}
            //if (parameters.Count != targetMethod.GetParameters().Length) {
            //    Debug.LogError("Parameter count mismatch. You gave " + parameters.Count + " parameters and the method '" + methodStr + "' wants " + targetMethod.GetParameters().Length + ". Note that optional parameters are not supported.");
            //    Application.logMessageReceived -= InterceptDebugLog;
            //    return;
            //}
            //try {
            //    targetMethod.Invoke(targetComponent, parameters.ToArray());
            //}
            //catch (ArgumentException) {
            //    var wanted = new StringBuilder();
            //    var got = new StringBuilder();
            //    string comma = "";
            //    foreach (var param in targetMethod.GetParameters()) {
            //        wanted.Append(comma);
            //        comma = ", ";
            //        wanted.Append(param.ParameterType.Name);
            //    }
            //    comma = "";
            //    foreach (var param in parameters) {
            //        got.Append(comma);
            //        comma = ", ";
            //        got.Append(param.GetType().Name);
            //    }
            //    Debug.LogError("Parameter type mismatch. Wanted: (" + wanted + "), got: (" + got + ").");
            //}
        }