-
Notifications
You must be signed in to change notification settings - Fork 103
/
Copy pathmain.h
706 lines (574 loc) · 20.6 KB
/
main.h
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
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
//d3d11 w2s base for some games by n7
#define SAFE_RELEASE(x) if (x) { x->Release(); x = NULL; }
typedef LRESULT(CALLBACK* WNDPROC)(HWND, UINT, WPARAM, LPARAM);
typedef uintptr_t PTR;
//globals
bool showmenu = false;
bool initonce = false;
//item states
bool wallhack = 1;
bool chams = 0;
bool esp = 1;
bool aimbot = 1;
int aimfov = 4;
int aimkey = 0;
int aimsens = 2;
int aimspeed_isbasedon_distance = 1;
float AimSpeed;
int aimspeed = 0; //5 slow dont move mouse faster than 5 pixel, 100 fast
int aimheight = 0; //aim height value
bool autoshoot = 0; //autoshoot
int as_xhairdst = 7.0; //autoshoot activates below this crosshair distance
DWORD Daimkey = VK_RBUTTON; //aimkey
bool IsPressed = false;
bool targetfound = false;
//int preaim = 0; //preaim to not aim behind
bool modelrecfinder = 1;
int modelfindmode = 1;
int countStride = -1;
int countIndexCount = -1;
int countveWidth = -1;
int countpscWidth = -1;
bool dumpshader = 0;
bool wtsfinder = 1;
int check_draw_result=0;
int check_drawindexed_result = 0;
int check_drawindexedinstanced_result = 0;
//rendertarget
ID3D11RenderTargetView* mainRenderTargetViewD3D11;
//wh
ID3D11DepthStencilState* DepthStencilState_TRUE = NULL; //depth off
ID3D11DepthStencilState* DepthStencilState_FALSE = NULL; //depth off
ID3D11DepthStencilState* DepthStencilState_ORIG = NULL; //depth on
//shader
ID3D11PixelShader* sGreen = NULL;
ID3D11PixelShader* sMagenta = NULL;
//pssetshaderresources
//D3D11_SHADER_RESOURCE_VIEW_DESC Descr;
//D3D11_TEXTURE2D_DESC texdesc;
//Viewport
float ViewportWidth;
float ViewportHeight;
float ScreenCenterX;
float ScreenCenterY;
int countnum = -1;
int g_dwLastAction = 0;
int vscStartSlot = 0;
int validvscStartSlot = 0;
bool method1 = 1;
bool method2 = 0;
bool method3 = 0;
bool method4 = 0;
//==========================================================================================================================
//get dir
using namespace std;
#include <fstream>
// getdir & log
char dlldir[320];
char* GetDirFile(char* name)
{
static char pldir[320];
strcpy_s(pldir, dlldir);
strcat_s(pldir, name);
return pldir;
}
//log
void Log(const char* fmt, ...)
{
if (!fmt) return;
char text[4096];
va_list ap;
va_start(ap, fmt);
vsprintf_s(text, fmt, ap);
va_end(ap);
ofstream logfile((PCHAR)"log.txt", ios::app);
if (logfile.is_open() && text) logfile << text << endl;
logfile.close();
}
#include <D3Dcompiler.h> //generateshader
#pragma comment(lib, "D3dcompiler.lib")
//generate shader func
HRESULT GenerateShader(ID3D11Device * pDevice, ID3D11PixelShader * *pShader, float r, float g, float b)
{
char szCast[] = "struct VS_OUT"
"{"
" float4 Position : SV_Position;"
" float4 Color : COLOR0;"
"};"
"float4 main( VS_OUT input ) : SV_Target"
"{"
" float4 col;"
" col.a = 1.0f;"
" col.r = %f;"
" col.g = %f;"
" col.b = %f;"
" return col;"
"}";
ID3D10Blob* pBlob;
char szPixelShader[1000];
sprintf_s(szPixelShader, szCast, r, g, b);
ID3DBlob* error;
HRESULT hr = D3DCompile(szPixelShader, sizeof(szPixelShader), "shader", NULL, NULL, "main", "ps_4_0", NULL, NULL, &pBlob, &error);
if (FAILED(hr))
return hr;
hr = pDevice->CreatePixelShader((DWORD*)pBlob->GetBufferPointer(), pBlob->GetBufferSize(), NULL, pShader);
if (FAILED(hr))
return hr;
return S_OK;
}
ID3D11Texture2D* texc = nullptr;
ID3D11ShaderResourceView* textureColor;
void GenerateTexture(uint32_t pixelcolor, DXGI_FORMAT format)//DXGI_FORMAT_R32G32B32A32_FLOAT DXGI_FORMAT_R8G8B8A8_UNORM
{
//static const uint32_t pixelcolor = 0xff00ff00; //0xff00ff00 green, 0xffff0000 blue, 0xff0000ff red
D3D11_SUBRESOURCE_DATA initData = { &pixelcolor, sizeof(uint32_t), 0 };
D3D11_TEXTURE2D_DESC desc;
desc.Width = 1;
desc.Height = 1;
desc.MipLevels = 1;
desc.ArraySize = 1;
desc.Format = format;
desc.SampleDesc.Count = 1;
desc.SampleDesc.Quality = 0;
desc.Usage = D3D11_USAGE_DEFAULT;
desc.BindFlags = D3D11_BIND_RENDER_TARGET | D3D11_BIND_SHADER_RESOURCE;
desc.CPUAccessFlags = 0;
desc.MiscFlags = 0;
pDevice->CreateTexture2D(&desc, &initData, &texc);
D3D11_SHADER_RESOURCE_VIEW_DESC srdes;
memset(&srdes, 0, sizeof(srdes));
srdes.Format = format;
srdes.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
srdes.Texture2D.MostDetailedMip = 0;
srdes.Texture2D.MipLevels = 1;
pDevice->CreateShaderResourceView(texc, &srdes, &textureColor);
}
void AimAtPos(float x, float y)
{
float TargetX = 0;
float TargetY = 0;
//X Axis
if (x != 0)
{
if (x > ScreenCenterX)
{
TargetX = -(ScreenCenterX - x);
TargetX /= AimSpeed;
if (TargetX + ScreenCenterX > ScreenCenterX * 2) TargetX = 0;
}
if (x < ScreenCenterX)
{
TargetX = x - ScreenCenterX;
TargetX /= AimSpeed;
if (TargetX + ScreenCenterX < 0) TargetX = 0;
}
}
//Y Axis
if (y != 0)
{
if (y > ScreenCenterY)
{
TargetY = -(ScreenCenterY - y);
TargetY /= AimSpeed;
if (TargetY + ScreenCenterY > ScreenCenterY * 2) TargetY = 0;
}
if (y < ScreenCenterY)
{
TargetY = y - ScreenCenterY;
TargetY /= AimSpeed;
if (TargetY + ScreenCenterY < 0) TargetY = 0;
}
}
if (aimspeed > 0)
{
//dont move mouse more than 50 pixels at time (ghetto HFR)
float dirX = TargetX > 0 ? 1.0f : -1.0f;
float dirY = TargetY > 0 ? 1.0f : -1.0f;
TargetX = dirX * fmin(aimspeed, abs(TargetX));
TargetY = dirY * fmin(aimspeed, abs(TargetY));
mouse_event(MOUSEEVENTF_MOVE, (float)TargetX, (float)TargetY, NULL, NULL);
}
else
if (TargetX != 0 && TargetY != 0)
mouse_event(MOUSEEVENTF_MOVE, (float)TargetX, (float)TargetY, NULL, NULL);
}
//==========================================================================================================================
//w2s stuff
struct vec2
{
float x, y;
};
struct vec3
{
float x, y, z;
};
struct vec4
{
float x, y, z, w;
};
#include <wrl/client.h> // For ComPtr
// Convenience macros
#define SAFE_RELEASE(p) if (p) { (p)->Release(); (p) = nullptr; }
// Using smart pointers to manage COM objects
using Microsoft::WRL::ComPtr;
// Function prototypes
void MapBuffer(ID3D11Buffer* pStageBuffer, void** ppData, UINT* pByteWidth);
void UnmapBuffer(ID3D11Buffer* pStageBuffer);
ComPtr<ID3D11Buffer> CopyBufferToCpu(ID3D11Buffer* pBuffer);
float GetDst(float Xx, float Yy, float xX, float yY);
void AddModel(ID3D11DeviceContext* pContext);
// Data structures
struct AimEspInfo_t {
float vOutX, vOutY, vOutZ;
float CrosshairDst;
};
// Globals
std::vector<AimEspInfo_t> AimEspInfo;
float matWorldView[4][4];
float matProj[4][4];
float* worldview = nullptr;
float* proj = nullptr;
// Functions
void MapBuffer(ID3D11Buffer* pStageBuffer, void** ppData, UINT* pByteWidth) {
if (!pStageBuffer || !ppData) {
Log("Invalid parameters passed to MapBuffer\n");
return;
}
D3D11_MAPPED_SUBRESOURCE subRes = {};
HRESULT res = pContext->Map(pStageBuffer, 0, D3D11_MAP_READ, 0, &subRes);
if (FAILED(res)) {
Log("Map failed: HRESULT 0x%08X\n", res);
return;
}
*ppData = subRes.pData;
if (pByteWidth) {
D3D11_BUFFER_DESC desc = {};
pStageBuffer->GetDesc(&desc);
*pByteWidth = desc.ByteWidth;
}
}
void UnmapBuffer(ID3D11Buffer* pStageBuffer) {
if (pStageBuffer) {
pContext->Unmap(pStageBuffer, 0);
}
}
ComPtr<ID3D11Buffer> CopyBufferToCpu(ID3D11Buffer* pBuffer) {
if (!pBuffer) return nullptr;
D3D11_BUFFER_DESC desc = {};
pBuffer->GetDesc(&desc);
D3D11_BUFFER_DESC stagingDesc = desc;
stagingDesc.BindFlags = 0;
stagingDesc.CPUAccessFlags = D3D11_CPU_ACCESS_READ;
stagingDesc.Usage = D3D11_USAGE_STAGING;
ComPtr<ID3D11Buffer> pStagingBuffer;
if (FAILED(pDevice->CreateBuffer(&stagingDesc, nullptr, &pStagingBuffer))) {
Log("Failed to create staging buffer\n");
return nullptr;
}
if (pStagingBuffer != NULL)
pContext->CopyResource(pStagingBuffer.Get(), pBuffer);
return pStagingBuffer;
}
float GetDst(float Xx, float Yy, float xX, float yY) {
return std::sqrt((yY - Yy) * (yY - Yy) + (xX - Xx) * (xX - Xx));
}
//w2s
int WorldViewCBnum = 2; //1
int ProjCBnum = 1;//2
int matProjnum = 16;//16
void AddModel(ID3D11DeviceContext* pContext) {
ComPtr<ID3D11Buffer> pWorldViewCB, pProjCB;
pContext->VSGetConstantBuffers(WorldViewCBnum, 1, pWorldViewCB.GetAddressOf()); // WorldViewCBnum
pContext->VSGetConstantBuffers(ProjCBnum, 1, pProjCB.GetAddressOf()); // ProjCBnum
if (!pWorldViewCB || !pProjCB) return;
ComPtr<ID3D11Buffer> m_pCurWorldViewCB = CopyBufferToCpu(pWorldViewCB.Get());
ComPtr<ID3D11Buffer> m_pCurProjCB = CopyBufferToCpu(pProjCB.Get());
if (!m_pCurWorldViewCB || !m_pCurProjCB) return;
MapBuffer(m_pCurWorldViewCB.Get(), (void**)&worldview, nullptr);
std::memcpy(matWorldView, &worldview[0], sizeof(matWorldView));
UnmapBuffer(m_pCurWorldViewCB.Get());
MapBuffer(m_pCurProjCB.Get(), (void**)&proj, nullptr);
std::memcpy(matProj, &proj[matProjnum], sizeof(matProj)); // matProjnum
UnmapBuffer(m_pCurProjCB.Get());
if (method1 == 1)
{
DirectX::XMVECTOR Pos = DirectX::XMVectorSet(0.0f, 0.0f, aimheight, 1.0f);
DirectX::XMMATRIX viewProjMatrix = DirectX::XMMatrixMultiply(
DirectX::XMLoadFloat4x4(reinterpret_cast<DirectX::XMFLOAT4X4*>(matWorldView)),
DirectX::XMLoadFloat4x4(reinterpret_cast<DirectX::XMFLOAT4X4*>(matProj))
);
DirectX::XMMATRIX WorldViewProj = viewProjMatrix;
float mx = Pos.m128_f32[0] * WorldViewProj.r[0].m128_f32[0] +
Pos.m128_f32[1] * WorldViewProj.r[1].m128_f32[0] +
Pos.m128_f32[2] * WorldViewProj.r[2].m128_f32[0] +
WorldViewProj.r[3].m128_f32[0];
float my = Pos.m128_f32[0] * WorldViewProj.r[0].m128_f32[1] +
Pos.m128_f32[1] * WorldViewProj.r[1].m128_f32[1] +
Pos.m128_f32[2] * WorldViewProj.r[2].m128_f32[1] +
WorldViewProj.r[3].m128_f32[1];
float mw = Pos.m128_f32[0] * WorldViewProj.r[0].m128_f32[3] +
Pos.m128_f32[1] * WorldViewProj.r[1].m128_f32[3] +
Pos.m128_f32[2] * WorldViewProj.r[2].m128_f32[3] +
WorldViewProj.r[3].m128_f32[3];
float xx = ((mx / mw) * (ViewportWidth / 2.0f)) + (ViewportWidth / 2.0f);
float yy = (ViewportHeight / 2.0f) - ((my / mw) * (ViewportHeight / 2.0f));
AimEspInfo_t pAimEspInfo = { xx, yy, mw };
AimEspInfo.push_back(pAimEspInfo);
}
if(method2==1)
{
DirectX::XMVECTOR Pos = XMVectorSet(0.0f, 0.0f, aimheight, 1.0f);
DirectX::XMMATRIX viewProjMatrix = DirectX::XMMatrixMultiply((FXMMATRIX)*matWorldView, (FXMMATRIX)*matProj);//multipication order matters
//transpose
DirectX::XMMATRIX WorldViewProj = DirectX::XMMatrixTranspose(viewProjMatrix); //transpose
float mx = Pos.m128_f32[0] * WorldViewProj.r[0].m128_f32[0] + Pos.m128_f32[1] * WorldViewProj.r[1].m128_f32[0] + Pos.m128_f32[2] * WorldViewProj.r[2].m128_f32[0] + WorldViewProj.r[3].m128_f32[0];
float my = Pos.m128_f32[0] * WorldViewProj.r[0].m128_f32[1] + Pos.m128_f32[1] * WorldViewProj.r[1].m128_f32[1] + Pos.m128_f32[2] * WorldViewProj.r[2].m128_f32[1] + WorldViewProj.r[3].m128_f32[1];
float mz = Pos.m128_f32[0] * WorldViewProj.r[0].m128_f32[2] + Pos.m128_f32[1] * WorldViewProj.r[1].m128_f32[2] + Pos.m128_f32[2] * WorldViewProj.r[2].m128_f32[2] + WorldViewProj.r[3].m128_f32[2];
float mw = Pos.m128_f32[0] * WorldViewProj.r[0].m128_f32[3] + Pos.m128_f32[1] * WorldViewProj.r[1].m128_f32[3] + Pos.m128_f32[2] * WorldViewProj.r[2].m128_f32[3] + WorldViewProj.r[3].m128_f32[3];
float xx, yy;
xx = ((mx / mw) * (ViewportWidth / 2.0f)) + (ViewportWidth / 2.0f);
yy = (ViewportHeight / 2.0f) - ((my / mw) * (ViewportHeight / 2.0f)); //- or + depends on the game
AimEspInfo_t pAimEspInfo = { static_cast<float>(xx), static_cast<float>(yy), static_cast<float>(mw) };
AimEspInfo.push_back(pAimEspInfo);
}
if (method3 == 1)
{
//TAB worldtoscreen unity 2018
DirectX::XMVECTOR Pos = XMVectorSet(0.0f, aimheight, 0.0f, 1.0);
//DirectX::XMVECTOR Pos = XMVectorSet(0.0f, aimheight, preaim, 1.0);
DirectX::XMMATRIX viewProjMatrix = DirectX::XMMatrixMultiply((FXMMATRIX)*matWorldView, (FXMMATRIX)*matProj);//multipication order matters
//normal
DirectX::XMMATRIX WorldViewProj = viewProjMatrix; //normal
float mx = Pos.m128_f32[0] * WorldViewProj.r[0].m128_f32[0] + Pos.m128_f32[1] * WorldViewProj.r[1].m128_f32[0] + Pos.m128_f32[2] * WorldViewProj.r[2].m128_f32[0] + WorldViewProj.r[3].m128_f32[0];
float my = Pos.m128_f32[0] * WorldViewProj.r[0].m128_f32[1] + Pos.m128_f32[1] * WorldViewProj.r[1].m128_f32[1] + Pos.m128_f32[2] * WorldViewProj.r[2].m128_f32[1] + WorldViewProj.r[3].m128_f32[1];
float mz = Pos.m128_f32[0] * WorldViewProj.r[0].m128_f32[2] + Pos.m128_f32[1] * WorldViewProj.r[1].m128_f32[2] + Pos.m128_f32[2] * WorldViewProj.r[2].m128_f32[2] + WorldViewProj.r[3].m128_f32[2];
float mw = Pos.m128_f32[0] * WorldViewProj.r[0].m128_f32[3] + Pos.m128_f32[1] * WorldViewProj.r[1].m128_f32[3] + Pos.m128_f32[2] * WorldViewProj.r[2].m128_f32[3] + WorldViewProj.r[3].m128_f32[3];
if (mw > 1.0f)
{
float invw = 1.0f / mw;
mx *= invw;
my *= invw;
float x = ViewportWidth / 2.0f;
float y = ViewportHeight / 2.0f;
x += 0.5f * mx * ViewportWidth + 0.5f;
y += 0.5f * my * ViewportHeight + 0.5f;//-
mx = x;
my = y;
}
else
{
mx = -1.0f;
my = -1.0f;
}
AimEspInfo_t pAimEspInfo = { static_cast<float>(mx), static_cast<float>(my), static_cast<float>(mw) };
AimEspInfo.push_back(pAimEspInfo);
}
if (method4 == 1)
{
/*
//new unity incomplete, todo: fix matrix is flipped
//1, 2, 43
D3DXMATRIX matrix, m1;
D3DXVECTOR4 position;
D3DXVECTOR4 input;
D3DXMatrixMultiply(&matrix, (D3DXMATRIX*)matWorldView, (D3DXMATRIX*)matProj);
D3DXMatrixTranspose(&matrix, &matrix);
position.x = input.x * matrix._11 + input.y * matrix._12 + input.z * matrix._13 + input.w * matrix._14;
position.y = input.x * matrix._21 + input.y * matrix._22 + input.z * matrix._23 + input.w * matrix._24;
position.z = input.x * matrix._31 + input.y * matrix._32 + input.z * matrix._33 + input.w * matrix._34;
position.w = input.x * matrix._41 + input.y * matrix._42 + input.z * matrix._43 + input.w * matrix._44;
float xx, yy;
xx = ((position.x / position.w) * (ViewportWidth / 2.0f)) + (ViewportWidth / 2.0f);
yy = (ViewportHeight / 2.0f) + ((position.y / position.w) * (ViewportHeight / 2.0f));
AimEspInfo_t pAimEspInfo = { static_cast<float>(xx), static_cast<float>(yy), static_cast<float>(position.w) };
AimEspInfo.push_back(pAimEspInfo);
*/
}
}
#include <string>
#include <fstream>
void SaveCfg()
{
ofstream fout;
fout.open(GetDirFile("w2sf.ini"), ios::trunc);
fout << "Wallhack " << wallhack << endl;
fout << "Chams " << chams << endl;
fout << "Esp " << esp << endl;
fout << "Aimbot " << aimbot << endl;
fout << "Aimkey " << aimkey << endl;
fout << "Aimfov " << aimfov << endl;
fout << "Aimspeedisbasedondistance " << aimspeed_isbasedon_distance << endl;
fout << "Aimspeed " << aimspeed << endl;
fout << "Aimheight " << aimheight << endl;
fout << "Autoshoot " << autoshoot << endl;
fout << "AsXhairdst " << as_xhairdst << endl;
fout << "Modelrecfinder " << modelrecfinder << endl;
fout << "Wtsfinder " << wtsfinder << endl;
fout << "Method1 " << method1 << endl;
fout << "Method2 " << method2 << endl;
fout << "Method3 " << method3 << endl;
fout << "Method4 " << method4 << endl;
fout << "WorldViewCBnum " << WorldViewCBnum << endl;
fout << "ProjCBnum " << ProjCBnum << endl;
fout << "matProjnum " << matProjnum << endl;
fout.close();
}
void LoadCfg()
{
ifstream fin;
string Word = "";
fin.open(GetDirFile("w2sf.ini"), ifstream::in);
fin >> Word >> wallhack;
fin >> Word >> chams;
fin >> Word >> esp;
fin >> Word >> aimbot;
fin >> Word >> aimkey;
fin >> Word >> aimfov;
fin >> Word >> aimspeed_isbasedon_distance;
fin >> Word >> aimspeed;
fin >> Word >> aimheight;
fin >> Word >> autoshoot;
fin >> Word >> as_xhairdst;
fin >> Word >> modelrecfinder;
fin >> Word >> wtsfinder;
fin >> Word >> method1;
fin >> Word >> method2;
fin >> Word >> method3;
fin >> Word >> method4;
fin >> Word >> WorldViewCBnum;
fin >> Word >> ProjCBnum;
fin >> Word >> matProjnum;
fin.close();
}
#define CHECK_FATAL(cond, msg) do { if (!(cond)) { MessageBoxA(NULL, msg, "FATAL ERROR", 0); ExitProcess(1); } } while (0)
#define COUNTOF(arr) (sizeof(arr)/sizeof(0[arr]))
#include <d3dcommon.h>
static uint64_t FNV1a(const uint8_t* data, size_t size)
{
uint64_t hash = 14695981039346656037ULL;
for (size_t i = 0; i < size; i++)
{
hash ^= data[i];
hash *= 1099511628211ULL;
}
return hash;
}
//static BOOL dump;
static WCHAR d3d11_shaders[MAX_PATH];
static void ShaderDump(const char* type, const char* ext, uint64_t id, const void* data, DWORD size)
{
CreateDirectoryW(d3d11_shaders, NULL);
WCHAR path[1024];
//wsprintfW(path, L"D:\\dumpshader.hlsl", d3d11_shaders, type, id, ext);
wsprintfW(path, L"%s\\%S_%016I64x.%S", d3d11_shaders, type, id, ext);
HANDLE f = CreateFileW(path, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (f != INVALID_HANDLE_VALUE)
{
DWORD written;
BOOL ok = WriteFile(f, data, size, &written, NULL);
if (!ok || written != size)
{
// TODO: report error
CHECK_FATAL(0, "Error writing to file!");
}
CloseHandle(f);
}
else
{
// TODO: report error
CHECK_FATAL(0, "Error creating output file!");
}
}
static void ShaderDisassemble(const char* type, uint64_t id, const void* bytecode, DWORD bytecode_size)
{
UINT flags = D3D_DISASM_ENABLE_DEFAULT_VALUE_PRINTS | D3D_DISASM_ENABLE_INSTRUCTION_OFFSET;
ID3DBlob* blob;
HRESULT hr = D3DDisassemble(bytecode, bytecode_size, flags, "", &blob);
if (SUCCEEDED(hr))
{
// ID3D10Blob is same as ID3DBlob
//LPVOID buffer = ID3D10Blob_GetBufferPointer(blob); //C
LPVOID buffer = (DWORD*)blob->GetBufferPointer(); //C++
//SIZE_T buffer_size = ID3D10Blob_GetBufferSize(blob);
SIZE_T buffer_size = blob->GetBufferSize();
ShaderDump(type, "asm", id, buffer, (DWORD)buffer_size);
SAFE_RELEASE(blob);
}
}
static void ShaderHook(ID3D11Device* device, const char* type, const void** bytecode, SIZE_T* bytecode_size)
{
//Log("1");
//uint64_t id = FNV1a(*bytecode, *bytecode_size);
//uint64_t id = FNV1a((uint8_t *)bytecode, *bytecode_size); //crashtown
uint64_t id = 1;
//if (dump)
//{
//Log("2");
ShaderDump(type, "bin", id, *bytecode, (DWORD)*bytecode_size);
ShaderDisassemble(type, id, *bytecode, (DWORD)*bytecode_size);
//}
//Log("3");
WCHAR hlsl[1024];
//wsprintfW(hlsl, L"D:\\dumpshader.hlsl", d3d11_shaders, type, id);
wsprintfW(hlsl, L"%s\\%S_%016I64x.hlsl", d3d11_shaders, type, id);
//Log("4");
if (GetFileAttributesW(hlsl) != INVALID_FILE_ATTRIBUTES)
{
//Log("5");
//D3D_FEATURE_LEVEL level = ID3D11Device_GetFeatureLevel(device);
D3D_FEATURE_LEVEL level = device->GetFeatureLevel();
int version;
if (level == D3D_FEATURE_LEVEL_11_1 || level == D3D_FEATURE_LEVEL_11_0)
{
version = 50;
}
else if (level == D3D_FEATURE_LEVEL_10_1)
{
version = 41;
}
else if (level == D3D_FEATURE_LEVEL_10_0)
{
version = 40;
}
else
{
// TODO: report error
return;
}
char target[16];
wsprintfA(target, "%s_%u_%u", type, version / 10, version % 10);
ID3DBlob* code = NULL;
ID3DBlob* error = NULL;
HRESULT hr = D3DCompileFromFile(hlsl, NULL, NULL, "main", target, D3DCOMPILE_OPTIMIZATION_LEVEL2 | D3DCOMPILE_WARNINGS_ARE_ERRORS, 0, &code, &error);
if (error != NULL)
{
WCHAR txt[1024];
//wsprintfW(txt, L"D:\\errorhlsl.txt", d3d11_shaders, type, id);
wsprintfW(txt, L"%s\\%S_%016I64x.hlsl.txt", d3d11_shaders, type, id);
//const void* error_data = ID3D10Blob_GetBufferPointer(error);
const void* error_data = (DWORD*)error->GetBufferPointer();
//DWORD error_size = (DWORD)ID3D10Blob_GetBufferSize(error);
DWORD error_size = error->GetBufferSize();
HANDLE f = CreateFileW(txt, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (f != INVALID_HANDLE_VALUE)
{
DWORD written;
BOOL ok = WriteFile(f, error_data, error_size, &written, NULL);
if (!ok || written != error_size)
{
// TODO: report error
}
CloseHandle(f);
}
SAFE_RELEASE(error);
}
if (SUCCEEDED(hr) && code != NULL)
{
// do not release code blob, small memory "leak", but whatever
//*bytecode = ID3D10Blob_GetBufferPointer(code);
*bytecode = (DWORD*)code->GetBufferPointer();
//*bytecode_size = ID3D10Blob_GetBufferSize(code);
*bytecode_size = code->GetBufferSize();
}
}
}