TXTD
In the context of Tomba! 2: The Evil Swine Return, a TXTD file is a data structure embedded within the game's resource files (like the DAT file). It contains text data used for in-game dialogues, descriptions, or other textual elements. This text is stored in a compressed or encoded format and is often accompanied by metadata that determines how the text is organized, displayed, or interacted with in the game.
Structure of a TXTD File
1. Header Section
- The file begins with a header containing the following fields:
- Master Root Pointer: Offset to the start of the master table.
- Master Entry Count: Number of entries in the master table.
- Additional padding or unused bytes.
2. Master Table
- A list of pointers to entry tables.
- Each pointer is represented by a relative address (offset from the start of the file or current context).
3. Entry Table
- For each master entry, there is a corresponding entry table.
- Contains metadata for individual text strings:
- Pointer to Text Data: Offset of the text string relative to the start of the table.
4. Text Strings
- Binary-encoded text data begins at the offsets specified in the entry table.
- Uses a custom encoding scheme to represent characters (as seen in the
letters
dictionary of the script). - Ends with a terminator byte (
0xFF
).
TXTD Encoding Scheme
- Character Representation:
- Characters are stored as byte values, with each byte mapping to a specific character or control code.
- Example:
0x41
→A
0x42
→B
0x43
→C
0x44
→D
0x45
→E
0x46
→F
- Control Codes:
- Non-alphanumeric bytes are often used for special formatting or commands.
- Examples:
0xFA
→ Line break (\n
)0xFC
→ Pause ({$PAUSE}
)0xF1 → {$COLOR_F1}
: Changes text color.0xC1 → {$END}
: Marks the end of a text block.
- Termination:
- Each text string ends with the byte
0xFF
, signaling the end of the string.
- Each text string ends with the byte
Tomba! 2 TXTD File Extraction Script
The script (which can be viewed here: main script, dictionary) is used for extracting and interpreting TXTD files from the DAT file in Tomba! 2: The Evil Swine Return. TXTD files often contain text data, such as in-game dialogues, descriptions, or other textual assets. The script decodes this data using a custom character set and formats it for readability or modification.
Script Details
Key Functions
preview(DAT, offset)
:- Main function for extracting text from the specified DAT file.
- Takes two arguments:
DAT
: Path to the DAT file.offset
: Offset where the TXTD data begins.
- Processes the data in two hierarchical layers:
- Master Entries: Top-level pointers directing to specific text blocks.
- Entry Headers: Sub-pointers within each master entry that direct to individual text strings.
- Calls
prepareText
andgetText
to decode and format the text.
prepareText(ptr, who, real, par1, par2, num)
:- Formats and retrieves text from a given pointer.
- Skips entries if the pointer is invalid (
0xFFFF
).
getText(real)
:- Converts a sequence of binary data into readable text using the
letters
dictionary. - Iterates until it encounters the terminator byte (
0xFF
), which signals the end of a text block.
- Converts a sequence of binary data into readable text using the
getB(number=1)
:- Helper function to read a specified number of bytes from the file and convert them into integers (little-endian format).
Dictionary: letters
The letters
dictionary maps hexadecimal values to their corresponding characters or control sequences. Key highlights include:
- Alphabet and Symbols: Maps standard alphanumeric characters (
A-Z
,a-z
,0-9
) and punctuation. - Special Characters: Supports extended characters such as
Ä
,¥
, and…
. - Control Codes:
{$END}
: Signals the end of a text block.{$PAUSE}
: Inserts a pause in the text.{$COLOR_F1}
: Changes text color (with{$END_COLOR_F0}
to revert).
Workflow of the Script
- Initialize:
- Define the path to the DAT file and the offset of the TXTD data.
- Load the DAT file in binary mode.
- Read Master Entries:
- Extract master root and the number of master entries.
- Use pointers in the master headers to locate the start of each text block.
- Process Entry Headers:
- For each master entry, extract sub-pointers (entry headers).
- Use these sub-pointers to locate individual text strings.
- Decode Text:
- Convert binary data into readable text using the
letters
dictionary. - Handle special formatting codes and ensure proper string termination.
- Convert binary data into readable text using the
- Output:
- Structure and output the extracted text for further use or modification.