diff --git a/SimpleConsole/Program.cs b/SimpleConsole/Program.cs index 8d412c4..41d2ffe 100644 --- a/SimpleConsole/Program.cs +++ b/SimpleConsole/Program.cs @@ -3,6 +3,7 @@ using System.Diagnostics; using System.Drawing; using System.Globalization; +using System.IO; using System.Linq; using System.Net.Mime; using System.Net.NetworkInformation; @@ -38,6 +39,10 @@ static void Main(string[] args) if (portfound) { Console.WriteLine("Loading"); + List cmdargs = new List(); + string command = string.Empty; + + if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { @@ -46,101 +51,67 @@ static void Main(string[] args) { Console.WriteLine("Starting game server..."); - Process process = new Process(); - -// Stop the process from opening a new window - process.StartInfo.RedirectStandardOutput = true; - process.StartInfo.UseShellExecute = false; - process.StartInfo.CreateNoWindow = true; - -// Setup executable and parameters string file = AppDomain.CurrentDomain.BaseDirectory + "/SCP_ET.exe"; Console.WriteLine(file); - process.StartInfo.FileName = file; - process.StartInfo.Arguments = "-consoleport " + port + " -logfile " + - AppDomain.CurrentDomain.BaseDirectory + "/logs/SCP-ETServerLog-" + - DateTime.UtcNow.Ticks + ".txt"; - Console.WriteLine(process.StartInfo.Arguments); -// Go - process.Start(); - gameprocess = process; + command = file; + cmdargs.Add("-consoleport"); + cmdargs.Add(port.ToString()); + cmdargs.Add("-logfile"); + cmdargs.Add(AppDomain.CurrentDomain.BaseDirectory + "/logs/SCP-ETServerLog-" + DateTime.UtcNow.Ticks + ".txt"); } else { Console.WriteLine("Starting game server..."); - Process process = new Process(); - -// Stop the process from opening a new window - process.StartInfo.RedirectStandardOutput = true; - process.StartInfo.UseShellExecute = false; - process.StartInfo.CreateNoWindow = true; - -// Setup executable and parameters - process.StartInfo.FileName = GetArg("-gamelocation") + "/SCP_ET.exe"; - process.StartInfo.Arguments = "-consoleport " + port + " -logfile " + - GetArg("-gamelocation") + "/logs/SCP-ETServerLog-" + - DateTime.UtcNow.Ticks + ".txt"; - Console.WriteLine(process.StartInfo.Arguments); - -// Go - process.Start(); - gameprocess = process; + string file = GetArg("-gamelocation") + "/SCP_ET.exe"; + Console.WriteLine(file); + command = file; + cmdargs.Add("-consoleport"); + cmdargs.Add(port.ToString()); + cmdargs.Add("-logfile"); + cmdargs.Add(GetArg("-gamelocation") + "/logs/SCP-ETServerLog-" + DateTime.UtcNow.Ticks + ".txt"); } } - - if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) + else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) { Console.WriteLine("Platform linux"); if (string.IsNullOrEmpty(GetArg("-gamelocation"))) { Console.WriteLine("Starting game server..."); - Process process = new Process(); - -// Stop the process from opening a new window - process.StartInfo.RedirectStandardOutput = true; - process.StartInfo.UseShellExecute = false; - process.StartInfo.CreateNoWindow = true; - -// Setup executable and parameters - process.StartInfo.FileName = AppDomain.CurrentDomain.BaseDirectory + "/scp_et.x86_64"; - process.StartInfo.Arguments = "-consoleport " + port + " -logfile " + - AppDomain.CurrentDomain.BaseDirectory + "/logs/SCP-ETServerLog-" + - DateTime.UtcNow.Ticks + ".txt"; - Console.WriteLine(process.StartInfo.Arguments); - -// Go - process.Start(); - gameprocess = process; + string file = AppDomain.CurrentDomain.BaseDirectory + "/scp_et.x86_64"; + Console.WriteLine(file); + command = file; + cmdargs.Add("-consoleport"); + cmdargs.Add(port.ToString()); + cmdargs.Add("-logfile"); + cmdargs.Add(AppDomain.CurrentDomain.BaseDirectory + "/logs/SCP-ETServerLog-" + DateTime.UtcNow.Ticks + ".txt"); } else { Console.WriteLine("Starting game server..."); - Process process = new Process(); - -// Stop the process from opening a new window - process.StartInfo.RedirectStandardOutput = true; - process.StartInfo.UseShellExecute = false; - process.StartInfo.CreateNoWindow = true; - -// Setup executable and parameters - process.StartInfo.FileName = GetArg("-gamelocation") + "/scp_et.x86_64"; - process.StartInfo.Arguments = "-consoleport " + port + " -logfile " + - GetArg("-gamelocation") + "/logs/SCP-ETServerLog-" + - DateTime.UtcNow.Ticks + ".txt"; - Console.WriteLine(process.StartInfo.Arguments); - -// Go - process.Start(); - gameprocess = process; + string file = GetArg("-gamelocation") + "/scp_et.x86_64"; + Console.WriteLine(file); + command = file; + cmdargs.Add("-consoleport"); + cmdargs.Add(port.ToString()); + cmdargs.Add("-logfile"); + cmdargs.Add(GetArg("-gamelocation") + "/logs/SCP-ETServerLog-" + DateTime.UtcNow.Ticks + ".txt"); } } - console = new TcpConsoleClient(); - console.ConnectToTcpServer(); - listeninput(); + ProcessStartInfo info2 = new ProcessStartInfo(Path.GetFullPath(command), string.Join(' ', cmdargs)); + + using (Process cmd = Process.Start(info2)) + { + console = new TcpConsoleClient(); + console.ConnectToTcpServer(); + AppDomain.CurrentDomain.DomainUnload += (s, e) => { cmd.Kill(); cmd.WaitForExit(); }; + AppDomain.CurrentDomain.ProcessExit += (s, e) => { cmd.Kill(); cmd.WaitForExit(); }; + AppDomain.CurrentDomain.UnhandledException += (s, e) => { cmd.Kill(); cmd.WaitForExit(); }; + listeninput(); + } } }