This repository has been archived by the owner on Jan 12, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lgvs_detours.cpp
301 lines (230 loc) · 7.45 KB
/
lgvs_detours.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library.
*/
#define CINTERFACE
#include "lgvs_detours.h"
#include <algorithm>
#include <bitset>
#include <new>
#include "detours.h"
#include "lgvs_system.h"
namespace lgvs {
Detours::Detours() :
is_initialized_(),
is_detoured_(),
d3d9_library_(),
fake_end_scene_(),
fake_test_coop_level_(),
fake_reset_(),
real_end_scene_(),
real_test_coop_level_(),
real_reset_()
{
}
Detours::~Detours()
{
uninitialize();
}
bool Detours::initialize(
FP_D3DD9_END_SCENE fake_end_scene,
FP_D3DD9_TEST_COOP_LEVEL fake_test_coop_level,
FP_D3DD9_RESET fake_reset)
{
uninitialize();
error_string_.clear();
if (!fake_end_scene) {
error_string_ = L"Null \"EndScene\" fake function pointer.";
return false;
}
if (!fake_test_coop_level) {
error_string_ = L"Null \"TestCooperativeLevel\" fake function pointer.";
return false;
}
if (!fake_reset) {
error_string_ = L"Null \"Reset\" fake function pointer.";
return false;
}
std::wstring d3d9_dll_path = System::combine_paths(
System::get_system_dir(), L"d3d9.dll");
d3d9_library_ = ::LoadLibraryW(d3d9_dll_path.c_str());
if (!d3d9_library_) {
error_string_ = L"Failed to load a system Direct3D 9 main library.";
return false;
}
bool is_succeed = true;
ATOM dummy_class = 0;
if (is_succeed) {
WNDCLASSEXW wce;
std::uninitialized_fill_n(
reinterpret_cast<char*>(&wce), sizeof(wce), 0);
wce.cbSize = sizeof(wce);
wce.style = CS_CLASSDC;
wce.lpfnWndProc = ::DefWindowProcW;
wce.lpszClassName = L"LgVidSubsDetoursDummyClass";
dummy_class = ::RegisterClassExW(&wce);
if (dummy_class == 0) {
is_succeed = false;
error_string_ = L"Failed to register a dummy window class.";
}
}
HWND dummy_window = nullptr;
if (is_succeed) {
dummy_window = ::CreateWindowW(
reinterpret_cast <LPCWSTR>(dummy_class),
L"LgVidSubs Dummy Window",
WS_OVERLAPPED | WS_CAPTION | WS_CLIPCHILDREN,
0, 0, 2, 2,
::GetDesktopWindow(),
nullptr, nullptr, 0);
if (dummy_window == nullptr) {
is_succeed = false;
error_string_ = L"Failed to create a dummy window.";
}
}
typedef LPDIRECT3D9 (WINAPI* FP_DIRECT3DCREATE9)(UINT SDKVersion);
FP_DIRECT3DCREATE9 d3d9_create = nullptr;
if (is_succeed) {
d3d9_create = reinterpret_cast<FP_DIRECT3DCREATE9>(
::GetProcAddress(d3d9_library_, "Direct3DCreate9"));
if (d3d9_create == nullptr) {
is_succeed = false;
error_string_ = L"Missing symbol: Direct3DCreate9.";
}
}
LPDIRECT3D9 d3d9 = nullptr;
if (is_succeed) {
d3d9 = d3d9_create(D3D_SDK_VERSION);
if (d3d9 == nullptr) {
is_succeed = false;
error_string_ = L"Failed to create a Direct3D9 instance.";
}
}
HRESULT d3d_result = D3D_OK;
LPDIRECT3DDEVICE9 d3d_device9 = nullptr;
if (is_succeed) {
D3DPRESENT_PARAMETERS pp;
std::uninitialized_fill_n(
reinterpret_cast<char*>(&pp), sizeof(pp), 0);
pp.SwapEffect = D3DSWAPEFFECT_FLIP;
pp.Windowed = TRUE;
d3d_result = IDirect3D9_CreateDevice(
d3d9,
D3DADAPTER_DEFAULT,
D3DDEVTYPE_HAL,
dummy_window,
D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&pp,
&d3d_device9);
if (d3d_result != D3D_OK) {
is_succeed = false;
error_string_ = L"Failed to create a Direct3D 9 device instance.";
}
}
if (is_succeed) {
fake_end_scene_ = fake_end_scene;
fake_test_coop_level_ = fake_test_coop_level;
fake_reset_ = fake_reset;
real_end_scene_ = d3d_device9->lpVtbl->EndScene;
real_test_coop_level_ = d3d_device9->lpVtbl->TestCooperativeLevel;
real_reset_ = d3d_device9->lpVtbl->Reset;
LONG d_result = 0;
d_result = ::DetourTransactionBegin();
d_result = ::DetourUpdateThread(::GetCurrentThread());
d_result = ::DetourAttach(&reinterpret_cast<PVOID&>(real_end_scene_), fake_end_scene);
d_result = ::DetourAttach(&reinterpret_cast<PVOID&>(real_test_coop_level_), fake_test_coop_level);
d_result = ::DetourAttach(&reinterpret_cast<PVOID&>(real_reset_), fake_reset);
if (d_result != 0) {
is_succeed = false;
d_result = ::DetourTransactionAbort();
} else {
d_result = ::DetourTransactionCommit();
is_succeed = (d_result == 0);
}
if (!is_succeed)
error_string_ = L"Failed to detour Direct3D 9 APIs.";
else
is_detoured_ = true;
}
if (d3d_device9)
IDirect3DDevice9_Release(d3d_device9);
if (d3d9)
IDirect3D9_Release(d3d9);
if (dummy_window)
::DestroyWindow(dummy_window);
if (dummy_class != 0)
::UnregisterClassW(reinterpret_cast<LPCWSTR>(dummy_class), nullptr);
if (is_succeed)
is_initialized_ = true;
else
uninitialize();
return is_succeed;
}
void Detours::uninitialize()
{
is_initialized_ = false;
if (is_detoured_) {
LONG d_result = 0;
d_result = ::DetourTransactionBegin();
d_result = ::DetourUpdateThread(::GetCurrentThread());
d_result = ::DetourDetach(
&reinterpret_cast<PVOID&>(real_end_scene_),
fake_end_scene_);
d_result = ::DetourDetach(
&reinterpret_cast<PVOID&>(real_test_coop_level_),
fake_test_coop_level_);
d_result = ::DetourDetach(
&reinterpret_cast<PVOID&>(real_reset_),
fake_reset_);
if (d_result != 0)
d_result = ::DetourTransactionAbort();
else
d_result = ::DetourTransactionCommit();
is_detoured_ = false;
}
fake_end_scene_ = nullptr;
fake_test_coop_level_ = nullptr;
fake_reset_ = nullptr;
real_end_scene_ = nullptr;
real_test_coop_level_ = nullptr;
real_reset_ = nullptr;
if (d3d9_library_) {
::FreeLibrary(d3d9_library_);
d3d9_library_ = nullptr;
}
}
bool Detours::is_initialized() const
{
return is_initialized_;
}
const std::wstring& Detours::get_error_string() const
{
return error_string_;
}
HRESULT STDMETHODCALLTYPE Detours::d3dd9_end_scene(
LPDIRECT3DDEVICE9 self)
{
return real_end_scene_(self);
}
HRESULT STDMETHODCALLTYPE Detours::d3dd9_test_coop_level(
LPDIRECT3DDEVICE9 self)
{
return real_test_coop_level_(self);
}
HRESULT STDMETHODCALLTYPE Detours::d3dd9_reset(
LPDIRECT3DDEVICE9 self,
D3DPRESENT_PARAMETERS* presentation_parameters)
{
return real_reset_(self, presentation_parameters);
}
} // namespace lgvs