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.ConfigException
|
||||
import com.typesafe.config.ConfigFactory
|
||||
import com.typesafe.config.ConfigFactory.*
|
||||
import com.typesafe.config.ConfigRenderOptions
|
||||
import fr.username404.snowygui.Snowy
|
||||
import io.github.config4k.extract
|
||||
|
@ -14,12 +14,6 @@ import java.io.File
|
|||
|
||||
object Configuration {
|
||||
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 {
|
||||
launch {
|
||||
file.writeText(
|
||||
|
@ -40,19 +34,27 @@ object Configuration {
|
|||
}
|
||||
@JvmField
|
||||
val obtained: Config = run {
|
||||
var result: Config = baseConf
|
||||
var result: Config = empty()
|
||||
with(file) {
|
||||
if (!exists()) {
|
||||
createNewFile()
|
||||
setWritable(true)
|
||||
setReadable(true)
|
||||
} else try {
|
||||
result = ConfigFactory.parseFile(file)
|
||||
result = parseFile(file)
|
||||
} catch (e: ConfigException) {
|
||||
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 {
|
||||
runBlocking {
|
||||
writeConfig(it)
|
||||
|
|
Loading…
Reference in New Issue