Improve the ModifiableValues property of Configuration.kt and make Configuration.obtained a non-lazy property again
This commit is contained in:
parent
ece6c40ab5
commit
7023d1a8f6
|
@ -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 {
|
||||||
Json.encodeToString(it as Macro)
|
mutableMapOf(
|
||||||
}) },
|
"enabledFeatures" to enabledFeatures,
|
||||||
"box_colors" to { ConfigValueFactory.fromAnyRef(mapOf<String, Int>(*ClickGui.clickBoxes.filter { box ->
|
"macros" to lazy {
|
||||||
box.name != null && Category.values().find { category -> box.name.key == category.translationKey }?.categoryColor != box.color
|
(ClickGui.components.find { it is ClickBox && it.isCategory(Category.MACROS) } as ClickBox).buttons.map {
|
||||||
}.map {
|
Json.encodeToString(it as Macro)
|
||||||
it.name!!.key to it.color
|
}
|
||||||
}.toTypedArray())) },
|
},
|
||||||
)
|
"box_colors" to mapOf<String, Int>(*ClickGui.clickBoxes.filter { box ->
|
||||||
private fun Config.withFullModifiableValues() = ModifiableValues.entries.fold(this) { previous, entry ->
|
box.name != null && Category.values().find { category -> box.name.key == category.translationKey }?.categoryColor != box.color
|
||||||
previous.withValue(entry.key, entry.value())
|
}.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 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)) }
|
||||||
}
|
} }
|
||||||
}
|
}
|
||||||
val foundMacros: Set<Macro> = run {
|
internal val foundMacros: Set<Macro> = run {
|
||||||
obtained.getStringList("macros").map {
|
obtained.getStringList("macros").map {
|
||||||
Json.decodeFromString<Macro>(it)
|
Json.decodeFromString<Macro>(it)
|
||||||
}.toSet()
|
}.toSet()
|
||||||
|
|
Loading…
Reference in New Issue