← All writings

Writing · Ep. 6 · 0x0957–0x139E · Reverse engineering

What's inside a Switch game? Finding OreGairu's dialogue

I started with an NSP file. There wasn't a folder called “Japanese dialogue” waiting beside it, and opening the package in a text editor wasn't going to help. I needed to work through a few layers before I could even find a sentence.

This is the first part of the OreGairu English Patch project: going from a game package to four readable messages, then putting an English line on screen. I used existing tools and AI assistance along the way. The useful part for me was learning how to check whether an explanation actually matched the files.

The game file is a package

The first job was to find out what the package contained. Its file table gave me names, sizes and locations to follow.

Here's what was inside this OreGairu package:

This is a map of the package I inspected. Another game can have a different set of archives, so I wouldn't assume every NSP has exactly these contents.

The Program archive led to two names I kept seeing throughout the project:

For the first translation experiment, RomFS was the place to start.

Getting the files out doesn't make them readable

I used a small Python extraction script around hactool. The script took the original NSP, a path to the required keys and a name for the output run. It unpacked the outer package, then called hactool to inspect and extract each NCA. This let me keep the original archives, extracted files and reports together without mixing them up.

It helped to separate three things I had initially been calling “decoding”:

Step

What it actually does

Unpack

Copy the members out of the outer container.

Decrypt

Make encrypted archive contents accessible using the required keys.

Decode

Work out what the bytes in an extracted file mean.

The third step was where the game-specific work began. Having a message file on disk didn't mean I could read its dialogue yet.

Before editing anything, I recorded the game version and file hashes. I also kept the extracted originals separate from generated output so I could start again without extracting the whole game.

What came out of extraction?

The result was a set of folders, plus reports describing what had been extracted. The run recovered 25,459 content files. It also kept the six original package members separately. That was far too much to open one file at a time.

Here's a shortened view of the output. <program-nca> stands in for the Program archive's long hexadecimal name; the paths underneath it are from this game.

