-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpraUtil.pas
More file actions
5281 lines (4582 loc) · 166 KB
/
praUtil.pas
File metadata and controls
5281 lines (4582 loc) · 166 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
{
Some useful functions.
Version is now in PRA_UTIL_VERSION
}
unit PraUtil;
const
// the version constant
PRA_UTIL_VERSION = 17.0;
// file flags
FILE_FLAG_ESM = 1;
FILE_FLAG_LOCALIZED = 128;
FILE_FLAG_ESL = 512;
FILE_FLAG_IGNORED = 4096;
// xedit version constants
XEDIT_VERSION_404 = $04000400;
XEDIT_VERSION_415h = $04010508;
XEDIT_VERSION_415j = $0401050A;
// JSON constants
JSON_TYPE_NONE = jdtNone; // none
JSON_TYPE_STRING = jdtString; // string
JSON_TYPE_INT = jdtInt; // int
JSON_TYPE_LONG = jdtLong; // long
JSON_TYPE_ULONG = jdtULong; // ulong
JSON_TYPE_FLOAT = jdtFloat; // float
JSON_TYPE_DATETIME = jdtDateTime; // datetime
JSON_TYPE_BOOL = jdtBool; // bool
JSON_TYPE_ARRAY = jdtArray; // array
JSON_TYPE_OBJECT = jdtObject; // object
// misc constants
MAX_EDID_LENGTH = 87; // 99-12, because 12 is the length of DUPLICATE000. And no, I don't care that the NG supposedly fixed this.
STRING_LINE_BREAK = #13#10;
// xEdit stuff
{
Builds an xEdit version cardinal out of individual parts.
"Build" must be a lowercase letter, a-z, or an empty string if no build letter is present. Or a number 0-255, I guess.
}
function xEditVersionToCardinal(major, minor, release: Cardinal; Build: string): Cardinal;
var
buildOrd, buildLength: integer;
buildChar: char;
begin
Result :=
((major and $000000FF) shl 24) or
((minor and $000000FF) shl 16) or
((release and $000000FF) shl 8);
buildLength := Length(Build);
if(buildLength >= 1) then begin
if(buildLength = 1) then begin
// try parsing it as a letter, a-z
buildChar := Copy(Build, 1, 1);
buildOrd := Ord(buildChar);
// ord(a)=97
// ord(z)=122
if(buildOrd >= 97) and (buildOrd <= 122) then begin
Result := Result + buildOrd - 96;
exit;
end;
end;
// if still alive, try to parse Build as a number
buildOrd := IntToStr(Build);
Result := Result + buildOrd and $FF;
end;
end;
{
Decodes an xEdit version cardinal, like wbVersionNumber, into a string.
}
function xEditVersionCardinalToString(versionCardinal: cardinal): string;
var
major, minor, release, build, buildOrd: integer;
buildString: string;
begin
major := (versionCardinal and $FF000000) shr 24;
minor := (versionCardinal and $FF0000) shr 16;
release := (versionCardinal and $FF00) shr 8;
build := (versionCardinal and $FF);
Result := IntToStr(major) + '.' + IntToStr(minor) + '.' + IntToStr(release);
if(build > 0) then begin
if(build <= 25) then begin
buildOrd := build - 1 + 97;
Result := Result + Chr(buildOrd);
end else begin
Result := Result + '.' + IntToStr(build);
end;
end;
end;
// generic stuff
{
Check if two file variables are referring to the same file
}
function FilesEqual(file1, file2: IwbFile): boolean;
begin
// Should be faster than comparing the filenames
Result := (GetLoadOrder(file1) = GetLoadOrder(file2));
end;
function isSameFile(file1, file2: IwbFile): boolean;
begin
Result := FilesEqual(file1, file2);
end;
{
Returns if e is either deleted, or flagged as initially disabled and opposite to player
}
function isConsideredDeleted(e: IInterface): boolean;
var
xesp: IInterface;
flags: integer;
begin
Result := true;
if(GetIsDeleted(e)) then exit;
if(GetIsInitiallyDisabled(e)) then begin
xesp := pathLinksTo(e, 'XESP\Reference');
if(FormID(xesp) = 20) then begin
flags := GetElementNativeValues(e, 'XESP\Flags');
// yes player
if((flags or 1) <> 0) then begin
//AddMessage('the flags are '+IntToStr(flags));
exit;
end;
end;
end;
Result := false;
end;
{
Returns if e is scrappable, either directly or via a scrap FLST, recursively
}
function isBaseObjectScrappable(e: IInterface): boolean;
var
i: integer;
curRef, product: IInterface;
begin
Result := false;
for i:=0 to ReferencedByCount(e)-1 do begin
curRef := ReferencedByIndex(e, i);
if(Signature(curRef) = 'COBJ') then begin
product := pathLinksTo(curRef, 'CNAM');
if(isSameForm(product, e)) then begin
Result := true;
exit;
end;
end else if(Signature(curRef) = 'FLST') then begin
if(isBaseObjectScrappable(curRef)) then begin
Result := true;
exit;
end;
end;
end;
end;
{
Check if two IInterfaces are equivalent, by recursively comparing the edit values of their contents
}
function ElementsEquivalent(e1, e2: IInterface): boolean;
var
i, count1, count2: Integer;
child1, child2: IInterface;
key1, key2, val1, val2: string;
tmpResult: boolean;
begin
Result := false;
// trivial crap
if (not assigned(e1)) and (not assigned(e2)) then begin
Result := true;
exit;
end;
if(Equals(e1, e2)) then begin
Result := true;
exit;
end;
count1 := ElementCount(e1);
count2 := ElementCount(e2);
if(count1 <> count2) then exit;
if(count1 = 0) then begin
val1 := GetEditValue(e1);
val2 := GetEditValue(e2);
Result := (val1 = val2);
exit;
end;
for i := 0 to ElementCount(e1)-1 do begin
child1 := ElementByIndex(e1, i);
child2 := ElementByIndex(e2, i);
key1 := DisplayName(child1);
key2 := DisplayName(child2);
if(key1 <> key2) then exit;
if (key1 <> '') then begin
tmpResult := ElementsEquivalent(child1, child2);
if(not tmpResult) then exit;
end;
end;
Result := true;
end;
{
Gets a file object by filename
}
function FindFile (name: String): IwbFile;
var
i: integer;
curFile: IwbFile;
begin
name := LowerCase(name);
Result := nil;
for i := 0 to FileCount-1 do
begin
curFile := FileByIndex(i);
if(LowerCase(GetFileName(curFile)) = name) then begin
Result := curFile;
exit;
end;
end;
end;
{
Returns whenever a file has the ESL header
}
function isFileLight(f: IInterface): boolean;
begin
// Turns out, xEdit had this thing all along. Good thing we have such a good documentation for it /s
Result := GetIsESL(f);
end;
{
Gets and trims element edit values, for SimSettlements city plans
}
function geevt(e: IInterface; name: string): string;
begin
Result := trim(GetElementEditValues(e, name));
end;
{
Like getElementEditValues, but with a default value when the path is missing
}
function getElementEditValuesDefault(e: IInterface; path: string; default: string): string;
begin
Result := getElementEditValues(e, path);
if(Result = '') then begin
Result := default;
end;
end;
{
Like getElementNativeValues, but with a default value when the path is missing
}
function getElementNativeValuesDefault(e: IInterface; path: string; default: variant): variant;
var
testVal: string;
begin
testVal := getElementEditValues(e, path);
if(testVal <> '') then begin
Result := getElementNativeValues(e, path);
end else begin
Result := default;
end;
end;
{
Gets an object by editor ID from any currently loaded file and any group.
}
function FindObjectByEdid(edid: String): IInterface;
var
iFiles: integer;
curFile: IInterface;
curRecord: IInterface;
begin
Result := nil;
if(edid = '') then exit;
curRecord := nil;
for iFiles := 0 to FileCount-1 do begin
curFile := FileByIndex(iFiles);
if(assigned(curFile)) then begin
curRecord := FindObjectInFileByEdid(curFile, edid);
if (assigned(curRecord)) then begin
Result := curRecord;
exit;
end;
end;
end;
end;
{
Gets an object by editor ID from the given file and any group.
}
function FindObjectInFileByEdid(theFile: IInterface; edid: string): IInterface;
var
iSigs: integer;
curGroup: IInterface;
curRecord: IInterface;
begin
Result := nil;
if(edid = '') then exit;
curRecord := nil;
for iSigs:=0 to ElementCount(theFile)-1 do begin
curGroup := ElementByIndex(theFile, iSigs);
if (Signature(curGroup) = 'GRUP') then begin
curRecord := MainRecordByEditorID(curGroup, edid);
if(assigned(curRecord)) then begin
Result := curRecord;
exit;
end;
end;
end;
end;
{
Gets an object by editor ID from any currently loaded file and any group.
This is not a performant function.
}
function FindObjectByEdidAndSignature(edid, sig: String): IInterface;
var
iFiles: integer;
curFile: IInterface;
curRecord: IInterface;
begin
Result := nil;
if(edid = '') then exit;
curRecord := nil;
for iFiles := 0 to FileCount-1 do begin
curFile := FileByIndex(iFiles);
if(assigned(curFile)) then begin
curRecord := FindObjectInFileByEdidAndSignature(curFile, edid, sig);
if (assigned(curRecord)) then begin
Result := curRecord;
exit;
end;
end;
end;
end;
{
Gets an object by editor ID and signature from the given file and any group.
}
function FindObjectInFileByEdidAndSignature(theFile: IInterface; edid, sig: string): IInterface;
var
curGroup: IInterface;
begin
Result := nil;
if (edid = '') or (sig = '') then exit;
curGroup := GroupBySignature(theFile, sig);
if(assigned(curGroup)) then begin
Result := MainRecordByEditorID(curGroup, edid);
end;
end;
{
Tries to find a reference with a given editor id. Iterates all the cells. Probably even worse than FindObjectByEdid
}
function FindReferenceByEdid(edid: string): IInterface;
var
iFiles: integer;
curFile: IInterface;
curRecord: IInterface;
begin
Result := nil;
if(edid = '') then exit;
curRecord := nil;
for iFiles := 0 to FileCount-1 do begin
curFile := FileByIndex(iFiles);
if(assigned(curFile)) then begin
curRecord := FindReferenceInFileByEdid(curFile, edid);
if (assigned(curRecord)) then begin
Result := curRecord;
exit;
end;
end;
end;
end;
function FindReferenceInBlockGrpByEdid(blockGrp: IInterface; edid: string; checkPersistentCell: boolean): IInterface;
var
iWorlds, blockidx, subblockidx, cellidx: integer;
worlds, interiors, wrldgrup, block, subblock, cell, wrld: IInterface;
foo: IInterface;
curSig: string;
begin
// traverse Blocks
for blockidx := 0 to ElementCount(blockGrp)-1 do begin
block := ElementByIndex(blockGrp, blockidx);
// traverse SubBlocks
for subblockidx := 0 to ElementCount(block)-1 do begin
subblock := ElementByIndex(block, subblockidx);
if(Signature(subblock) <> 'GRUP') then begin
continue;
end;
// traverse Cells
for cellidx := 0 to ElementCount(subblock)-1 do begin
cell := ElementByIndex(subblock, cellidx);
curSig := Signature(cell);
if (curSig = 'CELL') then begin
Result := findNamedReference(cell, edid);
if (assigned(Result)) then exit;
end else if(isReferenceSignature(curSig)) then begin
// this happens for persistent cells...
if(EditorID(cell) = edid) then begin
Result := cell;
exit;
end;
end;
end;
end;
{
}
end;
end;
function FindReferenceInFileByEdid(theFile: IInterface; edid: string): IInterface;
var
iWorlds, blockidx, subblockidx, cellidx: integer;
worlds, interiors, wrldgrup, block, subblock, cell, wrld: IInterface;
begin
// interiors
interiors := GroupBySignature(theFile, 'CELL');
Result := FindReferenceInBlockGrpByEdid(interiors, edid, false);
if(assigned(Result)) then exit;
// exteriors
worlds := GroupBySignature(theFile, 'WRLD');
for iWorlds:=0 to ElementCount(worlds)-1 do begin
wrld := ElementByIndex(worlds, iWorlds);
// need to find this world's persistent cell
wrldgrup := ChildGroup(wrld);
Result := FindReferenceInBlockGrpByEdid(wrldgrup, edid, true);
if(assigned(Result)) then exit;
end;
end;
function GetFormByEdid(edid: string): IInterface;
begin
Result := FindObjectByEdid(edid);
end;
function findInteriorCellByEdid(edid: string): IInterface;
var
iFiles: integer;
curFile: IInterface;
curRecord: IInterface;
begin
Result := nil;
if(edid = '') then exit;
curRecord := nil;
for iFiles := 0 to FileCount-1 do begin
curFile := FileByIndex(iFiles);
if(assigned(curFile)) then begin
curRecord := findInteriorCellInFileByEdid(curFile, edid);
if (assigned(curRecord)) then begin
Result := curRecord;
exit;
end;
end;
end;
end;
{
Iterates through all interior cells in a file, and returns the one with the matching edid
}
function findInteriorCellInFileByEdid(sourceFile: IInterface; edid: String): IInterface;
var
cellGroup: IInterface;
block, subblock, cell: IInterface;
i, j, k: integer;
begin
Result := nil;
cellGroup := GroupBySignature(sourceFile, 'CELL');
for i:=0 to ElementCount(cellGroup)-1 do begin
block := ElementByIndex(cellGroup, i);
for j:=0 to ElementCount(block)-1 do begin
subblock := ElementByIndex(block, j);
for k:=0 to ElementCount(subblock)-1 do begin
cell := ElementByIndex(subblock, k);
if(Signature(cell) = 'CELL') then begin
if(EditorID(cell) = edid) then begin
Result := cell;
exit;
end;
end;
end;
end;
end;
end;
{
Searches in persistent and temporary references for one with a specific editorID
}
function findNamedReference(cell: IInterface; refEdid: string): IInterface;
var
i: integer;
cur, test: IInterface;
begin
// persistent
test := FindChildGroup(ChildGroup(cell), 8, cell);
for i:=0 to ElementCount(test)-1 do begin
cur := ElementByIndex(test, i);
if(EditorID(cur) = refEdid) then begin
Result: = cur;
exit;
end;
end;
// temporary
test := FindChildGroup(ChildGroup(cell), 9, cell);
for i:=0 to ElementCount(test)-1 do begin
cur := ElementByIndex(test, i);
if(EditorID(cur) = refEdid) then begin
Result: = cur;
exit;
end;
end;
end;
{
Searches in subject using regexString. Returns the matched group of the given number.
Matched groups begin at 1, with 0 being the entire matched string.
Returns empty string on failure.
Example:
regexExtract('123 foobar 235 what', '([0-9]+) what', 1) -> '235'
}
function regexExtract(subject, regexString: string; returnMatchNr: integer): string;
var
regex: TPerlRegEx;
begin
regex := TPerlRegEx.Create();
Result := '';
try
regex.RegEx := regexString;
regex.Subject := subject;
if(regex.Match()) then begin
// misnomer, is actually the highest valid index of regex.Groups
if(regex.GroupCount >= returnMatchNr) then begin
Result := regex.Groups[returnMatchNr];
end;
end;
finally
RegEx.Free;
end;
end;
function regexReplace(subject, regexString, replacement: string): string;
var
regex: TPerlRegEx;
begin
Result := '';
regex := TPerlRegEx.Create();
try
regex.RegEx := regexString;
regex.Subject := subject;
regex.Replacement := replacement;
regex.ReplaceAll();
Result := regex.Subject;
finally
RegEx.Free;
end;
end;
{
Tries to extract the FormID from a string like 'REObjectJS01Note "Note" [BOOK:00031901]'.
If the string is just a plain hex number already, should parse that as well.
}
function findFormIdInString(someStr: string): cardinal;
var
regex: TPerlRegEx;
maybeFormId : cardinal;
maybeMatch: string;
begin
maybeFormId := 0;
Result := 0;
if (someStr = '') then exit;
maybeMatch := regexExtract(someStr, '^([0-9a-fA-F]+)$', 1);
if (maybeMatch <> '') then begin
maybeFormId := StrToInt64('$' + maybeMatch);
if(maybeFormId > 0) then begin
Result := maybeFormId;
exit;
end;
end;
maybeMatch := regexExtract(someStr, '\[....:([0-9a-fA-F]{8})\]', 1);
if (maybeMatch <> '') then begin
maybeFormId := StrToInt64('$' + maybeMatch);
if(maybeFormId > 0) then begin
Result := maybeFormId;
exit;
end;
end;
end;
{
Tries to find a form by strings like:
- Fallout4.esm:00031901
- REObjectJS01Note "Note" [BOOK:00031901]
- 00031901
- REObjectJS01Note
In the first case, it only cares about the FormID, not the EditorID, if any.
}
function findFormByString(someStr: string): IInterface;
var
maybeFormId: cardinal;
begin
Result := AbsStrToForm(someStr);
if(assigned(Result)) then begin
exit;
end;
maybeFormId := findFormIdInString(someStr);
if(maybeFormId > 0) then begin
Result := getFormByLoadOrderFormID(maybeFormId);
// return nil here if it failed, too
exit;
end;
Result := FindObjectByEdid(someStr);
end;
{
Like above, but also tries to find a reference
}
function findFormOrRefByString(someStr: string): IInterface;
var
maybeFormId: cardinal;
begin
Result := AbsStrToForm(someStr);
if(assigned(Result)) then begin
exit;
end;
maybeFormId := findFormIdInString(someStr);
if(maybeFormId > 0) then begin
Result := getFormByLoadOrderFormID(maybeFormId);
// return nil here if it failed, too
exit;
end;
Result := FindObjectByEdid(someStr);
if(not assigned(Result)) then begin
Result := FindReferenceByEdid(someStr);
end;
end;
{
Go upwards from a child to a main record
}
function getParentRecord(child: IInterface): IInterface;
var
t1: TwbElementType;
t2: TwbDefType;
begin
Result := nil;
t1 := ElementType(child);
if(t1 = etMainRecord) then begin
Result := child;
exit;
end;
Result := getParentRecord(GetContainer(child));
end;
function getFormByFilenameAndFormID(filename: string; id: cardinal): IInterface;
var
fileObj: IInterface;
{localFormId: cardinal;}
begin
Result := nil;
fileObj := FindFile(filename);
if(not assigned(fileObj)) then begin
exit;
end;
Result := getFormByFileAndFormID(FindFile(filename), id);
{
localFormId := FileToLoadOrderFormID(fileObj, id);
Result := RecordByFormID(fileObj, localFormId, true);
}
end;
procedure loadMasterList(list: TStringList; theFile: IInterface);
var
curFile: IInterface;
curFileName: string;
i: integer;
begin
for i:=0 to MasterCount(theFile)-1 do begin
curFile := MasterByIndex(theFile, i);
curFileName := GetFileName(curFile);
if(list.indexOf(curFileName) < 0) then begin
list.addObject(curFileName, curFile);
loadMasterList(list, curFile);
end;
end;
end;
function getMasterList(theFile: IInterface): TStringList;
begin
Result := TStringList.create();
loadMasterList(Result, theFile);
end;
function GetFirstNonOverrideElement(theFile: IwbFile): IInterface;
var
curGroup, curRecord: IInterface;
iSigs, i: integer;
begin
for iSigs:=0 to ElementCount(theFile)-1 do begin
curGroup := ElementByIndex(theFile, iSigs);
if (Signature(curGroup) = 'GRUP') then begin
for i:=0 to ElementCount(curGroup)-1 do begin
curRecord := ElementByIndex(curGroup, i);
if(IsMaster(curRecord)) then begin
Result := curRecord;
exit;
end;
end;
end;
end;
end;
{
Returns a formID with the value zero, but all the load order prefixes
This is because xEdit has absolutely no way whatsoever to actually get the true load order of a file, even less so for ESLs.
GetLoadOrder is a misnomer, it actually returns the index in the current list.
}
function GetZeroFormID(theFile: IwbFile): cardinal;
var
i: integer;
currentNormal, currentLight, fuckyou1: cardinal;
curFile: IwbFile;
fileName, curFilename: string;
begin
fileName := LowerCase(GetFileName(theFile));
if (fileName = 'fallout4.exe') or (fileName = 'fallout4.esm') then begin
Result := 0;
exit;
end;
currentNormal := $0;
currentLight := $0;
Result := $0;
for i := 0 to FileCount-1 do begin
curFile := FileByIndex(i);
curFilename := GetFileName(curFile);
if(isFileLight(curFile)) then begin
if(IsSameFile(theFile, curFile)) then begin
Result := (currentLight shl 12) or $FE000000;
exit;
end;
currentLight := currentLight + 1;
end else begin
if(IsSameFile(theFile, curFile)) then begin
Result := currentNormal shl 24;
exit;
end;
if not (LowerCase(curFilename) = 'fallout4.exe') then begin
// ugh... fallout4.exe is annoying
currentNormal := currentNormal + 1;
end;
end;
end;
end;
{
Returns a new FormID for the given file, load-order adjusted
}
function GetNewLoadOrderFormID(theFile: IwbFile): cardinal;
begin
Result := FileFormIDtoLoadOrderFormID(theFile, GetNewFormID(theFile));
end;
{
Returns the FormID of e with the LO prefix replaced with the corresponding master index in theFile.
That is, if the record 0x00001234 is from the second master, this will return 0x01001234
}
function getRelativeFormId(theFile: IwbFile; e: IInterface): cardinal;
var
numMasters, i: integer;
curMaster, mainRec, mainFile: IInterface;
begin
Result := 0;
mainRec := MasterOrSelf(e);
mainFile := GetFile(mainRec);
numMasters := MasterCount(theFile);
if(isSameFile(mainFile, theFile)) then begin
// my own file
Result := getLocalFormId(theFile, FormID(e)) or (numMasters shl 24);
exit;
end;
for i:=0 to numMasters-1 do begin
curMaster := MasterByIndex(theFile, i);
if(isSameFile(mainFile, curMaster)) then begin
// this file
Result := getLocalFormId(theFile, FormID(e)) or (i shl 24);
exit;
end;
end;
end;
{
DEPRECATED: this is actually just the same as RecordByFormID.
Returns an element by a formId, which is relative to the given file's master list.
That is, if theFile has at least 2 masters and the given id is 0x01001234, it will
try to find 0x00001234 in the second master.
If applicable, will return the corresponding override from theFile.
}
function elementByRelativeFormId(theFile: IwbFile; id: cardinal): IInterface;
var
numMasters, prefix, baseId: integer;
targetMaster, formMaster: IInterface;
begin
Result := RecordByFormID(theFile, id, true);
end;
{
Strips the LO prefix from a FormID
}
function getLocalFormId(theFile: IwbFile; id: cardinal): cardinal;
begin
if(isFileLight(theFile)) then begin
Result := $00000FFF and id;
end else begin;
Result := $00FFFFFF and id;
end;
end;
{
Strips the actual ID part from a FormID, leaving only the LO part
}
function getLoadOrderPrefix(theFile: IwbFile; id: cardinal): cardinal;
begin
if (isFileLight(theFile)) then begin
Result := $FFFFF000 and id;
end else begin
Result := $FF000000 and id;
end;
end;
function getElementLocalFormId(e: IInterface): cardinal;
begin
Result := getLocalFormId(GetFile(e), FormID(e));
end;
{
An actually functional version of FileFormIDtoLoadOrderFormID.
}
function FileToLoadOrderFormID(theFile: IwbFile; id: cardinal): cardinal;
var
prefix: cardinal;
begin
prefix := GetZeroFormID(theFile);
Result := prefix or id;
end;
{
Like FileByLoadOrder, loads the file by actual load order ID, not by the number in the list (aka the 0xFF000000 part)
}
function FileByRealLoadOrder(loadOrder: cardinal): IInterface;
var
i: integer;
id, mainLO, test: cardinal;
curFile: IwbFile;
curHeader: IInterface;
begin
// this sucks... but I have no better idea
for i := 0 to FileCount-1 do
begin
curFile := FileByIndex(i);
id := GetZeroFormID(curFile);
mainLO := ($FF000000 and id) shr 24;
if(mainLO = $FE) then begin
continue;
end;
if(mainLO = loadOrder) then begin
Result := curFile;
exit;
end;
end;
end;
{
Like FileByRealLoadOrder, but for light file load order (aka the 0x00FFF000 part)
}
function FileByLightLoadOrder(lightLoadOrder: cardinal): IInterface;
var
i: integer;
id, mainLO, eslLO: cardinal;
curFile: IwbFile;
begin
// this sucks... but I have no better idea
for i := 0 to FileCount-1 do
begin
curFile := FileByIndex(i);
id := GetZeroFormID(curFile);
mainLO := ($FF000000 and id) shr 24;
if(mainLO <> $FE) then begin
continue;
end;
eslLO := ($FFF000 and id) shr 12;
if(lightLoadOrder = eslLO) then begin
Result := curFile;
exit;
end;
end;
end;
function getFormByLoadOrderFormID(id: cardinal): IInterface;
var
lightLOIndex, loadOrderIndexInt, localFormId, fixedId, anotherFormId: cardinal;
fileIndex: integer;
theFile : IInterface;
isLight : boolean;
watFoo: string;
begin
Result := nil;
loadOrderIndexInt := ($FF000000 and id) shr 24;