-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcppdumputils.h
1280 lines (1201 loc) · 34.9 KB
/
cppdumputils.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
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#pragma once
#include <vector>
#include <string>
#include <sstream>
#include <d3d11.h>
#include <d3d11_1.h>
#include <d3d11_2.h>
#include <d3d11_3.h>
#include <d3d11_4.h>
#include <d3dcompiler.h>
#include <dxgi.h>
#include <dxgi1_2.h>
#include <dxgi1_3.h>
#include <dxgidebug.h>
#include <unordered_map>
#include <map>
#include <fstream>
#include <assert.h>
#include <mutex>
#include <atomic>
#include <thread>
#include <algorithm>
#include <memtrace.hpp>
#include <d3dutils.h>
/*static std::ofstream &out()
{
static bool init = false;
//std::ostringstream file;
static std::ofstream file("dump.cpp");
if (!init)
{
init = true;
file << "#include <cppdumputils.h>\n";
file << "extern HWND g_window;\n";
file << "#include <decls.inc>\n";
file << "typedef void(*Event)(void);\n";
file << "std::vector<std::pair<std::string, Event>> g_events = {\n";
file << "#include <table.inc>\n";
file << "};\n";
}
file.setf(std::ios::unitbuf);
return file;
}
static std::ofstream &declOut()
{
static std::ofstream file("decls.inc", std::ios_base::out | std::ios_base::binary);
file.setf(std::ios::unitbuf);
return file;
}
static std::ofstream &tableOut()
{
static std::ofstream file("table.inc", std::ios_base::out | std::ios_base::binary);
file.setf(std::ios::unitbuf);
return file;
}*/
/*static std::ofstream &blobOut()
{
static std::ofstream file("cppdump/blob", std::ios_base::out | std::ios_base::binary);
file.setf(std::ios::unitbuf);
return file;
}*/
static size_t &blobCounter()
{
static size_t counter = 0;
return counter;
}
std::mutex &getGlobalLock()
{
static std::mutex m;
return m;
}
#define GLOBAL_LOCK std::lock_guard<std::mutex> GLOBAL_LOCK_VAR(getGlobalLock())
std::atomic<bool> &getRecursionFlag(std::thread::id tid = std::this_thread::get_id())
{
static std::unordered_map<std::thread::id, std::atomic<bool>> m;
// could this be a race condtion? neeh
// when shit hits the fan this gotta be fixed
return m[tid];
}
static std::unordered_map<size_t, size_t> &getWrapTable()
{
static std::unordered_map<size_t, size_t> table;
return table;
}
static std::unordered_map<size_t, size_t> &getUnwrapTable()
{
static std::unordered_map<size_t, size_t> table;
return table;
}
template<typename T, typename WT>
T *getWrapper(T *t)
{
bool expectedRecursionFlag = false;
if (getRecursionFlag())
{
out() << "// getWrapper() recursion escape\n";
return t;
}
GLOBAL_LOCK;
auto &wt = getWrapTable();
auto fd = wt.find(reinterpret_cast<size_t>((void*)t));
if (fd == wt.end()) {
out() << "// getWrapper() table miss\n";
auto *wrap = new WT(t);
return reinterpret_cast<T*>(wrap);
}
else
{
out() << "// getWrapper() table hit\n";
auto *pWrap = (WT*)(*fd).second;
if (pWrap->CheckLifetime())
{
return (T*)(pWrap);
}
else
{
out() << "// getWrapper() stalled object\n";
auto *wrap = new WT(t);
return reinterpret_cast<T*>(wrap);
}
}
}
template<typename T>
T *unwrapUnsafe(T *t)
{
auto &uwt = getUnwrapTable();
auto fd = uwt.find(reinterpret_cast<size_t>((void*)t));
if (fd == uwt.end()) {
return t;
}
else
{
return reinterpret_cast<T*>((*fd).second);
}
}
template<typename T>
T *unwrap(T *t)
{
GLOBAL_LOCK;
return unwrapUnsafe(t);
}
std::ostream& operator<<(std::ostream& os, REFGUID guid) {
os << std::uppercase;
os.width(8);
os << std::hex << guid.Data1 << '-';
os.width(4);
os << std::hex << guid.Data2 << '-';
os.width(4);
os << std::hex << guid.Data3 << '-';
os.width(2);
os << std::hex
<< static_cast<short>(guid.Data4[0])
<< static_cast<short>(guid.Data4[1])
<< '-'
<< static_cast<short>(guid.Data4[2])
<< static_cast<short>(guid.Data4[3])
<< static_cast<short>(guid.Data4[4])
<< static_cast<short>(guid.Data4[5])
<< static_cast<short>(guid.Data4[6])
<< static_cast<short>(guid.Data4[7]);
os << std::nouppercase;
return os;
}
static std::vector<unsigned char> &getBlob()
{
static std::vector<unsigned char> blob(0x100);
return blob;
}
struct MapDesc
{
void *pData;
size_t size;
};
// pResource -> subresourceID -> MapDesc
static std::map<std::pair<ID3D11Resource *, UINT>, MemoryShadow> &getMapTable()
{
static std::map<std::pair<ID3D11Resource *, UINT>, MemoryShadow> table;
return table;
}
static size_t getEventNumber()
{
static size_t counter = 0u;
return counter++;
}
enum class ParamAnnot
{
_IN_,
_OUT_,
_INOUT_,
_IN_ARRAY_,
_OUT_ARRAY_,
_INOUT_ARRAY_
};
struct Param
{
std::string type;
std::string undertype;
std::string name;
ParamAnnot annot;
int ptrs;
bool isInterface;
size_t size;
size_t undersize;
std::string numparam;
};
struct Method
{
std::string retTy;
std::string name;
std::unordered_map<std::string, Param> params;
std::vector<std::string> paramsOrdered;
};
struct CppDumpState
{
size_t curFuncCounter = 0u;
size_t curCounter = 0u;
std::vector<std::string> allNames;
std::string cur_name = "dump0";
std::ostringstream source;
std::ostringstream header;
std::ostringstream table;
const size_t cacheSize = 0x1000000u;
size_t cacheOffset = 0u;
char *inMemBlob;
std::ofstream file;
CppDumpState():
file("cppdump/blob", std::ios_base::out | std::ios_base::binary)
{
inMemBlob = new char[cacheSize];
}
void dump()
{
// Assuming "cppdump/" exists in current working directory
{
//char buf[256];
//GetCurrentDirectoryA(256, buf);
std::ofstream file("cppdump/" + cur_name + ".cpp");
file << "#include <cpphelper.h>\n";
file << "extern HWND g_window;\n";
file << "#include \"" << cur_name << ".h\"\n";
file << "typedef void(*Event)(void);\n";
file << "std::vector<std::pair<std::string, Event>> g_" << cur_name << "_events = {\n";
file << "#include \"" << cur_name << ".tb\"\n";
file << "};\n";
file << source.str();
source = std::ostringstream();
}
{
std::ofstream file("cppdump/" + cur_name + ".h");
file << header.str();
header = std::ostringstream();
}
{
std::ofstream file("cppdump/" + cur_name + ".tb");
file << table.str();
table = std::ostringstream();
}
allNames.push_back(cur_name);
cur_name = "dump" + std::to_string(++curCounter);
}
void writeBlob(void const *pData, size_t size)
{
if (cacheSize < size)
{
if (cacheOffset)
{
file.write(inMemBlob, cacheOffset);
}
file.write((char*)pData, size);
}
else if (cacheOffset + size > cacheSize)
{
file.write(inMemBlob, cacheOffset);
cacheOffset = size;
memcpy(inMemBlob, pData, size);
}
else
{
memcpy(inMemBlob + cacheOffset, pData, size);
cacheOffset += size;
}
}
void addFunc(Method const &method, std::string const &funcName, std::string const &cppdump)
{
source << cppdump;
table << "{\"" << funcName << "\", " << funcName << "},\n";
header << "static void " << funcName << "();\n";
curFuncCounter++;
if (curFuncCounter > 10000)
{
curFuncCounter = 0;
dump();
}
}
~CppDumpState()
{
dump();
if (cacheOffset)
{
file.write(inMemBlob, cacheOffset);
}
delete inMemBlob;
{
std::ofstream file("cppdump/main.h");
file << "#include <vector>\n";
file << "typedef void(*Event)(void);\n";
for (auto const &name : allNames)
{
file << "extern std::vector<std::pair<std::string, Event>> g_" << name << "_events;\n";
}
file << "static std::vector<std::vector<std::pair<std::string, Event>>*> g_all_events = {\n";
for (auto const &name : allNames)
{
file << "&g_" << name << "_events,\n";
}
file << "};\n";
}
}
};
static CppDumpState &getDumpState()
{
static CppDumpState state;
return state;
}
static size_t serializePtr(void const *v, size_t size, int line)
{
if (!v)
{
return 0u;
}
static std::unordered_map<int, size_t> writtenBytesPerLine;
writtenBytesPerLine[line] += size;
size_t offset = blobCounter();
char pad[16] = { '#' };
size_t padSize = (0x10u - (offset & 0xfu)) & 0xfu;
if (padSize)
{
blobCounter() += padSize;
offset += padSize;
getDumpState().writeBlob(pad, padSize);
}
blobCounter() += size;
getDumpState().writeBlob((char*)v, size);
return offset;
}
template<typename T>
size_t serializePtr(T const *v, int line)
{
return serializePtr(v, sizeof(*v), line);
}
template<typename T>
size_t serializeRef(T const &v, int line)
{
return serializePtr(&v, sizeof(v), line);
}
static std::ostringstream &out()
{
return getDumpState().source;
}
struct Interface
{
std::string name;
std::unordered_map<std::string, Method> methods;
};
static std::unordered_map<std::string, Interface> &getGlobalObjTable()
{
static std::unordered_map<std::string, Interface> table;
return table;
}
struct TableCTX
{
Interface curInterface;
Method curMethod;
};
static TableCTX &getGlobalTableCTX()
{
static TableCTX ctx;
return ctx;
}
static void CLASS_BEGIN(std::string name)
{
auto &ctx = getGlobalTableCTX();
ctx.curInterface = { name, {} };
}
static void CLASS_END(std::string name)
{
auto &ctx = getGlobalTableCTX();
assert(ctx.curInterface.name == name);
getGlobalObjTable()[ctx.curInterface.name] = ctx.curInterface;
ctx.curInterface = {};
}
static void METHOD_BEGIN(std::string retTy, std::string name)
{
auto &ctx = getGlobalTableCTX();
ctx.curMethod = { retTy, name,{} };
}
static void METHOD_END(std::string retTy, std::string name)
{
auto &ctx = getGlobalTableCTX();
assert(ctx.curMethod.name == name);
ctx.curInterface.methods[name] = ctx.curMethod;
ctx.curMethod = {};
}
static void PARAM(std::string type, std::string undertype, std::string name, ParamAnnot annot,
int ptrs, bool inInterface, size_t size, size_t undersize, std::string numparam)
{
auto &ctx = getGlobalTableCTX();
ctx.curMethod.params[name] = {
type, undertype, name, annot, ptrs, inInterface, size, undersize, numparam
};
ctx.curMethod.paramsOrdered.push_back(name);
}
#define __INDENT__ "/*" << __LINE__ << "*/"
void printParamInit(std::stringstream &ss, Method const &method,
std::unordered_map<std::string, std::pair<void *, void *>> const ¶mValues, std::string paramName)
{
auto const ¶m = method.params.find(paramName)->second;
#if 0
if (param.annot != ParamAnnot::_IN_)
return;
#endif
auto pData = paramValues.find(paramName)->second;
if (param.annot == ParamAnnot::_IN_)
{
#if 1
#if 1
if (param.undertype == "D3D11_SUBRESOURCE_DATA")
{
if (method.name == "CreateBuffer")
{
D3D11_SUBRESOURCE_DATA *pSubres = *(D3D11_SUBRESOURCE_DATA**)pData.first;
D3D11_BUFFER_DESC *desc = *(D3D11_BUFFER_DESC**)paramValues.find("pDesc")->second.first;
if (desc && pSubres)
{
int num = desc->ByteWidth;
size_t hndl = serializePtr(pSubres, sizeof(*pSubres), __LINE__);
ss << __INDENT__ << param.undertype << " tmp_" << param.name
<< " = *getInBlobPtr<" << param.undertype << ">(" << hndl << ");\n";
size_t memhndl = serializePtr(pSubres->pSysMem, num, __LINE__);
ss << __INDENT__ << "tmp_" << param.name
<< ".pSysMem = getInBlobPtr<void>(" << memhndl << ");\n";
}
else
{
ss << __INDENT__ << param.type << " tmp_" << param.name
<< " = nullptr;\n";
}
return;
}
else
{
assert(false && "unsopported");
}
}
// It's an array of floats
if (method.name == "ClearRenderTargetView" && param.name == "ColorRGBA")
{
size_t hndl = serializePtr(*(void**)pData.first, param.undersize * 4, __LINE__);
ss << __INDENT__ << param.undertype << " tmp_" << param.name << "[4];\n";
ss << __INDENT__ << " memcpy(tmp_" << param.name << ", " <<
" getInBlobPtr<" << param.undertype << ">(" << hndl << "), 16u);\n";
return;
}
#endif
if (param.ptrs == 0)
{
#if 1
if (param.undertype == "D3D11_PRIMITIVE_TOPOLOGY")
{
ss << __INDENT__ << param.type << " tmp_" << param.name << " = (D3D11_PRIMITIVE_TOPOLOGY)" << *(D3D11_PRIMITIVE_TOPOLOGY*)pData.first << ";\n";
return;
}
else if (param.undertype == "DXGI_FORMAT")
{
ss << __INDENT__ << param.type << " tmp_" << param.name << " = (DXGI_FORMAT)" << *(DXGI_FORMAT*)pData.first << ";\n";
return;
}
else if (param.undertype == "INT")
{
ss << __INDENT__ << param.type << " tmp_" << param.name << " = " << *(INT*)pData.first << ";\n";
return;
}
else if (param.undertype == "UINT")
{
ss << __INDENT__ << param.type << " tmp_" << param.name << " = " << *(UINT*)pData.first << ";\n";
return;
}
else if (param.undertype == "HWND")
{
ss << __INDENT__ << param.type << " tmp_" << param.name << " = g_window;//(HWND)0x" << *(HWND*)pData.first << ";\n";
return;
}
else if (param.undertype == "GUID" || param.undertype == "IID")
{
auto guid = *(GUID*)pData.first;
ss << __INDENT__ << param.type << " tmp_" << param.name << " = {"
<< guid.Data1
<< ", " << guid.Data2
<< ", " << guid.Data3
<< ", {" << (int)guid.Data4[0]
<< ", " << (int)guid.Data4[1]
<< ", " << (int)guid.Data4[2]
<< ", " << (int)guid.Data4[3]
<< ", " << (int)guid.Data4[4]
<< ", " << (int)guid.Data4[5]
<< ", " << (int)guid.Data4[6]
<< ", " << (int)guid.Data4[7]
<< "}};\n";
return;
}
size_t hndl = serializePtr(pData.first, param.size, __LINE__);
ss << __INDENT__ << param.type << " tmp_" << param.name << " = getInBlobRef<" << param.undertype << ">(" << hndl << ");\n";
//assert(false && "unsopported");
#endif
}
else
#endif
#if 1
if (param.ptrs == 1)
{
if (*(void**)pData.first)
{
if (param.isInterface)
{
#if 1
ss << __INDENT__ << param.type << " tmp_" << param.name
<< " = getInterface<" << param.undertype << ">(0x" << *(void**)pData.first << ");\n";
#endif
}
else
{
#if 1
// It's an array although it's not an array in the spec
if (param.name == "pDesc" && method.name == "GetDisplayModeList")
{
auto num = *(UINT**)paramValues.find("pNumModes")->second.first;
if (!num || !*num)
{
ss << __INDENT__ << param.undertype << " tmp_" << param.name
<< " = nullptr;\n";
return;
}
size_t hndl = serializePtr(*(void**)pData.first, *num * param.undersize, __LINE__);
ss << __INDENT__ << param.undertype << " tmp_" << param.name << "[" << *num << "];\n";
ss << __INDENT__ << " memcpy(tmp_" << param.name << ", " <<
" getInBlobPtr<" << param.undertype << ">(" << hndl << "), " << *num * param.undersize << ");\n";
return;
}
size_t hndl = serializePtr(*(void**)pData.first, param.undersize, __LINE__);
ss << __INDENT__ << param.undertype << " tmp_" << param.name
<< " = *getInBlobPtr<" << param.undertype << ">(" << hndl << ");\n";
#endif
}
}
else
ss << __INDENT__ << param.type << " tmp_" << param.name << " = nullptr;\n";
}
else
#endif
if (param.ptrs == 2)
{
assert(false && "unsopported");
}
}
else if (param.annot == ParamAnnot::_INOUT_)
{
assert(param.ptrs == 1);
// we don't care if it wasn't provided
if (!*(void**)pData.first)
{
ss << __INDENT__ << param.type << " tmp_" << param.name
<< " = nullptr;\n";
return;
}
// we don't care about non interfaces either
if (!param.isInterface)
{
size_t hndl = serializePtr(*(void**)pData.first, param.undersize, __LINE__);
ss << __INDENT__ << param.undertype << " shadow_tmp_" << param.name << ";\n";
ss << __INDENT__ << param.type << " tmp_" << param.name << " = " <<
" &shadow_tmp_" << param.name << ";\n";
ss << __INDENT__ << " memcpy(tmp_" << param.name << ", " <<
" getInBlobPtr<" << param.undertype << ">(" << hndl << "), " << param.undersize << ");\n";
return;
}
assert(false && "unsopported");
/*
if (param.ptrs == 0)
{
assert(false && "unsopported");
}
else if (param.ptrs == 1)
{
if (param.undertype == "UINT")
{
if (pData && *(void**)pData)
{
auto ptr = (UINT*)pData;
ss << __INDENT__ << param.undertype << " v_tmp_" << param.name << " = " << << ";\n";
ss << __INDENT__ << param.undertype << " *tmp_" << param.name << " = " << " v_tmp_" << param.name << ";\n";
}
else
{
}
return;
}
}
else if (param.ptrs == 2)
{
assert(false && "unsopported");
}
*/
}
else if (param.annot == ParamAnnot::_IN_ARRAY_)
{
if (!*(void**)pData.first)
{
ss << __INDENT__ << param.type << " tmp_" << param.name
<< " = nullptr;\n";
return;
}
/* special case for
D3D11_SUBRESOURCE_DATA
D3D11_MAPPED_SUBRESOURCE
*/
if (param.undertype == "D3D11_SUBRESOURCE_DATA")
{
D3D11_SUBRESOURCE_DATA *pSubres = *(D3D11_SUBRESOURCE_DATA**)pData.first;
if (method.name == "CreateTexture1D")
{
assert(false && "unsopported");
}
else if (method.name == "CreateTexture2D")
{
D3D11_TEXTURE2D_DESC *desc = *(D3D11_TEXTURE2D_DESC**)paramValues.find("pDesc")->second.first;
int num = desc->ArraySize * desc->MipLevels;
ss << __INDENT__ << param.undertype << " tmp_" << param.name << "[" << num << "];\n";
assert(desc->ArraySize * desc->MipLevels);
//ss << __INDENT__ << "for (uint32_t i = 0; i < tmp_pDesc->ArraySize * tmp_pDesc->MipLevels; i++)\n";
for (uint32_t i = 0; i < desc->ArraySize; i++)
{
for (uint32_t j = 0; j < desc->MipLevels; j++)
{
int subresId = i * desc->MipLevels + j;
// How to calculate it properly for all types like BC2, BC7?
size_t resourceSize = _calcSubresourceSize(desc, subresId, pSubres[subresId].SysMemPitch, pSubres[subresId].SysMemSlicePitch);
assert(resourceSize);
size_t memhndl = serializePtr(pSubres[subresId].pSysMem, resourceSize, __LINE__);
size_t hndl = serializePtr(&pSubres[subresId], sizeof(*pSubres), __LINE__);
ss << __INDENT__ << "tmp_" << param.name
<< "[" << subresId << "]" << " = *getInBlobPtr<" << param.undertype << ">(" << hndl << ");\n";
ss << __INDENT__ << "tmp_" << param.name
<< "[" << subresId << "].pSysMem = getInBlobPtr<void>(" << memhndl << ");\n";
}
}
}
else if (method.name == "CreateTexture3D")
{
D3D11_TEXTURE3D_DESC *desc = *(D3D11_TEXTURE3D_DESC**)paramValues.find("pDesc")->second.first;
int num = desc->MipLevels;
ss << __INDENT__ << param.undertype << " tmp_" << param.name << "[" << num << "];\n";
assert(desc->MipLevels);
//ss << __INDENT__ << "for (uint32_t i = 0; i < tmp_pDesc->ArraySize * tmp_pDesc->MipLevels; i++)\n";
for (uint32_t j = 0; j < desc->MipLevels; j++)
{
int subresId = j;
// How to calculate it properly for all types like BC2, BC7?
size_t resourceSize = _calcSubresourceSize(desc, subresId, pSubres[subresId].SysMemPitch, pSubres[subresId].SysMemSlicePitch);
assert(resourceSize);
size_t memhndl = serializePtr(pSubres[subresId].pSysMem, resourceSize, __LINE__);
size_t hndl = serializePtr(&pSubres[subresId], sizeof(*pSubres), __LINE__);
ss << __INDENT__ << "tmp_" << param.name
<< "[" << subresId << "]" << " = *getInBlobPtr<" << param.undertype << ">(" << hndl << ");\n";
ss << __INDENT__ << "tmp_" << param.name
<< "[" << subresId << "].pSysMem = getInBlobPtr<void>(" << memhndl << ");\n";
}
//assert(false && "unsopported");
}
else
{
assert(false);
}
return;
}
auto numparam = method.params.find(param.numparam)->second;
assert(numparam.ptrs == 0 && (
numparam.undertype == "UINT"
|| numparam.undertype == "SIZE_T"
));
auto num = *(UINT*)paramValues.find(param.numparam)->second.first;
if (param.isInterface)
{
assert(param.ptrs == 2);
if (num)
{
ss << __INDENT__ << param.undertype << " *tmp_" << param.name
<< "[] = {\n";
void **pArr = *(void***)pData.first;
for (uint32_t i = 0; i < num; i++)
{
ss << __INDENT__ << "getInterface<" << param.undertype << ">(0x" << pArr[i] << "), \n";
}
ss << __INDENT__ << "};\n";
}
else
{
ss << __INDENT__ << param.type << " tmp_" << param.name
<< " = nullptr;\n";
}
return;
}
assert(param.ptrs == 1);
char *pBase = *(char**)pData.first;
if (param.undersize)
{
if (num)
{
if (param.name == "pInputElementDescs" && method.name == "CreateInputLayout")
{
ss << __INDENT__ << param.undertype << " tmp_" << param.name << "[" << num << "];\n";
for (uint32_t i = 0; i < num; i++)
{
size_t hndl = serializePtr(pBase + i * param.undersize, param.undersize, __LINE__);
ss << __INDENT__ << "tmp_" << param.name << "[" << i << "] = *getInBlobPtr<" << param.undertype << ">(" << hndl << "),\n";
}
for (uint32_t i = 0; i < num; i++)
{
D3D11_INPUT_ELEMENT_DESC desc = ((D3D11_INPUT_ELEMENT_DESC*)pBase)[i];
size_t hndl = serializePtr(desc.SemanticName, strlen(desc.SemanticName) + 1, __LINE__);
ss << __INDENT__ << "tmp_" << param.name << "[" << i << "].SemanticName = "
<< "getInBlobPtr<char>(" << hndl << ");\n";
}
return;
}
ss << __INDENT__ << param.undertype << " tmp_" << param.name << "[" << num << "] = {\n";
for (uint32_t i = 0; i < num; i++)
{
if (param.undertype == "INT")
{
ss << __INDENT__ << *(INT*)(pBase + i * param.undersize) << ",\n";
}
else if (param.undertype == "UINT")
{
ss << __INDENT__ << *(UINT*)(pBase + i * param.undersize) << ",\n";
}
else
{
size_t hndl = serializePtr(pBase + i * param.undersize, param.undersize, __LINE__);
ss << __INDENT__ << "getInBlobRef<" << param.undertype << ">(" << hndl << "),\n";
}
}
ss << __INDENT__ << "};\n";
}
else
{
ss << __INDENT__ << param.type << " tmp_" << param.name
<< " = nullptr;\n";
}
}
else
{
assert(param.undertype == "void");
size_t hndl = serializePtr(pBase, num, __LINE__);
ss << __INDENT__ << "char shadow_tmp_" << param.name << "[" << num << "];\n";
ss << __INDENT__ << param.type << " tmp_" << param.name << " = " <<
" (void*)shadow_tmp_" << param.name << ";\n";
ss << __INDENT__ << " memcpy(shadow_tmp_" << param.name << ", " <<
" getInBlobPtr<" << param.undertype << ">(" << hndl << "), " << num << ");\n";
}
//assert(false && "unsopported");
}
else if (param.annot == ParamAnnot::_OUT_)
{
if (!*(void**)pData.first)
{
ss << __INDENT__ << param.type << " tmp_" << param.name
<< " = nullptr;\n";
return;
}
if (param.ptrs == 0)
{
assert(false && "unsopported");
}
else if (param.ptrs == 1)
{
//assert(false && "unsopported");
ss << __INDENT__ << param.undertype << " tmp_" << param.name << ";\n";
}
else if (param.ptrs == 2)
{
ss << __INDENT__ << param.undertype << " *tmp_" << param.name << " = nullptr;\n";
}
}
else if (param.annot == ParamAnnot::_OUT_ARRAY_)
{
ss << __INDENT__ << param.undertype << " *tmp_" << param.name
<< "[0x80] = {};\n";
}
else if (param.annot == ParamAnnot::_INOUT_ARRAY_)
{
assert(false && "unsopported");
}
/*
if (param.annot == ParamAnnot::_IN_)
{
assert(param.ptrs == 0 || param.ptrs == 1);
if (param.ptrs == 1)
{
ss << __INDENT__ << param.type << " tmp_" << param.name << " = getInBlobRef<" << param.type << ">(" << item.second << ");\n";
}
else
{
ss << __INDENT__ << param.type << " tmp_" << param.name << " = getInBlobRef<" << param.type << ">(" << item.second << ");\n";
}
}
else if(param.annot == ParamAnnot::_INOUT_)
{
if (item.second)
{
if (param.ptrs == 2)
{
ss << __INDENT__ << param.type << " tmp_" << param.name << " = getInterface<" << param.undertype << ">(" << item.second << ");\n";
}
else if (param.ptrs == 1)
{
}
else
{
assert(false && "Unsupported");
}
}
else
ss << __INDENT__ << param.type << " tmp_" << param.name << ";\n";
}
*/
}
void printParam(std::stringstream &ss, Method const &method, Param const ¶m, std::pair<void *, void *> pData)
{
if (param.ptrs == 2)
{
if (param.annot == ParamAnnot::_INOUT_ARRAY_
|| param.annot == ParamAnnot::_IN_ARRAY_
|| param.annot == ParamAnnot::_OUT_ARRAY_
)
ss << __INDENT__ << " tmp_" << param.name;
else
ss << __INDENT__ << " &tmp_" << param.name;
}
else if (param.ptrs == 1)
{
if (param.isInterface)
ss << __INDENT__ << " tmp_" << param.name;
else
{
if (
param.annot == ParamAnnot::_IN_ARRAY_
|| param.annot == ParamAnnot::_INOUT_
)
ss << __INDENT__ << " tmp_" << param.name;
else
{
if (*(void**)pData.first)
{
if (
param.name == "pDesc" && method.name == "GetDisplayModeList"
|| param.name == "ColorRGBA" && method.name == "ClearRenderTargetView"
)
ss << __INDENT__ << " tmp_" << param.name;
else
ss << __INDENT__ << " &tmp_" << param.name;
}
else
ss << __INDENT__ << " tmp_" << param.name;
}
}
}
else
{
ss << __INDENT__ << " tmp_" << param.name;
}
}
void printParamFinale(std::stringstream &ss, Method const &method,
std::unordered_map<std::string, std::pair<void *, void *>> const ¶mValues, std::string paramName)
{
auto const ¶m = method.params.find(paramName)->second;
auto pData = paramValues.find(paramName)->second;
if (param.annot == ParamAnnot::_INOUT_)
{
// we don't care about non interfaces being returned
if (!param.isInterface)
{
return;
}
assert(false && "unsopported");
}
else if (param.annot == ParamAnnot::_OUT_)
{
if (param.undertype == "void" && param.ptrs ==2)
{
if (!*(void***)pData.second)
{
ss << __INDENT__ << "// " << param.name << " was nullptr\n";
return;
}
ss << __INDENT__ << "setInterface(0x" << **(void***)pData.second << ", tmp_" << param.name << ");\n";
return;
}
// we don't care about non interfaces being returned
if (!param.isInterface)
{
return;
}
assert(param.ptrs == 2);
assert(param.isInterface);
if (*(void***)pData.second)
ss << __INDENT__ << "setInterface(0x" << **(void***)pData.second << ", tmp_" << param.name << ");\n";
else
ss << __INDENT__ << "// " << param.name << " was null\n";
}
else if (param.annot == ParamAnnot::_OUT_ARRAY_)
{
// we don't care about non interfaces being returned
if (!param.isInterface)
{
return;
}
assert(param.ptrs == 2);
//assert(false && "unsopported");
// sometimes Get* methods return arrays of crap, ok let's handle this
if (!*(void**)pData.second)
{
return;
}
auto numparam = method.params.find(param.numparam)->second;
// It could've been size_t but we assume it's little endian and num < 2^31
uint32_t num = 0;
assert(
numparam.undertype == "UINT"
|| numparam.undertype == "SIZE_T"
);