# gxter-cli
`gxter` is a library for reading and writing GXT files for Rockstar Games' Grand
Theft Auto games, currently with support for GTA III, Vice City, San Andreas, IV
and V.
`gxter-cli` is a command-line utility that uses `gxter` to read and write GXT
files and convert them into TOML-based text files.
**gxter-cli is a work in progress, and support for other useful options is to be
added in the future.**
The program has been proven capable of decompiling the English language text
files from all three games supported, and compiling them back with small
modifications. Further testing may be necessary to prove whether or not larger
changes will work.
## Usage
The program runs on the command line and works with either GXT files or
TOML-based text files. Use the following arguments to tell what the program
should do:
- `-c`, `--character-table`: Use a custom "character table" in order to convert
between the game's internal encoding and UTF-8. This option is useful for
non-standard releases of the games.
- `-d`, `--decompile`: Read a GXT file, then output its contents on the screen
or into a file specified by the `-o` parameter below.
If this parameter is not specified, the program will assume the default
operation is to compile a text file into a GXT file instead. The program will
determine the GXT's format based on the file's structure and act accordingly.
- `-K`, `--key-sort`: When decompiling, list strings in the order of their keys,
according to the entries in TKEY. (GXT files are expected by the games to have
their strings sorted either by key or hash in the TKEY table, so this will
result in an alphabetical or hash-based sort.)
- `-n`, `--name-list`: When decompiling a GXT file, read a "name list"
consisting of raw string names. These string names have their CRC32 hashes
precalculated, and in case one of these is seen in a GTA SA format GXT file,
that hash is replaced with the name that matches it.
- `-O`, `--offset-sort`: When decompiling, list strings in the order of their
data *offsets* relative to TDAT, according to the entries in TKEY. This is
useful, as in GTA 3 and VC files, it is possible to determine which strings
were originally added earlier or later in the game's development (it is
notable that strings related to the PC ports of both games are close to the
end), and in GTA SA files, since the keys are CRC32 hashes, key-based ordering
results in pseudorandom arrangement of lines, whereas offset-based ordering
shows related lines closer to each other.
- `-o`, `--output` (argument: file name): When decompiling, output the resulting
data into a TOML file specified by the argument's value, instead of on screen.
When compiling, save the GXT file under the following file name.
- `-p`, `--pretty-print`: Instead of converting a text or GXT file, "pretty
print" its contents in a format designed for terminal output. Color tags in
the file's strings (e.g. `~r~` for red) will be used to change the text's
color, and (depending on the format) tags referring to PS2 or Xbox controller
buttons will be replaced with descriptive labels. **WARNING: since tag
recognition happens after the strings are decoded into Unicode, this function
may break if custom character tables are used.**
- `-s`, `--sanitize-tags`: If custom tables are used, these may replace ASCII
characters with different ones based on the translation the tables are
designed for. This option tries to make sure that characters within formatting
tags, which start and end with a tilde character, are *not* converted. This
means that, for instance, a Russian or Ukrainian character table that
converts `k` into `к` will not touch the `~k~` tag used for specifying keys,
and one that redefines `O` as `О` will not affect `~VEHICLE_LOOKLEFT~`.
The first parameter that doesn't fit these will be interpreted as the input file
name.
## Text File Format
The text file format is using TOML as a base and has the following structure:
```
format = "format"
gta2lang = "E"
[main_table]
NAME = "A string with a name (GTA III / VC format)."
"#01234567" = "A string with a CRC32 hash (GTA SA format)."
[aux_tables.TABLE]
NAME = "A string from an auxiliary table (GTA VC / SA format)."
```
The "format" parameter is a string specifying the format to be used for the
file, with the following values being allowed:
- `One`: GTA 1 (FXT format)
- `Two`: GTA 2 (WIP)
- `Three`: GTA III or VC on Xbox
- `Vice`: GTA VC, LCS, VCS
- `San` or `San8`: GTA SA / IV, 8-bit characters
- `Four` or `San16`: GTA IV, 16-bit characters
- `Five`: GTA V (GXT2 format)
The `main_table` section lists all the strings in the main table of the strings
file. In GTA 2, III and V format files, this is the only table, whereas in VC,
SA and IV format files, it's the first table among many.
The `aux_tables` section lists all the auxiliary tables, if they're provided.
Each table's name is specified after the period in each `aux_tables.` section.
When imported, these are converted into Rust strings, with extra care to make
sure even abnormal values (like non-zero bytes after zero bytes) are preserved.
Each string can be identified using either its name or CRC32 hash. When
imported, string-based names are converted into strings, and hash-based names
are converted into a string of a hash symbol (`#`) followed by the hexadecimal
value of the hash. If a string-based name happens to start with a hash symbol,
another one is added to the beginning, in order to prevent name collisions (so,
a string `#01234567` represents a hash of `0x01234567`, and `##01234567`
represents a real string with a hypothetical name `#01234567`, which in turn
would be hashed as `0xEA28B0AB`).
(Note: when a GXT file is being compiled, the reverse operation only happens if
the string for the key contains two hash signs (`##`). Key strings that start
with a single hash sign and aren't followed by exactly 8 other characters will
be read literally, so both `#NAME` and `##NAME` will work to encode a
literal name of `#NAME`)
A GXT file's strings consist of fixed-size characters (8 or 16 bits wide),
encoded using a custom encoding. These are converted into UTF-8 when imported.
The conversion is done first using any custom table that may be provided, then
using the character tables built into the program. The characters not specified
in the tables are handled as follows:
- Since a null-based byte or wide character is used in GXT files as a string
terminator, a GXT string can never contain one.
- Characters with values between 1 and 31 will be recorded as equivalent ASCII
control characters.
- Characters with codes between 32 and 255 (0x00FF) will be recorded as
Private Use Area characters with corresponding codes between U+E020 and U+E0FF
(0xE000 will be added to the codepoint).
- Characters with codes between 256 (0x0100) and 65535 (0xFFFF) will be recorded
as Supplementary Private Use Area-A characters with codes between U+F0000 and
U+FFEFF (0xFEF00 will be added to the codepoint).
Private Use Area codes are used in order to not imply that an unknown character
matches any existing Unicode character.
GXT2 files used by GTA V are stored in UTF-8 and are instead transcribed
directly.
## Character Table Format
A character table consists of correspondences between "data" (sequences of
characters as used in the raw file) and "text" (strings of Unicode characters).
The "data" field can also be named "from" and the "text" field can be named
"to", but it it used to build both decode (conversion from raw characters to
Unicode) and encode (conversion from Unicode back to raw characters) tables.
In order to resolve collisions (when several entries have the same "data" or "text" value),
the "primary" field can be specified as `true`, in which case, entries labeled as primary will
overwrite matching entries previously specified.
```
values = [
{data = {123}, text = "@", primary = true},
{from = {124}, to = "#"},
]
```
In this case, 123 and 124 are the decimal codes for the characters as used in
the GXT file, and the characters inside single quotes are Unicode characters.
If a character is not listed in the decode or encode table, it will be decoded
according to the default table for the corresponding format. This means that it
is not necessary to define any unchanged characters in the table file. This
means that characters that "pull double duty" as both original and modified
characters (for example, Latin `K` and Cyrillic `К`) will be properly encoded
even if only the Cyrillic `К` is defined in the decode or encode table.
## Name List Format
A name list is a simple TOML file consisting of a single field: an array of
strings named "names".
```
names = ["NAME1",
"NAME2",
"QWERTYU",
"IOPASDF",
"GHJKL"]
```
When a name list is loaded, its CRC32 hash values are precalculated, and used to
match hashes from GTA SA or IV format files to said names. The resulting
text file will then display these names instead of hash values.
**See the [README.md file of the original `gxter`
crate](https://github.com/slashdevslashurandom/gxter/blob/main/README.md) for information on the
file formats used in the application.**