diff --git a/common/src/main/kotlin/fr/username404/snowygui/config/Configuration.kt b/common/src/main/kotlin/fr/username404/snowygui/config/Configuration.kt index 25ae41f..f046253 100644 --- a/common/src/main/kotlin/fr/username404/snowygui/config/Configuration.kt +++ b/common/src/main/kotlin/fr/username404/snowygui/config/Configuration.kt @@ -1,7 +1,10 @@ package fr.username404.snowygui.config -import com.typesafe.config.* +import com.typesafe.config.Config +import com.typesafe.config.ConfigException import com.typesafe.config.ConfigFactory.* +import com.typesafe.config.ConfigRenderOptions +import com.typesafe.config.ConfigValueFactory import fr.username404.snowygui.ClickGui import fr.username404.snowygui.Snowy import fr.username404.snowygui.gui.elements.ClickBox @@ -20,19 +23,31 @@ import java.io.File import kotlin.reflect.KProperty object Configuration { - private val ModifiableValues = mutableMapOf ConfigValue>( - "enabledFeatures" to { ConfigValueFactory.fromMap(enabledFeatures) }, - "macros" to { ConfigValueFactory.fromIterable((ClickGui.components.find { it is ClickBox && it.isCategory(Category.MACROS) } as ClickBox).buttons.map { - Json.encodeToString(it as Macro) - }) }, - "box_colors" to { ConfigValueFactory.fromAnyRef(mapOf(*ClickGui.clickBoxes.filter { box -> - box.name != null && Category.values().find { category -> box.name.key == category.translationKey }?.categoryColor != box.color - }.map { - it.name!!.key to it.color - }.toTypedArray())) }, - ) - private fun Config.withFullModifiableValues() = ModifiableValues.entries.fold(this) { previous, entry -> - previous.withValue(entry.key, entry.value()) + @Deprecated("Use the getValue or setValue methods instead", level = DeprecationLevel.ERROR) + @JvmStatic + val ModifiableValues: MutableMap by lazy { + mutableMapOf( + "enabledFeatures" to enabledFeatures, + "macros" to lazy { + (ClickGui.components.find { it is ClickBox && it.isCategory(Category.MACROS) } as ClickBox).buttons.map { + Json.encodeToString(it as Macro) + } + }, + "box_colors" to mapOf(*ClickGui.clickBoxes.filter { box -> + box.name != null && Category.values().find { category -> box.name.key == category.translationKey }?.categoryColor != box.color + }.map { + it.name!!.key to it.color + }.toTypedArray()), + ) + } + private fun Config.withFullModifiableValues() = @Suppress("DEPRECATION_ERROR") + ModifiableValues.entries.fold(this) { previous, entry -> + previous.withValue(entry.key, entry.value.let { + ConfigValueFactory.fromAnyRef( + if (it !is Lazy<*>) ConfigValueFactory.fromAnyRef(it) else it.value + ) + } + ) } private val configDirectory: String = (Minecraft.getInstance().gameDirectory.absolutePath + File.separator + "config").also { File(it).mkdir() } private val file: File = File(configDirectory + File.separator + "snowy.conf") @@ -56,10 +71,12 @@ object Configuration { } operator fun invoke() = obtained - operator fun getValue(ref: Any?, property: KProperty<*>): Boolean = invoke().getValue(ref, property) - operator fun setValue(ref: Any?, property: KProperty<*>, value: T) { ModifiableValues[property.name] = { ConfigValueFactory.fromAnyRef(value) } } + inline operator fun getValue(ref: Any?, property: KProperty<*>): T = @Suppress("DEPRECATION_ERROR") + if (ModifiableValues.containsKey(property.name)) ModifiableValues[property.name] as T else invoke().getValue(ref, property) + operator fun setValue(ref: Any?, property: KProperty<*>, value: T) = @Suppress("DEPRECATION_ERROR") + ModifiableValues.setValue(ref, property, value) - private val obtained: Config by lazy { + private val obtained: Config = run { var result: Config = empty() with(file) { if (!exists()) { @@ -86,15 +103,15 @@ object Configuration { ).extract("Snowy") }.also { runBlocking { - writeConfig(it.value) + writeConfig(it) } } val enabledFeatures = mutableMapOf().apply { - if (obtained.hasPath("enabledFeatures")) { - putAll(obtained.extract("enabledFeatures")) - } + "enabledFeatures".let { obtained.run { + if (hasPath(it)) { putAll(extract(it)) } + } } } - val foundMacros: Set = run { + internal val foundMacros: Set = run { obtained.getStringList("macros").map { Json.decodeFromString(it) }.toSet()