From b7090b2550c7ca97f3f161ecd42ea06770d146b0 Mon Sep 17 00:00:00 2001 From: David Cernat Date: Sat, 1 Dec 2018 03:03:39 +0200 Subject: [PATCH] [Server] Add experimental option for not crashing from Lua script errors Additionally, fix return type of GetPluginEnforcementState() --- apps/openmw-mp/Networking.cpp | 11 ++++++++++ apps/openmw-mp/Networking.hpp | 3 +++ apps/openmw-mp/Script/Functions/Server.cpp | 12 ++++++++++- apps/openmw-mp/Script/Functions/Server.hpp | 25 +++++++++++++++++++++- apps/openmw-mp/Script/Script.hpp | 13 +++++++---- 5 files changed, 58 insertions(+), 6 deletions(-) diff --git a/apps/openmw-mp/Networking.cpp b/apps/openmw-mp/Networking.cpp index e5979e241..a3bab8141 100644 --- a/apps/openmw-mp/Networking.cpp +++ b/apps/openmw-mp/Networking.cpp @@ -31,6 +31,7 @@ Networking *Networking::sThis = 0; static int currentMpNum = 0; static bool pluginEnforcementState = true; +static bool scriptErrorIgnoringState = false; Networking::Networking(RakNet::RakPeerInterface *peer) : mclient(nullptr) { @@ -430,6 +431,16 @@ void Networking::setPluginEnforcementState(bool state) pluginEnforcementState = state; } +bool Networking::getScriptErrorIgnoringState() +{ + return scriptErrorIgnoringState; +} + +void Networking::setScriptErrorIgnoringState(bool state) +{ + scriptErrorIgnoringState = state; +} + const Networking &Networking::get() { return *sThis; diff --git a/apps/openmw-mp/Networking.hpp b/apps/openmw-mp/Networking.hpp index f03dd8f08..211bc8c1c 100644 --- a/apps/openmw-mp/Networking.hpp +++ b/apps/openmw-mp/Networking.hpp @@ -56,6 +56,9 @@ namespace mwmp bool getPluginEnforcementState(); void setPluginEnforcementState(bool state); + bool getScriptErrorIgnoringState(); + void setScriptErrorIgnoringState(bool state); + MasterClient *getMasterClient(); void InitQuery(std::string queryAddr, unsigned short queryPort); void setServerPassword(std::string passw) noexcept; diff --git a/apps/openmw-mp/Script/Functions/Server.cpp b/apps/openmw-mp/Script/Functions/Server.cpp index a0b1faa8f..5f188b61a 100644 --- a/apps/openmw-mp/Script/Functions/Server.cpp +++ b/apps/openmw-mp/Script/Functions/Server.cpp @@ -75,11 +75,16 @@ bool ServerFunctions::HasPassword() noexcept return mwmp::Networking::get().isPassworded(); } -int ServerFunctions::GetPluginEnforcementState() noexcept +bool ServerFunctions::GetPluginEnforcementState() noexcept { return mwmp::Networking::getPtr()->getPluginEnforcementState(); } +bool ServerFunctions::GetScriptErrorIgnoringState() noexcept +{ + return mwmp::Networking::getPtr()->getScriptErrorIgnoringState(); +} + void ServerFunctions::SetGameMode(const char *gameMode) noexcept { if (mwmp::Networking::getPtr()->getMasterClient()) @@ -102,6 +107,11 @@ void ServerFunctions::SetPluginEnforcementState(bool state) noexcept mwmp::Networking::getPtr()->setPluginEnforcementState(state); } +void ServerFunctions::SetScriptErrorIgnoringState(bool state) noexcept +{ + mwmp::Networking::getPtr()->setScriptErrorIgnoringState(state); +} + void ServerFunctions::SetRuleString(const char *key, const char *value) noexcept { auto mc = mwmp::Networking::getPtr()->getMasterClient(); diff --git a/apps/openmw-mp/Script/Functions/Server.hpp b/apps/openmw-mp/Script/Functions/Server.hpp index a8cfd5abf..7d4b0f10f 100644 --- a/apps/openmw-mp/Script/Functions/Server.hpp +++ b/apps/openmw-mp/Script/Functions/Server.hpp @@ -18,11 +18,13 @@ {"GetPort", ServerFunctions::GetPort},\ {"HasPassword", ServerFunctions::HasPassword},\ {"GetPluginEnforcementState", ServerFunctions::GetPluginEnforcementState},\ + {"GetScriptErrorIgnoringState", ServerFunctions::GetScriptErrorIgnoringState},\ \ {"SetGameMode", ServerFunctions::SetGameMode},\ {"SetHostname", ServerFunctions::SetHostname},\ {"SetServerPassword", ServerFunctions::SetServerPassword},\ {"SetPluginEnforcementState", ServerFunctions::SetPluginEnforcementState},\ + {"SetScriptErrorIgnoringState", ServerFunctions::SetScriptErrorIgnoringState},\ {"SetRuleString", ServerFunctions::SetRuleString},\ {"SetRuleValue", ServerFunctions::SetRuleValue},\ {"AddPluginHash", ServerFunctions::AddPluginHash} @@ -121,7 +123,16 @@ public: * * \return The enforcement state. */ - static int GetPluginEnforcementState() noexcept; + static bool GetPluginEnforcementState() noexcept; + + /** + * \brief Get the script error ignoring state of the server. + * + * If true, script errors will not crash the server. + * + * \return The script error ignoring state. + */ + static bool GetScriptErrorIgnoringState() noexcept; /** * \brief Set the game mode of the server, as displayed in the server browser. @@ -157,6 +168,18 @@ public: */ static void SetPluginEnforcementState(bool state) noexcept; + /** + * \brief Set whether script errors should be ignored or not. + * + * If true, script errors will not crash the server, but could have any number + * of unforeseen consequences, which is why this is a highly experimental + * setting. + * + * \param state The new script error ignoring state. + * \return void + */ + static void SetScriptErrorIgnoringState(bool state) noexcept; + /** * \brief Set a rule string for the server details displayed in the server browser. * diff --git a/apps/openmw-mp/Script/Script.hpp b/apps/openmw-mp/Script/Script.hpp index ffa98d712..903cdf975 100644 --- a/apps/openmw-mp/Script/Script.hpp +++ b/apps/openmw-mp/Script/Script.hpp @@ -4,15 +4,18 @@ #ifndef PLUGINSYSTEM3_SCRIPT_HPP #define PLUGINSYSTEM3_SCRIPT_HPP + +#include +#include +#include + #include "Types.hpp" #include "SystemInterface.hpp" #include "ScriptFunction.hpp" #include "ScriptFunctions.hpp" #include "Language.hpp" -#include -#include -#include +#include "Networking.hpp" class Script : private ScriptFunctions { @@ -98,7 +101,9 @@ public: catch (std::exception &e) { LOG_MESSAGE_SIMPLE(Log::LOG_ERROR, e.what()); - throw; + + if (!mwmp::Networking::getPtr()->getScriptErrorIgnoringState()) + throw; } } #endif