Add config file parser

Signed-off-by: Graham <gpe@openrs2.dev>
Graham 5 years ago
parent d32ea82032
commit d119cc07b5
  1. 3
      all/build.gradle.kts
  2. 26
      conf/build.gradle.kts
  3. 23
      conf/src/main/java/dev/openrs2/conf/Config.kt
  4. 15
      conf/src/main/java/dev/openrs2/conf/ConfigModule.kt
  5. 24
      conf/src/main/java/dev/openrs2/conf/ConfigProvider.kt
  6. 1
      etc/.gitignore
  7. 10
      etc/config.example.yaml
  8. 1
      settings.gradle.kts

@ -50,6 +50,9 @@ distributions {
from("${rootProject.projectDir}/DCO") from("${rootProject.projectDir}/DCO")
from("${rootProject.projectDir}/LICENSE") from("${rootProject.projectDir}/LICENSE")
from("${rootProject.projectDir}/README.md") from("${rootProject.projectDir}/README.md")
from("${rootProject.projectDir}/etc/config.example.yaml") {
into("etc")
}
from("${rootProject.projectDir}/share") { from("${rootProject.projectDir}/share") {
exclude(".*", "*~") exclude(".*", "*~")
into("share") into("share")

@ -0,0 +1,26 @@
plugins {
`maven-publish`
kotlin("jvm")
}
dependencies {
api("com.google.inject:guice:${Versions.guice}")
implementation(project(":yaml"))
}
publishing {
publications.create<MavenPublication>("maven") {
from(components["java"])
pom {
packaging = "jar"
name.set("OpenRS2 Configuration")
description.set(
"""
Provides a parser for OpenRS2's main configuration file.
""".trimIndent()
)
}
}
}

@ -0,0 +1,23 @@
package dev.openrs2.conf
data class Config(
val game: String,
val operator: String,
val domain: String
) {
val internalGame: String
val internalOperator: String
init {
internalGame = game.toInternalName()
internalOperator = operator.toInternalName()
}
companion object {
private val INTERNAL_NAME_REGEX = Regex("(?i)[^a-z0-9]+")
private fun String.toInternalName(): String {
return replace(INTERNAL_NAME_REGEX, "_").trim('_').toLowerCase()
}
}
}

@ -0,0 +1,15 @@
package dev.openrs2.conf
import com.google.inject.AbstractModule
import com.google.inject.Scopes
import dev.openrs2.yaml.YamlModule
class ConfigModule : AbstractModule() {
override fun configure() {
install(YamlModule())
bind(Config::class.java)
.toProvider(ConfigProvider::class.java)
.`in`(Scopes.SINGLETON)
}
}

@ -0,0 +1,24 @@
package dev.openrs2.conf
import com.fasterxml.jackson.databind.ObjectMapper
import java.nio.file.Files
import java.nio.file.Paths
import javax.inject.Inject
import javax.inject.Provider
class ConfigProvider @Inject constructor(private val mapper: ObjectMapper) : Provider<Config> {
override fun get(): Config {
if (Files.notExists(CONFIG_PATH)) {
Files.copy(EXAMPLE_CONFIG_PATH, CONFIG_PATH)
}
return Files.newBufferedReader(CONFIG_PATH).use { reader ->
mapper.readValue(reader, Config::class.java)
}
}
companion object {
private val CONFIG_PATH = Paths.get("etc/config.yaml")
private val EXAMPLE_CONFIG_PATH = Paths.get("etc/config.example.yaml")
}
}

1
etc/.gitignore vendored

@ -1,3 +1,4 @@
/config.yaml
/loader.p12 /loader.p12
/private.key /private.key
/public.key /public.key

@ -0,0 +1,10 @@
---
# The name of the game. All references to "RuneScape" are replaced with this
# string.
game: OpenRS2
# The name of the organisation running the game. All references to "Jagex" are
# replaced with this string.
operator: OpenRS2
# The game's domain name. All references to "runescape.com" are replaced with
# this string. It should, at a minimum, have `www` and `www-wtqa` hostnames.
domain: openrs2.dev

@ -9,6 +9,7 @@ include(
"cli", "cli",
"compress", "compress",
"compress-cli", "compress-cli",
"conf",
"crc32", "crc32",
"crypto", "crypto",
"decompiler", "decompiler",

Loading…
Cancel
Save