diff --git a/Server/Program.cs b/Server/Program.cs index 67eb502..a66d887 100644 --- a/Server/Program.cs +++ b/Server/Program.cs @@ -11,6 +11,7 @@ using Timer = System.Timers.Timer; Server.Server server = new Server.Server(); HashSet shineBag = new HashSet(); CancellationTokenSource cts = new CancellationTokenSource(); +bool restartRequested = false; Task listenTask = server.Listen(cts.Token); Logger consoleLogger = new Logger("Console"); DiscordBot bot = new DiscordBot(); @@ -598,6 +599,21 @@ CommandHandler.RegisterCommand("loadsettings", _ => { return "Loaded settings.json"; }); +CommandHandler.RegisterCommand("restartserver", args => +{ + if (args.Length != 0) + { + return "Usage: restartserver (no arguments)"; + } + else + { + consoleLogger.Info("Received restartserver command"); + restartRequested = true; + cts.Cancel(); + return "Restarting..."; + } +}); + Console.CancelKeyPress += (_, e) => { e.Cancel = true; consoleLogger.Info("Received Ctrl+C"); @@ -623,4 +639,16 @@ Task.Run(() => { }).ContinueWith(x => { if (x.Exception != null) { consoleLogger.Error(x.Exception.ToString()); } }); #pragma warning restore CS4014 -await listenTask; \ No newline at end of file +await listenTask; +if (restartRequested) //need to do this here because this needs to happen after the listener closes, and there isn't an + //easy way to sync in the restartserver command without it exiting Main() +{ + string? path = System.Reflection.Assembly.GetEntryAssembly()?.GetName().Name; + const string unableToStartMsg = "Unable to ascertain the executable location, you'll need to re-run the server manually."; + if (path != null) //path is probably just "Server", but in the context of the assembly, that's all you need to restart it. + { + Console.WriteLine($"Server Running on (pid): {System.Diagnostics.Process.Start(path)?.Id.ToString() ?? unableToStartMsg}"); + } + else + consoleLogger.Info(unableToStartMsg); +} \ No newline at end of file diff --git a/Server/Server.cs b/Server/Server.cs index 43f278d..87cca84 100644 --- a/Server/Server.cs +++ b/Server/Server.cs @@ -58,6 +58,7 @@ public class Server { } Logger.Info("Server closed"); + Console.WriteLine("\n\n\n"); //for the sake of the restart command. } }