The AIN file format (System 4)

System 4 AIN files contain a game’s code and script.

Encryption

AIN files are encrypted by XORing each byte of the input with the output of a mersenne twister PRNG seeded with the key 0x5D3E3.

Sections

AIN files are broken into a number of sections, each beginning with a 4 byte ASCII magic word. The structure of each section varies. Not all sections are present in all versions of the AIN format.

Magic Description Notes
VERS AIN file version
KEYC Keycode (???) Removed in v12
CODE Bytecode
FUNC Function descriptors
GLOB Global variable descriptors
GSET Global variable initvals Removed in v12
STRT Struct descriptors
MSG0 Messages
MSG1 Messages (encrypted) Since Rance IX (introduced mid-v6)
MAIN Index of main function
MSGF Index of message function
HLL0 Library descriptors
SWI0 Switch descriptors
GVER Game version
STR0 String constants
FNAM Filenames
OJMP ??? Removed in v7
FNCT Function type descriptors Until Oyako Rankan (removed mid-v6)
DELG Delegate descriptors Since Oyako Rankan (introduced mid-v6)
OBJG Global group names
ENUM Enums Since Rance X

VERS Section

Offset Size (Bytes) Description
0x0 4 AIN file version

KEYC Section

Offset Size (Bytes) Description
0x0 4 Keycode (???)

CODE Section

The CODE section contains the actual code of the game, compiled from the System 4 language down to bytecode.

Offset Size (Bytes) Description
0x0 4 Size of bytecode
0x4 Varies Bytecode

Instructions in System 4 bytecode consist of a 16-bit opcode followed by a variable number of 32-bit arguments (depending on the opcode).

FUNC Section

Offset Size (Bytes) Description
0x0 4 Function count
0x4 Varies Function descriptors

Function Descriptors

Size (Bytes) Description Notes
4 Function start address
Varies Function name (null-terminated)
4 Is label? AIN version 1-6 only
Varies Return type descriptor
4 Number of arguments
4 Total number of variables Includes arguments
4 Checksum AIN version 1+ only
Varies Variable descriptors

Variable Descriptors

Size (Bytes) Description Notes
Varies Variable name (null-terminated)
Varies Variable name2 (null-terminated) AIN version 12+ only
Varies Type descriptor Typically 24 bytes
4 Has value? AIN version 8+ only
Varies Variable value See below

If the ‘Has value?’ field is 1, then:

If ‘Has value?’ is 0, then ‘Variable value’ is not present.

Type Descriptors

A type descriptor is a 3-tuple of a data type, struct type and array rank, sometimes followed by an additional type descriptor for the sub-type.

Size (Bytes) Description
4 Data type
4 Struct Type
4 Array rank
Varies Sub-type

Starting with ain v11, the meaning of the ‘array rank’ value changes. Rather than giving the rank of an array, it becomes a boolean which indicates the presense of a sub-type. If this value is 1, then another type descriptor should be read recursively.

Return Type Descriptors

Before AIN v11, return type descripors inside of function and functype descriptors do not have an ‘array rank’ field like regular type descriptors.

Size (Bytes) Description
4 Data type
4 Struct Type

Starting in AIN v11, return type descriptors are identical to regular type descriptors found elsewhere.

GLOB Section

Offset Size (Bytes) Description
0x0 4 Global count
0x4 Varies Global variable descriptors

Global Variable Descriptors

Size (Bytes) Description Notes
Varies Variable name (null-terminated)
Varies Variable name2 (null-terminated) AIN version 12+ only
Varies Type descriptor Typically 24 bytes
4 Group index AIN version 5+ only

GSET Section

Offset Size (Bytes) Description
0x0 4 Global initval count
0x4 Varies Global initval descriptors

Global Initval Descriptors

Size (Bytes) Description Notes
4 Global index Index into GLOB array
4 Data type
Varies Value

If data type is AIN_STRING (12) then ‘Value’ is a null-terminated string. Otherwise it’s a 32-bit value.

STRT Section

Offset Size (Bytes) Description
0x0 4 Structure count
0x4 Varies Structure descriptors

Structure Descriptors

Size (Bytes) Description Notes
Varies Structure name (null-terminated)
4 Interface count AIN version 11+ only
Varies Interface descriptors AIN version 11+ only
4 Constructor function index
4 Destructor function index
4 Variable count
Varies Variable descriptors
4 Virtual method count Since HENTAI LABYRINTH
Varies Virtual methods (function indices) Since HENTAI LABYRINTH

Interface Descriptors

Size (Bytes) Description
4 Struct type
4 Unknown

MSG0 Section

Offset Size (Bytes) Description
0x0 4 Message count
0x4 Varies Messages (null-terminated strings)

MSG1 Section

Offset Size (Bytes) Description
0x0 4 Message count
0x4 Varies Messages (encrypted strings)

Encrypted Strings

Size (Bytes) Description
4 Length (bytes)
Varies Encrypted data

The encrypted data can be decrypted as follows,

for (int i = 0; i < len; i++) {
    data[i] -= (uint8_t)i;
    data[i] -= 0x60;
}

MAIN Section

Offset Size (Bytes) Description
0x0 4 main function index

MSGF Section

Offset Size (Bytes) Description
0x0 4 message function index

HLL0 Section

Offset Size (Bytes) Description
0x0 4 Library count
0x4 Varies Library descriptors

Library Descriptors

Size (Bytes) Description
Varies Name (null-terminated)
4 Function count
Varies Library function descriptors

Library Function Descriptors

Size (Bytes) Description Notes
Varies Name (null-terminated)
4 Return data type Until v14+
Varies Return type descriptor Since v14+
4 Argument Count
Varies Argument descriptors

Argument Descriptors

Size (Bytes) Description Notes
Varies Name (null-terminated)
4 Return data type Until v14+
Varies Return type descriptor Since v14+

SWI0 Section

Offset Size (Bytes) Description
0x0 4 Switch count
0x4 Varies Switch descriptors

Switch Descriptors

Size (Bytes) Description
4 Case type
4 Default address
4 Case count
Varies Case descriptors

Case Descriptors

Size (Bytes) Description
4 Value
4 Address

GVER Section

Offset Size (Bytes) Description
0x0 4 Game version

STR0 Section

Offset Size (Bytes) Description
0x0 4 String count
0x4 Varies Strings (null-terminated)

FNAM Section

Offset Size (Bytes) Description
0x0 4 File count
0x4 Varies File names (null-terminated)

OJMP Section

Offset Size (Bytes) Description
0x0 4 Unknown

FNCT Section

Offset Size (Bytes) Description
0x0 4 Functype count
0x4 Varies Functype descriptors

Functype Descriptors

Size (Bytes) Description
Varies Name (null-terminated)
Varies Return type descriptor
4 Argument count
4 Variable count
Varies Variable descriptors

DELG Section

Offset Size (Bytes) Description
0x0 4 Delegate count
0x4 Varies Functype descriptors

OBJG Section

Offset Size (Bytes) Description
0x0 4 Delegate count
0x4 Varies Global group names (null-terminated)

ENUM Section

Offset Size (Bytes) Description
0x0 4 Delegate count
0x4 Varies Enum names (null-terminated)