[Game Interactor] Add LoadGame and ExitGame hooks (#2542)

* add loadgame/exitgame GI hooks

* implement loadgame/exitgame hooks in game code

* move entrance tracker data lifecycle to hooks

* update cosmetic editor to update onloadgame hook
This commit is contained in:
Adam Bird 2023-02-26 23:04:57 -05:00 committed by GitHub
parent 17aeec4b13
commit f7bb807940
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 47 additions and 21 deletions

View File

@ -1791,8 +1791,8 @@ void DrawCosmeticsEditor(bool& open) {
ImGui::End(); ImGui::End();
} }
void RegisterOnLoadFileHook() { void RegisterOnLoadGameHook() {
GameInteractor::Instance->RegisterGameHook<GameInteractor::OnLoadFile>([](int32_t fileNum) { GameInteractor::Instance->RegisterGameHook<GameInteractor::OnLoadGame>([](int32_t fileNum) {
ApplyOrResetCustomGfxPatches(); ApplyOrResetCustomGfxPatches();
}); });
} }
@ -1817,7 +1817,7 @@ void InitCosmeticsEditor() {
SohImGui::RequestCvarSaveOnNextTick(); SohImGui::RequestCvarSaveOnNextTick();
ApplyOrResetCustomGfxPatches(); ApplyOrResetCustomGfxPatches();
RegisterOnLoadFileHook(); RegisterOnLoadGameHook();
} }
void CosmeticsEditor_RandomizeAll() { void CosmeticsEditor_RandomizeAll() {

View File

@ -206,6 +206,7 @@ static bool ResetHandler(std::shared_ptr<Ship::Console> Console, std::vector<std
SET_NEXT_GAMESTATE(&gPlayState->state, TitleSetup_Init, GameState); SET_NEXT_GAMESTATE(&gPlayState->state, TitleSetup_Init, GameState);
gPlayState->state.running = false; gPlayState->state.running = false;
GameInteractor::Instance->ExecuteHooks<GameInteractor::OnExitGame>(gSaveContext.fileNum);
return CMD_SUCCESS; return CMD_SUCCESS;
} }

View File

@ -84,13 +84,15 @@ public:
} }
} }
DEFINE_HOOK(OnLoadGame, void(int32_t fileNum));
DEFINE_HOOK(OnExitGame, void(int32_t fileNum));
DEFINE_HOOK(OnReceiveItem, void(u8 item)); DEFINE_HOOK(OnReceiveItem, void(u8 item));
DEFINE_HOOK(OnSceneInit, void(s16 sceneNum)); DEFINE_HOOK(OnSceneInit, void(s16 sceneNum));
DEFINE_HOOK(OnSaveFile, void(int fileNum)); DEFINE_HOOK(OnSaveFile, void(int32_t fileNum));
DEFINE_HOOK(OnLoadFile, void(int fileNum)); DEFINE_HOOK(OnLoadFile, void(int32_t fileNum));
DEFINE_HOOK(OnDeleteFile, void(int fileNum)); DEFINE_HOOK(OnDeleteFile, void(int32_t fileNum));
// Helpers // Helpers
static bool IsSaveLoaded(); static bool IsSaveLoaded();

View File

