diff --git a/libultraship/libultraship/GameSettings.cpp b/libultraship/libultraship/GameSettings.cpp index 0cd9aa2f4..51c7034c8 100644 --- a/libultraship/libultraship/GameSettings.cpp +++ b/libultraship/libultraship/GameSettings.cpp @@ -54,14 +54,12 @@ namespace Game { } void InitSettings() { - ModInternal::registerHookListener({ AUDIO_INIT, [](HookEvent ev) { - UpdateAudio(); - }}); - ModInternal::registerHookListener({ GFX_INIT, [](HookEvent ev) { + ModInternal::RegisterHook(UpdateAudio); + ModInternal::RegisterHook([] { gfx_get_current_rendering_api()->set_texture_filter((FilteringMode) CVar_GetS32("gTextureFilter", THREE_POINT)); SohImGui::console->opened = CVar_GetS32("gConsoleEnabled", 0); UpdateAudio(); - }}); + }); } void SetSeqPlayerVolume(SeqPlayers playerId, float volume) { diff --git a/libultraship/libultraship/Lib/Fast3D/gfx_pc.cpp b/libultraship/libultraship/Lib/Fast3D/gfx_pc.cpp index 6738f28fa..98bab7f78 100644 --- a/libultraship/libultraship/Lib/Fast3D/gfx_pc.cpp +++ b/libultraship/libultraship/Lib/Fast3D/gfx_pc.cpp @@ -2710,9 +2710,7 @@ void gfx_init(struct GfxWindowManagerAPI *wapi, struct GfxRenderingAPI *rapi, co //gfx_lookup_or_create_shader_program(precomp_shaders[i]); } - ModInternal::bindHook(GFX_INIT); - ModInternal::initBindHook(0); - ModInternal::callBindHook(0); + ModInternal::ExecuteHooks(); } struct GfxRenderingAPI *gfx_get_current_rendering_api(void) { diff --git a/libultraship/libultraship/SohHooks.cpp b/libultraship/libultraship/SohHooks.cpp index 7ede525d7..c11745e84 100644 --- a/libultraship/libultraship/SohHooks.cpp +++ b/libultraship/libultraship/SohHooks.cpp @@ -5,121 +5,6 @@ #include #include -std::map> listeners; -std::string hookName; -std::map initArgs; -std::map hookArgs; - -/* -############################# - Module: Hook C++ Handle -############################# -*/ - -namespace ModInternal { - - void registerHookListener(HookListener listener) { - listeners[listener.hookName].push_back(listener.callback); - } - - bool handleHook(std::shared_ptr call) { - std::string hookName = std::string(call->name); - for (size_t l = 0; l < listeners[hookName].size(); l++) { - (listeners[hookName][l])(call); - } - return call->cancelled; - } - - void bindHook(std::string name) { - hookName = name; - } - - void initBindHook(int length, ...) { - if (length > 0) { - va_list args; - va_start(args, length); - for (int i = 0; i < length; i++) { - HookParameter currentParam = va_arg(args, struct HookParameter); - initArgs[currentParam.name] = currentParam.parameter; - } - va_end(args); - } - } - - bool callBindHook(int length, ...) { - if (length > 0) { - va_list args; - va_start(args, length); - for (int i = 0; i < length; i++) { - HookParameter currentParam = va_arg(args, struct HookParameter); - hookArgs[currentParam.name] = currentParam.parameter; - } - va_end(args); - } - - HookCall call = { - .name = hookName, - .baseArgs = initArgs, - .hookedArgs = hookArgs - }; - const bool cancelled = handleHook(std::make_shared(call)); - - hookName = ""; - initArgs.clear(); - hookArgs.clear(); - - return cancelled; - } +void ModInternal_ExecuteAudioInitHooks() { + ModInternal::ExecuteHooks(); } - -/* -############################# - Module: Hook C Handle -############################# -*/ - -extern "C" { - - void bind_hook(char* name) { - hookName = std::string(name); - } - - void init_hook(int length, ...) { - if (length > 0) { - va_list args; - va_start(args, length); - for (int i = 0; i < length; i++) { - HookParameter currentParam = va_arg(args, struct HookParameter); - initArgs[currentParam.name] = currentParam.parameter; - } - va_end(args); - } - } - - bool call_hook(int length, ...) { - if (length > 0) { - va_list args; - va_start(args, length); - for (int i = 0; i < length; i++) { - HookParameter currentParam = va_arg(args, struct HookParameter); - hookArgs[currentParam.name] = currentParam.parameter; - } - va_end(args); - } - - HookCall call = { - .name = hookName, - .baseArgs = initArgs, - .hookedArgs = hookArgs - }; - - const bool cancelled = ModInternal::handleHook(std::make_shared(call)); - - hookName = ""; - initArgs.clear(); - hookArgs.clear(); - - return cancelled; - } - -} \ No newline at end of file diff --git a/libultraship/libultraship/SohHooks.h b/libultraship/libultraship/SohHooks.h index 9a41eeb0e..b5dec8159 100644 --- a/libultraship/libultraship/SohHooks.h +++ b/libultraship/libultraship/SohHooks.h @@ -1,80 +1,50 @@ #pragma once -struct HookParameter { - const char* name; - void* parameter; -}; - -#define LOOKUP_TEXTURE "F3D::LookupCacheTexture" -#define GRAYOUT_TEXTURE "Kaleido::GrayOutTexture" -#define INVALIDATE_TEXTURE "GBI::gSPInvalidateTexCache" -#define CONTROLLER_READ "N64::ControllerRead" - -#define AUDIO_INIT "AudioMgr::Init" - -#define LOAD_TEXTURE "ResourceMgr::LoadTexByName" - -#define UPDATE_VOLUME "AudioVolume::Bind" - -#define IMGUI_API_INIT "ImGuiApiInit" -#define IMGUI_API_DRAW "ImGuiApiDraw" - -#define WINDOW_API_INIT "WApiInit" -#define WINDOW_API_HANDLE_EVENTS "WApiHandleEvents" -#define WINDOW_API_START_FRAME "WApiStartFrame" - -// Graphics API Hooks -#define GFX_PRE_START_FRAME "GFXApiPreStartFrame" -#define GFX_POST_START_FRAME "GFXApiPostStartFrame" - -#define GFX_PRE_END_FRAME "GFXApiPreEndFrame" -#define GFX_POST_END_FRAME "GFXApiPostEndFrame" - -#define GFX_ON_REZISE "GFXApiOnResize" -#define GFX_INIT "GFXApiInit" -#define GFX_SHUTDOWN "GFXApiShutdown" - -// End - #ifdef __cplusplus -#define HOOK_PARAMETER(name, ptr) HookParameter({ name, static_cast(ptr) }) -#define BIND_HOOK(name, func) ModInternal::registerHookListener({ name, [this](HookEvent call) { func(call); }}) -#define BIND_PTR(name, type) static_cast(call->baseArgs[name]) -#define BIND_VAR(name, type) *BIND_PTR(name, type) - - #include -#include -#include -#include -struct HookCall { - std::string name; - std::map baseArgs; - std::map hookedArgs; - bool cancelled = false; -}; +#include "UltraController.h" -typedef std::shared_ptr HookEvent; -typedef std::function HookFunc; -struct HookListener { - std::string hookName; - HookFunc callback; - int priority = 0; -}; +#define DEFINE_HOOK(name, type) struct name { typedef std::function fn; } namespace ModInternal { - void registerHookListener(HookListener listener); - void bindHook(std::string name); - void initBindHook(int length, ...); - bool callBindHook(int length, ...); + + template + struct RegisteredHooks { + inline static std::vector functions; + }; + + template + void RegisterHook(typename H::fn h) { + RegisteredHooks::functions.push_back(h); + } + + template + void ExecuteHooks(Args&&... args) { + for (auto& fn : RegisteredHooks::functions) { + fn(std::forward(args)...); + } + } + + DEFINE_HOOK(ControllerRead, void(OSContPad* cont_pad)); + + DEFINE_HOOK(AudioInit, void()); + + DEFINE_HOOK(LoadTexture, void(const char* path, uint8_t** texture)); + + DEFINE_HOOK(GfxInit, void()); + } -#else +#endif -void bind_hook(char* name); -void init_hook(int length, ...); -bool call_hook(int length, ...); +#ifdef __cplusplus +extern "C" { +#endif -#endif \ No newline at end of file +void ModInternal_ExecuteAudioInitHooks(); + +#ifdef __cplusplus +} +#endif diff --git a/libultraship/libultraship/SohImGuiImpl.cpp b/libultraship/libultraship/SohImGuiImpl.cpp index dd345124b..ab967cbca 100644 --- a/libultraship/libultraship/SohImGuiImpl.cpp +++ b/libultraship/libultraship/SohImGuiImpl.cpp @@ -383,8 +383,7 @@ namespace SohImGui { ImGuiWMInit(); ImGuiBackendInit(); - ModInternal::registerHookListener({ GFX_INIT, [](const HookEvent ev) { - + ModInternal::RegisterHook([] { if (GlobalCtx2::GetInstance()->GetWindow()->IsFullscreen()) ShowCursor(CVar_GetS32("gOpenMenuBar", 0), Dialogues::dLoadSettings); @@ -399,7 +398,7 @@ namespace SohImGui { LoadTexture("C-Right", "assets/ship_of_harkinian/buttons/CRight.png"); LoadTexture("C-Up", "assets/ship_of_harkinian/buttons/CUp.png"); LoadTexture("C-Down", "assets/ship_of_harkinian/buttons/CDown.png"); - } }); + }); for (const auto& [i, controllers] : Ship::Window::Controllers) { @@ -408,9 +407,9 @@ namespace SohImGui { needs_save = true; } - ModInternal::registerHookListener({ CONTROLLER_READ, [](const HookEvent ev) { - pads = static_cast(ev->baseArgs["cont_pad"]); - } }); + ModInternal::RegisterHook([](OSContPad* cont_pad) { + pads = cont_pad; + }); Game::InitSettings(); } diff --git a/libultraship/libultraship/TextureMod.cpp b/libultraship/libultraship/TextureMod.cpp index b44987a62..87f17ed2c 100644 --- a/libultraship/libultraship/TextureMod.cpp +++ b/libultraship/libultraship/TextureMod.cpp @@ -1,5 +1,7 @@ #include "TextureMod.h" +#if 0 + #include #include #include @@ -121,4 +123,6 @@ namespace Ship { TexturePool.clear(); LoadedOTRS.clear(); } -} \ No newline at end of file +} + +#endif diff --git a/libultraship/libultraship/TextureMod.h b/libultraship/libultraship/TextureMod.h index 8dac99387..5f41873ca 100644 --- a/libultraship/libultraship/TextureMod.h +++ b/libultraship/libultraship/TextureMod.h @@ -3,6 +3,8 @@ #include #include "Lib/Fast3D/gfx_pc.h" +#if 0 + namespace Ship { enum TextureMod { GRAYSCALE, @@ -48,4 +50,6 @@ namespace Ship { data[x + 2] = gray; } } -} \ No newline at end of file +} + +#endif diff --git a/libultraship/libultraship/Window.cpp b/libultraship/libultraship/Window.cpp index bbd906c76..5c2cf3cae 100644 --- a/libultraship/libultraship/Window.cpp +++ b/libultraship/libultraship/Window.cpp @@ -112,11 +112,7 @@ extern "C" { } } - ModInternal::bindHook(CONTROLLER_READ); - ModInternal::initBindHook(1, - HookParameter({ .name = "cont_pad", .parameter = (void*)pad }) - ); - ModInternal::callBindHook(0); + ModInternal::ExecuteHooks(pad); } const char* ResourceMgr_GetNameByCRC(uint64_t crc) { @@ -168,12 +164,7 @@ extern "C" { if (hashStr != nullptr) { const auto res = static_cast(Ship::GlobalCtx2::GetInstance()->GetResourceManager()->LoadResource(hashStr->c_str()).get()); - ModInternal::bindHook(LOAD_TEXTURE); - ModInternal::initBindHook(2, - HookParameter({.name = "path", .parameter = (void*)hashStr->c_str() }), - HookParameter({.name = "texture", .parameter = static_cast(&res->imageData) }) - ); - ModInternal::callBindHook(0); + ModInternal::ExecuteHooks(hashStr->c_str(), &res->imageData); return reinterpret_cast(res->imageData); } else { @@ -200,12 +191,7 @@ extern "C" { char* ResourceMgr_LoadTexByName(char* texPath) { const auto res = static_cast(Ship::GlobalCtx2::GetInstance()->GetResourceManager()->LoadResource(texPath).get()); - ModInternal::bindHook(LOAD_TEXTURE); - ModInternal::initBindHook(2, - HookParameter({ .name = "path", .parameter = (void*)texPath }), - HookParameter({ .name = "texture", .parameter = static_cast(&res->imageData) }) - ); - ModInternal::callBindHook(0); + ModInternal::ExecuteHooks(texPath, &res->imageData); return (char*)res->imageData; } diff --git a/soh/src/code/audioMgr.c b/soh/src/code/audioMgr.c index 5b46ad8eb..e8c7be032 100644 --- a/soh/src/code/audioMgr.c +++ b/soh/src/code/audioMgr.c @@ -108,9 +108,7 @@ void AudioMgr_Init(AudioMgr* audioMgr, void* stack, OSPri pri, OSId id, SchedCon AudioLoad_SetDmaHandler(DmaMgr_DmaHandler); Audio_InitSound(); osSendMesg(&audioMgr->unk_C8, NULL, OS_MESG_BLOCK); - bind_hook(AUDIO_INIT); - init_hook(0); - call_hook(0); + ModInternal_ExecuteAudioInitHooks(); // Removed due to crash //IrqMgr_AddClient(audioMgr->irqMgr, &irqClient, &audioMgr->unk_74); hasInitialized = true; diff --git a/soh/src/overlays/misc/ovl_kaleido_scope/z_kaleido_scope_PAL.c b/soh/src/overlays/misc/ovl_kaleido_scope/z_kaleido_scope_PAL.c index 86201719a..d9f038059 100644 --- a/soh/src/overlays/misc/ovl_kaleido_scope/z_kaleido_scope_PAL.c +++ b/soh/src/overlays/misc/ovl_kaleido_scope/z_kaleido_scope_PAL.c @@ -2948,14 +2948,6 @@ void KaleidoScope_GrayOutTextureRGBA32(u32* texture, u16 pixelCount) { u16 gray; u16 i; - bind_hook( GRAYOUT_TEXTURE); - init_hook(2, - (struct HookParameter){ .name = "texture", .parameter = &texture }, - (struct HookParameter){ .name = "pixelCount", .parameter = &pixelCount } - ); - if (!call_hook(0)) - return; - texture = ResourceMgr_LoadTexByName(texture); for (i = 0; i < pixelCount; i++) {