This repository has been archived by the owner on Jun 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.cpp
757 lines (705 loc) · 22.1 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
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
#include "main.h"
//this is horrible, messy code.
//read https://github.com/nblockbuster/D2StaticDocs on the basics of statics
namespace fs = std::filesystem;
#pragma region Parsers
bool parseIndexBufferInMemory(int vertexCount, int fileSize)
{
//Submesh* sub;
//std::vector<std::vector<int16_t>> facesi16;
//std::vector<std::vector<uint32_t>> facesu32;
int e = 0;
int b = 0;
int l = 0;
int16_t face;
uint32_t u32face;
int n = vertexCount;
for (int i = 0; i < fileSize; i += 0x6) {
if (submesh->isU32) {
std::vector<uint32_t> uintFace;
uintFace.reserve(3);
for (int j = 0; j < 3; j++) {
memcpy((char*)&u32face, data + i + j * 4, 4);
uintFace.push_back(u32face);
}
i += 6;
if (u32face > vertexCount)
break;
//facesu32.push_back(uintFace);
submesh->facesu32.push_back(uintFace);
}
else {
std::vector<int16_t> intFace;
intFace.reserve(3);
for (int j = 0; j < 3; j++) {
memcpy((char*)&face, data + i + j * 2, 2);
intFace.push_back(face);
}
if (face > vertexCount)
break;
//facesi16.push_back(intFace);
submesh->faces.push_back(intFace);
}
if (lodCulling) {
if (b > fileSize / 3) {
if (fileSize > 12500) {
if (submesh->faces[l][0] + 8000 < submesh->faces[l - 2][0] && submesh->faces[l][1] + 8000 < submesh->faces[l - 2][1] && submesh->faces[l][2] + 8000 < submesh->faces[l - 2][2]) {
submesh->faces.resize(submesh->faces.size() - 1);
return false;
}
}
else if (fileSize > 5500) {
if (submesh->faces[l][0] + 3500 < submesh->faces[l - 2][0] && submesh->faces[l][1] + 3500 < submesh->faces[l - 2][1] && submesh->faces[l][2] + 3500 < submesh->faces[l - 2][2]) {
submesh->faces.resize(submesh->faces.size() - 1);
return false;
}
}
else if ((submesh->faces[l][0] + 200 < submesh->faces[l - 2][0] && submesh->faces[l][1] + 200 < submesh->faces[l - 2][1] && submesh->faces[l][2] + 200 < submesh->faces[l - 2][2])) {
submesh->faces.resize(submesh->faces.size() - 1);
return false;
}
}
l += 1;
b += 3;
/*
int c = 0;
if (submesh->lodsplit[c].off != (submesh->lodsplit[c-1].off + submesh->lodsplit[c-1].count)) {
c += 1;
std::cout << std::to_string(c) << "\n";
}
if (i == (submesh->lodsplit[c].off + submesh->lodsplit[c].count)) {
if (facesu32.size()) {
submesh->facesu32 = facesu32;
submeshes.push_back(submesh);
facesu32.clear();
c += 1;
}
else {
submesh->faces = facesi16;
submeshes.push_back(submesh);
facesi16.clear();
c+=1;
}
}
l += 1;
b += 3;
*/
}
}
//Submesh* submesh2 = new Submesh();
//submesh2->vertCol = submesh->vertCol;
//submesh2->vertColSlots = submesh->vertColSlots;
//submesh2->vertNorm = submesh->vertNorm;
//submesh2->vertPos = submesh->vertPos;
//submesh2->vertUV = submesh->vertUV;
//submesh2->faces = facesi16;
//submesh2->facesu32 = facesu32;
//submeshes.push_back(submesh2);
return true;
}
void parseVertexBufferInMemory(int fileSize, float scale)
{
int16_t x,y,z;
int16_t num;
for (int i = 0; i < fileSize; i += 0x10) {
std::vector<float> vertexpos;
vertexpos.reserve(3);
for (int j = 0; j < 3; j++)
{
memcpy((char*)&num, data + i + j * 2, 2);
vertexpos.push_back(((float)num / 32767) * scale);
}
submesh->vertPos.push_back(vertexpos);
}
}
void parseVertexNormalsInMemory(int fileSize) {
int16_t num;
uint16_t numW;
for (int i = 0x8; i < fileSize; i += 0x10) {
std::vector<float> norm;
norm.reserve(3);
for (int j = 0; j < 3; j++)
{
memcpy((char*)&num, data + i + j * 2, 2);
norm.push_back((float)num / 32767);
}
submesh->vertNorm.push_back(norm);
memcpy((char*)&numW, data + i + 6, 2);
submesh->vertNormW.push_back(numW);
}
}
void parseUVBufferInMemory(int fileSize, float uoff, float voff, float uscale, float vscale)
{
int16_t u, v;
for (int i = 0; i < fileSize; i += 0x4) {
std::vector<float> uvert;
uvert.reserve(2);
memcpy((char*)&u, data + i, 2);
memcpy((char*)&v, data + i + 2, 2);
//i need to fix this because it isnt right still
//how did i have this backwards??
//eh who cares
//OFF IS SCALE AND SCALE IS OFF NOW
float fixu = ((float)u / 32767) * uoff + uscale;// *0.08;
float fixv = ((float)v / 32767) * -voff + vscale;// *0.08;
//float fixu = ((float)u / 32767) * uscale + uoff;// *0.08;
//float fixv = ((float)v / 32767) * -vscale + voff;// *0.08;
uvert.push_back(fixu);
uvert.push_back(fixv);
submesh->vertUV.push_back(uvert);
}
}
void parseVCBufferInMemory(int fileSize) {
//literally just mont's vc code from MDE
bool bAnyValidVC = false;
for (int i = 0; i < fileSize; i += 4) {
int8_t val;
std::vector<float> vc;
for (int j = i; j < i + 4; j++) {
memcpy((char*)&val, data + j, 1);
if (!bAnyValidVC) {
if (val != 0) bAnyValidVC = true;
}
vc.push_back((float)val / 255);
}
submesh->vertCol.push_back(vc);
}
if (!bAnyValidVC) submesh->vertCol.clear();
}
#pragma endregion
static void show_usage()
{
std::cerr << "Usage: D2StaticExtractor -p [packages path] -o [output path] -i [input hash] -b [package ID] -f [filetype] -l -t"
<< std::endl;
std::cerr << "-i extracts a single main model hash\n";
std::cerr << "-b extracts all the static models available for that package ID. (-t is ignored)\n";
std::cerr << "-l disables hacky lod culling\n";
std::cerr << "-t extracts textures for given model\n";
std::cerr << "-f changes the file type that the textures are output to. (default PNG) Valid types:\n";
std::cerr << "bmp, jpg/jpeg, png, dds, tga, hdr, tif/tiff, wdp/hdp/jxr, ppm/pfm\n";
//Found here, under the file-type list: https://github.com/Microsoft/DirectXTex/wiki/Texconv#optional-switches-description
}
int main(int argc, char* argv[])
{
#pragma region Sarge
Sarge sarge;
sarge.setArgument("p", "pkgspath", "pkgs path", true);
sarge.setArgument("o", "outputpath", "output path", true);
sarge.setArgument("i", "inputhash", "hash of mainmodel file", true);
sarge.setArgument("b", "batch", "batch with pkg ID", true);
sarge.setArgument("l", "lodcull", "disable lod culling", false);
sarge.setArgument("t", "texex", "texture extraction", false);
sarge.setArgument("f", "filetype", "type to convert to", true);
sarge.setDescription("Destiny 2 static model extractor by nblock.");
sarge.setUsage("D2StaicExtractor");
if (!sarge.parseArguments(argc, argv))
{
std::cerr << "Couldn't parse arguments..." << std::endl;
show_usage();
return 1;
}
std::string pkgsPath, outputPath, batchPkg;
std::string modelHash = "";
std::string texTypeIn = "";
uint32_t sfhash32;
float scale = 1;
sarge.getFlag("pkgspath", pkgsPath);
sarge.getFlag("outputpath", outputPath);
sarge.getFlag("inputhash", modelHash);
sarge.getFlag("batch", batchPkg);
sarge.getFlag("filetype", texTypeIn);
lodCulling = true;
if (sarge.exists("lodcull"))
lodCulling = false;
if (pkgsPath == "" && outputPath == "")
show_usage();
packagesPath = pkgsPath;
#pragma endregion
#pragma region H64
std::unordered_map<uint64_t, uint32_t> hash64Table;
if (sarge.exists("texex")) {
std::ifstream f("h64");
if (f) {
hash64Table = loadH64Table();
if (hash64Table.size() < 10000) {
hash64Table = generateH64Table(pkgsPath);
}
}
else {
hash64Table = generateH64Table(pkgsPath);
saveH64Table(hash64Table);
}
}
#pragma endregion
if (modelHash != "") {
if (getReferenceFromHash(modelHash, packagesPath) != "446d8080") {
std::cout<<"Not a valid static model.\n";
exit(112);
}
outputPath += "/" + modelHash;
fs::create_directories(outputPath);
int fileSize;
hash = modelHash;
fileSize = getFile();
memcpy((char*)&sfhash32, data + 0x8, 4);
memcpy((void*)&scale, data + 0x3C, 4);
uint32_t extOff = 0;
hash = uint32ToHexStr(sfhash32);
fileSize = getFile();
uint32_t indexBuffer, vertexBuffer, uvBuffer, vcBuffer;
memcpy((char*)&indexBuffer, data + fileSize - 0x14, 4);
memcpy((char*)&vertexBuffer, data + fileSize - 0x10, 4);
memcpy((char*)&uvBuffer, data + fileSize - 0xC, 4);
memcpy((char*)&vcBuffer, data + fileSize - 0x8, 4);
float uoff, voff, uscale, vscale;
memcpy((void*)&uoff, data + 0x48, 4);
memcpy((void*)&voff, data + 0x4C, 4);
memcpy((void*)&uscale, data + 0x54, 4);
memcpy((void*)&vscale, data + 0x58, 4);
//This is very experimental and doesn't work yet.
/*
uint32_t val, amountLOD;
bool bFound = false;
extOff = fileSize -= 4;
while (true)
{
memcpy((char*)&val, data + extOff, 4);
if (val == 0x80806D37)
{
bFound = true;
extOff -= 8;
break;
}
extOff -= 4;
}
memcpy((char*)&amountLOD, data + extOff, 4);
extOff += 0x10;
int j = 0;
uint32_t splitnext_off;
uint32_t splitnext_count;
for (int o = extOff; o < extOff + (amountLOD * 12); o += 12) {
LODSplit split;
memcpy((char*)&split.off, data + o, 4);
memcpy((char*)&split.count, data + o + 4, 4);
memcpy((char*)&splitnext_off, data + o + 12, 4);
memcpy((char*)&splitnext_count, data + o + 16, 4);
std::cout << "CO: " + std::to_string(split.off) + " CC: " + std::to_string(split.count)
<< "\nNO: " + std::to_string(splitnext_off) + " NC: " + std::to_string(splitnext_count) << "\n";
if (j > 0 && submesh->lodsplit.size()) {
//if (split.off != (submesh->lodsplit[j - 1].off + submesh->lodsplit[j - 1].count))
//if (split.off == submesh->lodsplit[j - 1].off && split.count >)
// std::cout << "invalid LOD. SKIIIIIP\n";
//else
// submesh->lodsplit.push_back(split);
if (split.off == splitnext_off && split.count < splitnext_count) {
std::cout << "\ninvalid LOD or not LOD0. SKIIIIIP\n\n";
//o += 12;
}
else
submesh->lodsplit.push_back(split);
}
else if (split.off == splitnext_off && split.count < splitnext_count) {
std::cout << "\ninvalid LOD or not LOD0. SKIIIIIP\n\n";
//o += 12;
}
else
submesh->lodsplit.push_back(split);
j += 1;
}
*/
delete[] data;
hash = getReferenceFromHash(uint32ToHexStr(vertexBuffer), packagesPath);
fileSize = getFile();
if (scale == 0.000000)
scale = 1;
parseVertexBufferInMemory(fileSize, scale);
//parseVertexNormalsInMemory(fileSize);
delete[] data;
if (getReferenceFromHash(uint32ToHexStr(uvBuffer), packagesPath) != "ffffffff") {
hash = getReferenceFromHash(uint32ToHexStr(uvBuffer), packagesPath);
fileSize = getFile();
parseUVBufferInMemory(fileSize, uoff, voff, uscale, vscale);
delete[] data;
}
//uhhh this might work idk
hash = uint32ToHexStr(indexBuffer);
bool isu32 = false;
getFile();
memcpy((char*)&isu32, data + 1, 1);
submesh->isU32 = isu32;
delete[] data;
hash = getReferenceFromHash(uint32ToHexStr(indexBuffer), packagesPath);
fileSize = getFile();
parseIndexBufferInMemory(submesh->vertPos.size(), fileSize);
delete[] data;
/*
hash = getReferenceFromHash(uint32ToHexStr(vcBuffer), packagesPath);
if (hash != "ffffffff"){
fileSize = getFile();
parseVCBufferInMemory(fileSize);
delete[] data;
addVertColSlots(submesh);
}
*/
std::string fbxpath = outputPath + "/" + modelHash + ".fbx";
submesh->name = modelHash;
//submeshes.push_back(submesh);
//for (int i = 0; i < submeshes.size(); i++) {
// Submesh* sub = submeshes[i];
// sub->name = modelHash + "_" + std::to_string(i);
// FbxNode* node = fbxModel->addSubmeshToFbx(sub);
// nodes.push_back(node);
//}
FbxNode* node = fbxModel->addSubmeshToFbx(submesh);
nodes.push_back(node);
if (nodes.size()) {
for (auto& node : nodes) fbxModel->scene->GetRootNode()->AddChild(node);
fbxModel->save(fbxpath, false);
}
submesh->faces.clear();
submesh->facesu32.clear();
submesh->vertPos.clear();
submesh->vertUV.clear();
submesh->vertNorm.clear();
submesh->vertNormW.clear();
//parse MATERIALS ?
if (sarge.exists("texex")) {
std::string texType;
std::vector<std::string> validTypes = { "png","jpg","jpeg","bmp","dds","tga","hdr","tif","tiff","wdp","hdp","jxr","ppm","pfm" };
if (texTypeIn == "")
{
texType == "PNG";
}
else
{
for (const auto& type : validTypes)
{
if (boost::iequals(texTypeIn, type))
texType = boost::to_upper_copy(type);
}
}
hash = modelHash;
fileSize = getFile();
uint32_t val;
bool bFound = false;
extOff = fileSize -= 4;
while (true)
{
memcpy((char*)&val, data + extOff, 4);
if (val == 0x80800014)
{
bFound = true;
extOff -= 8;
break;
}
extOff -= 4;
}
std::vector<std::string> externalMaterials;
uint32_t extCount;
memcpy((char*)&extCount, data + extOff, 4);
extOff += 0x10;
std::set<std::uint32_t> existingMats;
for (int j = extOff; j < extOff + extCount * 4; j += 4)
{
memcpy((char*)&val, data + j, 4);
if (existingMats.find(val) == existingMats.end())
{
externalMaterials.push_back(uint32ToHexStr(val));
existingMats.insert(val);
}
}
for (auto exHash : externalMaterials) {
unsigned char* matdata = nullptr;
int matFileSize;
Package matpkg(getPkgID(exHash), packagesPath);
matdata = matpkg.getEntryData(exHash, matFileSize);
uint32_t textureCount;
uint32_t textureOffset;
memcpy((char*)&textureCount, matdata + 0x2B8, 4);
if (textureCount == 0) continue;
//memcpy((char*)&textureOffset, matdata + 0x308, 4);
//textureOffset += 488 + 0x10;
extOff = matFileSize - 16;
bFound = false;
while (true)
{
memcpy((char*)&val, matdata + extOff, 4);
if (val == 0x80806DCF)
{
bFound = true;
extOff += 8;
textureOffset = extOff;
break;
}
extOff -= 4;
}
if (!bFound)
continue;
uint64_t h64Val;
for (int v = textureOffset; v < textureOffset + textureCount * 0x18; v += 0x18) {
std::cout << std::to_string(v) << "\n";
uint8_t textureIndex;
memcpy((char*)&textureIndex, matdata + v, 1);
memcpy((char*)&val, matdata + v + 8, 4);
std::string h64Check = uint32ToHexStr(val);
std::string largeHash;
uint16_t textureFormat;
uint16_t width, height, arraySize;
uint32_t hash32;
if (h64Check == "ffffffff") {
memcpy((char*)&h64Val, matdata + v + 0x10, 8);
if (h64Val == 0) continue;
std::string textureHash = getHash64(h64Val, hash64Table);
if (textureHash == "ffffffff")
continue;
hash = textureHash;
getFile();
memcpy((char*)&textureFormat, data + 0x4, 2);
memcpy((char*)&width, data + 0x22, 2);
memcpy((char*)&height, data + 0x24, 2);
memcpy((char*)&arraySize, data + 0x28, 2);
memcpy((char*)&hash32, data + 60, 4);
largeHash = uint32ToHexStr(hash32);
delete[] data;
std::string finalHash = "";
if (largeHash != "ffffffff" && largeHash != "" && largeHash.substr(6, 2) == "80")
finalHash = largeHash;
else
finalHash = getReferenceFromHash(textureHash, packagesPath);
hash = finalHash;
fileSize = getFile();
fs::create_directories(outputPath + "/textures");
std::string fullSavePath = outputPath + "/textures/" + textureHash + ".DDS";
Texture* texture = new Texture();
texture->data = data;
texture->fileSize = fileSize;
texture->textureFormat = textureFormat;
texture->width = width;
texture->height = height;
texture->arraySize = arraySize;
texture->writeTexture(fullSavePath);
delete[] data;
free(texture);
std::string dxgiFormat = DXGI_FORMAT[textureFormat];
std::string texconv = "texconv.exe \"" + fullSavePath + "\" -nologo -y -ft " + texType + " -f " + dxgiFormat;
system(texconv.c_str());
fs::remove(fullSavePath);
}
else if (val > 0x80800000U) {
std::string textureHash = h64Check;
hash = textureHash;
getFile();
memcpy((char*)&textureFormat, data + 0x4, 2);
memcpy((char*)&width, data + 0x22, 2);
memcpy((char*)&height, data + 0x24, 2);
memcpy((char*)&arraySize, data + 0x28, 2);
memcpy((char*)&hash32, data + 60, 4);
largeHash = uint32ToHexStr(hash32);
delete[] data;
std::string finalHash = "";
if (largeHash != "ffffffff" && largeHash != "" && largeHash.substr(6, 2) == "80")
finalHash = largeHash;
else
finalHash = getReferenceFromHash(textureHash, packagesPath);
hash = finalHash;
fileSize = getFile();
fs::create_directories(outputPath + "/textures");
std::string fullSavePath = outputPath + "/textures/" + textureHash + ".DDS";
Texture* texture = new Texture();
texture->data = data;
texture->fileSize = fileSize;
texture->textureFormat = textureFormat;
texture->width = width;
texture->height = height;
texture->arraySize = arraySize;
texture->writeTexture(fullSavePath);
delete[] data;
free(texture);
std::string dxgiFormat = DXGI_FORMAT[textureFormat];
std::string texconv = "texconv.exe \"" + fullSavePath + "\" -nologo -y -ft " + texType + " -f " + dxgiFormat;
system(texconv.c_str());
fs::remove(fullSavePath);
}
}
}
}
std::cout << modelHash + ".fbx extracted.\n";
}
else if (batchPkg != "") {
Package pkg(batchPkg, packagesPath);
std::vector<std::string> hashes = pkg.getAllFilesGivenRef("446d8080");
outputPath += "/" + batchPkg + "/";
if (!fs::exists(outputPath))
fs::create_directories(outputPath);
std::cout << "Number of files to batch extract: " << std::to_string(hashes.size()) << "\n";
for (auto& modelhash : hashes)
{
if (modelhash == "")
break;
submesh->name = modelhash;
int fileSize = 0;
hash.clear();
hash = modelhash;
getFile();
memcpy((char*)&sfhash32, data + 0x8, 4);
memcpy((void*)&scale, data + 0x3C, 4);
hash.clear();
hash = uint32ToHexStr(sfhash32);
fileSize = getFile();
uint32_t indexBuffer, vertexBuffer, uvBuffer, vcBuffer;
memcpy((char*)&indexBuffer, data + fileSize - 0x14, 4);
memcpy((char*)&vertexBuffer, data + fileSize - 0x10, 4);
memcpy((char*)&uvBuffer, data + fileSize - 0xC, 4);
memcpy((char*)&vcBuffer, data + fileSize - 0x8, 4);
if (indexBuffer < 0x80800000 || vertexBuffer < 0x80800000 || uvBuffer < 0x80800000 || vcBuffer < 0x80800000)
continue;
float uoff, voff, uscale, vscale;
memcpy((void*)&uoff, data + 0x48, 4);
memcpy((void*)&voff, data + 0x4C, 4);
memcpy((void*)&uscale, data + 0x54, 4);
memcpy((void*)&vscale, data + 0x58, 4);
//This is very experimental and doesn't work yet.
/*
uint32_t val, amountLOD;
bool bFound = false;
extOff = fileSize -= 4;
while (true)
{
memcpy((char*)&val, data + extOff, 4);
if (val == 0x80806D37)
{
bFound = true;
extOff -= 8;
break;
}
extOff -= 4;
}
memcpy((char*)&amountLOD, data + extOff, 4);
extOff += 0x10;
int j = 0;
uint32_t splitnext_off;
uint32_t splitnext_count;
for (int o = extOff; o < extOff + (amountLOD * 12); o += 12) {
LODSplit split;
memcpy((char*)&split.off, data + o, 4);
memcpy((char*)&split.count, data + o + 4, 4);
memcpy((char*)&splitnext_off, data + o + 12, 4);
memcpy((char*)&splitnext_count, data + o + 16, 4);
std::cout << "CO: " + std::to_string(split.off) + " CC: " + std::to_string(split.count)
<< "\nNO: " + std::to_string(splitnext_off) + " NC: " + std::to_string(splitnext_count) << "\n";
if (j > 0 && submesh->lodsplit.size()) {
//if (split.off != (submesh->lodsplit[j - 1].off + submesh->lodsplit[j - 1].count))
//if (split.off == submesh->lodsplit[j - 1].off && split.count >)
// std::cout << "invalid LOD. SKIIIIIP\n";
//else
// submesh->lodsplit.push_back(split);
if (split.off == splitnext_off && split.count < splitnext_count) {
std::cout << "\ninvalid LOD or not LOD0. SKIIIIIP\n\n";
//o += 12;
}
else
submesh->lodsplit.push_back(split);
}
else if (split.off == splitnext_off && split.count < splitnext_count) {
std::cout << "\ninvalid LOD or not LOD0. SKIIIIIP\n\n";
//o += 12;
}
else
submesh->lodsplit.push_back(split);
j += 1;
}
*/
delete[] data;
hash = getReferenceFromHash(uint32ToHexStr(vertexBuffer), packagesPath);
fileSize = getFile();
if (scale == 0.000000)
scale = 1;
parseVertexBufferInMemory(fileSize, scale);
//parseVertexNormalsInMemory(fileSize);
delete[] data;
if (getReferenceFromHash(uint32ToHexStr(uvBuffer), packagesPath) != "ffffffff") {
hash = getReferenceFromHash(uint32ToHexStr(uvBuffer), packagesPath);
fileSize = getFile();
parseUVBufferInMemory(fileSize, uoff, voff, uscale, vscale);
delete[] data;
}
//uhhh this might work idk
hash = uint32ToHexStr(indexBuffer);
bool isu32 = false;
getFile();
memcpy((char*)&isu32, data + 1, 1);
submesh->isU32 = isu32;
delete[] data;
hash = getReferenceFromHash(uint32ToHexStr(indexBuffer), packagesPath);
fileSize = getFile();
parseIndexBufferInMemory(submesh->vertPos.size(), fileSize);
delete[] data;
//for later
/*
hash = uint32ToHexStr(vcBuffer);
if (hash != "FFFFFFFF"){
fileSize = getFile();
parseVCBufferInMemory(fileSize);
delete[] data;
addVertColSlots(submesh);
}
*/
std::string fbxpath = outputPath + "/" + modelhash + ".fbx";
submeshes.push_back(submesh);
FbxNode* node = fbxModel->addSubmeshToFbx(submesh);
nodes.push_back(node);
if (nodes.size()) {
for (auto& node : nodes) fbxModel->scene->GetRootNode()->AddChild(node);
fbxModel->save(fbxpath, false);
}
submesh->faces.clear();
submesh->facesu32.clear();
submesh->vertPos.clear();
submesh->vertUV.clear();
std::cout << modelhash + ".fbx extracted.\n";
submesh->name.clear();
nodes.clear();
fbxModel->scene->Clear();
submeshes.clear();
}
std::cout << "Finished extracting " + hashes.size() << " models.\n";
fbxModel->manager->Destroy();
}
}
int getFile()
{
pkgID = getPkgID(hash);
Package pkg(pkgID, packagesPath);
int fileSize;
data = pkg.getEntryData(hash, fileSize);
if (data == nullptr || sizeof(data) == 0) return 0;
return fileSize;
}
void addVertColSlots(Submesh* submesh) {
for (auto& w : submesh->vertNormW)
{
std::vector<float> vc = { 0., 0., 0., 1. };
switch (w & 0x7)
{
case 0:
vc[0] = 0.333;
break;
case 1:
vc[0] = 0.666;
break;
case 2:
vc[0] = 0.999;
break;
case 3:
vc[1] = 0.333;
break;
case 4:
vc[1] = 0.666;
break;
case 5:
vc[1] = 0.999;
break;
}
submesh->vertColSlots.push_back(vc);
}
}