message.setHandler now accepts a Json config in place of the name
This commit is contained in:
parent
6a9bc191fd
commit
90267c6105
@ -8,13 +8,7 @@ end
|
|||||||
|
|
||||||
function module.init()
|
function module.init()
|
||||||
for name, func in pairs(commands) do
|
for name, func in pairs(commands) do
|
||||||
message.setHandler("/" .. name, function(_, isLocal, ...)
|
message.setHandler({name = "/" .. name, localOnly = true}, func)
|
||||||
if not isLocal then
|
|
||||||
return
|
|
||||||
else
|
|
||||||
return func(...)
|
|
||||||
end
|
|
||||||
end)
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -173,7 +173,14 @@ protected:
|
|||||||
virtual void contextShutdown() override;
|
virtual void contextShutdown() override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
StringMap<LuaFunction> m_messageHandlers;
|
struct MessageHandler {
|
||||||
|
Maybe<LuaFunction> function;
|
||||||
|
String name;
|
||||||
|
bool passName = true;
|
||||||
|
bool localOnly = false;
|
||||||
|
};
|
||||||
|
|
||||||
|
StringMap<MessageHandler> m_messageHandlers;
|
||||||
};
|
};
|
||||||
|
|
||||||
template <typename Ret, typename... V>
|
template <typename Ret, typename... V>
|
||||||
@ -304,12 +311,25 @@ void LuaWorldComponent<Base>::uninit() {
|
|||||||
template <typename Base>
|
template <typename Base>
|
||||||
LuaMessageHandlingComponent<Base>::LuaMessageHandlingComponent() {
|
LuaMessageHandlingComponent<Base>::LuaMessageHandlingComponent() {
|
||||||
LuaCallbacks scriptCallbacks;
|
LuaCallbacks scriptCallbacks;
|
||||||
scriptCallbacks.registerCallback("setHandler",
|
scriptCallbacks.registerCallback("setHandler", [this](Variant<String, Json> message, Maybe<LuaFunction> handler) {
|
||||||
[this](String message, Maybe<LuaFunction> handler) {
|
MessageHandler handlerInfo = {};
|
||||||
if (handler)
|
|
||||||
m_messageHandlers.set(move(message), handler.take());
|
if (Json* config = message.ptr<Json>()) {
|
||||||
|
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<String>();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (handler) {
|
||||||
|
handlerInfo.function.emplace(handler.take());
|
||||||
|
m_messageHandlers.set(handlerInfo.name, handlerInfo);
|
||||||
|
}
|
||||||
else
|
else
|
||||||
m_messageHandlers.remove(message);
|
m_messageHandlers.remove(handlerInfo.name);
|
||||||
});
|
});
|
||||||
|
|
||||||
Base::addCallbacks("message", move(scriptCallbacks));
|
Base::addCallbacks("message", move(scriptCallbacks));
|
||||||
@ -323,7 +343,18 @@ Maybe<Json> LuaMessageHandlingComponent<Base>::handleMessage(
|
|||||||
|
|
||||||
if (auto handler = m_messageHandlers.ptr(message)) {
|
if (auto handler = m_messageHandlers.ptr(message)) {
|
||||||
try {
|
try {
|
||||||
return handler->template invoke<Json>(message, localMessage, luaUnpack(args));
|
if (handler->localOnly) {
|
||||||
|
if (!localMessage)
|
||||||
|
return {};
|
||||||
|
else if (handler->passName)
|
||||||
|
return handler->function->template invoke<Json>(message, luaUnpack(args));
|
||||||
|
else
|
||||||
|
return handler->function->template invoke<Json>(luaUnpack(args));
|
||||||
|
}
|
||||||
|
else if (handler->passName)
|
||||||
|
return handler->function->template invoke<Json>(message, localMessage, luaUnpack(args));
|
||||||
|
else
|
||||||
|
return handler->function->template invoke<Json>(localMessage, luaUnpack(args));
|
||||||
} catch (LuaException const& e) {
|
} catch (LuaException const& e) {
|
||||||
Logger::error(
|
Logger::error(
|
||||||
"Exception while invoking lua message handler for message '{}'. {}", message, outputException(e, true));
|
"Exception while invoking lua message handler for message '{}'. {}", message, outputException(e, true));
|
||||||
|
Loading…
Reference in New Issue
Block a user