From 6bb8bd95ee55bb71a21ebd07542cd92e64eb85a7 Mon Sep 17 00:00:00 2001 From: Isaac0-dev <62234577+Isaac0-dev@users.noreply.github.com> Date: Thu, 19 Jun 2025 17:14:15 +1000 Subject: [PATCH] fix HOOK_ON_PACKET_RECEIVE --- autogen/gen_hooks.py | 32 ++++++++++++++++++++++-- src/pc/lua/smlua_hook_events_autogen.inl | 8 +++--- 2 files changed, 33 insertions(+), 7 deletions(-) diff --git a/autogen/gen_hooks.py b/autogen/gen_hooks.py index 3ad78c07c..98aea889b 100644 --- a/autogen/gen_hooks.py +++ b/autogen/gen_hooks.py @@ -14,7 +14,7 @@ bool smlua_call_event_hooks_{hook_type}({parameters}) {{ if (L == NULL) {{ return false; }}{define_hook_result} struct LuaHookedEvent *hook = &sHookedEvents[{hook_type}]; - for (int i = 0; i < hook->count; i++) {{ + for (int i = 0; i < hook->count; i++) {{{check_mod_index} s32 prevTop = lua_gettop(L); // push the callback onto the stack @@ -49,6 +49,9 @@ SMLUA_CALL_EVENT_HOOKS_END = """ }} """ +SMLUA_CALL_EVENT_HOOKS_MOD_INDEX_CHECK = """ + if (hook->mod[i]->index != modIndex) { continue; }""" + SMLUA_INTEGER_TYPES = { "input": """ // push {name} @@ -164,6 +167,19 @@ SMLUA_TYPES = { }, } +SMLUA_NAMES = { + "valueIndex": { + "input": """ + // push {name} + lua_pushvalue(L, {name}); +""", + "output": "" + }, + "modIndex": { + "input": "", + "output": "" + }, +} def init(): @@ -270,19 +286,31 @@ def main(): return_on_output_set = SMLUA_CALL_EVENT_HOOKS_RETURN_ON_OUTPUT_SET if hook_return == HOOK_RETURN_ON_OUTPUT_SET else "" hook_result = "hookResult" if hook_return == HOOK_RETURN_NEVER else "false" + mod_index_found = False + for input in hook_event["inputs"]: + if input["name"] == "modIndex": + mod_index_found = True + break + generated += SMLUA_CALL_EVENT_HOOKS_BEGIN.format( hook_type=hook_event["type"], parameters=hook_event["parameters"], + check_mod_index=SMLUA_CALL_EVENT_HOOKS_MOD_INDEX_CHECK if mod_index_found else "", define_hook_result=define_hook_result ) for input in hook_event["inputs"]: + if input["name"] in SMLUA_NAMES: + generated += SMLUA_NAMES[input["name"]]["input"].format( + name=input["name"] + ) + continue generated += SMLUA_TYPES[input["type"]]["input"].format( name=input["name"] ) generated += SMLUA_CALL_EVENT_HOOKS_CALLBACK.format( - n_inputs=len(hook_event["inputs"]), + n_inputs=len(hook_event["inputs"]) - mod_index_found, n_outputs=len(hook_event["outputs"]), hook_type=hook_event["type"], set_hook_result=set_hook_result diff --git a/src/pc/lua/smlua_hook_events_autogen.inl b/src/pc/lua/smlua_hook_events_autogen.inl index e8f39042a..2fdeae37b 100644 --- a/src/pc/lua/smlua_hook_events_autogen.inl +++ b/src/pc/lua/smlua_hook_events_autogen.inl @@ -703,19 +703,17 @@ bool smlua_call_event_hooks_HOOK_ON_PACKET_RECEIVE(s32 modIndex, s32 valueIndex) struct LuaHookedEvent *hook = &sHookedEvents[HOOK_ON_PACKET_RECEIVE]; for (int i = 0; i < hook->count; i++) { + if (hook->mod[i]->index != modIndex) { continue; } s32 prevTop = lua_gettop(L); // push the callback onto the stack lua_rawgeti(L, LUA_REGISTRYINDEX, hook->reference[i]); - // push modIndex - lua_pushinteger(L, modIndex); - // push valueIndex - lua_pushinteger(L, valueIndex); + lua_pushvalue(L, valueIndex); // call the callback - if (0 != smlua_call_hook(L, 2, 0, 0, hook->mod[i], hook->modFile[i])) { + if (0 != smlua_call_hook(L, 1, 0, 0, hook->mod[i], hook->modFile[i])) { LOG_LUA("Failed to call the callback for hook %s", sLuaHookedEventTypeName[HOOK_ON_PACKET_RECEIVE]); continue; }