-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathCollection.cpp
1558 lines (1394 loc) · 59.7 KB
/
Collection.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
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 ***** */
// Collection.cpp
#include "Collection.h"
#include <direct.h>
//#include <boost/threadpool.hpp>
//SortedRecords::SortedRecords():
// size(0),
// records(NULL)
// {
// //
// }
//
//SortedRecords::~SortedRecords()
// {
// delete []records;
// }
//
//void SortedRecords::push_back(ModFile *&mod, Record *&record)
// {
// Record ** temp_records = new Record *[size + 1];
// bool placed = false;
// for(UINT32 x = 0, y = 0; x < size; ++y)
// {
// if(!placed && records[x]->GetParentMod()->ModID > record->GetParentMod()->ModID)
// {
// placed = true;
// temp_records[y] = record;
// }
// else
// {
// temp_records[y] = records[x];
// ++x;
// }
// }
// if(!placed)
// temp_records[size] = record;
// size++;
// delete []records;
// records = temp_records;
// }
//
//void SortedRecords::erase(UINT32 &index)
// {
// Record ** temp_records = new Record *[size - 1];
// for(UINT32 x = 0, y = 0; x < size; ++x)
// {
// if(x == index)
// continue;
// temp_records[y] = records[x];
// ++y;
// }
// size--;
// delete []records;
// records = temp_records;
// }
bool compHistory(Record *&lhs, Record *&rhs)
{
return lhs->GetParentMod()->ModID < rhs->GetParentMod()->ModID;
}
bool compConflicts(Record *&lhs, Record *&rhs)
{
return lhs->GetParentMod()->ModID > rhs->GetParentMod()->ModID;
}
bool sortMod(ModFile *lhs, ModFile *rhs)
{
//Esp's sort after esm's
//Non-existent esms sort before existing esps
//Non-existent esms retain their relative load order
//Existing esps sort by modified date
//Non-existent esps sort before existing esps
//Non-existent esps retain their relative load order
//New esps load last
#ifndef LHS_BEFORE_RHS
#define LHS_BEFORE_RHS true
#define LHS_AFTER_RHS false
#endif
if(lhs->TES4.IsESM())
{
if(rhs->TES4.IsESM())
{
if(lhs->ModTime == 0)
{
if(rhs->ModTime == 0)
return LHS_BEFORE_RHS;
return LHS_AFTER_RHS;
}
if(rhs->ModTime == 0)
return LHS_BEFORE_RHS;
return lhs->ModTime < rhs->ModTime;
}
return LHS_BEFORE_RHS;
}
if(rhs->TES4.IsESM())
return LHS_AFTER_RHS;
if(lhs->Flags.IsCreateNew)
{
if(rhs->Flags.IsCreateNew)
return LHS_BEFORE_RHS;
return LHS_AFTER_RHS;
}
if(rhs->Flags.IsCreateNew)
return LHS_BEFORE_RHS;
if(lhs->ModTime == 0)
return LHS_BEFORE_RHS;
if(rhs->ModTime == 0)
return LHS_AFTER_RHS;
return lhs->ModTime < rhs->ModTime;
}
Collection::Collection(STRING const &ModsPath, UINT32 _CollectionType):
ModsDir(NULL),
IsLoaded(false),
CollectionType(),
identical_records(),
changed_records(),
filter_records(),
filter_wspaces(),
filter_inclusive(false)
{
if(_CollectionType >= eIsUnknownGameType)
throw std::exception("CreateCollection: Error - Unable to create the collection. Invalid collection type specified.\n");
CollectionType = (whichGameTypes)_CollectionType;
ModsDir = new char[strlen(ModsPath)+1];
strcpy_s(ModsDir, strlen(ModsPath)+1, ModsPath);
}
Collection::~Collection()
{
delete []ModsDir;
for(UINT32 p = 0; p < ModFiles.size(); p++)
delete ModFiles[p];
for(UINT32 p = 0; p < Expanders.size(); p++)
delete Expanders[p];
//LoadOrder255 is shared with ModFiles, so no deleting
}
void Collection::SetFilterMode(bool inclusive) {
filter_inclusive = inclusive;
}
void Collection::AddRecordFilter(UINT32 recordtype) {
filter_records.insert(recordtype);
}
void Collection::AddWSpaceFilter(FORMID worldspace) {
filter_wspaces.insert(worldspace);
}
void Collection::ResetFilter() {
filter_records.clear();
filter_wspaces.clear();
}
ModFile * Collection::AddMod(STRING const &_FileName, ModFlags &flags, bool IsPreloading)
{
_chdir(ModsDir);
//Mods may not be added after collection is loaded.
//Prevent loading mods more than once
STRING ModName = DeGhostModName(_FileName);
ModFile *ModID = IsModAdded(ModName ? ModName : _FileName);
if(ModID != NULL)
{
//Suppress any warnings if masters are being loaded, or if the mod is being added with the same flags as before
if(IsPreloading || ModID->Flags.GetFlags() == flags.GetFlags())
{
delete []ModName;
return IsPreloading ? NULL : ModID;
}
printer("AddMod: Warning - Unable to add mod \"%s\". It already exists in the collection.\n", ModName ? ModName : _FileName);
delete []ModName;
return NULL;
}
if(IsLoaded)
{
if(!IsPreloading)
printer("AddMod: Error - Unable to add mod \"%s\". The collection has already been loaded.\n", ModName ? ModName : _FileName);
delete []ModName;
return NULL;
}
STRING FileName = new char[strlen(_FileName) + 1];
strcpy_s(FileName, strlen(_FileName) + 1, _FileName);
ModName = ModName ? ModName : FileName;
switch(CollectionType)
{
case eIsOblivion:
ModFiles.push_back(new TES4File(this, FileName, ModName, flags.GetFlags()));
ModFiles.back()->TES4.whichGame = eIsOblivion;
break;
case eIsFallout3:
printer("AddMod: Error - Unable to add mod \"%s\". Fallout 3 mod support is unimplemented.\n", ModName);
delete []ModName;
return NULL;
case eIsFalloutNewVegas:
ModFiles.push_back(new FNVFile(this, FileName, ModName, flags.GetFlags()));
ModFiles.back()->TES4.whichGame = eIsFalloutNewVegas;
break;
case eIsSkyrim:
ModFiles.push_back(new TES5File(this, FileName, ModName, flags.GetFlags()));
ModFiles.back()->TES4.whichGame = eIsSkyrim;
break;
default:
printer("AddMod: Error - Unable to add mod \"%s\". Invalid collection type.\n", ModName);
delete []ModName;
return NULL;
}
return ModFiles.back();
}
ModFile * Collection::IsModAdded(STRING const &ModName)
{
for(UINT32 p = 0;p < ModFiles.size();p++)
if(icmps(ModName, ModFiles[p]->ModName) == 0)
return ModFiles[p];
return NULL;
}
SINT32 Collection::SaveMod(ModFile *&curModFile, SaveFlags &flags, STRING const DestinationName)
{
if(!curModFile->Flags.IsSaveable)
{
printer("SaveMod: Error - Unable to save mod \"%s\". It is flagged as being non-saveable.\n", curModFile->ModName);
return -1;
}
if(flags.IsCloseCollection)
{
//clear up some memory
EditorID_ModFile_Record.clear();
FormID_ModFile_Record.clear();
ExtendedEditorID_ModFile_Record.clear();
ExtendedFormID_ModFile_Record.clear();
}
if(flags.IsCleanMasters)
CleanModMasters(curModFile);
//Some records (WRLD->CELL) may be created during the save process if necessary
RecordIndexer indexer(curModFile, curModFile->Flags.IsExtendedConflicts ? ExtendedEditorID_ModFile_Record: EditorID_ModFile_Record, curModFile->Flags.IsExtendedConflicts ? ExtendedFormID_ModFile_Record: FormID_ModFile_Record);
_chdir(ModsDir);
STRING temp_name = GetTemporaryFileName(DestinationName != NULL ? DestinationName : curModFile->ModName); //deleted when RenameOp is destroyed
//Save the mod to temp file
curModFile->Save(temp_name, Expanders, flags.IsCloseCollection, indexer);
//Delay renaming temp file to original filename until collection is closed
//This way the file mapping can remain open and the entire file doesn't have to be loaded into memory
closing_ops.push_back(new RenameOp(temp_name, DestinationName != NULL ? DestinationName : curModFile->FileName));
return 0;
}
SINT32 Collection::Load(bool (*_ProgressCallback)(const UINT32, const UINT32, const STRING))
{
ModFile *curModFile = NULL;
RecordIndexer indexer(EditorID_ModFile_Record, FormID_ModFile_Record);
RecordIndexer extended_indexer(ExtendedEditorID_ModFile_Record, ExtendedFormID_ModFile_Record);
bool Preloading = false;
std::vector<std::pair<ModFile *, std::vector<Record *> > > DeletedRecords;
if(IsLoaded)
{
printer("Load: Warning - Unable to load collection. It is already loaded.\n");
return 0;
}
try
{
_chdir(ModsDir);
//Brute force approach to loading all masters
//Could be done more elegantly with recursion
//printer("Before Preloading\n");
do {
Preloading = false;
for(UINT32 p = 0; p < (UINT32)ModFiles.size(); ++p)
{
curModFile = ModFiles[p];
curModFile->LoadTES4();
if(!curModFile->Flags.IsCreateNew && curModFile->Flags.IsAddMasters)
{
//Any new mods loaded this way inherit their flags
ModFlags preloadFlags(curModFile->Flags.GetFlags());
preloadFlags.IsNoLoad = !curModFile->Flags.IsLoadMasters;
for(UINT8 x = 0; x < curModFile->TES4.MAST.size(); ++x)
Preloading = (AddMod(curModFile->TES4.MAST[x], preloadFlags, true) != NULL || Preloading);
}
}
}while(Preloading);
//printer("Load order before sort\n");
//for(UINT32 x = 0; x < ModFiles.size(); ++x)
// printer("%02X: %s\n", x, ModFiles[x]->FileName);
//printer("\n");
std::_Insertion_sort(ModFiles.begin(), ModFiles.end(), sortMod);
std::vector<STRING> strLoadOrder255;
std::vector<STRING> strTempLoadOrder;
std::vector< std::vector<STRING> > strAllLoadOrder;
LoadOrder255.clear(); //shouldn't be needed
//printer("Load order:\n");
for(UINT32 p = 0; p < (UINT32)ModFiles.size(); ++p)
{
curModFile = ModFiles[p];
curModFile->ModID = p;
//printer("ModID %02X: %s", p, curModFile->FileName);
if(curModFile->Flags.IsInLoadOrder)
{
if(LoadOrder255.size() >= 255)
throw std::exception("Tried to load more than 255 mods.");
LoadOrder255.push_back(curModFile);
strLoadOrder255.push_back(curModFile->ModName);
//printer(" , OrderID %02X", LoadOrder255.size() - 1);
}
else if(!curModFile->Flags.IsIgnoreInactiveMasters) //every mod not in the std load order exists as if it and its masters are the only ones loaded
{//No need to sort since the masters should be in order well enough
for(UINT8 x = 0; x < curModFile->TES4.MAST.size(); ++x)
strTempLoadOrder.push_back(curModFile->TES4.MAST[x]);
strAllLoadOrder.push_back(strTempLoadOrder);
strTempLoadOrder.clear();
}
//printer("\n");
}
//printer("Load Set\n");
UINT8 expandedIndex = 0;
UINT32 x = 0;
for(UINT32 p = 0; p < (UINT32)ModFiles.size(); ++p)
{
curModFile = ModFiles[p];
if(_ProgressCallback && !(*_ProgressCallback)(p, (UINT32)ModFiles.size() - 1, curModFile->FileName))
{
/* TODO: clean abort */
}
RecordReader read_parser(curModFile);
//Loads GRUP and Record Headers. Fully loads GMST records.
curModFile->FormIDHandler.SetLoadOrder((curModFile->Flags.IsInLoadOrder || curModFile->Flags.IsIgnoreInactiveMasters) ? strLoadOrder255 : strAllLoadOrder[x++]);
//VHDPRINT(curModFile->Flags.IsSkipNewRecords ? 0xFF : curModFile->Flags.IsInLoadOrder ? expandedIndex : (UINT8)curModFile->TES4.MAST.size());
curModFile->FormIDHandler.CreateFormIDLookup(curModFile->Flags.IsSkipNewRecords ? 0xFF :
curModFile->Flags.IsInLoadOrder ? expandedIndex++ :
(UINT8)curModFile->TES4.MAST.size());
Expanders.push_back(new FormIDResolver(curModFile->FormIDHandler.ExpandTable, curModFile->FormIDHandler.FileStart, curModFile->FormIDHandler.FileEnd));
DeletedRecords.push_back(std::make_pair(curModFile, std::vector<Record *>()));
RecordIndexer &used_indexer = curModFile->Flags.IsExtendedConflicts ? extended_indexer : indexer;
used_indexer.SetModFile(curModFile);
curModFile->SetFilter(filter_inclusive, filter_records, filter_wspaces);
curModFile->Load(read_parser, used_indexer, Expanders, DeletedRecords.back().second);
}
//printer("Loaded\n");
strAllLoadOrder.clear();
UndeleteRecords(DeletedRecords);
IsLoaded = true;
}
catch(...)
{
IsLoaded = false;
throw;
}
return 0;
}
void Collection::UndeleteRecords(std::vector<std::pair<ModFile *, std::vector<Record *> > > &DeletedRecords)
{
//Deleted records are only composed of their header. All of their data is missing.
//This makes undeleting a record a bit tricky since you can't just toggle the records IsDeleted flag.
//This function tries to restore the data of all deleted records so that toggling the IsDeleted flag works as expected.
//It goes through the mod's masters, and uses the data from the newest record that hasn't been deleted.
//It is typically unable to restore the data to deleted injected records
Record *curRecord = NULL;
ModFile *curModFile = NULL;
Record *WinningRecord = NULL;
ModFile *WinningModFile = NULL;
SINT32 ModID = -1;
for(UINT32 ListIndex = 0; ListIndex < DeletedRecords.size(); ++ListIndex)
{
curModFile = DeletedRecords[ListIndex].first;
std::vector<Record *> &curRecords = DeletedRecords[ListIndex].second;
if(!curModFile->Flags.IsLoadMasters || !curModFile->Flags.IsInLoadOrder ||
curModFile->Flags.IsExtendedConflicts || curModFile->Flags.IsIgnoreInactiveMasters)
{
curRecords.clear();
continue;
}
UINT8 &CollapsedIndex = curModFile->FormIDHandler.CollapsedIndex;
const UINT8 (&CollapseTable)[256] = curModFile->FormIDHandler.CollapseTable;
for(UINT32 z = 0; z < curRecords.size(); ++z)
{
ModID = -1;
curRecord = curRecords[z];
WinningRecord = NULL;
if(curRecord->IsKeyedByEditorID())
{
//This is problematic because the EditorID will have been deleted.
//The only recourse is to try and find its match by FormID (which isn't 100% reliable)
//Luckily, these records should almost never be marked as deleted, so efficiency isn't a concern
STRING RecordEditorID = NULL;
for(EditorID_Iterator it = EditorID_ModFile_Record.begin(); it != EditorID_ModFile_Record.end(); ++it)
if(it->second->formID == curRecord->formID)
{
RecordEditorID = it->second->GetEditorIDKey();
//DPRINT("%s recovered", RecordEditorID);
break;
}
if(RecordEditorID != NULL)
{
for(EditorID_Range range = EditorID_ModFile_Record.equal_range(RecordEditorID); range.first != range.second; ++range.first)
{
WinningModFile = range.first->second->GetParentMod();
if((SINT32)WinningModFile->ModID > ModID && (WinningModFile->Flags.IsInLoadOrder || WinningModFile->Flags.IsIgnoreInactiveMasters))
//If the CollapseTable at a given expanded index is set to something other than the mod's CollapsedIndex,
// that means the mod has that other mod as a master.
if(CollapseTable[WinningModFile->FormIDHandler.ExpandedIndex] != CollapsedIndex)
{
if(range.first->second->IsDeleted() == false)
{
ModID = WinningModFile->ModID;
WinningRecord = range.first->second;
}
}
}
}
if(WinningRecord != NULL)
EditorID_ModFile_Record.insert(std::make_pair(RecordEditorID,curRecord));
}
else
{
for(FormID_Range range = FormID_ModFile_Record.equal_range(curRecord->formID); range.first != range.second; ++range.first)
{
WinningModFile = range.first->second->GetParentMod();
if((SINT32)WinningModFile->ModID > ModID && (WinningModFile->Flags.IsInLoadOrder || WinningModFile->Flags.IsIgnoreInactiveMasters))
//If the CollapseTable at a given expanded index is set to something other than the mod's CollapsedIndex,
// that means the mod has that other mod as a master.
if(CollapseTable[WinningModFile->FormIDHandler.ExpandedIndex] != CollapsedIndex)
{
if(range.first->second->IsDeleted() == false)
{
ModID = WinningModFile->ModID;
WinningRecord = range.first->second;
}
}
}
}
if(WinningRecord != NULL)
curRecord->recData = WinningRecord->recData;
else
{
//Deleted injected record?
RecordDeindexer deindexer(curRecord);
curRecord->GetParentMod()->DeleteRecord(curRecord, deindexer);
//printer("Load: Warning - Unable to undelete record %08X in \"%s\". Was not able to determine its source record.\n", curRecord->formID, curModFile->FileName);
}
}
}
}
SINT32 Collection::Unload()
{
RecordUnloader unloader;
for(UINT32 ListIndex = 0; ListIndex < ModFiles.size(); ++ListIndex)
ModFiles[ListIndex]->VisitAllRecords(unloader);
return 0;
}
FormID_Iterator Collection::LookupRecord(ModFile *&curModFile, const FORMID &RecordFormID, Record *&curRecord)
{
for(FormID_Range range = curModFile->Flags.IsExtendedConflicts ? ExtendedFormID_ModFile_Record.equal_range(RecordFormID) : FormID_ModFile_Record.equal_range(RecordFormID); range.first != range.second; ++range.first)
{
curRecord = range.first->second;
if(curRecord->GetParentMod() == curModFile)
return range.first;
}
curRecord = NULL;
return curModFile->Flags.IsExtendedConflicts ? ExtendedFormID_ModFile_Record.end() : FormID_ModFile_Record.end();
}
EditorID_Iterator Collection::LookupRecord(ModFile *&curModFile, STRING const &RecordEditorID, Record *&curRecord)
{
for(EditorID_Range range = curModFile->Flags.IsExtendedConflicts ? ExtendedEditorID_ModFile_Record.equal_range(RecordEditorID) : EditorID_ModFile_Record.equal_range(RecordEditorID); range.first != range.second; ++range.first)
{
curRecord = range.first->second;
if(curRecord->GetParentMod() == curModFile)
return range.first;
}
curRecord = NULL;
return curModFile->Flags.IsExtendedConflicts ? ExtendedEditorID_ModFile_Record.end() : EditorID_ModFile_Record.end();
}
FormID_Iterator Collection::LookupWinningRecord(const FORMID &RecordFormID, ModFile *&WinningModFile, Record *&WinningRecord, const bool GetExtendedConflicts)
{
WinningModFile = NULL;
WinningRecord = NULL;
FormID_Iterator Winning_it;
SINT32 ModID = -1;
ModFile *curModFile = NULL;
Record *curRecord = NULL;
for(FormID_Range range = FormID_ModFile_Record.equal_range(RecordFormID); range.first != range.second; ++range.first)
{
curRecord = range.first->second;
curModFile = curRecord->GetParentMod();
curRecord->IsWinning(false);
if((SINT32)curModFile->ModID > ModID && (curModFile->Flags.IsInLoadOrder || curModFile->Flags.IsIgnoreInactiveMasters))
{
ModID = curModFile->ModID;
WinningModFile = curModFile;
WinningRecord = range.first->second;
Winning_it = range.first;
}
}
if(WinningRecord != NULL)
WinningRecord->IsWinning(true);
if(GetExtendedConflicts)
{
for(FormID_Range range = ExtendedFormID_ModFile_Record.equal_range(RecordFormID); range.first != range.second; ++range.first)
{
curRecord = range.first->second;
curModFile = curRecord->GetParentMod();
curRecord->IsWinning(false);
if((SINT32)curModFile->ModID > ModID && (curModFile->Flags.IsInLoadOrder || curModFile->Flags.IsIgnoreInactiveMasters))
{
ModID = curModFile->ModID;
WinningModFile = curModFile;
WinningRecord = range.first->second;
Winning_it = range.first;
}
}
if(WinningRecord != NULL)
WinningRecord->IsExtendedWinning(true);
}
if(ModID > -1)
return Winning_it;
return FormID_ModFile_Record.end();
}
EditorID_Iterator Collection::LookupWinningRecord(STRING const &RecordEditorID, ModFile *&WinningModFile, Record *&WinningRecord, const bool GetExtendedConflicts)
{
WinningModFile = NULL;
WinningRecord = NULL;
EditorID_Iterator Winning_it;
SINT32 ModID = -1;
ModFile *curModFile = NULL;
Record *curRecord = NULL;
for(EditorID_Range range = EditorID_ModFile_Record.equal_range(RecordEditorID); range.first != range.second; ++range.first)
{
curRecord = range.first->second;
curModFile = curRecord->GetParentMod();
curRecord->IsWinning(false);
if((SINT32)curModFile->ModID > ModID && (curModFile->Flags.IsInLoadOrder || curModFile->Flags.IsIgnoreInactiveMasters))
{
ModID = curModFile->ModID;
WinningModFile = curModFile;
WinningRecord = range.first->second;
Winning_it = range.first;
}
}
if(WinningRecord != NULL)
WinningRecord->IsWinning(true);
if(GetExtendedConflicts)
{
for(EditorID_Range range = ExtendedEditorID_ModFile_Record.equal_range(RecordEditorID); range.first != range.second; ++range.first)
{
curRecord = range.first->second;
curModFile = curRecord->GetParentMod();
curRecord->IsWinning(false);
if((SINT32)curModFile->ModID > ModID && (curModFile->Flags.IsInLoadOrder || curModFile->Flags.IsIgnoreInactiveMasters))
{
ModID = curModFile->ModID;
WinningModFile = curModFile;
WinningRecord = range.first->second;
Winning_it = range.first;
}
}
if(WinningRecord != NULL)
WinningRecord->IsExtendedWinning(true);
}
if(ModID > -1)
return Winning_it;
return EditorID_ModFile_Record.end();
}
UINT32 Collection::GetNumRecordConflicts(Record *&curRecord, const bool GetExtendedConflicts)
{
UINT32 count = 0;
if(curRecord->IsKeyedByEditorID())
{
STRING RecordEditorID = curRecord->GetEditorIDKey();
if(RecordEditorID != NULL)
{
count = (UINT32)EditorID_ModFile_Record.count(RecordEditorID);
if(GetExtendedConflicts)
count += (UINT32)ExtendedEditorID_ModFile_Record.count(RecordEditorID);
}
}
else
{
count = (UINT32)FormID_ModFile_Record.count(curRecord->formID);
if(GetExtendedConflicts)
count += (UINT32)ExtendedFormID_ModFile_Record.count(curRecord->formID);
}
return count;
}
SINT32 Collection::GetRecordConflicts(Record *&curRecord, RECORDIDARRAY RecordIDs, const bool GetExtendedConflicts)
{
ModFile *curModFile = NULL;
if(curRecord->IsKeyedByEditorID())
{
STRING RecordEditorID = curRecord->GetEditorIDKey();
if(RecordEditorID != NULL)
{
for(EditorID_Range range = EditorID_ModFile_Record.equal_range(RecordEditorID); range.first != range.second; ++range.first)
{
curModFile = range.first->second->GetParentMod();
if(curModFile->Flags.IsInLoadOrder || curModFile->Flags.IsIgnoreInactiveMasters)
sortedConflicts.push_back(range.first->second);
}
if(GetExtendedConflicts)
{
for(EditorID_Range range = ExtendedEditorID_ModFile_Record.equal_range(RecordEditorID); range.first != range.second; ++range.first)
{
curModFile = range.first->second->GetParentMod();
if(curModFile->Flags.IsInLoadOrder || curModFile->Flags.IsIgnoreInactiveMasters)
sortedConflicts.push_back(range.first->second);
}
}
}
}
else
{
for(FormID_Range range = FormID_ModFile_Record.equal_range(curRecord->formID); range.first != range.second; ++range.first)
{
curModFile = range.first->second->GetParentMod();
if(curModFile->Flags.IsInLoadOrder || curModFile->Flags.IsIgnoreInactiveMasters)
sortedConflicts.push_back(range.first->second);
}
if(GetExtendedConflicts)
{
for(FormID_Range range = ExtendedFormID_ModFile_Record.equal_range(curRecord->formID); range.first != range.second; ++range.first)
{
curModFile = range.first->second->GetParentMod();
if(curModFile->Flags.IsInLoadOrder || curModFile->Flags.IsIgnoreInactiveMasters)
sortedConflicts.push_back(range.first->second);
}
}
}
UINT32 y = (UINT32)sortedConflicts.size();
if(y)
{
std::sort(sortedConflicts.begin(), sortedConflicts.end(), compConflicts);
for(UINT32 x = 0; x < y; ++x)
RecordIDs[x] = sortedConflicts[x];
sortedConflicts.clear();
}
return y;
}
SINT32 Collection::GetRecordHistory(Record *&curRecord, RECORDIDARRAY RecordIDs)
{
ModFile *curModFile = curRecord->GetParentMod();
if(curModFile->Flags.IsExtendedConflicts)
{
//Temporarily silenced until I figure out the best way to handle/avoid the issue
//printer("GetRecordHistory: Warning - No history available. Mod \"%s\" uses extended conflicts.\n", curModFile->ModName);
return -1;
}
UINT8 curCollapsedIndex = curModFile->FormIDHandler.CollapsedIndex;
const UINT8 (&CollapseTable)[256] = curModFile->FormIDHandler.CollapseTable;
ModFile *testModFile = NULL;
if(curRecord->IsKeyedByEditorID())
{
STRING RecordEditorID = curRecord->GetEditorIDKey();
if(RecordEditorID != NULL)
{
for(EditorID_Range range = EditorID_ModFile_Record.equal_range(RecordEditorID); range.first != range.second; ++range.first)
{
testModFile = range.first->second->GetParentMod();
if(testModFile->Flags.IsInLoadOrder || testModFile->Flags.IsIgnoreInactiveMasters)
if(CollapseTable[testModFile->FormIDHandler.ExpandedIndex] != curCollapsedIndex)
sortedConflicts.push_back(range.first->second);
}
}
}
else
{
for(FormID_Range range = FormID_ModFile_Record.equal_range(curRecord->formID); range.first != range.second; ++range.first)
{
testModFile = range.first->second->GetParentMod();
if(testModFile->Flags.IsInLoadOrder || testModFile->Flags.IsIgnoreInactiveMasters)
if(CollapseTable[testModFile->FormIDHandler.ExpandedIndex] != curCollapsedIndex)
sortedConflicts.push_back(range.first->second);
}
}
UINT32 y = (UINT32)sortedConflicts.size();
if(y)
{
std::sort(sortedConflicts.begin(), sortedConflicts.end(), compHistory);
for(UINT32 x = 0; x < y; ++x)
RecordIDs[x] = sortedConflicts[x];
sortedConflicts.clear();
}
return y;
}
UINT32 Collection::NextFreeExpandedFormID(ModFile *&curModFile, UINT32 depth)
{
UINT32 curFormID = curModFile->FormIDHandler.NextExpandedFormID();
FormID_Range range = curModFile->Flags.IsExtendedConflicts ? ExtendedFormID_ModFile_Record.equal_range(curFormID) : FormID_ModFile_Record.equal_range(curFormID);
//FormID doesn't exist in any mod, so it's free for use
if(range.first == range.second)
return curFormID;
//The formID already exists, so try again (either in that mod, or being injected into that mod)
//Wrap around and check for any freed formIDs until they're all checked. Unlikely to ever occur.
return (depth < 0x00FFFFFF) ? NextFreeExpandedFormID(curModFile, ++depth) : 0;
}
Record * Collection::CreateRecord(ModFile *&curModFile, const UINT32 &RecordType, FORMID RecordFormID, STRING const &RecordEditorID, const FORMID &ParentFormID, UINT32 CreateFlags)
{
if(!curModFile->Flags.IsInLoadOrder)
{
printer("CreateRecord: Error - Unable to create any records in mod \"%s\". It is not in the load order.\n", curModFile->ModName);
return NULL;
}
CreationFlags options(CreateFlags);
Record *DummyRecord = NULL;
Record *ParentRecord = NULL;
if((RecordFormID & 0x00FFFFFF) < END_HARDCODED_IDS)
RecordFormID &= 0x00FFFFFF;
//See if the requested record already exists
if(RecordFormID != 0)
LookupRecord(curModFile, RecordFormID, DummyRecord);
else if(RecordEditorID != NULL)
LookupRecord(curModFile, RecordEditorID, DummyRecord);
if(DummyRecord != NULL)
return DummyRecord;
//Lookup the required data, and ensure it exists
if(ParentFormID)
{
LookupRecord(curModFile, ParentFormID, ParentRecord);
if(ParentRecord == NULL)
{
printer("CreateRecord: Error - Unable to locate parent record (%08X). It does not exist in \"%s\".\n", ParentFormID, curModFile->ModName);
return NULL;
}
}
//Create the new record
Record *curRecord = curModFile->CreateRecord(RecordType, RecordEditorID, DummyRecord, ParentRecord, options);
if(curRecord == NULL)
{
printer("CreateRecord: Error - Unable to create record of type \"%c%c%c%c\" in mod \"%s\". An unknown error occurred.\n", ((STRING)&RecordType)[0], ((STRING)&RecordType)[1], ((STRING)&RecordType)[2], ((STRING)&RecordType)[3], curModFile->ModName);
return NULL;
}
//See if an existing record was returned instead of a new record
if(options.ExistingReturned)
return curRecord;
//Assign the new record a formID
//Ideally, if keyed by editor id, assign a new formID so that FormIDMasterUpdater doesn't add unneeded masters
//curRecord->formID = (RecordFormID == 0 || curRecord->IsKeyedByEditorID()) ? NextFreeExpandedFormID(curModFile) : RecordFormID;
//Trying to keep existing formID if possible to make TES4Edit happy
// and so future undeleting of EditorID keyed records has a chance of working
curRecord->formID = RecordFormID == 0 ? NextFreeExpandedFormID(curModFile) : RecordFormID;
//Then the destination mod's tables get used so that they can be updated
FormIDMasterUpdater checker(curModFile->FormIDHandler);
checker.Accept(curRecord->formID);
//curRecord->VisitFormIDs(checker); //Shouldn't be needed unless a record defaults to having formIDs set (none do atm)
//Index the new record
RecordIndexer indexer(curModFile, curModFile->Flags.IsExtendedConflicts ? ExtendedEditorID_ModFile_Record: EditorID_ModFile_Record, curModFile->Flags.IsExtendedConflicts ? ExtendedFormID_ModFile_Record: FormID_ModFile_Record);
indexer.Accept(curRecord);
if(RecordFormID != 0)
{
//Update the IsWinning flags for all related records
ModFile *WinningModfile = NULL;
Record *WinningRecord = NULL;
if(curRecord->IsKeyedByEditorID())
LookupWinningRecord(curRecord->GetEditorIDKey(), WinningModfile, WinningRecord, true);
else
LookupWinningRecord(curRecord->formID, WinningModfile, WinningRecord, true);
}
return curRecord;
}
Record * Collection::CopyRecord(Record *&curRecord, ModFile *&DestModFile, const FORMID &DestParentFormID, FORMID DestRecordFormID, STRING const &DestRecordEditorID, UINT32 CreateFlags)
{
ModFile *curModFile = curRecord->GetParentMod();
if(!curModFile->Flags.IsInLoadOrder && !curModFile->Flags.IsIgnoreInactiveMasters)
{
printer("CopyRecord: Error - Unable to copy any records from source mod \"%s\". It is not in the load order and may require absent masters.\n", curModFile->ModName);
return NULL;
}
CreationFlags options(CreateFlags);
Record *ParentRecord = NULL;
Record *RecordCopy = NULL;
if(options.SetAsOverride)
{
//See if its trying to copy a record that already exists in the destination mod
if(curRecord->IsKeyedByEditorID())
LookupRecord(DestModFile, DestRecordEditorID ? DestRecordEditorID : curRecord->GetEditorIDKey(), RecordCopy);
else
LookupRecord(DestModFile, DestRecordFormID ? DestRecordFormID : curRecord->formID, RecordCopy);
}
else if(DestRecordFormID != 0)
{
//If the objectID of a formID is less than END_HARDCODED_IDS, then it doesn't use the modIndex portion
//instead, it "belongs" to the engine, and they all override each other
if((DestRecordFormID & 0x00FFFFFF) < END_HARDCODED_IDS)
DestRecordFormID &= 0x00FFFFFF;
//See if its trying to copy a record that already exists in the destination mod
if(curRecord->IsKeyedByEditorID())
LookupRecord(DestModFile, DestRecordEditorID ? DestRecordEditorID : curRecord->GetEditorIDKey(), RecordCopy);
else
LookupRecord(DestModFile, DestRecordFormID, RecordCopy);
}
else if(curRecord->IsKeyedByEditorID())
{
//See if its trying to copy a record that already exists in the destination mod
LookupRecord(DestModFile, DestRecordEditorID ? DestRecordEditorID : curRecord->GetEditorIDKey(), RecordCopy);
}
if(RecordCopy != NULL)
return RecordCopy;
if(DestParentFormID)
{
//See if the parent record already exists in the destination mod
LookupRecord(DestModFile, DestParentFormID, ParentRecord);
if(ParentRecord == NULL)
{
ModFile *ParentModFile = NULL;
//If it doesn't, try and create it.
if(options.CopyWinningParent)
LookupWinningRecord(DestParentFormID, ParentModFile, ParentRecord);
else
LookupRecord(curModFile, DestParentFormID, ParentRecord);
if(ParentRecord == NULL)
{
printer("CopyRecord: Error - Unable to locate destination parent record (%08X). It does not exist in \"%s\" or \"%s\".\n", DestParentFormID, DestModFile->ModName, curModFile->ModName);
return NULL;
}
ParentRecord = CopyRecord(ParentRecord, DestModFile, ParentRecord->GetParentRecord() != NULL ? ParentRecord->GetParentRecord()->formID : 0, 0, 0, options.GetFlags());
if(ParentRecord == NULL)
{
printer("CopyRecord: Error - Unable to copy missing destination parent record (%08X). It does not exist in \"%s\", and there was an error copying it from \"%s\".\n", DestParentFormID, DestModFile->ModName, curModFile->ModName);
return NULL;
}
}
}
if(curModFile == DestModFile && options.SetAsOverride)
{
printer("CopyRecord: Error - Unable to copy (%08X) as an override record. Source and destination mods \"%s\" are the same.\n", curRecord->formID, curModFile->ModName);
return NULL;
}
if(!DestModFile->Flags.IsInLoadOrder && !options.SetAsOverride)
{
printer("CopyRecord: Error - Unable to copy (%08X) as a new record. Destination \"%s\" is not in the load order.\n", curRecord->formID, DestModFile->ModName);
return NULL;
}
//Create the record copy
RecordCopy = DestModFile->CreateRecord(curRecord->GetType(), DestRecordEditorID, curRecord, ParentRecord, options);
if(RecordCopy == NULL)
{
printer("CopyRecord: Error - Unable to create the copied record (%08X). An unknown error occurred when copying the record from \"%s\" to \"%s\".\n", curRecord->formID, DestModFile->ModName, curModFile->ModName);
return NULL;
}
//See if an existing record was returned instead of the requested copy
if(options.ExistingReturned)
return RecordCopy;
//Copy over the internal flags
RecordCopy->CBash_Flags = curRecord->CBash_Flags;
if(!curRecord->IsChanged())
RecordCopy->IsLoaded(false);
//Give the record a new formID if it isn't an override record
if(!options.SetAsOverride)
RecordCopy->formID = DestRecordFormID ? DestRecordFormID : NextFreeExpandedFormID(DestModFile);
//DPRINT("Copied %08X from %s to %s", RecordCopy->formID, curRecord->GetParentMod()->FileName, DestModFile->FileName);
//Ideally, assign the formID to the destination mod so that FormIDMasterUpdater doesn't add unneeded masters
//else if(RecordCopy->IsKeyedByEditorID())
// RecordCopy->formID = NextFreeExpandedFormID(DestModFile);
//Trying to keep existing formID if possible to make TES4Edit happy
// and so future undeleting of EditorID keyed records has a chance of working
//See if the destination mod masters need updating
//Ensure the record has been fully read
//Uses the source mod's formID resolution tables
RecordReader reader(curModFile->FormIDHandler, Expanders);
reader.Accept(RecordCopy);
//Then the destination mod's tables get used so that they can be updated
FormIDMasterUpdater checker(DestModFile->FormIDHandler);
checker.Accept(RecordCopy->formID);
RecordCopy->VisitFormIDs(checker);
//Index the record
RecordIndexer indexer(DestModFile, DestModFile->Flags.IsExtendedConflicts ? ExtendedEditorID_ModFile_Record: EditorID_ModFile_Record, DestModFile->Flags.IsExtendedConflicts ? ExtendedFormID_ModFile_Record: FormID_ModFile_Record);
indexer.Accept(RecordCopy);
if(curRecord->IsWinningDetermined() || curRecord->formID != RecordCopy->formID)
{
//Update the IsWinning flags for all related records
ModFile *WinningModfile = NULL;
Record *WinningRecord = NULL;
if(RecordCopy->IsKeyedByEditorID())
LookupWinningRecord(RecordCopy->GetEditorIDKey(), WinningModfile, WinningRecord, true);
else
LookupWinningRecord(RecordCopy->formID, WinningModfile, WinningRecord, true);
}
if(reader.result) //If the record was read, go ahead and unload it
RecordCopy->Unload();
return RecordCopy;
}
SINT32 Collection::CleanModMasters(ModFile *curModFile)
{
if(!curModFile->Flags.IsInLoadOrder)
{
printer("CleanModMasters: Error - Unable to clean \"%s\"'s masters. It is not in the load order.\n", curModFile->ModName);
return NULL;
}
RecordMasterCollector collector(curModFile->FormIDHandler, Expanders);
curModFile->VisitAllRecords(collector);
UINT32 cleaned = 0;
for(SINT32 ListIndex = curModFile->TES4.MAST.size() - 1; ListIndex >= 0 ; --ListIndex)
{
if(collector.collector.UsedTable[ListIndex] == 0)
{