Skip to content

Commit

Permalink
Fix play as kafei by swapping out resources later
Browse files Browse the repository at this point in the history
  • Loading branch information
garrettjoecox committed May 31, 2024
1 parent 73c468d commit cca1721
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 17 deletions.
6 changes: 2 additions & 4 deletions mm/2s2h/BenGui/BenMenuBar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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")) {
Expand Down
4 changes: 4 additions & 0 deletions mm/2s2h/Enhancements/GameInteractor/GameInteractor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ void GameInteractor_ExecuteOnRoomInit(s16 sceneId, s8 roomNum) {
GameInteractor::Instance->ExecuteHooksForFilter<GameInteractor::OnRoomInit>(sceneId, roomNum);
}

void GameInteractor_ExecuteOnPlayDestroy() {
GameInteractor::Instance->ExecuteHooks<GameInteractor::OnPlayDestroy>();
}

bool GameInteractor_ShouldActorInit(Actor* actor) {
bool result = true;
GameInteractor::Instance->ExecuteHooks<GameInteractor::ShouldActorInit>(actor, &result);
Expand Down
2 changes: 2 additions & 0 deletions mm/2s2h/Enhancements/GameInteractor/GameInteractor.h
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -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);
Expand Down
20 changes: 7 additions & 13 deletions mm/2s2h/Enhancements/Graphics/PlayAsKafei.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -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");

Expand Down Expand Up @@ -78,9 +74,7 @@ void RegisterPlayAsKafei() {
memcpy(&gLinkHumanSkelBackup, gLinkHumanSkelPtr, sizeof(SkeletonHeader));
memcpy(&gKafeiSkelBackup, gKafeiSkelPtr, sizeof(SkeletonHeader));

UpdatePlayAsKafeiSkeletons();
UpdatePlayAsKafeiOther();
UpdatePlayAsKafei();

GameInteractor::Instance->RegisterGameHook<GameInteractor::OnSceneInit>(
[](s8 sceneId, s8 spawnNum) { UpdatePlayAsKafeiOther(); });
GameInteractor::Instance->RegisterGameHook<GameInteractor::OnPlayDestroy>([]() { UpdatePlayAsKafei(); });
}
3 changes: 3 additions & 0 deletions mm/src/code/z_play.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit cca1721

Please # to comment.