-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathCommon.h
More file actions
2670 lines (2352 loc) · 78.4 KB
/
Copy pathCommon.h
File metadata and controls
2670 lines (2352 loc) · 78.4 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
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is CBash code.
*
* The Initial Developer of the Original Code is
* Waruddar.
* Portions created by the Initial Developer are Copyright (C) 2010
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
#pragma once
// Common.h
#include <time.h>
#include <io.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <share.h>
#include <errno.h>
#include <exception>
#include <boost/unordered_set.hpp>
#include <set>
#include <map>
//#include <boost/interprocess/file_mapping.hpp>
//#include <boost/interprocess/mapped_region.hpp>
#include <boost/iostreams/device/mapped_file.hpp>
#include <vector>
#include "MacroDefinitions.h"
extern int (*printer)(const char * _Format, ...);
extern SINT32 (*LoggingCallback)(const STRING);
extern void (*RaiseCallback)(const STRING);
enum whichGameTypes {
eIsOblivion = 0,
eIsFallout3,
eIsFalloutNewVegas,
eIsSkyrim,
eIsUnknownGameType
};
enum API_FieldTypes {
UNKNOWN_FIELD = 0,
MISSING_FIELD,
JUNK_FIELD,
BOOL_FIELD,
SINT8_FIELD,
UINT8_FIELD,
SINT16_FIELD,
UINT16_FIELD,
SINT32_FIELD,
UINT32_FIELD,
FLOAT32_FIELD,
RADIAN_FIELD,
FORMID_FIELD,
MGEFCODE_FIELD,
ACTORVALUE_FIELD,
FORMID_OR_UINT32_FIELD,
FORMID_OR_FLOAT32_FIELD,
UINT8_OR_UINT32_FIELD,
FORMID_OR_STRING_FIELD,
UNKNOWN_OR_FORMID_OR_UINT32_FIELD,
UNKNOWN_OR_SINT32_FIELD,
UNKNOWN_OR_UINT32_FLAG_FIELD,
MGEFCODE_OR_CHAR4_FIELD,
FORMID_OR_MGEFCODE_OR_ACTORVALUE_OR_UINT32_FIELD,
RESOLVED_MGEFCODE_FIELD,
STATIC_MGEFCODE_FIELD,
RESOLVED_ACTORVALUE_FIELD,
STATIC_ACTORVALUE_FIELD,
CHAR_FIELD,
CHAR4_FIELD,
STRING_FIELD,
ISTRING_FIELD,
STRING_OR_FLOAT32_OR_SINT32_FIELD,
LIST_FIELD,
PARENTRECORD_FIELD,
SUBRECORD_FIELD,
SINT8_FLAG_FIELD,
SINT8_TYPE_FIELD,
SINT8_FLAG_TYPE_FIELD,
SINT8_ARRAY_FIELD,
UINT8_FLAG_FIELD,
UINT8_TYPE_FIELD,
UINT8_FLAG_TYPE_FIELD,
UINT8_ARRAY_FIELD,
SINT16_FLAG_FIELD,
SINT16_TYPE_FIELD,
SINT16_FLAG_TYPE_FIELD,
SINT16_ARRAY_FIELD,
UINT16_FLAG_FIELD,
UINT16_TYPE_FIELD,
UINT16_FLAG_TYPE_FIELD,
UINT16_ARRAY_FIELD,
SINT32_FLAG_FIELD,
SINT32_TYPE_FIELD,
SINT32_FLAG_TYPE_FIELD,
SINT32_ARRAY_FIELD,
UINT32_FLAG_FIELD,
UINT32_TYPE_FIELD,
UINT32_FLAG_TYPE_FIELD,
UINT32_ARRAY_FIELD,
FLOAT32_ARRAY_FIELD,
RADIAN_ARRAY_FIELD,
FORMID_ARRAY_FIELD,
FORMID_OR_UINT32_ARRAY_FIELD,
MGEFCODE_OR_UINT32_ARRAY_FIELD,
STRING_ARRAY_FIELD,
ISTRING_ARRAY_FIELD,
SUBRECORD_ARRAY_FIELD,
UNDEFINED_FIELD
};
enum TopTypes {
eTop,
eWorld,
eInteriorBlock,
eInteriorSubBlock,
eExteriorBlock,
eExteriorSubBlock,
eCellChildren,
eTopicChildren,
eCellPersistent,
eCellTemporary,
eCellVWD
};
enum varType {
eNONE,
eUINT32,
eFORMID,
eVATSPARAM
};
class Ex_NULL : public std::exception
{
public:
const char * __CLR_OR_THIS_CALL what() const;
};
class Ex_INVALIDINDEX : public std::exception
{
public:
const char * __CLR_OR_THIS_CALL what() const;
};
class Ex_INVALIDCOLLECTIONINDEX : public std::exception
{
public:
const char * __CLR_OR_THIS_CALL what() const;
};
class Ex_INVALIDMODINDEX : public std::exception
{
public:
const char * __CLR_OR_THIS_CALL what() const;
};
//wrappers for _stricmp and strcmp that handle NULL args
int icmps(const STRING lhs, const STRING rhs);
int cmps(const STRING lhs, const STRING rhs);
class ModFile;
class Record;
class StringRecord;
struct sameStr
{
bool operator()( const STRING s1, const STRING s2 ) const;
};
bool ReadChunk(unsigned char *&buffer, const UINT32 &buffer_size, void *dest_buffer, const UINT32 &dest_buffer_size, const bool &skip_load);
typedef std::multimap<UINT32, Record *> FormID_Map;
typedef std::multimap<STRING, Record *, sameStr> EditorID_Map;
typedef FormID_Map::iterator FormID_Iterator;
typedef EditorID_Map::iterator EditorID_Iterator;
typedef std::pair<FormID_Iterator, FormID_Iterator> FormID_Range;
typedef std::pair<EditorID_Iterator, EditorID_Iterator> EditorID_Range;
typedef std::pair<varType, varType> FunctionArguments;
typedef std::map<UINT32, FunctionArguments>::value_type Function_ArgumentsType;
typedef std::map<UINT32, STRING>::value_type Function_NameType;
typedef std::map<UINT32, std::vector<UINT32> >::value_type RecordType_PossibleGroupsType;
typedef std::map<UINT32, std::vector<UINT32> >::const_iterator RecordType_PossibleGroups_Iterator;
typedef std::map<UINT32, FunctionArguments>::const_iterator Function_Arguments_Iterator;
typedef std::map<UINT32, STRING>::const_iterator ID_Name_Iterator;
extern const std::map<UINT32, FunctionArguments> Function_Arguments;
extern const std::map<UINT32, STRING> Function_Name;
extern const std::map<UINT32, STRING> Comparison_Name;
extern const std::map<UINT32, STRING> IDLEGroup_Name;
extern const std::map<UINT32, STRING> PACKAIType_Name;
extern const std::map<UINT32, STRING> PACKLocType_Name;
extern const std::map<UINT32, STRING> PACKTargetType_Name;
extern const std::map<UINT32, STRING> HardCodedFormID_EditorID;
extern const std::map<UINT32, FunctionArguments> FNVFunction_Arguments;
extern const UINT32 VATSFunction_Argument[];
extern const float flt_max;
extern const float flt_min;
extern const float flt_0;
extern const float flt_1;
extern const float flt_3;
extern const float flt_n2147483648;
#ifdef CBASH_CALLTIMING
extern std::map<char *, double> CallTime;
#endif
#ifdef CBASH_CALLCOUNT
extern std::map<char *, unsigned long> CallCount;
#endif
#ifdef CBASH_DEBUG_CHUNK
void peek_around(unsigned char *position, UINT32 length);
#endif
class GenericOp
{
public:
GenericOp();
~GenericOp();
virtual bool perform() abstract {};
};
class RenameOp : public GenericOp
{
private:
STRING original_name;
STRING destination_name;
public:
RenameOp(STRING _original_name, STRING _destination_name);
~RenameOp();
bool perform();
};
STRING DeGhostModName(STRING const ModName);
bool FileExists(STRING const FileName);
STRING GetTemporaryFileName(STRING FileName, bool IsBackup=false);
bool AlmostEqual(FLOAT32 A, FLOAT32 B, SINT32 maxUlps);
class FileWriter
{
private:
unsigned char *file_buffer, *record_buffer, *compressed_buffer;
UINT32 file_buffer_used, record_buffer_used, compressed_buffer_used;
UINT32 file_buffer_size, record_buffer_size, compressed_buffer_size;
int fh;
STRING FileName;
public:
FileWriter(STRING filename, UINT32 size);
~FileWriter();
SINT32 open();
SINT32 close();
void record_write(const void *source, UINT32 length);
void record_write_subheader(UINT32 signature, UINT32 length);
void record_write_subrecord(UINT32 signature, const void *source, UINT32 length);
UINT32 record_compress();
UINT32 record_size();
void record_flush();
UINT32 file_tell();
void file_write(const void *source_buffer, UINT32 source_buffer_used);
void file_write(UINT32 position, const void *source_buffer, UINT32 source_buffer_used);
};
class FormIDHandlerClass
{
//In order to identify a record across mods, two pieces of info are needed:
// 1) The originating mod.
// 2) A unique identifier within the mod.
//As long as those two requirements are met, the implementation details don't matter.
//In practice, there are two different implementations in use:
// 1) FormIDs (used by the game engine, TES4Edit, etc)
// 2) Long formIDs (used by Wrye Bash and therefore used in cint)
//A formID is composed of two parts, the modIndex and the objectID.
// The modIndex identifies the originating mod and the objectID identifies the record within that mod.
// The modIndex is a UINT8 index, and the objectID is a UINT24.
// They are combined into a single UINT32 where 0xFF000000 is the modIndex and the objectID is 0x00FFFFFF.
// I.E. a modIndex of 0x01 and an objectID of 0x00084F would result in the formID 0x0100084F.
// Considering that Oblivion.esm alone has over a million records,
// and that each record may reference multiple other records, this is both a fast and low memory method.
//
// There are a couple problems though:
// 1) A modIndex can only use 0-255, but there are several orders of magnitudes more mods.
// Inevitably, mods will end up using the same modIndex value.
// 2) A modIndex can't directly identify a mod; mods are named, not numbered.
//
// Bethesda solves these issues by:
// 1) Limiting mods to 254 (255 is reserved)
// 2) Translating a mod name to a unique modIndex value.
// The implementation details of these fixes differs depending on whether the record is on disk or in memory.
//
// If the record is on disk (being read/written):
// 1) There is an array of mod names in the TES4 record termed the masters. It is signed with "MAST".
// Whenever a record from another mod is added to a mod,
// its originating mod name is added to this array if not already present.
// This array is limited to 254 entries. Therefore a mod is unable to have more than 254 masters.
// 2) The modIndex identifies the array index of the originating mod's name.
// If the modIndex is greater than the array size (up to 255), it identifies that mod as the source.
//
// For example, if an esp named "Test.esp" uses records from "Oblivion.esm", "OOO.esm", and "COBL.esm",
// it will have these names in the MAST chunk of its TES4 record. A record with the formID 0x0100084F
// has a modIndex of 1, and an objectID of 0x84F. The modIndex is used to access the master array
// resulting in "OOO.esm". The record is thus identified as originating from "OOO.esm" with the unique
// identifier of 0x84F. Similarly, 0x0000084F is a completely different record that originates from
// "Oblivion.esm". On the other hand, 0x0300084F, 0x0400084F, and 0xFF00084F all refer to the same
// record that originates from "Test.esp". Although they have different modIndex values, they are all
// greater than the master array length. For the majority of formIDs originating within a mod, the
// modIndex will be equal to the size of the master array, but not always.
//
// If the record is in memory:
// 1) The number of active mods is limited to 254 (255 is used for the save file).
// These mods are arranged into a load order based primarily on the modified time of each mod, with
// esm mods sorting before esp mods.
// 2) The modIndex identifies the load order index of the mod. If the load order changes, the modIndex of a given
// record will change to reflect this.
//
// For example, if the mods "Test.esp", "Oblivion.esm", "OOO.esm", and "COBL.esm" are active, they may
// be sorted into a load order "Oblivion.esm", "COBL.esm", "OOO.esm", and "Test.esp". A record with the
// formID 0x0100084F has a modIndex of 1, and an objectID of 0x84F. The modIndex is used to access the
// load order array resulting in "COBL.esm". The record is thus identified as originating from "COBL.esm"
// with the unique identifier of 0x84F. Similarly, 0x0000084F is a completely different record that
// originates from "Oblivion.esm", 0x0200084F comes from "OOO.esm", 0x0300084F comes from "Test.esp", and
// 0xFF00084F comes from the savegame.
//
// Notice that the formID 0x0100084F in the above examples identifies two completely different records based on
// whether the record is on disk or in memory. The game engine has to be able to convert between the disk
// representation and the in memory representation. This is known as resolving the formID. It occurs every time
// the game engine is started, the Construction Set (CS) opens a mod, and when the CS saves a mod. Almost every
// record has to have its formID resolved as well as every formID that record happens to reference.
// (To complicate matters, some records are identified by their editorID instead of their formID, and any formID
// with an objectID <= 0x800 doesn't use the modIndex at all. Instead, they're considered to belong to the
// engine itself.)
//
// The key to doing this lays in the fact that in both cases, the modIndex can be used to get a mod's name.
// On disk, the modIndex directly references a name in the master array, and in memory, it references a mod at
// a given load order, and the engine knows the name of that mod.
//
// So, the on disk modIndex maps to a mod name which maps to the in memory modIndex.
//
// Combining the two previous examples, the on disk formID 0x0100084F in mod "Test.esp" refers to
// ("OOO.esm", 0x84F). The load order is "Oblivion.esm", "COBL.esm", "OOO.esm", and "Test.esp". The engine
// finds a match between "OOO.esm" and the mod name at load order position 0x02, so the in memory modIndex
// becomes 0x02, and the formID is 0x0200084F. "Oblivion.esm" has no masters, so the on disk formID 0x0100084F
// in mod "Oblivion.esm" refers to ("Oblivion.esm", 0x84F). The engine finds a match between
// "Oblivion.esm" and the mod name at load order position 0x00, so the modIndex becomes 0x00,
// and the in memory formID is 0x0000084F.
//
// When the CS saves a mod to disk, this process is essentially reversed. Given a new mod being saved
// to "SaveTest.esp" with the same load order as above, the in memory formID 0x0200084F refers to the mod at
// load order position 0x02 which has the name "OOO.esm". If "OOO.esm" is found at position 0x00 in
// the mod's masters, so the on disk formID becomes 0x0000084F.
//
// Instead of directly working with formIDs, Wrye Bash uses a concept it calls long formIDs.
// Rather than going from on disk modIndex to mod name to in memory modIndex, long formIDs stop at the mod name.
// So the long formID is always just one step away from becoming either an on disk formID or in memory formID.
//
// For example, using the same example setup as before, the on disk formID 0x0100084F in mod
// "Test.esp" refers to ("OOO.esm", 0x84F), so the long formID is just that, ("OOO.esm", 0x84F).
// When converting ("OOO.esm", 0x84F) back to the on disk formID, if "OOO.esm" is found at position 0x00
// in the mod's masters, the on disk formID becomes 0x0000084F.
//
// Each approach satisfies the conditions to uniquely identify a record, but they have their own pros and cons.
// FormID Pros:
// 1) A UINT32 uses much less memory than a string and UINT32.
// 2) It is much faster to load a UINT32 than a string and UINT32.
// 3) Comparing UINT32's is much faster than case insensitive comparing a string and a UINT32.
// FormID Cons:
// 1) The resolution process is a bit involved.
// 2) Depending on implementation, resolving a formID can be very slow.
// The naive approach is to case insensitively search through the load order and/or masters every time a
// formID is resolved.
// 3) Neither the on disk formID nor the in memory formID is stable. On disk formID 0x0100084F may refer to
// ("OOO.esm", 0x84F) at one point in time, and ("COBL.esm", 0x84F) at some other time. It all depends on
// whether the masters were changed. In memory formID 0x0100084F may refer to ("COBL.esm", 0x84F)
// at one point in time, and ("Test.esp", 0x84F) at some other time. It all depends on whether the load order
// was changed. It isn't trivial to match records across two different load orders.
// 4) A human can't simply look at the formID and know where the record originates.
//
// Long formID Pros:
// 1) A long formID is stable. It doesn't matter what the load order is, nor what the mod masters are.
// ("OOO.esm", 0x84F) always refers to the record in "OOO.esm" with the objectID 0x84F. This makes it
// extremely trivial to match records despite differing load orders. This allows actions such as
// importing data from a text file or from a mod file not in the load order.
// 2) The resolution process is simpler.
// 3) The originating mod of a record becomes extremely obvious.
// Long formID Cons:
// 1) Uses much more memory than a formID.
// 2) Takes much more time to process than a formID.
// 3) Have to work with a "non-standard" format, and deal with conversions when needed.
//
//CBash uses the formID internally, but defaults to the long formID in its Python interface.
// This involves extra work, but allows Python to work with a stable record identifier while
// preserving the speed and memory savings internally by using formIDs.
//
//CBash is heavily optimized for speedy formID resolution. Since there is a maximum of 255
// possible masters per mod, and a similar limit on the number of mods in a given load order,
// CBash can precompute all possible on disk modIndex to in memory modIndex matches. These
// are stored in a fixed size array, so that resolving a formID is a simple matter of
// selecting the appropriate resolution table, and using the given modIndex as the index.
// CBash uses the terminology "expand" when resolving an on disk formID to an in memory formID,
// and "collapse" when resolving an in memory formID to an on disk formID.
public:
std::vector<STRING> &MAST; //The list of masters
std::vector<STRING> LoadOrder255; //The current load order of active mods
boost::unordered_set<UINT32> NewTypes; //Tracks the type of any new records for reporting
UINT32 &nextObject; //The object counter for quickly providing new objectIDs
UINT8 ExpandTable[256]; //Maps the on disk modIndex to the in memory modIndex
UINT8 CollapseTable[256]; //Maps the in memory modIndex to the on disk modIndex (not always a direct inverse of ExpandTable)
unsigned char * FileStart;
unsigned char * FileEnd;
UINT8 ExpandedIndex; //The load order index
UINT8 CollapsedIndex; //The size of MAST
bool IsEmpty;
bool bMastersChanged;
SINT32 EmptyGRUPs;
std::vector<FORMID> OrphanedRecords;
FormIDHandlerClass(std::vector<STRING> &_MAST, UINT32 &_NextObject);
~FormIDHandlerClass();
void SetLoadOrder(std::vector<STRING> &cLoadOrder);
UINT32 NextExpandedFormID();
void CreateFormIDLookup(const UINT8 expandedIndex);
void UpdateFormIDLookup();
void AddMaster(STRING const curMaster);
bool MastersChanged();
bool IsValid(const unsigned char *_SrcBuf);
};
class CreationFlags
{
private:
enum createFlags
{
fSetAsOverride = 0x00000001,
fCopyWinningParent = 0x00000002
};
public:
CreationFlags();
CreationFlags(UINT32 nFlags);
~CreationFlags();
bool SetAsOverride;
bool CopyWinningParent;
//Internal use
bool ExistingReturned;
UINT32 GetFlags();
};
class ModFlags
{
private:
//MinLoad and FullLoad are exclusive
// If both are set, FullLoad takes priority
// If neither is set, the mod isn't loaded
//SkipNewRecords causes any new record to be ignored when the mod is loaded
// This may leave broken records behind (such as a quest override pointing to a new script that was ignored)
// So it shouldn't be used if planning on copying records unless you either check that there are no new records being referenced
//InLoadOrder makes the mod count towards the 255 limit and enables record creation and copying as new.
// If it is false, it forces Saveable to be false.
// Any mod with new records should have this set unless you're ignoring the new records.
// It causes the mod to be reported by GetNumModIDs, GetModIDs
//Saveable allows the mod to be saved.
//AddMasters causes the mod's masters to be added to the load order
// This is essential for most mod editing functions
//LoadMasters causes the mod's masters to be loaded into memory after being added
// This has no effect if AddMasters is false
// This is required if you want to lookup overridden records
//ExtendedConflicts causes any conflicting records to be ignored by most functions
// IsRecordWinning, GetNumRecordConflicts, GetRecordConflicts will report the extended conflicts only if asked
//TrackNewTypes causes the loader to track which record types in a mod are new and not overrides
// Increases load time per mod.
// It enables GetModNumTypes and GetModTypes for that mod.
//IndexLANDs causes LAND records to have extra indexing.
// Increases load time per mod.
// It allows the safe editing of land records heights.
// Modifying one LAND may require changes in an adjacent LAND to prevent seams
//FixupPlaceables moves any REFR,ACHR,ACRE records in a world cell to the actual cell they belong to.
// Increases load time per mod.
// Use if you're planning on iterating through every placeable in a specific cell
// so that you don't have to check the world cell as well.
//IgnoreAbsentMasters causes any records that override masters not in the load order to be dropped
// If it is true, it forces IsAddMasters to be false.
// Allows mods not in load order to copy records
//Only the following combinations are tested via Bash:
// Normal: (fIsMinLoad or fIsFullLoad) + fIsInLoadOrder + fIsSaveable + fIsAddMasters + fIsLoadMasters
// Dummy: fIsAddMasters
// Merged: (fIsMinLoad or fIsFullLoad) + fIsSkipNewRecords + fIgnoreAbsentMasters
// Scanned: (fIsMinLoad or fIsFullLoad) + fIsSkipNewRecords + fIsExtendedConflicts
enum modFlags
{
fIsMinLoad = 0x00000001,
fIsFullLoad = 0x00000002,
fIsSkipNewRecords = 0x00000004,
fIsInLoadOrder = 0x00000008,
fIsSaveable = 0x00000010,
fIsAddMasters = 0x00000020,
fIsLoadMasters = 0x00000040,
fIsExtendedConflicts = 0x00000080,
fIsTrackNewTypes = 0x00000100,
fIsIndexLANDs = 0x00000200,
fIsFixupPlaceables = 0x00000400,
fIsCreateNew = 0x00000800,
fIsIgnoreInactiveMasters = 0x00001000,
fIsSkipAllRecords = 0x00002000,
};
public:
ModFlags();
ModFlags(UINT32 _Flags);
~ModFlags();
bool IsMinLoad;
bool IsFullLoad;
bool IsNoLoad;
bool IsSkipNewRecords;
bool IsSkipAllRecords;
bool IsInLoadOrder;
bool IsSaveable;
bool IsAddMasters;
bool IsLoadMasters;
bool IsExtendedConflicts;
bool IsTrackNewTypes;
bool IsIndexLANDs;
bool IsFixupPlaceables;
bool IsCreateNew;
bool IsIgnoreInactiveMasters;
//For internal use, may not be set by constructor
bool LoadedGRUPs;
UINT32 GetFlags();
};
class SaveFlags
{
private:
enum saveFlags
{
fIsCleanMasters = 0x00000001,
fIsCloseCollection = 0x00000002
};
public:
SaveFlags();
SaveFlags(UINT32 _Flags);
~SaveFlags();
bool IsCleanMasters;
bool IsCloseCollection;
};
class StringRecord
{
private:
#ifdef CBASH_X64_COMPATIBILITY
bool IsOnDisk;
#define O_IS_ON_DISK(x) (x.IsOnDisk)
#define S_IS_ON_DISK (IsOnDisk)
#define O_SET_ON_DISK(x, y) (x.IsOnDisk = y)
#define S_SET_ON_DISK(x) (IsOnDisk = x)
#define S_GET_VALUE (value)
#define O_GET_VALUE(x) (x.value)
#define VAL_NAME value
#else
//Save memory on x86 builds by using the unused high bit of the STRING pointer
enum flagsFlags
{
fIsOnDisk = 0x80000000
};
#define O_IS_ON_DISK(x) (((UINT32)x._value & 0x80000000) != 0)
#define S_IS_ON_DISK (((UINT32)_value & 0x80000000) != 0)
#define O_SET_ON_DISK(x, y) (x._value = y ? (STRING)((UINT32)x._value | 0x80000000) : (STRING)((UINT32)x._value & ~0x80000000))
#define S_SET_ON_DISK(x) (_value = x ? (STRING)((UINT32)_value | 0x80000000) : (STRING)((UINT32)_value & ~0x80000000))
#define S_GET_VALUE ((STRING)((UINT32)_value & ~0x80000000))
#define O_GET_VALUE(x) ((STRING)((UINT32)x._value & ~0x80000000))
#define VAL_NAME _value
STRING _value;
#endif
public:
#ifdef CBASH_X64_COMPATIBILITY
STRING value;
#else
__declspec(property(get=GetString)) STRING value;
#endif
StringRecord();
StringRecord(const StringRecord &p);
~StringRecord();
UINT32 GetSize() const;
#ifndef CBASH_X64_COMPATIBILITY
STRING GetString();
#endif
bool IsLoaded() const;
void Load();
void Unload();
bool Read(unsigned char *&buffer, const UINT32 &subSize, const bool &CompressedOnDisk);
void Write(UINT32 _Type, FileWriter &writer);
void ReqWrite(UINT32 _Type, FileWriter &writer);
void Copy(STRING FieldValue);
void TruncateCopy(STRING FieldValue, UINT32 MaxSize);
bool equals(const StringRecord &other) const;
bool equalsi(const StringRecord &other) const;
StringRecord& operator = (const StringRecord &rhs);
};
class NonNullStringRecord
{
private:
UINT32 DiskSize;
STRING _value;
public:
__declspec(property(get=GetString)) STRING value;
NonNullStringRecord();
NonNullStringRecord(const NonNullStringRecord &p);
~NonNullStringRecord();
UINT32 GetSize() const;
STRING GetString();
bool IsLoaded() const;
void Load();
void Unload();
bool Read(unsigned char *&buffer, const UINT32 &subSize, const bool &CompressedOnDisk);
void Write(UINT32 _Type, FileWriter &writer);
void ReqWrite(UINT32 _Type, FileWriter &writer);
void Copy(STRING FieldValue);
bool equals(const NonNullStringRecord &other) const;
bool equalsi(const NonNullStringRecord &other) const;
NonNullStringRecord& operator = (const NonNullStringRecord &rhs);
};
class UnorderedPackedStrings
{
public:
std::vector<STRING> value;
UnorderedPackedStrings();
~UnorderedPackedStrings();
UINT32 GetSize() const;
bool IsLoaded() const;
void Load();
void Unload();
void resize(UINT32 newSize);
bool Read(unsigned char *&buffer, const UINT32 &subSize);
void Write(UINT32 _Type, FileWriter &writer);
void Copy(STRINGARRAY FieldValue, UINT32 ArraySize);
UnorderedPackedStrings& operator = (const UnorderedPackedStrings &rhs);
bool equals(const UnorderedPackedStrings &other) const;
bool equalsi(const UnorderedPackedStrings &other) const;
};
class RawRecord
{
private:
//Using the upper bit of size to store a flag
//This restricts the max size to a paltry 2GB, so not an issue
enum flagsFlags
{
fIsOnDisk = 0x80000000
};
public:
UINT32 size;
unsigned char *value;
RawRecord();
RawRecord(const RawRecord &p);
~RawRecord();
UINT32 GetSize() const;
bool IsLoaded() const;
void Load();
void Unload();
bool Read(unsigned char *&buffer, const UINT32 &subSize, const bool &CompressedOnDisk);
void Write(UINT32 _Type, FileWriter &writer);
void ReqWrite(UINT32 _Type, FileWriter &writer);
void Copy(unsigned char *FieldValue, UINT32 nSize);
RawRecord& operator = (const RawRecord &rhs);
bool operator ==(const RawRecord &other) const;
bool operator !=(const RawRecord &other) const;
};
//Base record field. Vestigial.
//Used when it isn't known if the record is required or optional.
//Should only be used with simple data types that should be initialized to 0 (int, float, etc) and not structs
//exponent parameter is only used on the float specialization
template<class T, T defaultValue=0>
struct SimpleSubRecord
{
T value;
bool isLoaded;
SimpleSubRecord():
isLoaded(false),
value(defaultValue)
{
//
}
~SimpleSubRecord()
{
//
}
UINT32 GetSize() const
{
return sizeof(T);
}
bool IsLoaded() const
{
return (isLoaded && value != defaultValue);
}
void Load()
{
isLoaded = true;
}
void Unload()
{
value = defaultValue;
isLoaded = false;
}
bool Read(unsigned char *&buffer, const UINT32 &subSize)
{
bool was_loaded = ReadChunk(buffer, subSize, &value, sizeof(T), isLoaded);
isLoaded = true;
return was_loaded;
}
void Write(UINT32 _Type, FileWriter &writer)
{
if(isLoaded && value != defaultValue)
writer.record_write_subrecord(_Type, &value, sizeof(T));
}
SimpleSubRecord<T, defaultValue>& operator = (const SimpleSubRecord<T, defaultValue> &rhs)
{
if(this != &rhs)
{
isLoaded = rhs.isLoaded;
value = rhs.value;
}
return *this;
}
bool operator ==(const SimpleSubRecord<T, defaultValue> &other) const
{
return (isLoaded == other.isLoaded &&
value == other.value);
}
bool operator !=(const SimpleSubRecord<T, defaultValue> &other) const
{
return !(*this == other);
}
};
template<const float &defaultValue=flt_0>
struct SimpleFloatSubRecord
{
FLOAT32 value;
bool isLoaded;
SimpleFloatSubRecord():
isLoaded(false),
value(defaultValue)
{
//
}
~SimpleFloatSubRecord()
{
//
}
UINT32 GetSize() const
{
return sizeof(FLOAT32);
}
bool IsLoaded() const
{
return (isLoaded && !AlmostEqual(value, defaultValue, 2));
}
void Load()
{
isLoaded = true;
}
void Unload()
{
value = defaultValue;
isLoaded = false;
}
bool Read(unsigned char *&buffer, const UINT32 &subSize)
{
bool was_loaded = ReadChunk(buffer, subSize, &value, sizeof(FLOAT32), isLoaded);
isLoaded = true;
return was_loaded;
}
void Write(UINT32 _Type, FileWriter &writer)
{
if(isLoaded && value != defaultValue)
writer.record_write_subrecord(_Type, &value, sizeof(FLOAT32));
}
void ReqWrite(UINT32 _Type, FileWriter &writer)
{
writer.record_write_subrecord(_Type, &value, sizeof(FLOAT32));
}
SimpleFloatSubRecord<defaultValue>& operator = (const SimpleFloatSubRecord<defaultValue> &rhs)
{
if(this != &rhs)
{
isLoaded = rhs.isLoaded;
value = rhs.value;
}
return *this;
}
bool operator ==(const SimpleFloatSubRecord<defaultValue> &other) const
{
return (isLoaded == other.isLoaded &&
AlmostEqual(value, other.value, 2));
}
bool operator !=(const SimpleFloatSubRecord<defaultValue> &other) const
{
return !(*this == other);
}
};
//Used for subrecords that are required
//Even if not actually loaded from disk, they are always considered loaded even if they're explicitly unloaded.
//Unloading them simply resets the values to default.
//Should only be used with simple data types that should be initialized to 0 (int, float, etc) and not structs
//exponent parameter is only used on the float specialization
template<class T, T defaultValue=0>
struct ReqSimpleSubRecord
{
T value;
ReqSimpleSubRecord():
value(defaultValue)
{
//
}
~ReqSimpleSubRecord()
{
//
}
UINT32 GetSize() const
{
return sizeof(T);
}
bool IsLoaded() const
{
return true;
}
void Load()
{
//
}
void Unload()
{
value = defaultValue;
}
bool Read(unsigned char *&buffer, const UINT32 &subSize)
{
return ReadChunk(buffer, subSize, &value, sizeof(T), false);
}
void Write(UINT32 _Type, FileWriter &writer)
{
writer.record_write_subrecord(_Type, &value, sizeof(T));
}
ReqSimpleSubRecord<T, defaultValue>& operator = (const ReqSimpleSubRecord<T, defaultValue> &rhs)
{
if(this != &rhs)
value = rhs.value;
return *this;
}
bool operator ==(const ReqSimpleSubRecord<T, defaultValue> &other) const
{
return (value == other.value);
}
bool operator !=(const ReqSimpleSubRecord<T, defaultValue> &other) const
{
return !(*this == other);
}
};
template<const float &defaultValue=flt_0>
struct ReqSimpleFloatSubRecord
{
FLOAT32 value;
ReqSimpleFloatSubRecord():
value(defaultValue)
{
//
}
~ReqSimpleFloatSubRecord()
{
//
}
UINT32 GetSize() const
{
return sizeof(FLOAT32);
}
bool IsLoaded() const
{
return true;