forked from OpenKotOR/mdledit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMDL.cpp
More file actions
1299 lines (1192 loc) · 59 KB
/
Copy pathMDL.cpp
File metadata and controls
1299 lines (1192 loc) · 59 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
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
#include "MDL.h"
#include <atomic>
#include <fstream>
#include <algorithm>
#include <shlwapi.h>
#include <cstring>
#include <cmath>
/// This file should be a general initializer/implementor of MDL.h
VertexData::VertexData(){}
VertexData::VertexData(VertexData &&) = default;
VertexData::VertexData(const Vector & v1, const Vector & v2, int nName):
vVertex(v1), vUV1(v2), nNameIndex(static_cast<unsigned short>(nName)) {}
Face::Face(Face &&) = default;
Aabb::~Aabb() = default;
Patch::~Patch() = default;
MeshHeader::~MeshHeader() = default;
Animation::Animation() = default;
Animation::~Animation() = default;
BWMHeader::BWMHeader() = default;
Node::Node() = default;
Node::Node(const Node &) = default;
Node::Node(Node &&) = default;
Node & Node::operator=(Node &&) = default;
Node::~Node() = default;
void MDL::Report(std::string sMessage){
if(PtrReport != nullptr) PtrReport(sMessage);
}
MDL::~MDL(){}
void MDL::ProgressSize(int nMin, int nMax){
if(PtrProgressSize != nullptr) PtrProgressSize(nMin, nMax);
}
void MDL::ProgressPos(int nPos){
if(PtrProgressPos != nullptr) PtrProgressPos(nPos);
}
int MDL::GetNameIndex(const std::string & sName){
if(!FH) throw mdlexception("MDL::GetNameIndex() ERROR: File header is not available.");
std::vector<Name> & Names = FH->MH.Names;
int nReturn = -1;
for(int n = 0; n < static_cast<int>(Names.size()); n++){
if(StringEqual(Names[n].sName, sName)){
nReturn = n;
break;
}
}
return nReturn;
}
std::string & MDL::GetNodeName(Node & node){
if(!FH) throw mdlexception("MDL::GetNodeName() ERROR: File header is not available.");
if(!node.Head.nNameIndex.Valid() || node.Head.nNameIndex >= FH->MH.Names.size())
throw mdlexception("MDL::GetNodeName() ERROR: Name index is too large to find a name.");
return FH->MH.Names.at(node.Head.nNameIndex).sName;
}
extern MDL Model;
unsigned int GetFunctionPointer(unsigned int FN_PTR, int n, bool bXbox, bool bK2){
if(bXbox){
if(bK2){
if(n == 1){
if(FN_PTR == FN_PTR_MODEL) return FN_PTR_XBOX_K2_MODEL_1;
else if(FN_PTR == FN_PTR_ANIM) return FN_PTR_XBOX_K2_ANIM_1;
else if(FN_PTR == FN_PTR_MESH) return FN_PTR_XBOX_K2_MESH_1;
else if(FN_PTR == FN_PTR_SKIN) return FN_PTR_XBOX_K2_SKIN_1;
else if(FN_PTR == FN_PTR_DANGLY) return FN_PTR_XBOX_K2_DANGLY_1;
}
else{
if(FN_PTR == FN_PTR_MODEL) return FN_PTR_XBOX_K2_MODEL_2;
else if(FN_PTR == FN_PTR_ANIM) return FN_PTR_XBOX_K2_ANIM_2;
else if(FN_PTR == FN_PTR_MESH) return FN_PTR_XBOX_K2_MESH_2;
else if(FN_PTR == FN_PTR_SKIN) return FN_PTR_XBOX_K2_SKIN_2;
else if(FN_PTR == FN_PTR_DANGLY) return FN_PTR_XBOX_K2_DANGLY_2;
}
}
else{
if(n == 1){
if(FN_PTR == FN_PTR_MODEL) return FN_PTR_XBOX_K1_MODEL_1;
else if(FN_PTR == FN_PTR_ANIM) return FN_PTR_XBOX_K1_ANIM_1;
else if(FN_PTR == FN_PTR_MESH) return FN_PTR_XBOX_K1_MESH_1;
else if(FN_PTR == FN_PTR_SKIN) return FN_PTR_XBOX_K1_SKIN_1;
else if(FN_PTR == FN_PTR_DANGLY) return FN_PTR_XBOX_K1_DANGLY_1;
}
else{
if(FN_PTR == FN_PTR_MODEL) return FN_PTR_XBOX_K1_MODEL_2;
else if(FN_PTR == FN_PTR_ANIM) return FN_PTR_XBOX_K1_ANIM_2;
else if(FN_PTR == FN_PTR_MESH) return FN_PTR_XBOX_K1_MESH_2;
else if(FN_PTR == FN_PTR_SKIN) return FN_PTR_XBOX_K1_SKIN_2;
else if(FN_PTR == FN_PTR_DANGLY) return FN_PTR_XBOX_K1_DANGLY_2;
}
}
}
else{
if(bK2){
if(n == 1){
if(FN_PTR == FN_PTR_MODEL) return FN_PTR_PC_K2_MODEL_1;
else if(FN_PTR == FN_PTR_ANIM) return FN_PTR_PC_K2_ANIM_1;
else if(FN_PTR == FN_PTR_MESH) return FN_PTR_PC_K2_MESH_1;
else if(FN_PTR == FN_PTR_SKIN) return FN_PTR_PC_K2_SKIN_1;
else if(FN_PTR == FN_PTR_DANGLY) return FN_PTR_PC_K2_DANGLY_1;
}
else{
if(FN_PTR == FN_PTR_MODEL) return FN_PTR_PC_K2_MODEL_2;
else if(FN_PTR == FN_PTR_ANIM) return FN_PTR_PC_K2_ANIM_2;
else if(FN_PTR == FN_PTR_MESH) return FN_PTR_PC_K2_MESH_2;
else if(FN_PTR == FN_PTR_SKIN) return FN_PTR_PC_K2_SKIN_2;
else if(FN_PTR == FN_PTR_DANGLY) return FN_PTR_PC_K2_DANGLY_2;
}
}
else{
if(n == 1){
if(FN_PTR == FN_PTR_MODEL) return FN_PTR_PC_K1_MODEL_1;
else if(FN_PTR == FN_PTR_ANIM) return FN_PTR_PC_K1_ANIM_1;
else if(FN_PTR == FN_PTR_MESH) return FN_PTR_PC_K1_MESH_1;
else if(FN_PTR == FN_PTR_SKIN) return FN_PTR_PC_K1_SKIN_1;
else if(FN_PTR == FN_PTR_DANGLY) return FN_PTR_PC_K1_DANGLY_1;
}
else{
if(FN_PTR == FN_PTR_MODEL) return FN_PTR_PC_K1_MODEL_2;
else if(FN_PTR == FN_PTR_ANIM) return FN_PTR_PC_K1_ANIM_2;
else if(FN_PTR == FN_PTR_MESH) return FN_PTR_PC_K1_MESH_2;
else if(FN_PTR == FN_PTR_SKIN) return FN_PTR_PC_K1_SKIN_2;
else if(FN_PTR == FN_PTR_DANGLY) return FN_PTR_PC_K1_DANGLY_2;
}
}
}
return -1;
}
unsigned int MDL::FunctionPointer1(unsigned int FN_PTR){
return GetFunctionPointer(FN_PTR, 1, bXbox, bK2);
}
unsigned int MDL::FunctionPointer2(unsigned int FN_PTR){
return GetFunctionPointer(FN_PTR, 2, bXbox, bK2);
}
std::vector<Vertex> MDL::GetWokVertData(Node & node){
std::vector<Vertex> verts_wok;
if(!(Wok && Wok->GetData())) return verts_wok;
BWMHeader & data = *Wok->GetData();
Vector vLyt;
if(FH) vLyt = FH->MH.vLytPosition;
if(data.faces.size() != node.Mesh.Faces.size()) return verts_wok;
/// All the checks have passed, get to business. First, build a vector of Faces in the order of the wok.
std::vector<Face> faces_wok;
std::vector<Face> unwalkable;
for(std::size_t f = 0; f < node.Mesh.Faces.size(); f++){
Face & face = node.Mesh.Faces.at(f);
if(IsMaterialWalkable(face.nMaterialID)) faces_wok.push_back(face);
else unwalkable.push_back(face);
}
for(std::size_t f = 0; f < unwalkable.size(); f++){
faces_wok.push_back(unwalkable.at(f));
}
/// Preserve the original vertex payload by default. Only overwrite the
/// positions that can be mapped unambiguously from the WOK. Returning an
/// empty vector means the WOK data cannot be represented without risking a
/// malformed ASCII mesh.
verts_wok = node.Mesh.Vertices;
std::vector<bool> bAssigned(verts_wok.size(), false);
for(std::size_t f = 0; f < node.Mesh.Faces.size(); f++){
for(int i = 0; i < 3; i++){
const auto & nMdlVert = faces_wok.at(f).nIndexVertex.at(i);
const auto & nWokVert = data.faces.at(f).nIndexVertex.at(i);
if(!nMdlVert.Valid() || !nWokVert.Valid()) return std::vector<Vertex>();
const long long nMdlVertIndex = nMdlVert;
const long long nWokVertIndex = nWokVert;
if(nMdlVertIndex < 0 || nWokVertIndex < 0 ||
static_cast<std::size_t>(nMdlVertIndex) >= verts_wok.size() ||
static_cast<std::size_t>(nWokVertIndex) >= data.verts.size()){
return std::vector<Vertex>();
}
const std::size_t nMdlIndex = static_cast<std::size_t>(nMdlVertIndex);
const Vector vMapped = data.verts.at(static_cast<std::size_t>(nWokVertIndex)) - vLyt - node.Head.vFromRoot;
if(bAssigned.at(nMdlIndex) && !verts_wok.at(nMdlIndex).MDXData.vVertex.Compare(vMapped, 0.00001)){
return std::vector<Vertex>();
}
verts_wok.at(nMdlIndex).assign(vMapped, true);
bAssigned.at(nMdlIndex) = true;
}
}
return verts_wok;
}
void MDL::GetLytPositionFromWok(){
if(!Wok) return;
if(!FH) return;
if(!Wok->GetData()) return;
FileHeader & Data = *FH;
BWMHeader & data = *Wok->GetData();
if(data.faces.empty()) return;
if(data.verts.empty()) return;
if(!data.faces.front().nIndexVertex.at(0).Valid()) return;
const unsigned int nWokVertIndex = data.faces.front().nIndexVertex.at(0);
if(nWokVertIndex >= data.verts.size()) return;
const unsigned int nSearchMaterial = data.faces.front().nMaterialID;
for(Node & node : Data.MH.ArrayOfNodes){
if(node.Head.nType & NODE_AABB){
for(Face & face : node.Mesh.Faces){
if(face.nMaterialID == nSearchMaterial){
if(!face.nIndexVertex.at(0).Valid()) return;
const unsigned int nMdlVertIndex = face.nIndexVertex.at(0);
if(nMdlVertIndex >= node.Mesh.Vertices.size()) return;
const Vector & wokVert = data.verts.at(nWokVertIndex);
const Vector & mdlVert = node.Mesh.Vertices.at(nMdlVertIndex).vFromRoot;
Data.MH.vLytPosition =
Vector(wokVert.fX - mdlVert.fX,
wokVert.fY - mdlVert.fY,
wokVert.fZ - mdlVert.fZ);
return;
}
}
return;
}
}
}
std::unique_ptr<FileHeader> & MDL::GetFileData(){
return FH;
}
std::string MDL::MakeUniqueName(int nNameIndex){
if(!FH) throw mdlexception("MDL::MakeUniqueName() failed because we're running it on a model without FileHeader data.");
std::vector<Name> & Names = FH->MH.Names;
std::string sReturn = Names.at(nNameIndex).sName.c_str();
std::vector<Node> & Nodes = FH->MH.ArrayOfNodes;
if(Nodes.size() > static_cast<unsigned>(nNameIndex)){
if(Nodes.at(nNameIndex).Head.nType & NODE_SABER && bLightsaberToTrimesh) sReturn = "2081__" + sReturn;
}
for(int n = 0; n < nNameIndex; n++){
if(StringEqual(Names.at(n).sName, sReturn, true)) return sReturn + "__dpl" + std::to_string(static_cast<int>(nNameIndex));
}
return sReturn;
}
int MDL::GetNodeIndexByNameIndex(int nIndex, int nAnimation){
if(!FH)
throw mdlexception("MDL::GetNodeIndexByNameIndex() ERROR: File header not available.");
FileHeader & Data = *FH;
if(nIndex < 0 || static_cast<unsigned>(nIndex) >= Data.MH.Names.size()){
std::string sMax = Data.MH.Names.empty() ? "none" : std::to_string(Data.MH.Names.size() - 1);
throw mdlexception("MDL::GetNodeIndexByNameIndex() ERROR: The index " + std::to_string(nIndex) + " lies outside the range of valid indices (0-" + sMax + ").");
}
int nReturn = -1;
if(nAnimation == -1){
for(int n = 0; n < static_cast<int>(Data.MH.ArrayOfNodes.size()); n++){
if(Data.MH.ArrayOfNodes.at(n).Head.nNameIndex == nIndex){
nReturn = n;
break;
}
}
}
else{
for(int n = 0; n < static_cast<int>(Data.MH.Animations.at(nAnimation).ArrayOfNodes.size()); n++){
if(Data.MH.Animations.at(nAnimation).ArrayOfNodes.at(n).Head.nNameIndex == nIndex){
nReturn = n;
break;
}
}
}
return nReturn;
}
bool MDL::HeadLinked(){
if(FH->MH.GH.nOffsetToRootNode != FH->MH.nOffsetToHeadRootNode) return true;
return false;
}
bool MDL::NodeExists(const std::string & sNodeName){
if(GetNameIndex(sNodeName) != -1) return true;
return false;
}
namespace {
void BuildTreeOrderArrayImpl(ModelHeader & header, Node & node, std::vector<unsigned short> & active, std::vector<unsigned short> & visited){
if(!node.Head.nNameIndex.Valid() || static_cast<unsigned short>(node.Head.nNameIndex) >= header.Names.size()){
throw mdlexception("BuildTreeOrderArray() error, node name index is invalid or out of scope!");
}
const unsigned short nNameIndex = static_cast<unsigned short>(node.Head.nNameIndex);
if(std::find(active.begin(), active.end(), nNameIndex) != active.end()){
throw mdlexception("BuildTreeOrderArray() error, cyclic child hierarchy detected!");
}
if(std::find(visited.begin(), visited.end(), nNameIndex) != visited.end()){
throw mdlexception("BuildTreeOrderArray() error, hierarchy reaches the same node more than once!");
}
active.push_back(nNameIndex);
visited.push_back(nNameIndex);
header.NameIndicesInTreeOrder.push_back(nNameIndex);
std::vector<unsigned short> localChildren;
localChildren.reserve(node.Head.ChildIndices.size());
for(auto n : node.Head.ChildIndices){
if(!n.Valid()) throw mdlexception("BuildTreeOrderArray() error, child name index is invalid!");
if(static_cast<unsigned short>(n) >= header.Names.size()) throw mdlexception("BuildTreeOrderArray() error, child name index out of scope!");
const unsigned short nChildIndex = static_cast<unsigned short>(n);
if(nChildIndex == nNameIndex) throw mdlexception("BuildTreeOrderArray() error, node is listed as its own child!");
if(std::find(localChildren.begin(), localChildren.end(), nChildIndex) != localChildren.end()){
throw mdlexception("BuildTreeOrderArray() error, duplicate child name index under one parent!");
}
localChildren.push_back(nChildIndex);
bool bFound = false;
for(Node & child : header.ArrayOfNodes){
if(child.Head.nNameIndex == n){
bFound = true;
BuildTreeOrderArrayImpl(header, child, active, visited);
break;
}
}
if(!bFound){
throw mdlexception("BuildTreeOrderArray() error, child name index does not resolve to a node!");
}
}
active.pop_back();
}
}
void ModelHeader::BuildTreeOrderArray(Node & node){
std::vector<unsigned short> oldOrder = NameIndicesInTreeOrder;
std::vector<unsigned short> newOrder;
std::vector<unsigned short> active;
std::vector<unsigned short> visited;
NameIndicesInTreeOrder.swap(newOrder);
try{
BuildTreeOrderArrayImpl(*this, node, active, visited);
oldOrder = NameIndicesInTreeOrder;
}
catch(...){
NameIndicesInTreeOrder.swap(oldOrder);
throw;
}
}
std::stringstream & MDL::GetReport(){
return ssReport;
}
void MDL::SaveReport(){
std::wstring sFile = GetFullPath();
if(safesubstr(sFile, sFile.size() - 6, 6) == L".ascii") sFile = safesubstr(sFile, 0, sFile.size() - 6);
if(safesubstr(sFile, sFile.size() - 4, 4) == L".mdl") sFile = safesubstr(sFile, 0, sFile.size() - 4);
sFile += L"_report.txt";
/// Create file
HANDLE hFile = bead_CreateWriteFile(sFile);
if(hFile == INVALID_HANDLE_VALUE){
std::cout << "File creation failed for " << sFile.c_str() << ". Aborting.\n";
return;
}
/// Write and close file
if(!bead_WriteFile(hFile, ssReport.str())){
std::cout << "File write failed for " << to_ansi(sFile) << ".\n";
}
CloseHandle(hFile);
}
void MDL::FlushData(){
FH.reset();
Ascii.reset();
Mdx.reset();
Wok.reset();
Pwk.reset();
Dwk0.reset();
Dwk1.reset();
Dwk2.reset();
PwkAscii.reset();
DwkAscii.reset();
FlushAll();
ClearStringstream(ssReport);
src = NoSource;
}
//Setters/general
bool MDL::LinkHead(bool bLink){
unsigned int nOffset;
if(bLink){
MdlInteger<unsigned short> nNameIndex;
for(unsigned short n = 0; n < FH->MH.Names.size() && !nNameIndex.Valid(); n++){
if(StringEqual(FH->MH.Names.at(n).sName, "neck_g", true)) nNameIndex = n;
}
if(nNameIndex.Valid()){
int nNodeIndex = GetNodeIndexByNameIndex(nNameIndex);
if(nNodeIndex == -1) throw mdlexception("MDL::LinkHead() error: dealing with a name index that does not have a node in geometry.");
nOffset = FH->MH.ArrayOfNodes.at(nNodeIndex).nOffset;
}
else return false;
}
else{
nOffset = FH->MH.GH.nOffsetToRootNode;
}
FH->MH.nOffsetToHeadRootNode = nOffset;
unsigned nHeadLinkOffset = 180;
WriteNumber(&nOffset, 0, "", &nHeadLinkOffset);
return true;
}
void MDL::UpdateTexture(Node & node, const std::string & sNew, int nTex){
std::string sNewTex = sNew.c_str();
if(nTex == 1){
node.Mesh.cTexture1 = sNewTex;
sNewTex.resize(32);
int nPos = node.nOffset + 12 + 168;
for(std::size_t n = 0; n < sNewTex.size(); ++n){
sBuffer.at(static_cast<std::size_t>(nPos) + n) = sNewTex.at(n);
}
}
else if(nTex == 2){
node.Mesh.cTexture2 = sNewTex;
sNewTex.resize(32);
int nPos = node.nOffset + 12 + 200;
for(std::size_t n = 0; n < sNewTex.size(); ++n){
sBuffer.at(static_cast<std::size_t>(nPos) + n) = sNewTex.at(n);
}
}
else if(nTex == 3){
node.Mesh.cTexture3 = sNewTex;
sNewTex.resize(12);
int nPos = node.nOffset + 12 + 232;
for(std::size_t n = 0; n < sNewTex.size(); ++n){
sBuffer.at(static_cast<std::size_t>(nPos) + n) = sNewTex.at(n);
}
}
else if(nTex == 4){
node.Mesh.cTexture4 = sNewTex;
sNewTex.resize(12);
int nPos = node.nOffset + 12 + 244;
for(std::size_t n = 0; n < sNewTex.size(); ++n){
sBuffer.at(static_cast<std::size_t>(nPos) + n) = sNewTex.at(n);
}
}
}
void MDL::WriteUintToPlaceholder(unsigned int nUint, int nOffset){
unsigned nInsert = static_cast<unsigned>(nOffset);
WriteNumber(&nUint, 0, "", &nInsert);
}
void MDL::WriteByteToPlaceholder(unsigned char nByte, int nOffset){
unsigned nInsert = static_cast<unsigned>(nOffset);
WriteNumber(&nByte, 0, "", &nInsert);
}
//ascii
void MDL::FlushAscii(){
if(Ascii) Ascii->FlushAll();
}
std::vector<char> & MDL::CreateAsciiBuffer(int nSize){
return Ascii->CreateBuffer(nSize);
}
bool MDL::ReadAscii(){
ReportObject ReportMdl(*this);
// Keep existing model state intact unless the full ASCII/companion-walkmesh
// read succeeds.
std::unique_ptr<FileHeader> oldFH = std::move(FH);
std::unique_ptr<MDX> oldMdx = std::move(Mdx);
std::unique_ptr<WOK> oldWok = std::move(Wok);
std::unique_ptr<PWK> oldPwk = std::move(Pwk);
std::unique_ptr<DWK> oldDwk0 = std::move(Dwk0);
std::unique_ptr<DWK> oldDwk1 = std::move(Dwk1);
std::unique_ptr<DWK> oldDwk2 = std::move(Dwk2);
const ModelSource oldSrc = src;
const bool oldK2 = bK2;
const bool oldXbox = bXbox;
auto RestoreAsciiReadState = [&](){
FH = std::move(oldFH);
Mdx = std::move(oldMdx);
Wok = std::move(oldWok);
Pwk = std::move(oldPwk);
Dwk0 = std::move(oldDwk0);
Dwk1 = std::move(oldDwk1);
Dwk2 = std::move(oldDwk2);
src = oldSrc;
bK2 = oldK2;
bXbox = oldXbox;
};
auto CommitAsciiReadState = [&](){
if(!Ascii){
// Companion-only ASCII import: preserve the existing model/MDX/WOK
// state and only commit the companion data that was actually read.
FH = std::move(oldFH);
Mdx = std::move(oldMdx);
Wok = std::move(oldWok);
src = oldSrc;
bK2 = oldK2;
bXbox = oldXbox;
if(!PwkAscii){
Pwk = std::move(oldPwk);
}
if(!DwkAscii){
Dwk0 = std::move(oldDwk0);
Dwk1 = std::move(oldDwk1);
Dwk2 = std::move(oldDwk2);
}
}
// Full model ASCII import: Ascii::Read() committed a new FileHeader.
// Do not reattach any previous binary payload or companion objects;
// doing so can make the new ASCII model inherit stale MDX/WOK/PWK/DWK
// data from the previously-open model.
};
try{
if(Ascii){
if(!Ascii->Read(*this)){
RestoreAsciiReadState();
return false;
}
else ReportMdl << "Mdl ascii read succesfully!\n";
}
if(PwkAscii){
if(!PwkAscii->ReadWalkmesh(*this, true)){
RestoreAsciiReadState();
return false;
}
else ReportMdl << "Pwk ascii read succesfully!\n";
}
if(DwkAscii){
if(!DwkAscii->ReadWalkmesh(*this, false)){
RestoreAsciiReadState();
return false;
}
else ReportMdl << "Dwk ascii read succesfully!\n";
}
}
catch(...){
RestoreAsciiReadState();
throw;
}
CommitAsciiReadState();
return true;
}
Location Node::GetLocation(){
Location location;
MdlInteger<unsigned short> nCtrlPos;
MdlInteger<unsigned short> nCtrlOri;
for(unsigned short c = 0; c < Head.Controllers.size(); c++){
if(Head.Controllers.at(c).nControllerType == CONTROLLER_HEADER_POSITION){
nCtrlPos = c;
}
if(Head.Controllers.at(c).nControllerType == CONTROLLER_HEADER_ORIENTATION){
nCtrlOri = c;
}
}
if(nCtrlPos.Valid()){
Controller & posctrl = Head.Controllers.at(nCtrlPos);
location.vPosition = Vector(Head.ControllerData.at(posctrl.nDataStart + 0),
Head.ControllerData.at(posctrl.nDataStart + 1),
Head.ControllerData.at(posctrl.nDataStart + 2));
}
if(nCtrlOri.Valid()){
Controller & orictrl = Head.Controllers.at(nCtrlOri);
if(orictrl.nColumnCount == 4){
location.oOrientation = Orientation(Head.ControllerData.at(orictrl.nDataStart + 0),
Head.ControllerData.at(orictrl.nDataStart + 1),
Head.ControllerData.at(orictrl.nDataStart + 2),
Head.ControllerData.at(orictrl.nDataStart + 3));
}
else if(orictrl.nColumnCount == 2){
location.oOrientation.SetQuaternion(DecompressQuaternion((unsigned int) Head.ControllerData.at(orictrl.nDataStart)));
}
}
return location;
}
const std::string MDL::sClassName = "MDL";
const std::string MDX::sClassName = "MDX";
const std::string WOK::sClassName = "WOK";
const std::string PWK::sClassName = "PWK";
const std::string DWK::sClassName = "DWK";
const std::string MDL::GetName(){
return sClassName;
}
const std::string MDX::GetName(){
return sClassName;
}
const std::string WOK::GetName(){
return sClassName;
}
const std::string PWK::GetName(){
return sClassName;
}
const std::string DWK::GetName(){
return sClassName;
}
bool LoadSupermodel(MDL & curmdl, std::unique_ptr<MDL> & Supermodel, bool bWarnIfUnavailable){
ReportObject ReportMdl(curmdl);
std::string sSMname = curmdl.GetFileData()->MH.cSupermodelName.c_str();
if(sSMname != "NULL" && sSMname != ""){
std::wstring sNewMdl = JoinPath(ParentPath(curmdl.GetFullPath()),
to_wide(curmdl.GetFileData()->MH.cSupermodelName) + L".mdl");
//Create file
HANDLE hFile = bead_CreateReadFile(sNewMdl);
//Check for problems
bool bOpen = true;
bool bWrongGame = false;
if(hFile == INVALID_HANDLE_VALUE)
bOpen = false;
if(bOpen){
std::vector<char> cBinary(4,0);
if(!bead_ReadFile(hFile, cBinary, 4)) bOpen = false;
/// Make sure that what we've read is a binary .mdl as far as we can tell
else if(cBinary[0]!='\0' || cBinary[1]!='\0' || cBinary[2]!='\0' || cBinary[3]!='\0') bOpen = false;
}
/// If we pass, then the file is definitely ready to be read.
if(bOpen){
std::vector<char> cBinary(17,0);
if(!bead_ReadFile(hFile, cBinary, 16)) bOpen = false;
unsigned nFunctionPointer = 0;
if(bOpen) std::memcpy(&nFunctionPointer, &cBinary.at(12), sizeof(nFunctionPointer));
if(bOpen && !((nFunctionPointer == FN_PTR_PC_K2_MODEL_1 || nFunctionPointer == FN_PTR_XBOX_K2_MODEL_1) && curmdl.bK2) &&
!((nFunctionPointer == FN_PTR_PC_K1_MODEL_1 || nFunctionPointer == FN_PTR_XBOX_K1_MODEL_1) && !curmdl.bK2)){
bOpen = false;
bWrongGame = true;
}
}
if(bOpen){
Supermodel.reset(new MDL);
ReportMdl << "Reading supermodel: \n" << to_ansi(sNewMdl.c_str()) << "\n";
unsigned long length = bead_GetFileLength(hFile);
std::vector<char> & sBufferRef = Supermodel->CreateBuffer(length);
if(!bead_ReadFile(hFile, sBufferRef)){
CloseHandle(hFile);
throw mdlexception("Error reading binary supermodel " + to_ansi(sNewMdl) + ".");
}
Supermodel->SetFilePath(sNewMdl);
CloseHandle(hFile);
Supermodel->DecompileModel(true);
return true;
}
else{
if(hFile != INVALID_HANDLE_VALUE) CloseHandle(hFile);
if(bWarnIfUnavailable){
if(bWrongGame) Warning(L"Binary supermodel " + std::wstring(sNewMdl.c_str()) + L" belongs to " + (curmdl.bK2 ? L"K1" : L"K2") + L", while the model is being loaded for " + (curmdl.bK2 ? L"K2" : L"K1") + L". "
L"The supermodel must belong to the game you are targeting, otherwise the supermodel animations will not work on the new model! The model will now be loaded without its supermodel.");
else Warning(L"Could not find binary supermodel " + std::wstring(sNewMdl.c_str()) + L" in the directory! "
L"The new model must be compiled with access to its supermodel, otherwise the supermodel animations will not work on the new model! The model will now be loaded without its supermodel.");
}
return false;
}
}
return false;
}
/// This function is to be used both when compiling and decompiling (to determine smoothing groups)
/// It is very expensive, so modify with care to keep it efficient. Any calculations that can be performed outside, should be.
void MDL::CreatePatches(){
FileHeader & Data = *FH;
ReportObject ReportMdl(*this);
Timer tPatches;
extern std::atomic_bool bCancelSG;
/// Build patches by walking unprocessed mesh vertices and
/// collecting every matching vertex from the current node onward with
/// a 1e-5 vertex-coordinate tolerance.
Report("Building Patch array... (this may take a while)");
ReportMdl << "Building Patch array...";
std::cout << " (this may take a while)";
ReportMdl << "\n";
std::cout << "\n";
struct PatchBuildCleanupGuard{
FileHeader & Data;
bool bCommit = false;
explicit PatchBuildCleanupGuard(FileHeader & data) : Data(data) {}
~PatchBuildCleanupGuard(){ if(!bCommit) Reset(); }
void Reset(){
Data.MH.PatchArrayPointers.clear();
Data.MH.nTotalVertCount = 0;
Data.MH.nTotalTangent1Count = 0;
Data.MH.nTotalTangent2Count = 0;
Data.MH.nTotalTangent3Count = 0;
Data.MH.nTotalTangent4Count = 0;
for(Node & node : Data.MH.ArrayOfNodes){
if(node.Head.nType & NODE_MESH){
for(Vertex & vert : node.Mesh.Vertices){
vert.nLinkedFacesIndex = static_cast<unsigned int>(-1);
}
}
}
}
void Commit(){ bCommit = true; }
} PatchCleanup(Data);
PatchCleanup.Reset();
struct VertexFaceRefs{
std::vector<unsigned int> FaceIndices;
unsigned int nSmoothingGroups = 0;
};
std::vector<std::vector<VertexFaceRefs>> FaceRefsByNode(Data.MH.ArrayOfNodes.size());
for(int n = 0; n < static_cast<int>(Data.MH.ArrayOfNodes.size()); n++){
Node & node = Data.MH.ArrayOfNodes.at(n);
if(!(node.Head.nType & NODE_MESH) || (node.Head.nType & NODE_SABER)) continue;
std::vector<VertexFaceRefs> & FaceRefs = FaceRefsByNode.at(n);
FaceRefs.resize(node.Mesh.Vertices.size());
for(int f = 0; f < static_cast<int>(node.Mesh.Faces.size()); f++){
Face & face = node.Mesh.Faces.at(f);
for(int i = 0; i < 3; i++){
unsigned int nVertex = face.nIndexVertex.at(i);
if(static_cast<std::size_t>(nVertex) >= FaceRefs.size()) continue;
FaceRefs.at(nVertex).FaceIndices.push_back(static_cast<unsigned int>(f));
FaceRefs.at(nVertex).nSmoothingGroups |= face.nSmoothingGroup;
}
}
}
for(int n = 0; n < static_cast<int>(Data.MH.ArrayOfNodes.size()); n++){
/// Currently, this takes all meshes except lightsabers, with render on or off.
if(Data.MH.ArrayOfNodes.at(n).Head.nType & NODE_MESH && !(Data.MH.ArrayOfNodes.at(n).Head.nType & NODE_SABER)){
Node & node = Data.MH.ArrayOfNodes.at(n);
/// Patch/tangent totals are counted from patches that actually
/// participate in faces. Meshes can contain unused binary vertices;
/// counting those as normals to recover makes smoothing recovery fail
/// even though they do not affect rendering or ASCII reconstruction.
/// For every vertex of every mesh.
for(int v = 0; v < static_cast<int>(node.Mesh.Vertices.size()); v++){
/// Proceed only if this vertex hasn't been processed yet.
if(!node.Mesh.Vertices.at(v).nLinkedFacesIndex.Valid()){
Vertex & vert = node.Mesh.Vertices.at(v);
Vector & vCoords = vert.vFromRoot;
/// Create new vector.
std::vector<Patch> CurrentPatchGroup;
unsigned int nPatchGroupIndex = static_cast<unsigned int>(Data.MH.PatchArrayPointers.size());
/// We've already gone through the nodes up to n and linked any vertices, so we can skip those.
for(int n2 = n; n2 < static_cast<int>(Data.MH.ArrayOfNodes.size()); n2++){
if(Data.MH.ArrayOfNodes.at(n2).Head.nType & NODE_MESH && !(Data.MH.ArrayOfNodes.at(n2).Head.nType & NODE_SABER)){
Node & node2 = Data.MH.ArrayOfNodes.at(n2);
/// Loop through all the verts in the mesh and look for matching vertices.
int nFirstVertex = (n2 == n ? v : 0);
for(int v2 = nFirstVertex; v2 < static_cast<int>(node2.Mesh.Vertices.size()); v2++){
/// Skip if this vertex has been processed.
if(node2.Mesh.Vertices.at(v2).nLinkedFacesIndex.Valid()) continue;
/// Check if vertices are equal enough.
Vector & vCoords2 = node2.Mesh.Vertices.at(v2).vFromRoot;
if(vCoords.Compare(vCoords2, 0.00001)){
/// Update reference to new vector.
node2.Mesh.Vertices.at(v2).nLinkedFacesIndex = nPatchGroupIndex;
const VertexFaceRefs & FaceRefs = FaceRefsByNode.at(n2).at(v2);
if(!FaceRefs.FaceIndices.empty()){
/// Build patch and add it to the group.
Patch OtherPatch;
OtherPatch.nNodeArrayIndex = n2;
OtherPatch.nVertex = v2;
OtherPatch.nPatchGroup = nPatchGroupIndex;
OtherPatch.FaceIndices = FaceRefs.FaceIndices;
OtherPatch.nSmoothingGroups = FaceRefs.nSmoothingGroups;
Data.MH.nTotalVertCount++;
if(node2.Mesh.nMdxDataBitmap & MDX_FLAG_TANGENT1) Data.MH.nTotalTangent1Count++;
if(node2.Mesh.nMdxDataBitmap & MDX_FLAG_TANGENT2) Data.MH.nTotalTangent2Count++;
if(node2.Mesh.nMdxDataBitmap & MDX_FLAG_TANGENT3) Data.MH.nTotalTangent3Count++;
if(node2.Mesh.nMdxDataBitmap & MDX_FLAG_TANGENT4) Data.MH.nTotalTangent4Count++;
CurrentPatchGroup.push_back(std::move(OtherPatch));
}
/// If ESC was pressed, abort here.
if(bCancelSG.load()) return;
}
}
}
}
/// Record group if any vertices matched.
if(!CurrentPatchGroup.empty()) Data.MH.PatchArrayPointers.push_back(std::move(CurrentPatchGroup));
}
}
}
}
/// Algorithm done.
PatchCleanup.Commit();
ReportMdl << "Done creating patches in " << tPatches.GetTime() << ".\n";
}
unsigned MDL::GetHeaderOffset(const Node & node, unsigned short nHeader){
unsigned nReturn = 0;
if(nHeader == NODE_HEADER) return nReturn;
else if(node.Head.nType & NODE_HEADER) nReturn += NODE_SIZE_HEADER;
if(nHeader == NODE_LIGHT) return nReturn;
else if(node.Head.nType & NODE_LIGHT) nReturn += NODE_SIZE_LIGHT;
if(nHeader == NODE_EMITTER) return nReturn;
else if(node.Head.nType & NODE_EMITTER) nReturn += NODE_SIZE_EMITTER;
if(nHeader == NODE_REFERENCE) return nReturn;
else if(node.Head.nType & NODE_REFERENCE) nReturn += NODE_SIZE_REFERENCE;
if(nHeader == NODE_MESH) return nReturn;
else if(node.Head.nType & NODE_MESH){
nReturn += NODE_SIZE_MESH;
if(bXbox) nReturn -= 4;
if(!bK2) nReturn -= 8;
}
if(nHeader == NODE_SKIN) return nReturn;
else if(node.Head.nType & NODE_SKIN) nReturn += NODE_SIZE_SKIN;
if(nHeader == NODE_DANGLY) return nReturn;
else if(node.Head.nType & NODE_DANGLY) nReturn += NODE_SIZE_DANGLY;
if(nHeader == NODE_AABB) return nReturn;
else if(node.Head.nType & NODE_AABB) nReturn += NODE_SIZE_AABB;
if(nHeader == NODE_SABER) return nReturn;
else if(node.Head.nType & NODE_SABER) nReturn += NODE_SIZE_SABER;
throw mdlexception("MDL::GetHeaderOffset(): unknown node header " + std::to_string(nHeader) + ".");
}
bool IsMaterialWalkable(int nMat){
if(nMat == MATERIAL_NONWALK ||
nMat == MATERIAL_OBSCURING ||
nMat == MATERIAL_TRANSPARENT ||
nMat == MATERIAL_LAVA ||
//nMat == MATERIAL_BOTTOMLESSPIT ||
nMat == MATERIAL_DEEPWATER ||
nMat == MATERIAL_SNOW) return false;
return true;
}
//This function together with the next one, checks the currently loaded data in MDL for any special properties
void MDL::CheckPeculiarities(){
ReportObject ReportMdl(*this);
FileHeader & Data = *FH;
std::stringstream ssReturn;
bool bUpdate = false;
Report("Checking for peculiarities...");
ssReturn << "Lucky you! Your model has some peculiarities:";
if(!Data.MH.GH.RuntimeArray1.empty()){
ssReturn << "\r\n - First empty runtime array in the GH has a some nonzero value!";
bUpdate = true;
}
if(!Data.MH.GH.RuntimeArray2.empty()){
ssReturn << "\r\n - Second empty runtime array in the GH has a some nonzero value!";
bUpdate = true;
}
if(Data.MH.GH.nRefCount != 0){
ssReturn << "\r\n - RefCount has a value!";
bUpdate = true;
}
if(Data.MH.GH.nModelType != 2){
ssReturn << "\r\n - Header ModelType different than 2!";
bUpdate = true;
}
if(Data.MH.nUnknown != 0 && !bWriteSmoothing){
ssReturn << "\r\n - Second classification padding number different than 0!";
bUpdate = true;
}
if(Data.MH.nChildModelCount != 0){
ssReturn << "\r\n - ChildModelCount has a value!";
bUpdate = true;
}
if(Data.MH.AnimationArray.GetDoCountsDiffer()){
ssReturn << "\r\n - AnimationArray counts differ!";
bUpdate = true;
}
if(Data.MH.nPadding != 0){
ssReturn << "\r\n - Unknown int32 after the Root Head Node Offset in the MH has a value!";
bUpdate = true;
}
if(Data.MH.nMdxLength2 != Data.nMdxLength){
ssReturn << "\r\n - MdxLength in FH and MdxLength2 in MH don't have the same value!";
bUpdate = true;
}
if(Data.MH.nOffsetIntoMdx != 0){
ssReturn << "\r\n - OffsetIntoMdx after the MdxLength2 in the MH has a value!";
bUpdate = true;
}
if(Data.MH.NameArray.GetDoCountsDiffer()){
ssReturn << "\r\n - NameArray counts differ!";
bUpdate = true;
}
for(unsigned int a = 0; a < Data.MH.AnimationArray.nCount; ++a){
if(!Data.MH.Animations.at(a).RuntimeArray1.empty()){
ssReturn << "\r\n - First empty runtime array in the Animation GH has a some nonzero value!";
bUpdate = true;
}
if(!Data.MH.Animations.at(a).RuntimeArray2.empty()){
ssReturn << "\r\n - Second empty runtime array in the Animation GH has a some nonzero value!";
bUpdate = true;
}
if(Data.MH.Animations.at(a).nRefCount != 0){
ssReturn << "\r\n - Animation counterpart to RefCount has a value!";
bUpdate = true;
}
if(Data.MH.Animations.at(a).EventArray.GetDoCountsDiffer()){
ssReturn << "\r\n - EventArray counts differ!";
bUpdate = true;
}
if(Data.MH.Animations.at(a).nPadding2 != 0){
ssReturn << "\r\n - Unknown int32 after EventArrayHead has a value!";
bUpdate = true;
}
if(Data.MH.Animations.at(a).nModelType != 5){
ssReturn << "\r\n - Animation ModelType is not 5!";
bUpdate = true;
}
if(CheckNodes(Data.MH.Animations.at(a).ArrayOfNodes, ssReturn, a)) bUpdate = true;
}
if(CheckNodes(Data.MH.ArrayOfNodes, ssReturn, -1)) bUpdate = true;
if(!bUpdate){
ReportMdl << "Checked for peculiarities, nothing to report.\n";
return;
}
MessageBox(NULL, ssReturn.str().c_str(), "Notification", MB_OK);
}
bool MDL::CheckNodes(std::vector<Node> & NodeArray, std::stringstream & ssReturn, int nAnimation){
bool bMasterUpdate = false;
for(std::size_t b = 0; b < NodeArray.size(); ++b){
if(NodeArray.at(b).Head.nType == 0){
//Ghost node
}
else{
bool bUpdate = false;
std::stringstream ssAdd;
std::string sCont;
if(nAnimation == -1) sCont = "geometry";
else sCont = FH->MH.Animations.at(nAnimation).sName.c_str();
ssAdd << "\r\n - " << FH->MH.Names.at(NodeArray.at(b).Head.nNameIndex).sName << " (" << sCont << ")";
if(NodeArray.at(b).Head.nType & NODE_HEADER){
if(NodeArray.at(b).Head.nPadding1 != 0){
ssAdd << "\r\n - Header: Unknown short after NameIndex has a value!";
bUpdate = true;
}
if(NodeArray.at(b).Head.ChildrenArray.GetDoCountsDiffer()){
ssAdd << "\r\n - Header: ChildArray counts differ!";
bUpdate = true;
}
if(NodeArray.at(b).Head.ControllerArray.GetDoCountsDiffer()){
ssAdd << "\r\n - Header: ControllerArray counts differ!";
bUpdate = true;
}
if(NodeArray.at(b).Head.ControllerDataArray.GetDoCountsDiffer()){
ssAdd << "\r\n - Header: ControllerDataArray counts differ!";
bUpdate = true;
}
for(std::size_t c = 0; c < NodeArray.at(b).Head.Controllers.size(); ++c){
Controller & ctrl = NodeArray.at(b).Head.Controllers.at(c);
if( (ctrl.nControllerType == CONTROLLER_HEADER_POSITION ||
ctrl.nControllerType == CONTROLLER_HEADER_ORIENTATION) &&
ctrl.nAnimation.Valid() &&
ctrl.nUnknown2 == ctrl.nControllerType + 8){}
else if(!ctrl.nUnknown2.Valid() &&
( (ctrl.nControllerType != CONTROLLER_HEADER_POSITION &&
ctrl.nControllerType != CONTROLLER_HEADER_ORIENTATION) ||
!ctrl.nAnimation.Valid()) ){}
else{