aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/moe/nea/firmament/util/data
diff options
context:
space:
mode:
authornea <nea@nea.moe>2023-05-16 01:23:43 +0200
committernea <nea@nea.moe>2023-05-16 01:23:43 +0200
commitead6762eb1c005914b05f9d3c29f334989c67513 (patch)
treecd1409756be2bc4a93195c31d432fef053afe002 /src/main/kotlin/moe/nea/firmament/util/data
parent96c546cc73880a7c502c17aadda6ca84c847692d (diff)
downloadFirmament-ead6762eb1c005914b05f9d3c29f334989c67513.tar.gz
Firmament-ead6762eb1c005914b05f9d3c29f334989c67513.tar.bz2
Firmament-ead6762eb1c005914b05f9d3c29f334989c67513.zip
Replace references to NEU with Firmament
Diffstat (limited to 'src/main/kotlin/moe/nea/firmament/util/data')
-rw-r--r--src/main/kotlin/moe/nea/firmament/util/data/DataHolder.kt60
-rw-r--r--src/main/kotlin/moe/nea/firmament/util/data/IDataHolder.kt75
-rw-r--r--src/main/kotlin/moe/nea/firmament/util/data/ProfileSpecificDataHolder.kt82
3 files changed, 217 insertions, 0 deletions
diff --git a/src/main/kotlin/moe/nea/firmament/util/data/DataHolder.kt b/src/main/kotlin/moe/nea/firmament/util/data/DataHolder.kt
new file mode 100644
index 0000000..4d66a96
--- /dev/null
+++ b/src/main/kotlin/moe/nea/firmament/util/data/DataHolder.kt
@@ -0,0 +1,60 @@
+package moe.nea.firmament.util.data
+
+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.firmament.Firmament
+
+abstract class DataHolder<T>(
+ val serializer: KSerializer<T>,
+ val name: String,
+ val default: () -> T
+) : IDataHolder<T> {
+
+
+ final override var data: T
+ private set
+
+ init {
+ data = readValueOrDefault()
+ IDataHolder.putDataHolder(this::class, this)
+ }
+
+ private val file: Path get() = Firmament.CONFIG_DIR.resolve("$name.json")
+
+ protected fun readValueOrDefault(): T {
+ if (file.exists())
+ try {
+ return Firmament.json.decodeFromString(
+ serializer,
+ file.readText()
+ )
+ } catch (e: Exception) {/* Expecting IOException and SerializationException, but Kotlin doesn't allow multi catches*/
+ IDataHolder.badLoads.add(name)
+ Firmament.logger.error(
+ "Exception during loading of config file $name. This will reset this config.",
+ e
+ )
+ }
+ return default()
+ }
+
+ private fun writeValue(t: T) {
+ file.writeText(Firmament.json.encodeToString(serializer, t))
+ }
+
+ override fun save() {
+ writeValue(data)
+ }
+
+ override fun load() {
+ data = readValueOrDefault()
+ }
+
+ override fun markDirty() {
+ IDataHolder.markDirty(this::class)
+ }
+
+}
diff --git a/src/main/kotlin/moe/nea/firmament/util/data/IDataHolder.kt b/src/main/kotlin/moe/nea/firmament/util/data/IDataHolder.kt
new file mode 100644
index 0000000..4fa8c38
--- /dev/null
+++ b/src/main/kotlin/moe/nea/firmament/util/data/IDataHolder.kt
@@ -0,0 +1,75 @@
+package moe.nea.firmament.util.data
+
+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.firmament.Firmament
+import moe.nea.firmament.events.ScreenOpenEvent
+
+interface IDataHolder<T> {
+ companion object {
+ internal var badLoads: MutableList<String> = CopyOnWriteArrayList()
+ private val allConfigs: MutableMap<KClass<out IDataHolder<*>>, IDataHolder<*>> = mutableMapOf()
+ private val dirty: MutableSet<KClass<out IDataHolder<*>>> = mutableSetOf()
+
+ internal fun <T : IDataHolder<K>, K> putDataHolder(kClass: KClass<T>, inst: IDataHolder<K>) {
+ allConfigs[kClass] = inst
+ }
+
+ fun <T : IDataHolder<K>, K> markDirty(kClass: KClass<T>) {
+ if (kClass !in allConfigs) {
+ Firmament.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) {
+ Firmament.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 data: T
+ fun save()
+ fun markDirty()
+ fun load()
+}
diff --git a/src/main/kotlin/moe/nea/firmament/util/data/ProfileSpecificDataHolder.kt b/src/main/kotlin/moe/nea/firmament/util/data/ProfileSpecificDataHolder.kt
new file mode 100644
index 0000000..c3e1d38
--- /dev/null
+++ b/src/main/kotlin/moe/nea/firmament/util/data/ProfileSpecificDataHolder.kt
@@ -0,0 +1,82 @@
+package moe.nea.firmament.util.data
+
+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.firmament.Firmament
+import moe.nea.firmament.util.SBData
+
+abstract class ProfileSpecificDataHolder<S>(
+ private val dataSerializer: KSerializer<S>,
+ val configName: String,
+ private val configDefault: () -> S
+) : IDataHolder<S?> {
+
+ var allConfigs: MutableMap<String, S>
+
+ override val data: S?
+ get() = SBData.profileCuteName?.let {
+ allConfigs.computeIfAbsent(it) { configDefault() }
+ }
+
+ init {
+ allConfigs = readValues()
+ readValues()
+ IDataHolder.putDataHolder(this::class, this)
+ }
+
+ private val configDirectory: Path get() = Firmament.CONFIG_DIR.resolve("profiles").resolve(configName)
+
+ 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 Firmament.json.decodeFromString(dataSerializer, it.readText())
+ } catch (e: Exception) { /* Expecting IOException and SerializationException, but Kotlin doesn't allow multi catches*/
+ IDataHolder.badLoads.add(configName)
+ Firmament.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(Firmament.json.encodeToString(dataSerializer, value))
+ }
+ }
+
+ override fun markDirty() {
+ IDataHolder.markDirty(this::class)
+ }
+
+ override fun load() {
+ allConfigs = readValues()
+ }
+
+}