From 4af8af677fa21d4261ae398d652ff7e47420a0a7 Mon Sep 17 00:00:00 2001 From: Agent X <44549182+Agent-11@users.noreply.github.com> Date: Tue, 28 Feb 2023 22:39:16 -0500 Subject: [PATCH 1/2] Touch up new hooks --- src/game/characters.c | 2 +- src/game/mario.c | 2 +- src/pc/lua/smlua_hooks.c | 16 +++++++++++----- src/pc/lua/smlua_hooks.h | 4 ++-- 4 files changed, 15 insertions(+), 9 deletions(-) diff --git a/src/game/characters.c b/src/game/characters.c index 841a3170e..b6a53cace 100644 --- a/src/game/characters.c +++ b/src/game/characters.c @@ -458,7 +458,7 @@ static s32 get_character_sound(struct MarioState* m, enum CharacterSound charact if (m == NULL || m->marioObj == NULL) { return 0; } s32 override = 0; - if (smlua_call_event_hooks_mario_charactersound_param_ret_int(HOOK_CHARACTER_SOUND, m, characterSound, &override)) { + if (smlua_call_event_hooks_mario_character_sound_param_ret_int(HOOK_CHARACTER_SOUND, m, characterSound, &override)) { return override; } diff --git a/src/game/mario.c b/src/game/mario.c index 33915df75..fce737d54 100644 --- a/src/game/mario.c +++ b/src/game/mario.c @@ -1094,7 +1094,7 @@ static u32 set_mario_action_cutscene(struct MarioState *m, u32 action, UNUSED u3 */ u32 set_mario_action(struct MarioState *m, u32 action, u32 actionArg) { u32 returnValue = 0; - smlua_call_event_hooks_int_param_ret_int(HOOK_BEFORE_SET_MARIO_ACTION, action, &returnValue); + smlua_call_event_hooks_mario_action_params_ret_int(HOOK_BEFORE_SET_MARIO_ACTION, m, action, &returnValue); if (returnValue == 1) { return TRUE; } else if (returnValue) { action = returnValue; } switch (action & ACT_GROUP_MASK) { diff --git a/src/pc/lua/smlua_hooks.c b/src/pc/lua/smlua_hooks.c index 62dc571f2..a880cf627 100644 --- a/src/pc/lua/smlua_hooks.c +++ b/src/pc/lua/smlua_hooks.c @@ -639,7 +639,7 @@ void smlua_call_event_hooks_on_chat_message(enum LuaHookedEventType hookType, st } } -bool smlua_call_event_hooks_mario_charactersound_param_ret_int(enum LuaHookedEventType hookType, struct MarioState* m, enum CharacterSound characterSound, s32* returnValue) { +bool smlua_call_event_hooks_mario_character_sound_param_ret_int(enum LuaHookedEventType hookType, struct MarioState* m, enum CharacterSound characterSound, s32* returnValue) { lua_State* L = gLuaState; if (L == NULL) { return false; } struct LuaHookedEvent* hook = &sHookedEvents[hookType]; @@ -676,7 +676,7 @@ bool smlua_call_event_hooks_mario_charactersound_param_ret_int(enum LuaHookedEve return false; } -void smlua_call_event_hooks_int_param_ret_int(enum LuaHookedEventType hookType, u32 param, u32* returnValue) { +void smlua_call_event_hooks_mario_action_params_ret_int(enum LuaHookedEventType hookType, struct MarioState *m, u32 action, u32* returnValue) { lua_State* L = gLuaState; if (L == NULL) { return; } struct LuaHookedEvent* hook = &sHookedEvents[hookType]; @@ -686,11 +686,17 @@ void smlua_call_event_hooks_int_param_ret_int(enum LuaHookedEventType hookType, // push the callback onto the stack lua_rawgeti(L, LUA_REGISTRYINDEX, hook->reference[i]); - // push params - lua_pushinteger(L, param); + // push mario state + lua_getglobal(L, "gMarioStates"); + lua_pushinteger(L, m->playerIndex); + lua_gettable(L, -2); + lua_remove(L, -2); + + // push action + lua_pushinteger(L, action); // call the callback - if (0 != smlua_call_hook(L, 1, 1, 0, hook->mod[i])) { + if (0 != smlua_call_hook(L, 2, 1, 0, hook->mod[i])) { LOG_LUA("Failed to call the callback: %u", hookType); continue; } diff --git a/src/pc/lua/smlua_hooks.h b/src/pc/lua/smlua_hooks.h index f522d8671..af3d42f41 100644 --- a/src/pc/lua/smlua_hooks.h +++ b/src/pc/lua/smlua_hooks.h @@ -114,8 +114,8 @@ void smlua_call_event_hooks_value_param(enum LuaHookedEventType hookType, int mo void smlua_call_event_hooks_use_act_select(enum LuaHookedEventType hookType, int value, bool* foundHook, bool* returnValue); void smlua_call_event_hooks_ret_bool(enum LuaHookedEventType hookType, bool* returnValue); void smlua_call_event_hooks_on_chat_message(enum LuaHookedEventType hookType, struct MarioState* m, const char* message, bool* returnValue); -bool smlua_call_event_hooks_mario_charactersound_param_ret_int(enum LuaHookedEventType hookType, struct MarioState* m, enum CharacterSound characterSound, s32* returnValue); -void smlua_call_event_hooks_int_param_ret_int(enum LuaHookedEventType hookType, u32 param, u32* returnValue); +bool smlua_call_event_hooks_mario_character_sound_param_ret_int(enum LuaHookedEventType hookType, struct MarioState* m, enum CharacterSound characterSound, s32* returnValue); +void smlua_call_event_hooks_mario_action_params_ret_int(enum LuaHookedEventType hookType, struct MarioState *m, u32 action, u32* returnValue); enum BehaviorId smlua_get_original_behavior_id(const BehaviorScript* behavior); const BehaviorScript* smlua_override_behavior(const BehaviorScript* behavior); From 5653b2bec462b5f5f420c8aa8754a3f725796c39 Mon Sep 17 00:00:00 2001 From: Agent X <44549182+Agent-11@users.noreply.github.com> Date: Tue, 28 Feb 2023 22:43:47 -0500 Subject: [PATCH 2/2] Add new param to documentation --- docs/lua/guides/hooks.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/lua/guides/hooks.md b/docs/lua/guides/hooks.md index 61a13695a..b70a1bd21 100644 --- a/docs/lua/guides/hooks.md +++ b/docs/lua/guides/hooks.md @@ -116,7 +116,7 @@ The lua functions sent to `hook_event()` will be automatically called by SM64 wh | HOOK_ON_CHAT_MESSAGE | Called when a chat message gets sent. Return `false` to prevent the message from being sent. | [MarioState](structs.md#MarioState) messageSender | | HOOK_OBJECT_SET_MODEL | Called when a behavior changes models. Also runs when a behavior spawns. | [Object](structs.md#Object) obj, `integer` modelID | | HOOK_CHARACTER_SOUND | Called when mario retrieves a character sound to play, return a character sound or `0` to override it. | [MarioState](structs.md#MarioState) mario, [enum CharacterSound](constants.md#enum-CharacterSound) characterSound | -| HOOK_BEFORE_SET_MARIO_ACTION | Called before Mario's action changes. Return an action to change the incoming action or `1` to cancel the action change. | `integer` incomingAction | +| HOOK_BEFORE_SET_MARIO_ACTION | Called before Mario's action changes. Return an action to change the incoming action or `1` to cancel the action change. | [MarioState](structs.md#MarioState) mario, `integer` incomingAction | ### Parameters