aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/moe/nea/notenoughupdates/util/config/ConfigHolder.kt
diff options
context:
space:
mode:
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)
- }
-
-}