private static void ConfigFile_OnChanged(object sender, FileSystemEventArgs e) { Console.WriteLine("{0} >>> Detected: Configs file 'server_config.ini' was {1}.", DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss"), e.ChangeType.ToString().ToLower()); Console.WriteLine("If you want to edit 'server_config.ini', please quit the application first.", DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss")); Configs.Restore(); }
static void Main() { // check run once var self = new Mutex(true, Application.StartupPath.GetHashCode().ToString(), out var allow); if (!allow) { MessageBox.Show("CSM is already running.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); Environment.Exit(-1); } var myProc = Process.GetCurrentProcess(); myProc.PriorityClass = ProcessPriorityClass.BelowNormal; myHwnd = myProc.MainWindowHandle; // Event Application.ThreadException += ExceptionHandler_CurrentThread; AppDomain.CurrentDomain.UnhandledException += ExceptionHandler_AppDomain; AppDomain.CurrentDomain.ProcessExit += ApplicationHandler_OnExit; SystemEvents.PowerModeChanged += ApplicationHandler_PowerModeChanged; Win32Api.ConsoleCTRL.ConsoleClosed(ApplicationHandler_OnClose); Win32Api.PowerMode.NoSleep(); Console.Title = "CSGO Server Manager v" + version; var conf = Configs.Check(); if (!conf) { Console.WriteLine("{0} >>> Configs was initialized -> You can modify it manually!", DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss")); } Logger.Create(); Helper.WatchFile(); while (!File.Exists(Configs.srcds)) { using (var fileBrowser = new OpenFileDialog()) { fileBrowser.Multiselect = false; fileBrowser.Filter = "CSGO Dedicated Server (srcds.exe)|srcds.exe"; if (fileBrowser.ShowDialog() != DialogResult.OK) { MessageBox.Show("Application Exit!\nYou can modify it manually!", "CSGO Server Manager"); Environment.Exit(0); } else { Configs.srcds = fileBrowser.FileName; Console.WriteLine("{0} >>> Set SRCDS path -> {1}", DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss"), Configs.srcds); } } } while (!File.Exists(Configs.steam)) { using (var fileBrowser = new OpenFileDialog()) { fileBrowser.Multiselect = false; fileBrowser.Filter = "SteamCmd (steamcmd.exe)|steamcmd.exe"; if (fileBrowser.ShowDialog() != DialogResult.OK) { MessageBox.Show("Application Exit!\nYou can modify it manually!", "CSGO Server Manager"); Environment.Exit(0); } else { Configs.steam = fileBrowser.FileName; Console.WriteLine("{0} >>> Set Steam path -> {1}", DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss"), Configs.steam); } } } if (string.IsNullOrEmpty(Configs.ip) || !IPAddress.TryParse(Configs.ip, out var ipadr)) { do { Console.WriteLine("Please input your Game Server IP ..."); Configs.ip = Console.ReadLine(); }while (!IPAddress.TryParse(Configs.ip, out ipadr)); } if (string.IsNullOrEmpty(Configs.port) || !int.TryParse(Configs.port, out var port)) { do { Console.WriteLine("Please input your Game Server Port (1 - 65535) ..."); Configs.port = Console.ReadLine(); }while (!int.TryParse(Configs.port, out port)); } Global.ipep = new IPEndPoint(ipadr, port); if (!Helper.PortAvailable(port)) { Console.WriteLine("{0} >>> Port[{1}] is unavailable! Finding Application...", DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss"), port); try { var exe = Helper.GetAppByPort(port); Console.WriteLine("{0} >>> Trigger SRCDS Quit -> App[{1}] PID[{2}]", DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss"), exe.MainWindowTitle, exe.Id); Helper.ForceQuit(exe); } catch (Exception e) { Console.WriteLine("{0} >>> Not found Application: {1}", DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss"), e.Message); } } var process = 0; Process.GetProcessesByName("srcds").ToList().ForEach(exe => { if (exe.MainModule.FileName.Equals(Configs.srcds)) { Helper.ForceQuit(exe); Console.WriteLine("{0} >>> Force close old srcds before new srcds start.", DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss")); } process++; }); Console.WriteLine("{0} >>> {1} SRCDS are running on current host.", DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss"), process); if (!string.IsNullOrEmpty(Configs.TokenApi)) { while (TokenApi.CheckTokens(true) <= 0) { Console.WriteLine("{0} >>> TokenApi -> Checking ...", DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss")); } Console.WriteLine("{0} >>> TokenApi -> feature is available.", DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss")); new Thread(Thread_CheckToken).Start(); } else { Console.WriteLine("{0} >>> TokenApi -> ApiKey was not found.", DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss")); } // Open editor? if (string.IsNullOrEmpty(Configs.token) && !conf) { Console.WriteLine("Do you want to edit server config manually? [Y/N]"); if (Console.ReadKey().Key == ConsoleKey.Y) { Process proc = null; try { proc = Process.Start(new ProcessStartInfo() { FileName = "notepad++.exe", Arguments = " \"" + Path.Combine(Application.StartupPath, "server_config.ini") + "\" ", WindowStyle = ProcessWindowStyle.Minimized }); MessageBox.Show("Please Edit server config in Notepad++!" + Environment.NewLine + "Don't forget to click save button!", "CSGO Server Manager", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } catch { if (File.Exists(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Kxnrl", "Notepad", "notepad++.exe"))) { proc = Process.Start(new ProcessStartInfo() { FileName = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Kxnrl", "Notepad", "notepad++.exe"), Arguments = " \"" + Path.Combine(Application.StartupPath, "server_config.ini") + "\" ", WindowStyle = ProcessWindowStyle.Minimized }); MessageBox.Show("Please Edit server config in Notepad++!" + Environment.NewLine + "Don't forget to click save button!", "CSGO Server Manager", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } else { proc = Process.Start(new ProcessStartInfo() { FileName = "notepad.exe", Arguments = " \"" + Path.Combine(Application.StartupPath, "server_config.ini") + "\" ", WindowStyle = ProcessWindowStyle.Minimized }); MessageBox.Show("Please Edit server config in Notepad!" + Environment.NewLine + "Don't forget to click save button!", "CSGO Server Manager", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } } finally { if (proc != null) { // reverse Win32Api.Window.Show(proc.MainWindowHandle); Win32Api.Window.Active(proc.MainWindowHandle); } Environment.Exit(0); } } } // check a2s key A2S.CheckFirewall(); // check server chan Logger.Check(); // current Win32Api.Window.Hide(myHwnd); currentShow = false; Global.tcrash = new Thread(Thread_CheckCrashs) { IsBackground = true, Name = "Crash Thread" }; Global.tcrash.Start(); new Thread(() => { tray.notifyMenu = new ContextMenu(); tray.showHide = new MenuItem("Show"); tray.exitButton = new MenuItem("Exit"); tray.notifyMenu.MenuItems.Add(0, tray.showHide); tray.notifyMenu.MenuItems.Add(1, tray.exitButton); tray.notifyIcon = new NotifyIcon() { BalloonTipIcon = ToolTipIcon.Info, ContextMenu = tray.notifyMenu, Text = "CSGO Server Manager", Icon = Properties.Resources.lilia, Visible = true, }; tray.showHide.Click += new EventHandler(ApplicationHandler_TrayIcon); tray.exitButton.Click += new EventHandler(ApplicationHandler_TrayIcon); Application.Run(); }).Start(); Thread.Sleep(5000); tray.notifyIcon.BalloonTipTitle = "CSGO Server Manager"; tray.notifyIcon.BalloonTipText = "Server Started!"; tray.notifyIcon.ShowBalloonTip(5000); while (true) { var input = Console.ReadLine(); if (Global.update) { Console.WriteLine("Updating ..."); continue; } if (Global.crash) { Console.WriteLine("Restarting ..."); continue; } switch (input.ToLower()) { case "show": Win32Api.Window.Show(Global.srcds.MainWindowHandle); Console.WriteLine("Show SRCDS window."); break; case "hide": Win32Api.Window.Hide(Global.srcds.MainWindowHandle); Console.WriteLine("Hide SRCDS window."); break; case "quit": Environment.Exit(0); break; case "exit": Environment.Exit(0); break; case "update": for (var cd = 60; cd > 0; cd--) { Console.WriteLine("Server restart in " + cd + " seconds"); Win32Api.Message.Write(Global.srcds.MainWindowHandle, "say Server restart in " + cd + " seconds"); Win32Api.Message.Send(Global.srcds.MainWindowHandle); Thread.Sleep(1000); if (Global.crash) { break; } } Logger.Log("Trigger server update."); Global.update = true; Global.tupdate.Abort(); Global.tupdate = null; new Thread(Thread_UpdateCSGO).Start(); break; case "restart": Logger.Log("Trigger server restart."); Global.tupdate.Abort(); Global.tcrash.Abort(); Global.tupdate = null; Global.tcrash = null; Global.srcds.EnableRaisingEvents = false; Global.srcds.Exited -= Srcds_OnExited; Helper.KillSRCDS(true); Global.tcrash = new Thread(Thread_CheckCrashs) { IsBackground = true, Name = "Crash Thread" }; Global.tcrash.Start(); break; default: if (input.StartsWith("exec ")) { input = input.Replace("exec ", ""); if (input.Length > 1) { Win32Api.Message.Write(Global.srcds.MainWindowHandle, input); Win32Api.Message.Send(Global.srcds.MainWindowHandle); Logger.Log("Execute server command: " + input); } else { Console.WriteLine("Command is invalid."); } } else { Console.WriteLine("Commands: "); Console.WriteLine("show - show srcds console window."); Console.WriteLine("hide - hide srcds console window."); Console.WriteLine("exec - exec command into srcds."); Console.WriteLine("quit - quit srcds and application."); Console.WriteLine("exit - quit srcds and application."); Console.WriteLine("update - force srcds update."); Console.WriteLine("restart - force srcds restart."); } break; } } }
private static void ConfigFile_OnRenamed(object sender, RenamedEventArgs e) { Console.WriteLine("{0} >>> Detected: Configs file 'server_config.ini' was deleted.", DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss")); Console.WriteLine("If you want to delete 'server_config.ini', please quit the application first.", DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss")); Configs.Restore(); }