feat(codegen): resolve dialog ids from a committed table instead of a ROM (#163)

This commit is contained in:
Fiereu authored and GitHub committed 2026-08-10 22:45:21 +02:00
1 parent 77f3d71329
commit 95c90284ac
11 files changed
+61490 -90

No files matched your search

-1
View File
@@ -32,7 +32,6 @@ jobs:
- name: Set up Gradle - name: Set up Gradle
uses: gradle/actions/setup-gradle@v6 uses: gradle/actions/setup-gradle@v6
# No ROMs on the runner, so dialog ids are placeholders.
- name: Build distributions - name: Build distributions
run: ./gradlew :server.login:installDist :server.game:installDist run: ./gradlew :server.login:installDist :server.game:installDist
-1
View File
@@ -54,7 +54,6 @@ jobs:
- name: Set up Gradle - name: Set up Gradle
uses: gradle/actions/setup-gradle@v6 uses: gradle/actions/setup-gradle@v6
# No ROMs on the runner, so dialog ids are placeholders.
- name: Build distributions - name: Build distributions
run: > run: >
./gradlew :server.login:assembleDist :server.game:assembleDist ./gradlew :server.login:assembleDist :server.game:assembleDist
+13 -5
View File
@@ -41,12 +41,20 @@ A dialog id is a file offset into the retail GBA ROM. The decomp is byte-identic
to it, so the generator encodes a text from the decomp, finds those bytes in the to it, so the generator encodes a text from the decomp, finds those bytes in the
ROM, and packs the offset into the id. The decomp alone has no offsets. ROM, and packs the offset into the id. The decomp alone has no offsets.
Put the Emerald (`BPEE`, Hoenn) and FireRed (`BPRE`, Kanto) ROMs in `roms/`. Resolving them needs the retail ROMs, so the result is committed instead, one
Filenames do not matter, each is identified by the game code in its GBA header. file per region under `codegen/dialog/`. The build reads only those, which is
The folder is **gitignored**, this project ships no ROMs. why CI and the release images get the same dialog as a local build.
Without them the build still succeeds and every dialog id is `0`, so CI passes but ROMs are needed only to **change** that table, after a decomp bump renames or
the client shows the wrong text. adds a label. Put the Emerald (`BPEE`) and FireRed (`BPRE`) ROMs in `roms/`,
which is **gitignored** because this project ships no ROMs, and run:
```bash
./gradlew :codegen:refreshDialogTable # then commit codegen/dialog/
```
Filenames do not matter, each ROM is identified by the game code in its GBA
header.
## Configuration ## Configuration
+21 -11
View File
@@ -34,9 +34,12 @@ val sourceDecompDir = rootProject.layout.projectDirectory.dir("decomp/pokeemeral
// from its src/data/items.json and container 54 of its src/data/text1.json. // from its src/data/items.json and container 54 of its src/data/text1.json.
val itemDataDir = rootProject.layout.projectDirectory.dir("decomp/pokeblack") val itemDataDir = rootProject.layout.projectDirectory.dir("decomp/pokeblack")
// ROMs (gitignored) that dialog codegen reads offsets from // Gitignored, and only the manual refresh tasks read them.
val romsDir = rootProject.layout.projectDirectory.dir("roms") val romsDir = rootProject.layout.projectDirectory.dir("roms")
// Committed, because a ROM is the only place these ids exist and no runner has one.
val dialogDataDir = layout.projectDirectory.dir("dialog")
jteCodegen { jteCodegen {
register("maps") { register("maps") {
mainClass.set("de.fiereu.openmmo.codegen.maps.Main") mainClass.set("de.fiereu.openmmo.codegen.maps.Main")
@@ -95,21 +98,28 @@ jteCodegen {
inputDirs.from(sourceDecompDir) inputDirs.from(sourceDecompDir)
extraArgs.set(listOf(sourceDecompDir.asFile.absolutePath)) extraArgs.set(listOf(sourceDecompDir.asFile.absolutePath))
} }
// Dialog ids are ROM offsets, so the generator reads a ROM from the roms folder,
// picking it by its GBA header game code. Each region is "package|gameCode|decompDir".
register("dialog") { register("dialog") {
mainClass.set("de.fiereu.openmmo.codegen.dialog.Main") mainClass.set("de.fiereu.openmmo.codegen.dialog.Main")
val fireredDir = rootProject.layout.projectDirectory.dir("decomp/pokefirered") inputDirs.from(dialogDataDir)
inputDirs.from(sourceDecompDir, fireredDir, romsDir) extraArgs.set(listOf(dialogDataDir.asFile.absolutePath) + regionSources.keys)
extraArgs.set(
listOf(
romsDir.asFile.absolutePath,
"hoenn|BPEE|${sourceDecompDir.asFile.absolutePath}",
"kanto|BPRE|${fireredDir.asFile.absolutePath}",
))
} }
} }
tasks.register<JavaExec>("refreshDialogTable") {
group = "codegen"
description =
"Re-resolve codegen/dialog from the ROMs in roms/ (manual, run after a decomp bump, commit the result)"
val fireredDir = rootProject.layout.projectDirectory.dir("decomp/pokefirered")
classpath = sourceSets["generator"].runtimeClasspath
mainClass.set("de.fiereu.openmmo.codegen.dialog.RefreshMain")
args(
romsDir.asFile.absolutePath,
dialogDataDir.asFile.absolutePath,
"hoenn|BPEE|${sourceDecompDir.asFile.absolutePath}",
"kanto|BPRE|${fireredDir.asFile.absolutePath}",
)
}
// One shot bootstrap of the overworld script stubs into server.game. Run by hand with // One shot bootstrap of the overworld script stubs into server.game. Run by hand with
// `gradlew :codegen:generateScriptStubs`. Deliberately not wired into the build, the emitted files // `gradlew :codegen:generateScriptStubs`. Deliberately not wired into the build, the emitted files
// are committed source so hand written ports are never overwritten on a normal build. // are committed source so hand written ports are never overwritten on a normal build.
File diff suppressed because it is too large. Load diff
File diff suppressed because it is too large. Load diff
@@ -1,9 +1,6 @@
package de.fiereu.openmmo.codegen.dialog package de.fiereu.openmmo.codegen.dialog
/** data class DialogLine(val label: String, val textId: Int, val text: String)
* One dialog line resolved from the decomp: its full label, packed textId, and readable preview.
*/
data class DialogLine(val label: String, val textId: Int, val preview: String)
/** A single enum entry inside a location's dialog enum. */ /** A single enum entry inside a location's dialog enum. */
data class DialogEntry(val name: String, val textId: Int, val preview: String) data class DialogEntry(val name: String, val textId: Int, val preview: String)
@@ -34,7 +34,10 @@ class DialogRenderer(
val className = RenderUtil.className(location) val className = RenderUtil.className(location)
val entries = val entries =
group group
.map { DialogEntry(RenderUtil.entryName(it.label), it.textId, it.preview) } .map {
DialogEntry(
RenderUtil.entryName(it.label), it.textId, RenderUtil.preview(it.text))
}
.distinctBy { it.name } .distinctBy { it.name }
.sortedBy { it.textId } .sortedBy { it.textId }
FileOutput(File(packageRoot, "${className.trim('`')}.kt").toPath()).use { out -> FileOutput(File(packageRoot, "${className.trim('`')}.kt").toPath()).use { out ->
@@ -0,0 +1,52 @@
package de.fiereu.openmmo.codegen.dialog
import java.io.File
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.JsonArray
import kotlinx.serialization.json.JsonObject
import kotlinx.serialization.json.JsonPrimitive
import kotlinx.serialization.json.int
import kotlinx.serialization.json.jsonArray
import kotlinx.serialization.json.jsonObject
import kotlinx.serialization.json.jsonPrimitive
object DialogTable {
private val json = Json { prettyPrint = true }
fun file(dataDir: File, region: String): File = File(dataDir, "$region.json")
fun read(file: File): List<DialogLine>? {
if (!file.isFile) return null
val root = json.parseToJsonElement(file.readText()).jsonObject
return root.getValue("lines").jsonArray.map { element ->
val line = element.jsonObject
DialogLine(
label = line.getValue("label").jsonPrimitive.content,
textId = line.getValue("id").jsonPrimitive.int,
text = line.getValue("text").jsonPrimitive.content,
)
}
}
fun write(file: File, region: String, gameCode: String, lines: List<DialogLine>) {
val body =
JsonObject(
mapOf(
"region" to JsonPrimitive(region),
"gameCode" to JsonPrimitive(gameCode),
"lines" to
JsonArray(
lines.map {
JsonObject(
mapOf(
"label" to JsonPrimitive(it.label),
"id" to JsonPrimitive(it.textId),
"text" to JsonPrimitive(it.text),
))
}),
))
file.parentFile.mkdirs()
file.writeText(json.encodeToString(JsonObject.serializer(), body) + "\n")
}
}
@@ -2,81 +2,23 @@
package de.fiereu.openmmo.codegen.dialog package de.fiereu.openmmo.codegen.dialog
import de.fiereu.openmmo.common.enums.Region
import java.io.File import java.io.File
// A GBA dialog id is the retail ROM file offset with the region on top, which is how the client
// knows the ROM to resolve it in. Captured Kanto ids carry 0 there and Hoenn ids carry 1.
private const val REGION_SHIFT = 28
private fun regionMode(region: String): Int {
val known =
Region.entries.find { it.name.equals(region, ignoreCase = true) }
?: error("unknown region '$region', its dialog ids cannot be built")
return known.wireValue.toInt() shl REGION_SHIFT
}
fun main(args: Array<String>) { fun main(args: Array<String>) {
require(args.size >= 5) { require(args.size >= 5) {
"Usage: <output-dir> <templates-dir> <class-cache-dir> <roms-dir> <region|gameCode|decomp>... got ${args.toList()}" "Usage: <output-dir> <templates-dir> <class-cache-dir> <data-dir> <region>... got ${args.toList()}"
} }
val outputDir = File(args[0]) val outputDir = File(args[0])
val templatesDir = File(args[1]) val templatesDir = File(args[1])
val classCacheDir = File(args[2]) val classCacheDir = File(args[2])
val romsDir = File(args[3]) val dataDir = File(args[3])
for (spec in args.drop(4)) { for (region in args.drop(4)) {
val (region, gameCode, decomp) = spec.split("|") val tableFile = DialogTable.file(dataDir, region)
generateRegion(region, gameCode, File(decomp), romsDir, outputDir, templatesDir, classCacheDir) val lines =
DialogTable.read(tableFile)
?: error("no dialog table at $tableFile, refresh it with :codegen:refreshDialogTable")
println("[dialog] $region: ${lines.size} lines from $tableFile")
DialogRenderer(region, templatesDir, outputDir, classCacheDir).render(lines)
} }
} }
private fun generateRegion(
region: String,
gameCode: String,
decompDir: File,
romsDir: File,
outputDir: File,
templatesDir: File,
classCacheDir: File,
) {
val renderer = DialogRenderer(region, templatesDir, outputDir, classCacheDir)
val rom = RomIndex.find(romsDir, gameCode)
val texts = TextParser(decompDir).parseAll()
println("[dialog] $region: parsed ${texts.size} text labels from $decompDir")
// Without a ROM we cannot resolve textIds, but the enums must still exist so code that
// references them compiles (for example in CI, where ROMs are never present). Emit every
// label with a placeholder textId so the build works, just without real dialog ids.
if (rom == null) {
println(
"[dialog] no $gameCode ROM found in $romsDir, generating $region labels without textIds")
renderer.render(texts.map { DialogLine(it.label, 0, RenderUtil.preview(it.content)) })
return
}
val charmap = Charmap.load(File(decompDir, "charmap.txt"))
val mode = regionMode(region)
var unencodable = 0
var notFound = 0
val lines =
texts.mapNotNull { t ->
val bytes = charmap.encode(t.content)
if (bytes == null) {
unencodable++
return@mapNotNull null
}
val offset = rom.offsetOf(bytes)
if (offset < 0) {
notFound++
return@mapNotNull null
}
DialogLine(t.label, mode or offset, RenderUtil.preview(t.content))
}
println(
"[dialog] $region: resolved ${lines.size} lines (skipped $unencodable unencodable, $notFound not in ROM)")
renderer.render(lines)
}
@@ -0,0 +1,68 @@
@file:JvmName("RefreshMain")
package de.fiereu.openmmo.codegen.dialog
import de.fiereu.openmmo.common.enums.Region
import java.io.File
// A GBA dialog id is the retail ROM file offset with the region on top, which is how the client
// knows the ROM to resolve it in. Captured Kanto ids carry 0 there and Hoenn ids carry 1.
private const val REGION_SHIFT = 28
private fun regionMode(region: String): Int {
val known =
Region.entries.find { it.name.equals(region, ignoreCase = true) }
?: error("unknown region '$region', its dialog ids cannot be built")
return known.wireValue.toInt() shl REGION_SHIFT
}
fun main(args: Array<String>) {
require(args.size >= 3) {
"Usage: <roms-dir> <data-dir> <region|gameCode|decomp>... got ${args.toList()}"
}
val romsDir = File(args[0])
val dataDir = File(args[1])
for (spec in args.drop(2)) {
val (region, gameCode, decomp) = spec.split("|")
refreshRegion(region, gameCode, File(decomp), romsDir, dataDir)
}
}
private fun refreshRegion(
region: String,
gameCode: String,
decompDir: File,
romsDir: File,
dataDir: File,
) {
val rom =
RomIndex.find(romsDir, gameCode)
?: error("no $gameCode ROM in $romsDir, so $region dialog ids cannot be resolved")
val texts = TextParser(decompDir).parseAll()
val charmap = Charmap.load(File(decompDir, "charmap.txt"))
val mode = regionMode(region)
var unencodable = 0
var notFound = 0
val lines =
texts.mapNotNull { t ->
val bytes = charmap.encode(t.content)
if (bytes == null) {
unencodable++
return@mapNotNull null
}
val offset = rom.offsetOf(bytes)
if (offset < 0) {
notFound++
return@mapNotNull null
}
DialogLine(t.label, mode or offset, t.content)
}
val file = DialogTable.file(dataDir, region)
DialogTable.write(file, region, gameCode, lines)
println(
"[dialog] $region: wrote ${lines.size} lines to $file (skipped $unencodable unencodable, $notFound not in ROM)")
}