-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMSH.h
4209 lines (3418 loc) · 116 KB
/
MSH.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 <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <sstream>
#include <vector>
#include <string_view>
#include "Material.h"
#include "Model.h"
#include <bitset>
#include <regex>
#include <cstdint>
// Object that holds all data on the MSH as well as functions
// for all required operations
class MSH
{
public:
// Sets the msh filename property
void SetMSHFilename(std::string Fname);
std::string GetMSHFilename();
// Read the MSH into memory and populate MSH object
bool ReadMSH();
// Renames the selected material
void RenameMaterial(unsigned short Selected, std::string name);
// Toggles the selected flag from the selected material
void SetFlag(unsigned short Selected, unsigned short Flag, unsigned short Value);
// Sets the RenderType of the selected material
void SetRT(unsigned short Selected, unsigned short RT);
// Sets Data0 value of selected material
void SetData0(unsigned short Selected, unsigned short V);
// Sets Data1 value of selected material
void SetData1(unsigned short Selected, unsigned short V);
// Sets texture name of TX0D
void SetTX0D(unsigned short Selected, std::string TexName);
// Sets texture name of TX1D
void SetTX1D(unsigned short Selected, std::string TexName);
// Sets texture name of TX2D
void SetTX2D(unsigned short Selected, std::string TexName);
// Sets texture name of TX3D
void SetTX3D(unsigned short Selected, std::string TexName);
// Set BGRA value for diffuse
void SetDiffuseGBRA(unsigned short Selected, float RGBA[4]);
// Set BGRA value for diffuse
void SetAmbientGBRA(unsigned short Selected, float RGBA[4]);
// Set BGRA value for diffuse
void SetSpecularGBRA(unsigned short Selected, float RGBA[4]);
// Set BGRA value for diffuse
void SetSpecularDecay(unsigned short Selected, unsigned int Value);
// Renames the selected model
void RenameModel(unsigned short Selected, std::string name);
// Sets the parent of the modl
void SetModelParent(unsigned short Selected, unsigned short ModelIndex);
// Sets the visibility of the selected modl
void SetModelVisibility(unsigned short Selected, unsigned short Visible);
// Sets the MATI of the selected SEGM
void SetClusterMaterial(unsigned short modl, unsigned short cluster, unsigned short material);
// Sets the name of the CTEX
void SetClothTex(unsigned short modl, std::string TexName);
// Removes vertex colors from the modl
void RemoveColors(unsigned short Selected);
// Sets the vertex color CLRB chunk
void SetCLRB(unsigned short Selected, unsigned short Cluster, unsigned short RGBA[4]);
// Displays all models according to specifications
void ListModels();
// Displays all materials according to specifications
void ListMaterials();
// Make any needed adjustments/edits to Data unsigned char array before writing
void PrepMSHForWrite();
// Write the MSH object to file
bool WriteMSH();
// Returns whether the MSH has been altered and needs to be written
bool MSHChanged();
// Imports MODL chunk
bool ImportMODL();
// Exports MODL chunk
bool ExportMODL(unsigned short ModelIndex);
private:
// String which holds the filename
std::string FileName;
// Size of the MSH file
size_t Size = 0;
// Char array that will hold data
unsigned char* Data = nullptr;
// String View used for all reading operations
std::string_view sv;
// Number of materials (MATD chunks attained from MATL chunk)
uint32_t MaterialCount = 0;
// Number of Models (MODL chunks)
uint32_t ModelCount = 0;
// Whether or not the model has been edited
bool CHANGED = false;
// Vector of material objects
std::vector<Material> Materials;
// Vector of model objects
std::vector<Model> Models;
// Vector that holds all MATD chunks
std::vector<unsigned char> MATD_Chunks;
// Vector that holds all MODL chunks
std::vector<unsigned char> MODL_Chunks;
// File positions for easy seeking
size_t HEDR_Size = 0;
size_t MATL_Count_Position = 0;
size_t MATL_Position = 0;
size_t MATL_Size = 0;
size_t MSH2_Position = 0;
size_t MSH2_Size = 0;
size_t BLN2_Position = 0;
size_t SKL2_Position = 0;
size_t ANM2_Position = 0;
// Read and save data concerning the material list
void ReadMATL();
// Populate the materials vector and save material info
void ReadMATD();
// Process DATA chunk for each material
void ReadDATA();
// Process ATRB chunk for each material
void ReadATRB();
// Process TX0D chunk for each material
void ReadTX0D();
// Process TX1D chunk for each material
void ReadTX1D();
// Process TX2D chunk for each material
void ReadTX2D();
// Process TX3D chunk for each material
void ReadTX3D();
// Populate the models vector and save modl info
void ReadMODL();
// Gets PRNT info on a parent MODL chunk
void ReadPRNT();
// Gets GEOM info on a parent MODL chunk
void ReadGEOM();
// Gets FLGS info from parent MODL chunk
void ReadFLGS();
// Gets CLTH info from parent MODL chunk
void ReadCLTH();
// Populates segment vector of MODL chunk
void ReadSEGM();
// Read MATI of each segment
void ReadMATI();
// Read CLRL of each segment if present
void ReadCLRL();
// Read CLRB of each segment if present
void ReadCLRB();
// Creates a new MATL chunk
std::vector<unsigned char> Create_MATL_Chunk();
// Creates a new MATD chunk from a material object
std::vector<unsigned char> Create_MATD_Chunk(Material Mat);
// Creates a new MODL chunk from Model object
std::vector<unsigned char> Create_MODL_Chunk(Model MODL);
// Make adjustments just for Materials
void PrepMatForWrite();
// Make adjustments just for models
void PrepModelForWrite();
// Returns position of specified chunk, or 0 if not found
size_t GetChunk(std::string header, size_t position);
// Pads a string by multiple of four with nulls
void PadString(std::vector<unsigned char>& str);
// Pads a string by multiple of four with nulls
void NullTerminate(std::vector<unsigned char>& str);
// So that View can interact with the MSH
friend class View;
};
// Creates a new MATL chunk to be written to file
inline std::vector<unsigned char> MSH::Create_MATL_Chunk()
{
// Get MATL size by size of MATD chunks
MATL_Size = (MATD_Chunks.size() + 4);
// Create a vector of bytes to be the new MATL chunk
std::vector<unsigned char> MATL_STR = { 'M', 'A', 'T', 'L' };
// Temp arrays to read in 4 byte sizes
char MATLSize[4] = { '\x00', '\x00', '\x00', '\x00' };
char MATCount[4] = { '\x00', '\x00', '\x00', '\x00' };
// Write to a stringstream the MATL chunk size, then read it
std::stringstream s;
s.setf(std::ios::binary);
s.write(reinterpret_cast<char*>(&MATL_Size), 4);
s.read(MATLSize, 4);
// Add the MATL size we just got to the vector
for (unsigned short a = 0; a < 4; a++)
MATL_STR.push_back((unsigned char)MATLSize[a]);
// Write to a stringstream the material count then read it
std::stringstream t;
t.setf(std::ios::binary);
t.write(reinterpret_cast<char*>(&MaterialCount), 4);
t.read(MATCount, 4);
// Add the material count we just got
for (unsigned short b = 0; b < 4; b++)
MATL_STR.push_back(MATCount[b]);
// Verbose output
if (DEBUG)
std::cout << "\n Create_MATL_Chunk: MATL chunk created! Size is "
<< MATL_STR.size() << std::endl;
return MATL_STR;
}
// Returns a MATD chunk as a unsigned char vector from a Material object
inline std::vector<unsigned char> MSH::Create_MATD_Chunk(Material Mat)
{
// Only bother if it's changed. Otherwise just copy to vector and push it
if (Mat.MATDChanged)
{
// Recalculate MATD_Size
// Add together constants DATA_SIZE (52 + 4 + 4 = 60) and ATRB_SIZE (4 + 4 + 4 = 12) for sure
// Add MatName_Size and Tex0, Tex1, Tex2, and Tex3 Sizes if applicable
unsigned int TempSize = 72 + static_cast<size_t>(Mat.MatName_Size) + 8;
if (Mat.TX0D_Size > 0 && !Mat.TX0D.empty())
TempSize += static_cast<size_t>(Mat.TX0D_Size) + 8;
if (Mat.TX1D_Size > 0 && !Mat.TX1D.empty())
TempSize += static_cast<size_t>(Mat.TX1D_Size) + 8;
if (Mat.TX2D_Size > 0 && !Mat.TX2D.empty())
TempSize += static_cast<size_t>(Mat.TX2D_Size) + 8;
if (Mat.TX3D_Size > 0 && !Mat.TX3D.empty())
TempSize += static_cast<size_t>(Mat.TX3D_Size) + 8;
Mat.MATD_Size = TempSize;
// Start off MATD and NAME vectors
std::vector<unsigned char> MATD_STR = { 'M', 'A', 'T', 'D' };
std::vector<unsigned char> NAME_STR = { 'N', 'A', 'M', 'E' };
// Temp arrays to read in sizes
char MATDSize[4] = { '\x00', '\x00', '\x00', '\x00' };
char NAMESize[4] = { '\x00', '\x00', '\x00', '\x00' };
// Write to a stringstream the MATD chunk size, then read it
std::stringstream s;
s.setf(std::ios::binary);
s.write(reinterpret_cast<char*>(&Mat.MATD_Size), 4);
s.read(MATDSize, 4);
// Write to a stringstream the NAME chunk size, then read it
std::stringstream t;
t.setf(std::ios::binary);
t.write(reinterpret_cast<char*>(&Mat.MatName_Size), 4);
t.read(NAMESize, 4);
// Add the sizes we just got to our vectors
for (unsigned short a = 0; a < 4; a++)
{
MATD_STR.push_back((unsigned char)MATDSize[a]);
NAME_STR.push_back((unsigned char)NAMESize[a]);
}
// Add the name chunk to the MATD
for (unsigned short m = 0; m < 8; m++)
MATD_STR.push_back(NAME_STR[m]);
for (unsigned short b = 0; b < Mat.MatName_Size; b++)
MATD_STR.push_back(Mat.MatName[b]);
// Now we have a string MATD chunk up to DATA...
// NOTE: Specular color must have a non-zero value for envmaps to appear!
// Same deal for specular. So check if either and make default if at 0.0
if (int(Mat.RenderType) == 6 || int(Mat.RenderType) == 4 || std::get<1>(Mat.MatFlags[0]))
if (Mat.S_RGBA[0] == 0.0 || Mat.S_RGBA[1] == 0.0 || Mat.S_RGBA[2] == 0.0 || Mat.S_RGBA[3] == 0.0)
{
Mat.S_RGBA[0] = 0.7;
Mat.S_RGBA[1] = 0.7;
Mat.S_RGBA[2] = 0.7;
Mat.S_RGBA[3] = 1.0;
}
std::vector<unsigned char> DATA_STR = { 'D', 'A', 'T', 'A', '\x34', '\x00', '\x00', '\x00' };
// RGBA values as unsigned char arrays
char Diff[4][4];
char Spec[4][4];
char Ambi[4][4];
// v is RGBA value
for (short v = 0; v < 4; v++)
{
// Ok now use stream trick again...
// For diffuse RGBA
std::stringstream w;
w.setf(std::ios::binary);
w.write(reinterpret_cast<char*>(&Mat.D_RGBA[v]), 4);
w.read(&Diff[v][0], 4);
// For specular RGBA
std::stringstream y;
y.setf(std::ios::binary);
y.write(reinterpret_cast<char*>(&Mat.S_RGBA[v]), 4);
y.read(&Spec[v][0], 4);
// For Ambient RGBA
std::stringstream z;
z.setf(std::ios::binary);
z.write(reinterpret_cast<char*>(&Mat.A_RGBA[v]), 4);
z.read(&Ambi[v][0], 4);
}
// Push back all our diffuse values
for (short v = 0; v < 4; v++)
for (short x = 0; x < 4; x++)
DATA_STR.push_back(Diff[v][x]);
// Push back all our specular values
for (short v = 0; v < 4; v++)
for (short x = 0; x < 4; x++)
DATA_STR.push_back(Spec[v][x]);
// Push back all our ambient values
for (short v = 0; v < 4; v++)
for (short x = 0; x < 4; x++)
DATA_STR.push_back(Ambi[v][x]);
// alright now finally push back the specular decay
char SpecDecay[4] = { '\x00', '\x00', '\x00', '\x00' };
std::stringstream z;
z.setf(std::ios::binary);
z.write(reinterpret_cast<char*>(&Mat.S_Decay), 4);
z.read(SpecDecay, 4);
for (unsigned short v = 0; v < 4; v++)
DATA_STR.push_back((unsigned char)SpecDecay[v]);
// Now make the ATRB chunk
std::vector<unsigned char> ATRB_STR = { 'A', 'T', 'R', 'B','\x04','\x00','\x00','\x00' };
ATRB_STR.push_back(Mat.CalculateATRB());
ATRB_STR.push_back(Mat.RenderType);
ATRB_STR.push_back(Mat.Data0);
ATRB_STR.push_back(Mat.Data1);
// Now add TX0D chunks if applicable
std::vector<unsigned char> TX0D_STR;
if (Mat.TX0D.size() > 0)
{
TX0D_STR = { 'T', 'X', '0', 'D' };
char TX0DSize[4] = { '\x00', '\x00', '\x00', '\x00' };
// Push size
std::stringstream u;
u.setf(std::ios::binary);
u.write(reinterpret_cast<char*>(&Mat.TX0D_Size), 4);
u.read(TX0DSize, 4);
for (unsigned short d = 0; d < 4; d++)
{
TX0D_STR.push_back((unsigned char)TX0DSize[d]);
}
// Push name to string
for (unsigned char ch : Mat.TX0D)
TX0D_STR.push_back(ch);
}
// Now add TX1D chunks if applicable
std::vector<unsigned char> TX1D_STR;
if (Mat.TX1D.size() > 0)
{
TX1D_STR = { 'T', 'X', '1', 'D' };
char TX1DSize[4] = { '\x00', '\x00', '\x00', '\x00' };
// Push size
std::stringstream u;
u.setf(std::ios::binary);
u.write(reinterpret_cast<char*>(&Mat.TX1D_Size), 4);
u.read(TX1DSize, 4);
for (unsigned short d = 0; d < 4; d++)
{
TX1D_STR.push_back((unsigned char)TX1DSize[d]);
}
// Push name to string
for (unsigned char ch : Mat.TX1D)
TX1D_STR.push_back(ch);
}
// Now add TX2D chunks if applicable
std::vector<unsigned char> TX2D_STR;
if (Mat.TX2D.size() > 0)
{
TX2D_STR = { 'T', 'X', '2', 'D' };
char TX2DSize[4] = { '\x00', '\x00', '\x00', '\x00' };
// Push size
std::stringstream u;
u.setf(std::ios::binary);
u.write(reinterpret_cast<char*>(&Mat.TX2D_Size), 4);
u.read(TX2DSize, 4);
for (unsigned short d = 0; d < 4; d++)
{
TX2D_STR.push_back((unsigned char)TX2DSize[d]);
}
// Push name to string
for (unsigned char ch : Mat.TX2D)
TX2D_STR.push_back(ch);
}
// Now add TX3D chunks if applicable
std::vector<unsigned char> TX3D_STR;
if (Mat.TX3D.size() > 0)
{
TX3D_STR = { 'T', 'X', '3', 'D' };
char TX3DSize[4] = { '\x00', '\x00', '\x00', '\x00' };
// Push size
std::stringstream u;
u.setf(std::ios::binary);
u.write(reinterpret_cast<char*>(&Mat.TX3D_Size), 4);
u.read(TX3DSize, 4);
for (unsigned short d = 0; d < 4; d++)
{
TX3D_STR.push_back((unsigned char)TX3DSize[d]);
}
// Push name to string
for (unsigned char ch : Mat.TX3D)
TX3D_STR.push_back(ch);
}
// All child chunks added! This is the complete MATD chunk in string
std::vector<unsigned char> MATD;
// Continue to push back all chunks into the MATD
for (unsigned char l : MATD_STR)
MATD.push_back(l);
for (unsigned char f : DATA_STR)
MATD.push_back(f);
for (unsigned char g : ATRB_STR)
MATD.push_back(g);
if (!TX0D_STR.empty())
for (unsigned char h : TX0D_STR)
MATD.push_back(h);
if (!TX1D_STR.empty())
for (unsigned char i : TX1D_STR)
MATD.push_back(i);
if (!TX2D_STR.empty())
for (unsigned char j : TX2D_STR)
MATD.push_back(j);
if (!TX3D_STR.empty())
for (unsigned char k : TX3D_STR)
MATD.push_back(k);
// Verbose output
if (DEBUG)
std::cout << "\n Create_MATD_Chunk: MATD chunk created! Size is "
<< MATD.size() << std::endl;
return MATD;
}
else
{
uint32_t TempSize = Mat.MATD_Size + 8;
std::vector<unsigned char> MATD;
MATD.reserve(TempSize);
std::string_view chunk = sv.substr((Mat.MATD_Position - 4), TempSize);
for (size_t ch = 0; ch < TempSize; ch++)
MATD.push_back((unsigned char)chunk[ch]);
// Verbose output
if (DEBUG)
std::cout << "\n Create_MATD_Chunk: MATD chunk created! Size is "
<< MATD.size() << std::endl;
return MATD;
}
}
// Returns a MODL chunk as a unsigned char vector from a Model object
// TODO: have to find way to remove CLRL and CLRB chunks!
inline std::vector<unsigned char> MSH::Create_MODL_Chunk(Model MODL)
{
uint32_t TempSize = (uint32_t)MODL.MODL_Size + 8;
// Our new MODL chunk that we'll operate on and return
std::vector<unsigned char> MODEL;
MODEL.reserve(TempSize);
// Create substring of the MODL chunk
std::string_view chunk = sv.substr( (MODL.MODL_Position - 4), TempSize);
// Now copy it over to the vector
for (size_t ch = 0; ch < TempSize; ch++)
MODEL.push_back((unsigned char)chunk[ch]);
// So instead of getchunk use std::find in the sv chunk
// Only bother if it's changed, otherwise just pack it and push it
if (MODL.MODLChanged)
{
// If the NAME has changed...
if (MODL.CHANGED[1])
{
size_t pos = chunk.find("NAME", 8) + 4;
unsigned int OldNAMESize = MODL.OG_Value[1];
signed long long Difference = OldNAMESize - MODL.Name_Size;
char NAMESize[4] = { '\x00', '\x00', '\x00', '\x00' };
// Write to a stringstream the NAME chunk size, then read it to NAMESize
std::stringstream t;
t.setf(std::ios::binary);
t.write(reinterpret_cast<char*>(&MODL.Name_Size), 4);
t.read(NAMESize, 4);
// Simply overwrite our NAME size to the vector
for (short C = 0; C < 4; C++)
MODEL[pos + C] = (unsigned char)NAMESize[C];
// Push up position to NAME
pos += 4;
// Erase old name from the vector
MODEL.erase((MODEL.begin() + pos), (MODEL.begin() + pos + OldNAMESize));
// Insert new name into the vector
for (unsigned short C = 0; C < MODL.Name_Size; C++)
MODEL.insert((MODEL.begin() + pos + C), MODL.Name[C]);
// Increase positions of items after NAME chunk
// If New name is smaller...
if (Difference > 0)
{
MODL.PRNT_Position -= static_cast<size_t>(std::abs(Difference));
MODL.FLGS_Position -= static_cast<size_t>(std::abs(Difference));
MODL.CTEX_Position -= static_cast<size_t>(std::abs(Difference));
for (unsigned short E = 0; E < MODL.Segments.size(); E++)
{
MODL.Segments.at(E).SEGM_Position -= static_cast<size_t>(std::abs(Difference));
MODL.Segments.at(E).MATI_Position -= static_cast<size_t>(std::abs(Difference));
MODL.Segments.at(E).CLRB_Position -= static_cast<size_t>(std::abs(Difference));
MODL.Segments.at(E).CLRL_Position -= static_cast<size_t>(std::abs(Difference));
}
}
// If New name is larger
else if (Difference < 0)
{
MODL.PRNT_Position += static_cast<size_t>(std::abs(Difference));
MODL.FLGS_Position += static_cast<size_t>(std::abs(Difference));
MODL.CTEX_Position += static_cast<size_t>(std::abs(Difference));
for (unsigned short E = 0; E < MODL.Segments.size(); E++)
{
MODL.Segments.at(E).SEGM_Position += static_cast<size_t>(std::abs(Difference));
MODL.Segments.at(E).MATI_Position += static_cast<size_t>(std::abs(Difference));
MODL.Segments.at(E).CLRB_Position += static_cast<size_t>(std::abs(Difference));
MODL.Segments.at(E).CLRL_Position += static_cast<size_t>(std::abs(Difference));
}
}
}
// If the PRNT has changed...
if (MODL.CHANGED[0])
{
// If there even was a PRNT
if (MODL.OG_Value > 0)
{
size_t pos = chunk.find("PRNT", 8) + 4;
unsigned int OldPRNTSize = MODL.OG_Value[0];
signed long Difference = OldPRNTSize - MODL.PRNT_Size;
char PRNTSize[4] = { '\x00', '\x00', '\x00', '\x00' };
// Write to a stringstream the PRNT chunk size, then read it to PRNTSize
std::stringstream t;
t.setf(std::ios::binary);
t.write(reinterpret_cast<char*>(&MODL.PRNT_Size), 4);
t.read(PRNTSize, 4);
// Simply overwrite our PRNT size to the vector
for (short C = 0; C < 4; C++)
MODEL[pos + C] = (unsigned char)PRNTSize[C];
// Push up position to PRNT
pos += 4;
// Erase old name from the vector
MODEL.erase((MODEL.begin() + pos), (MODEL.begin() + pos + OldPRNTSize));
// Insert new name into the vector
for (unsigned short C = 0; C < MODL.PRNT_Size; C++)
MODEL.insert((MODEL.begin() + pos + C), MODL.PRNT[C]);
// Increase positions of items after PRNT chunk
// If New name is smaller...
if (Difference > 0)
{
MODL.FLGS_Position -= std::abs(Difference);
MODL.CTEX_Position -= std::abs(Difference);
for (unsigned short E = 0; E < MODL.Segments.size(); E++)
{
MODL.Segments.at(E).SEGM_Position -= static_cast<size_t>(std::abs(Difference));
MODL.Segments.at(E).MATI_Position -= static_cast<size_t>(std::abs(Difference));
MODL.Segments.at(E).CLRB_Position -= static_cast<size_t>(std::abs(Difference));
MODL.Segments.at(E).CLRL_Position -= static_cast<size_t>(std::abs(Difference));
}
}
// If New name is larger
else if (Difference < 0)
{
MODL.FLGS_Position += std::abs(Difference);
MODL.CTEX_Position += std::abs(Difference);
for (unsigned short E = 0; E < MODL.Segments.size(); E++)
{
MODL.Segments.at(E).SEGM_Position -= static_cast<size_t>(std::abs(Difference));
MODL.Segments.at(E).MATI_Position -= static_cast<size_t>(std::abs(Difference));
MODL.Segments.at(E).CLRB_Position -= static_cast<size_t>(std::abs(Difference));
MODL.Segments.at(E).CLRL_Position -= static_cast<size_t>(std::abs(Difference));
}
}
}
else
{
// Newly made chunk will be inserted after NAME chunk
size_t pos = chunk.find("NAME", 8) + 4 + 4 + MODL.OG_Value[1];
signed short Difference = MODL.PRNT_Size;
char PRNTSize[4] = { '\x00', '\x00', '\x00', '\x00' };
std::vector<unsigned char> PRNT;
PRNT.reserve(static_cast<size_t>(MODL.PRNT_Size) + 8);
PRNT.push_back('P');
PRNT.push_back('R');
PRNT.push_back('N');
PRNT.push_back('T');
// Write to a stringstream the PRNT chunk size, then read it to PRNTSize
std::stringstream t;
t.setf(std::ios::binary);
t.write(reinterpret_cast<char*>(&MODL.PRNT_Size), 4);
t.read(PRNTSize, 4);
// Push the PRNT size to PRNT chunk
for (short C = 0; C < 4; C++)
PRNT.push_back((unsigned char)PRNTSize[C]);
// Push the PRNT name to the PRNT chunk
for (unsigned short C = 0; C < MODL.PRNT_Size; C++)
PRNT.push_back(MODL.PRNT[C]);
unsigned short Size = MODL.PRNT_Size + 8;
// Push the PRNT chunk to MODEL after NAME
for (short C = 0; C < Size; C++)
MODEL.insert((MODEL.begin() + pos + C), PRNT[C]);
// Shift up the other positions
MODL.FLGS_Position += Difference;
MODL.CTEX_Position += Difference;
for (unsigned short E = 0; E < MODL.Segments.size(); E++)
{
MODL.Segments.at(E).SEGM_Position -= static_cast<size_t>(std::abs(Difference));
MODL.Segments.at(E).MATI_Position -= static_cast<size_t>(std::abs(Difference));
MODL.Segments.at(E).CLRB_Position -= static_cast<size_t>(std::abs(Difference));
MODL.Segments.at(E).CLRL_Position -= static_cast<size_t>(std::abs(Difference));
}
}
}
// If the FLGS has changed...
if (MODL.CHANGED[2])
{
// This is unique in that we will rewrite it like MATD
unsigned int OldFLGSValue = MODL.OG_Value[2];
// If was there originally, minus 12 bytes from the position
if (OldFLGSValue == 1 && !MODL.FLGS)
{
// Take us to header start
size_t pos = chunk.find("FLGS", 8);
MODEL.erase((MODEL.begin() + pos), (MODEL.begin() + pos + 12));
// Shift back the positions of chunks after...
MODL.CTEX_Position -= 12;
for (unsigned short E = 0; E < MODL.Segments.size(); E++)
{
MODL.Segments.at(E).SEGM_Position -= static_cast<size_t>(std::abs(12));
MODL.Segments.at(E).MATI_Position -= static_cast<size_t>(std::abs(12));
MODL.Segments.at(E).CLRB_Position -= static_cast<size_t>(std::abs(12));
MODL.Segments.at(E).CLRL_Position -= static_cast<size_t>(std::abs(12));
}
}
// If was not there originally, insert 12 bytes of FLGS data after PRNT or NAME if no PRNT
else if (OldFLGSValue == 0 && MODL.FLGS)
{
size_t pos;
if (MODL.PRNT_Size > 0)
pos = chunk.find("PRNT", 8) + 8 + MODL.PRNT_Size;
else
pos = chunk.find("NAME", 8) + 8 + MODL.Name_Size;
unsigned char FLGS[12] = { 'F', 'L', 'G', 'S', '\x04', '\x00', '\x00', '\x00', '\x01', '\x00', '\x00', '\x00' };
// Insert new FLGS chunk into the vector
for (short C = 0; C < 12; C++)
MODEL.insert((MODEL.begin() + pos + C), FLGS[C]);
// Shift up positions of chunks after... CTEX that's you...
MODL.CTEX_Position += 12;
for (unsigned short E = 0; E < MODL.Segments.size(); E++)
{
MODL.Segments.at(E).SEGM_Position += static_cast<size_t>(std::abs(12));
MODL.Segments.at(E).MATI_Position += static_cast<size_t>(std::abs(12));
MODL.Segments.at(E).CLRB_Position += static_cast<size_t>(std::abs(12));
MODL.Segments.at(E).CLRL_Position += static_cast<size_t>(std::abs(12));
}
}
}
// If the CTEX has changed...
if (MODL.CHANGED[3])
{
size_t pos = chunk.find("CLTH", 8) + 12; // CTEX pos
size_t pos2 = pos - 8; // CLTH pos
size_t pos3 = chunk.find("GEOM", 4) + 12; // GEOM pos
unsigned int OldNAMESize = MODL.OG_Value[3];
signed long Difference = OldNAMESize - MODL.Name_Size;
char NAMESize[4] = { '\x00', '\x00', '\x00', '\x00' };
// Write to a stringstream the CTEX chunk size, then read it to NAMESize
std::stringstream t;
t.setf(std::ios::binary);
t.write(reinterpret_cast<char*>(&MODL.CTEX_Size), 4);
t.read(NAMESize, 4);
// Simply overwrite our CTEX size to the vector
for (short C = 0; C < 4; C++)
MODEL[pos + C] = (unsigned char)NAMESize[C];
// Push up position to CTEX
pos += 4;
// Erase old name from the vector
MODEL.erase((MODEL.begin() + pos), (MODEL.begin() + pos + OldNAMESize));
// Insert new name into the vector
for (unsigned short C = 0; C < MODL.CTEX_Size; C++)
MODEL.insert((MODEL.begin() + pos + C), MODL.CTEX[C]);
// If New name is smaller...
if (Difference > 0)
{
// Resize CLTH and GEOM headers
MODL.CLTH_Size -= static_cast<size_t>(Difference);
MODL.GEOM_Size -= static_cast<size_t>(Difference);
// Offset future positions
for (unsigned short E = 0; 0 < MODL.Segments.size(); E++)
{
MODL.Segments.at(E).CLRB_Position -= static_cast<size_t>(Difference);
MODL.Segments.at(E).CLRL_Position -= static_cast<size_t>(Difference);
MODL.Segments.at(E).MATI_Position -= static_cast<size_t>(Difference);
MODL.Segments.at(E).SEGM_Position -= static_cast<size_t>(Difference);
}
}
else if (Difference < 0)
{
// Resize CLTH and GEOM headers
MODL.CLTH_Size += static_cast<size_t>(Difference);
MODL.GEOM_Size += static_cast<size_t>(Difference);
// Offset future positions
for (unsigned short E = 0; 0 < MODL.Segments.size(); E++)
{
MODL.Segments.at(E).CLRB_Position += static_cast<size_t>(Difference);
MODL.Segments.at(E).CLRL_Position += static_cast<size_t>(Difference);
MODL.Segments.at(E).MATI_Position += static_cast<size_t>(Difference);
MODL.Segments.at(E).SEGM_Position += static_cast<size_t>(Difference);
}
}
// Now overwrite CLTH and GEOM sizes
char CLTHSize[4] = { '\x00', '\x00', '\x00', '\x00' };
// Write to a stringstream the CLTH chunk size, then read it
std::stringstream a;
a.setf(std::ios::binary);
a.write(reinterpret_cast<char*>(&MODL.CLTH_Size), 4);
a.read(CLTHSize, 4);
// Simply overwrite our CLTH size to the vector
for (short C = 0; C < 4; C++)
MODEL[pos2 + C] = (unsigned char)CLTHSize[C];
char GEOMSize[4] = { '\x00', '\x00', '\x00', '\x00' };
// Write to a stringstream the GEOM chunk size, then read it
std::stringstream b;
b.setf(std::ios::binary);
b.write(reinterpret_cast<char*>(&MODL.GEOM_Size), 4);
b.read(GEOMSize, 4);
// Simply overwrite our GEOM size to the vector
for (short C = 0; C < 4; C++)
MODEL[pos3 + C] = (unsigned char)GEOMSize[C];
}
// If the MATI has changed...
if (MODL.CHANGED[4])
{
size_t AlreadyRead = 0;
for (unsigned short C = 0; C < MODL.Segments.size(); C++)
{
size_t pos2 = chunk.find("MATI", AlreadyRead) + 8;
char mati[4] = { '\x00', '\x00', '\x00', '\x00' };
//Write to a std::string stream the MATI, then record it
std::stringstream q;
q.setf(std::ios::binary);
q.write(reinterpret_cast<char*>(&MODL.Segments.at(C).MATI), 4);
q.read(mati, 4);
//Erase old MATI from the vector
MODEL.erase((MODEL.begin() + pos2), (MODEL.begin() + pos2 + 4));
//Insert new mati into the vector
for (unsigned short d = 0; d < 4; d++)
MODEL.insert((MODEL.begin() + pos2 + d), (unsigned char)mati[d]);
AlreadyRead = pos2;
}
}
// If the CLRB has changed...
// NOTE: May need to resize GEOM and SEGM headers
//TODO: MUCH
if (MODL.CHANGED[5])
{
//Now insert/edit CLRB chunks
for (unsigned short C = 0; C < MODL.Segments.size(); C++)
{
if (MODL.Segments.at(C).CLRB_Present)
{
// There's currently a CLRB to write!
if (MODL.Segments.at(C).CLRB_OG)
{
// Find CLRB chunk position
size_t pos2 = chunk.find("CLRB", std::abs(signed long long(MODL.MODL_Position) - signed long long (MODL.Segments.at(C).SEGM_Position)));
if (C != MODL.Segments.size() - 1)
{
// If not last segment
if (pos2 < MODL.Segments.at(C + 1).SEGM_Position)
{
if (pos2 < Size)
{
// Verify in same SEGM chunk
pos2 += 8; // Skip to CLRB value
// Overwrite
for (unsigned short D = 0; D < 4; D++)
MODEL.at(pos2 + D) = MODL.Segments.at(C).CLRB[D];
}
}
}
else
{
if (pos2 < Size)
{
pos2 += 8; // Skip to CLRB value
// Overwrite
for (unsigned short D = 0; D < 4; D++)
MODEL.at(pos2 + D) = MODL.Segments.at(C).CLRB[D];
}
}
}
else
{
// If it was not originally there, create it!
unsigned char CLRB[12] = { 'C', 'L', 'R', 'B', '\x04', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00' };
// Push the CLRB in
for (unsigned short D = 0; D < 4; D++)
CLRB[8 + D] = MODL.Segments.at(C).CLRB[D];
// Find insertion point
size_t pos2 = chunk.find("UV0L", std::abs(signed long long(MODL.MODL_Position) - signed long long (MODL.Segments.at(C).SEGM_Position)));
if (C != MODL.Segments.size() - 1)
{
// If not last segment
if (pos2 < (std::abs(signed long long(MODL.MODL_Position) - signed long long (MODL.Segments.at(C + 1).SEGM_Position))))
{
if (pos2 > Size) // Only if model has UVs should it have vertex colors
{
for (unsigned short E = 0; E < 12; E++) // Insert CLRB chunk
MODEL.insert(MODEL.begin() + pos2 + E, CLRB[E]);
//Offset future positions by +12 bytes and resize heads SEGM and GEOM
MODL.CTEX_Position += 12;
MODL.Segments.at(C).SEGM_Size += 12;
MODL.GEOM_Size += 12;
size_t pos3 = chunk.find("GEOM", 4);
// Resize the GEOM header
char GEOMSize[4] = { '\x00', '\x00', '\x00', '\x00' };
// Write to a stringstream the GEOM chunk size, then read it
std::stringstream a;
a.setf(std::ios::binary);
a.write(reinterpret_cast<char*>(&MODL.GEOM_Size), 4);
a.read(GEOMSize, 4);
// Simply overwrite our GEOM size to the vector
for (short C = 0; C < 4; C++)
MODEL[pos3 + C] = (unsigned char)GEOMSize[C];
// This should be the SEGM position
size_t pos4 = std::abs(signed long long(MODL.MODL_Position) - signed long long(MODL.Segments.at(C).SEGM_Position));
// Resize the SEGM header
char SEGMSize[4] = { '\x00', '\x00', '\x00', '\x00' };