aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/moe/nea/notenoughupdates/util/config
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
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')
-rw-r--r--src/main/kotlin/moe/nea/notenoughupdates/util/config/ConfigHolder.kt60
-rw-r--r--src/main/kotlin/moe/nea/notenoughupdates/util/config/IConfigHolder.kt75
-rw-r--r--src/main/kotlin/moe/nea/notenoughupdates/util/config/ManagedConfig.kt206
-rw-r--r--src/main/kotlin/moe/nea/notenoughupdates/util/config/ProfileSpecificConfigHolder.kt82
4 files changed, 206 insertions, 217 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)
- }
-
-}
diff --git a/src/main/kotlin/moe/nea/notenoughupdates/util/config/IConfigHolder.kt b/src/main/kotlin/moe/nea/notenoughupdates/util/config/IConfigHolder.kt
deleted file mode 100644
index 2acc99d..0000000
--- a/src/main/kotlin/moe/nea/notenoughupdates/util/config/IConfigHolder.kt
+++ /dev/null
@@ -1,75 +0,0 @@
-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/ManagedConfig.kt b/src/main/kotlin/moe/nea/notenoughupdates/util/config/ManagedConfig.kt
new file mode 100644
index 0000000..a149bf2
--- /dev/null
+++ b/src/main/kotlin/moe/nea/notenoughupdates/util/config/ManagedConfig.kt
@@ -0,0 +1,206 @@
+package moe.nea.notenoughupdates.util.config
+
+import io.github.cottonmc.cotton.gui.client.CottonClientScreen
+import io.github.cottonmc.cotton.gui.client.LibGui
+import io.github.cottonmc.cotton.gui.client.LightweightGuiDescription
+import io.github.cottonmc.cotton.gui.widget.WButton
+import io.github.cottonmc.cotton.gui.widget.WLabel
+import io.github.cottonmc.cotton.gui.widget.WToggleButton
+import io.github.cottonmc.cotton.gui.widget.WWidget
+import io.github.cottonmc.cotton.gui.widget.data.Insets
+import kotlinx.serialization.decodeFromString
+import kotlinx.serialization.encodeToString
+import kotlinx.serialization.json.JsonElement
+import kotlinx.serialization.json.JsonObject
+import kotlinx.serialization.json.JsonPrimitive
+import kotlinx.serialization.json.boolean
+import kotlinx.serialization.json.jsonPrimitive
+import kotlin.io.path.createDirectories
+import kotlin.io.path.readText
+import kotlin.io.path.writeText
+import kotlin.properties.ReadOnlyProperty
+import kotlin.reflect.KProperty
+import net.minecraft.client.MinecraftClient
+import net.minecraft.text.Text
+import moe.nea.notenoughupdates.NotEnoughUpdates
+import moe.nea.notenoughupdates.gui.WGridPanelWithPadding
+import moe.nea.notenoughupdates.util.ScreenUtil.setScreenLater
+
+abstract class ManagedConfig(val name: String) {
+
+ class GuiAppender(val width: Int) {
+ private var row = 0
+ internal val panel = WGridPanelWithPadding(verticalPadding = 4, horizontalPadding = 4)
+ internal val reloadables = mutableListOf<(() -> Unit)>()
+ fun set(x: Int, y: Int, w: Int, h: Int, widget: WWidget) {
+ panel.add(widget, x, y, w, h)
+ }
+
+
+ fun onReload(reloadable: () -> Unit) {
+ reloadables.add(reloadable)
+ }
+
+ fun skipRows(r: Int) {
+ row += r
+ }
+
+ fun appendSplitRow(left: WWidget, right: WWidget) {
+ val lw = width / 2
+ set(0, row, lw, 1, left)
+ set(lw, row, width - lw, 1, right)
+ skipRows(1)
+ }
+
+ fun appendFullRow(widget: WWidget) {
+ set(0, row, width, 1, widget)
+ skipRows(1)
+ }
+ }
+
+ interface OptionHandler<T : Any> {
+ fun toJson(element: T): JsonElement?
+ fun fromJson(element: JsonElement): T
+ fun emitGuiElements(opt: Option<T>, guiAppender: GuiAppender)
+ }
+
+ inner class Option<T : Any> internal constructor(
+ val propertyName: String,
+ val default: () -> T,
+ val handler: OptionHandler<T>
+ ) : ReadOnlyProperty<Any?, T> {
+
+ private lateinit var _value: T
+ private var loaded = false
+ var value: T
+ get() {
+ if (!loaded)
+ load()
+ return _value
+ }
+ set(value) {
+ loaded = true
+ _value = value
+ }
+
+ override fun getValue(thisRef: Any?, property: KProperty<*>): T {
+ return value
+ }
+
+ private fun load() {
+ if (data.containsKey(propertyName)) {
+ try {
+ value = handler.fromJson(data[propertyName]!!)
+ } catch (e: Exception) {
+ NotEnoughUpdates.logger.error(
+ "Exception during loading of config file $name. This will reset this config.",
+ e
+ )
+ }
+ }
+ value = default()
+ }
+
+ internal fun toJson(): JsonElement? {
+ return handler.toJson(value)
+ }
+
+ fun appendToGui(guiapp: GuiAppender) {
+ handler.emitGuiElements(this, guiapp)
+ }
+ }
+
+ val file = NotEnoughUpdates.CONFIG_DIR.resolve("$name.json")
+ val data: JsonObject by lazy {
+ try {
+ NotEnoughUpdates.json.decodeFromString(
+ file.readText()
+ )
+ } catch (e: Exception) {
+ NotEnoughUpdates.logger.info("Could not read config $name. Loading empty config.")
+ JsonObject(mutableMapOf())
+ }
+ }
+
+ fun save() {
+ val data = JsonObject(allOptions.mapNotNull { (key, value) ->
+ value.toJson()?.let {
+ key to it
+ }
+ }.toMap())
+ file.parent.createDirectories()
+ file.writeText(NotEnoughUpdates.json.encodeToString(data))
+ }
+
+
+ val allOptions = mutableMapOf<String, Option<*>>()
+ val sortedOptions = mutableListOf<Option<*>>()
+
+ protected fun <T : Any> option(propertyName: String, default: () -> T, handler: OptionHandler<T>): Option<T> {
+ if (propertyName in allOptions) error("Cannot register the same name twice")
+ return Option(propertyName, default, handler).also {
+ allOptions[propertyName] = it
+ sortedOptions.add(it)
+ }
+ }
+
+ class BooleanHandler(val config: ManagedConfig) : OptionHandler<Boolean> {
+ override fun toJson(element: Boolean): JsonElement? {
+ return JsonPrimitive(element)
+ }
+
+ override fun fromJson(element: JsonElement): Boolean {
+ return element.jsonPrimitive.boolean
+ }
+
+ override fun emitGuiElements(opt: Option<Boolean>, guiAppender: GuiAppender) {
+ guiAppender.appendFullRow(
+ WToggleButton(Text.translatable("neu.config.${config.name}.${opt.propertyName}")).apply {
+ guiAppender.onReload { toggle = opt.value }
+ setOnToggle {
+ opt.value = it
+ config.save()
+ }
+ }
+ )
+ }
+ }
+
+ class ClickHandler(val config: ManagedConfig, val runnable: () -> Unit) : OptionHandler<Unit> {
+ override fun toJson(element: Unit): JsonElement? {
+ return null
+ }
+
+ override fun fromJson(element: JsonElement) {}
+
+ override fun emitGuiElements(opt: Option<Unit>, guiAppender: GuiAppender) {
+ guiAppender.appendSplitRow(
+ WLabel(Text.translatable("neu.config.${config.name}.${opt.propertyName}")),
+ WButton(Text.translatable("neu.config.${config.name}.${opt.propertyName}")).apply {
+ setOnClick {
+ runnable()
+ }
+ },
+ )
+ }
+ }
+
+ protected fun toggle(propertyName: String, default: () -> Boolean): Option<Boolean> {
+ return option(propertyName, default, BooleanHandler(this))
+ }
+
+ fun showConfigEditor() {
+ val lwgd = LightweightGuiDescription()
+ val guiapp = GuiAppender(20)
+ guiapp.panel.insets = Insets.ROOT_PANEL
+ sortedOptions.forEach { it.appendToGui(guiapp) }
+ guiapp.reloadables.forEach { it() }
+ lwgd.setRootPanel(guiapp.panel)
+ setScreenLater(CottonClientScreen(lwgd))
+ }
+
+ protected fun button(propertyName: String, runnable: () -> Unit): Option<Unit> {
+ return option(propertyName, { }, ClickHandler(this, runnable))
+ }
+
+}
diff --git a/src/main/kotlin/moe/nea/notenoughupdates/util/config/ProfileSpecificConfigHolder.kt b/src/main/kotlin/moe/nea/notenoughupdates/util/config/ProfileSpecificConfigHolder.kt
deleted file mode 100644
index 6a39e0a..0000000
--- a/src/main/kotlin/moe/nea/notenoughupdates/util/config/ProfileSpecificConfigHolder.kt
+++ /dev/null
@@ -1,82 +0,0 @@
-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()
- IConfigHolder.putConfig(this::class, this)
- }
-
- 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()
- }
-
-}