Improve the ModifiableValues property of Configuration.kt and make Configuration.obtained a non-lazy property again

This commit is contained in:
Username404 2021-06-05 18:00:56 +02:00
parent ece6c40ab5
commit 7023d1a8f6
Signed by: Username404-59
GPG Key ID: 7AB361FBB257A5D1
1 changed files with 39 additions and 22 deletions

View File

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