From c2853d9271bb10b856266b4f678b379b85c82adc Mon Sep 17 00:00:00 2001 From: CPunch Date: Sat, 17 Apr 2021 19:39:39 -0500 Subject: [PATCH] Added "/rscripts" command to reload all script states --- src/CustomCommands.cpp | 9 +++++++++ src/lua/LuaManager.cpp | 16 ++++++++++++++-- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/src/CustomCommands.cpp b/src/CustomCommands.cpp index 3a238dc..b6cfc74 100644 --- a/src/CustomCommands.cpp +++ b/src/CustomCommands.cpp @@ -10,6 +10,8 @@ #include "Transport.hpp" #include "Missions.hpp" +#include "lua/LuaManager.hpp" + #include #include #include @@ -1190,6 +1192,12 @@ static void pathCommand(std::string full, std::vector& args, CNSock Chat::sendServerMessage(sock, "[PATH] Unknown argument '" + args[1] + "'"); } +static void reloadScriptsCommand(std::string full, std::vector& args, CNSocket *sock) { + // reloads all scripts + LuaManager::stopScripts(); + LuaManager::loadScripts(); +} + static void registerCommand(std::string cmd, int requiredLevel, CommandHandler handlr, std::string help) { commands[cmd] = ChatCommand(requiredLevel, handlr, help); } @@ -1229,4 +1237,5 @@ void CustomCommands::init() { registerCommand("unregisterall", 50, unregisterallCommand, "clear all SCAMPER and MSS destinations"); registerCommand("redeem", 100, redeemCommand, "redeem a code item"); registerCommand("path", 30, pathCommand, "edit NPC paths"); + registerCommand("rscripts", 30, reloadScriptsCommand, "stops all script states and reloads all scripts"); } diff --git a/src/lua/LuaManager.cpp b/src/lua/LuaManager.cpp index 2dbcc4d..bcba9de 100644 --- a/src/lua/LuaManager.cpp +++ b/src/lua/LuaManager.cpp @@ -4,6 +4,7 @@ #include "lua/WorldWrapper.hpp" #include "lua/PlayerWrapper.hpp" +#include "PlayerManager.hpp" #include "servers/CNShardServer.hpp" #include "settings.hpp" @@ -61,7 +62,9 @@ struct scheduledThread { }; std::map scheduleQueue; -// pauses the script for x seconds +// pauses the script for x seconds, not very accurate but should be +// called within ~60ms or less of when it was schedueled +// will also return the time the thread was paused int OF_wait(lua_State *state) { double seconds = luaL_checknumber(state, 1); @@ -121,7 +124,8 @@ void LuaManager::init() { activeScripts = std::map(); - REGISTER_SHARD_TIMER(luaScheduler, 200); + // we want to be called after every poll(), so our timer delta is set to 0 + REGISTER_SHARD_TIMER(luaScheduler, 0); // load our scripts loadScripts(); @@ -142,6 +146,14 @@ void LuaManager::stopScripts() { // finally clear the map activeScripts.clear(); + + // walk through each player and unregister each event + for (auto pair: PlayerManager::players) { + if (pair.second->onChat != nullptr) { + delete pair.second->onChat; + pair.second->onChat = nullptr; + } + } } void LuaManager::loadScripts() {