forked from deorder/libbsarch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwbBSArchive.pas
2383 lines (2145 loc) · 82.6 KB
/
wbBSArchive.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 wbBSArchive;
interface
uses
SysUtils,
Classes,
Windows,
Threading,
wbStreams,
tfTypes,
tfMD5;
type
TMagic4 = array [0..3] of AnsiChar;
PMagic4 = ^TMagic4;
TDXGI = (
DXGI_FORMAT_UNKNOWN,
DXGI_FORMAT_R32G32B32A32_TYPELESS,
DXGI_FORMAT_R32G32B32A32_FLOAT,
DXGI_FORMAT_R32G32B32A32_UINT,
DXGI_FORMAT_R32G32B32A32_SINT,
DXGI_FORMAT_R32G32B32_TYPELESS,
DXGI_FORMAT_R32G32B32_FLOAT,
DXGI_FORMAT_R32G32B32_UINT,
DXGI_FORMAT_R32G32B32_SINT,
DXGI_FORMAT_R16G16B16A16_TYPELESS,
DXGI_FORMAT_R16G16B16A16_FLOAT,
DXGI_FORMAT_R16G16B16A16_UNORM,
DXGI_FORMAT_R16G16B16A16_UINT,
DXGI_FORMAT_R16G16B16A16_SNORM,
DXGI_FORMAT_R16G16B16A16_SINT,
DXGI_FORMAT_R32G32_TYPELESS,
DXGI_FORMAT_R32G32_FLOAT,
DXGI_FORMAT_R32G32_UINT,
DXGI_FORMAT_R32G32_SINT,
DXGI_FORMAT_R32G8X24_TYPELESS,
DXGI_FORMAT_D32_FLOAT_S8X24_UINT,
DXGI_FORMAT_R32_FLOAT_X8X24_TYPELESS,
DXGI_FORMAT_X32_TYPELESS_G8X24_UINT,
DXGI_FORMAT_R10G10B10A2_TYPELESS,
DXGI_FORMAT_R10G10B10A2_UNORM,
DXGI_FORMAT_R10G10B10A2_UINT,
DXGI_FORMAT_R11G11B10_FLOAT,
DXGI_FORMAT_R8G8B8A8_TYPELESS,
DXGI_FORMAT_R8G8B8A8_UNORM,
DXGI_FORMAT_R8G8B8A8_UNORM_SRGB,
DXGI_FORMAT_R8G8B8A8_UINT,
DXGI_FORMAT_R8G8B8A8_SNORM,
DXGI_FORMAT_R8G8B8A8_SINT,
DXGI_FORMAT_R16G16_TYPELESS,
DXGI_FORMAT_R16G16_FLOAT,
DXGI_FORMAT_R16G16_UNORM,
DXGI_FORMAT_R16G16_UINT,
DXGI_FORMAT_R16G16_SNORM,
DXGI_FORMAT_R16G16_SINT,
DXGI_FORMAT_R32_TYPELESS,
DXGI_FORMAT_D32_FLOAT,
DXGI_FORMAT_R32_FLOAT,
DXGI_FORMAT_R32_UINT,
DXGI_FORMAT_R32_SINT,
DXGI_FORMAT_R24G8_TYPELESS,
DXGI_FORMAT_D24_UNORM_S8_UINT,
DXGI_FORMAT_R24_UNORM_X8_TYPELESS,
DXGI_FORMAT_X24_TYPELESS_G8_UINT,
DXGI_FORMAT_R8G8_TYPELESS,
DXGI_FORMAT_R8G8_UNORM,
DXGI_FORMAT_R8G8_UINT,
DXGI_FORMAT_R8G8_SNORM,
DXGI_FORMAT_R8G8_SINT,
DXGI_FORMAT_R16_TYPELESS,
DXGI_FORMAT_R16_FLOAT,
DXGI_FORMAT_D16_UNORM,
DXGI_FORMAT_R16_UNORM,
DXGI_FORMAT_R16_UINT,
DXGI_FORMAT_R16_SNORM,
DXGI_FORMAT_R16_SINT,
DXGI_FORMAT_R8_TYPELESS,
DXGI_FORMAT_R8_UNORM,
DXGI_FORMAT_R8_UINT,
DXGI_FORMAT_R8_SNORM,
DXGI_FORMAT_R8_SINT,
DXGI_FORMAT_A8_UNORM,
DXGI_FORMAT_R1_UNORM,
DXGI_FORMAT_R9G9B9E5_SHAREDEXP,
DXGI_FORMAT_R8G8_B8G8_UNORM,
DXGI_FORMAT_G8R8_G8B8_UNORM,
DXGI_FORMAT_BC1_TYPELESS,
DXGI_FORMAT_BC1_UNORM,
DXGI_FORMAT_BC1_UNORM_SRGB,
DXGI_FORMAT_BC2_TYPELESS,
DXGI_FORMAT_BC2_UNORM,
DXGI_FORMAT_BC2_UNORM_SRGB,
DXGI_FORMAT_BC3_TYPELESS,
DXGI_FORMAT_BC3_UNORM,
DXGI_FORMAT_BC3_UNORM_SRGB,
DXGI_FORMAT_BC4_TYPELESS,
DXGI_FORMAT_BC4_UNORM,
DXGI_FORMAT_BC4_SNORM,
DXGI_FORMAT_BC5_TYPELESS,
DXGI_FORMAT_BC5_UNORM,
DXGI_FORMAT_BC5_SNORM,
DXGI_FORMAT_B5G6R5_UNORM,
DXGI_FORMAT_B5G5R5A1_UNORM,
DXGI_FORMAT_B8G8R8A8_UNORM,
DXGI_FORMAT_B8G8R8X8_UNORM,
DXGI_FORMAT_R10G10B10_XR_BIAS_A2_UNORM,
DXGI_FORMAT_B8G8R8A8_TYPELESS,
DXGI_FORMAT_B8G8R8A8_UNORM_SRGB,
DXGI_FORMAT_B8G8R8X8_TYPELESS,
DXGI_FORMAT_B8G8R8X8_UNORM_SRGB,
DXGI_FORMAT_BC6H_TYPELESS,
DXGI_FORMAT_BC6H_UF16,
DXGI_FORMAT_BC6H_SF16,
DXGI_FORMAT_BC7_TYPELESS,
DXGI_FORMAT_BC7_UNORM,
DXGI_FORMAT_BC7_UNORM_SRGB,
DXGI_FORMAT_AYUV,
DXGI_FORMAT_Y410,
DXGI_FORMAT_Y416,
DXGI_FORMAT_NV12,
DXGI_FORMAT_P010,
DXGI_FORMAT_P016,
DXGI_FORMAT_420_OPAQUE,
DXGI_FORMAT_YUY2,
DXGI_FORMAT_Y210,
DXGI_FORMAT_Y216,
DXGI_FORMAT_NV11,
DXGI_FORMAT_AI44,
DXGI_FORMAT_IA44,
DXGI_FORMAT_P8,
DXGI_FORMAT_A8P8,
DXGI_FORMAT_B4G4R4A4_UNORM,
DXGI_FORMAT_P208,
DXGI_FORMAT_V208,
DXGI_FORMAT_V408
);
TDDSHeader = packed record
Magic: TMagic4;
dwSize: Cardinal;
dwFlags: Cardinal;
dwHeight: Cardinal;
dwWidth: Cardinal;
dwPitchOrLinearSize: Cardinal;
dwDepth: Cardinal;
dwMipMapCount: Cardinal;
dwReserved1: array [0..10] of Cardinal;
ddspf: packed record
dwSize: Cardinal;
dwFlags: Cardinal;
dwFourCC: TMagic4;
dwRGBBitCount: Cardinal;
dwRBitMask: Cardinal;
dwGBitMask: Cardinal;
dwBBitMask: Cardinal;
dwABitMask: Cardinal;
end;
dwCaps: Cardinal;
dwCaps2: Cardinal;
dwCaps3: Cardinal;
dwCaps4: Cardinal;
dwReserved2: Cardinal;
end;
PDDSHeader = ^TDDSHeader;
TDDSHeaderDX10 = packed record
dxgiFormat: Integer;
resourceDimension: Cardinal;
miscFlags: Cardinal;
arraySize: Cardinal;
miscFlags2: Cardinal;
end;
PDDSHeaderDX10 = ^TDDSHeaderDX10;
TwbBSArchive = class;
TBSArchiveType = (baNone, baTES3, baTES4, baFO3, baSSE, baFO4, baFO4dds);
TBSArchiveState = (stReading, stWriting);
TBSArchiveStates = set of TBSArchiveState;
TBSFileIterationProcCompat = function(aArchive: Pointer; const aFileName: PChar;
aFileRecord: Pointer; aFolderRecord: Pointer; aContext: Pointer): Boolean; {$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};
TDDSInfo = record Width, Height, MipMaps: Integer; end;
TBSFileDDSInfoProcCompat = procedure(aArchive: Pointer; const aFileName: PChar;
var aInfo: TDDSInfo); {$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};
TwbBSHeaderTES3 = packed record
HashOffset: Cardinal;
FileCount: Cardinal;
end;
TwbBSFileTES3 = record
Hash: UInt64;
Size: Cardinal;
Offset: Cardinal;
Name: string;
end;
PwbBSFileTES3 = ^TwbBSFileTES3;
TwbBSHeaderTES4 = packed record
FoldersOffset: Cardinal;
Flags: Cardinal;
FolderCount: Cardinal;
FileCount: Cardinal;
FolderNamesLength: Cardinal;
FileNamesLength: Cardinal;
FileFlags: Cardinal;
end;
TwbBSFileTES4 = record
Hash: UInt64;
Size: Cardinal;
Offset: Int64;
Name: string;
function Compressed(bsa: TwbBSArchive): Boolean;
function RawSize: Cardinal;
end;
PwbBSFileTES4 = ^TwbBSFileTES4;
TwbBSFolderTES4 = record
Hash: UInt64;
FileCount: Cardinal;
Unk32: Cardinal;
Offset: Int64;
Name: string;
Files: array of TwbBSFileTES4;
end;
PwbBSFolderTES4 = ^TwbBSFolderTES4;
TwbBSHeaderFO4 = packed record
Magic: TMagic4;
FileCount: Cardinal;
FileTableOffset: Int64;
end;
TwbBSTexChunkRec = record
Size : Cardinal;
PackedSize : Cardinal;
Offset : Int64;
StartMip : Word;
EndMip : Word;
end;
PwbBSTexChunkRec = ^TwbBSTexChunkRec;
TwbBSFileFO4 = record
NameHash: Cardinal;
Ext: TMagic4;
DirHash: Cardinal;
// GNRL archive format
Unknown: Cardinal;
Offset: Int64;
PackedSize: Cardinal;
Size: Cardinal;
//
// DX10 archive format
UnknownTex : Byte;
//ChunkHeaderSize: Word;
Height : Word;
Width : Word;
NumMips : Byte;
DXGIFormat : Byte;
CubeMaps : Word;
TexChunks : array of TwbBSTexChunkRec;
//
Name: string;
function DXGIFormatName: string;
end;
PwbBSFileFO4 = ^TwbBSFileFO4;
TPackedDataHash = TMD5Digest;
TPackedDataInfo = record
Size: Cardinal;
Hash: TPackedDataHash;
FileRecord: Pointer;
end;
PPackedDataInfo = ^TPackedDataInfo;
TwbBSFileDataResult = record
Size: Cardinal;
Data: PByte;
end;
TwbBSEntryList = class(TStringList);
TwbBSArchive = class
private
fStream: TwbBaseCachedFileStream;
fStates: TBSArchiveStates;
fType: TBSArchiveType;
fFileName: string;
fMagic: TMagic4;
fVersion: Cardinal;
fCompress: Boolean;
fShareData: Boolean;
fDDSInfoProc: TBSFileDDSInfoProcCompat;
fHeaderTES3: TwbBSHeaderTES3;
fFilesTES3: array of TwbBSFileTES3;
fHeaderTES4: TwbBSHeaderTES4;
fFoldersTES4: array of TwbBSFolderTES4;
fHeaderFO4: TwbBSHeaderFO4;
fFilesFO4: array of TwbBSFileFO4;
fMaxChunkCount: Integer;
fSingleMipChunkX: Integer;
fSingleMipChunkY: Integer;
fDataOffset: Int64;
fPackedData: array of TPackedDataInfo;
fPackedDataCount: Integer;
function GetArchiveFormatName: string;
function GetFileCount: Cardinal;
procedure SetArchiveFlags(aFlags: Cardinal);
function FindFileRecordTES3(const aFileName: string; var aFileIdx: Integer): Boolean;
function FindFileRecordTES4(const aFileName: string; var aFolderIdx, aFileIdx: Integer): Boolean;
function FindFileRecordFO4(const aFileName: string; var aFileIdx: Integer): Boolean;
function GetDDSMipChunkNum(var aDDSInfo: TDDSInfo): Integer;
function CalcDataHash(aData: Pointer; aLen: Cardinal): TPackedDataHash;
function FindPackedData(aSize: Cardinal; aHash: TPackedDataHash; aFileRecord: Pointer): Boolean;
procedure AddPackedData(aSize: Cardinal; aHash: TPackedDataHash; aFileRecord: Pointer);
public
Sync: IReadWriteSync;
constructor Create;
destructor Destroy; override;
procedure LoadFromFile(const aFileName: string);
procedure CreateArchiveCompat(const aFileName: string; aType: TBSArchiveType;
aFilesList: TwbBSEntryList = nil);
procedure Save;
procedure AddFile(const aRootDir, aFileName: string); overload;
procedure AddFileCompat(const aFileName: string; const aSize: Cardinal; const aData: PByte);
function FindFileRecord(const aFileName: string): Pointer;
function ExtractFileDataCompat(aFileRecord: Pointer): TwbBSFileDataResult; overload;
function ExtractFileDataCompat(const aFileName: string): TwbBSFileDataResult; overload;
procedure ReleaseFileDataCompat(fileDataResult: TwbBSFileDataResult);
procedure ExtractFile(const aFileName, aSaveAs: string);
procedure IterateFilesCompat(aProc: TBSFileIterationProcCompat; aContext: Pointer = nil);
function FileExists(const aFileName: string): Boolean;
procedure ResourceListCompat(const aEntryResultList: TwbBSEntryList; aFolder: string = '');
procedure ResolveHashCompat(const aHash: UInt64; const aEntryResultList: TwbBSEntryList);
procedure Close;
property FileName: string read fFileName;
property ArchiveType: TBSArchiveType read fType;
property Version: Cardinal read fVersion;
property FormatName: string read GetArchiveFormatName;
property FileCount: Cardinal read GetFileCount;
property ArchiveFlags: Cardinal read fHeaderTES4.Flags write SetArchiveFlags;
property FileFlags: Cardinal read fHeaderTES4.FileFlags write fHeaderTES4.FileFlags;
property Compress: Boolean read fCompress write fCompress;
property ShareData: Boolean read fShareData write fShareData;
property DDSInfoProc: TBSFileDDSInfoProcCompat read fDDSInfoProc write fDDSInfoProc;
end;
function SplitDirName(const aFileName: string; var Dir, Name: string): Integer;
function SplitNameExt(const aFileName: string; var Name, Ext: string): Integer;
function CreateHashTES3(const aFileName: string): UInt64;
function CreateHashTES4(const aFileName: string): UInt64; overload;
function CreateHashFO4(const aFileName: string): Cardinal;
implementation
uses
TypInfo,
zlibEx,
lz4io;
const
TBSArchiveFormatName: array[TBSArchiveType] of string = (
'None',
'Morrowind',
'Oblivion',
'Fallout 3, New Vegas, Skyrim LE',
'Skyrim Special Edition',
'Fallout 4 General',
'Fallout 4 DDS'
);
MAGIC_TES3: TMagic4 = #0#1#0#0;
MAGIC_BSA : TMagic4 = 'BSA'#0;
MAGIC_BTDX: TMagic4 = 'BTDX';
MAGIC_GNRL: TMagic4 = 'GNRL';
MAGIC_DX10: TMagic4 = 'DX10';
MAGIC_DDS: TMagic4 = 'DDS ';
MAGIC_DXT1: TMagic4 = 'DXT1';
MAGIC_DXT3: TMagic4 = 'DXT3';
MAGIC_DXT5: TMagic4 = 'DXT5';
MAGIC_ATI1: TMagic4 = 'ATI1';
MAGIC_ATI2: TMagic4 = 'ATI2';
MAGIC_BC4S: TMagic4 = 'BC4S';
MAGIC_BC4U: TMagic4 = 'BC4U';
MAGIC_BC5S: TMagic4 = 'BC5S';
MAGIC_BC5U: TMagic4 = 'BC5U';
iFileFO4Unknown = $00100100;
iFileFO4Tail = $BAADF00D;
{ https://github.com/jonwd7/bae/blob/master/src/bsa.h }
// header versions
HEADER_VERSION_TES4 = $67; // Oblivion
HEADER_VERSION_FO3 = $68; // FO3, FNV, TES5
HEADER_VERSION_SSE = $69; // SSE
HEADER_VERSION_FO4 = $01; // FO4
// archive flags
ARCHIVE_PATHNAMES = $0001; // Whether the BSA has names for paths
ARCHIVE_FILENAMES = $0002; // Whether the BSA has names for files
ARCHIVE_COMPRESS = $0004; // Whether the files are compressed in archive (invert file's compression flag)
ARCHIVE_RETAINDIR = $0008;
ARCHIVE_RETAINNAME = $0010;
ARCHIVE_RETAINFOFF = $0020;
ARCHIVE_XBOX360 = $0040;
ARCHIVE_STARTUPSTR = $0080;
ARCHIVE_EMBEDNAME = $0100; // Whether the name is prefixed to the data
ARCHIVE_XMEM = $0200;
ARCHIVE_UNKNOWN10 = $0400;
// file flags
FILE_NIF = $0001;
FILE_DDS = $0002;
FILE_XML = $0004;
FILE_WAV = $0008;
FILE_MP3 = $0010;
FILE_TXT = $0020; // TXT, HTML, BAT, SCC
FILE_SPT = $0040;
FILE_FNT = $0080; // TEX, FNT
FILE_MISC = $0100; // CTL and others
FILE_SIZE_COMPRESS = $40000000; // Whether the file is compressed
DDSD_CAPS = $00000001;
DDSD_HEIGHT = $00000002;
DDSD_WIDTH = $00000004;
DDSD_PITCH = $00000008;
DDSD_PIXELFORMAT = $00001000;
DDSD_MIPMAPCOUNT = $00020000;
DDSD_LINEARSIZE = $00080000;
DDSD_DEPTH = $00800000;
DDSCAPS_COMPLEX = $00000008;
DDSCAPS_TEXTURE = $00001000;
DDSCAPS_MIPMAP = $00400000;
DDSCAPS2_CUBEMAP = $00000200;
DDSCAPS2_POSITIVEX = $00000400;
DDSCAPS2_NEGATIVEX = $00000800;
DDSCAPS2_POSITIVEY = $00001000;
DDSCAPS2_NEGATIVEY = $00002000;
DDSCAPS2_POSITIVEZ = $00004000;
DDSCAPS2_NEGATIVEZ = $00008000;
DDSCAPS2_VOLUME = $00200000;
DDPF_ALPHAPIXELS = $00000001;
DDPF_ALPHA = $00000002;
DDPF_FOURCC = $00000004;
DDPF_RGB = $00000040;
DDPF_YUV = $00000200;
DDPF_LUMINANCE = $00020000;
// DX10
DDS_DIMENSION_TEXTURE2D = $00000003;
DDS_RESOURCE_MISC_TEXTURECUBE = $00000004;
crc32table : array [0..255] of Cardinal = (
$00000000, $77073096, $ee0e612c, $990951ba, $076dc419, $706af48f,
$e963a535, $9e6495a3, $0edb8832, $79dcb8a4, $e0d5e91e, $97d2d988,
$09b64c2b, $7eb17cbd, $e7b82d07, $90bf1d91, $1db71064, $6ab020f2,
$f3b97148, $84be41de, $1adad47d, $6ddde4eb, $f4d4b551, $83d385c7,
$136c9856, $646ba8c0, $fd62f97a, $8a65c9ec, $14015c4f, $63066cd9,
$fa0f3d63, $8d080df5, $3b6e20c8, $4c69105e, $d56041e4, $a2677172,
$3c03e4d1, $4b04d447, $d20d85fd, $a50ab56b, $35b5a8fa, $42b2986c,
$dbbbc9d6, $acbcf940, $32d86ce3, $45df5c75, $dcd60dcf, $abd13d59,
$26d930ac, $51de003a, $c8d75180, $bfd06116, $21b4f4b5, $56b3c423,
$cfba9599, $b8bda50f, $2802b89e, $5f058808, $c60cd9b2, $b10be924,
$2f6f7c87, $58684c11, $c1611dab, $b6662d3d, $76dc4190, $01db7106,
$98d220bc, $efd5102a, $71b18589, $06b6b51f, $9fbfe4a5, $e8b8d433,
$7807c9a2, $0f00f934, $9609a88e, $e10e9818, $7f6a0dbb, $086d3d2d,
$91646c97, $e6635c01, $6b6b51f4, $1c6c6162, $856530d8, $f262004e,
$6c0695ed, $1b01a57b, $8208f4c1, $f50fc457, $65b0d9c6, $12b7e950,
$8bbeb8ea, $fcb9887c, $62dd1ddf, $15da2d49, $8cd37cf3, $fbd44c65,
$4db26158, $3ab551ce, $a3bc0074, $d4bb30e2, $4adfa541, $3dd895d7,
$a4d1c46d, $d3d6f4fb, $4369e96a, $346ed9fc, $ad678846, $da60b8d0,
$44042d73, $33031de5, $aa0a4c5f, $dd0d7cc9, $5005713c, $270241aa,
$be0b1010, $c90c2086, $5768b525, $206f85b3, $b966d409, $ce61e49f,
$5edef90e, $29d9c998, $b0d09822, $c7d7a8b4, $59b33d17, $2eb40d81,
$b7bd5c3b, $c0ba6cad, $edb88320, $9abfb3b6, $03b6e20c, $74b1d29a,
$ead54739, $9dd277af, $04db2615, $73dc1683, $e3630b12, $94643b84,
$0d6d6a3e, $7a6a5aa8, $e40ecf0b, $9309ff9d, $0a00ae27, $7d079eb1,
$f00f9344, $8708a3d2, $1e01f268, $6906c2fe, $f762575d, $806567cb,
$196c3671, $6e6b06e7, $fed41b76, $89d32be0, $10da7a5a, $67dd4acc,
$f9b9df6f, $8ebeeff9, $17b7be43, $60b08ed5, $d6d6a3e8, $a1d1937e,
$38d8c2c4, $4fdff252, $d1bb67f1, $a6bc5767, $3fb506dd, $48b2364b,
$d80d2bda, $af0a1b4c, $36034af6, $41047a60, $df60efc3, $a867df55,
$316e8eef, $4669be79, $cb61b38c, $bc66831a, $256fd2a0, $5268e236,
$cc0c7795, $bb0b4703, $220216b9, $5505262f, $c5ba3bbe, $b2bd0b28,
$2bb45a92, $5cb36a04, $c2d7ffa7, $b5d0cf31, $2cd99e8b, $5bdeae1d,
$9b64c2b0, $ec63f226, $756aa39c, $026d930a, $9c0906a9, $eb0e363f,
$72076785, $05005713, $95bf4a82, $e2b87a14, $7bb12bae, $0cb61b38,
$92d28e9b, $e5d5be0d, $7cdcefb7, $0bdbdf21, $86d3d2d4, $f1d4e242,
$68ddb3f8, $1fda836e, $81be16cd, $f6b9265b, $6fb077e1, $18b74777,
$88085ae6, $ff0f6a70, $66063bca, $11010b5c, $8f659eff, $f862ae69,
$616bffd3, $166ccf45, $a00ae278, $d70dd2ee, $4e048354, $3903b3c2,
$a7672661, $d06016f7, $4969474d, $3e6e77db, $aed16a4a, $d9d65adc,
$40df0b66, $37d83bf0, $a9bcae53, $debb9ec5, $47b2cf7f, $30b5ffe9,
$bdbdf21c, $cabac28a, $53b39330, $24b4a3a6, $bad03605, $cdd70693,
$54de5729, $23d967bf, $b3667a2e, $c4614ab8, $5d681b02, $2a6f2b94,
$b40bbe37, $c30c8ea1, $5a05df1b, $2d02ef8d
);
type
TPreallocatedMemoryStream = class(TCustomMemoryStream)
public
constructor Create(Ptr: Pointer; Size: Int64);
function Write(const Buffer; Count: Longint): Longint; override;
end;
constructor TPreallocatedMemoryStream.Create(Ptr: Pointer; Size: Int64);
begin
inherited Create;
SetPointer(Ptr, Size);
end;
function TPreallocatedMemoryStream.Write(const Buffer; Count: Integer): Longint;
begin
Result := Size-Position;
if Result > Count then
Result := Count;
System.Move(Buffer, Pointer(PByte(Memory) + Position)^, Result);
Seek(Result, soCurrent);
end;
function Magic2Int(aMagic: TMagic4): Cardinal; inline;
begin
Result := PCardinal(@aMagic)^;
end;
function Int2Magic(aInt: Cardinal): TMagic4; inline;
begin
Result := PMagic4(@aInt)^;
end;
function String2Magic(const aStr: string): TMagic4;
begin
Result := #0#0#0#0;
if Length(aStr) > 0 then Result[0] := AnsiChar(aStr[1]);
if Length(aStr) > 1 then Result[1] := AnsiChar(aStr[2]);
if Length(aStr) > 2 then Result[2] := AnsiChar(aStr[3]);
if Length(aStr) > 3 then Result[3] := AnsiChar(aStr[4]);
end;
function LowerByte(ch: AnsiChar): Byte; inline;
begin
case ch of
'A'..'Z':
Result := Byte(Ord(ch) + Ord('a')-Ord('A'));
else
Result := Byte(ch);
end;
end;
function LastCharPos(const s: string; const Chr: char): Integer; inline;
begin
for Result := Length(s) downto 1 do
if s[Result] = Chr then
Exit;
Result := 0;
end;
function Str2MagicInt(const s: string): Cardinal;
var
i: integer;
begin
Result := 0;
for i := 1 to Length(s) do begin
PByte(PByte(@Result) + i-1)^ := LowerByte(AnsiChar(s[i]));
if i = 4 then Break;
end;
end;
function SplitDirName(const aFileName: string; var Dir, Name: string): Integer;
begin
Result := LastCharPos(aFileName, '\');
if Result = 0 then
Result := LastCharPos(aFileName, '/');
Dir := Copy(aFileName, 1, Pred(Result));
Name := Copy(aFileName, Succ(Result), Length(aFileName) - Result);
end;
function SplitNameExt(const aFileName: string; var Name, Ext: string): Integer;
begin
Result := LastCharPos(aFileName, '.');
if Result <> 0 then begin
Name := Copy(aFileName, 1, Pred(Result));
Ext := Copy(aFileName, Result, Length(aFileName) - Result + 2);
end
else begin
Name := aFileName;
Ext := '';
end;
end;
function CreateHashTES3(const aFileName: string): UInt64;
var
s: AnsiString;
i, l: integer;
sum, off, temp, n: Cardinal;
begin
s := AnsiString(aFileName);
l := Length(s) shr 1;
sum := 0; off := 0;
for i := 1 to l do begin
temp := Cardinal(LowerByte(s[i])) shl (off and $1F);
sum := sum xor temp;
off := off + 8;
end;
Result := UInt64(sum) shl 32;
sum := 0; off := 0;
for i := l + 1 to Length(s) do begin
temp := Cardinal(LowerByte(s[i])) shl (off and $1F);
sum := sum xor temp;
n := temp and $1F;
sum := (sum shr n) or (sum shl (32 - n));
off := off + 8;
end;
Result := Result or sum;
end;
function CreateHashTES4(const aName, aExt: string): UInt64; overload;
var
i, l: integer;
hash: Cardinal;
ext: array [0..3] of Byte;
s, e: AnsiString;
begin
Result := 0;
s := AnsiString(aName);
e := AnsiString(aExt);
l := Length(s);
if l = 0 then
Exit;
Result := LowerByte(s[l]);
if l > 2 then
Result := Result or (Cardinal(LowerByte(s[l-1])) shl 8);
Result := Result or (l shl 16);
Result := Result or (Cardinal(LowerByte(s[1])) shl 24);
PCardinal(@ext)^ := 0;
for i := 1 to Length(e) do begin
ext[i-1] := LowerByte(e[i]);
if i = 4 then Break;
end;
case PCardinal(@ext)^ of
$00666B2E: Result := Result or $80; // .kf
$66696E2E: Result := Result or $8000; // .nif
$7364642E: Result := Result or $8080; // .dds
$7661772E: Result := Result or $80000000; // .wav
end;
hash := 0;
for i := 2 to l-2 do
hash := LowerByte(s[i]) + (hash shl 6) + (hash shl 16) - hash;
Result := Result + UInt64(hash) shl 32;
hash := 0;
for i := 1 to Length(e) do
hash := LowerByte(e[i]) + (hash shl 6) + (hash shl 16) - hash;
Result := Result + UInt64(hash) shl 32;
end;
function CreateHashTES4(const aFileName: string): UInt64; overload;
var
fname, fext: string;
begin
SplitNameExt(aFileName, fname, fext);
Result := CreateHashTES4(fname, fext);
end;
function CreateHashFO4(const aFileName: string): Cardinal;
var
i: Integer;
s: AnsiString;
c: AnsiChar;
begin
Result := 0;
s := AnsiString(aFileName);
for i := 1 to Length(s) do begin
c := s[i];
if Byte(c) > 127 then Continue;
if c = '/' then c := '\';
Result := (Result shr 8) xor crc32table[(Result xor LowerByte(c)) and $FF];
end;
end;
function TwbBSFileTES4.Compressed(bsa: TwbBSArchive): Boolean;
begin
Result := (bsa.ArchiveFlags and ARCHIVE_COMPRESS <> 0) xor (Size and FILE_SIZE_COMPRESS <> 0);
end;
function TwbBSFileTES4.RawSize: Cardinal;
begin
Result := Size and not FILE_SIZE_COMPRESS;
end;
function TwbBSFileFO4.DXGIFormatName: string;
begin
Result := GetEnumName(TypeInfo(TDXGI), Integer(DXGIFormat));
end;
{ TwbBSArchive }
constructor TwbBSArchive.Create;
begin
fType := baNone;
fMaxChunkCount := 4;
fSingleMipChunkX := 512;
fSingleMipChunkY := 512;
end;
destructor TwbBSArchive.Destroy;
begin
if fStates * [stReading, stWriting] <> [] then
Close;
end;
function TwbBSArchive.GetArchiveFormatName: string;
begin
Result := TBSArchiveFormatName[fType];
end;
function TwbBSArchive.GetFileCount: Cardinal;
begin
case fType of
baTES3:
Result := fHeaderTES3.FileCount;
baTES4, baFO3, baSSE:
Result := fHeaderTES4.FileCount;
baFO4, baFO4dds:
// because we use fHeaderFO4.FileCount as the current index in new archives
Result := Length(fFilesFO4);
else
Result := 0;
end;
end;
procedure TwbBSArchive.SetArchiveFlags(aFlags: Cardinal);
begin
if not (fType in [baTES4, baFO3, baSSE]) then
raise Exception.Create('Archive flags are not supported for this archive type');
fHeaderTES4.Flags := aFlags;
// force compression flag if needed
if fCompress then
fHeaderTES4.Flags := fHeaderTES4.Flags or ARCHIVE_COMPRESS;
end;
function TwbBSArchive.FindFileRecordTES3(const aFileName: string; var aFileIdx: Integer): Boolean;
var
h: UInt64;
i: integer;
begin
h := CreateHashTES3(aFileName);
Result := False;
for i := Low(fFilesTES3) to High(fFilesTES3) do
if fFilesTES3[i].Hash = h then begin
aFileIdx := i;
Result := True;
Exit;
end;
end;
function TwbBSArchive.FindFileRecordTES4(const aFileName: string; var aFolderIdx, aFileIdx: Integer): Boolean;
var
fdir, fname, name, ext: string;
h: UInt64;
i, j: integer;
begin
if SplitDirName(aFileName, fdir, fname) = 0 then
Exit(False);
Result := False;
h := CreateHashTES4(fdir, '');
for i := Low(fFoldersTES4) to High(fFoldersTES4) do begin
if h <> fFoldersTES4[i].Hash then
// since table is sorted by hash, we can abort when our hash is lesser
if h < fFoldersTES4[i].Hash then
Exit
else
Continue;
SplitNameExt(fname, name, ext);
h := CreateHashTES4(name, ext);
for j := Low(fFoldersTES4[i].Files) to High(fFoldersTES4[i].Files) do begin
if h <> fFoldersTES4[i].Files[j].Hash then
if h < fFoldersTES4[i].Files[j].Hash then
Exit
else
Continue;
Result := True;
aFolderIdx := i;
aFileIdx := j;
Exit;
end;
end;
end;
function TwbBSArchive.FindFileRecordFO4(const aFileName: string; var aFileIdx: Integer): Boolean;
var
fdir, fname, name, ext: string;
hdir, hfile: Cardinal;
hext: TMagic4;
i: integer;
begin
if SplitDirName(aFileName, fdir, fname) = 0 then
Exit(False);
SplitNameExt(fname, name, ext);
hdir := CreateHashFO4(fdir);
hfile := CreateHashFO4(name);
if Copy(ext, 1, 1) = '.' then
Delete(ext, 1, 1);
hext := String2Magic(LowerCase(ext));
Result := False;
for i := Low(fFilesFO4) to High(fFilesFO4) do
if (fFilesFO4[i].DirHash = hdir) and (fFilesFO4[i].NameHash = hfile) and (fFilesFO4[i].Ext = hext) then begin
aFileIdx := i;
Result := True;
Exit;
end;
end;
function TwbBSArchive.FindFileRecord(const aFileName: string): Pointer;
var
i, j: integer;
begin
Result := nil;
case fType of
baTES3:
if FindFileRecordTES3(aFileName, i) then Result := @fFilesTES3[i];
baTES4, baFO3, baSSE:
if FindFileRecordTES4(aFileName, i, j) then Result := @fFoldersTES4[i].Files[j];
baFO4, baFO4dds:
if FindFileRecordFO4(aFileName, i) then Result := @fFilesFO4[i];
end;
end;
function TwbBSArchive.GetDDSMipChunkNum(var aDDSInfo: TDDSInfo): Integer;
var
w, h: Integer;
begin
w := aDDSInfo.Width;
h := aDDSInfo.Height;
Result := 1;
while (Result < aDDSInfo.MipMaps) and
(Result < fMaxChunkCount) and
(w >= fSingleMipChunkX) and
(h >= fSingleMipChunkY)
do begin
Inc(Result);
w := w div 2;
h := h div 2;
end;
end;
function TwbBSArchive.CalcDataHash(aData: Pointer; aLen: Cardinal): TPackedDataHash;
var
fMD5: TMD5Alg;
begin
fMD5.Init(@fMD5);
fMD5.Update(@fMD5, aData, aLen);
fMD5.Done(@fMD5, @Result);
end;
function TwbBSArchive.FindPackedData(aSize: Cardinal; aHash: TPackedDataHash; aFileRecord: Pointer): Boolean;
var
i: Integer;
begin
Result := False;
if not fShareData then
Exit;
for i := 0 to Pred(fPackedDataCount) do
if (aSize = fPackedData[i].Size) and CompareMem(@aHash, @fPackedData[i].Hash, SizeOf(aHash)) then begin
case fType of
baTES3: begin
PwbBSFileTES3(aFileRecord).Size := PwbBSFileTES3(fPackedData[i].FileRecord).Size;
PwbBSFileTES3(aFileRecord).Offset := PwbBSFileTES3(fPackedData[i].FileRecord).Offset;
end;
baTES4, baFO3, baSSE: begin
PwbBSFileTES4(aFileRecord).Size := PwbBSFileTES4(fPackedData[i].FileRecord).Size;
PwbBSFileTES4(aFileRecord).Offset := PwbBSFileTES4(fPackedData[i].FileRecord).Offset;
end;
baFO4: begin
PwbBSFileFO4(aFileRecord).Size := PwbBSFileFO4(fPackedData[i].FileRecord).Size;
PwbBSFileFO4(aFileRecord).PackedSize := PwbBSFileFO4(fPackedData[i].FileRecord).PackedSize;
PwbBSFileFO4(aFileRecord).Offset := PwbBSFileFO4(fPackedData[i].FileRecord).Offset;
end;
baFO4dds: begin
PwbBSTexChunkRec(aFileRecord).Size := PwbBSTexChunkRec(fPackedData[i].FileRecord).Size;
PwbBSTexChunkRec(aFileRecord).PackedSize := PwbBSTexChunkRec(fPackedData[i].FileRecord).PackedSize;
PwbBSTexChunkRec(aFileRecord).Offset := PwbBSTexChunkRec(fPackedData[i].FileRecord).Offset;
end;
end;
Result := True;
Exit;
end;
end;
procedure TwbBSArchive.AddPackedData(aSize: Cardinal; aHash: TPackedDataHash; aFileRecord: Pointer);
begin
if not fShareData then
Exit;
if fPackedDataCount = Length(fPackedData) then
if Length(fPackedData) = 0 then
SetLength(fPackedData, 2048)
else
SetLength(fPackedData, Length(fPackedData) * 2);
fPackedData[fPackedDataCount].Size := aSize;
fPackedData[fPackedDataCount].Hash := aHash;
fPackedData[fPackedDataCount].FileRecord := aFileRecord;
Inc(fPackedDataCount);
end;
procedure TwbBSArchive.LoadFromFile(const aFileName: string);
var
i, j: Integer;
begin
if fStates * [stReading, stWriting] <> [] then
Close;
fStream := TwbReadOnlyCachedFileStream.Create(aFileName, fmOpenRead or fmShareDenyWrite);
// magic
fMagic := Int2Magic(fStream.ReadCardinal);
if fMagic = MAGIC_TES3 then fType := baTES3 else
if fMagic = MAGIC_BSA then fType := baTES4 else
if fMagic = MAGIC_BTDX then fType := baFO4 else
raise Exception.Create('Unknown archive format');
// archive version except Morrowind
if fType <> baTES3 then begin
fVersion := fStream.ReadCardinal;
case fVersion of
HEADER_VERSION_TES4: fType := baTES4;
HEADER_VERSION_FO3 : fType := baFO3;
HEADER_VERSION_SSE : fType := baSSE;
HEADER_VERSION_FO4 : fType := baFO4;
else
raise Exception.Create('Unknown archive version 0x' + IntToHex(fVersion, 8));
end;
end;
case fType of
//--------------------------------------------------
// Morrowind
baTES3: begin
// read header
fStream.ReadBuffer(fHeaderTES3, SizeOf(fHeaderTES3));
SetLength(fFilesTES3, fHeaderTES3.FileCount);
for i := Low(fFilesTES3) to High(fFilesTES3) do begin
fFilesTES3[i].Size := fStream.ReadCardinal;
fFilesTES3[i].Offset := fStream.ReadCardinal;
end;
// skip name offsets
fStream.Position := fStream.Position + 4 * fHeaderTES3.FileCount;
// read names
for i := Low(fFilesTES3) to High(fFilesTES3) do
fFilesTES3[i].Name := fStream.ReadStringTerm;
// read hashes
for i := Low(fFilesTES3) to High(fFilesTES3) do