feat(game): persist per-character story flags and vars
This commit is contained in:
1 parent
4879082eb0
commit
023f4eb305
13 files changed
+347
-2
No files matched your search
@@ -53,6 +53,18 @@ jteCodegen {
|
||||
inputDirs.from(sourceDecompDir)
|
||||
extraArgs.set(listOf(sourceDecompDir.asFile.absolutePath))
|
||||
}
|
||||
// Per region flag and var key constants for scripts. Names come from each decomp, so this is by
|
||||
// region like maps. The generic story store in server.game does not depend on these, they are the
|
||||
// GBA adapter that gives ported scripts readable keys.
|
||||
register("story") {
|
||||
mainClass.set("de.fiereu.openmmo.codegen.story.Main")
|
||||
inputDirs.from(
|
||||
regionSources.values.map { rootProject.layout.projectDirectory.dir("decomp/$it") })
|
||||
extraArgs.set(
|
||||
regionSources.map { (region, decomp) ->
|
||||
"$region|${rootProject.layout.projectDirectory.dir("decomp/$decomp").asFile.absolutePath}"
|
||||
})
|
||||
}
|
||||
register("typechart") {
|
||||
mainClass.set("de.fiereu.openmmo.codegen.typechart.Main")
|
||||
inputDirs.from(sourceDecompDir)
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
+61
@@ -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,
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user