GBX
From TM Wiki
Contents |
General information
TrackMania gamebox files (*.gbx) are generic container files that can contain everything from configuration to textures to track definitions. They consist of a header section and a data section.
In old versions of TrackMania they used to be text files - nowadays they are binary files. Integers are stored in little endian order. The data section is often compressed (using LZO).
Engines, classes, chunks
A .gbx file more specifically stores the serialization of one or more class instances. There is one main instance, and optionally a number of auxiliary instances.
The serializable classes are organized into 16 engines. Each class is also subdivided into chunks. A class is then not serialized in one go, but rather as a series of chunks. This allows Nadeo to easily extend classes in new TrackMania versions: instead of having to define a new class they can simply add more chunks to an existing one, and have older versions ignore these new chunk types.
The data in a gbx file follows the pattern <chunk ID> <chunk data>. A chunk ID is a 32-bit number that identifies the engine, the class, and the chunk in that class. If you for example see the bytes 07 30 04 03 in the file, that would correspond to the integer 0x03043007, and be interpreted as follows:
engine class chunk 03 043 007
All engines and classes are named; in this case, engine 3 is the Game engine, and class 043 in that engine is CGameCtnChallenge. Chunks do not have names.
Apart from chunk ID's there are also class ID's, which are just like chunk ID's except the chunk index part is ignored (and 0). For a complete overview of engines and classes, see Class ID's.
Header
The header contains things like compression information, the class ID of the main class instance, and a few chunks of the main class that serve as meta information (e.g. the thumbnail of a challenge).
- byte[3] magic: "GBX"
- int16 version: currently 6
- byte storageSettings[4]:
- 'B' or 'T': Binary or Text (always B, text is for backwards compatibility)
- 'U' or 'C': header Uncompressed or Compressed (typically U)
- 'U' or 'C': data Uncompressed or Compressed (typically C)
- 'R' or 'E': unknown purpose (typically R)
- int32 classID (class ID of main class instance)
- int32 headerSize
- byte[headerSize]:
- int32 numHeaderChunks
- HeaderEntry[numHeaderChunks]
- int32 chunkID
- int32 chunkSize (may have bit 31 set. This indicates a "heavy" header chunk which is skipped while scanning gbx files on game startup)
- concatenated data of header chunks
Data
- int32 numNodes: the total number of class instances in this gbx file, including the main instance. An internal list will be allocated with this number of entries; the main instance is at index 0.
- int32 unknown (always 0?)
- if data is compressed:
- int32 uncompressedSize
- int32 compressedSize
- byte data[compressedSize] (compressed with regular LZO)
- else:
- byte data[]
Reading the data
The data section contains further chunks of the main class instance, and may also contain auxiliary class instances. Reading the data section is started by creating an in-memory instance of the class corresponding to the main class ID (instances are called nodes internally), and calling ReadNode on it:
ReadNode()
{
while(true)
{
chunkID = ReadUInt32();
if(chunkID == 0xFACADE01)
return;
chunkFlags = GetChunkFlags(chunkID);
if(chunkFlags & 0x10)
{
skip = ReadUInt32();
chunkDataSize = ReadUInt32();
}
ReadChunk(chunkID);
}
}
GetChunkFlags() doesn't read anything from the file; it provides loading flags for the specified chunk ID. The only important flag is whether or not the chunk is "skippable". If it is, the chunk ID is followed by an int32 0x50494B53 ("SKIP", shows up as "PIKS" in the file due to little endian ordering) and an int32 specifying the size of the chunk data. This allows older version of TrackMania that don't know how to parse this chunk ID to skip over the chunk data and go to the next chunk. If the chunk is not skippable, the chunk data follows immediately after the chunk ID.
A dummy chunk ID of 0xFACADE01 signifies the end of the chunk list for the current class.
Chunk data is not self-describing; the program itself has to know how to read each one. In fact, if your program doesn't know a specific chunk ID and the chunk is not skippable, you can't even tell how long the chunk is.
We will first describe a number of "primitives" that are used when reading and writing chunks. Then we will describe the contents of a number of common chunks.
Primitives
- int8, int16, int32, int64, int128, float: regular little endian encoding
- vec3D:
- float x
- float y
- float z
- string:
- int32 length
- byte chars[length] (ascii, not zero-terminated)
- lookbackstring: a form of compression which allows to avoid repeating the same string multiple times. Everytime a new string is encountered, it is added to a string list, and from then on this list entry is referenced instead of repeating the string another time.
- if this is the first lookback string encountered:
- int32 version (currently 3).
- int32 index: has bit 31 or 30 set. A value of -1 means there is no string provided (null). Bit 30 means that a string value of "Unassigned" is also mapped to null. The actual index is bits 0-29. If it is 0, a new string follows (and will be added to the string list). If it is greater than one, use the string at stringlist[index - 1].
- if (index & 0x3FFFFFFF) is 0:
- string newString. Add to the string list.
- if this is the first lookback string encountered:
Note: after reading the header chunks, the lookback string state is reset. The string list is cleared completely, and the first lookback string in the data will again trigger the version number.
- fileref: path to an external file, e.g. a skin.
- byte version (currently 2)
- string filePath
- if file.length > 0 && version >= 1:
- string unknown
- meta: contains meta information like the track environment, time of day, and author.
- lookbackstring field1
- lookbackstring field2
- lookbackstring author
- noderef: a reference to an auxiliary class instance.
- int32 index. if this is -1, the node reference is empty (null).
- if the index is >= 0 and the node at the index has not been read yet:
- int32 classID: instantiate a new node for this class ID and store it in the node list at the specified index
- ReadNode()
Class descriptions
CGameCtnChallenge (03 043 000)
03043002
byte version int32 0 int32 bronzeTime (ms) int32 silverTime (ms) int32 goldTime (ms) int32 authorTime (ms) int32 price (coppers) int32 multilap (0/1) int32 trackType (0: Race, 1: Platform, 2: Puzzle, 3: Crazy, 5: Stunts) int32 int32 if version >= 11: int32
03043003
byte version meta (trackUID, environment, author) string trackName byte int32 string password (weak xor encryption, no longer used in newer track files; see 03043029) meta (timeOfDay, environment, author) float x 4 int128
03043004
int32
03043005
string xml
The XML block contains everything the 003 header chunk does, except for the password. Unlike the 003 chunk, however, it also contains the list of dependencies for the track (images, mods, music, etc.), as well as the version number of the software that created the track, the actual number of laps, and an optional Mod name.
03043007
int32 haveThumbnail
if haveThumbnail != 0:
int32 thumbSize
"<Thumbnail.jpg>"
byte thumb[thumbSize]
"</Thumbnail.jpg>"
"<Comments>"
string comments
"</Comments>"
0304300D
meta empty
03043011
noderef collectorList noderef challengeParameters int32
03043014 (skippable)
int32 string password (old style password with weak xor encryption. this chunk is no longer used in newer challenge files, see 03043029)
03043017 (skippable)
int32 count (int32 x 3) x count
03043018 (skippable)
int32 int32
03043019 (skippable)
fileref
0304301C (skippable)
int32
0304301F
meta (trackUID, environment, author)
string trackName
meta (timeOfDay, environment, author)
int32
int32
int32
int32
int32 flagsAre32Bit
int32 numBlocks
for each block:
lookbackstring blockName
byte rotation (0/1/2/3)
byte x
byte y
byte z
int16/int32 flags
if (flags & 0x8000) != 0: custom block
lookbackstring author
noderef
03043021
noderef noderef noderef
03043022
int32
03043024
fileref
03043025
int64 int64
03043026
noderef
03043027
int32 provided
if provided != 0:
byte
vec3D x 3
vec3D
float
float
float
03043028
ReadChunk(0x03043027) string
03043029 (skippable)
int128 passwordHash (salted MD5)
int32 CRC32("0x" + hex(passwordHash) + "???" + trackUID)
0304302A
int32
CGameCtnCollectorList (03 01B 000)
0301B000
int32 num
Item items[num]
meta
int32
CGameCtnChallengeParameters (03 05B 000)
0305B000
int32 int32 int32 int32 int32 int32 int32 int32
0305B001
string string string string
0305B002
int32 int32 int32 float float float int32 int32 int32 int32 int32 int32 int32 int32 int32 int32
0305B003
int32 float int32 int32 int32 int32
0305B004
int32 int32 int32 int32 int32
0305B005
int32 x 3 (ignored)
0305B006
int32 num int32 items[count] (ignored)
0305B008
int32 int32
CGameCtnBlockSkin (03 059 000)
03059000
string string
03059001
string fileref skin
03059002
string fileref skin fileref
CGameCtnReplayRecord (03 093 000)
03093000
int32 version
meta (trackUID, environment, author)
int32 time (ms)
string
if version > 5:
string driverLogin
03093001
string xml
The XML block contains the UID and replay ("best") time like in the header. It also contains the version number of the software that created the replay; optionally the respawns count (can be -1 or larger), the Stunts score, and a validable flag; and in recent versions occasionally two checkpoints fields.
03093002
int32 size byte GBX[size], the track the replay was recorded on
03093007 (skippable)
int32
03093014
int32 ignored (0xA) int32 numNodes noderef[numNodes] int32 ignored int32 num int64[numExtras]
09093015
noderef
CGameGhost (03 03F 005)
0303F005
int32 uncompressedSize
int32 compressedSize
byte compressedData[compressedSize]: (compressed with zlib deflate)
int32 classID
int32 bSkipList2
int32
int32
int32
int32 size
byte sampleData[size] (samples of position, rotation, speed... of the car during the race)
int32 numSamples
if numSamples > 0:
int32 firstSampleOffset
if numSamples > 1:
int32 sizePerSample
if sizePerSample == -1:
int32 sampleSizes[numSamples - 1]
if bSkipList2 == 0:
int32 num
int32 data[num]
A sample record looks as follows:
vec3D position int16 roll (0..FFFF -> 0..pi) int16 heading (0..FFFF -> -pi..pi) int16 pitch (0..FFFF -> -pi/2..pi/2) int16 speed (-> exp(speed/1000); 0x8000 means 0) byte vHeading (0..FF -> -pi..pi) byte vPitch (0..FF -> -pi/2 -> pi/2) int16 speed2 int16 vHeading2 int16 vPitch2 int16 (0..FFFF -> -1000..10000) int16 (0..FFFF -> -1000..1000) int16 (0..FFFF -> 0..30000) ... (more unknown data)
CGameCtnGhost (03 092 000)
CGameCtnGhost is a subclass of CGameGhost. If you encounter an unknown chunk ID while reading a CGameCtnGhost instance, delegate it to CGameGhost.
03092005 (skippable)
int32
03092008 (skippable)
int32
03092009 (skippable)
vec3D
0309200A (skippable)
int32
0309200B (skippable)
int32 num int64[num]
0309200C
int32 ignored
0309200E
lookbackstring uid
0309200F
string server
03092010
lookbackstring
03092012
int32 ignored int128
03092013 (skippable)
int32 int32
03092014 (skippable)
int32
03092015
lookbackstring playerName
03092017 (skippable)
int32 num fileref carSkins[num] string playerName string avatarFileName
03092018
meta
03092019
int32 field_10C
int32 ignored
int32 numControlNames
lookbackstring controlNames[numControlNames]
int32 numControlEntries
int32
ControlEntry[numControlEntries]
int32 time (ms + 100000)
byte controlNameIndex
int32 onoff (1/0)
string gameVersion
int32
int32
int32
string xml
int32
Applications and Libraries that can inspect the file format
- Extract GBX data - a PHP script to extract useful data from all .Challenge.Gbx files (including the thumbnail image), and from .Replay.Gbx files.
- Tally GBX versions - a PHP script to tally version data from all .Challenge.Gbx files (including sample challenges in all known versions).
- GBX Data Fetcher - two PHP classes to extract useful data from all .Challenge.Gbx files (including the thumbnail image) and from .Replay.Gbx files, and parse their XML blocks.
- Replay Parser - a PHP class to extract useful data from Replay data strings (from the GetValidationReplay method), and parse their XML blocks.
- Blockmix tools - challenge editing tools (Recompressor, ChallengeEdit, GBX-Master)
- Easy TM
- Trackmania Disassembler - includes a library allowing you to write your own applications that can read the format.
To do ... translate...

