-
Notifications
You must be signed in to change notification settings - Fork 2
/
dubhFunctions.pas
926 lines (834 loc) · 28.2 KB
/
dubhFunctions.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
unit dubhFunctions;
uses mteFunctions;
// --------------------------------------------------------------------
// AddMessage
// --------------------------------------------------------------------
procedure Log(const s: String);
begin
AddMessage(s);
end;
// --------------------------------------------------------------------
// Returns x number of characters from left, or returns whole string
// if x number of characters exceed length of string
// --------------------------------------------------------------------
function LeftStr(s: String; i: Integer): String;
begin
if i > Length(s) then
Result := s
else
Result := Copy(s, 0, i);
end;
// --------------------------------------------------------------------
// Returns x number of characters from right, or returns whole string
// if x number of characters exceed length of string
// --------------------------------------------------------------------
function RightStr(s: String; i: Integer): String;
begin
if i > Length(s) then
Result := s
else
Result := Copy(s, Length(s) - (i - 1), i);
end;
// --------------------------------------------------------------------
// Returns cell reference from REFR/ACHR
// --------------------------------------------------------------------
function GetCell(e: IInterface): IInterface;
var
cell: IInterface;
begin
cell := ElementByName(e, 'Cell');
if not Assigned(cell) then
Result := nil
else
Result := LinksTo(cell);
end;
// --------------------------------------------------------------------
// Return whether a value is null
// --------------------------------------------------------------------
function IsNull(x: Variant): Boolean;
var
sDefType: String;
begin
if not CanContainFormIDs(x) then begin
sDefType := DefTypeString(x);
if sDefType = 'dtInteger' then
Result := (GetNativeValue(x) = 0)
else if sDefType = 'dtFloat' then
Result := (GetNativeValue(x) = 0.0);
exit;
end;
if CanContainFormIDs(x) then
Result := HasString('00000000', GetEditValue(x), False);
end;
// --------------------------------------------------------------------
// Return a hexadecimal form id with signature
// --------------------------------------------------------------------
function ShortFormID(x: IInterface): String;
begin
Result := '[' + Signature(x) + ':' + HexFormID(x) + ']';
end;
// --------------------------------------------------------------------
// Return whether a number is divisible by another into a whole number
// --------------------------------------------------------------------
function DivisibleBy(x, y: Real): Boolean;
begin
Result := (x mod y = 0);
end;
// --------------------------------------------------------------------
// Recursively add value of named field to list
// --------------------------------------------------------------------
procedure RecursiveAddToList(e: IInterface; elementName: String; results: TStringList);
var
i, c: Integer;
kFile: IwbFile;
kMainRecord, kChild: IInterface;
sFile: String;
begin
kMainRecord := ContainingMainRecord(e);
c := ElementCount(e);
if c > 0 then begin
for i := 0 to Pred(c) do begin
kChild := ElementByIndex(e, i);
if ElementCount(kChild) > 0 then
RecursiveAddToList(kChild, elementName, results)
else
if HasString(elementName, Name(kChild), False) then
results.Add(SmallNameEx(kMainRecord) + #9 + GetEditValue(kChild));
end;
end;
end;
// --------------------------------------------------------------------
// Recursively add key-value pairs to list
// --------------------------------------------------------------------
procedure RecursiveAddPairToList(e: IInterface; keyName, valueName: String; results: TStringList);
var
i, c: Integer;
kFile: IwbFile;
kMainRecord, kChild: IInterface;
sFile, sTempKey: String;
begin
results.NameValueSeparator := '=';
kMainRecord := ContainingMainRecord(e);
sTempKey := '';
c := ElementCount(e);
if c > 0 then begin
for i := 0 to Pred(c) do begin
kChild := ElementByIndex(e, i);
if ElementCount(kChild) > 0 then
RecursiveAddPairToList(kChild, keyName, valueName, results)
else
if HasString(keyName, Name(kChild), False) then
sTempKey := GetEditValue(kChild);
if HasString(valueName, Name(kChild), False) then begin
results.CommaText := '"' + sTempKey + '"=' + GetEditValue(kChild);
sTempKey := '';
end;
end;
end;
end;
// --------------------------------------------------------------------
// Return comma-separated list of hexadecimal form ids
// --------------------------------------------------------------------
function FlagsToHex(e: IInterface): String;
var
i: Integer;
ls: TStringList;
begin
ls := TStringList.Create;
ls.Add(IntToHex(GetNativeValue(e), 8));
for i := 0 to ElementCount(e) - 1 do
ls.Add(IntToHex(GetNativeValue(ElementByIndex(e, i)), 8));
Result := ls.CommaText;
end;
// --------------------------------------------------------------------
// Return comma-separated list of element names
// --------------------------------------------------------------------
function FlagsToNames(e: IInterface): String;
var
i: Integer;
ls: TStringList;
begin
if not Assigned(e) then
Result := '';
ls := TStringList.Create;
for i := 0 to ElementCount(e) - 1 do
ls.Add(Name(ElementByIndex(e, i)));
Result := ls.CommaText;
end;
// --------------------------------------------------------------------
// Return path without labels
// --------------------------------------------------------------------
function BasePath(x: IInterface): String;
var
sPath: String;
begin
//sPath := TrimChar(#32, RegExReplace('\s[-]\s[a-zA-Z0-9 ]+', '', Trim(Path(x))));
sPath := Path(x);
sPath := Trim(sPath);
sPath := RegExReplace('\h+[-]\h+[a-zA-Z0-9 ]+', '', sPath);
sPath := Trim(sPath);
sPath := RegExReplace('^[a-zA-Z0-9]{4}', '', sPath);
sPath := Trim(sPath);
sPath := RegExReplace('^[\\]', '', sPath);
sPath := Trim(sPath);
sPath := RegExReplace('\h*[\\]\h+', '\\', sPath);
sPath := Trim(sPath);
sPath := RegExReplace('\h+[-]$', '', sPath);
sPath := Trim(sPath);
Result := sPath;
//Result := TrimChar(#32, RegExReplace('\s[-]\s\w+\s', Trim(Path(x)), ''));
//Result := TrimChar(#32, RegExReplace('\b[a-zA-Z0-9]+\b\K\h[-]$|\b[a-zA-Z0-9]+\b\K\h[-](.*)$|\b[a-zA-Z0-9]+\b\K\h[-]\h\b[a-zA-Z0-9]+\b$', TrimRight(Path(x)), '#'));
end;
// --------------------------------------------------------------------
// A shorter way to replace strings without RegEx
// --------------------------------------------------------------------
function StrReplace(this, that, subj: String): String;
begin
Result := StringReplace(subj, this, that, [rfReplaceAll, rfIgnoreCase]);
end;
function EscapeSlashes(s: String): String;
begin
Result := StrReplace('\', '\\', s);
end;
// --------------------------------------------------------------------
// Replace a substring with a RegEx pattern and return the new string
// --------------------------------------------------------------------
function RegExReplace(const ptrn, repl, subj: String): String;
var
re: TPerlRegEx;
output: String;
begin
re := TPerlRegEx.Create;
try
re.RegEx := ptrn;
re.Options := [];
re.Subject := subj;
re.Replacement := repl;
re.ReplaceAll;
output := re.Subject;
finally
re.Free;
Result := output;
end;
end;
// --------------------------------------------------------------------
// Return a substring with a RegEx pattern
// --------------------------------------------------------------------
function RegExMatch(ptrn, subj: String): String;
var
re: TPerlRegEx;
output: String;
begin
output := nil;
re := TPerlRegEx.Create;
try
re.RegEx := ptrn;
re.Options := [];
re.Subject := subj;
if re.Match then
output := re.MatchedText;
finally
re.Free;
Result := output;
end;
end;
function RegExMatchAll(ptrn, subj: String): TStringList;
var
re: TPerlRegEx;
output: TStringList;
i: Integer;
begin
output := TStringList.Create;
re := TPerlRegEx.Create;
try
re.RegEx := ptrn;
re.Options := [];
re.Subject := subj;
if re.Match then begin
output.Add(re.MatchedText);
repeat
output.Add(re.MatchedText);
until not re.MatchAgain;
end;
finally
re.Free;
Result := output;
end;
end;
// --------------------------------------------------------------------
// Return whether a string matches a RegEx pattern
// --------------------------------------------------------------------
function RegExMatches(ptrn, subj: String): Boolean;
var
re: TPerlRegEx;
output: String;
begin
output := False;
re := TPerlRegEx.Create;
try
re.RegEx := ptrn;
re.Options := [];
re.Subject := subj;
if re.Match then
output := re.FoundMatch;
finally
re.Free;
Result := output;
end;
end;
function UnCamelCase(s: String): String;
var
output: String;
begin
output := RegExReplace('(\B[A-Z])', ' $1', s);
Result := RegExReplace('(\B[0-9]{2})', ' $1', output);
end;
// --------------------------------------------------------------------
// Apply Markdown formatting to TStringList
// --------------------------------------------------------------------
function Markdown(const ls: TStringList; const d: Char; const pre: Boolean): TStringList;
var
mds, mdb, mde, mdd: String;
i: Integer;
lsmd: TStringList;
begin
if pre then begin
mdb := '<pre>';
mde := '</pre>';
mdd := '</pre> | <pre>';
end;
if not pre then begin
mdb := '';
mde := '';
mdd := ' | ';
end;
lsmd := TStringList.Create;
for i := 0 to ls.Count - 1 do begin
mds := mdb + StringReplace(ls[i], d, mdd, [rfReplaceAll, rfIgnoreCase]) + mde;
lsmd.Add(mds);
end;
Result := lsmd;
end;
// --------------------------------------------------------------------
// GetEditValue
// --------------------------------------------------------------------
function gev(const s: String): String;
begin
Result := GetEditValue(s);
end;
// --------------------------------------------------------------------
// SetEditValue
// --------------------------------------------------------------------
procedure sev(const x: IInterface; const s: String);
begin
SetEditValue(x, s);
end;
// --------------------------------------------------------------------
// GetNativeValue
// --------------------------------------------------------------------
function gnv(const x: IInterface): Variant;
begin
Result := GetNativeValue(x);
end;
// --------------------------------------------------------------------
// SetNativeValue
// --------------------------------------------------------------------
procedure snv(const x: IInterface; const v: Variant);
begin
SetNativeValue(x, v);
end;
// --------------------------------------------------------------------
// Lowercases two strings, compares them, and returns value
// --------------------------------------------------------------------
function StringCompare(const this, that: String; cs: Boolean): Integer;
begin
if not cs then
Result := CompareStr(Lowercase(this), Lowercase(that));
else
Result := CompareStr(this, that);
end;
// --------------------------------------------------------------------
// Returns true/false if element is assigned by element's signature
// --------------------------------------------------------------------
function AssignedBySignature(const x: IInterface; const s: String): Boolean;
begin
Result := Assigned(GetElement(x, s));
end;
// --------------------------------------------------------------------
// Returns the string for a condition type
// --------------------------------------------------------------------
function GetConditionType(const val: String): String;
var
operators: String;
a, b, c, d, e: Boolean;
begin
if GetChar(val, 1) = '1' then a := True; // equal to
if GetChar(val, 2) = '1' then b := True; // greater than
if GetChar(val, 3) = '1' then c := True; // less than
if GetChar(val, 4) = '1' then d := True; // or
if val = '00000000' then e := True;
if e then Result := 'Not equal to';
if a and not b and not c and not d then Result := 'Equal to';
if b and not a and not c and not d then Result := 'Greater than';
if c and not a and not b and not d then Result := 'Less than';
if a and b and not c and not d then Result := 'Greater than or equal to';
if a and c and not b and not d then Result := 'Less than or equal to';
if a and d and not b and not c then Result := 'Equal to / Or';
if b and d and not a and not c then Result := 'Greater than / Or';
if c and d and not a and not b then Result := 'Less than / Or';
if a and b and d and not c then Result := 'Greater than or equal to / Or';
if a and c and d and not b then Result := 'Less than or equal to / Or';
end;
// --------------------------------------------------------------------
// Returns the string of a condition type but with comparison operators
// --------------------------------------------------------------------
function GetConditionOperator(const val: String): String;
var
a, b, c, d, e: Boolean;
begin
if GetChar(val, 1) = '1' then a := True; // equal to
if GetChar(val, 2) = '1' then b := True; // greater than
if GetChar(val, 3) = '1' then c := True; // less than
if GetChar(val, 4) = '1' then d := True; // or
if val = '00000000' then e := True;
if e then Result := '<>';
if a and not b and not c and not d then Result := '=='; // 1000
if b and not a and not c and not d then Result := '>'; // 0100
if c and not a and not b and not d then Result := '<'; // 0010
if a and b and not c and not d then Result := '>='; // 1100
if a and c and not b and not d then Result := '<='; // 1010
if a and d and not b and not c then Result := '== / Or'; // 1001
if not a and b and not c and d then Result := '> / Or'; // 0101
if not a and not b and c and d then Result := '< / Or'; // 0011
if a and b and d and not c then Result := '>= / Or'; // 1101
if a and c and d and not b then Result := '<= / Or'; // 1011
end;
// --------------------------------------------------------------------
// Returns a localized string as a string from a hexadecimal Form ID
// Note: Init LocalizationGetStringsFromFile() for performance gain
// --------------------------------------------------------------------
function GetLStringByFormID(const hex: String; const sl: TStringList): String;
var
idx: Integer;
begin
// add LocalizationGetStringsFromFile('fallout4_en.strings', stringListObj);
// to Initialize
idx := sl.IndexOfObject(TObject(StrToInt('$' + hex)));
if idx > -1 then
Result := sl[idx]
else
Result := 'String not found in lookup table';
end;
// --------------------------------------------------------------------
// Converts a string to a hexadecimal value
// --------------------------------------------------------------------
function StrToHex(const s: String): string;
var
i: Integer;
begin
Result := '';
for i := 1 to Length(s) do
Result := Result + IntToHex(Ord(s[i]), 2);
end;
// --------------------------------------------------------------------
// Reverses a byte array and returns a string
// --------------------------------------------------------------------
function ReverseHex(const s: String): String;
var
i: Integer;
slSource, slTarget: TStringList;
begin
slSource := TStringList.Create;
slSource.Delimiter := #32;
slTarget := TStringList.Create;
slTarget.Delimiter := #32;
slSource.DelimitedText := s;
for i := slSource.Count - 1 downto 0 do
slTarget.Add(slSource[i]);
Result := TrimChar(' ', slTarget.DelimitedText);
end;
// --------------------------------------------------------------------
// Return the number of times a character occurs in a string
// --------------------------------------------------------------------
function NumOfChar(const s: String; const c: Char): Integer;
var
i: Integer;
begin
Result := 0;
for i := 1 to Length(s) do
if s[i] = c then Inc(Result);
end;
// --------------------------------------------------------------------
// Trim character from string and return string
// --------------------------------------------------------------------
function TrimChar(const c: Char; const s: String): String;
begin
Result := StringReplace(s, c, '', [rfReplaceAll, rfIgnoreCase]);
end;
// --------------------------------------------------------------------
// Returns True if the x signature is in the y list of signatures
// --------------------------------------------------------------------
function InDelimitedList(x, y: String; const z: Char): Boolean;
var
i: Integer;
l: TStringList;
begin
i := -1;
try
l := TStringList.Create;
l.Delimiter := z;
l.DelimitedText := y;
i := l.IndexOf(x);
finally
FreeAndNil(l);
x := nil;
y := nil;
end;
Result := i > -1;
end;
// --------------------------------------------------------------------
// Returns true/false if a string is in a TStringList
// --------------------------------------------------------------------
function InStringList(const s: String; const l: TStringList): Boolean;
begin
Result := (l.IndexOf(s) <> -1);
end;
// --------------------------------------------------------------------
// Returns the number of duplicates of a string in a TStringList
// --------------------------------------------------------------------
function CountDuplicatesInTStringList(s: String; l: TStringList): Integer;
var
i, j: Integer;
begin
if not InStringList(s, l) then
Result := 0;
j := 0;
for i := 0 to l.Count - 1 do
if (s = l[i]) then
j := j + 1;
Result := j;
end;
// --------------------------------------------------------------------
// Returns true if the needle is in the haystack
// --------------------------------------------------------------------
function HasString(const asNeedle, asHaystack: String; const abCaseSensitive: Boolean): Boolean;
begin
if abCaseSensitive then
if Pos(asNeedle, asHaystack) > 0 then
Result := True
else
if Pos(Lowercase(asNeedle), Lowercase(asHaystack)) > 0 then
Result := True;
end;
// --------------------------------------------------------------------
// Returns any element from a string
// --------------------------------------------------------------------
function GetElement(const x: IInterface; const s: String): IInterface;
begin
if Length(s) > 0 then begin
if Pos('[', s) > 0 then
Result := ElementByIP(x, s)
else if Pos('\', s) > 0 then
Result := ElementByPath(x, s)
else if s = Uppercase(s) then
Result := ElementBySignature(x, s)
else
Result := ElementByName(x, s);
end;
end;
// --------------------------------------------------------------------
// Returns a master overriding record. Int parameter should be 1 or 2.
// --------------------------------------------------------------------
function OverrideMaster(const x: IInterface; i: Integer): IInterface;
var
o: IInterface;
begin
o := Master(x);
if not Assigned(o) then
o := MasterOrSelf(x);
if OverrideCount(o) > i - 1 then
o := OverrideByIndex(o, OverrideCount(o) - i);
Result := o;
end;
// --------------------------------------------------------------------
// Returns the last overriding record
// --------------------------------------------------------------------
function LastOverride(const x: IInterface): IInterface;
var
o: IInterface;
begin
o := Master(x);
if not Assigned(o) then
o := MasterOrSelf(x);
if OverrideCount(o) > 0 then
o := OverrideByIndex(o, OverrideCount(o) - 1);
Result := o;
end;
// --------------------------------------------------------------------
// Returns "NAME/EDID [SIG_:00000000]" as a String
// --------------------------------------------------------------------
function SmallNameEx(const e: IInterface): String;
var
sig: String;
begin
if Signature(e) <> 'REFR' then sig := 'EDID' else sig := 'NAME';
Result := Trim(GetElementEditValues(e, sig) + ' [' + Signature(e) + ':' + HexFormID(e) + ']');
end;
// --------------------------------------------------------------------
// Returns the sortkey with handling for .nif paths, and unknown/unused
// data. Also uses a better delimiter.
// --------------------------------------------------------------------
function SortKeyEx(const e: IInterface): String;
var
i: Integer;
kElement: IInterface;
begin
Result := Lowercase(GetEditValue(e));
for i := 0 to ElementCount(e) - 1 do
begin
kElement := ElementByIndex(e, i);
if (Pos('unknown', Lowercase(Name(kElement))) > 0)
or (Pos('unused', Lowercase(Name(kElement))) > 0) then
exit;
if Result <> '' then
Result := Result + ' ' + SortKeyEx(kElement)
else
Result := SortKeyEx(kElement);
end;
end;
// --------------------------------------------------------------------
// Returns values from a text file as a TStringList
// --------------------------------------------------------------------
function LoadFromCsv(const bSorted, bDuplicates, bDelimited: Boolean; const d: String = ';'): TStringList;
var
objFile: TOpenDialog;
lsLines: TStringList;
begin
lsLines := TStringList.Create;
if bSorted then
lsLines.Sorted;
if bDuplicates then
lsLines.Duplicates := dupIgnore;
if bDelimited then
if d <> '' then
lsLines.NameValueSeparator := d
else
lsLines.NameValueSeparator := #44;
objFile := TOpenDialog.Create(nil);
try
objFile.InitialDir := GetCurrentDir;
objFile.Options := [ofFileMustExist];
objFile.Filter := '*.csv';
objFile.FilterIndex := 1;
if objFile.Execute then
lsLines.LoadFromFile(objFile.FileName);
finally
objFile.Free;
end;
Result := lsLines;
end;
// --------------------------------------------------------------------
// Returns values from a text file as a TStringList
// --------------------------------------------------------------------
function LoadFromDelimitedList(const sDelimiter: String = ';'): TStringList;
var
objFile: TOpenDialog;
lsLines: TStringList;
begin
lsLines := TStringList.Create;
lsLines.Delimiter := sDelimiter;
objFile := TOpenDialog.Create(nil);
try
objFile.InitialDir := GetCurrentDir;
objFile.Options := [ofFileMustExist];
objFile.Filter := '*.csv';
objFile.FilterIndex := 1;
if objFile.Execute then
lsLines.LoadFromFile(objFile.FileName);
finally
objFile.Free;
end;
Result := lsLines;
end;
// --------------------------------------------------------------------
// Converts a Formlist or Leveled List to a TStringList
// --------------------------------------------------------------------
function ListObjectToTStringList(e: IInterface): TStringList;
var
aParent, aChild: IINterface;
lsResults: TStringList;
i: Integer;
begin
lsResults := TStringList.Create;
lsResults.Sorted := False;
lsResults.Duplicates := dupAccept;
aParent := ElementByName(e, 'Leveled List Entries');
if not Assigned(aParent) then
aParent := ElementByName(e, 'FormIDs');
if Name(aParent) = 'Leveled List Entries' then begin
for i := 0 to ElementCount(aParent) - 1 do begin
aChild := ElementBySignature(ElementByIndex(aParent, i), 'LVLO');
lsResults.Add(GetEditValue(ElementByIndex(aChild, 0)) + ',' + HexFormID(LinksTo(ElementByIndex(aChild, 2))) + ',' + GetEditValue(ElementByIndex(aChild, 3)));
end;
end else begin
for i := 0 to ElementCount(aParent) - 1 do begin
aChild := ElementByIndex(aParent, i);
lsResults.Add(GetEditValue(aChild));
end;
end;
Result := lsResults;
end;
// --------------------------------------------------------------------
// Returns a group by signature, or adds the group if needed
// --------------------------------------------------------------------
function AddGroupBySignature(const f: IwbFile; const s: String): IInterface;
begin
Result := GroupBySignature(f, s);
if not Assigned(Result) then
Result := Add(f, s, True);
end;
// --------------------------------------------------------------------
// Adds a new record to a group
// --------------------------------------------------------------------
function AddNewRecordToGroup(const g: IInterface; const s: String): IInterface;
begin
Result := Add(g, s, True);
if not Assigned(Result) then
Result := Add(g, s, True); // tries twice because
end;
// --------------------------------------------------------------------
// Returns an element by string, or adds the element if needed
// --------------------------------------------------------------------
function AddElementByString(const r: IInterface; const s: String): IInterface;
begin
Result := GetElement(r, s);
if not Assigned(Result) then
Result := Add(r, s, True);
end;
// --------------------------------------------------------------------
// Returns an element by string, or adds the element if needed
// --------------------------------------------------------------------
function AssignElementByString(const r: IInterface; const s: String): IInterface;
begin
Result := ElementAssign(GetElement(r, s), HighInteger, nil, False);
end;
function AppendElementByString(const r: IInterface; const s: String): IInterface;
begin
Result := ElementAssign(GetElement(r, s), LowInteger, nil, False);
end;
// --------------------------------------------------------------------
// Adds a form to a formlist
// --------------------------------------------------------------------
procedure AddRecordToFormList(const f, r: IInterface);
var
l: IInterface;
begin
l := ElementAssign(f, HighInteger, nil, True);
SetEditValue(l, IntToHex(FixedFormID(r), 8));
end;
// --------------------------------------------------------------------
// Int to Bin
// --------------------------------------------------------------------
function IntToBin(value: LongInt; sz: Integer): String;
var
i: Integer;
begin
Result := '';
for i := sz - 1 downto 0 do
if value and (1 shl i) <> 0 then
Result := Result + '1';
else
Result := Result + '0';
end;
// --------------------------------------------------------------------
// Bin to Int
// --------------------------------------------------------------------
function BinToInt(value: String): LongInt;
var
i, sz: Integer;
begin
Result := 0;
sz := Length(value);
for i := sz downto 1 do
if Copy(value, i, 1) = '1' then
Result := Result + (1 shl (sz - i));
end;
// --------------------------------------------------------------------
// Binary representation of float to integer
// --------------------------------------------------------------------
function FloatBinToInt(value: String): Real;
var
i, sz: Integer;
begin
Result := 0;
sz := Length(Value);
for i := sz downto 1 do
if Copy(value, i, 1) = '1' then
Result := Result + 1 / (1 shl i);
end;
// --------------------------------------------------------------------
// Hex to Bin
// --------------------------------------------------------------------
function HexToBin(h: String): String;
var
box: Array [0..15] of String;
i: Integer;
begin
box[0] := '0000';
box[1] := '0001';
box[2] := '0010';
box[3] := '0011';
box[4] := '0100';
box[5] := '0101';
box[6] := '0110';
box[7] := '0111';
box[8] := '1000';
box[9] := '1001';
box[10] := '1010';
box[11] := '1011';
box[12] := '1100';
box[13] := '1101';
box[14] := '1110';
box[15] := '1111';
for i := Length(h) downto 1 do
Result := box[StrToInt('$' + h[i])] + Result;
end;
// --------------------------------------------------------------------
// Hex to Float
// --------------------------------------------------------------------
function HexToFloat(s: String): Real;
var
e: Integer;
f: Real;
b: String;
begin
b := HexToBin(s);
e := BinToInt(Copy(b, 2,8)) - 127;
f := 1 + FloatBinToInt(Copy(b, 10, 23));
if Copy(b, 1,1) <> '0' then
Result := -Power(2, e) * f
else
Result := Power(2, e) * f;
end;
// --------------------------------------------------------------------
// zilav's hex array to string function
// --------------------------------------------------------------------
function HexToString(s: String): String;
var
c: Char;
i: Integer;
begin
Result := '';
i := 1;
while i < Length(s) do begin
if s <> ' ' then begin
c := Chr(StrToInt('$' + Copy(s, i, 2)));
if c = #0 then
exit;
Result := Result + c;
i := i + 3;
end else
i := i + 1;
end;
end;
end.