-
Notifications
You must be signed in to change notification settings - Fork 42
/
main.cpp
295 lines (267 loc) · 8.91 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
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
#include "main.h"
#include <dxgi.h>
#include <d3d11.h>
#include <d3d11_4.h>
//#include <d3d11_3.h>
#include <wrl.h>
#include <ShlObj.h>
#include <system_error>
#include <string>
#include <filesystem>
#include <wincodec.h>
#include <cstdio>
#include <MinHook.h>
#include <cassert>
#include <chrono>
#include "export.h"
#include <d3d11shader.h>
#include <queue>
#include <d3dcompiler.h>
#include <vector>
#include <Eigen/Core>
using Microsoft::WRL::ComPtr;
using namespace std::experimental::filesystem;
using namespace std::string_literals;
using std::chrono::milliseconds;
using std::chrono::time_point;
using std::chrono::system_clock;
using std::vector;
using Eigen::Matrix4f;
using Eigen::Vector3f;
using Eigen::Vector4f;
typedef void(*draw_indexed_hook_t)(ID3D11DeviceContext*, UINT, UINT, INT);
void scriptMain();
void presentCallback(void* chain);
void OnKeyboardMessage(DWORD key, WORD repeats, BYTE scanCode, BOOL isExtended, BOOL isWithAlt, BOOL wasDownBefore, BOOL isUpNow);
//void draw_indexed_hook(ID3D11DeviceContext3* self, UINT IndexStart, UINT StartIndexLocation, INT BaseVertexLocation);
static time_point<system_clock> last_capture_color;
static time_point<system_clock> last_capture_depth;
static const char* logFilePath = "GTANativePlugin.log";
//--------
//offsets
//--------
const size_t drawIndexedOffset = 12;
const size_t clearDepthStencilViewOffset = 53;
//-------------------------
//interesting D3D resources
//-------------------------
static ComPtr<ID3D11DepthStencilView> lastDsv;
static ComPtr<ID3D11RenderTargetView> lastRtv;
static ComPtr<ID3D11Buffer> lastConstants;
static bool saveNextFrame = false;
static bool hooked = false;
//-------------------------
//global control variables
//-------------------------
static int draw_indexed_count = 0;
static void (_stdcall ID3D11DeviceContext::* origDrawInstanced)(UINT, UINT, INT) = nullptr;
static ComPtr<ID3D11DeviceContext> ctx;
int __stdcall DllMain(HMODULE hinstance, DWORD reason, LPVOID lpReserved)
{
MH_STATUS res;
auto f = fopen(logFilePath, "a");
switch(reason)
{
case DLL_PROCESS_ATTACH:
res = MH_Initialize();
if (res != MH_OK) fprintf(f, "Could not init Minihook\n");
//scriptRegister(hinstance, scriptMain);
presentCallbackRegister(presentCallback);
keyboardHandlerRegister(OnKeyboardMessage);
break;
case DLL_PROCESS_DETACH:
res = MH_Uninitialize();
if (res != MH_OK) fprintf(f, "Could not deinit MiniHook\n");
presentCallbackUnregister(presentCallback);
keyboardHandlerUnregister(OnKeyboardMessage);
//scriptUnregister(hinstance);
break;
}
fclose(f);
return TRUE;
}
void OnKeyboardMessage(DWORD key, WORD repeats, BYTE scanCode, BOOL isExtended, BOOL isWithAlt, BOOL wasDownBefore, BOOL isUpNow)
{
if(key == 'L' && !wasDownBefore && !isUpNow)
{
auto f = fopen("depth.raw", "w");
void* buf;
int size = export_get_depth_buffer(&buf);
fwrite(buf, 1, size, f);
fclose(f);
f = fopen("stencil.raw", "w");
size = export_get_stencil_buffer(&buf);
fwrite(buf, 1, size, f);
fclose(f);
f = fopen("color.raw", "w");
size = export_get_color_buffer(&buf);
fwrite(buf, 1, size, f);
fclose(f);
}
}
template<int offset, typename T>
static void* orig;
template<int offset, typename T>
static void* targets;
template<int offset, typename T>
void hook_function(T* inst, void* hook, bool unhook = false)
{
//__debugbreak();
void** vtbl = *reinterpret_cast<void***>(inst);
FILE* f = fopen("GTANativePlugin.log", "a");
//fprintf(f, "Hooking %p at offset %d\n", inst, offset);
MH_STATUS res = MH_OK;
DWORD oldProt = 0;
vtbl += offset;
//VirtualProtect(vtbl, 8, PAGE_READWRITE, &oldProt);
if (unhook)
{
res = MH_DisableHook(vtbl);
if(res != MH_OK) fprintf(f, "error %d disabling hook at offset %d\n", res, offset);
orig<offset, T> = nullptr;
}
else {
if(targets<offset, T> != nullptr && targets<offset, T> != *vtbl)
{
fprintf(f, "detected target change, someone else is screwing with our functions\n");
res = MH_DisableHook(targets<offset, T>);
if (res != MH_OK) fprintf(f, "errof %d disabling hook at offset %d\n", res, offset);
res = MH_RemoveHook(targets<offset, T>);
if (res != MH_OK) fprintf(f, "error %d removing hook at offset %d\n", res, offset);
targets<offset, T> = nullptr;
orig<offset, T> = nullptr;
}
if (orig<offset, T> == nullptr && targets<offset, T> != *vtbl) {
res = MH_CreateHook(*vtbl, hook, &(orig<offset, T>));
if(res != MH_OK) fprintf(f, "error %d creating hook at offset %d\n",res, offset);
}
if (targets<offset, T> != *vtbl) {
res = MH_EnableHook(*vtbl);
if (res != MH_OK) fprintf(f, "error %d enabling hook at offset %d\n", res, offset);
targets<offset, T> = *vtbl;
}
//*vtbl = reinterpret_cast<long long>(hook);
}
//VirtualProtect(vtbl, 8, oldProt, nullptr);
//fprintf(f, "clear_hook: %p\n", hook);
//fprintf(f, "clearFn: %p\n", (void*)(*(*reinterpret_cast<long long**>(inst) + 50)));
fclose(f);
}
template<int offset, typename T>
void unhook_function(T* inst)
{
hook_function<offset>(inst, nullptr, true);
}
void draw_hook_impl()
{
FILE* f = fopen("DrawLog.log", "a");
fprintf(f, "Draw Call\n");
fclose(f);
}
void draw_indexed_hook(ID3D11DeviceContext* self, UINT indexCount, UINT startLoc, UINT baseLoc) {
auto origMethod = reinterpret_cast<decltype(draw_indexed_hook)*>(orig<drawIndexedOffset, ID3D11DeviceContext>);
HRESULT hr;
ComPtr<ID3D11VertexShader> vs;
self->VSGetShader(&vs, nullptr, nullptr);
ComPtr<ID3D11Buffer> buf;
ComPtr<ID3D11Device> dev;
self->GetDevice(&dev);
self->VSGetConstantBuffers(1, 1, &buf);
if (buf != nullptr && draw_indexed_count == 1000) {
lastConstants = buf;
ExtractConstantBuffer(dev.Get(), self, buf.Get());
}
draw_indexed_count += 1;
origMethod(self, indexCount, startLoc, baseLoc);
}
void clear_render_target_view_hook(ID3D11DeviceContext* self, ID3D11RenderTargetView* rtv, float color[4])
{
auto origMethod = reinterpret_cast<void (*)(ID3D11DeviceContext*, ID3D11RenderTargetView*, float[4])>(orig<50, ID3D11DeviceContext>);
ComPtr<ID3D11RenderTargetView> curRTV;
self->OMGetRenderTargets(1, &curRTV, nullptr);
if (curRTV != nullptr)
{
D3D11_TEXTURE2D_DESC desc;
ComPtr<ID3D11Resource> res;
ComPtr<ID3D11Texture2D> tex;
curRTV->GetResource(&res);
HRESULT hr = S_OK;
hr = res.As(&tex);
if (hr != S_OK) return;
tex->GetDesc(&desc);
if (desc.Format == DXGI_FORMAT_B8G8R8A8_UNORM && desc.Width > 1000 && desc.Height > 1000) {
lastRtv = curRTV;
}
}
origMethod(self, rtv, color);
}
void clear_depth_stencil_view_hook(ID3D11DeviceContext* self, ID3D11DepthStencilView* dsv, UINT8 flags, float depth, UINT8 stencil)
{
auto origMethod = reinterpret_cast<decltype(&clear_depth_stencil_view_hook)>(orig<53, ID3D11DeviceContext>);
ComPtr<ID3D11DepthStencilView> curDSV;
self->OMGetRenderTargets(1, nullptr, &curDSV);
ComPtr<ID3D11Device> dev;
self->GetDevice(&dev);
if (curDSV != nullptr) {
D3D11_TEXTURE2D_DESC desc;
ComPtr<ID3D11Resource> res;
ComPtr<ID3D11Texture2D> tex;
curDSV->GetResource(&res);
HRESULT hr = S_OK;
hr = res.As(&tex);
if (hr != S_OK) return;
tex->GetDesc(&desc);
if (lastDsv == nullptr && desc.Format == DXGI_FORMAT_R32G8X24_TYPELESS && desc.Width > 1000 && desc.Height > 1000) {
lastDsv = curDSV;
ExtractDepthBuffer(dev.Get(), self, res.Get());
last_capture_depth = system_clock::now();
}
}
origMethod(self, dsv, flags, depth, stencil);
}
void presentCallback(void* chain)
{
draw_indexed_count = 0;
FILE* f = fopen(logFilePath, "a");
HRESULT hr = S_OK;
auto swapChain = static_cast<IDXGISwapChain*>(chain);
ComPtr<ID3D11Device> dev;
ComPtr<ID3D11DeviceContext> ctx;
hr = swapChain->GetDevice(__uuidof(ID3D11Device), &dev);
if (hr != S_OK) throw std::system_error(hr, std::system_category());
dev->GetImmediateContext(&ctx);
/*
if (swapChain != hooked_chain)
{
if(hooked_chain != nullptr)
{
unhook_function<50>(ctx.Get());
unhook_function<53>(ctx.Get());
}
ComPtr<ID3D10Multithread> multithread;
hr = ctx.As(&multithread);
if (hr != S_OK) throw std::system_error(hr, std::system_category());
multithread->SetMultithreadProtected(true);
hook_function<50>(ctx.Get(), &clear_render_target_view_hook);
hook_function<53>(ctx.Get(), &clear_depth_stencil_view_hook);
hooked_chain = swapChain;
}*/
ComPtr<ID3D11Multithread> multithread;
hr = ctx.As(&multithread);
if (hr != S_OK) throw std::system_error(hr, std::system_category());
multithread->SetMultithreadProtected(true);
hook_function<drawIndexedOffset>(ctx.Get(), &draw_indexed_hook);
//hook_function<50>(ctx.Get(), &clear_render_target_view_hook);
hook_function<53>(ctx.Get(), &clear_depth_stencil_view_hook);
if (f == nullptr) throw std::system_error(errno, std::system_category());
ComPtr<ID3D11Resource> depthres;
ComPtr<ID3D11Resource> colorres;
ctx->OMGetRenderTargets(1, &lastRtv, nullptr);
last_capture_color = system_clock::now();
lastRtv->GetResource(&colorres);
ExtractColorBuffer(dev.Get(), ctx.Get(), colorres.Get());
//lastDsv.Reset();
lastDsv = nullptr;
lastRtv = nullptr;
fclose(f);
}