diff options
author | nea <romangraef@gmail.com> | 2022-09-28 12:45:56 +0200 |
---|---|---|
committer | nea <romangraef@gmail.com> | 2022-09-28 12:45:56 +0200 |
commit | 4d73331a449f0b0647066f7dde0628730fe0e178 (patch) | |
tree | 047f463e13d14ea6cf9c8b37602a756f6880f9a0 /src/main/kotlin/moe/nea/notenoughupdates/util/config | |
parent | ec66c82198fe2d61d699d553c1254f08b43fcc65 (diff) | |
download | Firmament-4d73331a449f0b0647066f7dde0628730fe0e178.tar.gz Firmament-4d73331a449f0b0647066f7dde0628730fe0e178.tar.bz2 Firmament-4d73331a449f0b0647066f7dde0628730fe0e178.zip |
Fairy souls
Diffstat (limited to 'src/main/kotlin/moe/nea/notenoughupdates/util/config')
3 files changed, 216 insertions, 0 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 new file mode 100644 index 0000000..e8a9649 --- /dev/null +++ b/src/main/kotlin/moe/nea/notenoughupdates/util/config/ConfigHolder.kt @@ -0,0 +1,60 @@ +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) + } + +} diff --git a/src/main/kotlin/moe/nea/notenoughupdates/util/config/IConfigHolder.kt b/src/main/kotlin/moe/nea/notenoughupdates/util/config/IConfigHolder.kt new file mode 100644 index 0000000..2acc99d --- /dev/null +++ b/src/main/kotlin/moe/nea/notenoughupdates/util/config/IConfigHolder.kt @@ -0,0 +1,75 @@ +package moe.nea.notenoughupdates.util.config + +import java.util.concurrent.CopyOnWriteArrayList +import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientLifecycleEvents +import kotlin.reflect.KClass +import net.minecraft.client.MinecraftClient +import net.minecraft.server.command.CommandOutput +import net.minecraft.text.Text +import moe.nea.notenoughupdates.NotEnoughUpdates +import moe.nea.notenoughupdates.events.ScreenOpenEvent + +interface IConfigHolder<T> { + companion object { + internal var badLoads: MutableList<String> = CopyOnWriteArrayList() + private val allConfigs: MutableMap<KClass<out IConfigHolder<*>>, IConfigHolder<*>> = mutableMapOf() + private val dirty: MutableSet<KClass<out IConfigHolder<*>>> = mutableSetOf() + + internal fun <T : IConfigHolder<K>, K> putConfig(kClass: KClass<T>, inst: IConfigHolder<K>) { + allConfigs[kClass] = inst + } + + fun <T : IConfigHolder<K>, K> markDirty(kClass: KClass<T>) { + if (kClass !in allConfigs) { + NotEnoughUpdates.logger.error("Tried to markDirty '${kClass.qualifiedName}', which isn't registered as 'IConfigHolder'") + return + } + dirty.add(kClass) + } + + private fun performSaves() { + val toSave = dirty.toList().also { + dirty.clear() + } + for (it in toSave) { + val obj = allConfigs[it] + if (obj == null) { + NotEnoughUpdates.logger.error("Tried to save '${it}', which isn't registered as 'ConfigHolder'") + continue + } + obj.save() + } + } + + private fun warnForResetConfigs(player: CommandOutput) { + if (badLoads.isNotEmpty()) { + player.sendMessage( + Text.literal( + "The following configs have been reset: ${badLoads.joinToString(", ")}. " + + "This can be intentional, but probably isn't." + ) + ) + badLoads.clear() + } + } + + fun registerEvents() { + ScreenOpenEvent.subscribe { event -> + performSaves() + val p = MinecraftClient.getInstance().player + if (p != null) { + warnForResetConfigs(p) + } + } + ClientLifecycleEvents.CLIENT_STOPPING.register(ClientLifecycleEvents.ClientStopping { + performSaves() + }) + } + + } + + val config: T + fun save() + fun markDirty() + fun load() +} 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() + } + +} |