0
0
Fork 0
mirror of https://github.com/Sanae6/SmoOnlineServer.git synced 2024-12-22 00:10:23 +00:00

Merge pull request #19 from TheUbMunster/restart-command

This commit is contained in:
Sanae 2022-07-29 13:19:04 -06:00 committed by GitHub
commit e7a3347a37
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 1 deletions

View file

@ -11,6 +11,7 @@ using Timer = System.Timers.Timer;
Server.Server server = new Server.Server();
HashSet<int> shineBag = new HashSet<int>();
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;
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);
}

View file

@ -58,6 +58,7 @@ public class Server {
}
Logger.Info("Server closed");
Console.WriteLine("\n\n\n"); //for the sake of the restart command.
}
}