From 5862e6e1cfd7f1cceb4e964bffaeb4df5f8811ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Paulo?= Date: Fri, 8 Mar 2024 11:21:26 -0300 Subject: [PATCH] * Save players on closing server --- src/canary_server.cpp | 28 ++++++++++++++++------------ src/canary_server.hpp | 2 +- 2 files changed, 17 insertions(+), 13 deletions(-) diff --git a/src/canary_server.cpp b/src/canary_server.cpp index 56cd53bf567..4a476046264 100644 --- a/src/canary_server.cpp +++ b/src/canary_server.cpp @@ -42,7 +42,6 @@ CanaryServer::CanaryServer( rsa(rsa), serviceManager(serviceManager) { logInfos(); - toggleForceCloseButton(); g_game().setGameState(GAME_STATE_STARTUP); std::set_new_handler(badAllocationHandler); srand(static_cast(OTSYS_TIME())); @@ -51,6 +50,7 @@ CanaryServer::CanaryServer( #ifdef _WIN32 SetConsoleTitleA(ProtocolStatus::SERVER_NAME.c_str()); + saveServerAtClose(); #endif } @@ -216,18 +216,22 @@ void CanaryServer::logInfos() { } /** - *It is preferable to keep the close button off as it closes the server without saving (this can cause the player to lose items from houses and others informations, since windows automatically closes the process in five seconds, when forcing the close) - * Choose to use "CTROL + C" or "CTROL + BREAK" for security close - * To activate/deactivate window; - * \param MF_GRAYED Disable the "x" (force close) button - * \param MF_ENABLED Enable the "x" (force close) button + *Save players on server closing */ -void CanaryServer::toggleForceCloseButton() { -#ifdef OS_WINDOWS - const HWND hwnd = GetConsoleWindow(); - const HMENU hmenu = GetSystemMenu(hwnd, FALSE); - EnableMenuItem(hmenu, SC_CLOSE, MF_GRAYED); -#endif +void CanaryServer::saveServerAtClose() { + SetConsoleCtrlHandler( + [](DWORD ctrlType) -> BOOL { + if (ctrlType == CTRL_CLOSE_EVENT) { + if (!g_game().getGameState() == GAME_STATE_STARTUP) { + g_logger().info("Closing the server and saving the players"); + g_saveManager().scheduleAll(); + exit(0); + } + } + return FALSE; + }, + TRUE + ); } void CanaryServer::badAllocationHandler() { diff --git a/src/canary_server.hpp b/src/canary_server.hpp index bb22232b8e4..c97e29a2e83 100644 --- a/src/canary_server.hpp +++ b/src/canary_server.hpp @@ -53,7 +53,6 @@ class CanaryServer { std::atomic loaderStatus = LoaderStatus::LOADING; void logInfos(); - static void toggleForceCloseButton(); static void badAllocationHandler(); static void shutdown(); @@ -67,4 +66,5 @@ class CanaryServer { void loadMaps() const; void setupHousesRent(); void modulesLoadHelper(bool loaded, std::string moduleName); + void saveServerAtClose(); };