fix HOOK_ON_PACKET_RECEIVE

This commit is contained in:
Isaac0-dev 2025-06-19 17:14:15 +10:00
parent 9f5ec527d8
commit 6bb8bd95ee
2 changed files with 33 additions and 7 deletions

View file

@ -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

View file

@ -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;
}