From d119cc07b52e5979df5d261e0cbe610f46fea458 Mon Sep 17 00:00:00 2001 From: Graham Date: Mon, 27 Apr 2020 21:48:40 +0100 Subject: [PATCH] Add config file parser Signed-off-by: Graham --- all/build.gradle.kts | 3 +++ conf/build.gradle.kts | 26 +++++++++++++++++++ conf/src/main/java/dev/openrs2/conf/Config.kt | 23 ++++++++++++++++ .../java/dev/openrs2/conf/ConfigModule.kt | 15 +++++++++++ .../java/dev/openrs2/conf/ConfigProvider.kt | 24 +++++++++++++++++ etc/.gitignore | 1 + etc/config.example.yaml | 10 +++++++ settings.gradle.kts | 1 + 8 files changed, 103 insertions(+) create mode 100644 conf/build.gradle.kts create mode 100644 conf/src/main/java/dev/openrs2/conf/Config.kt create mode 100644 conf/src/main/java/dev/openrs2/conf/ConfigModule.kt create mode 100644 conf/src/main/java/dev/openrs2/conf/ConfigProvider.kt create mode 100644 etc/config.example.yaml diff --git a/all/build.gradle.kts b/all/build.gradle.kts index e738b8a7ea..208789dc24 100644 --- a/all/build.gradle.kts +++ b/all/build.gradle.kts @@ -50,6 +50,9 @@ distributions { from("${rootProject.projectDir}/DCO") from("${rootProject.projectDir}/LICENSE") from("${rootProject.projectDir}/README.md") + from("${rootProject.projectDir}/etc/config.example.yaml") { + into("etc") + } from("${rootProject.projectDir}/share") { exclude(".*", "*~") into("share") diff --git a/conf/build.gradle.kts b/conf/build.gradle.kts new file mode 100644 index 0000000000..ee1afe46de --- /dev/null +++ b/conf/build.gradle.kts @@ -0,0 +1,26 @@ +plugins { + `maven-publish` + kotlin("jvm") +} + +dependencies { + api("com.google.inject:guice:${Versions.guice}") + + implementation(project(":yaml")) +} + +publishing { + publications.create("maven") { + from(components["java"]) + + pom { + packaging = "jar" + name.set("OpenRS2 Configuration") + description.set( + """ + Provides a parser for OpenRS2's main configuration file. + """.trimIndent() + ) + } + } +} diff --git a/conf/src/main/java/dev/openrs2/conf/Config.kt b/conf/src/main/java/dev/openrs2/conf/Config.kt new file mode 100644 index 0000000000..2a00b041b0 --- /dev/null +++ b/conf/src/main/java/dev/openrs2/conf/Config.kt @@ -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() + } + } +} diff --git a/conf/src/main/java/dev/openrs2/conf/ConfigModule.kt b/conf/src/main/java/dev/openrs2/conf/ConfigModule.kt new file mode 100644 index 0000000000..b1be1fbee3 --- /dev/null +++ b/conf/src/main/java/dev/openrs2/conf/ConfigModule.kt @@ -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) + } +} diff --git a/conf/src/main/java/dev/openrs2/conf/ConfigProvider.kt b/conf/src/main/java/dev/openrs2/conf/ConfigProvider.kt new file mode 100644 index 0000000000..61f509bfbe --- /dev/null +++ b/conf/src/main/java/dev/openrs2/conf/ConfigProvider.kt @@ -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 { + 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") + } +} diff --git a/etc/.gitignore b/etc/.gitignore index 2fb49ccbc0..ff48994cf8 100644 --- a/etc/.gitignore +++ b/etc/.gitignore @@ -1,3 +1,4 @@ +/config.yaml /loader.p12 /private.key /public.key diff --git a/etc/config.example.yaml b/etc/config.example.yaml new file mode 100644 index 0000000000..c2dc9b70c4 --- /dev/null +++ b/etc/config.example.yaml @@ -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 diff --git a/settings.gradle.kts b/settings.gradle.kts index 1d70b24d48..11032624a6 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -9,6 +9,7 @@ include( "cli", "compress", "compress-cli", + "conf", "crc32", "crypto", "decompiler",