feat(game): persist per-character story flags and vars

This commit is contained in:
Fiereu committed 2026-07-26 14:36:07 +02:00
1 parent 4879082eb0
commit 023f4eb305
13 files changed
+347 -2

No files matched your search

@@ -0,0 +1,17 @@
@import java.util.List
@param String pkg
@param String objectName
@param String namespace
@param List<String> names
package ${pkg}
/**
* Story keys generated from the GBA decomp. Each value is the region namespaced key a script hands
* to ScriptContext (setFlag/isFlagSet, setVar/getVar). Generated, do not edit by hand.
*/
object ${objectName} {
@for(String name : names)
const val ${name} = "${namespace}/${name}"
@endfor
}
@@ -0,0 +1,23 @@
package de.fiereu.openmmo.codegen.story
import java.io.File
/**
* Pulls the flag and var names out of a GBA decomp. Only the names matter here, the numeric values
* are the client's concern and are not needed to key the game agnostic story store. The FLAG_/VAR_
* prefix match skips the range markers like TEMP_FLAGS_START and FLAGS_COUNT on its own.
*/
object FlagVarParser {
fun flags(decompDir: File): List<String> =
names(File(decompDir, "include/constants/flags.h"), "FLAG_")
fun vars(decompDir: File): List<String> =
names(File(decompDir, "include/constants/vars.h"), "VAR_")
private fun names(file: File, prefix: String): List<String> {
require(file.exists()) { "Missing ${file.path}" }
val re = Regex("""^#define\s+($prefix\w+)\b""")
return file.readLines().mapNotNull { re.find(it.trim())?.groupValues?.get(1) }.distinct()
}
}
@@ -0,0 +1,23 @@
@file:JvmName("Main")
package de.fiereu.openmmo.codegen.story
import java.io.File
fun main(args: Array<String>) {
require(args.size >= 4) {
"Usage: <output-dir> <templates-dir> <class-cache-dir> <region|decomp>... got ${args.toList()}"
}
val outputDir = File(args[0])
val templatesDir = File(args[1])
val classCacheDir = File(args[2])
for (spec in args.drop(3)) {
val (region, decomp) = spec.split("|")
val decompDir = File(decomp)
val flags = FlagVarParser.flags(decompDir)
val vars = FlagVarParser.vars(decompDir)
println("[story] $region: parsed ${flags.size} flags, ${vars.size} vars from $decompDir")
StoryConstantsRenderer(region, templatesDir, outputDir, classCacheDir).render(flags, vars)
}
}
@@ -0,0 +1,61 @@
package de.fiereu.openmmo.codegen.story
import gg.jte.ContentType
import gg.jte.TemplateEngine
import gg.jte.output.FileOutput
import gg.jte.resolve.DirectoryCodeResolver
import java.io.File
/**
* Emits one Kotlin object of flag key constants and one of var key constants per region. Each
* constant value is the region namespaced story key ("hoenn/FLAG_ADVENTURE_STARTED") that scripts
* hand to [de.fiereu.openmmo.server.game.script.ScriptContext]. The namespace keeps the two
* regions' overlapping names from colliding in the store.
*/
class StoryConstantsRenderer(
region: String,
private val templatesDir: File,
private val outputDir: File,
private val classCacheDir: File,
) {
private val namespace = region
private val basePackage = "de.fiereu.openmmo.story.generated.$region"
private val objectPrefix = region.replaceFirstChar { it.uppercase() }
fun render(flags: List<String>, vars: List<String>) {
classCacheDir.mkdirs()
val engine =
TemplateEngine.create(
DirectoryCodeResolver(templatesDir.toPath()),
classCacheDir.toPath(),
ContentType.Plain,
)
val packageRoot = File(outputDir, basePackage.replace('.', '/'))
if (packageRoot.exists()) packageRoot.deleteRecursively()
packageRoot.mkdirs()
write(engine, packageRoot, "${objectPrefix}Flags", flags)
write(engine, packageRoot, "${objectPrefix}Vars", vars)
}
private fun write(
engine: TemplateEngine,
packageRoot: File,
objectName: String,
names: List<String>,
) {
FileOutput(File(packageRoot, "$objectName.kt").toPath()).use { out ->
engine.render(
"StoryConstants.jte",
mapOf(
"pkg" to basePackage,
"objectName" to objectName,
"namespace" to namespace,
"names" to names,
),
out,
)
}
}
}