-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathD3DDeviceFetcher.cpp
More file actions
175 lines (141 loc) · 4.64 KB
/
Copy pathD3DDeviceFetcher.cpp
File metadata and controls
175 lines (141 loc) · 4.64 KB
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
#include "hacklib/D3DDeviceFetcher.h"
#include "hacklib/Hooker.h"
#include <Windows.h>
#include <condition_variable>
#include <mutex>
using namespace hl;
std::mutex g_mutex;
std::condition_variable g_condVar;
LPDIRECT3DDEVICE9 g_pD3D9Device;
IDXGISwapChain* g_pD3D11SwapChain;
ID3D11Device* g_pD3D11Device;
ID3D11DeviceContext* g_pD3D11DeviceContext;
class DummyWindow
{
public:
~DummyWindow()
{
if (m_hWnd)
{
DestroyWindow(m_hWnd);
UnregisterClassA("DXTMP", GetModuleHandleA(NULL));
}
}
HWND create()
{
WNDCLASSEXA wc = { 0 };
wc.cbSize = sizeof(wc);
wc.style = CS_CLASSDC;
wc.lpfnWndProc = DefWindowProc;
wc.hInstance = GetModuleHandleA(NULL);
wc.lpszClassName = "DXTMP";
RegisterClassExA(&wc);
m_hWnd =
CreateWindowA("DXTMP", 0, WS_OVERLAPPEDWINDOW, 100, 100, 300, 300, GetDesktopWindow(), 0, wc.hInstance, 0);
return m_hWnd;
}
private:
HWND m_hWnd = NULL;
};
static void cbEndScene(hl::CpuContext* ctx)
{
std::lock_guard lock(g_mutex);
#ifdef ARCH_64BIT
// Microsoft x64
g_pD3D9Device = (LPDIRECT3DDEVICE9)ctx->RCX;
#else
// __stdcall
g_pD3D9Device = *(LPDIRECT3DDEVICE9*)(ctx->ESP + sizeof(void*));
#endif
g_condVar.notify_one();
}
IDirect3DDevice9* D3DDeviceFetcher::GetD3D9Device(int timeout)
{
// clean globals
g_pD3D9Device = NULL;
DummyWindow wnd;
auto hWnd = wnd.create();
if (!hWnd)
return NULL;
// create a dummy d3d device
LPDIRECT3D9 pD3D = Direct3DCreate9(D3D_SDK_VERSION);
if (!pD3D)
return NULL;
D3DPRESENT_PARAMETERS d3dPar = { 0 };
d3dPar.Windowed = TRUE;
d3dPar.SwapEffect = D3DSWAPEFFECT_DISCARD;
LPDIRECT3DDEVICE9 pDev = NULL;
pD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_NULLREF, hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dPar,
&pDev);
if (!pDev)
return NULL;
// fetch the location of endscene in the d3d9 module via vtable of dummy device
auto endScene = ((uintptr_t**)pDev)[0][42];
// clean up dummy device
pDev->Release();
pD3D->Release();
Hooker hooker;
hooker.hookVEH(endScene, cbEndScene);
std::unique_lock lock(g_mutex);
g_condVar.wait_for(lock, std::chrono::milliseconds(timeout), [] { return g_pD3D9Device != NULL; });
return g_pD3D9Device;
}
static void cbPresent(hl::CpuContext* ctx)
{
std::lock_guard lock(g_mutex);
#ifdef ARCH_64BIT
// Microsoft x64
g_pD3D11SwapChain = (IDXGISwapChain*)ctx->RCX;
#else
// __stdcall
g_pD3D11SwapChain = *(IDXGISwapChain**)(ctx->ESP + sizeof(void*));
#endif
g_pD3D11SwapChain->GetDevice(__uuidof(g_pD3D11Device), (void**)&g_pD3D11Device);
g_pD3D11Device->GetImmediateContext(&g_pD3D11DeviceContext);
// The above calls increased the reference counts, so decrease it again.
g_pD3D11Device->Release();
g_pD3D11DeviceContext->Release();
g_condVar.notify_one();
}
D3DDeviceFetcher::D3D11COMs D3DDeviceFetcher::GetD3D11Device(int timeout)
{
g_pD3D11SwapChain = NULL;
g_pD3D11Device = NULL;
g_pD3D11DeviceContext = NULL;
D3D11COMs d3d11;
DummyWindow wnd;
auto hWnd = wnd.create();
if (!hWnd)
return d3d11;
D3D_FEATURE_LEVEL featureLevel = D3D_FEATURE_LEVEL_11_0;
DXGI_SWAP_CHAIN_DESC swapChainDesc = { 0 };
swapChainDesc.BufferCount = 1;
swapChainDesc.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
swapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
swapChainDesc.OutputWindow = hWnd;
swapChainDesc.SampleDesc.Count = 1;
swapChainDesc.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;
swapChainDesc.Windowed = TRUE;
IDXGISwapChain* pSwapChain = NULL;
ID3D11Device* pDevice = NULL;
ID3D11DeviceContext* pDeviceContext = NULL;
HRESULT hr =
D3D11CreateDeviceAndSwapChain(NULL, D3D_DRIVER_TYPE_HARDWARE, NULL, 0, &featureLevel, 1, D3D11_SDK_VERSION,
&swapChainDesc, &pSwapChain, &pDevice, NULL, &pDeviceContext);
if (!pSwapChain || !pDevice || !pDeviceContext)
return d3d11;
auto present = ((uintptr_t**)pSwapChain)[0][8];
pDevice->Release();
pDeviceContext->Release();
pSwapChain->Release();
Hooker hooker;
hooker.hookVEH(present, cbPresent);
std::unique_lock lock(g_mutex);
g_condVar.wait_for(
lock, std::chrono::milliseconds(timeout),
[] { return g_pD3D11SwapChain != NULL && g_pD3D11Device != NULL && g_pD3D11DeviceContext != NULL; });
d3d11.pSwapChain = g_pD3D11SwapChain;
d3d11.pDevice = g_pD3D11Device;
d3d11.pDeviceContext = g_pD3D11DeviceContext;
return d3d11;
}