-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathuDFExplorer_PPAKManager.pas
598 lines (506 loc) · 18.7 KB
/
uDFExplorer_PPAKManager.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
{
******************************************************
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/.
}
{
Psychonauts Pc Level pack files *.ppf
File format seems to have changed in the newer steam and gog releases.
UNFINISHED
}
unit uDFExplorer_PPAKManager;
interface
uses
classes, sysutils, Contnrs, forms, math, system.IOUtils,
uDFExplorer_BaseBundleManager, uFileReader, uMemReader, uDFExplorer_Types,
uDFExplorer_Funcs, uZlib,
RTTI;
type
TPPAKManager = 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;
function GetPsychoDDS(Index: integer): TPsychonautsDDS;
//function OLDCalculateMipmapSize(Mipmaps, TextureID, TextureSize: integer): integer;
//function OLDCalculateMainTextureSize(TextureID, Width, Height: integer): integer;
function CalculateIndividualTextureSize(TextureFormat: TDDSTextureFormat; Width, Height: integer): integer;
function CalculateFullTextureSize(Mipmaps, Width, Height: integer; TextureFormat: TDDSTextureFormat): integer;
procedure Log(Text: string); override;
//procedure ParsePPAK_OLD_ATTEMPT;
procedure ParsePPAK;
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 PsychoDDS [Index: integer]: TPsychonautsDDS read GetPsychoDDS;
property BigEndian: boolean read fBigEndian;
end;
const
strErrInvalidFile: string = 'Not a valid bundle';
implementation
{ TBundleManager }
constructor TPPAKManager.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 TPPAKManager.Destroy;
begin
if BundleFiles <> nil then
begin
BundleFiles.Free;
BundleFiles:=nil;
end;
if fBundle <> nil then
fBundle.free;
inherited;
end;
function TPPAKManager.DetectBundle: boolean;
var
BlockHeader: integer;
begin
Result := false;
BlockHeader := fBundle.ReadDWord;
if BlockHeader = 1262571600 then //PPAK
begin
Result := true;
fBundle.BigEndian := false;
fBigEndian := false;
end
end;
function TPPAKManager.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 TPPAKManager.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 TPPAKManager.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 TPPAKManager.GetFilesCount: integer;
begin
if BundleFiles <> nil then
result:=BundleFiles.Count
else
result:=0;
end;
function TPPAKManager.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 TPPAKManager.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;
function TPPAKManager.GetPsychoDDS(Index: integer): TPsychonautsDDS;
begin
if (not assigned(BundleFiles)) or
(index < 0) or
(index > GetFilesCount) then
result:= nil
else
result:=TDFFile(BundleFiles.Items[Index]).PsychonautsDDS;
end;
procedure TPPAKManager.Log(Text: string);
begin
if assigned(fOnDebug) then fOnDebug(Text);
end;
procedure TPPAKManager.ParseFiles;
begin
if fBigEndian then //All LE reading auto converted to BE
Log('Detected as : big endian');
ParsePPAK;
end;
//New format RE'ed by John Peel https://github.com/JohnPeel/ppf/wiki/PPF-File-Format
procedure TPPAKManager.ParsePPAK;
var
BundleVersion, LanguageID, LanguageSize, TextureCount, i, j, TextureSize,
Path_ptr, Anim_ptr, PathLength, AnimFrameCount, CalculatedTextureSize,
TextureWidth, TextureHeight, TextureNumMipmaps, Has_Palette, ResourceCount,
TempWidth, TempHeight, ScriptsVersion, TextureDataStart: integer;
TextureFormat: TDDSTextureFormat;
TextureType: TPsychoTextureType;
FileObject: TDFFile;
LanguageHeader: word;
begin
//Initialise these here to stop compiler warnings. Todo refactor this procedure.
TextureType := TPsychoTextureType(0);
TextureFormat := TDDSTextureFormat(0);
TextureWidth := 0;
TextureHeight := 0;
TextureNumMipmaps := 0;
TextureDataStart := 0;
fBundle.Position:= 0;
if fBundle.ReadBlockName <> 'PPAK' then
begin
raise EInvalidFile.Create( strErrInvalidFile );
end;
//Version section marker 0xFDFD
if fBundle.ReadWord() = $FDFD then
BundleVersion := fBundle.ReadWord() //1
else
fBundle.Seek(-2, soFromCurrent); //If identifier not there this whole section doesnt exist
{Language section marker 0xFFFF
If languages block is present then there's often multiple language blocks each with multiple textures eg in BBA1.ppf.
No counter for how many language blocks there are so just keep repeating until we dont get a 0xFFFF language header - meaning we are then onto the MPAK section.}
LanguageHeader := 0;
repeat
LanguageHeader := fBundle.ReadWord();
if LanguageHeader = $FFFF then
begin
LanguageID := fBundle.ReadWord();
LanguageSize := fBundle.ReadDWord();
end
else
fBundle.Seek(-2, soFromCurrent); //If identifier not there this whole section doesnt exist
TextureCount := fBundle.ReadWord();
//Log(IntToStr(TextureCount));
//Log('Bundle pos before textures' + inttostr(fBundle.Position));
for i := 0 to TextureCount -1 do
begin
FileObject := TDFFile.Create;
FileObject.Compressed := false;
FileObject.CompressionType := 0;
FileObject.FileTypeIndex := -1;
FileObject.FileExtension := '';
FileObject.Offset := fBundle.Position;
FileObject.FileType := ft_HeaderlessPsychoDDSImage;
if BundleVersion = 1 then
begin
if fBundle.ReadDWord <> $31545820 then //' XT1' identifier
raise EInvalidFile.Create( 'XT1 header expected but not found!' );
TextureSize := fBundle.ReadDWord;
FileObject.Size := TextureSize;
end
else
Log('WARNING bundle version 0 so texturesize + fileobjectsize potentially not set!*****************');
fBundle.Seek(12, soFromCurrent); // element_id, texture_handle, palette_handle
Path_ptr := fBundle.ReadDWord;
Anim_ptr := fBundle.ReadDWord;
fBundle.Seek(20, soFromCurrent); // density, visual_importance, memory_importance, unknown0, flags
//If there's a path
if Path_ptr <> 0 then
begin
PathLength := fBundle.ReadWord;
FileObject.FileName := PChar(fBundle.ReadString(PathLength)); //Null terminated
FileObject.FileExtension := ExtractFileExt( FileObject.FileName );
end;
//If its an animation
if Anim_ptr <> 0 then
begin
AnimFrameCount := fBundle.ReadDWord;
fBundle.Seek(24, soFromCurrent); //Rest of anim info
end
else
AnimFrameCount := 1;
if AnimFrameCount = 0 then
begin
Log('Anim frame count is 0!');
raise EInvalidFile.Create( strErrInvalidFile );
exit;
end;
//Multiple animation frames means we have to parse them all. First file in common.ppf for example
for j := 0 to AnimFrameCount -1 do
begin
//Now at texture structure
fBundle.Seek(4, soFromCurrent); //element_id
TextureFormat := TDDSTextureFormat(fBundle.ReadDWord);
TextureType := TPsychoTextureType(fBundle.ReadDWord);
fBundle.Seek(4, soFromCurrent); //Flags
TextureWidth := fBundle.ReadDWord;
TextureHeight := fBundle.ReadDWord;
TextureNumMipmaps := fBundle.ReadDWord;
fBundle.Seek(16, soFromCurrent);
if TextureFormat = PAL8 then
begin
Has_Palette := fBundle.ReadWord;
if Has_Palette <> 0 then
fBundle.Seek(4, soFromCurrent); //Seek past the palette
end;
//Store current pos for where texture data actually begins
if j=0 then
TextureDataStart := fBundle.Position - FileObject.Offset;
//Correct for mipmaps being 0 when...they arent.
if TextureNumMipmaps = 0 then
begin
//Log('Mipmaps=0 so correcting...');
TempWidth := TextureWidth;
TempHeight := TextureHeight;
while (TempWidth > 0) and (TempHeight > 0) do
begin
TempWidth := TempWidth shr 1;
TempHeight := TempHeight shr 1;
TextureNumMipmaps := TextureNumMipmaps + 1;
end;
end;
//Log(FileObject.FileName + ' TextureSize=' + IntToStr(TextureSize) + ' Width=' + IntToStr(TextureWidth) + ' Height=' + IntToStr(TextureHeight) + ' HasPalette=' + inttostr(Has_Palette) + ' AnimFrameCount=' + IntToStr(AnimFrameCount) + ' TextureType=' + TRttiEnumerationType.GetName(TextureType) + ' TextureFormat=' + TRttiEnumerationType.GetName(TextureFormat) + ' TextureNumMipmaps=' + inttostr(TextureNumMipmaps));
CalculatedTextureSize := CalculateFullTextureSize(TextureNumMipmaps, TextureWidth, TextureHeight, TextureFormat);
if TextureType = Cubemap then
CalculatedTextureSize := CalculatedTextureSize * 6;
//Log('Offset before seek past texture=' + inttostr(fBUndle.Position) + ' CalculatedTextureSize=' + inttostr(CalculatedTextureSize ));
fBundle.Seek(CalculatedTextureSize, soFromCurrent); //Seek past the texture
end;
//Now texture data
FileObject.PsychonautsDDS := TPsychonautsDDS.Create;
FileObject.PsychonautsDDS.TextureType := TextureFormat;
FileObject.PsychonautsDDS.Width := TextureWidth;
FileObject.PsychonautsDDS.Height := TextureHeight;
FileObject.PsychonautsDDS.Mipmaps := TextureNumMipmaps;
FileObject.PsychonautsDDS.DataOffset := TextureDataStart; //fBundle.Position - FileObject.Offset;
FileObject.PsychonautsDDS.MainTextureSize := CalculateIndividualTextureSize(TextureFormat, TextureWidth, TextureHeight);
FileObject.PsychonautsDDS.IsCubemap := (TextureType = Cubemap); //TODO swap for enum TPsychoTextureType and just store that
//Log('Offset=' + inttostr(fBUndle.Position) + ' CalculatedTextureSize=' + inttostr(CalculatedTextureSize ));
BundleFiles.Add(FileObject);
end;
until LanguageHeader <> $FFFF;
//MPAK section
if fBundle.ReadBlockName <> 'MPAK' then
begin
Log('MPAK header not found!');
exit;
end;
ResourceCount := fBundle.ReadWord;
//Log('NUM MPAK Files ' + inttostr(numfiles));
for I := 0 to ResourceCount -1 do
begin
FileObject := TDFFile.Create;
FileObject.Compressed := false;
FileObject.CompressionType := 0;
FileObject.FileTypeIndex := -1;
FileObject.FileExtension := '';
FileObject.FileType := ft_other;
PathLength := fBundle.ReadWord;
FileObject.FileName := PChar(fBundle.ReadString(PathLength)); //Null terminated
FileObject.FileExtension := ExtractFileExt( FileObject.FileName );
fBundle.Seek(2, sofromcurrent); //??
FileObject.Size := fBundle.ReadDWord;
FileObject.Offset := fBundle.Position;
fBundle.Seek(FileObject.Size, sofromcurrent);
BundleFiles.Add(FileObject);
end;
//ScriptsVersion section
if fBundle.ReadWord() = $FCFC then
ScriptsVersion := fBundle.ReadWord //Version = 1
else
fBundle.Seek(-2, soFromCurrent); //If identifier not there this whole section doesnt exist
//Global section
ResourceCount := fBundle.ReadWord;
for i := 0 to ResourceCount -1 do
begin
FileObject := TDFFile.Create;
FileObject.Compressed := false;
FileObject.CompressionType := 0;
FileObject.FileTypeIndex := -1;
FileObject.FileExtension := '';
FileObject.FileType := ft_other;
PathLength := fBundle.ReadWord;
FileObject.FileName := PChar(fBundle.ReadString(PathLength)); //Null terminated
FileObject.FileExtension := '.lua'; //ExtractFileExt( FileObject.FileName );
FileObject.Size := fBundle.ReadDWord;
FileObject.Offset := fBundle.Position;
fBundle.Seek(FileObject.Size, sofromcurrent);
BundleFiles.Add(FileObject);
end;
//Scripts section
ResourceCount := fBundle.ReadWord;
for i := 0 to ResourceCount -1 do
begin
FileObject := TDFFile.Create;
FileObject.Compressed := false;
FileObject.CompressionType := 0;
FileObject.FileTypeIndex := -1;
FileObject.FileExtension := '';
FileObject.FileType := ft_other;
if ScriptsVersion = 1 then //This path section only exists if ScriptsVersion is 1
begin
PathLength := fBundle.ReadWord;
FileObject.FileName := PChar(fBundle.ReadString(PathLength)); //Null terminated
end
else
FileObject.FileName := 'UnnamedScript' + inttostr(i);
FileObject.FileExtension := '.lua';
FileObject.Size := fBundle.ReadDWord;
FileObject.Offset := fBundle.Position;
fBundle.Seek(FileObject.Size, sofromcurrent);
BundleFiles.Add(FileObject);
end;
//Level section
FileObject := TDFFile.Create;
FileObject.Compressed := false;
FileObject.CompressionType := 0;
FileObject.FileTypeIndex := -1;
FileObject.FileType := ft_Other;
FileObject.FileName := TPath.GetFileNameWithoutExtension (fBundleFileName) + ' level file.plb';
FileObject.FileExtension := '.plb';
FileObject.Size := fBundle.Size - fBundle.Position;
FileObject.Offset := fBundle.Position;
BundleFiles.Add(FileObject);
if (Assigned(FOnDoneLoading)) then
FOnDoneLoading(BundleFiles.Count);
end;
function TPPAKManager.CalculateIndividualTextureSize(
TextureFormat: TDDSTextureFormat; Width, Height: integer): integer;
begin
result := 0;
case TextureFormat of
A8R8G8B8, V16U16: result := (Width * Height) * 4;
R8G8B8: result := (Width * Height) * 3;
A4R4G4B4, A1R5G5B5, X1R5G5B5, R5G6B5, V8U8: result := (Width * Height) * 2;
L8,A8, AL8, PAL8: result := (Width * Height);
DXT1: result := Max(1, ((Width + 3) div 4)) * Max(1, ((Height + 3) div 4)) * 8;
DXT3, DXT5: result := Max(1, ((Width + 3) div 4)) * Max(1, ((Height + 3) div 4)) * 16;
else
Log('Texture size not calculated for ' + TRttiEnumerationType.GetName(TextureFormat));
end
end;
function TPPAKManager.CalculateFullTextureSize(Mipmaps, Width, Height: integer;
TextureFormat: TDDSTextureFormat): integer;
var
i: Integer;
begin
result := 0;
for i := 0 to Mipmaps -1 do
begin
result := result + CalculateIndividualTextureSize(TextureFormat, Width, Height);
Width := Width shr 1;
Height := Height shr 1;
end;
end;
procedure TPPAKManager.SaveFile(FileNo: integer; DestDir, FileName: string);
var
SaveFile: TFileStream;
begin
if TDFFile(BundleFiles.Items[FileNo]).Size <= 0 then
begin
Log(strErrFileSize);
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 TPPAKManager.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 TPPAKManager.SaveFileToStream(FileNo: integer; DestStream: TStream);
var
Ext: string;
TempStream: TMemoryStream;
begin
if TDFFile(BundleFiles.Items[FileNo]).Size <= 0 then
begin
Log(strErrFileSize);
exit;
end;
if (FileNo < 0) or (FileNo > BundleFiles.Count) then
begin
Log(strErrFileNo);
exit;
end;
Ext:=Uppercase(ExtractFileExt(TDFFile(BundleFiles.Items[FileNo]).FileName));
fBundle.Seek(TDFFile(BundleFiles.Items[FileNo]).Offset, sofrombeginning);
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.