[Home]GameInfo/GK3Barn

HomePage | GameInfo | RecentChanges | Preferences | TableOfContents | AboutTheWiki

The Gabriel Knight 3 barn files contain nearly all the resource files used in the game. It's considered a "flat" filesystem since there are no folders or directories. Each of the 3600 files has its own unique name.

The "4cc" type is just a group of four characters. An "int" is assumed to be 32 bits and a "word" is 16 bits. A "string" is just a zero terminated C style string. The SYSTEMTIME structure is a structure common to Windows. There is more information here: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/sysinfo/base/systemtime_str.asp. The files should be decoded as little endian.

The Header

Offset Size Type Description
0 4 4cc A four character identifier for the file. Should be "GK3!"
4 4 4cc Another four character identifier. Should be "Barn"
8 2 unsigned word Minor version of GK3
10 2 unsigned word Major version of GK3
12 2 unsigned word Minor version of the header
14 2 unsigned word Major version of the header
16 4 unsigned int Total size of the barn file
20 4 unsigned int Offset to section directory

If the barn header version is 1.0, then there will be a header "extension" at this point with additional information.
Offset Size Type Description
24 4 unsigned int Executable build number
28 4 unsigned int Content build number
32 4 unsigned int Random number for tracking changes
36 16 SYSTEMTIME Barn build time
52 16 SYSTEMTIME Barn's last modified time
68 100 string Copyright info
168 100 string Build info

Section Directory

Next is the section directory. You should use the offset read from the header to get to it, just in case it's not where you think it is. There are several different types of directories, but the only two that appear to be used in the game are "Data" and "DDir." More on these later.

Offset Size Type Description
?? 4 unsigned int Number of directory entries

After this point is an array of the following structure. The number of these structures is the same as the number of directory entries you just read.

Offset Size Type Description
?? 4 4cc Type of directory (note that this will be in reverse; "Data" and "DDir" will be in the file as "ataD" and "riDD")
?? + 4 2 unsigned word Minor version of directory
?? + 6 2 unsigned word Major version of directory
?? + 8 4 unsigned int Random number
?? + 12 4 unsigned int Directory checksum
?? + 16 4 unsigned int Size of the directory
?? + 20 4 unsigned int Header section offset (relative to the beginning of the barn)
?? + 24 4 unsigned int Data section offset (relative to the beginning of the barn)

The header section offset is the offset to the beginning of a directory section. The structure of the directory is different depending on what type of directory it is. The DDir directory contains information about which barn file the directory is describing, how many files are in the directory, the size of the file, and much more. The Data directory contains the files themselves. Each barn has only one Data section but may have more than one DDir section.

The DDir Section

Here is the DDir structure:

Offset Size Type Description
?? 32 string Name of the barn file the directory describes (will be blank if it's describing the current barn)
?? + 32 4 4cc ID of the directory section
?? + 36 40 string Description of the data directory
?? + 76 4 unsigned int Random number
?? + 80 4 unsigned int Number of files in the directory

Next is an array of offsets to information about the files. The number of elements in this array is the number of files in the directory.

Offset Size Type Description
?? 4 unsigned int Offset to entry about a file

Right after this array is an optional extention of the DDir structure. If the version of the DDir structure is 1.2 then it will also have this structure.

Offset Size Type Description
?? 4 unsigned integer Which disc the barn this directory describes is on
?? + 4 4 unsigned integer Priority code (not sure how this is used)

Next comes an array of data entries. Each entry contains offsets to the file, file size, the type of compression used on the file, etc. Use the offsets read in before the 1.2 version block to get to the entries.

Offset Size Type Description
?? 4 unsigned integer Size of the file within the barn
?? + 4 4 unsigned integer Offset of the file (relative to the beginning of the data section!)
?? + 8 4 unsigned integer Data entry checksum
?? + 12 1 unsigned byte Entry type (not sure what this is for)
?? + 13 1 unsigned byte Format that the file is in (0 = uncompressed, 1 = ZLib, 2 = LZO)
?? + 14 1 unsigned byte File name length
?? + 15 ?? string Name of the file

That's it (finally!) for the DDir section.

The Data Section

The Data section does not have a header, or even a structure. The offsets and sizes of the files within the Data section need to be read in from the DDir section. The Data section is just otherwise a huge block of bytes that make up files.

Uncompressing Compressed Files

Some files in the barn will be compressed. Nonstreaming files (which make up most of the files in a barn) are compressed with LZO while streaming files are compressed using ZLib.

Decompressing LZO Files

The LZO algorithm used to decompress files is the LZO1X algorithm. The first 4 bytes of an LZO compressed file represent the uncompressed size of the file, and the next 4 bytes represent the compressed size of the file. Each of these fields do not take into account the eight byte header - the sizes are JUST for the file itself.

Offset Size Type Description
?? 4 unsigned integer Size of the full uncompressed file
?? + 4 4 unsigned integer Size of the compressed file
?? + 8 ?? ?? File data

These two sizes will be necessary to properly decompress the file. Also remember that decompression must begin at the eight byte offset, not at where the barn says the file starts.

Decompressing ZLib Files

Not much is known about decompressing ZLib files at this time :(

HomePage | GameInfo | RecentChanges | Preferences | TableOfContents | AboutTheWiki
This page is read-only | View other revisions
Last edited July 10, 2006 12:57 am CDT by c-71-226-79-33.hsd1.ms.comcast.net (diff)
Search:
Creative Commons License
All articles licensed under a Creative Commons License.