-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathmain.cpp
More file actions
571 lines (457 loc) · 20 KB
/
main.cpp
File metadata and controls
571 lines (457 loc) · 20 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
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
//Made by @ImShotZz
//https://www.unknowncheats.me/forum/members/2116480.html
//d3d11 w2s for ut4 engine games by n7
//credits to @Griizz
#include <vector>
#include <sstream>
#include <chrono>
#include <memory>
#include <string>
#include <iomanip>
#include <d3d11.h>
#include <D3D11Shader.h>
#include <D3Dcompiler.h>
#pragma comment(lib, "D3dcompiler.lib")
#pragma comment(lib, "d3d11.lib")
#pragma comment(lib, "winmm.lib")
#include "MinHook/include/MinHook.h"
#include "global.h"
#include "bones.h"
#include "hooks.h"
#include "renderer.h"
#include "main.h"
#include "util.h"
#include <map>
#define FONT_SIZE 11.0f
#define FONT_OFFSET 16.0f
#define FONT_TYPE L"Verdana"
#define RED_COLOR Color{ 1.0f, 0.0f, 0.0f, 1.0f }
#define GREEN_COLOR Color{ 0.0f, 1.0f, 0.0f, 1.0f }
#define BLACK_COLOR Color{ 0.0f, 0.0f, 0.0f, 0.9f }
#define YELLOW_COLOR Color{ 1.0f, 1.0f, 0.0f, 1.0f }
#define TRANSPARENT_COLOR Color{ 1.0f, 1.0f, 1.0f, 0.1f }
void GetAddresses()
{
Global::BaseAddress = (DWORD_PTR)GetModuleHandle(NULL);
GetModuleInformation(GetCurrentProcess(), (HMODULE)Global::BaseAddress, &Global::info, sizeof(Global::info));
auto AddrUWorld = Util::FindPattern((PBYTE)Global::BaseAddress, Global::info.SizeOfImage, (PBYTE)"\x48\x89\x05\x00\x00\x00\x00\x0F\x28\xD6", "xxx????xxx", 0);
auto OffUWorld = *reinterpret_cast<uint32_t*>(AddrUWorld + 3);
Global::m_UWorld = reinterpret_cast<SDK::UWorld**>(AddrUWorld + 7 + OffUWorld);
auto AddrGObj = Util::FindPattern((PBYTE)Global::BaseAddress, Global::info.SizeOfImage, (PBYTE)"\x48\x8D\x0D\x00\x00\x00\x00\xE8\x00\x00\x00\x00\x89\x43\x04\x48\x83\xC4\x20\x5B\xC3", "xxx????x????xxxxxxxxx", 0);
auto OffGObj = *reinterpret_cast<uint32_t*>(AddrGObj + 3);
SDK::UObject::GObjects = reinterpret_cast<SDK::FUObjectArray*>(AddrGObj + 7 + OffGObj);
auto AddrGName = Util::FindPattern((PBYTE)Global::BaseAddress, Global::info.SizeOfImage, (PBYTE)"\x48\x8B\x35\x00\x00\x00\x00\x41\x0F\xB7\xC4\x4D\x8D\xA5\x00\x00\x00\x00\x49", "xxx????xxxxxxx????x", 0);
auto OffGName = *reinterpret_cast<uint32_t*>(AddrGName + 3);
SDK::FName::GNames = *reinterpret_cast<SDK::TNameEntryArray**>(AddrGName + 7 + OffGName);
Util::Engine::boneAddress = (DWORD_PTR)Util::FindPattern((PBYTE)Global::BaseAddress, Global::info.SizeOfImage, (PBYTE)"\x40\x53\x55\x57\x41\x56\x48\x81\xEC\x00\x00\x00\x00\x33\xED\x4D\x63\xF0", "xxxxxxxxx????xxxxx", 0);
}
DWORD WINAPI UpdateThread(LPVOID)
{
GetAddresses();
printf("UWorld: 0x%X\n", Global::m_UWorld);
printf("GObjects: 0x%X\n", SDK::UObject::GObjects);
printf("GNames: 0x%X\n", SDK::FName::GNames);
printf("Bones: 0x%X\n\n", Util::Engine::boneAddress);
while (true)
{
if ((*Global::m_UWorld) == nullptr)
continue;
Global::m_PersistentLevel = (*Global::m_UWorld)->PersistentLevel;
Global::m_LocalPlayer = (*Global::m_UWorld)->OwningGameInstance->LocalPlayers[0];
Global::m_AActors = &Global::m_PersistentLevel->AActors;
Sleep(1);
}
return NULL;
}
std::unique_ptr<Renderer> renderer;
void DrawEsp()
{
if (Global::m_PersistentLevel != nullptr && Global::m_LocalPlayer != nullptr && Global::m_LocalPlayer->PlayerController->AcknowledgedPawn != nullptr)
{
SDK::TArray<SDK::AActor*> actors = Global::m_PersistentLevel->AActors;
for (int i = 0; i < actors.Num(); i++)
{
SDK::AActor* actor = Global::m_PersistentLevel->AActors[i];
if (actor == nullptr || actor->RootComponent == nullptr)
continue;
if (actor->IsA(SDK::ABP_PlayerPirate_C::StaticClass()))
{
SDK::ABP_PlayerPirate_C* pawn = static_cast<SDK::ABP_PlayerPirate_C*>(actor);
if (pawn == nullptr || pawn->PlayerState == nullptr)
continue;
if (Util::IsLocalPlayer(actor))
continue;
SDK::APlayerState* state = static_cast<SDK::APlayerState*>(pawn->PlayerState);
if (!state->PlayerName.IsValid() || pawn->RootComponent == nullptr)
continue;
// text size
Vec2 size;
// Get Name
std::wstring playerName = state->PlayerName.c_str();
// Draw Box
SDK::FVector HeadPos, LeftFootPos, RightFootPos, BottomPos;
Util::Engine::GetBoneLocation(pawn->Mesh, &HeadPos, Head);
Util::Engine::GetBoneLocation(pawn->Mesh, &LeftFootPos, Foot_l);
Util::Engine::GetBoneLocation(pawn->Mesh, &RightFootPos, Foot_r);
BottomPos = Util::Vector::Divide(Util::Vector::Add(LeftFootPos, RightFootPos), 2);
HeadPos.Z += 20.f;
BottomPos.Z -= 20.f;
SDK::FVector2D ScreenHead, ScreenBottom;
Global::m_LocalPlayer->PlayerController->ProjectWorldLocationToScreen(HeadPos, &ScreenHead);
Global::m_LocalPlayer->PlayerController->ProjectWorldLocationToScreen(BottomPos, &ScreenBottom);
//Util::Engine::WorldToScreen(Global::m_LocalPlayer->PlayerController, HeadPos, &ScreenHead);
//Util::Engine::WorldToScreen(Global::m_LocalPlayer->PlayerController, BottomPos, &ScreenBottom);
if (ScreenHead.X == 0 && ScreenHead.Y == 0)
continue;
float height = ScreenBottom.Y - ScreenHead.Y;
float width = height * 0.6f;
renderer->drawOutlinedRect(Vec4(ScreenHead.X - width / 2, ScreenHead.Y, width, height), 2.f, RED_COLOR, TRANSPARENT_COLOR);
// Get Distance
float distance = Util::GetDistance(Global::m_LocalPlayer->PlayerController->AcknowledgedPawn->RootComponent->RelativeLocation, HeadPos);
std::wstring distanceText = Util::DistanceToString(distance);
//Draw Name
size = renderer->getTextExtent(playerName, FONT_SIZE, FONT_TYPE);
renderer->drawText(Vec2(ScreenHead.X - size.x / 2, ScreenHead.Y - 13.f), playerName, RED_COLOR, 0, FONT_SIZE, FONT_TYPE);
// Draw Distance
size = renderer->getTextExtent(distanceText, FONT_SIZE, FONT_TYPE);
renderer->drawText(Vec2(ScreenHead.X - size.x / 2, ScreenHead.Y + height + 1.f), distanceText, RED_COLOR, 0, FONT_SIZE, FONT_TYPE);
}
/*else if (actor->IsA(SDK::ABP__C::StaticClass()))
{
SDK::ABMXTest_C* pawn = static_cast<SDK::ABMXTest_C*>(actor);
if (pawn == nullptr)
continue;
if (pawn->RootComponent == nullptr)
continue;
SDK::FVector pos = pawn->RootComponent->RelativeLocation;
SDK::FVector2D screenPos;
Global::m_LocalPlayer->PlayerController->ProjectWorldLocationToScreen(pos, &screenPos);
if (screenPos.X == 0 && screenPos.Y == 0)
continue;
// Get Distance
float distance = Util::GetDistance(Global::m_LocalPlayer->PlayerController->AcknowledgedPawn->RootComponent->RelativeLocation, pos);
std::wstring distanceText = Util::DistanceToString(distance);
// Draw Distance
Vec2 size = renderer->getTextExtent(distanceText, FONT_SIZE, FONT_TYPE);
renderer->drawText(Vec2(screenPos.X - size.x / 2, screenPos.Y), L"Bike [" + distanceText + L"]", YELLOW_COLOR, 0, FONT_SIZE, FONT_TYPE);
}*/
}
}
}
HRESULT __stdcall hookD3D11Present(IDXGISwapChain* pSwapChain, UINT SyncInterval, UINT Flags)
{
if (firstTime)
{
//get device
if (SUCCEEDED(pSwapChain->GetDevice(__uuidof(ID3D11Device), (void **)&pDevice)))
{
pSwapChain->GetDevice(__uuidof(pDevice), (void**)&pDevice);
pDevice->GetImmediateContext(&pContext);
}
//create depthstencilstate
D3D11_DEPTH_STENCIL_DESC stencilDesc;
stencilDesc.DepthFunc = D3D11_COMPARISON_LESS;
stencilDesc.StencilEnable = true;
stencilDesc.StencilReadMask = 0xFF;
stencilDesc.StencilWriteMask = 0xFF;
stencilDesc.FrontFace.StencilFailOp = D3D11_STENCIL_OP_KEEP;
stencilDesc.FrontFace.StencilDepthFailOp = D3D11_STENCIL_OP_INCR;
stencilDesc.FrontFace.StencilPassOp = D3D11_STENCIL_OP_KEEP;
stencilDesc.FrontFace.StencilFunc = D3D11_COMPARISON_ALWAYS;
stencilDesc.BackFace.StencilFailOp = D3D11_STENCIL_OP_KEEP;
stencilDesc.BackFace.StencilDepthFailOp = D3D11_STENCIL_OP_DECR;
stencilDesc.BackFace.StencilPassOp = D3D11_STENCIL_OP_KEEP;
stencilDesc.BackFace.StencilFunc = D3D11_COMPARISON_ALWAYS;
stencilDesc.DepthEnable = true;
stencilDesc.DepthWriteMask = D3D11_DEPTH_WRITE_MASK_ALL;
pDevice->CreateDepthStencilState(&stencilDesc, &myDepthStencilStates[static_cast<int>(eDepthState::ENABLED)]);
stencilDesc.DepthEnable = false;
stencilDesc.DepthWriteMask = D3D11_DEPTH_WRITE_MASK_ALL;
pDevice->CreateDepthStencilState(&stencilDesc, &myDepthStencilStates[static_cast<int>(eDepthState::DISABLED)]);
stencilDesc.DepthEnable = false;
stencilDesc.DepthWriteMask = D3D11_DEPTH_WRITE_MASK_ZERO;
stencilDesc.StencilEnable = false;
stencilDesc.StencilReadMask = UINT8(0xFF);
stencilDesc.StencilWriteMask = 0x0;
pDevice->CreateDepthStencilState(&stencilDesc,
&myDepthStencilStates[static_cast<int>(eDepthState::NO_READ_NO_WRITE)]);
stencilDesc.DepthEnable = true;
stencilDesc.DepthWriteMask = D3D11_DEPTH_WRITE_MASK_ALL; //
stencilDesc.DepthFunc = D3D11_COMPARISON_GREATER_EQUAL;
stencilDesc.StencilEnable = false;
stencilDesc.StencilReadMask = UINT8(0xFF);
stencilDesc.StencilWriteMask = 0x0;
stencilDesc.FrontFace.StencilFailOp = D3D11_STENCIL_OP_ZERO;
stencilDesc.FrontFace.StencilDepthFailOp = D3D11_STENCIL_OP_ZERO;
stencilDesc.FrontFace.StencilPassOp = D3D11_STENCIL_OP_KEEP;
stencilDesc.FrontFace.StencilFunc = D3D11_COMPARISON_EQUAL;
stencilDesc.BackFace.StencilFailOp = D3D11_STENCIL_OP_ZERO;
stencilDesc.BackFace.StencilDepthFailOp = D3D11_STENCIL_OP_ZERO;
stencilDesc.BackFace.StencilPassOp = D3D11_STENCIL_OP_ZERO;
stencilDesc.BackFace.StencilFunc = D3D11_COMPARISON_NEVER;
pDevice->CreateDepthStencilState(&stencilDesc, &myDepthStencilStates[static_cast<int>(eDepthState::READ_NO_WRITE)]);
//use the back buffer address to create the render target
if (SUCCEEDED(pSwapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), (LPVOID*)&RenderTargetTexture)))
{
pDevice->CreateRenderTargetView(RenderTargetTexture, nullptr, &RenderTargetView);
RenderTargetTexture->Release();
}
renderer = std::make_unique<Renderer>(pDevice);
firstTime = false;
}
// shaders
if (!psRed)
{
GenerateShader(pDevice, &psRed, 0.85f, 0.0f, 0.0f);
}
if (!psGreen)
{
GenerateShader(pDevice, &psGreen, 0.0f, 0.85f, 0.0f);
}
// viewport
pContext->RSGetViewports(&vps, &viewport);
ScreenCenterX = viewport.Width / 2.0f;
ScreenCenterY = viewport.Height / 2.0f;
// call before you draw
pContext->OMSetRenderTargets(1, &RenderTargetView, nullptr);
renderer->begin();
renderer->drawText(Vec2(14.0f, 10.0f), L"Made by Zanzo", RED_COLOR, 0, 12.0f, FONT_TYPE);
if ((*Global::m_UWorld) != nullptr)
DrawEsp();
renderer->draw();
renderer->end();
return phookD3D11Present(pSwapChain, SyncInterval, Flags);
}
//==========================================================================================================================
void __stdcall hookD3D11DrawIndexed(ID3D11DeviceContext* pContext, UINT IndexCount, UINT StartIndexLocation,
INT BaseVertexLocation)
{
//get stride & vdesc.ByteWidth
pContext->IAGetVertexBuffers(0, 1, &veBuffer, &Stride, &veBufferOffset);
if (veBuffer)
veBuffer->GetDesc(&vedesc);
if (veBuffer != nullptr)
{
veBuffer->Release();
veBuffer = nullptr;
}
//get indesc.ByteWidth
pContext->IAGetIndexBuffer(&inBuffer, &inFormat, &inOffset);
if (inBuffer)
inBuffer->GetDesc(&indesc);
if (inBuffer != nullptr)
{
inBuffer->Release();
inBuffer = nullptr;
}
//get pscdesc.ByteWidth
pContext->PSGetConstantBuffers(pscStartSlot, 1, &pcsBuffer);
if (pcsBuffer)
pcsBuffer->GetDesc(&pscdesc);
if (pcsBuffer != nullptr)
{
pcsBuffer->Release();
pcsBuffer = nullptr;
}
return phookD3D11DrawIndexed(pContext, IndexCount, StartIndexLocation, BaseVertexLocation);
}
//==========================================================================================================================
void __stdcall hookD3D11PSSetShaderResources(ID3D11DeviceContext* pContext, UINT StartSlot, UINT NumViews,
ID3D11ShaderResourceView*const * ppShaderResourceViews)
{
pssrStartSlot = StartSlot;
for (UINT j = 0; j < NumViews; j++)
{
//resources loop
ID3D11ShaderResourceView* pShaderResView = ppShaderResourceViews[j];
if (pShaderResView)
{
pShaderResView->GetDesc(&Descr);
ID3D11Resource* Resource;
pShaderResView->GetResource(&Resource);
auto* Texture = (ID3D11Texture2D *)Resource;
Texture->GetDesc(&texdesc);
}
}
return phookD3D11PSSetShaderResources(pContext, StartSlot, NumViews, ppShaderResourceViews);
}
//==========================================================================================================================
void __stdcall hookD3D11CreateQuery(ID3D11Device* pDevice, const D3D11_QUERY_DESC* pQueryDesc, ID3D11Query** ppQuery)
{
return phookD3D11CreateQuery(pDevice, pQueryDesc, ppQuery);
}
//==========================================================================================================================
void __stdcall hookD3D11DrawIndexedInstanced(ID3D11DeviceContext* pContext, UINT IndexCountPerInstance, UINT InstanceCount, UINT StartIndexLocation, INT BaseVertexLocation, UINT StartInstanceLocation)
{
if (GetAsyncKeyState(VK_F9) & 1)
return phookD3D11DrawIndexedInstanced(pContext, IndexCountPerInstance, InstanceCount, StartIndexLocation, BaseVertexLocation, StartInstanceLocation);
}
//==========================================================================================================================
void __stdcall hookD3D11Draw(ID3D11DeviceContext* pContext, UINT VertexCount, UINT StartVertexLocation)
{
if (GetAsyncKeyState(VK_F9) & 1)
return phookD3D11Draw(pContext, VertexCount, StartVertexLocation);
}
//==========================================================================================================================
void __stdcall hookD3D11DrawInstanced(ID3D11DeviceContext* pContext, UINT VertexCountPerInstance, UINT InstanceCount, UINT StartVertexLocation, UINT StartInstanceLocation)
{
if (GetAsyncKeyState(VK_F9) & 1)
return phookD3D11DrawInstanced(pContext, VertexCountPerInstance, InstanceCount, StartVertexLocation, StartInstanceLocation);
}
//==========================================================================================================================
void __stdcall hookD3D11DrawInstancedIndirect(ID3D11DeviceContext* pContext, ID3D11Buffer* pBufferForArgs, UINT AlignedByteOffsetForArgs)
{
if (GetAsyncKeyState(VK_F9) & 1)
return phookD3D11DrawInstancedIndirect(pContext, pBufferForArgs, AlignedByteOffsetForArgs);
}
//==========================================================================================================================
void __stdcall hookD3D11DrawIndexedInstancedIndirect(ID3D11DeviceContext* pContext, ID3D11Buffer* pBufferForArgs, UINT AlignedByteOffsetForArgs)
{
if (GetAsyncKeyState(VK_F9) & 1)
return phookD3D11DrawIndexedInstancedIndirect(pContext, pBufferForArgs, AlignedByteOffsetForArgs);
}
//==========================================================================================================================
void __stdcall hookD3D11VSSetConstantBuffers(ID3D11DeviceContext* pContext, UINT StartSlot, UINT NumBuffers, ID3D11Buffer*const * ppConstantBuffers)
{
if (ppConstantBuffers != nullptr)
vsStartSlot = StartSlot;
return phookD3D11VSSetConstantBuffers(pContext, StartSlot, NumBuffers, ppConstantBuffers);
}
//==========================================================================================================================
void __stdcall hookD3D11PSSetSamplers(ID3D11DeviceContext* pContext, UINT StartSlot, UINT NumSamplers, ID3D11SamplerState*const * ppSamplers)
{
if (ppSamplers != nullptr)
psStartSlot = StartSlot;
return phookD3D11PSSetSamplers(pContext, StartSlot, NumSamplers, ppSamplers);
}
//=========================================================================================================================
const int MultisampleCount = 1; // Set to 1 to disable multisampling
LRESULT CALLBACK DXGIMsgProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
DWORD __stdcall InitializeHook(LPVOID)
{
HMODULE hDXGIDLL = nullptr;
do
{
hDXGIDLL = GetModuleHandle("dxgi.dll");
Sleep(100);
}
while (!hDXGIDLL);
Sleep(100);
CreateThread(nullptr, 0, UpdateThread, nullptr, 0, nullptr);
IDXGISwapChain* pSwapChain;
WNDCLASSEXA wc = {
sizeof(WNDCLASSEX), CS_CLASSDC, DXGIMsgProc, 0L, 0L, GetModuleHandleA(nullptr), nullptr, nullptr, nullptr, nullptr,
"DX", nullptr
};
RegisterClassExA(&wc);
HWND hWnd = CreateWindowA("DX", nullptr, WS_OVERLAPPEDWINDOW, 100, 100, 300, 300, nullptr, nullptr, wc.hInstance,
nullptr);
D3D_FEATURE_LEVEL requestedLevels[] = {D3D_FEATURE_LEVEL_11_0, D3D_FEATURE_LEVEL_10_1};
D3D_FEATURE_LEVEL obtainedLevel;
ID3D11Device* d3dDevice = nullptr;
ID3D11DeviceContext* d3dContext = nullptr;
DXGI_SWAP_CHAIN_DESC scd;
ZeroMemory(&scd, sizeof(scd));
scd.BufferCount = 1;
scd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
scd.BufferDesc.Scaling = DXGI_MODE_SCALING_UNSPECIFIED;
scd.BufferDesc.ScanlineOrdering = DXGI_MODE_SCANLINE_ORDER_UNSPECIFIED;
scd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
scd.Flags = DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH;
scd.OutputWindow = hWnd;
scd.SampleDesc.Count = MultisampleCount;
scd.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;
scd.Windowed = ((GetWindowLongPtr(hWnd, GWL_STYLE) & WS_POPUP) != 0) ? false : true;
scd.BufferDesc.Width = 1;
scd.BufferDesc.Height = 1;
scd.BufferDesc.RefreshRate.Numerator = 0;
scd.BufferDesc.RefreshRate.Denominator = 1;
UINT createFlags = 0;
IDXGISwapChain* d3dSwapChain = nullptr;
if (FAILED(D3D11CreateDeviceAndSwapChain(
nullptr,
D3D_DRIVER_TYPE_HARDWARE,
nullptr,
createFlags,
requestedLevels,
sizeof(requestedLevels) / sizeof(D3D_FEATURE_LEVEL),
D3D11_SDK_VERSION,
&scd,
&pSwapChain,
&pDevice,
&obtainedLevel,
&pContext)))
{
MessageBox(hWnd, "Failed to create directX device and swapchain!", "Error", MB_ICONERROR);
return NULL;
}
pSwapChainVtable = (DWORD_PTR*)pSwapChain;
pSwapChainVtable = (DWORD_PTR*)pSwapChainVtable[0];
pContextVTable = (DWORD_PTR*)pContext;
pContextVTable = (DWORD_PTR*)pContextVTable[0];
pDeviceVTable = (DWORD_PTR*)pDevice;
pDeviceVTable = (DWORD_PTR*)pDeviceVTable[0];
if (MH_Initialize() != MH_OK)
{
return 1;
}
if (MH_CreateHook((DWORD_PTR*)pSwapChainVtable[8], hookD3D11Present,
reinterpret_cast<void**>(&phookD3D11Present)) != MH_OK)
{
return 1;
}
if (MH_EnableHook((DWORD_PTR*)pSwapChainVtable[8]) != MH_OK)
{
return 1;
}
if (MH_CreateHook((DWORD_PTR*)pContextVTable[12], hookD3D11DrawIndexed,
reinterpret_cast<void**>(&phookD3D11DrawIndexed)) != MH_OK)
{
return 1;
}
if (MH_EnableHook((DWORD_PTR*)pContextVTable[12]) != MH_OK)
{
return 1;
}
DWORD dwOld;
VirtualProtect(phookD3D11Present, 2, PAGE_EXECUTE_READWRITE, &dwOld);
while (true)
{
Sleep(10);
}
pDevice->Release();
pContext->Release();
pSwapChain->Release();
return NULL;
}
//==========================================================================================================================
BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
{
switch (fdwReason)
{
case DLL_PROCESS_ATTACH:
DisableThreadLibraryCalls(hinstDLL);
CreateThread(nullptr, 0, InitializeHook, nullptr, 0, nullptr);
break;
case DLL_PROCESS_DETACH:
if (MH_Uninitialize() != MH_OK)
return 1;
if (MH_DisableHook((DWORD_PTR*)pSwapChainVtable[8]) != MH_OK)
return 1;
if (MH_DisableHook((DWORD_PTR*)pContextVTable[12]) != MH_OK)
return 1;
if (MH_DisableHook((DWORD_PTR*)pContextVTable[13]) != MH_OK)
return 1;
if (MH_DisableHook((DWORD_PTR*)pContextVTable[8]) != MH_OK)
return 1;
if (MH_DisableHook((DWORD_PTR*)pContextVTable[7]) != MH_OK)
return 1;
if (MH_DisableHook((DWORD_PTR*)pContextVTable[10]) != MH_OK)
return 1;
break;
}
return TRUE;
}