From 90267c610530d8be1c14288fc742b73d3473050f Mon Sep 17 00:00:00 2001 From: Kae <80987908+Novaenia@users.noreply.github.com> Date: Tue, 15 Aug 2023 14:20:56 +1000 Subject: message.setHandler now accepts a Json config in place of the name --- source/game/scripting/StarLuaComponents.hpp | 49 +++++++++++++++++++++++------ 1 file changed, 40 insertions(+), 9 deletions(-) (limited to 'source/game/scripting/StarLuaComponents.hpp') diff --git a/source/game/scripting/StarLuaComponents.hpp b/source/game/scripting/StarLuaComponents.hpp index 7e30c39..2098829 100644 --- a/source/game/scripting/StarLuaComponents.hpp +++ b/source/game/scripting/StarLuaComponents.hpp @@ -173,7 +173,14 @@ protected: virtual void contextShutdown() override; private: - StringMap m_messageHandlers; + struct MessageHandler { + Maybe function; + String name; + bool passName = true; + bool localOnly = false; + }; + + StringMap m_messageHandlers; }; template @@ -304,13 +311,26 @@ void LuaWorldComponent::uninit() { template LuaMessageHandlingComponent::LuaMessageHandlingComponent() { LuaCallbacks scriptCallbacks; - scriptCallbacks.registerCallback("setHandler", - [this](String message, Maybe handler) { - if (handler) - m_messageHandlers.set(move(message), handler.take()); - else - m_messageHandlers.remove(message); - }); + scriptCallbacks.registerCallback("setHandler", [this](Variant message, Maybe handler) { + MessageHandler handlerInfo = {}; + + if (Json* config = message.ptr()) { + handlerInfo.passName = config->getBool("passName", false); + handlerInfo.localOnly = config->getBool("localOnly", false); + handlerInfo.name = config->getString("name"); + } else { + handlerInfo.passName = true; + handlerInfo.localOnly = false; + handlerInfo.name = message.get(); + } + + if (handler) { + handlerInfo.function.emplace(handler.take()); + m_messageHandlers.set(handlerInfo.name, handlerInfo); + } + else + m_messageHandlers.remove(handlerInfo.name); + }); Base::addCallbacks("message", move(scriptCallbacks)); } @@ -323,7 +343,18 @@ Maybe LuaMessageHandlingComponent::handleMessage( if (auto handler = m_messageHandlers.ptr(message)) { try { - return handler->template invoke(message, localMessage, luaUnpack(args)); + if (handler->localOnly) { + if (!localMessage) + return {}; + else if (handler->passName) + return handler->function->template invoke(message, luaUnpack(args)); + else + return handler->function->template invoke(luaUnpack(args)); + } + else if (handler->passName) + return handler->function->template invoke(message, localMessage, luaUnpack(args)); + else + return handler->function->template invoke(localMessage, luaUnpack(args)); } catch (LuaException const& e) { Logger::error( "Exception while invoking lua message handler for message '{}'. {}", message, outputException(e, true)); -- cgit v1.2.3