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 ReadCallback(IAsyncResult ar) { // Retrieve the state object and the handler socket // from the asynchronous state object. var state = (StateObject)ar.AsyncState; var handler = state.WorkSocket; // Read data from the client socket. var bytesRead = handler.EndReceive(ar); if (bytesRead <= 0) { return; } if (state.BufferSize == -1) { if (bytesRead != StateObject.HeaderBufferSize || state.RecvCount != 0) { throw new Exception("AGGGRRR"); } var cmd = BitConverter.ToInt32(state.HeaderBuffer, 0); if (cmd <= 0) { return; } state.BufferSize = cmd; Program.WriteLine("[AGENT] Buffer size is " + state.BufferSize); state.Buffer = new byte[state.BufferSize]; // Not all data received. Get more. handler.BeginReceive(state.Buffer, 0, state.BufferSize, 0, ReadCallback, state); //if (cmd == -1) //{ // Program.WriteLine("[AGENT] command newpatch"); // if (Directory.Exists("newpatch")) // { // PATCHER.patch("newpatch"); // PATCHER.restartall(); // } //} } else { state.RecvCount += bytesRead; if (state.RecvCount < state.BufferSize) { // Not all data received. Get more. handler.BeginReceive(state.Buffer, state.RecvCount, state.BufferSize - state.RecvCount, 0, ReadCallback, state); } else if (AcceptPatches) { var formatter = new BinaryFormatter(); var buffer = new MemoryStream(state.Buffer); var patch = (object[][])formatter.Deserialize(buffer); object[] currentomi; // get a copy of current omi to update it using (var source = File.OpenRead(@"versioning\current\Res-Tex\omi.tex")) currentomi = FormatterOmi.Read(source); DBstuff.UpdateDB(patch, currentomi[0] as object[][]); Patcher.EmptyFolder("temp"); Directory.CreateDirectory(@"temp\Res-Tex"); using (var dest = File.OpenWrite(@"temp\Res-Tex\omi.tex")) FormatterOmi.Write(dest, currentomi); Patcher.AddNewPatch(new DirectoryInfo("temp")); Patcher.RestartAll(); Send(handler, new byte[] { 0xFF, 0xFF, 0xFF, 0xFF }); } else { Send(handler, new byte[] { 0xFF, 0xFF, 0, 0 }); } } }
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; } } } }