-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTemplate.cpp
More file actions
7181 lines (6651 loc) · 346 KB
/
Copy pathTemplate.cpp
File metadata and controls
7181 lines (6651 loc) · 346 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
/* File: Template.cpp
Project: Kanabo
Purpose: Defines the classes for every type of game data. An instance of one of these classes represents a single game data resource.
Notes: Used by GameData to compose structured resources in memory from a game data file. See GameData.cpp's header comment for terminology.
Margin Guide: |----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- -----|
Copyright 2026 Iritscen */
#include "Template.hpp"
#include <algorithm> // needed on Windows for std::sort
#include <array> // needed on Windows for std::array
#include <cstdio> // for fwrite() and fflush()
#include <cstdlib> // for std::strtod()
#include <format> // needed on Windows for std::format
#include <iomanip> // needed on Windows for std::quoted
#include <iostream>
#if MAC
#include <unistd.h> // for close() and mkstemp()
#endif
using std::array;
using std::cerr;
using std::copy;
using std::cout;
using std::format;
using std::function;
using std::make_unique;
using std::map;
using std::max;
using std::min;
using std::optional;
using std::pair;
using std::reference_wrapper;
using std::quoted;
using std::sort;
using std::span;
using std::stable_sort;
using std::string;
using std::to_string;
using std::unique_ptr;
using std::vector;
// Per-format order helpers
namespace
{
// WinAlpha6 PC1_0 MacBeta4 PC1_1 PS2
constexpr array<int, 5> kAlphaOnly(int n) {return { n, kOrderAbsent, kOrderAbsent, kOrderAbsent, kOrderAbsent};}
constexpr array<int, 5> kWindowsOnly(int n) {return {n, n, kOrderAbsent, kOrderAbsent, kOrderAbsent};}
constexpr array<int, 5> kRetailOnly(int n) {return { kOrderAbsent, n, n, n, n};}
constexpr array<int, 5> kMacAndPS2(int n) {return { kOrderAbsent, kOrderAbsent, n, n, n};}
constexpr array<int, 5> kMacOnly(int n) {return { kOrderAbsent, kOrderAbsent, n, n, kOrderAbsent};}
constexpr array<int, 5> kPS2Only(int n) {return { kOrderAbsent, kOrderAbsent, kOrderAbsent, kOrderAbsent, n};}
constexpr array<int, 5> kNotPS2(int n) {return { n, n, n, n, kOrderAbsent};}
auto FormatUsesSepForBINA = [](Format f) {return (f >= Format::PC1_1);}; // BINA is found in separate file starting with v1.1, not beta 4
auto BAP = [](uint32_t offset) {return (32 - (offset % 32)) % 32;}; // byte alignment padding
}
/****************************** Template ******************************/
// Fundamental base class used for polymorphic collections of Template____ objects; it is subclassed in order to create structs designed for the
// data payload of each type of game resource. See TemplateONCC for model comments that apply to all child classes.
Template::Template(string tagName, string instName, Format dataFormat)
: vwvlaSizeOffset_(0x0),
hasLinks_(false),
hasAuxData_(false),
hasPaletteData_(false),
hasSoundData_(false),
certified_(false),
tag_(std::move(tagName)),
name_(std::move(instName)),
origFormat_(dataFormat),
curFormat_(dataFormat),
instDataSize_(0),
auxDataSize_(0),
isPlaceholder_(false),
isUnnamed_(false),
index_("index", 0),
level_("level", 1),
indexNew_("index (for writing)"),
levelNew_("level (for writing)"),
fields_{&index_, &level_}
{
}
// Sort "fields_" in place for the given format: sort by authored order (present fields first in ascending order, absent fields last), then walk
// the sorted present fields to compute each one's offset from the cumulative size of preceding fields. After this call, callers can iterate
// "fields_" directly and break when field->offset() == kOffsetAbsent.
void Template::sortFieldsFor(Format fmt)
{
// Sort by order for this format: present fields first (ascending), absent fields last
stable_sort(fields_.begin(), fields_.end(), [fmt](Field * a, Field * b) // NOLINT(bugprone-nondeterministic-pointer-iteration-order)
{
int oa = a->orderFor(fmt), ob = b->orderFor(fmt);
if (oa == kOrderAbsent) return false;
if (ob == kOrderAbsent) return true;
return oa < ob;
});
// Compute offsets from accumulated sizes and set "curFormat_" on each field
uint32_t runningOffset = 0;
for (auto * field : fields_)
{
field->setCurrentFormat(fmt);
if (field->orderFor(fmt) == kOrderAbsent)
{
field->offset() = kOffsetAbsent;
continue;
}
field->offset() = runningOffset;
runningOffset += field->size();
}
// Save our newly calculated size
instDataSize_ = runningOffset;
}
// Same as sortFieldsFor() but operates on "fieldsAux_" and updates "auxDataSize_"
// Note: Templates that fully override processAuxData() (bypassing this call) are responsible for setting "auxDataSize_" themselves
void Template::sortFieldsAuxFor(Format fmt)
{
stable_sort(fieldsAux_.begin(), fieldsAux_.end(), [fmt](Field * a, Field * b) // NOLINT(bugprone-nondeterministic-pointer-iteration-order)
{
int oa = a->orderFor(fmt), ob = b->orderFor(fmt);
if (oa == kOrderAbsent) return false;
if (ob == kOrderAbsent) return true;
return oa < ob;
});
uint32_t runningOffset = 0;
for (auto * field : fieldsAux_)
{
field->setCurrentFormat(fmt);
if (field->orderFor(fmt) == kOrderAbsent)
{
field->offset() = kOffsetAbsent;
continue;
}
field->offset() = runningOffset;
runningOffset += field->size();
}
auxDataSize_ = runningOffset;
}
// Process full data payload from the level data file
void Template::processData(span<char> dataPacket)
{
// Give the template a chance to create and add its variable array fields to "fields_" before we iterate over them
bool fwvlaSafetyExceeded = false;
uint32_t arraySize = 0, arraySizeToUse = 0;
static constexpr uint32_t kVarArraySizeSafety = 300'000; // largest observed array size is AKOT IDXA for alpha's level18 which is 200,300
if (hasFWVarArray())
{
if (fwvlaSizeOffset_.second == 16)
{
uint16_t arraySize16 = 0;
DataIO::ReadNum(arraySize16, dataPacket, fwvlaSizeOffset_.first);
arraySize = arraySize16;
}
else
DataIO::ReadNum(arraySize, dataPacket, fwvlaSizeOffset_.first);
// Important safety check in the event that we erroneously read a very large number from the data
if (arraySize > kVarArraySizeSafety)
{
arraySizeToUse = kVarArraySizeSafety;
fwvlaSafetyExceeded = true;
}
else
arraySizeToUse = arraySize;
createFWVarArray(static_cast<int32_t>(arraySizeToUse));
}
// Sort fields for "origFormat_" and set "curFormat_" member in each field
sortFieldsFor(origFormat_);
// Read in the data for each field
for (auto * field : fields_)
{
if (field->offset() == kOffsetAbsent)
break;
DataIO::ReadData(field->var(), dataPacket, field->offset(), field->fixedSize());
}
// Adjust these values for the left-shifts Bungie applied to them when saving to disk
index_.val() >>= 8;
level_.val() >>= 25;
postProcessData(dataPacket);
if (fwvlaSafetyExceeded)
cerr << "Warning: Fixed-width variable array size of " << arraySize << " in instance " << quoted(name_) << " (#" << index_.val()
<< ") exceeds the safety limit of " << kVarArraySizeSafety << "; capped at " << kVarArraySizeSafety << ".\n";
}
// Iterate over "fieldsAux_" to process the aux data payload (if there is one). Calls prepareAuxFields() first so that subclasses can determine
// the size of any dynamic fields, then sortFieldsAuxFor() to compute per-field offsets (also sets "auxDataSize_"), then reads each field at
// auxFieldsBaseOffset() + field offset.
void Template::processAuxData(vector<char> & dataPacket)
{
// Give the template a chance to create and add variable array fields to "auxFields_" before we iterate over them
bool vwvlaSafetyExceeded = false;
uint32_t arraySize = 0, arraySizeToUse = 0, nextObjSize = 0, lastObjSize = 0;
static constexpr uint32_t kVarArraySizeSafety = 300; // largest observed array size is 138 in the BINA CHAR of level8
static constexpr uint32_t kObjSizeSafety = 10'000; // largest observed object size is 5416 bytes for MELE
if (vwvlaSizeOffset_)
{
size_t sizeOffset = auxFieldsBaseOffset() + vwvlaSizeOffset_;
DataIO::ReadNum(nextObjSize, dataPacket, sizeOffset);
vwvlaObjectSizes_.clear();
while (nextObjSize > 0)
{
if (nextObjSize > kObjSizeSafety)
{
cerr << "Warning: Next object size of " << nextObjSize << " exceeds object size safety limit of " << kObjSizeSafety << ".\n";
break;
}
arraySize++;
vwvlaObjectSizes_.push_back(nextObjSize);
sizeOffset += nextObjSize + 4; // advance past this int32 field and the next object to find the next size
lastObjSize = nextObjSize;
if (not DataIO::ReadNum(nextObjSize, dataPacket, sizeOffset))
{
cerr << "Warning: Failure to read next object size in array.\n";
break;
}
}
// Important safety check in the event that we erroneously read a very large number from the data
if (arraySize > kVarArraySizeSafety)
{
cerr << "Warning: Variable-width variable array size of " << arraySize << " in instance " << quoted(name_)
<< " exceeds the safety limit of " << kVarArraySizeSafety << "; capping at " << kVarArraySizeSafety << ".\n";
arraySizeToUse = kVarArraySizeSafety;
}
else
arraySizeToUse = arraySize;
createVWVarArray(static_cast<int32_t>(arraySizeToUse), lastObjSize, dataPacket);
}
if (fieldsAux_.empty())
return;
prepareAuxFields();
sortFieldsAuxFor(origFormat_);
size_t baseOffset = auxFieldsBaseOffset();
for (auto * field : fieldsAux_)
{
if (field->offset() == kOffsetAbsent)
break;
DataIO::ReadData(field->var(), dataPacket, field->offset() + baseOffset, field->fixedSize());
}
postProcessAux();
if (vwvlaSafetyExceeded)
cerr << "Warning: Variable-width variable array size of " << arraySize << " in instance " << quoted(name_) << " (#" << index_.val()
<< ") exceeds the safety limit of " << kVarArraySizeSafety << "; capped at " << kVarArraySizeSafety << ".\n";
}
// Get the names of any resources linked by name from this one. GameData must pass us a lookup function in order to allow us to see the other
// instances in GameData's memory. Then we pass it to any FieldLink member that is possibly within this Template subclass so it can perform the
// actual lookup. It ain't pretty but it seemed like the best solution for sending information from GameData -> Template -> Field. Alternately
// GameData could iterate all fields in each instance as it creates them and then attempt to call FieldLink::resolveLink() on them, but this
// would be piercing two layers directly, GameData -> Field, and I didn't like that thought too much.
void Template::processLinks(const function<string(int)> & lookupFn)
{
if (lookupFn)
{
// Iterate over every field and try to call FieldLink::resolveLink(), passing it the lookup function we got from GameData; the method is
// only defined for class FieldLink. We could test with dynamic_cast if each field is class FieldLink and only call resolveLink() for
// those fields, but it would be much slower.
for (auto * field : fields_)
{
if (field->offset() == kOffsetAbsent)
break;
field->resolveLink(lookupFn);
}
for (auto * field : fieldsAux_)
{
if (field->offset() == kOffsetAbsent)
break;
field->resolveLink(lookupFn);
}
}
}
// Print out every field, relying on each Field___ class's str() method to smartly print its data
string Template::report(bool showOffsets, const string & filterField, const string & filterValue)
{
// Lambda for wildcard field name matching. Supports '-' as a wildcard matching any sequence of characters.
auto fieldNameMatchesPattern = [](const string & fieldName, const string & pattern) -> bool
{
size_t ni = 0, pi = 0, lastStar = string::npos, lastNi = 0;
const size_t n = fieldName.size(), p = pattern.size();
while (ni < n)
{
if (pi < p && pattern[pi] == '-')
{
lastStar = pi++;
lastNi = ni;
}
else if (pi < p && pattern[pi] == fieldName[ni])
{
++ni; ++pi;
}
else if (lastStar != string::npos)
{
pi = lastStar + 1;
ni = ++lastNi;
}
else
return false;
}
while (pi < p && pattern[pi] == '-')
++pi;
return pi == p;
};
// Iterate and print the instance data fields
string s;
for (auto * field : fields_)
{
if (field->offset() == kOffsetAbsent)
break;
if (not filterField.empty() && not fieldNameMatchesPattern(field->name(), filterField))
continue;
if (not filterValue.empty() && not field->matchesFilter(filterValue))
continue;
if (field->show() /*&& field->name() != "index" && field->name() != "level"*/)
{
if (showOffsets)
s += format(" {} (0x{:X}): {}\n", field->name(), field->offset(), field->str());
else
s += format(" {}: {}\n", field->name(), field->str());
}
}
// Iterate and print the aux data fields
for (auto * field : fieldsAux_)
{
if (field->offset() == kOffsetAbsent)
break;
if (not filterField.empty() && not fieldNameMatchesPattern(field->name(), filterField))
continue;
if (not filterValue.empty() && not field->matchesFilter(filterValue))
continue;
if (field->show())
{
if (showOffsets)
s += format(" {} (0x{:X}): {}\n", field->name(), field->offset(), field->str());
else
s += format(" {}: {}\n", field->name(), field->str());
}
}
return s;
}
// Return just the size of the instance data in the level data file, i.e. the total of all fields in "fields_"
uint32_t Template::dataSize()
{
return instDataSize_;
}
// Return just the size of the instance data in the level data file, i.e. the total of all fields in "fields_"
uint32_t Template::auxDataSize()
{
return auxDataSize_;
}
// Like auxDataSize() but includes the 32-byte alignment padding that OniSplit inserts between sections in multi-section templates (e.g. TRAM)
uint32_t Template::auxDataSizeAligned()
{
if (auxSectionAnchors_.size() <= 1)
return auxDataSize_;
uint32_t total = 0;
for (size_t i = 0; i < auxSectionAnchors_.size(); i++)
{
uint32_t sectionStart = auxSectionAnchors_[i].second->offset();
uint32_t sectionEnd = (i + 1 < auxSectionAnchors_.size())
? auxSectionAnchors_[i + 1].second->offset()
: auxDataSize_;
total += sectionEnd - sectionStart;
if (i + 1 < auxSectionAnchors_.size())
total += BAP(total);
}
return total;
}
// Return total size of data from both the level data file and the raw/separate file
uint32_t Template::totalSize()
{
return instDataSize_ + auxDataSize_;
}
// Stage the value each aux-data offset field should hold when this instance is exported, given the file offset where its aux block will begin.
// The exported value is written to defVar() so the field's original on-disk value (used for reporting) is preserved. The single-section case
// (one contiguous aux blob located by one offset field) is the default; multi-section templates describe their layout in "auxSectionAnchors_",
// each section's offset field pointing at the start of its data within the contiguous blob (the section's first field's combined offset).
void Template::assignExportAuxOffsets(uint32_t auxBlockStart)
{
if (auxSectionAnchors_.empty())
{
for (auto * field : fields_)
if (field->getAuxOffset())
field->defVar() = auxBlockStart;
}
else
{
// Walk each section in order, aligning each one's start to a 32-byte boundary after the previous section ends
uint32_t sectionStart = auxBlockStart;
for (size_t i = 0; i < auxSectionAnchors_.size(); i++)
{
auto & [offsetField, firstField] = auxSectionAnchors_[i];
offsetField->defVar() = sectionStart;
size_t nextPackedOffset = (i + 1 < auxSectionAnchors_.size())
? auxSectionAnchors_[i + 1].second->offset()
: auxDataSize_;
uint32_t sectionEnd = sectionStart + static_cast<uint32_t>(nextPackedOffset - firstField->offset());
sectionStart = sectionEnd + BAP(sectionEnd);
}
}
}
void Template::createKnowledgeVarFields()
{
if (hasFWVarArray())
createFWVarArray(1);
if (vwvlaSizeOffset_)
{
vector<char> emptyData;
createVWVarArray(1, 0, emptyData);
}
}
/**************************** TemplateBINA ****************************/
/* The only purpose of this class is to serve as a parent for the BINA subtypes below; it is not used directly */
TemplateBINA::TemplateBINA(string tagName, string instName, Format dataFormat)
: Template(std::move(tagName), std::move(instName), dataFormat),
binaSize("aux data size", 2),
binaOffset("aux data offset", 3)
{
hasAuxData_ = true;
certified_ = true;
fields_.insert(fields_.end(), {&binaSize, &binaOffset});
}
unique_ptr<Template> TemplateBINA::create(const string & instName, Format dataFormat)
{
return make_unique<TemplateBINA>("BINA", instName, dataFormat);
}
uint32_t TemplateBINA::binaDataSize()
{
return binaSize.val();
}
uint32_t TemplateBINA::binaDataOffset()
{
return binaOffset.val();
}
size_t TemplateBINA::auxFieldsBaseOffset()
{
return binaDataOffset();
}
bool TemplateBINA::hasSepData()
{
// Mac beta 4, which predated the Windows gold master by a few days, still had the BINA data stored in the raw file even though separate
// files had been introduced to the Mac builds
return FormatUsesSepForBINA(origFormat_);
}
/************************* TemplateBINA_OBJC *************************/
TemplateBINA_OBJC::TemplateBINA_OBJC(string tagName, string instName, Format dataFormat)
: TemplateBINA(std::move(tagName), std::move(instName), dataFormat),
binaSubtype("BINA subtype tag", 1),
objcListSize("OBJC list size", 2),
objcVersion("OBJC version", 3)
{
certified_ = true;
objectFlagValues =
{
{OFLocked, 0x1},
{OFPlaced, 0x2},
{OFTemp, 0x3},
{OFGunk, 0x4}
};
objectFlagNames =
{
{OFLocked, "locked"},
{OFPlaced, "placed by tool"},
{OFTemp, "to be deleted"},
{OFGunk, "has gunk in env"}
};
fieldsAux_.insert(fieldsAux_.end(), {&binaSubtype, &objcListSize, &objcVersion});
}
unique_ptr<Template> TemplateBINA_OBJC::create(const string & instName, Format dataFormat)
{
return make_unique<TemplateBINA_OBJC>("BINA/OBJC", instName, dataFormat);
}
/*********************** TemplateBINA_OBJC_CHAR ***********************/
TemplateBINA_OBJC_CHAR::TemplateBINA_OBJC_CHAR(string instName, Format dataFormat)
: TemplateBINA_OBJC("BINA/OBJC/CHAR", std::move(instName), dataFormat)
{
certified_ = true;
vwvlaSizeOffset_ = 0x0C;
charFlagValues =
{
{IsPlayer, 0x1},
{RandVariant, 0x2},
{NotInitial, 0x4},
{NPC, 0x8},
{MultSpawn, 0x10},
{Spawned, 0x20},
{Unkillable, 0x40},
{Superammo, 0x80},
{Omniscient, 0x100},
{HasLSI, 0x200},
{IsBoss, 0x400},
{HardUpgrade, 0x800},
{NoAutoDrop, 0x1000}
};
charFlagNames =
{
{IsPlayer, "player"},
{RandVariant, "random variant"},
{NotInitial, "not initially spawned"},
{NPC, "non-combatant"},
{MultSpawn, "can multiply spawn"},
{Spawned, "spawned (runtime)"},
{Unkillable, "un-killable"},
{Superammo, "infinite ammo"},
{Omniscient, "omniscient"},
{HasLSI, "has LSI"},
{IsBoss, "boss"},
{HardUpgrade, "upgrade on Hard"},
{NoAutoDrop, "no auto-drop"}
};
jobTypeValues =
{
{JTNone, 0},
{Idle, 1},
{Guard, 2},
{Patrol, 3},
{TeamBattle, 4}
};
jobTypeNames =
{
{JTNone, "None"},
{Idle, "Idle"},
{Guard, "Guard"},
{Patrol, "Patrol"},
{TeamBattle, "Team Battle"}
};
teamNumValues =
{
{Konoko, 0},
{TCTF, 1},
{Syndicate, 2},
{Neutral, 3},
{SecurityGuard, 4},
{RogueKonoko, 5},
{Switzerland, 6},
{SyndicateAccessory, 7}
};
teamNumNames =
{
{Konoko, "Konoko"},
{TCTF, "TCTF"},
{Syndicate, "Syndicate"},
{Neutral, "Neutral"},
{SecurityGuard, "Security"},
{RogueKonoko, "Rogue Konoko"},
{Switzerland, "Switzerland"},
{SyndicateAccessory, "Syndicate Accessory"}
};
alertLevelValues =
{
{Lull, 0},
{Low, 1},
{Medium, 2},
{High, 3},
{Combat, 4}
};
alertLevelNames =
{
{Lull, "Lull"},
{Low, "Low"},
{Medium, "Medium"},
{High, "High"},
{Combat, "Combat"}
};
pursuitModeValues =
{
{PMNone, 0},
{Forget, 1},
{GoTo, 2},
{Wait, 3},
{Look, 4},
{Move, 5},
{Hunt, 6},
{Glance, 7}
};
pursuitModeNames =
{
{PMNone, "None"},
{Forget, "Forget"},
{GoTo, "GoTo"},
{Wait, "Wait"},
{Look, "Look"},
{Move, "Move"},
{Hunt, "Hunt"},
{Glance, "Glance"}
};
pursuitLostValues =
{
{ReturnToJob, 0},
{KeepLooking, 1},
{FindAlarm, 2}
};
pursuitLostNames =
{
{ReturnToJob, "Return to job"},
{KeepLooking, "Keep looking"},
{FindAlarm, "Find alarm"}
};
}
unique_ptr<Template> TemplateBINA_OBJC_CHAR::create(const string & instName, Format dataFormat)
{
return make_unique<TemplateBINA_OBJC_CHAR>(instName, dataFormat);
}
void TemplateBINA_OBJC_CHAR::createVWVarArray(int32_t arraySize, uint32_t lastObjSize, vector<char> & /*dataPacket*/)
{
// Some levels in Win A6 have a different CHAR format than others, so here we validate that the object size is one of the three known values
// and then we make a note of which alpha variant of CHAR is in play so that we can hide/show a single field that differs between them
uint32_t correctWidthRel = 544, correctWidthAl1 = 448, correctWidthAl2 = 480;
bool alphaVariant2 = (origFormat_ == Format::WinAlpha6 && lastObjSize == correctWidthAl2);
if (lastObjSize > 0) // GameDataManager::reportKnowledge() will pass us 0 because it's manufacturing a dummy object
{
if ((origFormat_ > Format::WinAlpha6 && lastObjSize != correctWidthRel) ||
((origFormat_ == Format::WinAlpha6 && lastObjSize != correctWidthAl1) &&
(origFormat_ == Format::WinAlpha6 && lastObjSize != correctWidthAl2)))
{
cerr << "Error: CHAR: Incorrect object size (" << lastObjSize << ") passed in for variable array. Expected ";
if (origFormat_ == Format::WinAlpha6)
cerr << correctWidthAl1 << " or " << correctWidthAl2 << ".\n";
else
cerr << correctWidthRel << ".\n";
return;
}
}
const size_t sz = static_cast<size_t>(arraySize);
objcNextSize.reserve(sz + 1); // +1 for the zero terminator at end of list
objcSubtype.reserve(sz);
objcID.reserve(sz);
objcFlags.reserve(sz);
objcPosX.reserve(sz);
objcPosY.reserve(sz);
objcPosZ.reserve(sz);
objcRotX.reserve(sz);
objcRotY.reserve(sz);
objcRotZ.reserve(sz);
charFlags.reserve(sz);
charClass.reserve(sz);
charName.reserve(sz);
weapName.reserve(sz);
fnSpawn.reserve(sz);
fnDie.reserve(sz);
fnNotice.reserve(sz);
fnAlarm.reserve(sz);
fnHurt.reserve(sz);
fnDefeat.reserve(sz);
fnOutOfAmmo.reserve(sz);
fnNoPath.reserve(sz);
healthDelta.reserve(sz);
jobType.reserve(sz);
patrolPathID.reserve(sz);
combatID.reserve(sz);
meleeID.reserve(sz);
neutralID.reserve(sz);
invAmmoUse.reserve(sz);
invAmmoDrop.reserve(sz);
invCellsUse.reserve(sz);
invCellsDrop.reserve(sz);
invHypoUse.reserve(sz);
invHypoDrop.reserve(sz);
invShieldUse.reserve(sz);
invShieldDrop.reserve(sz);
invCloakUse.reserve(sz);
invCloakDrop.reserve(sz);
invUnusedUse.reserve(sz);
invUnusedDrop.reserve(sz);
teamNum.reserve(sz);
clipPerc.reserve(sz);
alertInitial.reserve(sz);
alertMinimum.reserve(sz);
alertJobStart.reserve(sz);
alertInvestigate.reserve(sz);
alarmGroups.reserve(sz);
pursuitStrongUnseen.reserve(sz);
pursuitWeakUnseen.reserve(sz);
pursuitStrongSeen.reserve(sz);
pursuitWeakSeen.reserve(sz);
pursuitLost.reserve(sz);
int s = static_cast<int>(fieldsAux_.size()) + 1; // resume adding fields after TemplateBINA_OBJC's fields were added
int nf = 52; // number of fields that we're adding (OBJC header + CHAR fields)
for (int32_t i = 0; i < arraySize; ++i)
{
objcNextSize.emplace_back("next object size [" + to_string(i) + "]", s + (nf * i) + 0);
objcSubtype.emplace_back("OBJC subtype [" + to_string(i) + "]", s + (nf * i) + 1);
objcID.emplace_back("object ID [" + to_string(i) + "]", s + (nf * i) + 2);
objcFlags.emplace_back("object flags [" + to_string(i) + "]", s + (nf * i) + 3, true, true, 0, 0,
&objectFlagValues, &objectFlagNames);
objcPosX.emplace_back("position X [" + to_string(i) + "]", s + (nf * i) + 4);
objcPosY.emplace_back("position Y [" + to_string(i) + "]", s + (nf * i) + 5);
objcPosZ.emplace_back("position Z [" + to_string(i) + "]", s + (nf * i) + 6);
objcRotX.emplace_back("rotation X [" + to_string(i) + "]", s + (nf * i) + 7);
objcRotY.emplace_back("rotation Y [" + to_string(i) + "]", s + (nf * i) + 8);
objcRotZ.emplace_back("rotation Z [" + to_string(i) + "]", s + (nf * i) + 9);
charFlags.emplace_back("character flags [" + to_string(i) + "]", s + (nf * i) + 10, true, true, 0, 0,
&charFlagValues, &charFlagNames);
charClass.emplace_back("character class [" + to_string(i) + "]", s + (nf * i) + 11, true, true, "", "", 64);
charName.emplace_back("character name [" + to_string(i) + "]", s + (nf * i) + 12, true, true, "", "", 32);
weapName.emplace_back("weapon name [" + to_string(i) + "]", s + (nf * i) + 13, true, true, "", "", 64);
fnSpawn.emplace_back("spawn script [" + to_string(i) + "]", s + (nf * i) + 14, true, true, "", "", 32);
fnDie.emplace_back("death script [" + to_string(i) + "]", s + (nf * i) + 15, true, true, "", "", 32);
fnNotice.emplace_back("notice script [" + to_string(i) + "]", s + (nf * i) + 16, true, true, "", "", 32);
fnAlarm.emplace_back("alarm script [" + to_string(i) + "]", s + (nf * i) + 17, true, true, "", "", 32);
fnHurt.emplace_back("hurt script [" + to_string(i) + "]", s + (nf * i) + 18, true, true, "", "", 32);
if (alphaVariant2)
fnDefeat.emplace_back("defeat script [" + to_string(i) + "]", s + (nf * i) + 19, true, true, "", "", 32);
else
fnDefeat.emplace_back("defeat script [" + to_string(i) + "]", kRetailOnly(s + (nf * i) + 19), true, true, "", "",
32);
fnOutOfAmmo.emplace_back("out of ammo script [" + to_string(i) + "]", kRetailOnly(s + (nf * i) + 20), true, true, "", "",
32);
fnNoPath.emplace_back("no path script [" + to_string(i) + "]", kRetailOnly(s + (nf * i) + 21), true, true, "", "",
32);
healthDelta.emplace_back("health delta [" + to_string(i) + "]", s + (nf * i) + 22);
jobType.emplace_back("job type [" + to_string(i) + "]", s + (nf * i) + 23, true, true, 0, 0, &jobTypeValues,
&jobTypeNames);
patrolPathID.emplace_back("patrol path ID [" + to_string(i) + "]", s + (nf * i) + 24);
combatID.emplace_back("combat ID [" + to_string(i) + "]", s + (nf * i) + 25);
meleeID.emplace_back("melee ID [" + to_string(i) + "]", s + (nf * i) + 26);
neutralID.emplace_back("neutral ID [" + to_string(i) + "]", s + (nf * i) + 27);
invAmmoUse.emplace_back("ballistic ammo (use) [" + to_string(i) + "]", s + (nf * i) + 28);
invAmmoDrop.emplace_back("ballistic ammo (drop) [" + to_string(i) + "]", s + (nf * i) + 29);
invCellsUse.emplace_back("energy cells (use) [" + to_string(i) + "]", s + (nf * i) + 30);
invCellsDrop.emplace_back("energy cells (drop) [" + to_string(i) + "]", s + (nf * i) + 31);
invHypoUse.emplace_back("hypos (use) [" + to_string(i) + "]", s + (nf * i) + 32);
invHypoDrop.emplace_back("hypos (drop) [" + to_string(i) + "]", s + (nf * i) + 33);
invShieldUse.emplace_back("shield (use) [" + to_string(i) + "]", s + (nf * i) + 34);
invShieldDrop.emplace_back("shield (drop) [" + to_string(i) + "]", s + (nf * i) + 35);
invCloakUse.emplace_back("cloak (use) [" + to_string(i) + "]", s + (nf * i) + 36);
invCloakDrop.emplace_back("cloak (drop) [" + to_string(i) + "]", s + (nf * i) + 37);
invUnusedUse.emplace_back("unused item (use) [" + to_string(i) + "]", s + (nf * i) + 38);
invUnusedDrop.emplace_back("unused item (drop) [" + to_string(i) + "]", s + (nf * i) + 39);
teamNum.emplace_back("team [" + to_string(i) + "]", s + (nf * i) + 40, true, true, 0, 0, &teamNumValues,
&teamNumNames);
clipPerc.emplace_back("clip percentage [" + to_string(i) + "]", s + (nf * i) + 41);
alertInitial.emplace_back("initial alert [" + to_string(i) + "]", s + (nf * i) + 42, true, true, 0, 0,
&alertLevelValues, &alertLevelNames);
alertMinimum.emplace_back("minimum alert [" + to_string(i) + "]", s + (nf * i) + 43, true, true, 0, 0,
&alertLevelValues, &alertLevelNames);
alertJobStart.emplace_back("job start alert [" + to_string(i) + "]", s + (nf * i) + 44, true, true, 0, 0,
&alertLevelValues, &alertLevelNames);
alertInvestigate.emplace_back("investigate alert [" + to_string(i) + "]", s + (nf * i) + 45, true, true, 0, 0,
&alertLevelValues, &alertLevelNames);
alarmGroups.emplace_back("alarm groups [" + to_string(i) + "]", s + (nf * i) + 46);
pursuitStrongUnseen.emplace_back("pursuit (strong, unseen) [" + to_string(i) + "]", s + (nf * i) + 47, true, true, 0, 0,
&pursuitModeValues, &pursuitModeNames);
pursuitWeakUnseen.emplace_back("pursuit (weak, unseen) [" + to_string(i) + "]", s + (nf * i) + 48, true, true, 0, 0,
&pursuitModeValues, &pursuitModeNames);
pursuitStrongSeen.emplace_back("pursuit (strong, seen) [" + to_string(i) + "]", s + (nf * i) + 49, true, true, 0, 0,
&pursuitModeValues, &pursuitModeNames);
pursuitWeakSeen.emplace_back("pursuit (weak, seen) [" + to_string(i) + "]", s + (nf * i) + 50, true, true, 0, 0,
&pursuitModeValues, &pursuitModeNames);
pursuitLost.emplace_back("pursuit (lost) [" + to_string(i) + "]", s + (nf * i) + 51, true, true, 0, 0,
&pursuitLostValues, &pursuitLostNames);
}
for (size_t i = 0; i < sz; ++i)
fieldsAux_.insert(fieldsAux_.end(), {&objcNextSize[i], &objcSubtype[i], &objcID[i], &objcFlags[i], &objcPosX[i], &objcPosY[i],
&objcPosZ[i], &objcRotX[i], &objcRotY[i], &objcRotZ[i], &charFlags[i], &charClass[i], &charName[i],
&weapName[i], &fnSpawn[i], &fnDie[i], &fnNotice[i], &fnAlarm[i], &fnHurt[i], &fnDefeat[i],
&fnOutOfAmmo[i], &fnNoPath[i], &healthDelta[i], &jobType[i], &patrolPathID[i], &combatID[i],
&meleeID[i], &neutralID[i], &invAmmoUse[i], &invAmmoDrop[i], &invCellsUse[i], &invCellsDrop[i],
&invHypoUse[i], &invHypoDrop[i], &invShieldUse[i], &invShieldDrop[i], &invCloakUse[i],
&invCloakDrop[i], &invUnusedUse[i], &invUnusedDrop[i], &teamNum[i], &clipPerc[i], &alertInitial[i],
&alertMinimum[i], &alertJobStart[i], &alertInvestigate[i], &alarmGroups[i],
&pursuitStrongUnseen[i], &pursuitWeakUnseen[i], &pursuitStrongSeen[i], &pursuitWeakSeen[i],
&pursuitLost[i]});
// Add the closing zero-value next-object field that terminates the OBJC list
objcNextSize.emplace_back("next object size [end]", s + (nf * arraySize));
fieldsAux_.push_back(&objcNextSize.back());
}
/*********************** TemplateBINA_OBJC_CMBT ***********************/
TemplateBINA_OBJC_CMBT::TemplateBINA_OBJC_CMBT(string instName, Format dataFormat)
: TemplateBINA_OBJC("BINA/OBJC/CMBT", std::move(instName), dataFormat)
{
certified_ = true;
vwvlaSizeOffset_ = 0x0C;
behaviorTypeValues =
{
{BTNone, 0},
{Stare, 1},
{HoldAndFire, 2},
{FiringCharge, 3},
{Melee, 4},
{BarabbasShoot, 5},
{BarabbasAdvance, 6},
{BarabbasMelee, 7},
{SNinjaFireball, 8},
{SNinjaAdvance, 9},
{SNinjaMelee, 10},
{RunForAlarm, 11},
{MMuroMelee, 12},
{MMuroThunderbolt, 13}
};
behaviorTypeNames =
{
{BTNone, "None"},
{Stare, "Stare"},
{HoldAndFire, "Hold And Fire"},
{FiringCharge, "Firing Charge"},
{Melee, "Melee"},
{BarabbasShoot, "Barabbas Shoot"},
{BarabbasAdvance, "Barabbas Advance"},
{BarabbasMelee, "Barabbas Melee"},
{SNinjaFireball, "SNinja Fireball"},
{SNinjaAdvance, "SNinja Advance"},
{SNinjaMelee, "SNinja Melee"},
{RunForAlarm, "Run For Alarm"},
{MMuroMelee, "MMuro Melee"},
{MMuroThunderbolt, "MMuro Thunderbolt"}
};
meleeOverrideValues =
{
{MONo, 0},
{IfPunched, 1},
{Canceled, 2},
{ShortRange, 3},
{MediumRange, 4},
{AlwaysMelee, 5}
};
meleeOverrideNames =
{
{MONo, "No"},
{IfPunched, "If Punched"},
{Canceled, "Canceled"},
{ShortRange, "Short Range"},
{MediumRange, "Medium Range"},
{AlwaysMelee, "Always Melee"}
};
noGunBehaviorValues =
{
{NGMelee, 0},
{Retreat, 1},
{RunToAlarm, 2}
};
noGunBehaviorNames =
{
{NGMelee, "Melee"},
{Retreat, "Retreat"},
{RunToAlarm, "Run To Alarm"}
};
}
unique_ptr<Template> TemplateBINA_OBJC_CMBT::create(const string & instName, Format dataFormat)
{
return make_unique<TemplateBINA_OBJC_CMBT>(instName, dataFormat);
}
void TemplateBINA_OBJC_CMBT::createVWVarArray(int32_t arraySize, uint32_t lastObjSize, vector<char> & /*dataPacket*/)
{
uint32_t correctWidth = (origFormat_ == Format::WinAlpha6 ? 156U : 180U);
if (lastObjSize > 0 && lastObjSize != correctWidth)
{
cerr << "Error: CMBT: Incorrect object size (" << lastObjSize << " vs. " << correctWidth << ") passed in for variable array.\n";
return;
}
const size_t sz = static_cast<size_t>(arraySize);
objcNextSize.reserve(sz + 1); // +1 for the zero terminator at end of list
objcSubtype.reserve(sz);
objcID.reserve(sz);
objcFlags.reserve(sz);
objcPosX.reserve(sz);
objcPosY.reserve(sz);
objcPosZ.reserve(sz);
objcRotX.reserve(sz);
objcRotY.reserve(sz);
objcRotZ.reserve(sz);
profileName.reserve(sz);
combatID.reserve(sz);
combatIDA6.reserve(sz);
behaviorLongRange.reserve(sz);
behaviorMedRange.reserve(sz);
behaviorShortRange.reserve(sz);
behaviorMedRetreat.reserve(sz);
behaviorLongRetreat.reserve(sz);
medRangeThreshold.reserve(sz);
meleeOverride.reserve(sz);
noGunBehavior.reserve(sz);
shortRangeThreshold.reserve(sz);
pursuitDistance.reserve(sz);
panicHurt.reserve(sz);
panicGunfire.reserve(sz);
panicMelee.reserve(sz);
panicSight.reserve(sz);
paddingIDA6.reserve(sz);
alarmSearchDist.reserve(sz);
alarmIgnoreDist.reserve(sz);
alarmAttackDist.reserve(sz);
alarmDamageThreshold.reserve(sz);
alarmFightTimer.reserve(sz);
int s = static_cast<int>(fieldsAux_.size()) + 1; // resume adding fields after TemplateBINA_OBJC's fields were added
int nf = 35; // number of fields that we're adding (OBJC header + CMBT fields)
for (int32_t i = 0; i < arraySize; ++i)
{
objcNextSize.emplace_back("next object size [" + to_string(i) + "]", s + (nf * i) + 0);
objcSubtype.emplace_back("OBJC subtype [" + to_string(i) + "]", s + (nf * i) + 1);
objcID.emplace_back("object ID [" + to_string(i) + "]", s + (nf * i) + 2);
objcFlags.emplace_back("object flags [" + to_string(i) + "]", s + (nf * i) + 3, true, true, 0, 0,
&objectFlagValues, &objectFlagNames);
objcPosX.emplace_back("position X [" + to_string(i) + "]", s + (nf * i) + 4);
objcPosY.emplace_back("position Y [" + to_string(i) + "]", s + (nf * i) + 5);
objcPosZ.emplace_back("position Z [" + to_string(i) + "]", s + (nf * i) + 6);
objcRotX.emplace_back("rotation X [" + to_string(i) + "]", s + (nf * i) + 7);
objcRotY.emplace_back("rotation Y [" + to_string(i) + "]", s + (nf * i) + 8);
objcRotZ.emplace_back("rotation Z [" + to_string(i) + "]", s + (nf * i) + 9);
profileName.emplace_back("profile name [" + to_string(i) + "]", s + (nf * i) + 10, true, true, "", "", 64);
combatID.emplace_back("combat ID [" + to_string(i) + "]", kRetailOnly(s + (nf * i) + 11));
combatIDA6.emplace_back("combat ID (A6) [" + to_string(i) + "]", kAlphaOnly(s + (nf * i) + 12));
behaviorLongRange.emplace_back("long range behavior [" + to_string(i) + "]", s + (nf * i) + 13, true, true, 0, 0,
&behaviorTypeValues, &behaviorTypeNames);
behaviorMedRange.emplace_back("medium range behavior [" + to_string(i) + "]", s + (nf * i) + 14, true, true, 0, 0,
&behaviorTypeValues, &behaviorTypeNames);
behaviorShortRange.emplace_back("short range behavior [" + to_string(i) + "]", s + (nf * i) + 15, true, true, 0, 0,
&behaviorTypeValues, &behaviorTypeNames);
behaviorMedRetreat.emplace_back("medium retreat behavior [" + to_string(i) + "]", s + (nf * i) + 16, true, true, 0, 0,
&behaviorTypeValues, &behaviorTypeNames);
behaviorLongRetreat.emplace_back("long retreat behavior [" + to_string(i) + "]", s + (nf * i) + 17, true, true, 0, 0,
&behaviorTypeValues, &behaviorTypeNames);
medRangeThreshold.emplace_back("medium range threshold [" + to_string(i) + "]", s + (nf * i) + 18);
meleeOverride.emplace_back("melee override behavior [" + to_string(i) + "]", s + (nf * i) + 19, true, true, 0, 0,
&meleeOverrideValues, &meleeOverrideNames);
noGunBehavior.emplace_back("no gun behavior [" + to_string(i) + "]", s + (nf * i) + 20, true, true, 0, 0,
&noGunBehaviorValues, &noGunBehaviorNames);
shortRangeThreshold.emplace_back("short range threshold [" + to_string(i) + "]", s + (nf * i) + 21);
pursuitDistance.emplace_back("pursuit distance [" + to_string(i) + "]", s + (nf * i) + 22);
panicHurt.emplace_back("panic hurt [" + to_string(i) + "]", kRetailOnly(s + (nf * i) + 23));
panicGunfire.emplace_back("panic gunfire [" + to_string(i) + "]", s + (nf * i) + 24);
panicMelee.emplace_back("panic melee [" + to_string(i) + "]", s + (nf * i) + 25);
panicSight.emplace_back("panic sight [" + to_string(i) + "]", s + (nf * i) + 26);
paddingIDA6.emplace_back("padding (alpha 6) [" + to_string(i) + "]", kAlphaOnly(s + (nf * i) + 27));
alarmSearchDist.emplace_back("alarm search distance [" + to_string(i) + "]", kRetailOnly(s + (nf * i) + 30));
alarmIgnoreDist.emplace_back("alarm enemy ignore distance [" + to_string(i) + "]", kRetailOnly(s + (nf * i) + 31));
alarmAttackDist.emplace_back("alarm enemy attack distance [" + to_string(i) + "]", kRetailOnly(s + (nf * i) + 32));
alarmDamageThreshold.emplace_back("alarm damage threshold [" + to_string(i) + "]", kRetailOnly(s + (nf * i) + 33));
alarmFightTimer.emplace_back("alarm fight timer [" + to_string(i) + "]", kRetailOnly(s + (nf * i) + 34));
}
for (size_t i = 0; i < sz; ++i)
fieldsAux_.insert(fieldsAux_.end(), {&objcNextSize[i], &objcSubtype[i], &objcID[i], &objcFlags[i], &objcPosX[i], &objcPosY[i],
&objcPosZ[i], &objcRotX[i], &objcRotY[i], &objcRotZ[i], &profileName[i], &combatID[i],
&combatIDA6[i], &behaviorLongRange[i], &behaviorMedRange[i], &behaviorShortRange[i],
&behaviorMedRetreat[i], &behaviorLongRetreat[i], &medRangeThreshold[i], &meleeOverride[i],
&noGunBehavior[i], &shortRangeThreshold[i], &pursuitDistance[i], &panicHurt[i], &panicGunfire[i],
&panicMelee[i], &panicSight[i], &paddingIDA6[i], &alarmSearchDist[i], &alarmIgnoreDist[i],
&alarmAttackDist[i], &alarmDamageThreshold[i], &alarmFightTimer[i]});