Skip to content

Commit 0b97121

Browse files
committed
Seems to be finally functional, in debugging and normal modes.
1 parent 14baeb3 commit 0b97121

File tree

6 files changed

+123
-68
lines changed

6 files changed

+123
-68
lines changed

d3d11/dllmain.cpp

+67-11
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,29 @@
33
#include <windows.h>
44
#include <d3d11.h>
55

6+
using D3D11CoreCreateDevice_t = HRESULT (WINAPI *)(
7+
IDXGIFactory * pFactory,
8+
IDXGIAdapter * pAdapter,
9+
UINT Flags,
10+
const D3D_FEATURE_LEVEL * pFeatureLevels,
11+
UINT FeatureLevels,
12+
ID3D11Device * *ppDevice);
13+
D3D11CoreCreateDevice_t RealD3D11CoreCreateDevice = nullptr;
14+
15+
using D3D11CoreCreateLayeredDevice_t = HRESULT (WINAPI *)(
16+
const void* unknown0,
17+
DWORD unknown1,
18+
const void* unknown2,
19+
REFIID riid,
20+
void** ppvObj);
21+
D3D11CoreCreateLayeredDevice_t RealD3D11CoreCreateLayeredDevice = nullptr;
22+
23+
using D3D11CoreGetLayeredDeviceSize_t = SIZE_T (WINAPI *)(const void* unknown0, DWORD unknown1);
24+
D3D11CoreGetLayeredDeviceSize_t RealD3D11CoreGetLayeredDeviceSize = nullptr;
25+
26+
using D3D11CoreRegisterLayers_t = HRESULT (WINAPI *)(const void* unknown0, DWORD unknown1);
27+
D3D11CoreRegisterLayers_t RealD3D11CoreRegisterLayers = nullptr;
28+
629
#define DX11_CREATE_PLIST pAdapter, DriverType, Software, Flags, pFeatureLevels, FeatureLevels, SDKVersion, pSwapChainDesc, ppSwapChain, ppDevice, pFeatureLevel, ppImmediateContext
730

831
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()
3962
GetSystemDirectory(infoBuf, 4096);
4063
lstrcatW(infoBuf, L"\\d3d11.dll");
4164

65+
auto existingModule = GetModuleHandle(infoBuf);
66+
if(existingModule)
67+
return existingModule;
68+
4269
return LoadLibrary(infoBuf);
4370
}
4471

