Save the Snowy configuration at exit

This commit is contained in:
Username404-59 2021-04-23 15:29:48 +02:00
parent 0c107be94b
commit 994a377a26
Signed by: Username404-59
GPG Key ID: 7AB361FBB257A5D1
1 changed files with 24 additions and 3 deletions

View File

@ -3,6 +3,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.ConfigRenderOptions
import fr.username404.snowygui.Snowy
import net.minecraft.client.Minecraft
import java.io.File
@ -14,6 +15,7 @@ object Configuration {
| displayInitMessage = true
|}
""".trimMargin()
private val baseConf = ConfigFactory.parseString(base)
@JvmField
val obtained: Config = run {
lateinit var result: Config
@ -26,10 +28,29 @@ object Configuration {
}; result = try {
ConfigFactory.parseFile(file)
} catch (e: ConfigException) {
Snowy.logs.warn("Could not parse the snowy configuration file, the default configuration will be used instead.")
ConfigFactory.parseString(base)
Snowy.logs.warn("Could not parse the snowy configuration file, the default configuration will be used instead."); baseConf
}
}
ConfigFactory.load(result).getConfig("Snowy")
ConfigFactory.load(result).getConfig("Snowy").resolveWith(baseConf)
}
init {
Runtime.getRuntime().addShutdownHook(
Thread {
file.writeText(
"""
Snowy {
${
obtained.root().render(
ConfigRenderOptions.defaults()
.setFormatted(true)
.setJson(false)
.setOriginComments(false)
).trimEnd()
}
}
""".trimIndent()
)
}
)
}
}