From 8ac7dc6b7d65a363b450d6799e9f135537a88303 Mon Sep 17 00:00:00 2001 From: Garrett Cox Date: Thu, 30 May 2024 22:10:45 -0500 Subject: [PATCH] Fix play as kafei by swapping out resources later (#617) --- mm/2s2h/BenGui/BenMenuBar.cpp | 6 ++---- .../GameInteractor/GameInteractor.cpp | 4 ++++ .../GameInteractor/GameInteractor.h | 2 ++ mm/2s2h/Enhancements/Graphics/PlayAsKafei.cpp | 20 +++++++------------ mm/src/code/z_play.c | 3 +++ 5 files changed, 18 insertions(+), 17 deletions(-) diff --git a/mm/2s2h/BenGui/BenMenuBar.cpp b/mm/2s2h/BenGui/BenMenuBar.cpp index 5cea0a443..d5a2efc96 100644 --- a/mm/2s2h/BenGui/BenMenuBar.cpp +++ b/mm/2s2h/BenGui/BenMenuBar.cpp @@ -531,10 +531,8 @@ void DrawEnhancementsMenu() { } if (UIWidgets::BeginMenu("Modes")) { - if (UIWidgets::CVarCheckbox("Play As Kafei", "gModes.PlayAsKafei", - { .tooltip = "Requires scene reload to take effect." })) { - UpdatePlayAsKafeiSkeletons(); - } + UIWidgets::CVarCheckbox("Play As Kafei", "gModes.PlayAsKafei", + { .tooltip = "Requires scene reload to take effect." }); ImGui::EndMenu(); } if (UIWidgets::BeginMenu("Player Movement")) { diff --git a/mm/2s2h/Enhancements/GameInteractor/GameInteractor.cpp b/mm/2s2h/Enhancements/GameInteractor/GameInteractor.cpp index 0eafed218..020169fc2 100644 --- a/mm/2s2h/Enhancements/GameInteractor/GameInteractor.cpp +++ b/mm/2s2h/Enhancements/GameInteractor/GameInteractor.cpp @@ -49,6 +49,10 @@ void GameInteractor_ExecuteOnRoomInit(s16 sceneId, s8 roomNum) { GameInteractor::Instance->ExecuteHooksForFilter(sceneId, roomNum); } +void GameInteractor_ExecuteOnPlayDestroy() { + GameInteractor::Instance->ExecuteHooks(); +} + bool GameInteractor_ShouldActorInit(Actor* actor) { bool result = true; GameInteractor::Instance->ExecuteHooks(actor, &result); diff --git a/mm/2s2h/Enhancements/GameInteractor/GameInteractor.h b/mm/2s2h/Enhancements/GameInteractor/GameInteractor.h index 8359f3003..27c61447f 100644 --- a/mm/2s2h/Enhancements/GameInteractor/GameInteractor.h +++ b/mm/2s2h/Enhancements/GameInteractor/GameInteractor.h @@ -255,6 +255,7 @@ class GameInteractor { DEFINE_HOOK(OnSceneInit, (s8 sceneId, s8 spawnNum)); DEFINE_HOOK(OnRoomInit, (s8 sceneId, s8 roomNum)); + DEFINE_HOOK(OnPlayDestroy, ()); DEFINE_HOOK(ShouldActorInit, (Actor * actor, bool* should)); DEFINE_HOOK(OnActorInit, (Actor * actor)); @@ -296,6 +297,7 @@ void GameInteractor_ExecuteBeforeMoonCrashSaveReset(); void GameInteractor_ExecuteOnSceneInit(s16 sceneId, s8 spawnNum); void GameInteractor_ExecuteOnRoomInit(s16 sceneId, s8 roomNum); +void GameInteractor_ExecuteOnPlayDestroy(); bool GameInteractor_ShouldActorInit(Actor* actor); void GameInteractor_ExecuteOnActorInit(Actor* actor); diff --git a/mm/2s2h/Enhancements/Graphics/PlayAsKafei.cpp b/mm/2s2h/Enhancements/Graphics/PlayAsKafei.cpp index 9b3e6b45e..9c69d6704 100644 --- a/mm/2s2h/Enhancements/Graphics/PlayAsKafei.cpp +++ b/mm/2s2h/Enhancements/Graphics/PlayAsKafei.cpp @@ -17,20 +17,12 @@ extern TexturePtr sPlayerMouthTextures[PLAYER_FORM_MAX][PLAYER_MOUTH_MAX]; static SkeletonHeader gLinkHumanSkelBackup; static SkeletonHeader gKafeiSkelBackup; -void UpdatePlayAsKafeiSkeletons() { +void UpdatePlayAsKafei() { if (CVarGetInteger("gModes.PlayAsKafei", 0)) { auto gLinkHumanSkelResource = Ship::Context::GetInstance()->GetResourceManager()->LoadResource(gLinkHumanSkel); SkeletonHeader* gLinkHumanSkelPtr = (SkeletonHeader*)gLinkHumanSkelResource->GetRawPointer(); memcpy(gLinkHumanSkelPtr, &gKafeiSkelBackup, sizeof(SkeletonHeader)); - } else { - auto gLinkHumanSkelResource = Ship::Context::GetInstance()->GetResourceManager()->LoadResource(gLinkHumanSkel); - SkeletonHeader* gLinkHumanSkelPtr = (SkeletonHeader*)gLinkHumanSkelResource->GetRawPointer(); - memcpy(gLinkHumanSkelPtr, &gLinkHumanSkelBackup, sizeof(SkeletonHeader)); - } -} -void UpdatePlayAsKafeiOther() { - if (CVarGetInteger("gModes.PlayAsKafei", 0)) { ResourceMgr_PatchGfxByName(gLinkHumanWaistDL, "gLinkHumanWaistDL0", 0, gsSPDisplayListOTRFilePath(gKafeiWaistDL)); ResourceMgr_PatchGfxByName(gLinkHumanWaistDL, "gLinkHumanWaistDL1", 1, gsSPEndDisplayList()); @@ -49,6 +41,10 @@ void UpdatePlayAsKafeiOther() { sPlayerMouthTextures[PLAYER_FORM_HUMAN][2] = (TexturePtr)gKafeiMouthAngryTex; sPlayerMouthTextures[PLAYER_FORM_HUMAN][3] = (TexturePtr)gKafeiMouthHappyTex; } else { + auto gLinkHumanSkelResource = Ship::Context::GetInstance()->GetResourceManager()->LoadResource(gLinkHumanSkel); + SkeletonHeader* gLinkHumanSkelPtr = (SkeletonHeader*)gLinkHumanSkelResource->GetRawPointer(); + memcpy(gLinkHumanSkelPtr, &gLinkHumanSkelBackup, sizeof(SkeletonHeader)); + ResourceMgr_UnpatchGfxByName(gLinkHumanWaistDL, "gLinkHumanWaistDL0"); ResourceMgr_UnpatchGfxByName(gLinkHumanWaistDL, "gLinkHumanWaistDL1"); @@ -78,9 +74,7 @@ void RegisterPlayAsKafei() { memcpy(&gLinkHumanSkelBackup, gLinkHumanSkelPtr, sizeof(SkeletonHeader)); memcpy(&gKafeiSkelBackup, gKafeiSkelPtr, sizeof(SkeletonHeader)); - UpdatePlayAsKafeiSkeletons(); - UpdatePlayAsKafeiOther(); + UpdatePlayAsKafei(); - GameInteractor::Instance->RegisterGameHook( - [](s8 sceneId, s8 spawnNum) { UpdatePlayAsKafeiOther(); }); + GameInteractor::Instance->RegisterGameHook([]() { UpdatePlayAsKafei(); }); } diff --git a/mm/src/code/z_play.c b/mm/src/code/z_play.c index 0d7dab16d..a939ed217 100644 --- a/mm/src/code/z_play.c +++ b/mm/src/code/z_play.c @@ -467,6 +467,9 @@ void Play_Destroy(GameState* thisx) { KaleidoScopeCall_Destroy(this); KaleidoManager_Destroy(); ZeldaArena_Cleanup(); + + GameInteractor_ExecuteOnPlayDestroy(); + // #region 2S2H [General] Making gPlayState available gPlayState = NULL; // #endregion