private static void Exec(string cmd) { if (cmd.Length <= 0) { return; } switch (cmd) { case "newpatch": { var di = new DirectoryInfo("newpatch"); if (di.Exists) { Patcher.AddNewPatch(di); Patcher.RestartAll(); } else { WriteLine("newpatch directory does not exit"); } break; } case "forcepatch": { var di = new DirectoryInfo("newpatch"); if (di.Exists) { Patcher.AddNewPatch(di, true); Patcher.RestartAll(); } else { WriteLine("newpatch directory does not exit"); } break; } case "restartall": Patcher.RestartAll(); break; case "killall": Patcher.KillAll(); break; default: if (cmd.StartsWith("setversion:")) { Patcher.ChangeVersion(Patcher.StrToVer(cmd.Substring("setversion:".Length))); } else if (cmd.StartsWith("tcpserver:")) { bool opt; if (bool.TryParse(cmd.Substring("tcpserver:".Length), out opt)) { WriteLine("TCP server is " + (opt ? "enabled." : "disabled.")); Server.AcceptPatches = opt; } } else if (cmd == "version") { WriteLine(Patcher.VerToStr(Patcher.Version)); } else { WriteLine("Doing nothing, unknown command."); } break; } }
private static void Main() { WriteLine("Current version is: " + Patcher.VerToStr(Patcher.Version)); new Thread(() => HttpStuff.Listen(@"http://10.10.20.6:11080/")).Start(); new Thread(Server.StartListening).Start(); // Configure console. //Console.BufferWidth = 80; //Console.WindowWidth = 80; Console.TreatControlCAsInput = true; // Console.WriteLine("Enter a string. Press <Enter> or Esc to exit."); while (!Stop.WaitOne(20)) { if (_currentline == string.Empty && Console.CursorLeft == 0) { Console.Write("> "); } if (Console.KeyAvailable) { string cmd = null; ConsoleKeyInfo keyInfo; lock (_currentline) { keyInfo = Console.ReadKey(true); // Ignore if Alt or Ctrl is pressed. if ((keyInfo.Modifiers & ConsoleModifiers.Alt) == ConsoleModifiers.Alt) { continue; } if ((keyInfo.Modifiers & ConsoleModifiers.Control) == ConsoleModifiers.Control) { continue; } // Ignore if KeyChar value is \u0000. if (keyInfo.KeyChar == '\u0000') { continue; } // Ignore tab key. if (keyInfo.Key == ConsoleKey.Tab) { continue; } // Handle backspace. if (keyInfo.Key == ConsoleKey.Backspace) { // Are there any characters to erase? if (_currentline.Length > 0) { // Determine where we are in the console buffer. var cursorCol = Console.CursorLeft - 1; var oldLength = _currentline.Length; var extraRows = (oldLength + 2) / Console.BufferWidth; _currentline = _currentline.Substring(0, oldLength - 1); Console.CursorLeft = 0; Console.CursorTop = Console.CursorTop - extraRows; Console.Write("> "); Console.Write(_currentline); Console.Write(" "); if (cursorCol >= 0) { Console.CursorLeft = cursorCol; } else { --Console.CursorTop; Console.CursorLeft = Console.BufferWidth - 1; } } continue; } // Handle Escape key. if (keyInfo.Key == ConsoleKey.Escape) { // Are there any characters to erase? Erase them all. if (_currentline.Length > 0) { // Determine where we are in the console buffer. var oldLength = _currentline.Length; var extraRows = (oldLength + 2) / Console.BufferWidth; Console.CursorLeft = 0; Console.CursorTop = Console.CursorTop - extraRows; Console.Write("> "); Console.Write(new string(' ', oldLength)); Console.CursorTop = Console.CursorTop - extraRows; Console.CursorLeft = 2; } _currentline = string.Empty; continue; } if (keyInfo.Key == ConsoleKey.Enter) { cmd = _currentline; _currentline = string.Empty; Console.WriteLine(); } else { // Handle key by adding it to input string. Console.Write(keyInfo.KeyChar); _currentline += keyInfo.KeyChar; } } if (keyInfo.Key == ConsoleKey.Enter) { Exec(cmd); _currentline = string.Empty; } } } }