From 3f12a8542ad62ec12367c8ff3205b4404fa2a881 Mon Sep 17 00:00:00 2001 From: Luan Luciano Date: Thu, 11 Jul 2024 06:20:34 -0300 Subject: [PATCH] feat: new global event OnSave (#2025) --- data/libs/functions/revscriptsys.lua | 4 ++++ src/game/game.cpp | 5 ++++- src/lua/functions/core/game/global_functions.cpp | 2 ++ src/lua/functions/events/global_event_functions.cpp | 2 ++ src/lua/functions/events/global_event_functions.hpp | 1 + src/lua/global/globalevent.cpp | 13 ++++++++++++- src/lua/global/globalevent.hpp | 2 ++ src/lua/lua_definitions.hpp | 1 + src/server/signals.cpp | 2 ++ 9 files changed, 30 insertions(+), 2 deletions(-) diff --git a/data/libs/functions/revscriptsys.lua b/data/libs/functions/revscriptsys.lua index 84b8275e3ce..515522d6443 100644 --- a/data/libs/functions/revscriptsys.lua +++ b/data/libs/functions/revscriptsys.lua @@ -233,6 +233,10 @@ do self:type("periodchange") self:onPeriodChange(value) return + elseif key == "onSave" then + self:type("save") + self:onSave(value) + return end rawset(self, key, value) end diff --git a/src/game/game.cpp b/src/game/game.cpp index 702c46f3f2d..2348d384569 100644 --- a/src/game/game.cpp +++ b/src/game/game.cpp @@ -593,7 +593,8 @@ void Game::setGameState(GameState_t newState) { } case GAME_STATE_SHUTDOWN: { - g_globalEvents().execute(GLOBALEVENT_SHUTDOWN); + g_globalEvents().save(); + g_globalEvents().shutdown(); // kick all players that are still online auto it = players.begin(); @@ -611,6 +612,8 @@ void Game::setGameState(GameState_t newState) { } case GAME_STATE_CLOSED: { + g_globalEvents().save(); + /* kick all players without the CanAlwaysLogin flag */ auto it = players.begin(); while (it != players.end()) { diff --git a/src/lua/functions/core/game/global_functions.cpp b/src/lua/functions/core/game/global_functions.cpp index c8a857cfffa..bed0ff27026 100644 --- a/src/lua/functions/core/game/global_functions.cpp +++ b/src/lua/functions/core/game/global_functions.cpp @@ -16,6 +16,7 @@ #include "lua/functions/core/game/global_functions.hpp" #include "lua/scripts/lua_environment.hpp" #include "lua/scripts/script_environment.hpp" +#include "lua/global/globalevent.hpp" #include "server/network/protocol/protocolstatus.hpp" #include "creatures/players/wheel/player_wheel.hpp" #include "lua/global/lua_timer_event_descr.hpp" @@ -706,6 +707,7 @@ int GlobalFunctions::luaStopEvent(lua_State* L) { } int GlobalFunctions::luaSaveServer(lua_State* L) { + g_globalEvents().save(); g_saveManager().scheduleAll(); pushBoolean(L, true); return 1; diff --git a/src/lua/functions/events/global_event_functions.cpp b/src/lua/functions/events/global_event_functions.cpp index ea6a5f7df03..129a466e97c 100644 --- a/src/lua/functions/events/global_event_functions.cpp +++ b/src/lua/functions/events/global_event_functions.cpp @@ -40,6 +40,8 @@ int GlobalEventFunctions::luaGlobalEventType(lua_State* L) { global->setEventType(GLOBALEVENT_PERIODCHANGE); } else if (tmpStr == "onthink") { global->setEventType(GLOBALEVENT_ON_THINK); + } else if (tmpStr == "save") { + global->setEventType(GLOBALEVENT_SAVE); } else { g_logger().error("[GlobalEventFunctions::luaGlobalEventType] - " "Invalid type for global event: {}"); diff --git a/src/lua/functions/events/global_event_functions.hpp b/src/lua/functions/events/global_event_functions.hpp index 6c988a61ca1..e04072a24e4 100644 --- a/src/lua/functions/events/global_event_functions.hpp +++ b/src/lua/functions/events/global_event_functions.hpp @@ -25,6 +25,7 @@ class GlobalEventFunctions final : LuaScriptInterface { registerMethod(L, "GlobalEvent", "onShutdown", GlobalEventFunctions::luaGlobalEventOnCallback); registerMethod(L, "GlobalEvent", "onRecord", GlobalEventFunctions::luaGlobalEventOnCallback); registerMethod(L, "GlobalEvent", "onPeriodChange", GlobalEventFunctions::luaGlobalEventOnCallback); + registerMethod(L, "GlobalEvent", "onSave", GlobalEventFunctions::luaGlobalEventOnCallback); } private: diff --git a/src/lua/global/globalevent.cpp b/src/lua/global/globalevent.cpp index 04420431def..32fa1a01ab1 100644 --- a/src/lua/global/globalevent.cpp +++ b/src/lua/global/globalevent.cpp @@ -66,6 +66,14 @@ void GlobalEvents::startup() const { execute(GLOBALEVENT_STARTUP); } +void GlobalEvents::shutdown() const { + execute(GLOBALEVENT_SHUTDOWN); +} + +void GlobalEvents::save() const { + execute(GLOBALEVENT_SAVE); +} + void GlobalEvents::timer() { time_t now = time(nullptr); @@ -165,7 +173,8 @@ GlobalEventMap GlobalEvents::getEventMap(GlobalEvent_t type) { case GLOBALEVENT_PERIODCHANGE: case GLOBALEVENT_STARTUP: case GLOBALEVENT_SHUTDOWN: - case GLOBALEVENT_RECORD: { + case GLOBALEVENT_RECORD: + case GLOBALEVENT_SAVE: { GlobalEventMap retMap; for (const auto &it : serverMap) { if (it.second->getEventType() == type) { @@ -196,6 +205,8 @@ std::string GlobalEvent::getScriptTypeName() const { return "onPeriodChange"; case GLOBALEVENT_ON_THINK: return "onThink"; + case GLOBALEVENT_SAVE: + return "onSave"; default: g_logger().error("[GlobalEvent::getScriptTypeName] - Invalid event type"); return std::string(); diff --git a/src/lua/global/globalevent.hpp b/src/lua/global/globalevent.hpp index 004c6cb5f8c..128a743b3fb 100644 --- a/src/lua/global/globalevent.hpp +++ b/src/lua/global/globalevent.hpp @@ -29,6 +29,8 @@ class GlobalEvents final : public Scripts { } void startup() const; + void shutdown() const; + void save() const; void timer(); void think(); diff --git a/src/lua/lua_definitions.hpp b/src/lua/lua_definitions.hpp index 083871e237b..81add18e516 100644 --- a/src/lua/lua_definitions.hpp +++ b/src/lua/lua_definitions.hpp @@ -108,6 +108,7 @@ enum GlobalEvent_t { GLOBALEVENT_RECORD, GLOBALEVENT_PERIODCHANGE, GLOBALEVENT_ON_THINK, + GLOBALEVENT_SAVE, }; enum ModuleType_t { diff --git a/src/server/signals.cpp b/src/server/signals.cpp index c85b21312eb..626a918f677 100644 --- a/src/server/signals.cpp +++ b/src/server/signals.cpp @@ -15,6 +15,7 @@ #include "lib/thread/thread_pool.hpp" #include "lua/creature/events.hpp" #include "lua/scripts/lua_environment.hpp" +#include "lua/global/globalevent.hpp" #include "server/signals.hpp" Signals::Signals(asio::io_service &service) : @@ -92,6 +93,7 @@ void Signals::sigtermHandler() { void Signals::sigusr1Handler() { // Dispatcher thread g_logger().info("SIGUSR1 received, saving the game state..."); + g_globalEvents().save(); g_saveManager().scheduleAll(); }