diff --git a/soh/soh/Enhancements/debugger/hookDebugger.cpp b/soh/soh/Enhancements/debugger/hookDebugger.cpp index 782cf4f7a..a9d6ca891 100644 --- a/soh/soh/Enhancements/debugger/hookDebugger.cpp +++ b/soh/soh/Enhancements/debugger/hookDebugger.cpp @@ -4,28 +4,29 @@ #include #include -static std::unordered_map*> hookData; +static std::map*> hookData; const ImVec4 grey = ImVec4(0.75, 0.75, 0.75, 1); const ImVec4 yellow = ImVec4(1, 1, 0, 1); const ImVec4 red = ImVec4(1, 0, 0, 1); 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"); return; } - if (ImGui::BeginTable( - ("Table##" + std::string(hookName)).c_str(), - 4, - ImGuiTableFlags_Resizable | ImGuiTableFlags_Reorderable | ImGuiTableFlags_Hideable | ImGuiTableFlags_Borders | ImGuiTableFlags_SizingFixedFit - )) { - ImGui::TableSetupColumn("Id"); - ImGui::TableSetupColumn("Type"); - ImGui::TableSetupColumn("Registration Info"); - //ImGui::TableSetupColumn("Stub"); - ImGui::TableSetupColumn("Number of Calls"); + ImGui::Text("Total Registered: %d", numHooks); + + if (ImGui::BeginTable(("Table##" + std::string(hookName)).c_str(), 4, + ImGuiTableFlags_Resizable | ImGuiTableFlags_Reorderable | ImGuiTableFlags_Hideable | + ImGuiTableFlags_Borders | ImGuiTableFlags_SizingFixedFit)) { + ImGui::TableSetupColumn("ID", ImGuiTableColumnFlags_WidthFixed); + ImGui::TableSetupColumn("Type", ImGuiTableColumnFlags_WidthFixed); + ImGui::TableSetupColumn("Registration Info", ImGuiTableColumnFlags_WidthStretch); + ImGui::TableSetupColumn("# Calls", ImGuiTableColumnFlags_WidthFixed); ImGui::TableHeadersRow(); for (auto& [id, hookInfo] : (*hookData[hookName])) { ImGui::TableNextRow(); @@ -39,7 +40,7 @@ void DrawHookRegisteringInfos(const char* hookName) { ImGui::Text("Normal"); break; case HOOK_TYPE_ID: - ImGui::Text("Id"); + ImGui::Text("ID"); break; case HOOK_TYPE_PTR: ImGui::Text("Ptr"); @@ -54,27 +55,19 @@ void DrawHookRegisteringInfos(const char* hookName) { ImGui::TableNextColumn(); 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 { - 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::Text("%d", hookInfo.calls); } @@ -84,12 +77,9 @@ void DrawHookRegisteringInfos(const char* hookName) { void HookDebuggerWindow::DrawElement() { #ifndef __cpp_lib_source_location - ImGui::TextColored( - yellow, - "Some features of the Hook Debugger are unavaliable because SoH was compiled " - "without \"\" support " - "(\"__cpp_lib_source_location\" not defined in \"\")." - ); + ImGui::TextColored(yellow, "Some features of the Hook Debugger are unavailable because SoH was compiled " + "without \"\" support " + "(\"__cpp_lib_source_location\" not defined in \"\")."); #endif for (auto& [hookName, _] : hookData) { @@ -101,9 +91,9 @@ void HookDebuggerWindow::DrawElement() { } void HookDebuggerWindow::InitElement() { - #define DEFINE_HOOK(name, _) hookData.insert({#name, GameInteractor::Instance->GetHookData()}); +#define DEFINE_HOOK(name, _) hookData.insert({ #name, GameInteractor::Instance->GetHookData() }); - #include "../game-interactor/GameInteractor_HookTable.h" +#include "../game-interactor/GameInteractor_HookTable.h" - #undef DEFINE_HOOK +#undef DEFINE_HOOK } diff --git a/soh/soh/Enhancements/game-interactor/GameInteractor.h b/soh/soh/Enhancements/game-interactor/GameInteractor.h index 8c6be086f..4d8e6dcb4 100644 --- a/soh/soh/Enhancements/game-interactor/GameInteractor.h +++ b/soh/soh/Enhancements/game-interactor/GameInteractor.h @@ -92,19 +92,20 @@ void GameInteractor_SetTriforceHuntCreditsWarpActive(uint8_t state); } #endif - #ifdef __cplusplus #include #include +#include #include #include #include #include + #include #ifdef __cpp_lib_source_location #include #else -#pragma message("Compiling without support, the Hook Debugger will not be avaliable") +#pragma message("Compiling without support, the Hook Debugger will not be available") #endif typedef uint32_t HOOK_ID; @@ -124,24 +125,31 @@ struct HookRegisteringInfo { const char* function; 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) : - valid(true), file(_file), line(_line), column(_column), function(_function), type(_type) {} + HookRegisteringInfo(const char* _file, std::uint_least32_t _line, std::uint_least32_t _column, + 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 { uint32_t calls; HookRegisteringInfo registering; - - HookInfo() : calls(0), registering(HookRegisteringInfo{}) {} - HookInfo(HookRegisteringInfo _registering) : calls(0), registering(_registering) {} }; #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 -#define GET_CURRENT_REGISTERING_INFO(type) HookRegisteringInfo{} +#define GET_CURRENT_REGISTERING_INFO(type) (HookRegisteringInfo{}) #endif #define REGISTER_VB_SHOULD(flag, body) \ @@ -171,23 +179,23 @@ struct HookInfo { hookId = GameInteractor::Instance->RegisterGameHookForID(id, body); \ } \ } -#define COND_VB_SHOULD(id, condition, body) \ - { \ - static HOOK_ID hookId = 0; \ +#define COND_VB_SHOULD(id, condition, body) \ + { \ + static HOOK_ID hookId = 0; \ GameInteractor::Instance->UnregisterGameHookForID(hookId); \ - hookId = 0; \ - if (condition) { \ - hookId = REGISTER_VB_SHOULD(id, body); \ - } \ + hookId = 0; \ + if (condition) { \ + hookId = REGISTER_VB_SHOULD(id, body); \ + } \ } class GameInteractor { -public: + public: static GameInteractor* Instance; // Game State class State { - public: + public: static bool NoUIActive; static GILinkSize LinkSize; static bool InvisibleLinkActive; @@ -219,14 +227,15 @@ public: // Game Hooks HOOK_ID nextHookId = 1; + template struct RegisteredGameHooks { inline static std::unordered_map functions; inline static std::unordered_map> functionsForID; inline static std::unordered_map> functionsForPtr; inline static std::unordered_map> functionsForFilter; - //Used for the hook debugger - inline static std::unordered_map hookData; + // Used for the hook debugger + inline static std::map hookData; }; template struct HooksToUnregister { @@ -236,39 +245,43 @@ public: inline static std::vector hooksForFilter; }; - template std::unordered_map* GetHookData() { + template std::map* GetHookData() { return &RegisteredGameHooks::hookData; } // General Hooks - template HOOK_ID RegisterGameHook( - typename H::fn h + template #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 - ) { - // Ensure hook id is unique and not 0, which is reserved for invalid hooks - if (this->nextHookId == 0 || this->nextHookId >= UINT32_MAX) this->nextHookId = 1; + if (this->nextHookId == 0 || this->nextHookId >= UINT32_MAX) + this->nextHookId = 1; while (RegisteredGameHooks::functions.find(this->nextHookId) != RegisteredGameHooks::functions.end()) { this->nextHookId++; } RegisteredGameHooks::functions[this->nextHookId] = h; - RegisteredGameHooks::hookData[this->nextHookId] = HookInfo{GET_CURRENT_REGISTERING_INFO(HOOK_TYPE_NORMAL)}; + RegisteredGameHooks::hookData[this->nextHookId] = + HookInfo{ 0, GET_CURRENT_REGISTERING_INFO(HOOK_TYPE_NORMAL) }; return this->nextHookId++; } template void UnregisterGameHook(HOOK_ID hookId) { - if (hookId == 0) return; + if (hookId == 0) + return; HooksToUnregister::hooks.push_back(hookId); } template void ExecuteHooks(Args&&... args) { + // Remove pending hooks for this type for (auto& hookId : HooksToUnregister::hooks) { RegisteredGameHooks::functions.erase(hookId); RegisteredGameHooks::hookData.erase(hookId); } HooksToUnregister::hooks.clear(); + // Execute hooks for (auto& hook : RegisteredGameHooks::functions) { hook.second(std::forward(args)...); RegisteredGameHooks::hookData[hook.first].calls += 1; @@ -276,39 +289,60 @@ public: } // ID based Hooks - template HOOK_ID RegisterGameHookForID( - int32_t id, typename H::fn h + template #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 - ) { - if (this->nextHookId == 0 || this->nextHookId >= UINT32_MAX) this->nextHookId = 1; - while (RegisteredGameHooks::functionsForID[id].find(this->nextHookId) != RegisteredGameHooks::functionsForID[id].end()) { + if (this->nextHookId == 0 || this->nextHookId >= UINT32_MAX) + this->nextHookId = 1; + while (RegisteredGameHooks::functionsForID[id].find(this->nextHookId) != + RegisteredGameHooks::functionsForID[id].end()) { this->nextHookId++; } RegisteredGameHooks::functionsForID[id][this->nextHookId] = h; - RegisteredGameHooks::hookData[this->nextHookId] = HookInfo{GET_CURRENT_REGISTERING_INFO(HOOK_TYPE_ID)}; + RegisteredGameHooks::hookData[this->nextHookId] = HookInfo{ 0, GET_CURRENT_REGISTERING_INFO(HOOK_TYPE_ID) }; return this->nextHookId++; } template void UnregisterGameHookForID(HOOK_ID hookId) { - if (hookId == 0) return; + if (hookId == 0) + return; HooksToUnregister::hooksForID.push_back(hookId); } template void ExecuteHooksForID(int32_t id, Args&&... args) { - for (auto& hookId : HooksToUnregister::hooksForID) { - for (auto it = RegisteredGameHooks::functionsForID[id].begin(); it != RegisteredGameHooks::functionsForID[id].end(); ) { - if (it->first == hookId) { + // Remove pending hooks for this type + for (auto hookIdIt = HooksToUnregister::hooksForID.begin(); + hookIdIt != HooksToUnregister::hooksForID.end();) { + bool remove = false; + + if (RegisteredGameHooks::functionsForID[id].size() == 0) { + break; + } + + for (auto it = RegisteredGameHooks::functionsForID[id].begin(); + it != RegisteredGameHooks::functionsForID[id].end();) { + if (it->first == *hookIdIt) { it = RegisteredGameHooks::functionsForID[id].erase(it); - HooksToUnregister::hooksForID.erase(std::remove(HooksToUnregister::hooksForID.begin(), HooksToUnregister::hooksForID.end(), hookId), HooksToUnregister::hooksForID.end()); - RegisteredGameHooks::hookData.erase(hookId); + RegisteredGameHooks::hookData.erase(*hookIdIt); + remove = true; + break; } else { ++it; } } + + if (remove) { + hookIdIt = HooksToUnregister::hooksForID.erase(hookIdIt); + } else { + ++hookIdIt; + } } + // Execute hooks for (auto& hook : RegisteredGameHooks::functionsForID[id]) { hook.second(std::forward(args)...); RegisteredGameHooks::hookData[hook.first].calls += 1; @@ -316,39 +350,60 @@ public: } // PTR based Hooks - template HOOK_ID RegisterGameHookForPtr( - uintptr_t ptr, typename H::fn h + template #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 - ) { - if (this->nextHookId == 0 || this->nextHookId >= UINT32_MAX) this->nextHookId = 1; - while (RegisteredGameHooks::functionsForPtr[ptr].find(this->nextHookId) != RegisteredGameHooks::functionsForPtr[ptr].end()) { + if (this->nextHookId == 0 || this->nextHookId >= UINT32_MAX) + this->nextHookId = 1; + while (RegisteredGameHooks::functionsForPtr[ptr].find(this->nextHookId) != + RegisteredGameHooks::functionsForPtr[ptr].end()) { this->nextHookId++; } RegisteredGameHooks::functionsForPtr[ptr][this->nextHookId] = h; - RegisteredGameHooks::hookData[this->nextHookId] = HookInfo{GET_CURRENT_REGISTERING_INFO(HOOK_TYPE_PTR)}; + RegisteredGameHooks::hookData[this->nextHookId] = HookInfo{ 0, GET_CURRENT_REGISTERING_INFO(HOOK_TYPE_PTR) }; return this->nextHookId++; } template void UnregisterGameHookForPtr(HOOK_ID hookId) { - if (hookId == 0) return; + if (hookId == 0) + return; HooksToUnregister::hooksForPtr.push_back(hookId); } template void ExecuteHooksForPtr(uintptr_t ptr, Args&&... args) { - for (auto& hookId : HooksToUnregister::hooksForPtr) { - for (auto it = RegisteredGameHooks::functionsForPtr[ptr].begin(); it != RegisteredGameHooks::functionsForPtr[ptr].end(); ) { - if (it->first == hookId) { + // Remove pending hooks for this type + for (auto hookIdIt = HooksToUnregister::hooksForPtr.begin(); + hookIdIt != HooksToUnregister::hooksForPtr.end();) { + bool remove = false; + + if (RegisteredGameHooks::functionsForPtr[ptr].size() == 0) { + break; + } + + for (auto it = RegisteredGameHooks::functionsForPtr[ptr].begin(); + it != RegisteredGameHooks::functionsForPtr[ptr].end();) { + if (it->first == *hookIdIt) { it = RegisteredGameHooks::functionsForPtr[ptr].erase(it); - HooksToUnregister::hooksForPtr.erase(std::remove(HooksToUnregister::hooksForPtr.begin(), HooksToUnregister::hooksForPtr.end(), hookId), HooksToUnregister::hooksForPtr.end()); - RegisteredGameHooks::hookData.erase(hookId); + RegisteredGameHooks::hookData.erase(*hookIdIt); + remove = true; + break; } else { ++it; } } + + if (remove) { + hookIdIt = HooksToUnregister::hooksForPtr.erase(hookIdIt); + } else { + ++hookIdIt; + } } + // Execute hooks for (auto& hook : RegisteredGameHooks::functionsForPtr[ptr]) { hook.second(std::forward(args)...); RegisteredGameHooks::hookData[hook.first].calls += 1; @@ -356,33 +411,40 @@ public: } // Filter based Hooks - template HOOK_ID RegisterGameHookForFilter( - typename H::filter f, typename H::fn h + template #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 - ) { - if (this->nextHookId == 0 || this->nextHookId >= UINT32_MAX) this->nextHookId = 1; - while (RegisteredGameHooks::functionsForFilter.find(this->nextHookId) != RegisteredGameHooks::functionsForFilter.end()) { + if (this->nextHookId == 0 || this->nextHookId >= UINT32_MAX) + this->nextHookId = 1; + while (RegisteredGameHooks::functionsForFilter.find(this->nextHookId) != + RegisteredGameHooks::functionsForFilter.end()) { this->nextHookId++; } RegisteredGameHooks::functionsForFilter[this->nextHookId] = std::make_pair(f, h); - RegisteredGameHooks::hookData[this->nextHookId] = HookInfo{GET_CURRENT_REGISTERING_INFO(HOOK_TYPE_FILTER)}; + RegisteredGameHooks::hookData[this->nextHookId] = + HookInfo{ 0, GET_CURRENT_REGISTERING_INFO(HOOK_TYPE_FILTER) }; return this->nextHookId++; } template void UnregisterGameHookForFilter(HOOK_ID hookId) { - if (hookId == 0) return; + if (hookId == 0) + return; HooksToUnregister::hooksForFilter.push_back(hookId); } template void ExecuteHooksForFilter(Args&&... args) { + // Remove pending hooks for this type for (auto& hookId : HooksToUnregister::hooksForFilter) { RegisteredGameHooks::functionsForFilter.erase(hookId); RegisteredGameHooks::hookData.erase(hookId); } HooksToUnregister::hooksForFilter.clear(); + // Execute hooks for (auto& hook : RegisteredGameHooks::functionsForFilter) { if (hook.second.first(std::forward(args)...)) { hook.second.second(std::forward(args)...); @@ -391,8 +453,62 @@ public: } } + template void ProcessUnregisteredHooks() { + // Normal + for (auto& hookId : HooksToUnregister::hooks) { + RegisteredGameHooks::functions.erase(hookId); + RegisteredGameHooks::hookData.erase(hookId); + } + HooksToUnregister::hooks.clear(); + + // ID + for (auto& hookId : HooksToUnregister::hooksForID) { + for (auto& idGroup : RegisteredGameHooks::functionsForID) { + for (auto it = idGroup.second.begin(); it != idGroup.second.end();) { + if (it->first == hookId) { + it = idGroup.second.erase(it); + RegisteredGameHooks::hookData.erase(hookId); + } else { + ++it; + } + } + } + } + HooksToUnregister::hooksForID.clear(); + + // Ptr + for (auto& hookId : HooksToUnregister::hooksForPtr) { + for (auto& ptrGroup : RegisteredGameHooks::functionsForPtr) { + for (auto it = ptrGroup.second.begin(); it != ptrGroup.second.end();) { + if (it->first == hookId) { + it = ptrGroup.second.erase(it); + RegisteredGameHooks::hookData.erase(hookId); + } else { + ++it; + } + } + } + } + HooksToUnregister::hooksForPtr.clear(); + + // Filter + for (auto& hookId : HooksToUnregister::hooksForFilter) { + RegisteredGameHooks::functionsForFilter.erase(hookId); + RegisteredGameHooks::hookData.erase(hookId); + } + HooksToUnregister::hooksForFilter.clear(); + } + + void RemoveAllQueuedHooks() { +#define DEFINE_HOOK(name, _) ProcessUnregisteredHooks(); + +#include "GameInteractor_HookTable.h" + +#undef DEFINE_HOOK + } + class HookFilter { - public: + public: static auto ActorNotPlayer(Actor* actor) { return actor->id != ACTOR_PLAYER; } @@ -401,15 +517,11 @@ public: return actor->id != ACTOR_PLAYER; } static auto ActorMatchIdAndParams(int16_t id, int16_t params) { - return [id, params](Actor* actor) { - return actor->id == id && actor->params == params; - }; + return [id, params](Actor* actor) { return actor->id == id && actor->params == params; }; } // For use with Should hooks static auto SActorMatchIdAndParams(int16_t id, int16_t params) { - return [id, params](Actor* actor, bool* result) { - return actor->id == id && actor->params == params; - }; + return [id, params](Actor* actor, bool* result) { return actor->id == id && actor->params == params; }; } }; @@ -430,7 +542,7 @@ public: static bool CanAddOrTakeAmmo(int16_t amount, int16_t item); class RawAction { - public: + public: 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 bool CheckFlag(int16_t flagType, int16_t flag); diff --git a/soh/soh/Enhancements/game-interactor/GameInteractor_Hooks.cpp b/soh/soh/Enhancements/game-interactor/GameInteractor_Hooks.cpp index b41030cfa..9c861062b 100644 --- a/soh/soh/Enhancements/game-interactor/GameInteractor_Hooks.cpp +++ b/soh/soh/Enhancements/game-interactor/GameInteractor_Hooks.cpp @@ -19,6 +19,9 @@ void GameInteractor_ExecuteOnExitGame(int32_t fileNum) { } void GameInteractor_ExecuteOnGameStateMainStart() { + // Cleanup all hooks at the start of each frame + GameInteractor::Instance->RemoveAllQueuedHooks(); + GameInteractor::Instance->ExecuteHooks(); }