@ -6,6 +6,14 @@ extern PlayState* gPlayState;
// MARK: - Gameplay // MARK: - Gameplay
void GameInteractor_ExecuteOnLoadGame(int32_t fileNum) {
GameInteractor::Instance->ExecuteHooks<GameInteractor::OnLoadGame>(fileNum);
}
void GameInteractor_ExecuteOnExitGame(int32_t fileNum) {
GameInteractor::Instance->ExecuteHooks<GameInteractor::OnExitGame>(fileNum);
}
void GameInteractor_ExecuteOnReceiveItemHooks(u8 item) { void GameInteractor_ExecuteOnReceiveItemHooks(u8 item) {
GameInteractor::Instance->ExecuteHooks<GameInteractor::OnReceiveItem>(item); GameInteractor::Instance->ExecuteHooks<GameInteractor::OnReceiveItem>(item);
} }
@ -16,14 +24,14 @@ void GameInteractor_ExecuteOnSceneInitHooks(s16 sceneNum) {
// MARK: - Save Files // MARK: - Save Files
void GameInteractor_ExecuteOnSaveFile(int fileNum) { void GameInteractor_ExecuteOnSaveFile(int32_t fileNum) {
GameInteractor::Instance->ExecuteHooks<GameInteractor::OnSaveFile>(fileNum); GameInteractor::Instance->ExecuteHooks<GameInteractor::OnSaveFile>(fileNum);
} }
void GameInteractor_ExecuteOnLoadFile(int fileNum) { void GameInteractor_ExecuteOnLoadFile(int32_t fileNum) {
GameInteractor::Instance->ExecuteHooks<GameInteractor::OnLoadFile>(fileNum); GameInteractor::Instance->ExecuteHooks<GameInteractor::OnLoadFile>(fileNum);
} }
void GameInteractor_ExecuteOnDeleteFile(int fileNum) { void GameInteractor_ExecuteOnDeleteFile(int32_t fileNum) {
GameInteractor::Instance->ExecuteHooks<GameInteractor::OnDeleteFile>(fileNum); GameInteractor::Instance->ExecuteHooks<GameInteractor::OnDeleteFile>(fileNum);
} }

View File

@ -1,10 +1,12 @@
#include "GameInteractor.h" #include "GameInteractor.h"
// MARK: - Gameplay // MARK: - Gameplay
extern "C" void GameInteractor_ExecuteOnLoadGame(int32_t fileNum);
extern "C" void GameInteractor_ExecuteOnExitGame(int32_t fileNum);
extern "C" void GameInteractor_ExecuteOnReceiveItemHooks(u8 item); extern "C" void GameInteractor_ExecuteOnReceiveItemHooks(u8 item);
extern "C" void GameInteractor_ExecuteOnSceneInit(s16 sceneNum); extern "C" void GameInteractor_ExecuteOnSceneInit(s16 sceneNum);
// MARK: - Save Files // MARK: - Save Files
extern "C" void GameInteractor_ExecuteOnSaveFile(int fileNum); extern "C" void GameInteractor_ExecuteOnSaveFile(int32_t fileNum);
extern "C" void GameInteractor_ExecuteOnLoadFile(int fileNum); extern "C" void GameInteractor_ExecuteOnLoadFile(int32_t fileNum);
extern "C" void GameInteractor_ExecuteOnDeleteFile(int fileNum); extern "C" void GameInteractor_ExecuteOnDeleteFile(int32_t fileNum);

View File

@ -1,9 +1,7 @@
#include "randomizer_entrance_tracker.h" #include "randomizer_entrance_tracker.h"
#include "../../util.h" #include "soh/OTRGlobals.h"
#include "../../OTRGlobals.h"
#include <ImGuiImpl.h> #include <ImGuiImpl.h>
#include "../../UIWidgets.hpp" #include "soh/UIWidgets.hpp"
#include <map> #include <map>
#include <string> #include <string>
@ -18,10 +16,12 @@ extern "C" {
#include "macros.h" #include "macros.h"
extern PlayState* gPlayState; extern PlayState* gPlayState;
#include "randomizer_entrance.h" #include "soh/Enhancements/randomizer/randomizer_entrance.h"
#include "randomizer_grotto.h" #include "soh/Enhancements/randomizer/randomizer_grotto.h"
} }
#include "soh/Enhancements/game-interactor/GameInteractor.h"
#define COLOR_ORANGE IM_COL32(230, 159, 0, 255) #define COLOR_ORANGE IM_COL32(230, 159, 0, 255)
#define COLOR_GREEN IM_COL32(0, 158, 115, 255) #define COLOR_GREEN IM_COL32(0, 158, 115, 255)
#define COLOR_GRAY IM_COL32(155, 155, 155, 255) #define COLOR_GRAY IM_COL32(155, 155, 155, 255)
@ -920,4 +920,12 @@ void DrawEntranceTracker(bool& open) {
void InitEntranceTracker() { void InitEntranceTracker() {
SohImGui::AddWindow("Randomizer", "Entrance Tracker", DrawEntranceTracker, CVarGetInteger("gEntranceTrackerEnabled", 0) == 1); SohImGui::AddWindow("Randomizer", "Entrance Tracker", DrawEntranceTracker, CVarGetInteger("gEntranceTrackerEnabled", 0) == 1);
// Setup hooks for loading and clearing the entrance tracker data
GameInteractor::Instance->RegisterGameHook<GameInteractor::OnLoadGame>([](int32_t fileNum) {
InitEntranceTrackingData();
});
GameInteractor::Instance->RegisterGameHook<GameInteractor::OnExitGame>([](int32_t fileNum) {
ClearEntranceTrackingData();
});
} }

View File

@ -7,6 +7,7 @@
#include <ImGuiImpl.h> #include <ImGuiImpl.h>
#include "soh/frame_interpolation.h" #include "soh/frame_interpolation.h"
#include "soh/Enhancements/debugconsole.h" #include "soh/Enhancements/debugconsole.h"
#include "soh/Enhancements/game-interactor/GameInteractor.h"
#include "soh/Enhancements/randomizer/randomizer_entrance.h" #include "soh/Enhancements/randomizer/randomizer_entrance.h"
#include <overlays/actors/ovl_En_Niw/z_en_niw.h> #include <overlays/actors/ovl_En_Niw/z_en_niw.h>
@ -427,10 +428,12 @@ void Play_Init(GameState* thisx) {
} }
} }
// Invalid entrance, so immediately exit the game to opening title
if (gSaveContext.entranceIndex == -1) { if (gSaveContext.entranceIndex == -1) {
gSaveContext.entranceIndex = 0; gSaveContext.entranceIndex = 0;
play->state.running = false; play->state.running = false;
SET_NEXT_GAMESTATE(&play->state, Opening_Init, OpeningContext); SET_NEXT_GAMESTATE(&play->state, Opening_Init, OpeningContext);
GameInteractor_ExecuteOnExitGame(gSaveContext.fileNum);
return; return;
} }

