Skip to content
This repository was archived by the owner on Sep 17, 2024. It is now read-only.

Commit 69f10cd

Browse files
committed
Better startup hooking logic
1 parent 9326fe0 commit 69f10cd

File tree

3 files changed

+24
-14
lines changed

3 files changed

+24
-14
lines changed

source/wrapper_generic/EOSOverlay.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ uint32_t __fastcall HookedEOS_Overlay_Stub(void *a1, void *a2, void *a3)
2323
return 0xFFFFFFFF;
2424
}
2525

26-
void TryInterceptEOSFunction(void *ModuleHandle, const void *FunctionName, void **FunctionPointer)
26+
bool TryInterceptEOSFunction(void *ModuleHandle, const void *FunctionName, void **FunctionPointer)
2727
{
2828
if (!FunctionName || !*FunctionPointer || reinterpret_cast<uintptr_t>(FunctionName) < 0x10000)
29-
return;
29+
return false;
3030

3131
// Each export from EOSOVH-Win64-Shipping.dll requires interception. They're all guilty of calling the
3232
// overlay initialization function, which causes other hooks to be removed.
@@ -36,6 +36,8 @@ void TryInterceptEOSFunction(void *ModuleHandle, const void *FunctionName, void
3636
continue;
3737

3838
*FunctionPointer = &HookedEOS_Overlay_Stub;
39-
break;
39+
return true;
4040
}
41+
42+
return false;
4143
}

source/wrapper_generic/dllmain.cpp

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,15 @@
55
// sl.common.dll loads _nvngx.dll <- we are here
66
// _nvngx.dll loads nvngx_dlssg.dll <- intercept this stage
77
//
8-
constinit const wchar_t *TargetLibrariesToHook[] = { L"sl.interposer.dll", L"sl.common.dll", L"sl.dlss_g.dll", L"_nvngx.dll", L"EOSSDK-Win64-Shipping.dll" };
8+
std::vector<const wchar_t *> TargetLibrariesToHook = { L"sl.interposer.dll", L"sl.common.dll", L"sl.dlss_g.dll", L"_nvngx.dll" };
9+
constinit const wchar_t *TargetEGSOverlayDll = L"EOSSDK-Win64-Shipping.dll";
910
constinit const wchar_t *TargetImplementationDll = L"nvngx_dlssg.dll";
1011
constinit const wchar_t *RelplacementImplementationDll = L"dlssg_to_fsr3_amd_is_better.dll";
1112

1213
bool EnableAggressiveHooking;
1314

14-
void TryInterceptNvAPIFunction(void *ModuleHandle, const void *FunctionName, void **FunctionPointer);
15-
void TryInterceptEOSFunction(void *ModuleHandle, const void *FunctionName, void **FunctionPointer);
15+
bool TryInterceptNvAPIFunction(void *ModuleHandle, const void *FunctionName, void **FunctionPointer);
16+
bool TryInterceptEOSFunction(void *ModuleHandle, const void *FunctionName, void **FunctionPointer);
1617
bool PatchImportsForModule(const wchar_t *Path, HMODULE ModuleHandle);
1718

1819
void *LoadImplementationDll()
@@ -117,8 +118,8 @@ bool PatchImportsForModule(const wchar_t *Path, HMODULE ModuleHandle)
117118
std::wstring_view libFileName(Path);
118119

119120
const bool isMatch = std::any_of(
120-
std::begin(TargetLibrariesToHook),
121-
std::end(TargetLibrariesToHook),
121+
TargetLibrariesToHook.begin(),
122+
TargetLibrariesToHook.end(),
122123
[&](const wchar_t *Target)
123124
{
124125
return libFileName.ends_with(Target);
@@ -150,19 +151,22 @@ BOOL WINAPI DllMain(HINSTANCE hInstDLL, DWORD fdwReason, LPVOID lpvReserved)
150151
OutputDebugStringW(L"DEBUG: Shim built with commit ID " BUILD_GIT_COMMIT_HASH "\n");
151152

152153
if (EnableAggressiveHooking)
154+
{
155+
TargetLibrariesToHook.push_back(TargetEGSOverlayDll);
153156
LoadLibraryW(L"sl.interposer.dll");
157+
}
154158

155159
// We probably loaded after sl.interposer.dll and sl.common.dll. Try patching them up front.
156160
bool anyPatched = std::count_if(
157-
std::begin(TargetLibrariesToHook),
158-
std::end(TargetLibrariesToHook),
161+
TargetLibrariesToHook.begin(),
162+
TargetLibrariesToHook.end(),
159163
[](const wchar_t *Target)
160164
{
161-
return PatchImportsForModule(Target, GetModuleHandleW(Target));
165+
return PatchImportsForModule(Target, GetModuleHandleW(Target)) && _wcsicmp(Target, TargetEGSOverlayDll) != 0;
162166
}) > 0;
163167

164168
// If zero Streamline dlls were loaded we'll have to hook the game's LoadLibrary calls and wait
165-
if (!anyPatched)
169+
if (!anyPatched && EnableAggressiveHooking)
166170
anyPatched = PatchImportsForModule(TargetLibrariesToHook[0], GetModuleHandleW(nullptr));
167171

168172
// Hooks can't be removed once they're in place. Pin this DLL in memory.

source/wrapper_generic/nvapi.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,18 @@ void *__stdcall HookedNvAPI_QueryInterface(NV_INTERFACE InterfaceId)
5252
return result;
5353
}
5454

55-
void TryInterceptNvAPIFunction(void *ModuleHandle, const void *FunctionName, void **FunctionPointer)
55+
bool TryInterceptNvAPIFunction(void *ModuleHandle, const void *FunctionName, void **FunctionPointer)
5656
{
5757
if (!FunctionName || !*FunctionPointer || reinterpret_cast<uintptr_t>(FunctionName) < 0x10000)
58-
return;
58+
return false;
5959

6060
if (_stricmp(static_cast<const char *>(FunctionName), "nvapi_QueryInterface") == 0)
6161
{
6262
OriginalNvAPI_QueryInterface = static_cast<PfnNvAPI_QueryInterface>(*FunctionPointer);
6363
*FunctionPointer = &HookedNvAPI_QueryInterface;
64+
65+
return true;
6466
}
67+
68+
return false;
6569
}

0 commit comments

Comments
 (0)