From d5e810989cc1f52e6fd7a8618bc13831eb1768d2 Mon Sep 17 00:00:00 2001 From: N/A Date: Fri, 11 Nov 2022 22:29:44 +0400 Subject: [PATCH] Use main luastate when registering events handlers By default, when addEventHandler gets called, it passes a coroutine state, which is inacceptible and may get broken in future, this will lead to infinity call of main(). To avoid this problem, we have to use a main luastate instead. Thank you, FYP --- src/RakLua.cpp | 4 +++- src/RakLua.h | 2 +- src/main.cpp | 2 +- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/RakLua.cpp b/src/RakLua.cpp index 5f9affb..12cd057 100644 --- a/src/RakLua.cpp +++ b/src/RakLua.cpp @@ -43,10 +43,12 @@ void RakLua::postRakClientInitialization(uintptr_t rakClientIntf) mState = eInitState::OK; } -bool RakLua::addEventHandler(sol::this_state& ts, eEventType type, sol::function detour) +bool RakLua::addEventHandler(sol::this_state& ts, eEventType type, sol::function &detour_f) { sol::state_view lua{ ts }; int id = lua["script"]["this"]["id"]; + auto main_thread = sol::main_thread(ts); + sol::protected_function detour(main_thread, detour_f); switch (type) { diff --git a/src/RakLua.h b/src/RakLua.h index 16810a9..84fcfcc 100644 --- a/src/RakLua.h +++ b/src/RakLua.h @@ -30,7 +30,7 @@ class RakLua void postRakClientInitialization(uintptr_t rakClientIntf); - bool addEventHandler(sol::this_state& ts, eEventType type, sol::function detour); + bool addEventHandler(sol::this_state& ts, eEventType type, sol::function &detour_f); void destroyHandlers(sol::this_state& ts); inline bool isInitialized() { return mState == eInitState::OK; } diff --git a/src/main.cpp b/src/main.cpp index 9dc2c91..d0d4898 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -234,7 +234,7 @@ sol::table open(sol::this_state ts) gRakLua.initialize(); sol::table module = lua.create_table(); - module["VERSION"] = 2.11; + module["VERSION"] = 2.12; module.set_function("getState", &getState); module.set_function("registerHandler", ®isterHandler);