dwgx@blog:~$dwgx
> cd ../posts

thtk: forking a Touhou game data reverse-engineering toolkit

// created

Touhou Toolkit

thtk (Touhou Toolkit) is a set of C command-line tools written by the thpatch crew behind the Touhou fan translations, built specifically to pull apart the proprietary data formats of the Touhou series. This is an experimental fork I pulled over to study how they write reverse-engineering tools. It doesn’t track upstream, so don’t treat it as one.

Every Touhou title stuffs its archives, animations, scripts, and stages into its own binary formats, and thtk brute-forces its way through them into working tools, covering everything from the early games all the way to the 20th installment. It’s not one big do-everything Swiss army knife, but a handful of small tools that each own one slice:

  • thdat handles the DAT archives, x to unpack and c to pack, with the widest game coverage
  • thanm handles ANM animations and sprite sheets, x to extract and l to list contents
  • thecl handles ECL scripts, d to decompile and c to compile back; all the bullet-pattern logic lives here
  • thmsg handles MSG dialogue text, d to dump and c to compile; the first thing translators touch
  • thstd handles STD stage layouts, a dump/compile pair

The approach is consistent throughout: each proprietary format gets its own tool that first decodes it into something a human can read and edit, then compiles your changes back into the binary the game accepts. To get started, run thdat x on the DAT of some title and peel it apart layer by layer.

The real fun is in the source. thecl, thmsg, and thstd use Yacc/Lex to write the lexer and grammar, and the text you dump out is actually a small DSL, with compile just parsing it back into bytes. This bidirectional pipeline between binary and readable script is clean, and reading through how it distinguishes format versions with table-driven logic, how it leaves hooks for cross-version compatibility, and how CMake strings together a pile of extlibs will clear up plenty of things you’d previously just hand-waved past.

One heads-up: this repo only gets tinkered with locally. The game’s proprietary binaries and anything carrying path credentials never get committed. The tools themselves are public, but the game data you extract is someone else’s copyrighted material, so mind that boundary yourself. For licensing, look at the thpatch/thtk upstream; I haven’t touched that part.