-
Notifications
You must be signed in to change notification settings - Fork 5
/
wbHelpers.pas
1015 lines (914 loc) · 29.3 KB
/
wbHelpers.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 wbHelpers;
{$I wbDefines.inc}
interface
uses
Classes,
Windows,
SysUtils,
Graphics,
ShellAPI,
ShlObj,
IniFiles,
Registry,
wbInterface,
Imaging,
ImagingTypes;
function wbDistance(const a, b: TwbVector): Single; overload
function wbDistance(const a, b: IwbMainRecord): Single; overload;
function wbGetSiblingREFRsWithin(const aMainRecord: IwbMainRecord; aDistance: Single): TDynMainRecords;
function FindMatchText(Strings: TStrings; const Str: string): Integer;
function IsFileESM(const aFileName: string): Boolean;
function IsFileESP(const aFileName: string): Boolean;
procedure DeleteDirectory(const DirName: string);
function FullPathToFilename(aString: string): string;
procedure wbFlipBitmap(aBitmap: TBitmap; MirrorType: Integer); // MirrorType: 1 - horizontal, 2 - vertical, 0 - both
function wbAlphaBlend(DestDC, X, Y, Width, Height,
SrcDC, SrcX, SrcY, SrcWidth, SrcHeight, Alpha: integer): Boolean;
procedure SaveFont(aIni: TMemIniFile; aSection, aName: string; aFont: TFont);
procedure LoadFont(aIni: TMemIniFile; aSection, aName: string; aFont: TFont);
function wbDDSDataToBitmap(aData: TBytes; Bitmap: TBitmap): Boolean;
function wbDDSStreamToBitmap(aStream: TStream; Bitmap: TBitmap): Boolean;
function wbCRC32Data(aData: TBytes): Cardinal;
function wbCRC32File(aFileName: string): Cardinal;
function wbDecodeCRCList(const aList: string): TDynCardinalArray;
function wbSHA1Data(aData: TBytes): string;
function wbSHA1File(aFileName: string): string;
function wbMD5Data(aData: TBytes): string;
function wbMD5File(aFileName: string): string;
function wbIsAssociatedWithExtension(aExt: string): Boolean;
function wbAssociateWithExtension(aExt, aName, aDescr: string): Boolean;
type
PnxLeveledListCheckCircularStack = ^TnxLeveledListCheckCircularStack;
TnxLeveledListCheckCircularStack = record
rllcLast : PnxLeveledListCheckCircularStack;
rllcMainRecord : IwbMainRecord;
end;
procedure wbLeveledListCheckCircular(const aMainRecord: IwbMainRecord; aStack: PnxLeveledListCheckCircularStack);
type
TnxFastStringList = class(TStringList)
protected
function CompareStrings(const S1, S2: string): Integer; override;
public
constructor CreateSorted(aDups : TDuplicates = dupError);
procedure Clear(aFreeObjects: Boolean = False); reintroduce;
end;
TnxFastStringListCS = class(TnxFastStringList)
public
procedure AfterConstruction; override;
end;
TnxFastStringListIC = class(TnxFastStringList)
end;
function wbExtractNameFromPath(aPathName: String): String;
function wbCounterAfterSet(aCounterName: String; const aElement: IwbElement): Boolean;
function wbCounterByPathAfterSet(aCounterName: String; const aElement: IwbElement): Boolean;
function wbCounterContainerAfterSet(aCounterName: String; anArrayName: String; const aElement: IwbElement; DeleteOnEmpty: Boolean = False): Boolean;
function wbCounterContainerByPathAfterSet(aCounterName: String; anArrayName: String; const aElement: IwbElement): Boolean;
// BSA helper
function MakeDataFileName(FileName, DataPath: String): String;
function FindBSAs(IniName, DataPath: String; var bsaNames: TStringList; var bsaMissing: TStringList): Integer;
function HasBSAs(ModName, DataPath: String; Exact, modini: Boolean; var bsaNames: TStringList; var bsaMissing: TStringList): Integer;
implementation
uses
wbSort;
procedure wbLeveledListCheckCircular(const aMainRecord: IwbMainRecord; aStack: PnxLeveledListCheckCircularStack);
var
Stack : TnxLeveledListCheckCircularStack;
s : string;
CER : IwbContainerElementRef;
LLE : IwbContainerElementRef;
i : Integer;
LVLO : IwbContainerElementRef;
Reference : IwbContainerElementRef;
MainRecord : IwbMainRecord;
begin
Stack.rllcLast := aStack;
Stack.rllcMainRecord := aMainRecord;
while Assigned(aStack) do begin
if aStack.rllcMainRecord.LoadOrderFormID = aMainRecord.LoadOrderFormID then begin
s := aMainRecord.Name;
aStack := Stack.rllcLast;
while Assigned(aStack) do begin
s := ' -> ' + s;
s := aStack.rllcMainRecord.Name + s;
if aStack.rllcMainRecord.LoadOrderFormID = aMainRecord.LoadOrderFormID then
Break;
aStack := aStack.rllcLast;
end;
s := 'Circular Leveled List found: ' + s;
raise Exception.Create(s);
end;
aStack := aStack.rllcLast;
end;
if aMainRecord.IsTagged then
Exit;
aMainRecord.Tag;
if Supports(aMainRecord, IwbContainerElementRef, CER) then begin
if Supports(CER.ElementByName['Leveled List Entries'], IwbContainerElementRef, LLE) then begin
for i := 0 to Pred(LLE.ElementCount) do
if Supports(LLE.Elements[i], IwbContainerElementRef, LVLO) then begin
if Supports(LVLO.ElementByName['Reference'], IwbContainerElementRef, Reference) then begin
if Supports(Reference.LinksTo, IwbMainRecord, MainRecord) then begin
if (MainRecord.Signature = aMainRecord.Signature) then begin
MainRecord := MainRecord.WinningOverride;
wbLeveledListCheckCircular(MainRecord, @Stack);
end;
end;
end;
end;
end;
end;
end;
function Vec3Subtract(out vOut: TwbVector; const v1, v2: TwbVector): TwbVector;
begin
with vOut do
begin
x:= v1.x - v2.x;
y:= v1.y - v2.y;
z:= v1.z - v2.z;
end;
Result := vOut;
end;
function Vec3Length(const v: TwbVector): Single;
begin
with v do Result:= Sqrt(x*x + y*y + z*z);
end;
function wbDistance(const a, b: TwbVector): Single;
var
t: TwbVector;
begin
Result := Vec3Length(Vec3Subtract(t,a,b));
end;
function wbDistance(const a, b: IwbMainRecord): Single; overload;
var
PosA, PosB: TwbVector;
begin
if not a.GetPosition(PosA) then
raise Exception.Create('GetPosition failed');
if not b.GetPosition(PosB) then
raise Exception.Create('GetPosition failed');
Result := wbDistance(PosA, PosB);
end;
function wbGetSiblingREFRsWithin(const aMainRecord: IwbMainRecord; aDistance: Single): TDynMainRecords;
var
Count : Integer;
Position : TwbVector;
MaxLoadOrder: Integer;
procedure FindREFRs(const aElement: IwbElement);
var
MainRecord : IwbMainRecord;
Container : IwbContainerElementRef;
i : Integer;
Temp : TwbVector;
begin
if Supports(aElement, IwbMainRecord, MainRecord) then begin
if not (aMainRecord.LoadOrderFormID = MainRecord.LoadOrderFormID) and
MainRecord.GetPosition(Temp) and
(wbDistance(Temp,Position) <= aDistance) then begin
if High(Result) < Count then
SetLength(Result, Length(Result) * 2);
Result[Count] := MainRecord.HighestOverrideOrSelf[MaxLoadOrder];
Inc(Count);
end;
end else
if Supports(aElement, IwbContainerElementRef, Container) then
for i := 0 to Pred(Container.ElementCount) do
FindREFRs(Container.Elements[i]);
end;
var
GroupRecord : IwbGroupRecord;
CellMaster : IwbMainRecord;
i, j : Integer;
begin
Result := nil;
if not aMainRecord.GetPosition(Position) then
Exit;
if not Supports(aMainRecord.Container, IwbGroupRecord, GroupRecord) then
Exit;
if not (GroupRecord.GroupType in [8..10]) then
Exit;
CellMaster := GroupRecord.ChildrenOf;
if not Assigned(CellMaster) then
Exit;
CellMaster := CellMaster.MasterOrSelf;
MaxLoadOrder := aMainRecord._File.LoadOrder;
Count := 0;
SetLength(Result, 1024);
FindREFRs(CellMaster.ChildGroup);
for i := 0 to Pred(CellMaster.OverrideCount) do
if CellMaster.Overrides[i]._File.LoadOrder <= aMainRecord._File.LoadOrder then
FindREFRs(CellMaster.Overrides[i])
else
Break;
SetLength(Result, Count);
if Length(Result) > 1 then begin
wbMergeSort(@Result[0], Length(Result), CompareElementsFormIDAndLoadOrder);
j := 0;
for i := Succ(Low(Result)) to High(Result) do begin
if (Result[j].LoadOrderFormID <> Result[i].LoadOrderFormID) and not (Result[j].IsDeleted) then
Inc(j);
if j <> i then
Result[j] := Result[i];
end;
SetLength(Result, Succ(j));
end;
end;
function FindMatchText(Strings: TStrings; const Str: string): Integer;
begin
for Result := 0 to Strings.Count-1 do
if SameText(Strings[Result], Str) then
Exit;
Result := -1;
end;
function IsFileESM(const aFileName: string): Boolean;
const
ghostesm = '.esm.ghost';
begin
Result := SameText(ExtractFileExt(aFileName), '.esm') or
SameText(Copy(aFileName, Length(aFileName) - Length(ghostesm) + 1, Length(ghostesm)), ghostesm)
end;
function IsFileESP(const aFileName: string): Boolean;
const
ghostesp = '.esp.ghost';
begin
Result := SameText(ExtractFileExt(aFileName), '.esp') or
SameText(Copy(aFileName, Length(aFileName) - Length(ghostesp) + 1, Length(ghostesp)), ghostesp)
end;
procedure DeleteDirectory(const DirName: string);
var
FileOp: TSHFileOpStruct;
begin
FillChar(FileOp, SizeOf(FileOp), 0);
FileOp.wFunc := FO_DELETE;
FileOp.pFrom := PChar(DirName+#0);//double zero-terminated
FileOp.fFlags := FOF_SILENT or FOF_NOERRORUI or FOF_NOCONFIRMATION;
SHFileOperation(FileOp);
end;
function FullPathToFilename(aString: string): string;
var
i: Integer;
s: string;
begin
s := aString;
for i := Length(s) downto 1 do
if Copy(s, i, 3) = ' \ ' then begin
Delete(s, i, 1);
Delete(s, i+1, 1);
end else if s[i] = '"' then
s[i] := ''''
else if s[i] = ':' then
s[i] := '-';
Result := s;
end;
procedure wbFlipBitmap(aBitmap: TBitmap; MirrorType: Integer);
var
MemBmp: TBitmap;
Dest: TRect;
begin
if not Assigned(aBitmap) then
Exit;
MemBmp := TBitmap.Create;
try
MemBmp.Assign(aBitmap);
case MirrorType of
1:
begin
Dest.Left := MemBmp.Width;
Dest.Top := 0;
Dest.Right := -MemBmp.Width;
Dest.Bottom := MemBmp.Height
end;
2:
begin
Dest.Left := 0;
Dest.Top := MemBmp.Height;
Dest.Right := MemBmp.Width;
Dest.Bottom := -MemBmp.Height
end;
0:
begin
Dest.Left := MemBmp.Width;
Dest.Top := MemBmp.Height;
Dest.Right := -MemBmp.Width;
Dest.Bottom := -MemBmp.Height
end;
end;
StretchBlt(MemBmp.Canvas.Handle, Dest.Left, Dest.Top, Dest.Right, Dest.Bottom,
MemBmp.Canvas.Handle, 0, 0, MemBmp.Width, MemBmp.Height,
SRCCOPY);
aBitmap.Assign(MemBmp);
finally
FreeAndNil(MemBmp);
end;
end;
function wbAlphaBlend(DestDC, X, Y, Width, Height,
SrcDC, SrcX, SrcY, SrcWidth, SrcHeight, Alpha: integer): Boolean;
var
BlendFunc: TBlendFunction;
begin
BlendFunc.BlendOp := AC_SRC_OVER;
BlendFunc.BlendFlags := 0;
BlendFunc.SourceConstantAlpha := Alpha;
if Alpha = 255 then
BlendFunc.AlphaFormat := AC_SRC_ALPHA
else
BlendFunc.AlphaFormat := 0;
Result := Windows.AlphaBlend(DestDC, X, Y, Width, Height, SrcDC, SrcX, SrcY, SrcWidth, SrcHeight, BlendFunc);
end;
procedure SaveFont(aIni: TMemIniFile; aSection, aName: string; aFont: TFont);
begin
aIni.WriteString(aSection, aName + 'Name', aFont.Name);
aIni.WriteInteger(aSection, aName + 'CharSet', aFont.CharSet);
aIni.WriteInteger(aSection, aName + 'Color', aFont.Color);
aIni.WriteInteger(aSection, aName + 'Size', aFont.Size);
aIni.WriteInteger(aSection, aName + 'Style', Byte(aFont.Style));
end;
procedure LoadFont(aIni: TMemIniFile; aSection, aName: string; aFont: TFont);
begin
aFont.Name := aIni.ReadString(aSection, aName + 'Name', aFont.Name);
aFont.CharSet := TFontCharSet(aIni.ReadInteger(aSection, aName + 'CharSet', aFont.CharSet));
aFont.Color := TColor(aIni.ReadInteger(aSection, aName + 'Color', aFont.Color));
aFont.Size := aIni.ReadInteger(aSection, aName + 'Size', aFont.Size);
aFont.Style := TFontStyles(Byte(aIni.ReadInteger(aSection, aName + 'Style', Byte(aFont.Style))));
end;
var
crctbl: array[0..7] of array[0..255] of cardinal;
procedure CRCInit;
var
c: cardinal;
i, j: integer;
begin;
for i:=0 to 255 do begin;
c:=i;
for j:=1 to 8 do if odd(c)
then c:=(c shr 1) xor $EDB88320
else c:=(c shr 1);
crctbl[0][i]:=c;
end;
for i:=0 to 255 do begin;
c:=crctbl[0][i];
for j:=1 to 7 do begin;
c:=(c shr 8) xor crctbl[0][byte(c)];
crctbl[j][i]:=c;
end;
end;
end;
function ShaCrcRefresh(OldCRC: cardinal; BufPtr: pointer; BufLen: integer): cardinal;
// Fast CRC32 calculator
// (c) Aleksandr Sharahov 2009
// Free for any use
asm
test edx, edx
jz @ret
neg ecx
jz @ret
push ebx
@head:
test dl, 3
jz @bodyinit
movzx ebx, byte [edx]
inc edx
xor bl, al
shr eax, 8
xor eax, [ebx*4 + crctbl]
inc ecx
jnz @head
pop ebx
@ret:
ret
@bodyinit:
sub edx, ecx
add ecx, 8
jg @bodydone
push esi
push edi
mov edi, edx
mov edx, eax
@bodyloop:
mov ebx, [edi + ecx - 4]
xor edx, [edi + ecx - 8]
movzx esi, bl
mov eax, [esi*4 + crctbl + 1024*3]
movzx esi, bh
xor eax, [esi*4 + crctbl + 1024*2]
shr ebx, 16
movzx esi, bl
xor eax, [esi*4 + crctbl + 1024*1]
movzx esi, bh
xor eax, [esi*4 + crctbl + 1024*0]
movzx esi, dl
xor eax, [esi*4 + crctbl + 1024*7]
movzx esi, dh
xor eax, [esi*4 + crctbl + 1024*6]
shr edx, 16
movzx esi, dl
xor eax, [esi*4 + crctbl + 1024*5]
movzx esi, dh
xor eax, [esi*4 + crctbl + 1024*4]
add ecx, 8
jg @done
mov ebx, [edi + ecx - 4]
xor eax, [edi + ecx - 8]
movzx esi, bl
mov edx, [esi*4 + crctbl + 1024*3]
movzx esi, bh
xor edx, [esi*4 + crctbl + 1024*2]
shr ebx, 16
movzx esi, bl
xor edx, [esi*4 + crctbl + 1024*1]
movzx esi, bh
xor edx, [esi*4 + crctbl + 1024*0]
movzx esi, al
xor edx, [esi*4 + crctbl + 1024*7]
movzx esi, ah
xor edx, [esi*4 + crctbl + 1024*6]
shr eax, 16
movzx esi, al
xor edx, [esi*4 + crctbl + 1024*5]
movzx esi, ah
xor edx, [esi*4 + crctbl + 1024*4]
add ecx, 8
jle @bodyloop
mov eax, edx
@done:
mov edx, edi
pop edi
pop esi
@bodydone:
sub ecx, 8
jl @tail
pop ebx
ret
@tail:
movzx ebx, byte [edx + ecx];
xor bl,al;
shr eax,8;
xor eax, [ebx*4 + crctbl];
inc ecx;
jnz @tail;
pop ebx
ret
end;
function wbCRC32Data(aData: TBytes): Cardinal;
begin
Result := not ShaCrcRefresh($FFFFFFFF, @aData[0], Length(aData));
end;
function wbCRC32File(aFileName: string): Cardinal;
var
Data: TBytes;
begin
Result := 0;
if FileExists(aFileName) then
with TFileStream.Create(aFileName, fmOpenRead + fmShareDenyNone) do try
SetLength(Data, Size);
ReadBuffer(Data[0], Length(Data));
Result := wbCRC32Data(Data);
finally
Free;
end;
end;
function wbDecodeCRCList(const aList: string): TDynCardinalArray;
var
i: Integer;
s: string;
j: Int64;
begin
Result := nil;
try
with TStringList.Create do try
CommaText := aList;
for i := 0 to Pred(Count) do begin
s := Trim(Strings[i]);
if Length(s) <> 8 then
Abort;
j := StrToInt64('$'+s);
if (j < Low(Cardinal)) or (j > High(Cardinal)) then
Abort;
SetLength(Result, Succ(Length(Result)));
Result[High(Result)] := j;
end;
finally
Free;
end;
except
SetLength(Result, 1);
Result[0] := $FFFFFFFF;
end;
end;
function CryptAcquireContext(var phProv: DWORD;
pszContainer, pszProvider: LPCSTR; dwProvType, dwFlags: DWORD): BOOL;
stdcall; external advapi32 name 'CryptAcquireContextA';
function CryptCreateHash(hProv,Algid,hKey,dwFlags: DWORD;
var phHash: DWORD): BOOL; stdcall; external advapi32;
function CryptHashData(hHash: DWORD; pbData: PBYTE; dwDataLen,
dwFlags: DWORD): BOOL; stdcall; external advapi32;
function CryptGetHashParam(hHash, dwParam: DWORD; pbData: PBYTE;
var pdwDataLen: DWORD; dwFlags: DWORD): BOOL; stdcall; external advapi32;
function CryptDestroyHash(hHash: DWORD): BOOL; stdcall; external advapi32;
function CryptReleaseContext(hProv: DWORD; dwFlags: DWORD): BOOL; stdcall; external advapi32;
function CryptoAPIGetHash(Data: Pointer; nSize: Cardinal; HashType: Cardinal): TBytes;
const
HP_HASHVAL = $0002; {hash value}
PROV_RSA_FULL = 1;
CRYPT_VERIFYCONTEXT = $F0000000;
var
hProv, hHash: Cardinal;
begin
if CryptAcquireContext(hProv, nil, nil, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT) then try
if CryptCreateHash(hProv, HashType, 0, 0, hHash) then try
if CryptHashData(hHash, Data, nSize, 0) then begin
if CryptGetHashParam(hHash, HP_HASHVAL, nil, nSize, 0) then begin
SetLength(Result, nSize);
if not CryptGetHashParam(hHash, HP_HASHVAL, @Result[0], nSize, 0) then
SetLength(Result, 0);
end;
end;
finally
CryptDestroyHash(hHash);
end;
finally
CryptReleaseContext(hProv, 0);
end;
end;
const
ALG_CRC32 = $0001;
ALG_MD2 = $8001;
ALG_MD4 = $8002;
ALG_MD5 = $8003;
ALG_SHA = $8004;
function wbCryptoApiHashData(aData: TBytes; aALG: Cardinal): string;
function BytesToHexStr(aBytes: TBytes): string;
var
i: Cardinal;
bt: Byte;
const
Hex = '0123456789abcdef';
begin
Result:= '';
for i:= Low(aBytes) to High(aBytes) do begin
bt := aBytes[i];
Result:= Result + Hex[bt shr $4 + 1] + Hex[bt and $0f + 1]
end;
end;
begin
Result := BytesToHexStr(CryptoAPIGetHash(@aData[0], Length(aData), aALG));
end;
function wbSHA1Data(aData: TBytes): string;
begin
Result := wbCryptoApiHashData(aData, ALG_SHA);
end;
function wbSHA1File(aFileName: string): string;
var
Data: TBytes;
begin
Result := '';
if FileExists(aFileName) then
with TFileStream.Create(aFileName, fmOpenRead + fmShareDenyNone) do try
SetLength(Data, Size);
ReadBuffer(Data[0], Length(Data));
Result := wbSHA1Data(Data);
finally
Free;
end;
end;
function wbMD5Data(aData: TBytes): string;
begin
Result := wbCryptoApiHashData(aData, ALG_MD5);
end;
function wbMD5File(aFileName: string): string;
var
Data: TBytes;
begin
Result := '';
if FileExists(aFileName) then
with TFileStream.Create(aFileName, fmOpenRead + fmShareDenyNone) do try
SetLength(Data, Size);
ReadBuffer(Data[0], Length(Data));
Result := wbMD5Data(Data);
finally
Free;
end;
end;
{ TnxFastStringList }
procedure TnxFastStringList.Clear(aFreeObjects: Boolean);
var
i: Integer;
begin
if aFreeObjects then
for i := 0 to Pred(Count) do
Objects[i].Free;
inherited Clear;
end;
function TnxFastStringList.CompareStrings(const S1, S2: string): Integer;
begin
{x$IFDEF DCC6OrLater}
if CaseSensitive then
Result := CompareStr(S1, S2)
else
{x$ENDIF}
Result := CompareText(S1, S2);
end;
constructor TnxFastStringList.CreateSorted(aDups: TDuplicates);
begin
Create;
Duplicates := aDups;
Sorted := True;
end;
{ TnxFastStringListCS }
procedure TnxFastStringListCS.AfterConstruction;
begin
inherited;
{x$IFDEF DCC6OrLater}
CaseSensitive := True;
{x$ENDIF}
end;
function wbExtractNameFromPath(aPathName: String): String;
begin
Result := aPathName;
while Pos('\', Result)>0 do
Delete(Result, 1, Pos('\', Result))
end;
function wbCounterAfterSet(aCounterName: String; const aElement: IwbElement): Boolean;
var
Element : IwbElement;
Container : IwbContainer;
SelfAsContainer : IwbContainer;
begin
Result := False;
if wbBeginInternalEdit then try
if (Length(aCounterName)>=4) and Supports(aElement.Container, IwbContainer, Container) and
Supports(aElement, IwbContainer, SelfAsContainer) then begin
Element := Container.ElementByName[aCounterName];
if not Assigned(Element) then // Signatures not listed in mrDef cannot be added
Element := Container.Add(Copy(aCounterName, 1, 4));
if Assigned(Element) and (SameText(Element.Name, aCounterName)) then try
if (Element.GetNativeValue<>SelfAsContainer.GetElementCount) then
Element.SetNativeValue(SelfAsContainer.GetElementCount);
Result := True;
except
// No exception if the value cannot be set, expected non value
end;
end;
finally
wbEndInternalEdit;
end;
end;
function wbCounterByPathAfterSet(aCounterName: String; const aElement: IwbElement): Boolean;
var
Element : IwbElement;
Container : IwbContainer;
SelfAsContainer : IwbContainer;
begin
Result := False;
if wbBeginInternalEdit then try
if (Length(aCounterName)>=4) and Supports(aElement.Container, IwbContainer, Container) and
Supports(aElement, IwbContainer, SelfAsContainer) then begin
Element := Container.ElementByPath[aCounterName];
// if not Assigned(Element) then // Signatures not listed in mrDef cannot be added
// Element := Container.Add(Copy(aCounterName, 1, 4));
if Assigned(Element) and (SameText(Element.Name, wbExtractNameFromPath(aCounterName))) then try
if (Element.GetNativeValue<>SelfAsContainer.GetElementCount) then
Element.SetNativeValue(SelfAsContainer.GetElementCount);
Result := True;
except
// No exception if the value cannot be set, expected non value
end;
end;
finally
wbEndInternalEdit;
end;
end;
function wbCounterContainerAfterSet(aCounterName: String; anArrayName: String; const aElement: IwbElement; DeleteOnEmpty: Boolean = False): Boolean;
var
Element : IwbElement;
Elems : IwbElement;
Container : IwbContainer;
begin
Result := False; // You may need to check alterative counter name
if wbBeginInternalEdit then try
if Supports(aElement, IwbContainer, Container) then begin
Element := Container.ElementByName[aCounterName];
Elems := Container.ElementByName[anArrayName];
if Assigned(Element) then begin
if not Assigned(Elems) then
if Element.GetNativeValue<>0 then
Element.SetNativeValue(0)
else if DeleteOnEmpty then
Container.RemoveElement(aCounterName);
Result := True; // Counter member exists
end;
end;
finally
wbEndInternalEdit;
end;
end;
function wbCounterContainerByPathAfterSet(aCounterName: String; anArrayName: String; const aElement: IwbElement): Boolean;
var
Element : IwbElement;
Elems : IwbElement;
Container : IwbContainer;
begin
Result := False; // You may need to check alterative counter name
if wbBeginInternalEdit then try
if Supports(aElement, IwbContainer, Container) then begin
Element := Container.ElementByPath[aCounterName];
Elems := Container.ElementByName[anArrayName];
if Assigned(Element) then begin
if not Assigned(Elems) then
if Element.GetNativeValue<>0 then
Element.SetNativeValue(0);
Result := True; // Counter member exists
end;
end;
finally
wbEndInternalEdit;
end;
end;
// BSA helper
function MakeDataFileName(FileName, DataPath: String): String;
begin
// MO uses 3 chars aliases
if Length(FileName) < 3 then
Result := ''
else if not ((FileName[1] = '\') or (FileName[2] = ':')) then
Result := DataPath + FileName
else
Result := FileName;
end;
function FindBSAs(IniName, DataPath: String; var bsaNames: TStringList; var bsaMissing: TStringList): Integer;
var
i: Integer;
j: Integer;
s: String;
t: String;
begin
Result := 0;
j := 0;
if Assigned(bsaNames) then
j := bsaNames.Count;
if Assigned(bsaMissing) then
j := j + bsaMissing.Count;
if Assigned(bsaNames) then
// TIniFile uses GetPrivateProfileString() to read data, it is virtualized by MO
// TMemIniFile reads from string list directly, not supported by MO
with TIniFile.Create(iniName) do try
with TStringList.Create do try
if wbGameMode in [gmTES4, gmFO3, gmFNV] then
Text := StringReplace(ReadString('Archive', 'sArchiveList', ''), ',' ,#10, [rfReplaceAll])
else
Text := StringReplace(
ReadString('Archive', 'sResourceArchiveList', '') + ',' + ReadString('Archive', 'sResourceArchiveList2', ''),
',', #10, [rfReplaceAll]
);
for i := 0 to Pred(Count) do begin
s := Trim(Strings[i]);
t := MakeDataFileName(s, DataPath);
if (Length(t)>0) then
if FileExists(t) then begin
if wbContainerHandler.ContainerExists(t) then
Continue;
bsaNames.Add(s);
end else
if Assigned(bsaMissing) then
bsaMissing.Add(s);
end;
Result := bsaNames.Count + bsaMissing.Count - j; // How many were added
finally
Free;
end;
finally
Free;
end;
end;
function HasBSAs(ModName, DataPath: String; Exact, modini: Boolean; var bsaNames: TStringList; var bsaMissing: TStringList): Integer;
var
j: Integer;
t: String;
F: TSearchRec;
begin
Result := 0;
j := 0;
if Assigned(bsaNames) then
j := bsaNames.Count;
if Assigned(bsaMissing) then
j := j + bsaMissing.Count;
// All games prior to Skyrim load BSA files with partial matching, Skyrim requires exact name match and
// can use a private ini to specify the bsa to use.
if not exact then
ModName := ModName + '*';
if FindFirst(DataPath + ModName + '.bsa', faAnyFile, F) = 0 then try
repeat
if wbContainerHandler.ContainerExists(DataPath + F.Name) then
Continue;
t := MakeDataFileName(F.Name, DataPath);
if (Length(t)>0) and FileExists(t) then begin
if not wbContainerHandler.ContainerExists(t) then
if Assigned(bsaNames) then
bsaNames.Add(F.Name);
end else
if Assigned(bsaMissing) then
bsaMissing.Add(F.Name);
until FindNext(F) <> 0;
Result := bsaNames.Count + bsaMissing.Count - j;
finally
FindClose(F);
end;
if modIni then
Result := Result + FindBSAs(DataPath+ChangeFileExt(ModName, '.ini'), DataPath, bsaNames, bsaMissing);
end;
function wbDDSDataToBitmap(aData: TBytes; Bitmap: TBitmap): Boolean;
var
img: TImageData;
ms: TMemoryStream;
begin
Result := False;
if not LoadImageFromMemory(@aData[0], Length(aData), img) then
Exit;
ms := TMemoryStream.Create;
try
if SaveImageToStream('BMP', ms, img) then begin
ms.Position := 0;
Bitmap.LoadFromStream(ms);
Result := True;
end;
finally
FreeImage(img);
ms.Free;
end;
end;
function wbDDSStreamToBitmap(aStream: TStream; Bitmap: TBitmap): Boolean;
var
img: TImageData;
ms: TMemoryStream;
begin
Result := False;
if not LoadImageFromStream(aStream, img) then
Exit;
ms := TMemoryStream.Create;
try
if SaveImageToStream('BMP', ms, img) then begin
ms.Position := 0;
Bitmap.LoadFromStream(ms);
Result := True;
end;
finally
FreeImage(img);
ms.Free;
end;
end;
function wbIsAssociatedWithExtension(aExt: string): Boolean;
var
Name: string;
begin
Result := False;
with TRegistry.Create do try
RootKey := HKEY_CURRENT_USER;
if OpenKey('\Software\Classes\' + LowerCase(aExt), False) then begin
Name := ReadString('');
if OpenKey('\Software\Classes\' + Name + '\DefaultIcon', False) then
if SameText(ReadString(''), ParamStr(0)) then
Result := True;
end;
finally
Free;
end;
end;
function wbAssociateWithExtension(aExt, aName, aDescr: string): Boolean;
begin
Result := False;
if aExt = '' then
Exit
else
aExt := LowerCase(aExt);
if aExt[1] <> '.' then
aExt := '.' + aExt;
with TRegistry.Create do try
RootKey := HKEY_CURRENT_USER;
if OpenKey('\Software\Classes\' + aExt, True) then
WriteString('', aName)
else
raise Exception.Create('Not enough rights to modify the registry');
if OpenKey('\Software\Classes\' + aName, True) then
WriteString('', aDescr);
if OpenKey('\Software\Classes\' + aName + '\DefaultIcon', True) then
WriteString('', ParamStr(0));