View File

@ -611,6 +611,4 @@ void Sram_InitSram(GameState* gameState) {
// When going from a rando save to a vanilla save within the same game instance // When going from a rando save to a vanilla save within the same game instance
// we need to reset the entrance table back to its vanilla state // we need to reset the entrance table back to its vanilla state
Entrance_ResetEntranceTable(); Entrance_ResetEntranceTable();
// Clear out the entrance tracker
Entrance_ClearEntranceTrackingData();
} }

View File

@ -9,6 +9,7 @@
#include <GameVersions.h> #include <GameVersions.h>
#include "objects/object_mag/object_mag.h" #include "objects/object_mag/object_mag.h"
#include "objects/gameplay_keep/gameplay_keep.h" #include "objects/gameplay_keep/gameplay_keep.h"
#include "soh/Enhancements/game-interactor/GameInteractor.h"
#define NORMAL_QUEST 0 #define NORMAL_QUEST 0
#define MASTER_QUEST 1 #define MASTER_QUEST 1
@ -2148,7 +2149,6 @@ void FileChoose_LoadGame(GameState* thisx) {
if (gSaveContext.n64ddFlag) { if (gSaveContext.n64ddFlag) {
// Setup the modified entrance table and entrance shuffle table for rando // Setup the modified entrance table and entrance shuffle table for rando
Entrance_Init(); Entrance_Init();
Entrance_InitEntranceTrackingData();
// Handle randomized spawn positions after the save context has been setup from load // Handle randomized spawn positions after the save context has been setup from load
// When remeber save location is on, set save warp if the save was in an a grotto, or // When remeber save location is on, set save warp if the save was in an a grotto, or
@ -2159,6 +2159,8 @@ void FileChoose_LoadGame(GameState* thisx) {
Entrance_SetSavewarpEntrance(); Entrance_SetSavewarpEntrance();
} }
} }
GameInteractor_ExecuteOnLoadGame(gSaveContext.fileNum);
} }
static void (*gSelectModeUpdateFuncs[])(GameState*) = { static void (*gSelectModeUpdateFuncs[])(GameState*) = {

View File

@ -14,6 +14,7 @@
#include "vt.h" #include "vt.h"
#include "soh/frame_interpolation.h" #include "soh/frame_interpolation.h"
#include "soh/Enhancements/game-interactor/GameInteractor.h"
#include "soh/Enhancements/randomizer/randomizer_entrance.h" #include "soh/Enhancements/randomizer/randomizer_entrance.h"
static void* sEquipmentFRATexs[] = { static void* sEquipmentFRATexs[] = {
@ -4180,6 +4181,7 @@ void KaleidoScope_Update(PlayState* play)
} else { } else {
play->state.running = 0; play->state.running = 0;
SET_NEXT_GAMESTATE(&play->state, Opening_Init, OpeningContext); SET_NEXT_GAMESTATE(&play->state, Opening_Init, OpeningContext);
GameInteractor_ExecuteOnExitGame(gSaveContext.fileNum);
} }
} }
} }