Use the default configuration as a fallback in Configuration.kt
This commit is contained in:
parent
1a4ba27e36
commit
7cc0f17fc4
|
@ -2,7 +2,7 @@ package fr.username404.snowygui.config
|
||||||
|
|
||||||
import com.typesafe.config.Config
|
import com.typesafe.config.Config
|
||||||
import com.typesafe.config.ConfigException
|
import com.typesafe.config.ConfigException
|
||||||
import com.typesafe.config.ConfigFactory
|
import com.typesafe.config.ConfigFactory.*
|
||||||
import com.typesafe.config.ConfigRenderOptions
|
import com.typesafe.config.ConfigRenderOptions
|
||||||
import fr.username404.snowygui.Snowy
|
import fr.username404.snowygui.Snowy
|
||||||
import io.github.config4k.extract
|
import io.github.config4k.extract
|
||||||
|
@ -14,12 +14,6 @@ import java.io.File
|
||||||
|
|
||||||
object Configuration {
|
object Configuration {
|
||||||
private val file: File = File(Minecraft.getInstance().gameDirectory.absolutePath + File.separator + "snowy.conf")
|
private val file: File = File(Minecraft.getInstance().gameDirectory.absolutePath + File.separator + "snowy.conf")
|
||||||
private val base = """
|
|
||||||
|Snowy {
|
|
||||||
| displayInitMessage = true
|
|
||||||
|}
|
|
||||||
""".trimMargin()
|
|
||||||
private val baseConf = ConfigFactory.parseString(base)
|
|
||||||
private suspend fun writeConfig(c: Config) = coroutineScope {
|
private suspend fun writeConfig(c: Config) = coroutineScope {
|
||||||
launch {
|
launch {
|
||||||
file.writeText(
|
file.writeText(
|
||||||
|
@ -40,19 +34,27 @@ object Configuration {
|
||||||
}
|
}
|
||||||
@JvmField
|
@JvmField
|
||||||
val obtained: Config = run {
|
val obtained: Config = run {
|
||||||
var result: Config = baseConf
|
var result: Config = empty()
|
||||||
with(file) {
|
with(file) {
|
||||||
if (!exists()) {
|
if (!exists()) {
|
||||||
createNewFile()
|
createNewFile()
|
||||||
setWritable(true)
|
setWritable(true)
|
||||||
setReadable(true)
|
setReadable(true)
|
||||||
} else try {
|
} else try {
|
||||||
result = ConfigFactory.parseFile(file)
|
result = parseFile(file)
|
||||||
} catch (e: ConfigException) {
|
} catch (e: ConfigException) {
|
||||||
Snowy.logs.warn("Could not parse the snowy configuration file, the default configuration will be used instead.")
|
Snowy.logs.warn("Could not parse the snowy configuration file, the default configuration will be used instead.")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
ConfigFactory.load(result).extract<Config>("Snowy").resolveWith(baseConf)
|
load(result).withFallback(
|
||||||
|
parseString(
|
||||||
|
"""
|
||||||
|
|Snowy {
|
||||||
|
| displayInitMessage = true
|
||||||
|
|}
|
||||||
|
""".trimMargin()
|
||||||
|
)
|
||||||
|
).extract<Config>("Snowy")
|
||||||
}.also {
|
}.also {
|
||||||
runBlocking {
|
runBlocking {
|
||||||
writeConfig(it)
|
writeConfig(it)
|
||||||
|
|
Loading…
Reference in New Issue