Reformat Configuration.kt

This commit is contained in:
Username404 2021-06-19 14:53:34 +02:00
parent 4ce4e79287
commit 9a2a78a3a3
Signed by: Username404-59
GPG Key ID: 7AB361FBB257A5D1
1 changed files with 45 additions and 39 deletions

View File

@ -3,8 +3,8 @@ package fr.username404.snowygui.config
import com.typesafe.config.Config
import com.typesafe.config.ConfigException
import com.typesafe.config.ConfigFactory.empty
import com.typesafe.config.ConfigFactory.parseFile
import com.typesafe.config.ConfigFactory.load
import com.typesafe.config.ConfigFactory.parseFile
import com.typesafe.config.ConfigFactory.parseString
import com.typesafe.config.ConfigRenderOptions
import com.typesafe.config.ConfigValueFactory
@ -16,6 +16,11 @@ import fr.username404.snowygui.gui.feature.Category
import fr.username404.snowygui.gui.feature.Macro
import io.github.config4k.extract
import io.github.config4k.getValue
import net.minecraft.client.Minecraft
import java.io.File
import kotlin.reflect.KProperty
import kotlinx.coroutines.CoroutineStart
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.launch
@ -23,9 +28,6 @@ import kotlinx.coroutines.runBlocking
import kotlinx.serialization.decodeFromString
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json
import net.minecraft.client.Minecraft
import java.io.File
import kotlin.reflect.KProperty
object Configuration {
@Deprecated("Use the getValue or setValue methods instead", level = DeprecationLevel.ERROR)
@ -45,42 +47,8 @@ object Configuration {
}.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")
private suspend fun writeConfig(c: Config) = coroutineScope {
launch(start = CoroutineStart.UNDISPATCHED) {
file.writeText(
"""
|Snowy {
|${
c.root().render(
ConfigRenderOptions.defaults()
.setFormatted(true)
.setJson(false)
.setOriginComments(false)
).prependIndent(" ").trimEnd()
}
|}
""".trimMargin()
)
}
}
operator fun invoke() = obtained
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 = run {
var result: Config = empty()
with(file) {
@ -113,7 +81,9 @@ object Configuration {
}
val enabledFeatures = mutableMapOf<String, Boolean>().apply {
"enabledFeatures".let { obtained.run {
if (hasPath(it)) { putAll(extract(it)) }
if (hasPath(it)) {
putAll(extract(it))
}
} }
}
internal val foundMacros: Set<Macro> = run {
@ -135,4 +105,40 @@ object Configuration {
}
)
}
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 suspend fun writeConfig(c: Config) = coroutineScope {
launch(start = CoroutineStart.UNDISPATCHED) {
file.writeText(
"""
|Snowy {
|${
c.root().render(
ConfigRenderOptions.defaults()
.setFormatted(true)
.setJson(false)
.setOriginComments(false)
).prependIndent(" ").trimEnd()
}
|}
""".trimMargin()
)
}
}
operator fun invoke() = obtained
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)
}