From 9a2a78a3a3e369c58cdaf08f1162fb94e3d2608e Mon Sep 17 00:00:00 2001 From: Username404 Date: Sat, 19 Jun 2021 14:53:34 +0200 Subject: [PATCH] Reformat Configuration.kt --- .../snowygui/config/Configuration.kt | 84 ++++++++++--------- 1 file changed, 45 insertions(+), 39 deletions(-) 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 60345e9..af5621f 100644 --- a/common/src/main/kotlin/fr/username404/snowygui/config/Configuration.kt +++ b/common/src/main/kotlin/fr/username404/snowygui/config/Configuration.kt @@ -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 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 = run { var result: Config = empty() with(file) { @@ -113,7 +81,9 @@ object Configuration { } val enabledFeatures = mutableMapOf().apply { "enabledFeatures".let { obtained.run { - if (hasPath(it)) { putAll(extract(it)) } + if (hasPath(it)) { + putAll(extract(it)) + } } } } internal val foundMacros: Set = 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 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) }