aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/moe/nea/notenoughupdates/util/config/ProfileSpecificConfigHolder.kt
diff options
context:
space:
mode:
authornea <romangraef@gmail.com>2022-09-28 12:45:56 +0200
committernea <romangraef@gmail.com>2022-09-28 12:45:56 +0200
commit4d73331a449f0b0647066f7dde0628730fe0e178 (patch)
tree047f463e13d14ea6cf9c8b37602a756f6880f9a0 /src/main/kotlin/moe/nea/notenoughupdates/util/config/ProfileSpecificConfigHolder.kt
parentec66c82198fe2d61d699d553c1254f08b43fcc65 (diff)
downloadFirmament-4d73331a449f0b0647066f7dde0628730fe0e178.tar.gz
Firmament-4d73331a449f0b0647066f7dde0628730fe0e178.tar.bz2
Firmament-4d73331a449f0b0647066f7dde0628730fe0e178.zip
Fairy souls
Diffstat (limited to 'src/main/kotlin/moe/nea/notenoughupdates/util/config/ProfileSpecificConfigHolder.kt')
-rw-r--r--src/main/kotlin/moe/nea/notenoughupdates/util/config/ProfileSpecificConfigHolder.kt81
1 files changed, 81 insertions, 0 deletions
diff --git a/src/main/kotlin/moe/nea/notenoughupdates/util/config/ProfileSpecificConfigHolder.kt b/src/main/kotlin/moe/nea/notenoughupdates/util/config/ProfileSpecificConfigHolder.kt
new file mode 100644
index 0000000..44a79c4
--- /dev/null
+++ b/src/main/kotlin/moe/nea/notenoughupdates/util/config/ProfileSpecificConfigHolder.kt
@@ -0,0 +1,81 @@
+package moe.nea.notenoughupdates.util.config
+
+import java.nio.file.Path
+import kotlinx.serialization.KSerializer
+import kotlin.io.path.createDirectories
+import kotlin.io.path.deleteExisting
+import kotlin.io.path.exists
+import kotlin.io.path.extension
+import kotlin.io.path.listDirectoryEntries
+import kotlin.io.path.nameWithoutExtension
+import kotlin.io.path.readText
+import kotlin.io.path.writeText
+import moe.nea.notenoughupdates.NotEnoughUpdates
+import moe.nea.notenoughupdates.util.SBData
+
+abstract class ProfileSpecificConfigHolder<S>(
+ private val configSerializer: KSerializer<S>,
+ val configName: String,
+ private val configDefault: () -> S
+) : IConfigHolder<S?> {
+
+ var allConfigs: MutableMap<String, S>
+
+ override val config: S?
+ get() = SBData.profileCuteName?.let {
+ allConfigs.computeIfAbsent(it) { configDefault() }
+ }
+
+ init {
+ allConfigs = readValues()
+ readValues()
+ }
+
+ private val configDirectory: Path get() = NotEnoughUpdates.CONFIG_DIR.resolve("profiles")
+
+ private fun readValues(): MutableMap<String, S> {
+ if (!configDirectory.exists()) {
+ configDirectory.createDirectories()
+ }
+ val profileFiles = configDirectory.listDirectoryEntries()
+ return profileFiles
+ .filter { it.extension == "json" }
+ .mapNotNull {
+ try {
+ it.nameWithoutExtension to NotEnoughUpdates.json.decodeFromString(configSerializer, it.readText())
+ } catch (e: Exception) { /* Expecting IOException and SerializationException, but Kotlin doesn't allow multi catches*/
+ IConfigHolder.badLoads.add(configName)
+ NotEnoughUpdates.logger.error(
+ "Exception during loading of profile specific config file $it ($configName). This will reset that profiles config.",
+ e
+ )
+ null
+ }
+ }.toMap().toMutableMap()
+ }
+
+ override fun save() {
+ if (!configDirectory.exists()) {
+ configDirectory.createDirectories()
+ }
+ val c = allConfigs
+ configDirectory.listDirectoryEntries().forEach {
+ if (it.nameWithoutExtension !in c) {
+ it.deleteExisting()
+ }
+ }
+ c.forEach { (name, value) ->
+ val f = configDirectory.resolve("$name.json")
+ f.writeText(NotEnoughUpdates.json.encodeToString(configSerializer, value))
+ }
+ }
+
+ override fun markDirty() {
+ IConfigHolder.markDirty(this::class)
+ }
+
+ override fun load() {
+ allConfigs = readValues()
+ }
+
+}