Skip to content

Commit

Permalink
More permissive function wrapper logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Norbyte committed Jun 8, 2024
1 parent 09cdb40 commit a436be8
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 14 deletions.
5 changes: 5 additions & 0 deletions BG3Extender/Extender/Client/IMGUI/DX11.inl
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ public:

void EnableHooks() override
{
if (CreateDeviceHook_.IsWrapped()) return;

DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());
auto dx11Lib = LoadLibraryW(L"d3d11.dll");
Expand All @@ -88,9 +90,12 @@ public:

void DisableHooks() override
{
if (!CreateDeviceHook_.IsWrapped()) return;

DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());
CreateDeviceHook_.Unwrap();
CreateDxgiFactoryHook_.Unwrap();
DetourTransactionCommit();
}

Expand Down
4 changes: 4 additions & 0 deletions BG3Extender/Extender/Client/IMGUI/Vulkan.inl
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ public:

void EnableHooks() override
{
if (CreateInstanceHook_.IsWrapped()) return;

DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());
auto createInstance = vkGetInstanceProcAddr(nullptr, "vkCreateInstance");
Expand All @@ -72,6 +74,8 @@ public:

void DisableHooks() override
{
if (!CreateInstanceHook_.IsWrapped()) return;

DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());
CreateInstanceHook_.Unwrap();
Expand Down
35 changes: 21 additions & 14 deletions CoreLib/Wrappers.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ namespace bg3se {
FARPROC ExportProc = GetProcAddress(Module, ProcName);
if (ExportProc == NULL) {
ERR("Could not locate export '%s'", ProcName);
return;
}

Wrap(ExportProc, NewFunction);
Expand All @@ -49,7 +50,8 @@ namespace bg3se {
void Wrap(void * Function, FuncType NewFunction)
{
if (IsWrapped()) {
throw std::runtime_error("Tried to wrap function multiple times");
ERR("[%s] Tried to wrap function multiple times", typeid(*this).name());
return;
}

OriginalFunc = Function;
Expand All @@ -58,7 +60,7 @@ namespace bg3se {
gRegisteredTrampolines.insert(NewFunction);
auto status = DetourAttachEx((PVOID *)&TrampolineFunc, (PVOID)NewFunc, (PDETOUR_TRAMPOLINE *)&FuncTrampoline, NULL, NULL);
if (status != NO_ERROR) {
ERR("Detour attach failed on %p", Function);
ERR("[%s] Detour attach failed on %p", typeid(*this).name(), Function);
OriginalFunc = nullptr;
TrampolineFunc = nullptr;
NewFunc = nullptr;
Expand All @@ -71,7 +73,7 @@ namespace bg3se {
if (IsWrapped()) {
DWORD result = DetourDetach((PVOID *)&TrampolineFunc, (PVOID)NewFunc);
if (result != NO_ERROR) {
ERR("DetourDetach failed on %p", OriginalFunc);
ERR("[%s] DetourDetach failed on %p", typeid(*this).name(), OriginalFunc);
}

OriginalFunc = nullptr;
Expand Down Expand Up @@ -160,23 +162,23 @@ namespace bg3se {

void Wrap(HMODULE Module, char const * ProcName)
{
this->wrapped_.Wrap(Module, ProcName, &CallToTrampoline);

if (gHook != nullptr) {
Fail("Hook already registered");
ERR("[%s] Hook already registered", typeid(*this).name());
return;
}

this->wrapped_.Wrap(Module, ProcName, &CallToTrampoline);
gHook = this;
}

void Wrap(void * Function)
{
this->wrapped_.Wrap(Function, &CallToTrampoline);

if (gHook != nullptr) {
Fail("Hook already registered");
ERR("[%s] Hook already registered", typeid(*this).name());
}

this->wrapped_.Wrap(Function, &CallToTrampoline);

gHook = this;
}

Expand All @@ -194,7 +196,8 @@ namespace bg3se {
void SetWrapper(NoContextHookFuncType* wrapper)
{
if (hook_ != nullptr) {
throw std::runtime_error("Function already wrapped");
ERR("[%s] Function already wrapped", typeid(*this).name());
return;
}

gRegisteredTrampolines.insert(ResolveRealFunctionAddress(&NoContextHook));
Expand All @@ -208,7 +211,8 @@ namespace bg3se {
void SetWrapper(R (* wrapper)(TContext*, BaseFuncType*, Params...), TContext* context)
{
if (hook_ != nullptr) {
throw std::runtime_error("Function already wrapped");
ERR("[%s] Function already wrapped", typeid(*this).name());
return;
}

gRegisteredTrampolines.insert(ResolveRealFunctionAddress(wrapper));
Expand All @@ -221,7 +225,8 @@ namespace bg3se {
void SetWrapper(R (TContext::* wrapper)(BaseFuncType*, Params...), TContext* context)
{
if (hook_ != nullptr) {
throw std::runtime_error("Function already wrapped");
ERR("[%s] Function already wrapped", typeid(*this).name());
return;
}

auto fun = MethodPtrHelpers<TContext, R (BaseFuncType*, Params...)>::ToFunction(wrapper);
Expand All @@ -235,7 +240,8 @@ namespace bg3se {
void SetPreHook(void (TContext::* wrapper)(Params...), TContext* context)
{
if (hook_ != nullptr) {
throw std::runtime_error("Function already wrapped");
ERR("[%s] Function already wrapped", typeid(*this).name());
return;
}

gRegisteredTrampolines.insert(ResolveRealFunctionAddress(&StaticPreHook));
Expand All @@ -248,7 +254,8 @@ namespace bg3se {
void SetPostHook(PostHookCallbackSignature<TContext, R, Params...>::Type wrapper, TContext* context)
{
if (hook_ != nullptr) {
throw std::runtime_error("Function already wrapped");
ERR("[%s] Function already wrapped", typeid(*this).name());
return;
}

gRegisteredTrampolines.insert(ResolveRealFunctionAddress(&StaticPostHook));
Expand Down

0 comments on commit a436be8

Please sign in to comment.