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
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<String, () -> 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<String, Int>(*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<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)
}
},
"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
}.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 <T> setValue(ref: Any?, property: KProperty<*>, value: T) { ModifiableValues[property.name] = { ConfigValueFactory.fromAnyRef(value) } }
inline operator fun <reified T> 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 <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()
with(file) {
if (!exists()) {
@ -86,15 +103,15 @@ object Configuration {
).extract<Config>("Snowy")
}.also {
runBlocking {
writeConfig(it.value)
writeConfig(it)
}
}
val enabledFeatures = mutableMapOf<String, Boolean>().apply {
if (obtained.hasPath("enabledFeatures")) {
putAll(obtained.extract("enabledFeatures"))
}
"enabledFeatures".let { obtained.run {
if (hasPath(it)) { putAll(extract(it)) }
} }
}
val foundMacros: Set<Macro> = run {
internal val foundMacros: Set<Macro> = run {
obtained.getStringList("macros").map {
Json.decodeFromString<Macro>(it)
}.toSet()