From e8ef7de4b995f9d6a30e4a7b4845cc48b2da9d7d Mon Sep 17 00:00:00 2001 From: Lion Kortlepel Date: Mon, 19 Dec 2022 12:41:45 +0100 Subject: [PATCH] fix a bug in TriggerLocalEvent which would cause the server to std::terminate() it was refusing to push multiple return values onto a result table. Not sure what changed to make this happen, but oh well. --- src/TLuaEngine.cpp | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/src/TLuaEngine.cpp b/src/TLuaEngine.cpp index 572d228..becd5eb 100644 --- a/src/TLuaEngine.cpp +++ b/src/TLuaEngine.cpp @@ -5,6 +5,7 @@ #include "LuaAPI.h" #include "TLuaPlugin.h" #include "Uuid.h" +#include "sol/types.hpp" #include #include @@ -514,17 +515,21 @@ sol::table TLuaEngine::StateThreadData::Lua_TriggerGlobalEvent(const std::string sol::table TLuaEngine::StateThreadData::Lua_TriggerLocalEvent(const std::string& EventName, sol::variadic_args EventArgs) { // TODO: make asynchronous? - sol::table Result = mStateView.create_table(); + auto Result = mStateView.create_table(); for (const auto& Handler : mEngine->GetEventHandlersForState(EventName, mStateId)) { auto Fn = mStateView[Handler]; if (Fn.valid() && Fn.get_type() == sol::type::function) { auto FnRet = Fn(EventArgs); if (FnRet.valid()) { - Result.add(FnRet); + for (const auto& Res : FnRet) { + Result.add(Res); + } } else { sol::error Err = FnRet; beammp_lua_error(std::string("TriggerLocalEvent: ") + Err.what()); } + } else { + beammp_lua_errorf("Handler '{}' for event '{}' in state '{}' is NOT a function, and will be ignored.", Handler, EventName, mStateId); } } return Result; @@ -790,9 +795,10 @@ TLuaEngine::StateThreadData::StateThreadData(const std::string& Name, TLuaStateI MPTable.set_function("TriggerGlobalEvent", [&](const std::string& EventName, sol::variadic_args EventArgs) -> sol::table { return Lua_TriggerGlobalEvent(EventName, EventArgs); }); - MPTable.set_function("TriggerLocalEvent", [&](const std::string& EventName, sol::variadic_args EventArgs) -> sol::table { - return Lua_TriggerLocalEvent(EventName, EventArgs); - }); + MPTable.set_function( + "TriggerLocalEvent", [&](const std::string& EventName, sol::variadic_args EventArgs) -> auto{ + return Lua_TriggerLocalEvent(EventName, EventArgs); + }); MPTable.set_function("TriggerClientEvent", &LuaAPI::MP::TriggerClientEvent); MPTable.set_function("TriggerClientEventJson", &LuaAPI::MP::TriggerClientEventJson); MPTable.set_function("GetPlayerCount", &LuaAPI::MP::GetPlayerCount);