aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/moe/nea/notenoughupdates/util/config/ConfigHolder.kt
diff options
context:
space:
mode:
authornea <romangraef@gmail.com>2022-10-22 00:34:22 +0200
committernea <romangraef@gmail.com>2022-10-22 00:34:22 +0200
commitf85c449ed586c7ced780423943e55bfa5abaeb0f (patch)
tree0c5f59c73cc9e5fcb07de6e3f1f102e2413def27 /src/main/kotlin/moe/nea/notenoughupdates/util/config/ConfigHolder.kt
parentc98d4693f1fc9dcae8d78cb33e6233629f4cb2e7 (diff)
downloadFirmament-f85c449ed586c7ced780423943e55bfa5abaeb0f.tar.gz
Firmament-f85c449ed586c7ced780423943e55bfa5abaeb0f.tar.bz2
Firmament-f85c449ed586c7ced780423943e55bfa5abaeb0f.zip
rudimentary config gui (again)
Diffstat (limited to 'src/main/kotlin/moe/nea/notenoughupdates/util/config/ConfigHolder.kt')
-rw-r--r--src/main/kotlin/moe/nea/notenoughupdates/util/config/ConfigHolder.kt60
1 files changed, 0 insertions, 60 deletions
diff --git a/src/main/kotlin/moe/nea/notenoughupdates/util/config/ConfigHolder.kt b/src/main/kotlin/moe/nea/notenoughupdates/util/config/ConfigHolder.kt
deleted file mode 100644
index e8a9649..0000000
--- a/src/main/kotlin/moe/nea/notenoughupdates/util/config/ConfigHolder.kt
+++ /dev/null
@@ -1,60 +0,0 @@
-package moe.nea.notenoughupdates.util.config
-
-import java.nio.file.Path
-import kotlinx.serialization.KSerializer
-import kotlin.io.path.exists
-import kotlin.io.path.readText
-import kotlin.io.path.writeText
-import moe.nea.notenoughupdates.NotEnoughUpdates
-
-abstract class ConfigHolder<T>(
- val serializer: KSerializer<T>,
- val name: String,
- val default: () -> T
-) : IConfigHolder<T> {
-
-
- final override var config: T
- private set
-
- init {
- config = readValueOrDefault()
- IConfigHolder.putConfig(this::class, this)
- }
-
- private val file: Path get() = NotEnoughUpdates.CONFIG_DIR.resolve("$name.json")
-
- protected fun readValueOrDefault(): T {
- if (file.exists())
- try {
- return NotEnoughUpdates.json.decodeFromString(
- serializer,
- file.readText()
- )
- } catch (e: Exception) {/* Expecting IOException and SerializationException, but Kotlin doesn't allow multi catches*/
- IConfigHolder.badLoads.add(name)
- NotEnoughUpdates.logger.error(
- "Exception during loading of config file $name. This will reset this config.",
- e
- )
- }
- return default()
- }
-
- private fun writeValue(t: T) {
- file.writeText(NotEnoughUpdates.json.encodeToString(serializer, t))
- }
-
- override fun save() {
- writeValue(config)
- }
-
- override fun load() {
- config = readValueOrDefault()
- }
-
- override fun markDirty() {
- IConfigHolder.markDirty(this::class)
- }
-
-}