Hook unregister fixes and hook debugger tweaks (#5139)

* clang-format before

* Hook unregister fixes and hook debugger tweaks
This commit is contained in:
Archez 2025-03-19 06:14:43 -04:00 committed by GitHub
commit 9795f696c8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 217 additions and 112 deletions

View file

@ -4,28 +4,29 @@
#include <string> #include <string>
#include <version> #include <version>
static std::unordered_map<const char*, std::unordered_map<HOOK_ID, HookInfo>*> hookData; static std::map<const char*, std::map<HOOK_ID, HookInfo>*> hookData;
const ImVec4 grey = ImVec4(0.75, 0.75, 0.75, 1); const ImVec4 grey = ImVec4(0.75, 0.75, 0.75, 1);
const ImVec4 yellow = ImVec4(1, 1, 0, 1); const ImVec4 yellow = ImVec4(1, 1, 0, 1);
const ImVec4 red = ImVec4(1, 0, 0, 1); const ImVec4 red = ImVec4(1, 0, 0, 1);
void DrawHookRegisteringInfos(const char* hookName) { void DrawHookRegisteringInfos(const char* hookName) {
if ((*hookData[hookName]).size() == 0) { size_t numHooks = (*hookData[hookName]).size();
if (numHooks == 0) {
ImGui::TextColored(grey, "No hooks found"); ImGui::TextColored(grey, "No hooks found");
return; return;
} }
if (ImGui::BeginTable( ImGui::Text("Total Registered: %d", numHooks);
("Table##" + std::string(hookName)).c_str(),
4, if (ImGui::BeginTable(("Table##" + std::string(hookName)).c_str(), 4,
ImGuiTableFlags_Resizable | ImGuiTableFlags_Reorderable | ImGuiTableFlags_Hideable | ImGuiTableFlags_Borders | ImGuiTableFlags_SizingFixedFit ImGuiTableFlags_Resizable | ImGuiTableFlags_Reorderable | ImGuiTableFlags_Hideable |
)) { ImGuiTableFlags_Borders | ImGuiTableFlags_SizingFixedFit)) {
ImGui::TableSetupColumn("Id"); ImGui::TableSetupColumn("ID", ImGuiTableColumnFlags_WidthFixed);
ImGui::TableSetupColumn("Type"); ImGui::TableSetupColumn("Type", ImGuiTableColumnFlags_WidthFixed);
ImGui::TableSetupColumn("Registration Info"); ImGui::TableSetupColumn("Registration Info", ImGuiTableColumnFlags_WidthStretch);
//ImGui::TableSetupColumn("Stub"); ImGui::TableSetupColumn("# Calls", ImGuiTableColumnFlags_WidthFixed);
ImGui::TableSetupColumn("Number of Calls");
ImGui::TableHeadersRow(); ImGui::TableHeadersRow();
for (auto& [id, hookInfo] : (*hookData[hookName])) { for (auto& [id, hookInfo] : (*hookData[hookName])) {
ImGui::TableNextRow(); ImGui::TableNextRow();
@ -39,7 +40,7 @@ void DrawHookRegisteringInfos(const char* hookName) {
ImGui::Text("Normal"); ImGui::Text("Normal");
break; break;
case HOOK_TYPE_ID: case HOOK_TYPE_ID:
ImGui::Text("Id"); ImGui::Text("ID");
break; break;
case HOOK_TYPE_PTR: case HOOK_TYPE_PTR:
ImGui::Text("Ptr"); ImGui::Text("Ptr");
@ -54,27 +55,19 @@ void DrawHookRegisteringInfos(const char* hookName) {
ImGui::TableNextColumn(); ImGui::TableNextColumn();
if (hookInfo.registering.valid) { if (hookInfo.registering.valid) {
ImGui::Text("%s(%d:%d) %s", hookInfo.registering.file, hookInfo.registering.line, hookInfo.registering.column, hookInfo.registering.function); // Replace the space after the return type of the parent function with a non-breaking space
std::string parentFunction = std::string(hookInfo.registering.function);
size_t pos = parentFunction.find_first_of(" ");
if (pos != std::string::npos) {
parentFunction.replace(pos, 1, "\u00A0");
}
// Non breaking space to keep the arrow with the parent function
ImGui::TextWrapped("%s(%d:%d) <-\u00A0%s", hookInfo.registering.file, hookInfo.registering.line,
hookInfo.registering.column, parentFunction.c_str());
} else { } else {
ImGui::TextColored(yellow, "[Unavaliable]"); ImGui::TextColored(yellow, "[Unavailable]");
} }
//TODO: not currently possible
/*
ImGui::TableNextColumn();
ImGui::BeginDisabled();
bool stubButtonPressed = ImGui::Button(("Stub##" + std::to_string(id)).c_str());
UIWidgets::SetLastItemHoverText("Stub this hook.\nThis is not possible to automatically undo.");
if (stubButtonPressed) {
//stub
}
ImGui::EndDisabled();
*/
ImGui::TableNextColumn(); ImGui::TableNextColumn();
ImGui::Text("%d", hookInfo.calls); ImGui::Text("%d", hookInfo.calls);
} }
@ -84,12 +77,9 @@ void DrawHookRegisteringInfos(const char* hookName) {
void HookDebuggerWindow::DrawElement() { void HookDebuggerWindow::DrawElement() {
#ifndef __cpp_lib_source_location #ifndef __cpp_lib_source_location
ImGui::TextColored( ImGui::TextColored(yellow, "Some features of the Hook Debugger are unavailable because SoH was compiled "
yellow, "without \"<source_location>\" support "
"Some features of the Hook Debugger are unavaliable because SoH was compiled " "(\"__cpp_lib_source_location\" not defined in \"<version>\").");
"without \"<source_location>\" support "
"(\"__cpp_lib_source_location\" not defined in \"<version>\")."
);
#endif #endif
for (auto& [hookName, _] : hookData) { for (auto& [hookName, _] : hookData) {
@ -101,9 +91,9 @@ void HookDebuggerWindow::DrawElement() {
} }
void HookDebuggerWindow::InitElement() { void HookDebuggerWindow::InitElement() {
#define DEFINE_HOOK(name, _) hookData.insert({#name, GameInteractor::Instance->GetHookData<GameInteractor::name>()}); #define DEFINE_HOOK(name, _) hookData.insert({ #name, GameInteractor::Instance->GetHookData<GameInteractor::name>() });
#include "../game-interactor/GameInteractor_HookTable.h" #include "../game-interactor/GameInteractor_HookTable.h"
#undef DEFINE_HOOK #undef DEFINE_HOOK
} }

View file

@ -92,19 +92,20 @@ void GameInteractor_SetTriforceHuntCreditsWarpActive(uint8_t state);
} }
#endif #endif
#ifdef __cplusplus #ifdef __cplusplus
#include <stdarg.h> #include <stdarg.h>
#include <thread> #include <thread>
#include <map>
#include <unordered_map> #include <unordered_map>
#include <vector> #include <vector>
#include <functional> #include <functional>
#include <string> #include <string>
#include <version> #include <version>
#ifdef __cpp_lib_source_location #ifdef __cpp_lib_source_location
#include <source_location> #include <source_location>
#else #else
#pragma message("Compiling without <source_location> support, the Hook Debugger will not be avaliable") #pragma message("Compiling without <source_location> support, the Hook Debugger will not be available")
#endif #endif
typedef uint32_t HOOK_ID; typedef uint32_t HOOK_ID;
@ -124,24 +125,31 @@ struct HookRegisteringInfo {
const char* function; const char* function;
HookType type; HookType type;
HookRegisteringInfo() : valid(false), file("unknown file"), line(0), column(0), function("unknown function"), type(HOOK_TYPE_NORMAL) {} HookRegisteringInfo()
: valid(false), file("unknown file"), line(0), column(0), function("unknown function"), type(HOOK_TYPE_NORMAL) {
}
HookRegisteringInfo(const char* _file, std::uint_least32_t _line, std::uint_least32_t _column, const char* _function, HookType _type) : HookRegisteringInfo(const char* _file, std::uint_least32_t _line, std::uint_least32_t _column,
valid(true), file(_file), line(_line), column(_column), function(_function), type(_type) {} const char* _function, HookType _type)
: valid(true), file(_file), line(_line), column(_column), function(_function), type(_type) {
// Trim off user parent directories
const char* trimmed = strstr(_file, "soh/soh/");
if (trimmed != nullptr) {
file = trimmed;
}
}
}; };
struct HookInfo { struct HookInfo {
uint32_t calls; uint32_t calls;
HookRegisteringInfo registering; HookRegisteringInfo registering;
HookInfo() : calls(0), registering(HookRegisteringInfo{}) {}
HookInfo(HookRegisteringInfo _registering) : calls(0), registering(_registering) {}
}; };
#ifdef __cpp_lib_source_location #ifdef __cpp_lib_source_location
#define GET_CURRENT_REGISTERING_INFO(type) HookRegisteringInfo{location.file_name(), location.line(), location.column(), location.function_name(), type} #define GET_CURRENT_REGISTERING_INFO(type) \
(HookRegisteringInfo{ location.file_name(), location.line(), location.column(), location.function_name(), type })
#else #else
#define GET_CURRENT_REGISTERING_INFO(type) HookRegisteringInfo{} #define GET_CURRENT_REGISTERING_INFO(type) (HookRegisteringInfo{})
#endif #endif
#define REGISTER_VB_SHOULD(flag, body) \ #define REGISTER_VB_SHOULD(flag, body) \
@ -171,23 +179,23 @@ struct HookInfo {
hookId = GameInteractor::Instance->RegisterGameHookForID<GameInteractor::hookType>(id, body); \ hookId = GameInteractor::Instance->RegisterGameHookForID<GameInteractor::hookType>(id, body); \
} \ } \
} }
#define COND_VB_SHOULD(id, condition, body) \ #define COND_VB_SHOULD(id, condition, body) \
{ \ { \
static HOOK_ID hookId = 0; \ static HOOK_ID hookId = 0; \
GameInteractor::Instance->UnregisterGameHookForID<GameInteractor::OnVanillaBehavior>(hookId); \ GameInteractor::Instance->UnregisterGameHookForID<GameInteractor::OnVanillaBehavior>(hookId); \
hookId = 0; \ hookId = 0; \
if (condition) { \ if (condition) { \
hookId = REGISTER_VB_SHOULD(id, body); \ hookId = REGISTER_VB_SHOULD(id, body); \
} \ } \
} }
class GameInteractor { class GameInteractor {
public: public:
static GameInteractor* Instance; static GameInteractor* Instance;
// Game State // Game State
class State { class State {
public: public:
static bool NoUIActive; static bool NoUIActive;
static GILinkSize LinkSize; static GILinkSize LinkSize;
static bool InvisibleLinkActive; static bool InvisibleLinkActive;
@ -219,14 +227,15 @@ public:
// Game Hooks // Game Hooks
HOOK_ID nextHookId = 1; HOOK_ID nextHookId = 1;
template <typename H> struct RegisteredGameHooks { template <typename H> struct RegisteredGameHooks {
inline static std::unordered_map<HOOK_ID, typename H::fn> functions; inline static std::unordered_map<HOOK_ID, typename H::fn> functions;
inline static std::unordered_map<int32_t, std::unordered_map<HOOK_ID, typename H::fn>> functionsForID; inline static std::unordered_map<int32_t, std::unordered_map<HOOK_ID, typename H::fn>> functionsForID;
inline static std::unordered_map<uintptr_t, std::unordered_map<HOOK_ID, typename H::fn>> functionsForPtr; inline static std::unordered_map<uintptr_t, std::unordered_map<HOOK_ID, typename H::fn>> functionsForPtr;
inline static std::unordered_map<HOOK_ID, std::pair<typename H::filter, typename H::fn>> functionsForFilter; inline static std::unordered_map<HOOK_ID, std::pair<typename H::filter, typename H::fn>> functionsForFilter;
//Used for the hook debugger // Used for the hook debugger
inline static std::unordered_map<HOOK_ID, HookInfo> hookData; inline static std::map<HOOK_ID, HookInfo> hookData;
}; };
template <typename H> struct HooksToUnregister { template <typename H> struct HooksToUnregister {
@ -236,39 +245,43 @@ public:
inline static std::vector<HOOK_ID> hooksForFilter; inline static std::vector<HOOK_ID> hooksForFilter;
}; };
template <typename H> std::unordered_map<uint32_t, HookInfo>* GetHookData() { template <typename H> std::map<uint32_t, HookInfo>* GetHookData() {
return &RegisteredGameHooks<H>::hookData; return &RegisteredGameHooks<H>::hookData;
} }
// General Hooks // General Hooks
template <typename H> HOOK_ID RegisterGameHook( template <typename H>
typename H::fn h
#ifdef __cpp_lib_source_location #ifdef __cpp_lib_source_location
, const std::source_location location = std::source_location::current() HOOK_ID RegisterGameHook(typename H::fn h, const std::source_location location = std::source_location::current()) {
#else
HOOK_ID RegisterGameHook(typename H::fn h) {
#endif #endif
) { if (this->nextHookId == 0 || this->nextHookId >= UINT32_MAX)
// Ensure hook id is unique and not 0, which is reserved for invalid hooks this->nextHookId = 1;
if (this->nextHookId == 0 || this->nextHookId >= UINT32_MAX) this->nextHookId = 1;
while (RegisteredGameHooks<H>::functions.find(this->nextHookId) != RegisteredGameHooks<H>::functions.end()) { while (RegisteredGameHooks<H>::functions.find(this->nextHookId) != RegisteredGameHooks<H>::functions.end()) {
this->nextHookId++; this->nextHookId++;
} }
RegisteredGameHooks<H>::functions[this->nextHookId] = h; RegisteredGameHooks<H>::functions[this->nextHookId] = h;
RegisteredGameHooks<H>::hookData[this->nextHookId] = HookInfo{GET_CURRENT_REGISTERING_INFO(HOOK_TYPE_NORMAL)}; RegisteredGameHooks<H>::hookData[this->nextHookId] =
HookInfo{ 0, GET_CURRENT_REGISTERING_INFO(HOOK_TYPE_NORMAL) };
return this->nextHookId++; return this->nextHookId++;
} }
template <typename H> void UnregisterGameHook(HOOK_ID hookId) { template <typename H> void UnregisterGameHook(HOOK_ID hookId) {
if (hookId == 0) return; if (hookId == 0)
return;
HooksToUnregister<H>::hooks.push_back(hookId); HooksToUnregister<H>::hooks.push_back(hookId);
} }
template <typename H, typename... Args> void ExecuteHooks(Args&&... args) { template <typename H, typename... Args> void ExecuteHooks(Args&&... args) {
// Remove pending hooks for this type
for (auto& hookId : HooksToUnregister<H>::hooks) { for (auto& hookId : HooksToUnregister<H>::hooks) {
RegisteredGameHooks<H>::functions.erase(hookId); RegisteredGameHooks<H>::functions.erase(hookId);
RegisteredGameHooks<H>::hookData.erase(hookId); RegisteredGameHooks<H>::hookData.erase(hookId);
} }
HooksToUnregister<H>::hooks.clear(); HooksToUnregister<H>::hooks.clear();
// Execute hooks
for (auto& hook : RegisteredGameHooks<H>::functions) { for (auto& hook : RegisteredGameHooks<H>::functions) {
hook.second(std::forward<Args>(args)...); hook.second(std::forward<Args>(args)...);
RegisteredGameHooks<H>::hookData[hook.first].calls += 1; RegisteredGameHooks<H>::hookData[hook.first].calls += 1;
@ -276,39 +289,60 @@ public:
} }
// ID based Hooks // ID based Hooks
template <typename H> HOOK_ID RegisterGameHookForID( template <typename H>
int32_t id, typename H::fn h
#ifdef __cpp_lib_source_location #ifdef __cpp_lib_source_location
, const std::source_location location = std::source_location::current() HOOK_ID RegisterGameHookForID(int32_t id, typename H::fn h,
std::source_location location = std::source_location::current()) {
#else
HOOK_ID RegisterGameHookForID(int32_t id, typename H::fn h) {
#endif #endif
) { if (this->nextHookId == 0 || this->nextHookId >= UINT32_MAX)
if (this->nextHookId == 0 || this->nextHookId >= UINT32_MAX) this->nextHookId = 1; this->nextHookId = 1;
while (RegisteredGameHooks<H>::functionsForID[id].find(this->nextHookId) != RegisteredGameHooks<H>::functionsForID[id].end()) { while (RegisteredGameHooks<H>::functionsForID[id].find(this->nextHookId) !=
RegisteredGameHooks<H>::functionsForID[id].end()) {
this->nextHookId++; this->nextHookId++;
} }
RegisteredGameHooks<H>::functionsForID[id][this->nextHookId] = h; RegisteredGameHooks<H>::functionsForID[id][this->nextHookId] = h;
RegisteredGameHooks<H>::hookData[this->nextHookId] = HookInfo{GET_CURRENT_REGISTERING_INFO(HOOK_TYPE_ID)}; RegisteredGameHooks<H>::hookData[this->nextHookId] = HookInfo{ 0, GET_CURRENT_REGISTERING_INFO(HOOK_TYPE_ID) };
return this->nextHookId++; return this->nextHookId++;
} }
template <typename H> void UnregisterGameHookForID(HOOK_ID hookId) { template <typename H> void UnregisterGameHookForID(HOOK_ID hookId) {
if (hookId == 0) return; if (hookId == 0)
return;
HooksToUnregister<H>::hooksForID.push_back(hookId); HooksToUnregister<H>::hooksForID.push_back(hookId);
} }
template <typename H, typename... Args> void ExecuteHooksForID(int32_t id, Args&&... args) { template <typename H, typename... Args> void ExecuteHooksForID(int32_t id, Args&&... args) {
for (auto& hookId : HooksToUnregister<H>::hooksForID) { // Remove pending hooks for this type
for (auto it = RegisteredGameHooks<H>::functionsForID[id].begin(); it != RegisteredGameHooks<H>::functionsForID[id].end(); ) { for (auto hookIdIt = HooksToUnregister<H>::hooksForID.begin();
if (it->first == hookId) { hookIdIt != HooksToUnregister<H>::hooksForID.end();) {
bool remove = false;
if (RegisteredGameHooks<H>::functionsForID[id].size() == 0) {
break;
}
for (auto it = RegisteredGameHooks<H>::functionsForID[id].begin();
it != RegisteredGameHooks<H>::functionsForID[id].end();) {
if (it->first == *hookIdIt) {
it = RegisteredGameHooks<H>::functionsForID[id].erase(it); it = RegisteredGameHooks<H>::functionsForID[id].erase(it);
HooksToUnregister<H>::hooksForID.erase(std::remove(HooksToUnregister<H>::hooksForID.begin(), HooksToUnregister<H>::hooksForID.end(), hookId), HooksToUnregister<H>::hooksForID.end()); RegisteredGameHooks<H>::hookData.erase(*hookIdIt);
RegisteredGameHooks<H>::hookData.erase(hookId); remove = true;
break;
} else { } else {
++it; ++it;
} }
} }
if (remove) {
hookIdIt = HooksToUnregister<H>::hooksForID.erase(hookIdIt);
} else {
++hookIdIt;
}
} }
// Execute hooks
for (auto& hook : RegisteredGameHooks<H>::functionsForID[id]) { for (auto& hook : RegisteredGameHooks<H>::functionsForID[id]) {
hook.second(std::forward<Args>(args)...); hook.second(std::forward<Args>(args)...);
RegisteredGameHooks<H>::hookData[hook.first].calls += 1; RegisteredGameHooks<H>::hookData[hook.first].calls += 1;
@ -316,39 +350,60 @@ public:
} }
// PTR based Hooks // PTR based Hooks
template <typename H> HOOK_ID RegisterGameHookForPtr( template <typename H>
uintptr_t ptr, typename H::fn h
#ifdef __cpp_lib_source_location #ifdef __cpp_lib_source_location
, const std::source_location location = std::source_location::current() HOOK_ID RegisterGameHookForPtr(uintptr_t ptr, typename H::fn h,
const std::source_location location = std::source_location::current()) {
#else
HOOK_ID RegisterGameHookForPtr(uintptr_t ptr, typename H::fn h) {
#endif #endif
) { if (this->nextHookId == 0 || this->nextHookId >= UINT32_MAX)
if (this->nextHookId == 0 || this->nextHookId >= UINT32_MAX) this->nextHookId = 1; this->nextHookId = 1;
while (RegisteredGameHooks<H>::functionsForPtr[ptr].find(this->nextHookId) != RegisteredGameHooks<H>::functionsForPtr[ptr].end()) { while (RegisteredGameHooks<H>::functionsForPtr[ptr].find(this->nextHookId) !=
RegisteredGameHooks<H>::functionsForPtr[ptr].end()) {
this->nextHookId++; this->nextHookId++;
} }
RegisteredGameHooks<H>::functionsForPtr[ptr][this->nextHookId] = h; RegisteredGameHooks<H>::functionsForPtr[ptr][this->nextHookId] = h;
RegisteredGameHooks<H>::hookData[this->nextHookId] = HookInfo{GET_CURRENT_REGISTERING_INFO(HOOK_TYPE_PTR)}; RegisteredGameHooks<H>::hookData[this->nextHookId] = HookInfo{ 0, GET_CURRENT_REGISTERING_INFO(HOOK_TYPE_PTR) };
return this->nextHookId++; return this->nextHookId++;
} }
template <typename H> void UnregisterGameHookForPtr(HOOK_ID hookId) { template <typename H> void UnregisterGameHookForPtr(HOOK_ID hookId) {
if (hookId == 0) return; if (hookId == 0)
return;
HooksToUnregister<H>::hooksForPtr.push_back(hookId); HooksToUnregister<H>::hooksForPtr.push_back(hookId);
} }
template <typename H, typename... Args> void ExecuteHooksForPtr(uintptr_t ptr, Args&&... args) { template <typename H, typename... Args> void ExecuteHooksForPtr(uintptr_t ptr, Args&&... args) {
for (auto& hookId : HooksToUnregister<H>::hooksForPtr) { // Remove pending hooks for this type
for (auto it = RegisteredGameHooks<H>::functionsForPtr[ptr].begin(); it != RegisteredGameHooks<H>::functionsForPtr[ptr].end(); ) { for (auto hookIdIt = HooksToUnregister<H>::hooksForPtr.begin();
if (it->first == hookId) { hookIdIt != HooksToUnregister<H>::hooksForPtr.end();) {
bool remove = false;
if (RegisteredGameHooks<H>::functionsForPtr[ptr].size() == 0) {
break;
}
for (auto it = RegisteredGameHooks<H>::functionsForPtr[ptr].begin();
it != RegisteredGameHooks<H>::functionsForPtr[ptr].end();) {
if (it->first == *hookIdIt) {
it = RegisteredGameHooks<H>::functionsForPtr[ptr].erase(it); it = RegisteredGameHooks<H>::functionsForPtr[ptr].erase(it);
HooksToUnregister<H>::hooksForPtr.erase(std::remove(HooksToUnregister<H>::hooksForPtr.begin(), HooksToUnregister<H>::hooksForPtr.end(), hookId), HooksToUnregister<H>::hooksForPtr.end()); RegisteredGameHooks<H>::hookData.erase(*hookIdIt);
RegisteredGameHooks<H>::hookData.erase(hookId); remove = true;
break;
} else { } else {
++it; ++it;
} }
} }
if (remove) {
hookIdIt = HooksToUnregister<H>::hooksForPtr.erase(hookIdIt);
} else {
++hookIdIt;
}
} }
// Execute hooks
for (auto& hook : RegisteredGameHooks<H>::functionsForPtr[ptr]) { for (auto& hook : RegisteredGameHooks<H>::functionsForPtr[ptr]) {
hook.second(std::forward<Args>(args)...); hook.second(std::forward<Args>(args)...);
RegisteredGameHooks<H>::hookData[hook.first].calls += 1; RegisteredGameHooks<H>::hookData[hook.first].calls += 1;
@ -356,33 +411,40 @@ public:
} }
// Filter based Hooks // Filter based Hooks
template <typename H> HOOK_ID RegisterGameHookForFilter( template <typename H>
typename H::filter f, typename H::fn h
#ifdef __cpp_lib_source_location #ifdef __cpp_lib_source_location
, const std::source_location location = std::source_location::current() HOOK_ID RegisterGameHookForFilter(typename H::filter f, typename H::fn h,
const std::source_location location = std::source_location::current()) {
#else
HOOK_ID RegisterGameHookForFilter(typename H::filter f, typename H::fn h) {
#endif #endif
) { if (this->nextHookId == 0 || this->nextHookId >= UINT32_MAX)
if (this->nextHookId == 0 || this->nextHookId >= UINT32_MAX) this->nextHookId = 1; this->nextHookId = 1;
while (RegisteredGameHooks<H>::functionsForFilter.find(this->nextHookId) != RegisteredGameHooks<H>::functionsForFilter.end()) { while (RegisteredGameHooks<H>::functionsForFilter.find(this->nextHookId) !=
RegisteredGameHooks<H>::functionsForFilter.end()) {
this->nextHookId++; this->nextHookId++;
} }
RegisteredGameHooks<H>::functionsForFilter[this->nextHookId] = std::make_pair(f, h); RegisteredGameHooks<H>::functionsForFilter[this->nextHookId] = std::make_pair(f, h);
RegisteredGameHooks<H>::hookData[this->nextHookId] = HookInfo{GET_CURRENT_REGISTERING_INFO(HOOK_TYPE_FILTER)}; RegisteredGameHooks<H>::hookData[this->nextHookId] =
HookInfo{ 0, GET_CURRENT_REGISTERING_INFO(HOOK_TYPE_FILTER) };
return this->nextHookId++; return this->nextHookId++;
} }
template <typename H> void UnregisterGameHookForFilter(HOOK_ID hookId) { template <typename H> void UnregisterGameHookForFilter(HOOK_ID hookId) {
if (hookId == 0) return; if (hookId == 0)
return;
HooksToUnregister<H>::hooksForFilter.push_back(hookId); HooksToUnregister<H>::hooksForFilter.push_back(hookId);
} }
template <typename H, typename... Args> void ExecuteHooksForFilter(Args&&... args) { template <typename H, typename... Args> void ExecuteHooksForFilter(Args&&... args) {
// Remove pending hooks for this type
for (auto& hookId : HooksToUnregister<H>::hooksForFilter) { for (auto& hookId : HooksToUnregister<H>::hooksForFilter) {
RegisteredGameHooks<H>::functionsForFilter.erase(hookId); RegisteredGameHooks<H>::functionsForFilter.erase(hookId);
RegisteredGameHooks<H>::hookData.erase(hookId); RegisteredGameHooks<H>::hookData.erase(hookId);
} }
HooksToUnregister<H>::hooksForFilter.clear(); HooksToUnregister<H>::hooksForFilter.clear();
// Execute hooks
for (auto& hook : RegisteredGameHooks<H>::functionsForFilter) { for (auto& hook : RegisteredGameHooks<H>::functionsForFilter) {
if (hook.second.first(std::forward<Args>(args)...)) { if (hook.second.first(std::forward<Args>(args)...)) {
hook.second.second(std::forward<Args>(args)...); hook.second.second(std::forward<Args>(args)...);
@ -391,8 +453,62 @@ public:
} }
} }
template <typename H> void ProcessUnregisteredHooks() {
// Normal
for (auto& hookId : HooksToUnregister<H>::hooks) {
RegisteredGameHooks<H>::functions.erase(hookId);
RegisteredGameHooks<H>::hookData.erase(hookId);
}
HooksToUnregister<H>::hooks.clear();
// ID
for (auto& hookId : HooksToUnregister<H>::hooksForID) {
for (auto& idGroup : RegisteredGameHooks<H>::functionsForID) {
for (auto it = idGroup.second.begin(); it != idGroup.second.end();) {
if (it->first == hookId) {
it = idGroup.second.erase(it);
RegisteredGameHooks<H>::hookData.erase(hookId);
} else {
++it;
}
}
}
}
HooksToUnregister<H>::hooksForID.clear();
// Ptr
for (auto& hookId : HooksToUnregister<H>::hooksForPtr) {
for (auto& ptrGroup : RegisteredGameHooks<H>::functionsForPtr) {
for (auto it = ptrGroup.second.begin(); it != ptrGroup.second.end();) {
if (it->first == hookId) {
it = ptrGroup.second.erase(it);
RegisteredGameHooks<H>::hookData.erase(hookId);
} else {
++it;
}
}
}
}
HooksToUnregister<H>::hooksForPtr.clear();
// Filter
for (auto& hookId : HooksToUnregister<H>::hooksForFilter) {
RegisteredGameHooks<H>::functionsForFilter.erase(hookId);
RegisteredGameHooks<H>::hookData.erase(hookId);
}
HooksToUnregister<H>::hooksForFilter.clear();
}
void RemoveAllQueuedHooks() {
#define DEFINE_HOOK(name, _) ProcessUnregisteredHooks<name>();
#include "GameInteractor_HookTable.h"
#undef DEFINE_HOOK
}
class HookFilter { class HookFilter {
public: public:
static auto ActorNotPlayer(Actor* actor) { static auto ActorNotPlayer(Actor* actor) {
return actor->id != ACTOR_PLAYER; return actor->id != ACTOR_PLAYER;
} }
@ -401,15 +517,11 @@ public:
return actor->id != ACTOR_PLAYER; return actor->id != ACTOR_PLAYER;
} }
static auto ActorMatchIdAndParams(int16_t id, int16_t params) { static auto ActorMatchIdAndParams(int16_t id, int16_t params) {
return [id, params](Actor* actor) { return [id, params](Actor* actor) { return actor->id == id && actor->params == params; };
return actor->id == id && actor->params == params;
};
} }
// For use with Should hooks // For use with Should hooks
static auto SActorMatchIdAndParams(int16_t id, int16_t params) { static auto SActorMatchIdAndParams(int16_t id, int16_t params) {
return [id, params](Actor* actor, bool* result) { return [id, params](Actor* actor, bool* result) { return actor->id == id && actor->params == params; };
return actor->id == id && actor->params == params;
};
} }
}; };
@ -430,7 +542,7 @@ public:
static bool CanAddOrTakeAmmo(int16_t amount, int16_t item); static bool CanAddOrTakeAmmo(int16_t amount, int16_t item);
class RawAction { class RawAction {
public: public:
static void SetSceneFlag(int16_t sceneNum, int16_t flagType, int16_t flag); static void SetSceneFlag(int16_t sceneNum, int16_t flagType, int16_t flag);
static void UnsetSceneFlag(int16_t sceneNum, int16_t flagType, int16_t flag); static void UnsetSceneFlag(int16_t sceneNum, int16_t flagType, int16_t flag);
static bool CheckFlag(int16_t flagType, int16_t flag); static bool CheckFlag(int16_t flagType, int16_t flag);

View file

@ -19,6 +19,9 @@ void GameInteractor_ExecuteOnExitGame(int32_t fileNum) {
} }
void GameInteractor_ExecuteOnGameStateMainStart() { void GameInteractor_ExecuteOnGameStateMainStart() {
// Cleanup all hooks at the start of each frame
GameInteractor::Instance->RemoveAllQueuedHooks();
GameInteractor::Instance->ExecuteHooks<GameInteractor::OnGameStateMainStart>(); GameInteractor::Instance->ExecuteHooks<GameInteractor::OnGameStateMainStart>();
} }