From 0b97121fb4fcbdc26e028bdc35702c53fc85401c Mon Sep 17 00:00:00 2001 From: FriendlyFire Date: Wed, 21 Sep 2022 15:24:41 -0400 Subject: [PATCH] Seems to be finally functional, in debugging and normal modes. --- d3d11/dllmain.cpp | 78 +++++++++++++++++++++++++++++++------ d3d9/d3d9.vcxproj | 12 +----- dxgi/dllmain.cpp | 55 ++++++++++++++++++-------- loader_core.sln | 8 +++- loader_core/deffile.def | 12 ------ loader_core/loader_core.cpp | 26 +++++-------- 6 files changed, 123 insertions(+), 68 deletions(-) diff --git a/d3d11/dllmain.cpp b/d3d11/dllmain.cpp index 02d6130..e39b4a1 100644 --- a/d3d11/dllmain.cpp +++ b/d3d11/dllmain.cpp @@ -3,6 +3,29 @@ #include #include +using D3D11CoreCreateDevice_t = HRESULT (WINAPI *)( + IDXGIFactory * pFactory, + IDXGIAdapter * pAdapter, + UINT Flags, + const D3D_FEATURE_LEVEL * pFeatureLevels, + UINT FeatureLevels, + ID3D11Device * *ppDevice); +D3D11CoreCreateDevice_t RealD3D11CoreCreateDevice = nullptr; + +using D3D11CoreCreateLayeredDevice_t = HRESULT (WINAPI *)( + const void* unknown0, + DWORD unknown1, + const void* unknown2, + REFIID riid, + void** ppvObj); +D3D11CoreCreateLayeredDevice_t RealD3D11CoreCreateLayeredDevice = nullptr; + +using D3D11CoreGetLayeredDeviceSize_t = SIZE_T (WINAPI *)(const void* unknown0, DWORD unknown1); +D3D11CoreGetLayeredDeviceSize_t RealD3D11CoreGetLayeredDeviceSize = nullptr; + +using D3D11CoreRegisterLayers_t = HRESULT (WINAPI *)(const void* unknown0, DWORD unknown1); +D3D11CoreRegisterLayers_t RealD3D11CoreRegisterLayers = nullptr; + #define DX11_CREATE_PLIST pAdapter, DriverType, Software, Flags, pFeatureLevels, FeatureLevels, SDKVersion, pSwapChainDesc, ppSwapChain, ppDevice, pFeatureLevel, ppImmediateContext typedef HRESULT(WINAPI* D3D11CreateDeviceAndSwapChainFunc)(IDXGIAdapter* pAdapter, D3D_DRIVER_TYPE DriverType, HMODULE Software, UINT Flags, const D3D_FEATURE_LEVEL* pFeatureLevels, UINT FeatureLevels, UINT SDKVersion, const DXGI_SWAP_CHAIN_DESC* pSwapChainDesc, IDXGISwapChain** ppSwapChain, ID3D11Device** ppDevice, D3D_FEATURE_LEVEL* pFeatureLevel, ID3D11DeviceContext** ppImmediateContext); @@ -39,9 +62,21 @@ HMODULE GetD3D11Module() GetSystemDirectory(infoBuf, 4096); lstrcatW(infoBuf, L"\\d3d11.dll"); + auto existingModule = GetModuleHandle(infoBuf); + if(existingModule) + return existingModule; + return LoadLibrary(infoBuf); } +void FreeD3D11Module() { + wchar_t infoBuf[4096]; + GetSystemDirectory(infoBuf, 4096); + lstrcatW(infoBuf, L"\\d3d11.dll"); + + FreeLibrary(GetModuleHandle(infoBuf)); +} + FARPROC GetD3D11Function(LPCSTR name) { static HMODULE hmod = GetD3D11Module(); @@ -50,6 +85,9 @@ FARPROC GetD3D11Function(LPCSTR name) #define GET_D3D11_FUNC(name) (decltype(name)*)GetD3D11Function(#name) +bool ShouldTryLoading = true; +void LoadAllFunctions(); + extern "C" HRESULT WINAPI D3D11CoreCreateDevice( IDXGIFactory * pFactory, IDXGIAdapter * pAdapter, @@ -58,8 +96,9 @@ extern "C" HRESULT WINAPI D3D11CoreCreateDevice( UINT FeatureLevels, ID3D11Device * *ppDevice) { - static auto func = GET_D3D11_FUNC(D3D11CoreCreateDevice); - return func(pFactory, pAdapter, Flags, pFeatureLevels, FeatureLevels, ppDevice); + if(ShouldTryLoading) + LoadAllFunctions(); + return RealD3D11CoreCreateDevice(pFactory, pAdapter, Flags, pFeatureLevels, FeatureLevels, ppDevice); } extern "C" HRESULT WINAPI D3D11CoreCreateLayeredDevice( @@ -69,27 +108,44 @@ extern "C" HRESULT WINAPI D3D11CoreCreateLayeredDevice( REFIID riid, void** ppvObj) { - static auto func = GET_D3D11_FUNC(D3D11CoreCreateLayeredDevice); - return func(unknown0, unknown1, unknown2, riid, ppvObj); + if(ShouldTryLoading) + LoadAllFunctions(); + return RealD3D11CoreCreateLayeredDevice(unknown0, unknown1, unknown2, riid, ppvObj); } extern "C" SIZE_T WINAPI D3D11CoreGetLayeredDeviceSize(const void* unknown0, DWORD unknown1) { - static auto func = GET_D3D11_FUNC(D3D11CoreGetLayeredDeviceSize); - return func(unknown0, unknown1); + if(ShouldTryLoading) + LoadAllFunctions(); + return RealD3D11CoreGetLayeredDeviceSize(unknown0, unknown1); } extern "C" HRESULT WINAPI D3D11CoreRegisterLayers(const void* unknown0, DWORD unknown1) { - static auto func = GET_D3D11_FUNC(D3D11CoreRegisterLayers); - return func(unknown0, unknown1); + if(ShouldTryLoading) + LoadAllFunctions(); + return RealD3D11CoreRegisterLayers(unknown0, unknown1); +} + +void LoadAllFunctions() { + RealD3D11CoreCreateDevice = GET_D3D11_FUNC(D3D11CoreCreateDevice); + RealD3D11CoreCreateLayeredDevice = GET_D3D11_FUNC(D3D11CoreCreateLayeredDevice); + RealD3D11CoreGetLayeredDeviceSize = GET_D3D11_FUNC(D3D11CoreGetLayeredDeviceSize); + RealD3D11CoreRegisterLayers = GET_D3D11_FUNC(D3D11CoreRegisterLayers); + + ShouldTryLoading = false; } BOOL APIENTRY DllMain( HMODULE hModule, - DWORD ul_reason_for_call, + DWORD fdwReason, LPVOID lpReserved ) { - return TRUE; -} + if(fdwReason == DLL_PROCESS_DETACH) + { + ShouldTryLoading = true; + FreeD3D11Module(); + } + return true; +} diff --git a/d3d9/d3d9.vcxproj b/d3d9/d3d9.vcxproj index c041299..324aaea 100644 --- a/d3d9/d3d9.vcxproj +++ b/d3d9/d3d9.vcxproj @@ -105,11 +105,7 @@ false deffile.def - - IF DEFINED GW2_INSTALL_DIR ( -xcopy "$(TargetPath)" "$(GW2_INSTALL_DIR)\bin64\" /Y -) - + @@ -142,11 +138,7 @@ xcopy "$(TargetPath)" "$(GW2_INSTALL_DIR)\bin64\" /Y false deffile.def - - IF DEFINED GW2_INSTALL_DIR ( -xcopy "$(TargetPath)" "$(GW2_INSTALL_DIR)\bin64\" /Y -) - + diff --git a/dxgi/dllmain.cpp b/dxgi/dllmain.cpp index 8834e83..8c5daea 100644 --- a/dxgi/dllmain.cpp +++ b/dxgi/dllmain.cpp @@ -1,9 +1,10 @@ #define WIN32_LEAN_AND_MEAN #include -using Compat_t = void*(WINAPI *)(); -Compat_t RealCompatValue = nullptr; -Compat_t RealCompatString = nullptr; +using CompatValue_t = BOOL(WINAPI *)(LPCSTR szName, UINT64 *pValue); +using CompatString_t = BOOL(WINAPI *)(LPCSTR szName, ULONG *pSize, LPSTR lpData, bool Flag); +CompatValue_t RealCompatValue = nullptr; +CompatString_t RealCompatString = nullptr; typedef HRESULT(WINAPI* DXGIFactoryCreate0)(REFIID riid, void** ppFactory); typedef HRESULT(WINAPI* DXGIFactoryCreate1)(REFIID riid, void** ppFactory); @@ -53,9 +54,21 @@ HMODULE GetDXGIModule() GetSystemDirectory(infoBuf, 4096); lstrcatW(infoBuf, L"\\dxgi.dll"); + auto existingModule = GetModuleHandle(infoBuf); + if(existingModule) + return existingModule; + return LoadLibrary(infoBuf); } +void FreeDXGIModule() { + wchar_t infoBuf[4096]; + GetSystemDirectory(infoBuf, 4096); + lstrcatW(infoBuf, L"\\dxgi.dll"); + + FreeLibrary(GetModuleHandle(infoBuf)); +} + FARPROC GetDXGIFunction(LPCSTR name) { static HMODULE hmod = GetDXGIModule(); @@ -64,14 +77,28 @@ FARPROC GetDXGIFunction(LPCSTR name) #define GET_DXGI_FUNC(name) (decltype(name)*)GetDXGIFunction(#name) -extern "C" void* WINAPI CompatValue() +bool ShouldTryLoading = true; +void LoadAllFunctions(); + +extern "C" BOOL WINAPI CompatValue(LPCSTR szName, UINT64 *pValue) { - return RealCompatValue(); + if(ShouldTryLoading) + LoadAllFunctions(); + return RealCompatValue(szName, pValue); } -extern "C" void* WINAPI CompatString() +extern "C" BOOL WINAPI CompatString(LPCSTR szName, ULONG *pSize, LPSTR lpData, bool Flag) { - return RealCompatString(); + if(ShouldTryLoading) + LoadAllFunctions(); + return RealCompatString(szName, pSize, lpData, Flag); +} + +void LoadAllFunctions() { + RealCompatValue = GET_DXGI_FUNC(CompatValue); + RealCompatString = GET_DXGI_FUNC(CompatString); + + ShouldTryLoading = false; } BOOL APIENTRY DllMain( HMODULE hModule, @@ -79,16 +106,12 @@ BOOL APIENTRY DllMain( HMODULE hModule, LPVOID lpReserved ) { - switch (fdwReason) + if(fdwReason == DLL_PROCESS_DETACH) { - case DLL_PROCESS_ATTACH: - RealCompatValue = GET_DXGI_FUNC(CompatValue); - RealCompatString = GET_DXGI_FUNC(CompatString); - break; - case DLL_PROCESS_DETACH: - RealCompatValue = nullptr; - RealCompatString = nullptr; - break; + ShouldTryLoading = true; + FreeDXGIModule(); } + + return true; } diff --git a/loader_core.sln b/loader_core.sln index c6883b3..a04a2ab 100644 --- a/loader_core.sln +++ b/loader_core.sln @@ -1,9 +1,13 @@  Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio Version 16 -VisualStudioVersion = 16.0.29728.190 +# Visual Studio Version 17 +VisualStudioVersion = 17.3.32901.215 MinimumVisualStudioVersion = 10.0.40219.1 Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "loader_core", "loader_core\loader_core.vcxproj", "{024CCE36-7DE3-4ACC-80E6-EF25F9894C11}" + ProjectSection(ProjectDependencies) = postProject + {5A73591F-E0F2-400D-9C93-146CD068E18A} = {5A73591F-E0F2-400D-9C93-146CD068E18A} + {C5DD718A-8B5E-493D-97F3-A39F2812E782} = {C5DD718A-8B5E-493D-97F3-A39F2812E782} + EndProjectSection EndProject Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "fake_client", "fake_client\fake_client.vcxproj", "{33FC0BB7-E821-4B68-BB81-B16BD7F76C84}" ProjectSection(ProjectDependencies) = postProject diff --git a/loader_core/deffile.def b/loader_core/deffile.def index 2cdb48e..2a2ca08 100644 --- a/loader_core/deffile.def +++ b/loader_core/deffile.def @@ -1,18 +1,6 @@ LIBRARY addonLoader.dll EXPORTS -; d3d9 - - Direct3DCreate9 @30 - Direct3DCreate9Ex - D3DPERF_BeginEvent - D3DPERF_EndEvent - D3DPERF_SetMarker - D3DPERF_SetRegion - D3DPERF_QueryRepeatFrame - D3DPERF_SetOptions - D3DPERF_GetStatus - ; d3d11 D3D11CreateDevice diff --git a/loader_core/loader_core.cpp b/loader_core/loader_core.cpp index 81818b6..7311670 100644 --- a/loader_core/loader_core.cpp +++ b/loader_core/loader_core.cpp @@ -5,11 +5,11 @@ loader_core loader_core::instance; loader_core::loader_core() { - state = LDR_DLL_LOADED; + state = LDR_DLL_LOADED; } loader_core::~loader_core() -{ +{ } loader_state loader_core::GetCurrentState() @@ -83,9 +83,9 @@ void loader_core::log_text_fmt(gw2al_log_level level, const wchar_t * source, co va_list arg; va_start(arg, fmt); - + vswprintf(buf, 4096, fmt, arg); - + va_end(arg); gw2al_core__log_text(level, (wchar_t*)source, buf); @@ -93,10 +93,6 @@ void loader_core::log_text_fmt(gw2al_log_level level, const wchar_t * source, co void loader_core::innerInit() { - HMODULE directDraw = GetModuleHandleA("ddraw.dll"); - if(directDraw) - return; - if (SwitchState(LDR_ADDON_LOAD)) { bool isFirstLoad = gw2al_core__init(); @@ -116,9 +112,7 @@ IDirect3D9* loader_core::RouteD3DCreate(UINT sdkVer) IDirect3D9* ret = NULL; - HMODULE directDraw = GetModuleHandleA("ddraw.dll"); - - if (!directDraw && d3d9_create_hook) + if (d3d9_create_hook) { LOG_DEBUG(L"core", L"Calling D3D9Create, hook = 0x%016llX", d3d9_create_hook); ret = d3d9_create_hook(); @@ -136,7 +130,7 @@ IDirect3D9* loader_core::RouteD3DCreate(UINT sdkVer) typedef IDirect3D9* (WINAPI* Direct3DCreate9Func)(UINT sdkver); Direct3DCreate9Func origDirect3DCreate9 = (Direct3DCreate9Func)GetProcAddress(sys_d3d9, "Direct3DCreate9"); - ret = origDirect3DCreate9(sdkVer); + ret = origDirect3DCreate9(sdkVer); } LOG_DEBUG(L"core", L"ID3D9 = 0x%016llX", ret); @@ -155,9 +149,7 @@ HRESULT loader_core::RouteDXGIFactoryCreate(UINT ver, UINT Flags, REFIID riid, v HRESULT ret = NULL; - HMODULE directDraw = GetModuleHandleA("ddraw.dll"); - - if (!directDraw && dxgi_create_hook) + if (dxgi_create_hook) { LOG_DEBUG(L"core", L"Calling DXGICreate, hook = 0x%016llX", dxgi_create_hook); ret = dxgi_create_hook(ver, Flags, riid, ppFactory); @@ -202,7 +194,7 @@ HRESULT loader_core::OnDXGIFactoryCreate(UINT ver, UINT Flags, REFIID riid, void IDirect3D9 * loader_core::OnD3DCreate(UINT sdkVer) -{ +{ innerInit(); return RouteD3DCreate(sdkVer); } @@ -285,7 +277,7 @@ void loader_core::SignalUnload() } BOOL loader_core::SwitchState(loader_state newState) -{ +{ int doSwitch = 0; switch (state)