PAK (Metroid Prime 3)

See PAK (File Format) for the other revisions of this format.

The .pak format in Metroid Prime 3 and Donkey Kong Country Returns is a new version of the pak format with some slight changes. The most notable change is that resource IDs have been extended from 32 bits to 64 bits. The new format also aligns to 64-byte boundaries, rather than 32. Aside from that, the other changes are mainly organizational; it's still largely the same data as older versions of the format.

Note that the Metroid Prime 3 E3 prototype uses a slightly modified version of the Metroid Prime pak format, not this one. See the PAK (Metroid Prime) page for more info on that revision of the format.

Format
The header is split into a number of smaller chunks that are each padded to the next 64-byte offset.

Table of Contents
The next part of the file, starting at 0x40, is a brief list of each section in the pak and its size. Although it uses a count value, this section always lists the same data in the same order.

Named Resources
This section, always starting at 0x80, lists named resources. These are the files that the game has direct access to; every other file in the pak will be a dependency of one of these.

It starts with a 32-bit count, followed by the table itself; each entry of the table is structured as follows:

Resource Table
The final table contains a list of every resource in the pak. Following a 32-bit count value, each resource entry is structured as follows:

Compression
Some files within paks are compressed; in Prime 3, files are compressed with segmented LZO1X, just like Prime 2. In DKCR, on the other hand, files are compressed as a single zlib stream, like Prime 1. In either game, every compressed file is formatted the same way, starting with some metadata detailing the structure of the compressed data.

Each compressed block is structured as follows.

For each block, if the compressed and decompressed sizes match, that indicates the block is uncompressed.

The flag value can be set to the following possible values:


 * If the block count is 1, it will be 0xA0 on block 1.
 * If blocks 2/3 are compressed, they will have it set to 0xC0.
 * If blocks 2/3 are uncompressed, they will have it set to 0x40.
 * 0 in all other circumstances.

Files are left uncompressed if the compressed data is larger than the uncompressed data. Barring that, the following formats are always compressed:


 * TXTR
 * CMDL
 * CSKR
 * CHAR
 * SAND
 * ANIM
 * SCAN
 * FONT
 * CSMP
 * STRG
 * CAAD
 * DCLN
 * USRC

The following formats are compressed when their uncompressed size is at least 0x80 bytes:


 * PART
 * ELSC
 * SWHC
 * WPSC
 * CRSC
 * BFRC
 * SPSC
 * DPSC

File Order
File order matters significantly and easily makes the difference between a pak loading quickly and optimally, or taking 30+ seconds on every door. While more research is required to figure out exactly how files should be ordered to optimize loading, the game generally clusters together resources that are used together, and a file's dependencies generally appear directly before the file itself. The game also often duplicates assets that are used multiple times within the same pak; there might be 10-11 copies of the same model file in order to allow the game to more easily find and load that file when it needs it.

Tools

 * PakTool by Aruki