forked from doitsujin/atelier-sync-fix
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmain.cpp
201 lines (153 loc) · 5.08 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#include <iostream>
#include "impl.h"
#include "util.h"
#include <array>
#include <cstddef>
#include <cstdint>
#include <cstring>
#ifdef _MSC_VER
#define DLLEXPORT
#else
#define DLLEXPORT __declspec(dllexport)
#endif
namespace atfix {
Log log("atfix.log");
/** Load system D3D11 DLL and return entry points */
using PFN_D3D11CreateDevice = HRESULT (__stdcall *) (
IDXGIAdapter*, D3D_DRIVER_TYPE, HMODULE, UINT, const D3D_FEATURE_LEVEL*,
UINT, UINT, ID3D11Device**, D3D_FEATURE_LEVEL*, ID3D11DeviceContext**);
using PFN_D3D11CreateDeviceAndSwapChain = HRESULT (__stdcall *) (
IDXGIAdapter*, D3D_DRIVER_TYPE, HMODULE, UINT, const D3D_FEATURE_LEVEL*,
UINT, UINT, const DXGI_SWAP_CHAIN_DESC*, IDXGISwapChain**, ID3D11Device**,
D3D_FEATURE_LEVEL*, ID3D11DeviceContext**);
struct D3D11Proc {
PFN_D3D11CreateDevice D3D11CreateDevice = nullptr;
PFN_D3D11CreateDeviceAndSwapChain D3D11CreateDeviceAndSwapChain = nullptr;
};
D3D11Proc loadSystemD3D11() {
static mutex initMutex;
static D3D11Proc d3d11Proc;
if (d3d11Proc.D3D11CreateDevice)
return d3d11Proc;
std::lock_guard lock(initMutex);
if (d3d11Proc.D3D11CreateDevice)
return d3d11Proc;
HMODULE libD3D11 = LoadLibraryExA("d3d11_proxy.dll", nullptr, LOAD_LIBRARY_SEARCH_APPLICATION_DIR);
if (libD3D11) {
log("Using d3d11_proxy.dll");
} else {
std::array<char, MAX_PATH + 1> path = { };
if (!GetSystemDirectoryA(path.data(), MAX_PATH))
return D3D11Proc();
std::strncat(path.data(), "\\d3d11.dll", MAX_PATH);
log("Using ", path.data());
libD3D11 = LoadLibraryA(path.data());
if (!libD3D11) {
log("Failed to load d3d11.dll (", path.data(), ")");
return D3D11Proc();
}
}
d3d11Proc.D3D11CreateDevice = reinterpret_cast<PFN_D3D11CreateDevice>(
GetProcAddress(libD3D11, "D3D11CreateDevice"));
d3d11Proc.D3D11CreateDeviceAndSwapChain = reinterpret_cast<PFN_D3D11CreateDeviceAndSwapChain>(
GetProcAddress(libD3D11, "D3D11CreateDeviceAndSwapChain"));
log("D3D11CreateDevice @ ", reinterpret_cast<void*>(d3d11Proc.D3D11CreateDevice));
log("D3D11CreateDeviceAndSwapChain @ ", reinterpret_cast<void*>(d3d11Proc.D3D11CreateDeviceAndSwapChain));
return d3d11Proc;
}
}
extern "C" {
DLLEXPORT HRESULT __stdcall D3D11CreateDevice(
IDXGIAdapter* pAdapter,
D3D_DRIVER_TYPE DriverType,
HMODULE Software,
UINT Flags,
const D3D_FEATURE_LEVEL* pFeatureLevels,
UINT FeatureLevels,
UINT SDKVersion,
ID3D11Device** ppDevice,
D3D_FEATURE_LEVEL* pFeatureLevel,
ID3D11DeviceContext** ppImmediateContext) {
if (ppDevice)
*ppDevice = nullptr;
if (ppImmediateContext)
*ppImmediateContext = nullptr;
auto proc = atfix::loadSystemD3D11();
if (!proc.D3D11CreateDevice)
return E_FAIL;
ID3D11Device* device = nullptr;
ID3D11DeviceContext* context = nullptr;
HRESULT hr = (*proc.D3D11CreateDevice)(pAdapter, DriverType, Software,
Flags, pFeatureLevels, FeatureLevels, SDKVersion, &device, pFeatureLevel,
&context);
if (FAILED(hr))
return hr;
atfix::hookDevice(device);
atfix::hookContext(context);
if (ppDevice) {
device->AddRef();
*ppDevice = device;
}
if (ppImmediateContext) {
context->AddRef();
*ppImmediateContext = context;
}
device->Release();
context->Release();
return hr;
}
DLLEXPORT HRESULT __stdcall D3D11CreateDeviceAndSwapChain(
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) {
if (ppDevice)
*ppDevice = nullptr;
if (ppImmediateContext)
*ppImmediateContext = nullptr;
if (ppSwapChain)
*ppSwapChain = nullptr;
auto proc = atfix::loadSystemD3D11();
if (!proc.D3D11CreateDeviceAndSwapChain)
return E_FAIL;
ID3D11Device* device = nullptr;
ID3D11DeviceContext* context = nullptr;
HRESULT hr = (*proc.D3D11CreateDeviceAndSwapChain)(pAdapter, DriverType, Software,
Flags, pFeatureLevels, FeatureLevels, SDKVersion, pSwapChainDesc, ppSwapChain,
&device, pFeatureLevel, &context);
if (FAILED(hr))
return hr;
atfix::hookDevice(device);
atfix::hookContext(context);
if (ppDevice) {
device->AddRef();
*ppDevice = device;
}
if (ppImmediateContext) {
context->AddRef();
*ppImmediateContext = context;
}
device->Release();
context->Release();
return hr;
}
BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved) {
switch (fdwReason) {
case DLL_PROCESS_ATTACH:
MH_Initialize();
break;
case DLL_PROCESS_DETACH:
MH_Uninitialize();
break;
}
return TRUE;
}
}