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

+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.
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")
// Committed, because a ROM is the only place these ids exist and no runner has one.
val dialogDataDir = layout.projectDirectory.dir("dialog")
jteCodegen {
register("maps") {
mainClass.set("de.fiereu.openmmo.codegen.maps.Main")
@@ -95,21 +98,28 @@ jteCodegen {
inputDirs.from(sourceDecompDir)
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") {
mainClass.set("de.fiereu.openmmo.codegen.dialog.Main")
val fireredDir = rootProject.layout.projectDirectory.dir("decomp/pokefirered")
inputDirs.from(sourceDecompDir, fireredDir, romsDir)
extraArgs.set(
listOf(
romsDir.asFile.absolutePath,
"hoenn|BPEE|${sourceDecompDir.asFile.absolutePath}",
"kanto|BPRE|${fireredDir.asFile.absolutePath}",
))
inputDirs.from(dialogDataDir)
extraArgs.set(listOf(dialogDataDir.asFile.absolutePath) + regionSources.keys)
}
}
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
// `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.
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
/**
* 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)
data class DialogLine(val label: String, val textId: Int, val text: String)
/** A single enum entry inside a location's dialog enum. */
data class DialogEntry(val name: String, val textId: Int, val preview: String)
@@ -34,7 +34,10 @@ class DialogRenderer(
val className = RenderUtil.className(location)
val entries =
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 }
.sortedBy { it.textId }
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
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 >= 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 templatesDir = File(args[1])
val classCacheDir = File(args[2])
val romsDir = File(args[3])
val dataDir = File(args[3])
for (spec in args.drop(4)) {
val (region, gameCode, decomp) = spec.split("|")
generateRegion(region, gameCode, File(decomp), romsDir, outputDir, templatesDir, classCacheDir)
for (region in args.drop(4)) {
val tableFile = DialogTable.file(dataDir, region)
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)")
}