diff --git a/CHANGELOG.md b/CHANGELOG.md index 1192799c..d1d6ac53 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,7 @@ This is the rolling changelog for TShock for Terraria. Use past tense when addin * Added `/slay` as an alias for `/kill` to be more consistent with other server mods. (@hakusaro) * Added `/god` as an alias for `/godmode` to be more consistent with other server mods. (@hakusaro) * Fixed ridiculous typo in `Amethyst Gemtree` text. (@hakusaro) +* Fixed `CTRL + C` / interactive console interrupt not safely shutting down the server. Now, interrupts will cause a safe shutdown (saving the world and disconnecting all players before fully shutting down). Previously, interrupts caused an unsafe shutdown (not saving the world). (@hakusaro) ## TShock 4.5.4 * Fixed ridiculous typo in `GetDataHandlers` which caused TShock to read the wrong field in the packet for `usingBiomeTorches`. (@hakusaro, @Arthri) diff --git a/TShockAPI/TShock.cs b/TShockAPI/TShock.cs index c2bfbc39..ed74b882 100644 --- a/TShockAPI/TShock.cs +++ b/TShockAPI/TShock.cs @@ -214,6 +214,8 @@ namespace TShockAPI TerrariaApi.Reporting.CrashReporter.HeapshotRequesting += CrashReporter_HeapshotRequesting; + Console.CancelKeyPress += new ConsoleCancelEventHandler(ConsoleCancelHandler); + try { CliParser.Reset(); @@ -638,6 +640,20 @@ namespace TShockAPI } } + /// ConsoleCancelHandler - Handles when Ctrl + C is sent to the server for a safe shutdown. + /// The sender + /// The ConsoleCancelEventArgs associated with the event. + private void ConsoleCancelHandler(object sender, ConsoleCancelEventArgs args) + { + // Cancel the default behavior + args.Cancel = true; + + Log.ConsoleInfo("Interrupt received. Saving the world and shutting down."); + + // Perform a safe shutdown + TShock.Utils.StopServer(true, "Server console interrupted!"); + } + /// HandleCommandLine - Handles the command line parameters passed to the server. /// parms - The array of arguments passed in through the command line. private void HandleCommandLine(string[] parms)