diff --git a/autogen/lua_definitions/constants.lua b/autogen/lua_definitions/constants.lua index 3a437fd8..9b4ef878 100644 --- a/autogen/lua_definitions/constants.lua +++ b/autogen/lua_definitions/constants.lua @@ -9324,7 +9324,10 @@ HOOK_ON_OBJECT_LOAD = 40 HOOK_ON_PLAY_SOUND = 41 --- @type LuaHookedEventType -HOOK_MAX = 42 +HOOK_ON_SEQ_LOAD = 42 + +--- @type LuaHookedEventType +HOOK_MAX = 43 --- @class HudDisplayFlags diff --git a/docs/lua/constants.md b/docs/lua/constants.md index f483010c..e6b572d7 100644 --- a/docs/lua/constants.md +++ b/docs/lua/constants.md @@ -3332,7 +3332,8 @@ | HOOK_OVERRIDE_PHYS_STEP_DEFACTO_SPEED | 39 | | HOOK_ON_OBJECT_LOAD | 40 | | HOOK_ON_PLAY_SOUND | 41 | -| HOOK_MAX | 42 | +| HOOK_ON_SEQ_LOAD | 42 | +| HOOK_MAX | 43 | [:arrow_up_small:](#) diff --git a/src/audio/load.c b/src/audio/load.c index a78dd616..29f6f032 100644 --- a/src/audio/load.c +++ b/src/audio/load.c @@ -12,6 +12,7 @@ #include "pc/platform.h" #include "pc/fs/fs.h" #include "pc/lua/utils/smlua_audio_utils.h" +#include "pc/lua/smlua_hooks.h" #define ALIGN16(val) (((val) + 0xF) & ~0xF) @@ -1552,6 +1553,13 @@ void preload_sequence(u32 seqId, u8 preloadMask) { void load_sequence_internal(u32 player, u32 seqId, s32 loadAsync); void load_sequence(u32 player, u32 seqId, s32 loadAsync) { + u8 returnValue = 0; + + smlua_call_event_hooks_on_seq_load(HOOK_ON_SEQ_LOAD, player, seqId, loadAsync, &returnValue); + if (returnValue != 0) { + seqId = returnValue; + } + if (!loadAsync) { gAudioLoadLock = AUDIO_LOCK_LOADING; } diff --git a/src/pc/lua/smlua_constants_autogen.c b/src/pc/lua/smlua_constants_autogen.c index 2fd0b6c2..6272cca9 100644 --- a/src/pc/lua/smlua_constants_autogen.c +++ b/src/pc/lua/smlua_constants_autogen.c @@ -3307,7 +3307,8 @@ char gSmluaConstants[] = "" "HOOK_OVERRIDE_PHYS_STEP_DEFACTO_SPEED = 39\n" "HOOK_ON_OBJECT_LOAD = 40\n" "HOOK_ON_PLAY_SOUND = 41\n" -"HOOK_MAX = 42\n" +"HOOK_ON_SEQ_LOAD = 42\n" +"HOOK_MAX = 43\n" "ACTION_HOOK_EVERY_FRAME = 0\n" "ACTION_HOOK_GRAVITY = 1\n" "ACTION_HOOK_MAX = 2\n" diff --git a/src/pc/lua/smlua_hooks.c b/src/pc/lua/smlua_hooks.c index c6c16bda..574c18e6 100644 --- a/src/pc/lua/smlua_hooks.c +++ b/src/pc/lua/smlua_hooks.c @@ -727,6 +727,35 @@ void smlua_call_event_hooks_on_play_sound(enum LuaHookedEventType hookType, s32 } } +void smlua_call_event_hooks_on_seq_load(enum LuaHookedEventType hookType, u32 player, u32 seqId, s32 loadAsync, u8* returnValue) { + lua_State* L = gLuaState; + if (L == NULL) { return; } + struct LuaHookedEvent* hook = &sHookedEvents[hookType]; + for (int i = 0; i < hook->count; i++) { + s32 prevTop = lua_gettop(L); + + lua_rawgeti(L, LUA_REGISTRYINDEX, hook->reference[i]); + lua_pushinteger(L, player); + lua_pushinteger(L, seqId); + lua_pushinteger(L, loadAsync); + + // Call the callback + if (0 != smlua_call_hook(L, 3, 1, 0, hook->mod[i])) { + LOG_LUA("Failed to call the callback: %u", hookType); + continue; + } + + // Output the return value + if (lua_type(L, -1) == LUA_TNUMBER) { + *returnValue = smlua_to_integer(L, -1); + lua_settop(L, prevTop); + return; + } else { + lua_settop(L, prevTop); + } + } +} + void smlua_call_event_hooks_use_act_select(enum LuaHookedEventType hookType, int value, bool* foundHook, bool* returnValue) { lua_State* L = gLuaState; *foundHook = false; diff --git a/src/pc/lua/smlua_hooks.h b/src/pc/lua/smlua_hooks.h index a0e2d2b9..56b5479b 100644 --- a/src/pc/lua/smlua_hooks.h +++ b/src/pc/lua/smlua_hooks.h @@ -53,6 +53,7 @@ enum LuaHookedEventType { HOOK_OVERRIDE_PHYS_STEP_DEFACTO_SPEED, HOOK_ON_OBJECT_LOAD, HOOK_ON_PLAY_SOUND, + HOOK_ON_SEQ_LOAD, HOOK_MAX, }; @@ -99,6 +100,7 @@ static const char* LuaHookedEventTypeName[] = { "HOOK_OVERRIDE_PHYS_STEP_DEFACTO_SPEED", "HOOK_ON_OBJECT_LOAD", "HOOK_ON_PLAY_SOUND", + "HOOK_ON_SEQ_LOAD", "HOOK_MAX" }; @@ -147,6 +149,7 @@ bool smlua_call_event_hooks_mario_param_and_int_ret_int(enum LuaHookedEventType bool smlua_call_event_hooks_mario_param_ret_float(enum LuaHookedEventType hookType, struct MarioState* m, f32* returnValue); bool smlua_call_event_hooks_mario_param_and_int_and_int_ret_int(enum LuaHookedEventType hookType, struct MarioState* m, s32 param, u32 args, s32* returnValue); void smlua_call_event_hooks_graph_node_object_and_int_param(enum LuaHookedEventType hookType, struct GraphNodeObject* node, s32 param); +void smlua_call_event_hooks_on_seq_load(enum LuaHookedEventType hookType, u32 player, u32 seqId, s32 loadAsync, u8* returnValue); enum BehaviorId smlua_get_original_behavior_id(const BehaviorScript* behavior); const BehaviorScript* smlua_override_behavior(const BehaviorScript* behavior);