From 322e4983aed401ad6601b77e6425be2b78d32419 Mon Sep 17 00:00:00 2001 From: MysterD Date: Fri, 25 Mar 2022 23:06:14 -0700 Subject: [PATCH] Add HOOK_ON_PAUSE_EXIT --- autogen/lua_definitions/constants.lua | 5 ++++- docs/lua/constants.md | 3 ++- docs/lua/hooks.md | 1 + src/game/level_update.c | 4 ++++ src/pc/lua/smlua_constants_autogen.c | 3 ++- src/pc/lua/smlua_hooks.c | 20 ++++++++++++++++++++ src/pc/lua/smlua_hooks.h | 3 +++ 7 files changed, 36 insertions(+), 3 deletions(-) diff --git a/autogen/lua_definitions/constants.lua b/autogen/lua_definitions/constants.lua index 7e13c4f06..74f5bf487 100644 --- a/autogen/lua_definitions/constants.lua +++ b/autogen/lua_definitions/constants.lua @@ -5419,7 +5419,10 @@ HOOK_ON_OBJECT_UNLOAD = 14 HOOK_ON_SYNC_OBJECT_UNLOAD = 15 --- @type LuaHookedEventType -HOOK_MAX = 16 +HOOK_ON_PAUSE_EXIT = 16 + +--- @type LuaHookedEventType +HOOK_MAX = 17 --- @class ModelExtendedId diff --git a/docs/lua/constants.md b/docs/lua/constants.md index 3287371bf..9fdba5f90 100644 --- a/docs/lua/constants.md +++ b/docs/lua/constants.md @@ -1920,7 +1920,8 @@ | HOOK_ON_SYNC_VALID | 13 | | HOOK_ON_OBJECT_UNLOAD | 14 | | HOOK_ON_SYNC_OBJECT_UNLOAD | 15 | -| HOOK_MAX | 16 | +| HOOK_ON_PAUSE_EXIT | 16 | +| HOOK_MAX | 17 | [:arrow_up_small:](#) diff --git a/docs/lua/hooks.md b/docs/lua/hooks.md index cac58087f..3a49cf1fa 100644 --- a/docs/lua/hooks.md +++ b/docs/lua/hooks.md @@ -102,6 +102,7 @@ The lua functions sent to `hook_event()` will be automatically called by SM64 wh | HOOK_ON_SYNC_VALID | Called when the current area is synchronized | None | | HOOK_ON_OBJECT_UNLOAD | Called when any object is unloaded | [Object](structs.md#Object) unloadedObject | | HOOK_ON_SYNC_OBJECT_UNLOAD | Called when any networked object is unloaded | [Object](structs.md#Object) unloadedObject | +| HOOK_ON_PAUSE_EXIT | Called when the local player exits through the pause screen | `boolean` usedExitToCastle | ### Parameters diff --git a/src/game/level_update.c b/src/game/level_update.c index 186757b33..6f4f127af 100644 --- a/src/game/level_update.c +++ b/src/game/level_update.c @@ -1152,6 +1152,8 @@ s32 play_mode_normal(void) { return 0; } +#include // DO NOT COMMIT + s32 play_mode_paused(void) { if (gPauseScreenMode == 0) { set_menu_mode(RENDER_PAUSE_SCREEN); @@ -1162,6 +1164,7 @@ s32 play_mode_paused(void) { } else if (gPauseScreenMode == 2) { level_trigger_warp(&gMarioStates[0], WARP_OP_EXIT); set_play_mode(PLAY_MODE_NORMAL); + smlua_call_event_hooks_bool_param(HOOK_ON_PAUSE_EXIT, false); } else if (gPauseScreenMode == 3) { // Exit level if (gDebugLevelSelect) { @@ -1172,6 +1175,7 @@ s32 play_mode_paused(void) { gSavedCourseNum = COURSE_NONE; } set_play_mode(PLAY_MODE_CHANGE_LEVEL); + smlua_call_event_hooks_bool_param(HOOK_ON_PAUSE_EXIT, true); } /* else if (gPauseScreenMode == 4) { // We should only be getting "int 4" to here initiate_warp(LEVEL_CASTLE, 1, 0x1F, 0); diff --git a/src/pc/lua/smlua_constants_autogen.c b/src/pc/lua/smlua_constants_autogen.c index 570dbd4c3..a76e9b5c6 100644 --- a/src/pc/lua/smlua_constants_autogen.c +++ b/src/pc/lua/smlua_constants_autogen.c @@ -1948,7 +1948,8 @@ char gSmluaConstants[] = "" "HOOK_ON_SYNC_VALID = 13\n" "HOOK_ON_OBJECT_UNLOAD = 14\n" "HOOK_ON_SYNC_OBJECT_UNLOAD = 15\n" -"HOOK_MAX = 16\n" +"HOOK_ON_PAUSE_EXIT = 16\n" +"HOOK_MAX = 17\n" "E_MODEL_NONE = 0\n" "E_MODEL_MARIO = 1\n" "E_MODEL_SMOKE = 2\n" diff --git a/src/pc/lua/smlua_hooks.c b/src/pc/lua/smlua_hooks.c index 481ad519b..37a9fd382 100644 --- a/src/pc/lua/smlua_hooks.c +++ b/src/pc/lua/smlua_hooks.c @@ -75,6 +75,26 @@ void smlua_call_event_hooks(enum LuaHookedEventType hookType) { } } +void smlua_call_event_hooks_bool_param(enum LuaHookedEventType hookType, bool value) { + lua_State* L = gLuaState; + if (L == NULL) { return; } + struct LuaHookedEvent* hook = &sHookedEvents[hookType]; + for (int i = 0; i < hook->count; i++) { + // push the callback onto the stack + lua_rawgeti(L, LUA_REGISTRYINDEX, hook->reference[i]); + + // push value + lua_pushboolean(L, value); + + // call the callback + if (0 != smlua_call_hook(L, 1, 0, 0, hook->mod[i])) { + LOG_LUA("Failed to call the callback: %u, %s", hookType, lua_tostring(L, -1)); + smlua_logline(); + continue; + } + } +} + void smlua_call_event_hooks_mario_param(enum LuaHookedEventType hookType, struct MarioState* m) { lua_State* L = gLuaState; if (L == NULL) { return; } diff --git a/src/pc/lua/smlua_hooks.h b/src/pc/lua/smlua_hooks.h index 6dfa7d1dd..b3391b046 100644 --- a/src/pc/lua/smlua_hooks.h +++ b/src/pc/lua/smlua_hooks.h @@ -21,6 +21,7 @@ enum LuaHookedEventType { HOOK_ON_SYNC_VALID, HOOK_ON_OBJECT_UNLOAD, HOOK_ON_SYNC_OBJECT_UNLOAD, + HOOK_ON_PAUSE_EXIT, HOOK_MAX, }; @@ -41,12 +42,14 @@ static char* LuaHookedEventTypeName[] = { "HOOK_ON_SYNC_VALID", "HOOK_ON_OBJECT_UNLOAD", "HOOK_ON_SYNC_OBJECT_UNLOAD", + "HOOK_ON_PAUSE_EXIT", "HOOK_MAX" }; extern u32 gLuaMarioActionIndex; void smlua_call_event_hooks(enum LuaHookedEventType hookType); +void smlua_call_event_hooks_bool_param(enum LuaHookedEventType hookType, bool value); void smlua_call_event_hooks_mario_param(enum LuaHookedEventType hookType, struct MarioState* m); void smlua_call_event_hooks_mario_params(enum LuaHookedEventType hookType, struct MarioState* m1, struct MarioState* m2); void smlua_call_event_hooks_mario_params_ret_bool(enum LuaHookedEventType hookType, struct MarioState* m1, struct MarioState* m2, bool* returnValue);