Redesign hooks mechanism (#352)

* Redesign hooks mechanism

* Use reference instead of copy
This commit is contained in:
Emill 2022-05-21 19:22:25 +02:00 committed by GitHub
parent b359d642f4
commit e56af6a7a3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 62 additions and 228 deletions

View File

@ -54,14 +54,12 @@ namespace Game {
}
void InitSettings() {
ModInternal::registerHookListener({ AUDIO_INIT, [](HookEvent ev) {
UpdateAudio();
}});
ModInternal::registerHookListener({ GFX_INIT, [](HookEvent ev) {
ModInternal::RegisterHook<ModInternal::AudioInit>(UpdateAudio);
ModInternal::RegisterHook<ModInternal::GfxInit>([] {
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) {

View File

@ -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<ModInternal::GfxInit>();
}
struct GfxRenderingAPI *gfx_get_current_rendering_api(void) {

View File

@ -5,121 +5,6 @@
#include <stdarg.h>
#include <iostream>
std::map<std::string, std::vector<HookFunc>> listeners;
std::string hookName;
std::map<std::string, void*> initArgs;
std::map<std::string, void*> hookArgs;
/*
#############################
Module: Hook C++ Handle
#############################
*/
namespace ModInternal {
void registerHookListener(HookListener listener) {
listeners[listener.hookName].push_back(listener.callback);
}
bool handleHook(std::shared_ptr<HookCall> 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<HookCall>(call));
hookName = "";
initArgs.clear();
hookArgs.clear();
return cancelled;
}
void ModInternal_ExecuteAudioInitHooks() {
ModInternal::ExecuteHooks<ModInternal::AudioInit>();
}
/*
#############################
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<HookCall>(call));
hookName = "";
initArgs.clear();
hookArgs.clear();
return cancelled;
}
}

View File

@ -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<void*>(ptr) })
#define BIND_HOOK(name, func) ModInternal::registerHookListener({ name, [this](HookEvent call) { func(call); }})
#define BIND_PTR(name, type) static_cast<type>(call->baseArgs[name])
#define BIND_VAR(name, type) *BIND_PTR(name, type)
#include <functional>
#include <string>
#include <map>
#include <memory>
struct HookCall {
std::string name;
std::map<std::string, void*> baseArgs;
std::map<std::string, void*> hookedArgs;
bool cancelled = false;
};
#include "UltraController.h"
typedef std::shared_ptr<HookCall> HookEvent;
typedef std::function<void(HookEvent)> HookFunc;
struct HookListener {
std::string hookName;
HookFunc callback;
int priority = 0;
};
#define DEFINE_HOOK(name, type) struct name { typedef std::function<type> fn; }
namespace ModInternal {
void registerHookListener(HookListener listener);
void bindHook(std::string name);
void initBindHook(int length, ...);
bool callBindHook(int length, ...);
template <typename H>
struct RegisteredHooks {
inline static std::vector<typename H::fn> functions;
};
template <typename H>
void RegisterHook(typename H::fn h) {
RegisteredHooks<H>::functions.push_back(h);
}
template <typename H, typename... Args>
void ExecuteHooks(Args&&... args) {
for (auto& fn : RegisteredHooks<H>::functions) {
fn(std::forward<Args>(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
void ModInternal_ExecuteAudioInitHooks();
#ifdef __cplusplus
}
#endif

View File

@ -383,8 +383,7 @@ namespace SohImGui {
ImGuiWMInit();
ImGuiBackendInit();
ModInternal::registerHookListener({ GFX_INIT, [](const HookEvent ev) {
ModInternal::RegisterHook<ModInternal::GfxInit>([] {
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<OSContPad*>(ev->baseArgs["cont_pad"]);
} });
ModInternal::RegisterHook<ModInternal::ControllerRead>([](OSContPad* cont_pad) {
pads = cont_pad;
});
Game::InitSettings();
}

View File

@ -1,5 +1,7 @@
#include "TextureMod.h"
#if 0
#include <map>
#include <string>
#include <iostream>
@ -121,4 +123,6 @@ namespace Ship {
TexturePool.clear();
LoadedOTRS.clear();
}
}
}
#endif

View File

@ -3,6 +3,8 @@
#include <PR/ultra64/gbi.h>
#include "Lib/Fast3D/gfx_pc.h"
#if 0
namespace Ship {
enum TextureMod {
GRAYSCALE,
@ -48,4 +50,6 @@ namespace Ship {
data[x + 2] = gray;
}
}
}
}
#endif

View File

@ -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<ModInternal::ControllerRead>(pad);
}
const char* ResourceMgr_GetNameByCRC(uint64_t crc) {
@ -168,12 +164,7 @@ extern "C" {
if (hashStr != nullptr) {
const auto res = static_cast<Ship::Texture*>(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<void*>(&res->imageData) })
);
ModInternal::callBindHook(0);
ModInternal::ExecuteHooks<ModInternal::LoadTexture>(hashStr->c_str(), &res->imageData);
return reinterpret_cast<char*>(res->imageData);
} else {
@ -200,12 +191,7 @@ extern "C" {
char* ResourceMgr_LoadTexByName(char* texPath) {
const auto res = static_cast<Ship::Texture*>(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<void*>(&res->imageData) })
);
ModInternal::callBindHook(0);
ModInternal::ExecuteHooks<ModInternal::LoadTexture>(texPath, &res->imageData);
return (char*)res->imageData;
}

View File

@ -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;

View File

@ -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++) {