72+
void FreeD3D11Module() {
73+
wchar_t infoBuf[4096];
74+
GetSystemDirectory(infoBuf, 4096);
75+
lstrcatW(infoBuf, L"\\d3d11.dll");
76+
77+
FreeLibrary(GetModuleHandle(infoBuf));
78+
}
79+
4580
FARPROC GetD3D11Function(LPCSTR name)
4681
{
4782
static HMODULE hmod = GetD3D11Module();
@@ -50,6 +85,9 @@ FARPROC GetD3D11Function(LPCSTR name)
5085

5186
#define GET_D3D11_FUNC(name) (decltype(name)*)GetD3D11Function(#name)
5287

88+
bool ShouldTryLoading = true;
89+
void LoadAllFunctions();
90+
5391
extern "C" HRESULT WINAPI D3D11CoreCreateDevice(
5492
IDXGIFactory * pFactory,
5593
IDXGIAdapter * pAdapter,
@@ -58,8 +96,9 @@ extern "C" HRESULT WINAPI D3D11CoreCreateDevice(
5896
UINT FeatureLevels,
5997
ID3D11Device * *ppDevice)
6098
{
61-
static auto func = GET_D3D11_FUNC(D3D11CoreCreateDevice);
62-
return func(pFactory, pAdapter, Flags, pFeatureLevels, FeatureLevels, ppDevice);
99+
if(ShouldTryLoading)
100+
LoadAllFunctions();
101+
return RealD3D11CoreCreateDevice(pFactory, pAdapter, Flags, pFeatureLevels, FeatureLevels, ppDevice);
63102
}
64103

65104
extern "C" HRESULT WINAPI D3D11CoreCreateLayeredDevice(
@@ -69,27 +108,44 @@ extern "C" HRESULT WINAPI D3D11CoreCreateLayeredDevice(
69108
REFIID riid,
70109
void** ppvObj)
71110
{
72-
static auto func = GET_D3D11_FUNC(D3D11CoreCreateLayeredDevice);
73-
return func(unknown0, unknown1, unknown2, riid, ppvObj);
111+
if(ShouldTryLoading)
112+
LoadAllFunctions();
113+
return RealD3D11CoreCreateLayeredDevice(unknown0, unknown1, unknown2, riid, ppvObj);
74114
}
75115

76116
extern "C" SIZE_T WINAPI D3D11CoreGetLayeredDeviceSize(const void* unknown0, DWORD unknown1)
77117
{
78-
static auto func = GET_D3D11_FUNC(D3D11CoreGetLayeredDeviceSize);
79-
return func(unknown0, unknown1);
118+
if(ShouldTryLoading)
119+
LoadAllFunctions();
120+
return RealD3D11CoreGetLayeredDeviceSize(unknown0, unknown1);
80121
}
81122

82123
extern "C" HRESULT WINAPI D3D11CoreRegisterLayers(const void* unknown0, DWORD unknown1)
83124
{
84-
static auto func = GET_D3D11_FUNC(D3D11CoreRegisterLayers);
85-
return func(unknown0, unknown1);
125+
if(ShouldTryLoading)
126+
LoadAllFunctions();
127+
return RealD3D11CoreRegisterLayers(unknown0, unknown1);
128+
}
129+
130+
void LoadAllFunctions() {
131+
RealD3D11CoreCreateDevice = GET_D3D11_FUNC(D3D11CoreCreateDevice);
132+
RealD3D11CoreCreateLayeredDevice = GET_D3D11_FUNC(D3D11CoreCreateLayeredDevice);
133+
RealD3D11CoreGetLayeredDeviceSize = GET_D3D11_FUNC(D3D11CoreGetLayeredDeviceSize);
134+
RealD3D11CoreRegisterLayers = GET_D3D11_FUNC(D3D11CoreRegisterLayers);
135+
136+
ShouldTryLoading = false;
86137
}
87138

88139
BOOL APIENTRY DllMain( HMODULE hModule,
89-
DWORD ul_reason_for_call,
140+
DWORD fdwReason,
90141
LPVOID lpReserved
91142
)
92143
{
93-
return TRUE;
94-
}
144+
if(fdwReason == DLL_PROCESS_DETACH)
145+
{
146+
ShouldTryLoading = true;
147+
FreeD3D11Module();
148+
}
95149

150+
return true;
151+
}

d3d9/d3d9.vcxproj

+2-10
Original file line numberDiff line numberDiff line change
@@ -105,11 +105,7 @@
105105
<EnableUAC>false</EnableUAC>
106106
<ModuleDefinitionFile>deffile.def</ModuleDefinitionFile>
107107
</Link>
108-
<PostBuildEvent>
109-
<Command>IF DEFINED GW2_INSTALL_DIR (
110-
xcopy "$(TargetPath)" "$(GW2_INSTALL_DIR)\bin64\" /Y
111-
)</Command>
112-
</PostBuildEvent>
108+
<PostBuildEvent />
113109
</ItemDefinitionGroup>
114110
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
115111
<ClCompile>
@@ -142,11 +138,7 @@ xcopy "$(TargetPath)" "$(GW2_INSTALL_DIR)\bin64\" /Y
142138
<EnableUAC>false</EnableUAC>
143139
<ModuleDefinitionFile>deffile.def</ModuleDefinitionFile>
144140
</Link>
145-
<PostBuildEvent>
146-
<Command>IF DEFINED GW2_INSTALL_DIR (
147-
xcopy "$(TargetPath)" "$(GW2_INSTALL_DIR)\bin64\" /Y
148-
)</Command>
149-
</PostBuildEvent>
141+
<PostBuildEvent />
150142
</ItemDefinitionGroup>
151143
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
152144
<ClCompile>

dxgi/dllmain.cpp

+39-16
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
#define WIN32_LEAN_AND_MEAN
22
#include <windows.h>
33

4-
using Compat_t = void*(WINAPI *)();
5-
Compat_t RealCompatValue = nullptr;
6-
Compat_t RealCompatString = nullptr;
4+
using CompatValue_t = BOOL(WINAPI *)(LPCSTR szName, UINT64 *pValue);
5+
using CompatString_t = BOOL(WINAPI *)(LPCSTR szName, ULONG *pSize, LPSTR lpData, bool Flag);
6+
CompatValue_t RealCompatValue = nullptr;
7+
CompatString_t RealCompatString = nullptr;
78

89
typedef HRESULT(WINAPI* DXGIFactoryCreate0)(REFIID riid, void** ppFactory);
910
typedef HRESULT(WINAPI* DXGIFactoryCreate1)(REFIID riid, void** ppFactory);
@@ -53,9 +54,21 @@ HMODULE GetDXGIModule()
5354
GetSystemDirectory(infoBuf, 4096);
5455
lstrcatW(infoBuf, L"\\dxgi.dll");
5556

57+
auto existingModule = GetModuleHandle(infoBuf);
58+
if(existingModule)
59+
return existingModule;
60+
5661
return LoadLibrary(infoBuf);
5762
}
5863

64+
void FreeDXGIModule() {
65+
wchar_t infoBuf[4096];
66+
GetSystemDirectory(infoBuf, 4096);
67+
lstrcatW(infoBuf, L"\\dxgi.dll");
68+
69+
FreeLibrary(GetModuleHandle(infoBuf));
70+
}
71+
5972
FARPROC GetDXGIFunction(LPCSTR name)
6073
{
6174
static HMODULE hmod = GetDXGIModule();
@@ -64,31 +77,41 @@ FARPROC GetDXGIFunction(LPCSTR name)
6477

6578
#define GET_DXGI_FUNC(name) (decltype(name)*)GetDXGIFunction(#name)
6679

67-
extern "C" void* WINAPI CompatValue()
80+
bool ShouldTryLoading = true;
81+
void LoadAllFunctions();
82+
83+
extern "C" BOOL WINAPI CompatValue(LPCSTR szName, UINT64 *pValue)
6884
{
69-
return RealCompatValue();
85+
if(ShouldTryLoading)
86+
LoadAllFunctions();
87+
return RealCompatValue(szName, pValue);
7088
}
7189

72-
extern "C" void* WINAPI CompatString()
90+
extern "C" BOOL WINAPI CompatString(LPCSTR szName, ULONG *pSize, LPSTR lpData, bool Flag)
7391
{
74-
return RealCompatString();
92+
if(ShouldTryLoading)
93+
LoadAllFunctions();
94+
return RealCompatString(szName, pSize, lpData, Flag);
95+
}
96+
97+
void LoadAllFunctions() {
98+
RealCompatValue = GET_DXGI_FUNC(CompatValue);
99+
RealCompatString = GET_DXGI_FUNC(CompatString);
100+
101+
ShouldTryLoading = false;
75102
}
76103

77104
BOOL APIENTRY DllMain( HMODULE hModule,
78105
DWORD fdwReason,
79106
LPVOID lpReserved
80107
)
81108
{
82-
switch (fdwReason)
109+
if(fdwReason == DLL_PROCESS_DETACH)
83110
{
84-
case DLL_PROCESS_ATTACH:
85-
RealCompatValue = GET_DXGI_FUNC(CompatValue);
86-
RealCompatString = GET_DXGI_FUNC(CompatString);
87-
break;
88-
case DLL_PROCESS_DETACH:
89-
RealCompatValue = nullptr;
90-
RealCompatString = nullptr;
91-
break;
111+
ShouldTryLoading = true;
112+
FreeDXGIModule();
92113
}
114+
115+
return true;
93116
}
94117

loader_core.sln

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11

22
Microsoft Visual Studio Solution File, Format Version 12.00
3-
# Visual Studio Version 16
4-
VisualStudioVersion = 16.0.29728.190
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.3.32901.215
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "loader_core", "loader_core\loader_core.vcxproj", "{024CCE36-7DE3-4ACC-80E6-EF25F9894C11}"
7+
ProjectSection(ProjectDependencies) = postProject
8+
{5A73591F-E0F2-400D-9C93-146CD068E18A} = {5A73591F-E0F2-400D-9C93-146CD068E18A}
9+
{C5DD718A-8B5E-493D-97F3-A39F2812E782} = {C5DD718A-8B5E-493D-97F3-A39F2812E782}
10+
EndProjectSection
711
EndProject
812
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "fake_client", "fake_client\fake_client.vcxproj", "{33FC0BB7-E821-4B68-BB81-B16BD7F76C84}"
913
ProjectSection(ProjectDependencies) = postProject

loader_core/deffile.def

-12
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,6 @@
11
LIBRARY addonLoader.dll
22
EXPORTS
33

4-
; d3d9
5-
6-
Direct3DCreate9 @30
7-
Direct3DCreate9Ex
8-
D3DPERF_BeginEvent
9-
D3DPERF_EndEvent
10-
D3DPERF_SetMarker
11-
D3DPERF_SetRegion
12-
D3DPERF_QueryRepeatFrame
13-
D3DPERF_SetOptions
14-
D3DPERF_GetStatus
15-
164
; d3d11
175

186
D3D11CreateDevice

loader_core/loader_core.cpp

+9-17
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ loader_core loader_core::instance;
55

66
loader_core::loader_core()
77
{
8-
state = LDR_DLL_LOADED;
8+
state = LDR_DLL_LOADED;
99
}
1010

1111
loader_core::~loader_core()
12-
{
12+
{
1313
}
1414

1515
loader_state loader_core::GetCurrentState()
@@ -83,20 +83,16 @@ void loader_core::log_text_fmt(gw2al_log_level level, const wchar_t * source, co
8383

8484
va_list arg;
8585
va_start(arg, fmt);
86-
86+
8787
vswprintf(buf, 4096, fmt, arg);
88-
88+
8989
va_end(arg);
9090

9191
gw2al_core__log_text(level, (wchar_t*)source, buf);
9292
}
9393

9494
void loader_core::innerInit()
9595
{
96-
HMODULE directDraw = GetModuleHandleA("ddraw.dll");
97-
if(directDraw)
98-
return;
99-
10096
if (SwitchState(LDR_ADDON_LOAD))
10197
{
10298
bool isFirstLoad = gw2al_core__init();
@@ -116,9 +112,7 @@ IDirect3D9* loader_core::RouteD3DCreate(UINT sdkVer)
116112

117113
IDirect3D9* ret = NULL;
118114

119-
HMODULE directDraw = GetModuleHandleA("ddraw.dll");
120-
121-
if (!directDraw && d3d9_create_hook)
115+
if (d3d9_create_hook)
122116
{
123117
LOG_DEBUG(L"core", L"Calling D3D9Create, hook = 0x%016llX", d3d9_create_hook);
124118
ret = d3d9_create_hook();
@@ -136,7 +130,7 @@ IDirect3D9* loader_core::RouteD3DCreate(UINT sdkVer)
136130
typedef IDirect3D9* (WINAPI* Direct3DCreate9Func)(UINT sdkver);
137131

138132
Direct3DCreate9Func origDirect3DCreate9 = (Direct3DCreate9Func)GetProcAddress(sys_d3d9, "Direct3DCreate9");
139-
ret = origDirect3DCreate9(sdkVer);
133+
ret = origDirect3DCreate9(sdkVer);
140134
}
141135

142136
LOG_DEBUG(L"core", L"ID3D9 = 0x%016llX", ret);
@@ -155,9 +149,7 @@ HRESULT loader_core::RouteDXGIFactoryCreate(UINT ver, UINT Flags, REFIID riid, v
155149

156150
HRESULT ret = NULL;
157151

158-
HMODULE directDraw = GetModuleHandleA("ddraw.dll");
159-
160-
if (!directDraw && dxgi_create_hook)
152+
if (dxgi_create_hook)
161153
{
162154
LOG_DEBUG(L"core", L"Calling DXGICreate, hook = 0x%016llX", dxgi_create_hook);
163155
ret = dxgi_create_hook(ver, Flags, riid, ppFactory);
@@ -202,7 +194,7 @@ HRESULT loader_core::OnDXGIFactoryCreate(UINT ver, UINT Flags, REFIID riid, void
202194

203195

204196
IDirect3D9 * loader_core::OnD3DCreate(UINT sdkVer)
205-
{
197+
{
206198
innerInit();
207199
return RouteD3DCreate(sdkVer);
208200
}
@@ -285,7 +277,7 @@ void loader_core::SignalUnload()
285277
}
286278

287279
BOOL loader_core::SwitchState(loader_state newState)
288-
{
280+
{
289281
int doSwitch = 0;
290282

291283
switch (state)

0 commit comments

Comments
 (0)