From c5dff8b913018e698a7e67ef270062eeaefdb24d Mon Sep 17 00:00:00 2001 From: Lion Kortlepel Date: Fri, 10 May 2024 14:44:51 +0200 Subject: [PATCH 1/2] fix lua assertion on event argument passing When adding elements to a table, the .add() function does not behave as expected in various cases, making it really shit and difficult to use. Instead, we keep our own index and just add one by one. It works, and it's easy to understand. This may lead to indices which are nil, i.e. non-fully-sequential tables, but I can't be asked to worry about it because that shouldn't be an issue when we use .set() everywhere. --- src/TLuaEngine.cpp | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/src/TLuaEngine.cpp b/src/TLuaEngine.cpp index 20421d0..f990ae8 100644 --- a/src/TLuaEngine.cpp +++ b/src/TLuaEngine.cpp @@ -458,7 +458,8 @@ std::vector TLuaEngine::StateThreadData::JsonStringToArray(JsonStri sol::table TLuaEngine::StateThreadData::Lua_TriggerGlobalEvent(const std::string& EventName, sol::variadic_args EventArgs) { auto Table = mStateView.create_table(); - for (const sol::stack_proxy& Arg : EventArgs) { + int i = 1; + for (auto Arg : EventArgs) { switch (Arg.get_type()) { case sol::type::none: case sol::type::userdata: @@ -466,19 +467,20 @@ sol::table TLuaEngine::StateThreadData::Lua_TriggerGlobalEvent(const std::string case sol::type::thread: case sol::type::function: case sol::type::poly: - Table.add(BEAMMP_INTERNAL_NIL); + Table.set(i, BEAMMP_INTERNAL_NIL); beammp_warnf("Passed a value of type '{}' to TriggerGlobalEvent(\"{}\", ...). This type can not be serialized, and cannot be passed between states. It will arrive as in handlers.", sol::type_name(EventArgs.lua_state(), Arg.get_type()), EventName); break; case sol::type::lua_nil: - Table.add(BEAMMP_INTERNAL_NIL); + Table.set(i, BEAMMP_INTERNAL_NIL); break; case sol::type::string: case sol::type::number: case sol::type::boolean: case sol::type::table: - Table.add(Arg); + Table.set(i, Arg); break; } + ++i; } JsonString Str { LuaAPI::MP::JsonEncode(Table) }; beammp_debugf("json: {}", Str.value); @@ -520,11 +522,13 @@ sol::table TLuaEngine::StateThreadData::Lua_TriggerGlobalEvent(const std::string sol::state_view StateView(mState); sol::table Result = StateView.create_table(); auto Vector = Self.get>>("ReturnValueImpl"); + int i = 1; for (const auto& Value : Vector) { if (!Value->Ready) { return sol::lua_nil; } - Result.add(Value->Result); + Result.set(i, Value->Result); + ++i; } return Result; }); @@ -534,12 +538,14 @@ 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(); + int i = 1; 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); + Result.set(i, FnRet); + ++i; } else { sol::error Err = FnRet; beammp_lua_error(std::string("TriggerLocalEvent: ") + Err.what()); From b995a222ff87c3e1408d1b7c9997f7cfdf801a5b Mon Sep 17 00:00:00 2001 From: Lion Kortlepel Date: Fri, 10 May 2024 14:57:22 +0200 Subject: [PATCH 2/2] bump version --- include/Common.h | 2 +- vcpkg | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/include/Common.h b/include/Common.h index 4eb9981..2e886c4 100644 --- a/include/Common.h +++ b/include/Common.h @@ -150,7 +150,7 @@ private: static inline std::mutex mShutdownHandlersMutex {}; static inline std::deque mShutdownHandlers {}; - static inline Version mVersion { 3, 4, 0 }; + static inline Version mVersion { 3, 4, 1 }; }; void SplitString(std::string const& str, const char delim, std::vector& out); diff --git a/vcpkg b/vcpkg index 326d8b4..6978381 160000 --- a/vcpkg +++ b/vcpkg @@ -1 +1 @@ -Subproject commit 326d8b43e365352ba3ccadf388d989082fe0f2a6 +Subproject commit 6978381401d33a5ad6a3385895d12e383083712a