-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathuDFExplorer_PKGManager.pas
424 lines (359 loc) · 11.9 KB
/
uDFExplorer_PKGManager.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
{
******************************************************
DoubleFine Explorer
By Bennyboy
Http://quickandeasysoftware.net
******************************************************
}
{
This Source Code Form is subject to the terms of the Mozilla Public
License, v. 2.0. If a copy of the MPL was not distributed with this
file, You can obtain one at http://mozilla.org/MPL/2.0/.
}
{
PKG files from Psychonauts (all pc releases)
}
unit uDFExplorer_PKGManager;
interface
uses
classes, sysutils, Contnrs, forms,
uDFExplorer_BaseBundleManager, uFileReader, uMemReader, uDFExplorer_Types,
uDFExplorer_Funcs, uZlib;
type
TPKGManager = class (TBundleManager)
private
fBigEndian: boolean;
protected
fBundle: TExplorerFileStream;
fBundleFileName: string;
function DetectBundle: boolean; override;
function GetFilesCount: integer; override;
function GetFileName(Index: integer): string; override;
function GetFileSize(Index: integer): integer; override;
function GetFileOffset(Index: integer): int64; override;
function GetFileType(Index: integer): TFiletype; override;
function GetFileExtension(Index: integer): string; override;
procedure Log(Text: string); override;
procedure ParsePKGBundle;
public
BundleFiles: TObjectList;
constructor Create(ResourceFile: string); override;
destructor Destroy; override;
procedure ParseFiles; override;
procedure SaveFile(FileNo: integer; DestDir, FileName: string); override;
procedure SaveFileToStream(FileNo: integer; DestStream: TStream); override;
procedure SaveFiles(DestDir: string); override;
property Count: integer read GetFilesCount;
property FileName[Index: integer]: string read GetFileName;
property FileSize[Index: integer]: integer read GetFileSize;
property FileOffset[Index: integer]: int64 read GetFileOffset;
property FileType[Index: integer]: TFileType read GetFileType;
property FileExtension[Index: integer]: string read GetFileExtension;
property BigEndian: boolean read fBigEndian;
end;
const
strErrInvalidFile: string = 'Not a valid bundle';
implementation
{ TBundleManager }
constructor TPKGManager.Create(ResourceFile: string);
begin
try
fBundle:=TExplorerFileStream.Create(ResourceFile);
except on E: EInvalidFile do
raise;
end;
fBundleFileName:=ExtractFileName(ResourceFile);
BundleFiles:=TObjectList.Create(true);
if DetectBundle = false then
raise EInvalidFile.Create( strErrInvalidFile );
end;
destructor TPKGManager.Destroy;
begin
if BundleFiles <> nil then
begin
BundleFiles.Free;
BundleFiles:=nil;
end;
if fBundle <> nil then
fBundle.free;
inherited;
end;
function TPKGManager.DetectBundle: boolean;
var
BlockHeader: integer;
begin
Result := false;
BlockHeader := fBundle.ReadDWord;
if BlockHeader = 1196118106 then //ZPKG
begin
Result := true;
fBundle.BigEndian := false;
fBigEndian := false;
end
end;
function TPKGManager.GetFileExtension(Index: integer): string;
begin
if (not assigned(BundleFiles)) or
(index < 0) or
(index > GetFilesCount) then
result:= ''
else
result:=TDFFile(BundleFiles.Items[Index]).FileExtension;
end;
function TPKGManager.GetFileName(Index: integer): string;
begin
if (not assigned(BundleFiles)) or
(index < 0) or
(index > GetFilesCount) then
result:= ''
else
result:=TDFFile(BundleFiles.Items[Index]).FileName;
end;
function TPKGManager.GetFileOffset(Index: integer): int64;
begin
if (not assigned(BundleFiles)) or
(index < 0) or
(index > GetFilesCount) then
result:=0
else
result:=TDFFile(BundleFiles.Items[Index]).offset;
end;
function TPKGManager.GetFilesCount: integer;
begin
if BundleFiles <> nil then
result:=BundleFiles.Count
else
result:=0;
end;
function TPKGManager.GetFileSize(Index: integer): integer;
begin
if (not assigned(BundleFiles)) or
(index < 0) or
(index > GetFilesCount) then
result:=-1
else
result:=TDFFile(BundleFiles.Items[Index]).size;
end;
function TPKGManager.GetFileType(Index: integer): TFileType;
begin
if (not assigned(BundleFiles)) or
(index < 0) or
(index > GetFilesCount) then
result:= ft_Unknown
else
result:=TDFFile(BundleFiles.Items[Index]).FileType;
end;
procedure TPKGManager.Log(Text: string);
begin
if assigned(fOnDebug) then fOnDebug(Text);
end;
procedure TPKGManager.ParseFiles;
begin
if fBigEndian then //All LE reading auto converted to BE
Log('Detected as : big endian');
ParsePKGBundle;
end;
procedure TPKGManager.ParsePKGBundle;
var
NumFiles, NumDirs, FolderDirOffset, NameDirOffset, FileExtDirOffset,
FileExtensionOffset, FilenameOffset, i, j, OldPosition, ReturnPos1,
ReturnPos2, StartIndex, EndIndex, ThisID: integer;
FileObject: TDFFile;
CurrName: string;
Letter: char;
ReturnNames, PathNames: TStringList;
ReturnIDs, PathIDsStart, PathIDsEnd: array of integer;
begin
fBundle.Position:=0;
if fBundle.ReadBlockName <> 'ZPKG' then
begin
raise EInvalidFile.Create( strErrInvalidFile );
end;
//Read Header
fBundle.seek(4, sofromcurrent); //version
fBundle.seek(4, sofromcurrent); //first file offset
NumFiles := fBundle.readdword; //number of files
FolderDirOffset := fBundle.readdword; //offset of folder directory
NumDirs := fBundle.readdword; //number of records in folder directory
NameDirOffset := fBundle.readdword; //name dir offset
FileExtDirOffset := fBundle.readdword; //file extension dir offset
//Parse File Records
fBundle.Position := 512;
for I := 0 to NumFiles -1 do
begin
fBundle.seek(1, sofromcurrent); //null
FileExtensionOffset := fBundle.ReadWord; //offset in file ext dir
fBundle.seek(1, sofromcurrent); //null
FilenameOffset := fBundle.ReadDWord; //offset in filename dir
FileObject := TDFFile.Create;
FileObject.Compressed := false;
FileObject.CompressionType := 0;
FileObject.FileTypeIndex := -1;
//Now get filename and filetype
OldPosition := fBundle.Position;
fBundle.Position := FileExtensionOffset + FileExtDirOffset;
FileObject.FileExtension := Trim(PChar(fBundle.ReadString(5)));
//Need to add the . in
if FileObject.FileExtension <> '' then
FileObject.FileExtension := '.' + FileObject.FileExtension;
fBundle.Position := FilenameOffset + NameDirOffset;
FileObject.FileName := PChar(fBundle.ReadString(100)) + FileObject.FileExtension;
//Correct the file type
FileObject.FileType := GetFileTypeFromFileExtension( FileObject.FileExtension);
if (FileObject.FileType = ft_Unknown) and (FileObject.FileExtension <> '') then
Log('Unknown file type ' + FileObject.FileExtension);
fBundle.Position := OldPosition;
FileObject.Offset := fBundle.ReadDWord;
FileObject.Size := fBundle.ReadDWord;
FileObject.UncompressedSize := FileObject.Size; //Not compressed
BundleFiles.Add(FileObject);
end;
//Now parse the folder Dir. Its 12 bytes per record
//Algorithm by Watto https://github.com/bgbennyboy/DoubleFine-Explorer/issues/4
fBundle.Position := FolderDirOffset;
SetLength(ReturnIDs, NumDirs);
CurrName := '';
ReturnNames := TStringList.Create;
PathNames := TStringList.Create;
try
for i := 0 to NumDirs -1 do
begin
Letter := chr(fBundle.ReadByte); //One letter of a dir string
fBundle.Seek(1, soFromCurrent); //Null byte after the letter
ReturnPos1 := fBundle.ReadWord;
if ReturnPos1 <> 0 then
begin
ReturnNames.Add(CurrName);
ReturnIDs[ ReturnNames.Count -1 ] := returnPos1;
end;
ReturnPos2 := fBundle.ReadWord;
if ReturnPos2 <> 0 then
begin
ReturnNames.Add(CurrName);
ReturnIDs[ ReturnNames.Count -1 ] := returnPos2;
end;
CurrName := CurrName + Letter; //CurrName contains the dir string we are building
fBundle.Seek(2, soFromCurrent); //2 bytes - Character ID (incremental from 1)
StartIndex := fBundle.ReadWord; //2 bytes - First File ID in this Folder
EndIndex := fBundle.ReadWord; //2 bytes- Last File ID in this Folder
if (StartIndex <> 0) and (EndIndex <> 0) then
begin
PathNames.Add(CurrName);
SetLength(PathIDsStart, PathNames.Count); //messy way of doing this - todo have array of records instead
SetLength(PathIDsEnd, PathNames.Count);
PathIDsStart[PathNames.Count-1] := StartIndex;
PathIDsEnd[PathNames.Count-1] := EndIndex;
end;
//Process the returns now
ThisID := i + 1;
for j := 0 to ReturnNames.Count -1 do
begin
if ReturnIDs[j] = thisID then //Found one
begin
currName := ReturnNames.Strings[j];
//Shuffle the return arrays to remove this entry
if j <> ReturnNames.Count -1 then
begin
returnIDs[j] := returnIDs[ ReturnNames.Count -1];
returnNames[j] := returnNames[ ReturnNames.Count -1];
end;
end;
end;
end;
//Sanitise the directory strings
for i := 0 to pathNames.Count -1 do
begin
PathNames[i] := IncludeTrailingPathDelimiter(PathNames[i]);
PathNames[i] := StringReplace(PathNames[i], '/', '\', [rfReplaceAll])
end;
//Match the dir string to each file
for i := 0 to PathNames.Count -1 do
begin
//Get each folder string, then loop through the path IDs associated with it and append the dir to each name
for j := PathIDsStart[i]-1 to PathIDsEnd[i]-1 do //-1 because pathids start from 1 and our list starts from 0
TDFFile(BundleFiles[j]).FileName := pathNames[i] + TDFFile(BundleFiles[j]).FileName;
end;
finally
ReturnNames.Free;
PathNames.Free;
end;
if (Assigned(FOnDoneLoading)) then
FOnDoneLoading(NumFiles);
end;
procedure TPKGManager.SaveFile(FileNo: integer; DestDir, FileName: string);
var
SaveFile: TFileStream;
begin
if TDFFile(BundleFiles.Items[FileNo]).Size <= 0 then
begin
Log(strErrFileSize + FileName);
exit;
end;
if (FileNo < 0) or (FileNo > BundleFiles.Count) then
begin
Log(strErrFileNo);
exit;
end;
SaveFile:=tfilestream.Create(IncludeTrailingPathDelimiter(DestDir) + FileName,
fmOpenWrite or fmCreate);
try
SaveFileToStream(FileNo,SaveFile);
finally
SaveFile.Free;
end;
end;
procedure TPKGManager.SaveFiles(DestDir: string);
var
i: integer;
SaveFile: TFileStream;
begin
for I := 0 to BundleFiles.Count - 1 do
begin
ForceDirectories(extractfilepath(IncludeTrailingPathDelimiter(DestDir) +
ExtractPartialPath( TDFFile(BundleFiles.Items[i]).FileName)));
SaveFile:=TFileStream.Create(IncludeTrailingPathDelimiter(DestDir) +
TDFFile(BundleFiles.Items[i]).FileName , fmOpenWrite or fmCreate);
try
SaveFileToStream(i, SaveFile);
finally
SaveFile.free;
if Assigned(FOnProgress) then FOnProgress(GetFilesCount -1, i);
Application.Processmessages;
end;
end;
end;
procedure TPKGManager.SaveFileToStream(FileNo: integer; DestStream: TStream);
var
Ext: string;
TempStream: TMemoryStream;
begin
if TDFFile(BundleFiles.Items[FileNo]).Size <= 0 then
begin
Log(strErrFileSize + TDFFile(BundleFiles.Items[FileNo]).FileName);
exit;
end;
if (FileNo < 0) or (FileNo > BundleFiles.Count) then
begin
Log(strErrFileNo);
exit;
end;
Ext:=Uppercase(ExtractFileExt(TDFFile(BundleFiles.Items[FileNo]).FileName));
fBundle.Position := TDFFile(BundleFiles.Items[FileNo]).Offset;
if TDFFile(BundleFiles.Items[FileNo]).Compressed then
begin
TempStream := tmemorystream.Create;
try
TempStream.CopyFrom(fBundle, TDFFile(BundleFiles.Items[FileNo]).Size);
//tempstream.SaveToFile('c:\users\ben\desktop\testfile');
Tempstream.Position := 0;
DecompressZLib(TempStream, TDFFile(BundleFiles.Items[FileNo]).UnCompressedSize,
DestStream);
finally
TempStream.Free;
end
end
else
DestStream.CopyFrom(fBundle, TDFFile(BundleFiles.Items[FileNo]).Size);
DestStream.Position:=0;
end;
end.