forked from TES5Edit/TES5Edit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wbImplementation.pas
17178 lines (14867 loc) · 512 KB
/
wbImplementation.pas
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
{*******************************************************************************
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.
*******************************************************************************}
unit wbImplementation;
{$I wbDefines.inc}
{$DEFINE DBGSUBREC}
interface
uses
Variants,
Windows,
Classes,
SysUtils,
Contnrs,
Math,
wbInterface,
{$IFDEF USE_CODESITE}
CodeSiteLogging,
{$ENDIF}
{$IFDEF USE_PARALLEL_BUILD_REFS}
System.SyncObjs,
{$ENDIF}
Zlibex,
lz4;
const
DefaultVCS1 = 0;
DefaultVCS2 = 0;
var
RecordToSkip : TStringList;
GroupToSkip : TStringList;
ChaptersToSkip : TStringList;
SubRecordOrderList : TStringList;
function wbMastersForFile(const aFileName: string; aMasters: TStrings; aIsESM: PBoolean = nil; aIsESL: PBoolean = nil): Boolean; overload;
function wbMastersForFile(const aFileName: string; out aMasters: TDynStrings; aIsESM: PBoolean = nil; aIsESL: PBoolean = nil): Boolean; overload;
function wbFile(const aFileName: string; aLoadOrder: Integer = -1; aCompareTo: string = '';
IsTemporary: Boolean = False; aOnlyHeader: Boolean = False): IwbFile;
function wbNewFile(const aFileName: string; aLoadOrder: Integer; aIsESL: Boolean): IwbFile;
procedure wbFileForceClosed;
function StartsWith(const s, t: string): Boolean;
function wbCopyElementToFile(const aSource: IwbElement; aFile: IwbFile; aAsNew, aDeepCopy: Boolean; const aPrefixRemove, aPrefix, aSuffix: string): IwbElement;
function wbCopyElementToRecord(const aSource: IwbElement; aMainRecord: IwbMainRecord; aAsNew, aDeepCopy: Boolean): IwbElement;
function wbFindWinningMainRecordByEditorID(const aSignature: TwbSignature; const aEditorID: string): IwbMainRecord;
function wbFormListToArray(const aFormList: IwbMainRecord; const aSignatures: string): TDynMainRecords;
function wbCreateKeepAliveRoot: IwbKeepAliveRoot;
function wbBeginKeepAlive: Integer;
function wbEndKeepAlive: Integer;
implementation
uses
wbInit,
wbHelpers,
wbSort;
const
EmptyPtr: AnsiChar = #0;
type
PwbKeepAliveContext = ^TwbKeepAliveContext;
TwbKeepAliveContext = record
kacHead : IwbContainerElementRef;
kacPrev : PwbKeepAliveContext;
kacFinished : Boolean;
end;
TwbKeepAliveRoot = class(TInterfacedObject, IwbKeepAliveRoot)
protected {private}
karKAC: PwbKeepAliveContext;
procedure Setup;
procedure Teardown;
constructor Create;
destructor Destroy; override;
{---IwbKeepAliveRoot---}
procedure Done;
end;
function wbCreateKeepAliveRoot: IwbKeepAliveRoot;
begin
Result := TwbKeepAliveRoot.Create;
end;
threadvar
wbKeepAliveContext : PwbKeepAliveContext;
wbKeepAliveCount : Integer;
wbKeepAliveRoot : IwbKeepAliveRoot;
function wbBeginKeepAlive: Integer;
begin
Result := Succ(wbKeepAliveCount);
wbKeepAliveCount := Result;
if (Result > 0) and not Assigned(wbKeepAliveRoot) then
wbKeepAliveRoot := wbCreateKeepAliveRoot;
end;
function wbEndKeepAlive: Integer;
begin
Result := Pred(wbKeepAliveCount);
wbKeepAliveCount := Result;
if Result = 0 then
wbKeepAliveRoot := nil;
end;
const
TheEmptyPlugin = 'SavesEmptyPlugin.esp';
type
TwbMainRecordEntryHeader = record
mrehGeneration : Cardinal;
mrehHead : Pointer;
mrehTail : Pointer;
mrehCount : Cardinal;
mrehInUse : Boolean;
procedure BeginUse;
procedure EndUse;
end;
var
mreHeader: TwbMainRecordEntryHeader;
function wbCopyElementToFile(const aSource: IwbElement; aFile: IwbFile; aAsNew, aDeepCopy: Boolean; const aPrefixRemove, aPrefix, aSuffix: string): IwbElement;
var
MainRecord : IwbMainRecord;
Container : IwbContainer;
Target : IwbElement;
begin
Inc(wbCopyIsRunning);
try
if (wbCurrentTick>0) and (wbCurrentTick+500<GetTickCount64) then begin
wbProgressCallback('');
wbCurrentTick := GetTickCount64;
end;
Container := aSource.Container;
if Assigned(Container) then begin
if Supports(Container, IwbMainRecord, MainRecord) then
Container := MainRecord.HighestOverrideOrSelf[aFile.LoadOrder];
Target := wbCopyElementToFile(Container, aFile, False, False, aPrefixRemove, aPrefix, aSuffix)
end else begin
Result := aFile;
Exit;
end;
if Assigned(Target) then
Result := Target.AddIfMissing(aSource, aAsNew, aDeepCopy, aPrefixRemove, aPrefix, aSuffix)
else
Result := nil;
finally
Dec(wbCopyIsRunning);
end;
end;
function wbCopyElementToRecord(const aSource: IwbElement; aMainRecord: IwbMainRecord; aAsNew, aDeepCopy: Boolean): IwbElement;
var
Container : IwbContainer;
Target : IwbElement;
CER : IwbContainerElementRef;
begin
CER := aMainRecord as IwbContainerElementRef;
if Assigned(aSource) and (aSource.ElementType = etMainRecord) then begin
if aSource.Equals(aMainRecord) then
Result := nil
else
Result := aMainRecord;
Exit;
end;
Container := aSource.Container;
Assert(Assigned(Container));
Target := wbCopyElementToRecord(Container, aMainRecord, False, False);
if Assigned(Target) then
Result := Target.AddIfMissing(aSource, aAsNew, aDeepCopy, '', '', '')
else
Result := nil;
end;
function StartsWith(const s, t: string): Boolean;
var
i : Integer;
begin
Result := False;
if Length(s) < Length(t) then
Exit;
for i := 1 to Length(t) do
if UpCase(s[i]) <> UpCase(t[i]) then
Exit;
Result := True;
end;
type
IwbElementInternal = interface(IwbElement)
['{556DF03C-2723-46FC-99C6-F50BB5E66F86}']
procedure SetContainer(const aContainer: IwbContainer);
procedure SetSortOrder(aIndex: Integer);
procedure SetMemoryOrder(aIndex: Integer);
function GetMemoryOrder: Integer;
procedure SetModified(aValue: Boolean);
procedure SetInternalModified(aValue: Boolean);
function GetCountedRecordCount: Cardinal;
procedure PrepareSave;
procedure MasterCountUpdated(aOld, aNew: Byte);
procedure MasterIndicesUpdated(const aOld, aNew: TwbFileIDs);
procedure FindUsedMasters(aMasters: PwbUsedMasters);
procedure InvalidateStorage;
function Reached: Boolean;
function BeginDecide: Boolean;
procedure EndDecide;
property Modified: Boolean
read GetModified
write SetModified;
property MemoryOrder: Integer
read GetMemoryOrder
write SetMemoryOrder;
property InternalModified: Boolean
write SetInternalModified;
end;
TwbElement = class(TInterfacedObject, IInterface, IwbElement, IwbElementInternal)
protected
eContainer : Pointer{IwbContainer}; //weak reference
eSortOrder : Integer;
eMemoryOrder : Integer;
eStates : TwbElementStates;
eSortKey : string;
eExtendedSortKey : string;
eExternalRefs : Integer;
eContainerRef : IwbContainerElementRef;
eUpdateCount : Integer;
eGeneration : Integer;
{---IInterface---}
function _AddRef: Integer; virtual; stdcall;
function _Release: Integer; virtual; stdcall;
{---IwbElementInternal---}
function InternalAddRef: Integer; stdcall;
function InternalRelease: Integer; stdcall;
function IwbElementInternal._AddRef = InternalAddRef;
function IwbElementInternal._Release = InternalRelease;
function GetCountedRecordCount: Cardinal; virtual;
procedure PrepareSave; virtual;
procedure MasterCountUpdated(aOld, aNew: Byte); virtual;
procedure MasterIndicesUpdated(const aOld, aNew: TwbFileIDs); virtual;
procedure FindUsedMasters(aMasters: PwbUsedMasters); virtual;
procedure InvalidateStorage; virtual;
procedure InvalidateParentStorage; virtual;
function Reached: Boolean; virtual;
function LinksToParent: Boolean; virtual;
procedure SetMemoryOrder(aIndex: Integer);
function GetMemoryOrder: Integer;
function BeginDecide: Boolean;
procedure EndDecide;
procedure NotifyChanged(aContainer: Pointer);
procedure NotifyChangedInternal(aContainer: Pointer); virtual;
procedure ReportRequiredMasters(aStrings: TStrings; aAsNew: Boolean; Recursive: Boolean = True; Initial: Boolean = false); virtual;
function GetElementID: Pointer;
function GetElementStates: TwbElementStates;
procedure SetElementState(aState: TwbElementState; Clear: Boolean = false);
function Equals(const aElement: IwbElement): Boolean; reintroduce;
procedure Hide;
procedure Show;
function GetIsHidden: Boolean;
function HasErrors: Boolean; virtual;
function GetValue: string; virtual;
function GetCheck: string; virtual;
function GetSortKey(aExtended: Boolean): string; virtual;
function GetSortKeyInternal(aExtended: Boolean): string; virtual;
function GetSortPriority: Integer; virtual;
function GetName: string; virtual;
function GetBaseName: string; virtual;
function GetDisplayName: string; virtual;
function GetShortName: string; virtual;
function GetPath: string; virtual;
function GetFullPath: string; virtual;
function GetPathName: string; virtual;
function GetSkipped: Boolean; virtual;
function GetDef: IwbNamedDef; virtual;
function GetValueDef: IwbValueDef; virtual;
function GetResolvedValueDef: IwbValueDef; virtual;
function GetElementType: TwbElementType; virtual;
procedure DoReset(aForce: Boolean); virtual;
function GetContainer: IwbContainer;
function GetContainingMainRecord: IwbMainRecord; virtual;
function GetFile: IwbFile; virtual;
function GetReferenceFile: IwbFile; virtual;
function GetSortOrder: Integer;
procedure BuildRef; virtual;
procedure AddReferencedFromID(aFormID: TwbFormID); virtual;
function CompareExchangeFormID(aOldFormID: TwbFormID; aNewFormID: TwbFormID): Boolean; virtual;
function GetIsEditable: Boolean; virtual;
function GetIsRemoveable: Boolean; virtual;
function GetEditValue: string; virtual;
procedure SetEditValue(const aValue: string); virtual;
function GetNativeValue: Variant; virtual;
procedure SetNativeValue(const aValue: Variant); virtual;
procedure RequestStorageChange(var aBasePtr, aEndPtr: Pointer; aNewSize: Cardinal); virtual;
function GetConflictPriority: TwbConflictPriority; virtual;
function GetConflictPriorityCanChange: Boolean; virtual;
function GetModified: Boolean;
procedure MarkModifiedRecursive; virtual;
function GetIsInjected: Boolean; virtual;
function GetReferencesInjected: Boolean; virtual;
function GetInjectionSourceFiles: TDynFiles; virtual;
function GetIsNotReachable: Boolean; virtual;
procedure SetModified(aValue: Boolean); virtual;
procedure SetInternalModified(aValue: Boolean); virtual;
function GetDataSize: Integer; virtual;
procedure SetDataSize(aSize: Integer); virtual;
procedure MergeStorage(var aBasePtr: Pointer; aEndPtr: Pointer);
procedure MergeStorageInternal(var aBasePtr: Pointer; aEndPtr: Pointer); virtual;
procedure InformStorage(var aBasePtr: Pointer; aEndPtr: Pointer); virtual;
procedure Remove; virtual;
function CanContainFormIDs: Boolean; virtual;
function AddIfMissing(const aElement: IwbElement; aAsNew, aDeepCopy : Boolean; const aPrefixRemove, aPrefix, aSuffix: string): IwbElement;
function AddIfMissingInternal(const aElement: IwbElement; aAsNew, aDeepCopy : Boolean; const aPrefixRemove, aPrefix, aSuffix: string): IwbElement; virtual;
procedure ResetConflict; virtual;
procedure ResetReachable; virtual;
function RemoveInjected(aCanRemove: Boolean): Boolean; virtual;
function GetEditType: TwbEditType; virtual;
function GetEditInfo: string; virtual;
function GetDontShow: Boolean; virtual;
procedure SetToDefault;
procedure SetToDefaultInternal; virtual;
function CanAssign(aIndex: Integer; const aElement: IwbElement; aCheckDontShow: Boolean): Boolean;
function CanAssignInternal(aIndex: Integer; const aElement: IwbElement; aCheckDontShow: Boolean): Boolean; virtual;
function Assign(aIndex: Integer; const aElement: IwbElement; aOnlySK: Boolean): IwbElement;
function AssignInternal(aIndex: Integer; const aElement: IwbElement; aOnlySK: Boolean): IwbElement; virtual;
procedure WriteToStream(aStream: TStream; aResetModified: Boolean);
procedure WriteToStreamInternal(aStream: TStream; aResetModified: Boolean); virtual;
function GetLinksTo: IwbElement; virtual;
function GetNoReach: Boolean;
procedure SetContainer(const aContainer: IwbContainer); virtual;
procedure SetSortOrder(aIndex: Integer);
procedure DoAfterSet(const aOldValue, aNewValue: Variant); virtual;
procedure MoveUp;
procedure MoveDown;
function CanMoveUp: Boolean;
function CanMoveDown: Boolean;
procedure NextMember;
procedure PreviousMember;
function CanChangeMember: Boolean;
procedure Tag;
procedure ResetTags; virtual;
function IsTagged: Boolean;
function CopyInto(const aFile: IwbFile; aAsNew, aDeepCopy: Boolean; const aPrefixRemove, aPrefix, aSuffix: string): IwbElement;
function BeginUpdate: Integer;
function EndUpdate: Integer;
procedure UpdatedEnded; virtual;
constructor Create(const aContainer: IwbContainer);
procedure BeforeDestruction; override;
procedure AfterConstruction; override;
class function NewInstance: TObject; override;
procedure FreeInstance; override;
function GetTreeHead: Boolean; // Is the element expected to be a "header record" in the tree navigator
function GetTreeBranch: Boolean; // Is the element expected to show in the tree navigator
end;
TDynElementInternals = array of IwbElementInternal;
IwbContainerInternal = interface(IwbContainer)
['{8D9AC0D3-3961-4320-A036-EB4771B081CD}']
function ReleaseElements: TDynElementInternals;
procedure ElementChanged(const aElement: IwbElement; aContainer: Pointer);
procedure SortBySortOrder;
procedure CreatedEmpty;
procedure MoveElementUp(const aElement: IwbElement);
procedure MoveElementDown(const aElement: IwbElement);
function CanMoveElementUp(const aElement: IwbElement): Boolean;
function CanMoveElementDown(const aElement: IwbElement): Boolean;
procedure NextElementMember(const aElement: IwbElement);
procedure PreviousElementMember(const aElement: IwbElement);
function CanChangeElementMember(const aElement: IwbElement): Boolean;
end;
TwbContainer = class(TwbElement, IwbContainerElementRef, IwbContainer, IwbContainerInternal)
protected
cntElements : TDynElementInternals;
cntElementsMap : TDynCardinalArray;
cntElementRefs : Integer;
cntStates : TwbContainerStates;
cntKeepAliveNext : IwbContainerElementRef;
cntRefsBuildAt : Integer;
function _AddRef: Integer; override; stdcall;
function _Release: Integer; override; stdcall;
{---IwbContainerElementRef---}
function ElementAddRef: Integer; stdcall;
function ElementRelease: Integer; stdcall;
function IwbContainerElementRef._AddRef = ElementAddRef;
function IwbContainerElementRef._Release = ElementRelease;
function GetCountedRecordCount: Cardinal; override;
procedure PrepareSave; override;
procedure MasterCountUpdated(aOld, aNew: Byte); override;
procedure MasterIndicesUpdated(const aOld, aNew: TwbFileIDs); override;
procedure FindUsedMasters(aMasters: PwbUsedMasters); override;
procedure SortBySortOrder; virtual;
procedure CreatedEmpty;
function Reached: Boolean; override;
function RemoveInjected(aCanRemove: Boolean): Boolean; override;
procedure ReportRequiredMasters(aStrings: TStrings; aAsNew: Boolean; Recursive: Boolean = True; Initial: Boolean = false); override;
procedure ResetConflict; override;
procedure ResetReachable; override;
procedure DoReset(aForce: Boolean); override;
procedure DoInit; virtual;
function HasErrors: Boolean; override;
function GetSortKeyInternal(aExtended: Boolean): string; override;
function GetDataSize: Integer; override;
procedure MergeStorageInternal(var aBasePtr: Pointer; aEndPtr: Pointer); override;
procedure InformStorage(var aBasePtr: Pointer; aEndPtr: Pointer); override;
procedure BuildRef; override;
procedure MarkModifiedRecursive; override;
function CanAssignInternal(aIndex: Integer; const aElement: IwbElement; aCheckDontShow: Boolean): Boolean; override;
function AssignInternal(aIndex: Integer; const aElement: IwbElement; aOnlySK: Boolean): IwbElement; override;
function GetIsInSK(aIndex: Integer): Boolean; virtual;
procedure SetToDefaultInternal; override;
procedure WriteToStreamInternal(aStream: TStream; aResetModified: Boolean); override;
function GetElement(aIndex: Integer): IwbElement;
function GetElementCount: Integer;
function GetElementByName(const aName: string): IwbElement;
function GetRecordBySignature(const aSignature: TwbSignature): IwbRecord;
function GetElementByMemoryOrder(aSortOrder: Integer): IwbElement;
function GetElementBySignature(const aSignature: TwbSignature): IwbElement;
function GetElementBySortOrder(aSortOrder: Integer): IwbElement;
function GetAdditionalElementCount: Integer; virtual;
procedure ReverseElements;
function GetContainerStates: TwbContainerStates;
function GetElementByPath(const aPath: string): IwbElement;
function GetElementValue(const aName: string): string;
function GetElementExists(const aName: string): Boolean;
function GetElementEditValue(const aName: string): string;
procedure SetElementEditValue(const aName, aValue: string);
function GetElementNativeValue(const aName: string): Variant;
procedure SetElementNativeValue(const aName: string; const aValue: Variant);
function GetElementLinksTo(const aName: string): IwbElement;
function GetElementSortKey(const aName: string; aExtended: Boolean): string;
function ResolveElementName(aName: string; out aRemainingName: string; aCanCreate: Boolean = False): IwbElement; virtual;
procedure AddElement(const aElement: IwbElement); virtual;
procedure InsertElement(aPosition: Integer; const aElement: IwbElement);
function RemoveElement(aPos: Integer; aMarkModified: Boolean = False): IwbElement; overload; virtual;
function RemoveElement(const aElement: IwbElement; aMarkModified: Boolean = False): IwbElement; overload; virtual;
function RemoveElement(const aName: string): IwbElement; overload;
function LastElement: IwbElement;
function CanElementReset: Boolean; virtual;
function GetAddList: TDynStrings; virtual;
function Add(const aName: string; aSilent: Boolean): IwbElement; virtual;
function IsElementRemoveable(const aElement: IwbElement): Boolean; virtual;
function IsElementEditable(const aElement: IwbElement): Boolean; virtual;
function IndexOf(const aElement: IwbElement): Integer; virtual;
function ReleaseElements: TDynElementInternals;
procedure ElementChanged(const aElement: IwbElement; aContainer: Pointer); virtual;
procedure NotifyChangedInternal(aContainer: Pointer); override;
function CompareExchangeFormID(aOldFormID: TwbFormID; aNewFormID: TwbFormID): Boolean; override;
procedure Init; virtual;
procedure Reset; virtual;
procedure Bar;
function ReleaseKeepAlive: IwbContainerElementRef;
procedure MoveElementUp(const aElement: IwbElement);
procedure MoveElementDown(const aElement: IwbElement);
function CanMoveElementUp(const aElement: IwbElement): Boolean;
function CanMoveElementDown(const aElement: IwbElement): Boolean;
function CanMoveElement: Boolean; virtual;
procedure NextElementMember(const aElement: IwbElement);
procedure PreviousElementMember(const aElement: IwbElement);
function CanChangeElementMember(const aElement: IwbElement): Boolean;
function FindBySortKey(const aSortKey: string; aExtended: Boolean; out aIndex: Integer): Boolean;
procedure AfterConstruction; override;
procedure BeforeDestruction; override;
class function NewInstance: TObject; override;
destructor Destroy; override;
procedure FreeInstance; override;
procedure ResetTags; override;
end;
IwbFileInternal = interface(IwbFile)
['{E1334034-06D0-4299-BFE0-E0DE97C128E2}']
procedure AddMainRecord(const aRecord: IwbMainRecord);
procedure RemoveMainRecord(const aRecord: IwbMainRecord);
procedure InjectMainRecord(const aRecord: IwbMainRecord);
procedure RemoveInjectedMainRecord(const aRecord: IwbMainRecord);
procedure ForceClosed;
procedure GetMasters(aMasters: TStrings);
end;
TwbFile = class(TwbContainer, IwbFile, IwbFileInternal)
protected
flFileName : string;
flLoadOrder : Integer;
flLoadOrderFileID : TwbFileID;
flCompareTo : string;
flStates : TwbFileStates;
flUnsavedSince : TDateTime;
flFileHandle : THandle;
flMapHandle : THandle;
flView : Pointer;
flSize : Cardinal;
flEndPtr : Pointer;
flCRC32 : Cardinal;
flMasters : array of IwbFile;
flRecords : array of IwbMainRecord;
flRecordsCount : Integer; {only used during loading}
flRecordsByEditorID : array of IwbMainRecord;
flRecordsByEditorIDCount : Integer; {only used during loading}
flLoadFinished : Boolean;
flFormIDsSorted : Boolean;
flInjectedRecords : array of IwbMainRecord;
procedure flOpenFile; virtual;
procedure flCloseFile; virtual;
procedure flProgress(const aStatus: string);
function Reached: Boolean; override;
function GetElementType: TwbElementType; override;
function GetFile: IwbFile; override;
function GetReferenceFile: IwbFile; override;
function GetName: string; override;
function GetBaseName: string; override;
procedure PrepareSave; override;
procedure SetModified(aValue: Boolean); override;
procedure BuildRef; override;
function BuildOrLoadRef(aOnlyLoad: Boolean): TwbBuildOrLoadRefResult;
function FindFormID(aFormID: TwbFormID; var Index: Integer): Boolean;
function FindInjectedID(aFormID: TwbFormID; var Index: Integer): Boolean;
function FindEditorID(const aEditorID: string; var Index: Integer): Boolean;
function GetMasterRecordByFormID(aFormID: TwbFormID; aAllowInjected: Boolean): IwbMainRecord;
function GetAddList: TDynStrings; override;
function Add(const aName: string; aSilent: Boolean): IwbElement; override;
function IsElementRemoveable(const aElement: IwbElement): Boolean; override;
function IsElementEditable(const aElement: IwbElement): Boolean; override;
function GetIsEditable: Boolean; override;
function GetIsRemoveable: Boolean; override;
procedure WriteToStreamInternal(aStream: TStream; aResetModified: Boolean); override;
function AddIfMissingInternal(const aElement: IwbElement; aAsNew, aDeepCopy : Boolean; const aPrefixRemove, aPrefix, aSuffix: string): IwbElement; override;
function NewFormID: TwbFormID;
{---IwbFile---}
function GetFileName: string;
function GetUnsavedSince: TDateTime; inline;
function HasMaster(const aFileName: string): Boolean;
function GetMaster(aIndex: Integer): IwbFile; inline;
function GetMasterCount: Integer; inline;
function GetRecordByFormID(aFormID: TwbFormID; aAllowInjected: Boolean): IwbMainRecord;
function GetRecordByEditorID(const aEditorID: string): IwbMainRecord;
function GetGroupBySignature(const aSignature: TwbSignature): IwbGroupRecord;
function HasGroup(const aSignature: TwbSignature): Boolean;
function GetFileStates: TwbFileStates; inline;
function GetCRC32: Cardinal;
function GetRecord(aIndex: Integer): IwbMainRecord; inline;
function GetRecordCount: Integer; inline;
function GetHeader: IwbMainRecord;
function GetLoadOrder: Integer; inline;
procedure ForceLoadOrder(aValue: Integer);
procedure SetLoadOrder(aValue: Integer);
function GetLoadOrderFileID: TwbFileID;
function GetFileFileID: TwbFileID;
function LoadOrderFormIDtoFileFormID(aFormID: TwbFormID): TwbFormID;
function FileFormIDtoLoadOrderFormID(aFormID: TwbFormID): TwbFormID;
function LoadOrderFileIDtoFileFileID(aFileID: TwbFileID): TwbFileID;
function FileFileIDtoLoadOrderFileID(aFileID: TwbFileID): TwbFileID;
procedure AddMasters(aMasters: TStrings);
procedure AddMasterIfMissing(const aMaster: string);
procedure SortMasters;
procedure CleanMasters;
procedure BuildReachable;
function GetIsESM: Boolean;
procedure SetIsESM(Value: Boolean);
function GetIsESL: Boolean;
procedure SetIsESL(Value: Boolean);
function GetIsLocalized: Boolean;
procedure SetIsLocalized(Value: Boolean);
function GetNextObjectID: Cardinal;
procedure SetNextObjectID(aObjectID: Cardinal);
function GetIsNotPlugin: Boolean;
function GetHasNoFormID: Boolean;
procedure SetHasNoFormID(Value: Boolean);
{---IwbFileInternal---}
procedure AddMainRecord(const aRecord: IwbMainRecord);
procedure RemoveMainRecord(const aRecord: IwbMainRecord);
procedure InjectMainRecord(const aRecord: IwbMainRecord);
procedure RemoveInjectedMainRecord(const aRecord: IwbMainRecord);
procedure ForceClosed;
procedure GetMasters(aMasters: TStrings); virtual;
procedure Scan; virtual;
procedure SortRecords;
procedure SortRecordsByEditorID;
procedure AddMaster(const aFileName: string; isTemporary: Boolean = False); overload;
procedure AddMaster(const aFile: IwbFile); overload;
constructor Create(const aFileName: string; aLoadOrder: Integer; aCompareTo: string; aOnlyHeader: Boolean; IsTemporary: Boolean = False);
constructor CreateNew(const aFileName: string; aLoadOrder: Integer; aIsEsl: Boolean);
public
destructor Destroy; override;
end;
TwbFileSource = class(TwbFile)
protected
procedure Scan; override;
constructor CreateNew(const aFileName: string; aLoadOrder: Integer);
procedure GetMasters(aMasters: TStrings); override;
end;
TwbDataContainerFlag = (
dcfDontCompare,
dcfDontMerge,
dcfDontSave,
dcfStorageInvalid
);
TwbDataContainerFlags = set of TwbDataContainerFlag;
IwbDataContainerInternal = interface(IwbDataContainer)
['{E13AE2AD-20CB-4429-86C2-0DEC3ECEE38B}']
procedure UpdateStorageFromElements;
end;
TwbDataContainer = class(TwbContainer, IwbDataContainer, IwbDataContainerInternal)
protected
dcBasePtr : Pointer;
dcEndPtr : Pointer;
dcDataBasePtr : Pointer;
dcDataEndPtr : Pointer;
dcDataStorage : TBytes;
dcFlags : TwbDataContainerFlags;
constructor Create(const aContainer : IwbContainer;
var aBasePtr : Pointer;
aEndPtr : Pointer;
const aPrevMainRecord : IwbMainRecord); virtual;
procedure InitDataPtr; virtual; abstract;
function GetDataPrefixSize: Integer; virtual;
function GetResolvedValueDef: IwbValueDef; override;
procedure InvalidateStorage; override;
procedure SetContainer(const aContainer: IwbContainer); override;
procedure SetModified(aValue: Boolean); override;
procedure RequestStorageChange(var aBasePtr, aEndPtr: Pointer; aNewSize: Cardinal); override;
procedure WriteToStreamInternal(aStream: TStream; aResetModified: Boolean); override;
procedure MergeStorageInternal(var aBasePtr: Pointer; aEndPtr: Pointer); override;
procedure InformStorage(var aBasePtr: Pointer; aEndPtr: Pointer); override;
function DoCheckSizeAfterWrite: Boolean; virtual;
procedure SetToDefaultInternal; override;
function IsFlags: Boolean; virtual;
function GetEditType: TwbEditType; override;
function GetEditInfo: string; override;
function GetConflictPriority: TwbConflictPriority; override;
{---IwbDataContainer---}
function GetDataBasePtr: Pointer;
function GetDataEndPtr: Pointer;
function GetDataSize: Integer; override;
procedure SetDataSize(aSize: Integer); override;
function GetDontCompare: Boolean;
function GetDontSave: Boolean;
function IsValidOffset(aBasePtr, aEndPtr: Pointer; anOffset: Integer): Boolean;
function IsLocalOffset(anOffset: Integer): Boolean;
{--- IwbDataContainerInternal ---}
procedure UpdateStorageFromElements; virtual;
end;
TwbRecordClass = class of TwbRecord;
TwbRecord = class(TwbDataContainer, IwbRecord, IwbHasSignature)
protected
recSkipped : Boolean;
protected
constructor Create(const aContainer : IwbContainer;
var aBasePtr : Pointer;
aEndPtr : Pointer;
const aPrevMainRecord : IwbMainRecord); overload; override;
function GetSignature: TwbSignature;
procedure ScanData; virtual; abstract;
procedure InformPrevMainRecord(const aPrevMainRecord : IwbMainRecord); virtual;
procedure SortBySortOrder; override;
public
class function CreateForPtr(var aPtr : Pointer;
aEndPtr : Pointer;
const aContainer : IwbContainer;
const aPrevMainRecord : IwbMainRecord)
: IwbRecord;
function GetName: string; override;
function GetSkipped: Boolean; override;
end;
PwbMainRecordStruct = ^TwbMainRecordStruct;
TwbMainRecordStruct = packed record
mrsSignature : TwbSignature;
mrsDataSize : Cardinal;
mrsFlags : TwbMainRecordStructFlags;
mrsFormID : TwbFormID;
mrsVCS1 : Cardinal;
mrsVersion : Word;
mrsVCS2 : Word;
end;
IwbMainRecordInternal = interface(IwbMainRecord)
['{405C85E0-2261-4078-B99C-199007D31544}']
procedure AddOverride(const aMainRecord: IwbMainRecord);
procedure RemoveOverride(const aMainRecord: IwbMainRecord);
procedure SetMaster(const aMaster: IwbMainRecord);
procedure YouAreTheMaster(const aOverrides, aReferencedBy: TDynMainRecords; aReferencedByCount: Integer); overload;
procedure YouAreTheMaster(const aOldMaster: IwbMainRecord; const aOverrides, aReferencedBy: TDynMainRecords; aReferencedByCount: Integer); overload;
procedure YouGotAMaster(const aMaster: IwbMainRecord);
procedure SetChildGroup(const aGroup: IwbGroupRecord);
procedure RemoveChildGroup(const aGroup: IwbGroupRecord);
procedure SetReferencesInjected(aValue: Boolean);
procedure ClearForRelease;
procedure SaveRefsToStream(aStream: TStream);
procedure LoadRefsFromStream(aStream: TStream);
procedure MakeHeaderWriteable;
function mrStruct: PwbMainRecordStruct;
end;
IwbMainRecordEntry = interface(IwbMainRecordInternal)
['{0C89F580-C95A-4A6C-85EA-BD5E411788A4}']
procedure RemoveEntry;
procedure RemoveEntryInternal;
procedure InsertEntryAfter(const aEntry: IwbMainRecordEntry);
procedure InsertEntryHead;
procedure InsertEntryTail;
function GetPrevEntry: IwbMainRecordEntry;
procedure SetPrevEntry(const aEntry: IwbMainRecordEntry);
function GetNextEntry: IwbMainRecordEntry;
procedure SetNextEntry(const aEntry: IwbMainRecordEntry);
function GetIsInList: Boolean;
property PrevEntry: IwbMainRecordEntry
read GetPrevEntry
write SetPrevEntry;
property NextEntry: IwbMainRecordEntry
read GetNextEntry
write SetNextEntry;
property IsInList: Boolean
read GetIsInList;
end;
TwbMainRecordState = (
mrsBuildingRef,
mrsReferencedByUnsorted,
mrsIsInjected,
mrsIsInjectedChecked,
mrsReferencesInjected,
mrsReferencesInjectedChecked,
mrsSearchedChildGroup,
mrsHasVWDMeshChecked,
mrsHasVWDMesh,
mrsHasPrecombinedMeshChecked,
mrsHasPrecombinedMesh,
mrsBaseRecordChecked,
mrsQuickInit,
mrsQuickInitDone,
mrsHasMeshChecked,
mrsHasMesh,
mrsNoUpdateRefs,
mrBasePtrAllocated
);
TwbMainRecordStates = set of TwbMainRecordState;
IwbContainedIn = interface
['{002F064A-81B8-40EB-AA09-E5F7AE061D9E}']
procedure ContainerChanged;
end;
TwbMainRecord = class(TwbRecord, IwbMainRecord, IwbMainRecordInternal, IwbMainRecordEntry, IwbContainedIn)
protected
mrDef : IwbRecordDef;
mrLoadOrderFormID : TwbFormID;
mrFixedFormID : TwbFormID;
mrMaster : Pointer{IwbMainRecord};
mrOverrides : TDynMainRecords;
mrOverridesSorted : Boolean;
mrEditorID : string;
mrFullName : string;
mrStates : TwbMainRecordStates;
mrBaseRecordID : TwbFormID;
mrPrecombinedCellID : Cardinal;
mrPrecombinedID : Cardinal;
mrConflictAll : TConflictAll;
mrConflictThis : TConflictThis;
mrDataStorage : TBytes;
mrGroup : IwbGroupRecord;
mrReferencedBy : TDynMainRecords;
mrReferencedByCount : Integer;
mrReferencedBySize : Integer;
mrReferences : TwbFormIDs;
mrTmpRefFormIDs : TwbFormIDs;
mrTmpRefFormIDHigh : Integer;
mreGeneration : Integer;
mrePrev : Pointer;
mreNext : Pointer;
function mrStruct: PwbMainRecordStruct; inline;
procedure ElementChanged(const aElement: IwbElement; aContainer: Pointer); override;
function RemoveElement(aPos: Integer; aMarkModified: Boolean = False): IwbElement; overload; override;
function ResolveElementName(aName: string; out aRemainingName: string; aCanCreate: Boolean = False): IwbElement; override;
function GetIsInjected: Boolean; override;
function GetReferencesInjected: Boolean; override;
function GetInjectionSourceFiles: TDynFiles; override;
function RemoveInjected(aCanRemove: Boolean): Boolean; override;
function GetIsNotReachable: Boolean; override;
function GetCountedRecordCount: Cardinal; override;
procedure InitDataPtr; override;
procedure DecompressIfNeeded;
procedure ScanData; override;
procedure WriteToStreamInternal(aStream: TStream; aResetModified: Boolean); override;
procedure MergeStorageInternal(var aBasePtr: Pointer; aEndPtr: Pointer); override;
procedure InformStorage(var aBasePtr: Pointer; aEndPtr: Pointer); override;
function CanContainFormIDs: Boolean; override;
function CompareExchangeFormID(aOldFormID: TwbFormID; aNewFormID: TwbFormID): Boolean; override;
function CanElementReset: Boolean; override;
procedure Remove; override;
procedure PrepareSave; override;
procedure MasterCountUpdated(aOld, aNew: Byte); override;
procedure MasterIndicesUpdated(const aOld, aNew: TwbFileIDs); override;
procedure FindUsedMasters(aMasters: PwbUsedMasters); override;
function GetReferenceFile: IwbFile; override;
procedure ReportRequiredMasters(aStrings: TStrings; aAsNew: Boolean; Recursive: Boolean = True; Initial: Boolean = false); override;
function LinksToParent: Boolean; override;
function Reached: Boolean; override;
function GetContainingMainRecord: IwbMainRecord; override;
procedure DoBuildRef(aRemove: Boolean);
procedure BuildRef; override;
procedure AddReferencedFromID(aFormID: TwbFormID); override;
procedure ResetConflict; override;
procedure ResetReachable; override;
procedure Init; override;
procedure Reset; override;
function GetPath: string; override;
function GetValue: string; override;
function GetSortKeyInternal(aExtended: Boolean): string; override;
function GetSortPriority: Integer; override;
function GetAdditionalElementCount: Integer; override;
function GetIsEditable: Boolean; override;
function GetEditValue: string; override;
procedure SetEditValue(const aValue: string); override;
function GetNativeValue: Variant; override;
procedure SetNativeValue(const aValue: Variant); override;
function IsElementRemoveable(const aElement: IwbElement): Boolean; override;
procedure SetContainer(const aContainer: IwbContainer); override;
function FindReferencedBy(const aMainRecord: IwbMainRecord; var Index: Integer): Boolean;
function CanAssignInternal(aIndex: Integer; const aElement: IwbElement; aCheckDontShow: Boolean): Boolean; override;
function AssignInternal(aIndex: Integer; const aElement: IwbElement; aOnlySK: Boolean): IwbElement; override;
function AddIfMissingInternal(const aElement: IwbElement; aAsNew, aDeepCopy : Boolean; const aPrefixRemove, aPrefix, aSuffix: string): IwbElement; override;
procedure CollapseStorage;
function GetAddList: TDynStrings; override;
function Add(const aName: string; aSilent: Boolean): IwbElement; override;
function CheckChildOfCell: Boolean;
procedure UpdateCellChildGroup;
procedure UpdateInteriorCellGroup;
procedure MarkModifiedRecursive; override;
{---IwbMainRecord---}
function GetDef: IwbNamedDef; override;
function GetElementType: TwbElementType; override;
function GetFormID: TwbFormID; inline;
function GetFixedFormID: TwbFormID; inline;
function DoGetFixedFormID: TwbFormID;
function GetLoadOrderFormID: TwbFormID; inline;
function DoGetLoadOrderFormID: TwbFormID;
procedure SetLoadOrderFormID(aFormID: TwbFormID);
function GetEditorID: string;