Rudimentary disassembler / assembler for initialization scripts in Dragon Quest IX.
The game uses a generic binary scripting format for configuring initialization of various things, e.g. configuring lighting effects for an area or determining loot available from chests and other containers. Every binary script is comprised of a sequence of opcodes paired with a list of primitive operands, all of type string, int or float. The meaning of each opcode is specified within the game's code and is generally not known at this stage (note opcodes can be reused across different script purposes - for example, the opcode 0x64 might have one particular purpose in scripts to configure lighting, and a very different purpose in scripts to specify loot tables).
At the most basic level, this tool allows for 'disassembling' the binary format into a more human-readable (and human-editable) format. The output takes the form of a Lua script representing the sequence of opcodes and operands. A sample output may look like:
opcode_66(0, "foo", 5.8, -12)
opcode_6a(2)
opcode_6a(3)
You can then re-assemble the script back into the binary format: this works by executing the Lua script with each of the opcode_xx functions hooked to emit the appropriate bytes. As such, you can use any regular Lua functionality e.g. outputting commands in a loop.
We also provide some 'contexts' to allow, optionally, for disassembly into and reassembly from something a bit more descriptive. A context is a directory containing two Lua files up.lua and down.lua which aid in making disassembled code more idiomatic and converting idiomatic code back to the flat Lua format above respectively. A sample output from the disassembler using a suitable context may look like:
lighting_config{ mode = LIGHTING_MODE_BASIC, gradient_offset = -0.05 }
set_basic_lighting{ time = TIME_NIGHT, light_dir = { 0.63, 0.72, 1.21 }, background_color = RGB(0, 0, 8), ... }
Each of the subdirectories of contexts is a context for a particular script type. We hope to provide more contexts as we discover new uses for the scripting system. See Contexts.md for more detailed information on creating contexts.
dqixscript path/to/input/file [-o path/to/output/file] [-c path/to/context/directory]
This will attempt to assemble any file whose extension is .lua (note this includes files such as foo.bin.lua) and will attempt to disassemble any other file.