games/oregairu/
├── extracted/base/
│   ├── package/                  Original NCA files, ticket and certificate
│   └── content/<program-nca>/
│       ├── exefs/
│       │   ├── main              Compiled game module
│       │   └── main.npdm         Program metadata
│       └── romfs/
│           ├── script/
│           │   ├── *.scx
│           │   └── mes00/*.msb
│           ├── system/font.png
│           ├── bg/
│           ├── chara/
│           ├── voice/
│           ├── bgm/
│           └── se/
└── reports/base/
    ├── status.json               Did the run finish? Were failures reported?
    ├── files.csv                 File paths, sizes and hashes
    ├── manifest.json             Inputs, tools, metadata and verification results
    └── inventory.md              A shorter summary to read

The reports came from the Python wrapper, rather than hactool producing this whole layout by itself. I checked that the run had finished and that it reported no hactool verification failures. The three content archives listed in the game's content metadata also matched their expected hashes.

At this point I had a checked extraction and a searchable file list. I still had no readable dialogue.

How did I narrow it down to message files?

I started with the inventory: group files by extension, look at their folders and sizes, then inspect the opening bytes of promising samples. Names gave me somewhere to look. The bytes helped check whether those names meant anything.

These were the useful clues in the first pass:

What I found

Why it looked worth investigating

What I knew at that point

593 .msb files under script/mes00/

The folder name suggested messages; sampled files began with MES followed by zero

Message-data candidates, not decoded dialogue yet

593 .scx files under script/

Sampled files began with SC3 followed by zero

Script candidates; matching counts alone didn't prove which script used which message file

system/font.png and related images

Their filenames and PNG headers suggested fonts

Somewhere to look if the text used custom character codes

Images under bg/, chara/ and system/

Some visible text might be drawn into artwork

A separate investigation from dialogue decoding

Audio under voice/, bgm/ and se/

Filenames and .opus extensions pointed to sound

Leave these alone for the first text experiment

The .msb files were the best starting point because both their location and their signature pointed toward messages. Later work connected them to references in the .scx scripts. I didn't need to solve the whole scene system to try reading one small message file first.

Picking a file small enough to understand

I compared candidate file sizes and inspected a few. Some tiny files were empty; another contained a diagnostic message about a branch destination error. Those weren't very useful for recognizing a conversation.

The sample I settled on was:

script/mes00/og_n002ess2_00.msb

It was only 326 bytes. Reading its header showed four message records, which gave me a manageable target. Recognizable names and repeated patterns would help test the character mapping once I got that far.

So the result of the inventory step was a shortlist, and the result of this closer look was one small file to decode. The .msb extension got my attention; the structure inside the file had to support the rest.

A tiny file with its own contents list

I started by checking the first few bytes to see whether the file had a recognizable signature. Many formats put a fixed sequence near the beginning so a reader can identify what it is opening. It was a useful place to look before trying to make sense of the rest of the file.

In this sample, the first four bytes were:

4D 45 53 00
 M  E  S  [zero byte]

Seeing MES made this look like a promising message-file candidate. It also matched the signature used by the existing MES reader I checked. That gave me a format to investigate, but I still needed to test whether its record count and offsets made sense for this file.

After it came a short header and a table pointing to the messages:

Position in the file

What was there

0x00

The MES signature and zero byte

0x04

Format version, 1 in this sample

0x08

Number of records, 4

0x0C

Where the message data starts, 0x30 or 48 bytes in

0x10 onward

Four pairs of message IDs and offsets

Here, each message's offset was measured from the start of the message data. To find it in the file, the reader added the two positions:

message position = start of message data + message's relative offset

The header stored its numbers in little-endian order. Reading them the other way would give a completely unreasonable record count.

The layout agreed with MagesTools' MES reader. I also checked it against all 593 MSB files in this game's baseline. Their structure fit, including 17 empty files. That gave me confidence about where the records were. It still didn't explain the text inside them.

The letters were hiding in a picture

The dialogue bytes weren't ordinary UTF-8 text. The character codes pointed into the game's font atlas.

For the glyph tokens in this sample, the decoder read two bytes as a number, then subtracted 0x8000 to get a cell index. Here's one actual example:

Bytes in the message:  8B B6
Character code:       0x8BB6
Subtract 0x8000:      0x0BB6, or 2998

The font used 48 × 48 pixel cells, with 64 columns. Counting from zero, cell 2998 was at column 54, row 46. The character in that cell was 比, the first character in Hikigaya's surname.

The next codes gave me the rest of 比企谷小町, Hikigaya Komachi. That was much more convincing than a string of random symbols.

Hiragana, kanji and punctuation from OreGairu's font, with the message codes printed below each cell
The 59 character codes used in the first sample, checked against the actual game font. Look for 8BB6 near the bottom: its glyph is 比.

A character table from Committee of Zero's sc3tools helped suggest labels for the cells. I checked all 59 codes used by this sample against the font image rather than assuming another game's table would match. One suggested label needed changing because OreGairu's cell visibly showed an ellipsis.

There was another catch: these character codes used big-endian byte order, while the header used little-endian. Even inside one file, I couldn't assume every number followed the same rule.

Some bytes weren't letters

The four records had a repeated shape:

01  <speaker characters>  02  <message characters>  03  FF

Recognizable names before 02 suggested a boundary between the speaker and dialogue. The other bytes were control markers, not characters to translate. I didn't yet know exactly what 03 did during playback, so I kept it unchanged.

That became a rule for the decoder: keep the original bytes, and stop when something is unknown. Silently skipping a byte might make the exported sentence look cleaner while breaking the game later.

The decoder wrote two useful outputs: decoded.txt for reading the messages, and decoded.json for keeping the record IDs, offsets, original bytes and decoded tokens together. It also produced the glyph sheet shown above so I could check the character mapping visually. It hadn't changed any game files at this stage.

One of the four decoded lines was:

「なんだよ、成功とか失敗とかあんのか」

Roughly: “What do you mean? There are successes and failures?”

It was a small result, but it answered the question I'd started with. I had found the words and could explain how those bytes became Japanese.

Before changing it, put it back together

Reading a file is only half the job. I also needed to write one the game would accept.

The next check was an unchanged rebuild: use the decoded records and their preserved bytes to write a new .msb, including its header and offset table. Then compare that file with the original byte for byte. The small sample matched exactly.

That gave me a rebuilt game file, rather than just a readable export. If the comparison had failed before I edited a sentence, adding English would only have made the problem harder to track down.

After the small sample, I moved to og_n001ess0_00.msb, an opening-scene file with 41 messages. An early scene made testing easier because I could reach it repeatedly. I checked an unchanged replacement first, then tried changing a line into English.

An early English dialogue line displayed in OreGairu's original message box
The first English-line experiment in Astris. The speaker name was still Japanese at this stage.

That screenshot was the point where the work stopped being just an exported text file. A line had made the trip from the original game, through the decoder and rebuild, back onto the screen.

There was plenty left to do. Other files used characters and commands the first decoder didn't understand, and longer English sentences brought their own layout problems. But I now had a small test I could repeat: read a known file, rebuild it, change one thing, and see what happened.

The next article gets into what happened when “one line” became thousands of messages in a translation queue.