Skip to content

Commit

Permalink
Use main luastate when registering events handlers
Browse files Browse the repository at this point in the history
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
  • Loading branch information
THE-FYP authored and Northn committed Nov 11, 2022
1 parent 80b28dd commit d5e8109
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 3 deletions.
4 changes: 3 additions & 1 deletion src/RakLua.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
2 changes: 1 addition & 1 deletion src/RakLua.h
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand Down
2 changes: 1 addition & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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", &registerHandler);
Expand Down

0 comments on commit d5e8109

Please sign in to comment.