From 488c0e3de136943604e21bee4f49969bd3121895 Mon Sep 17 00:00:00 2001 From: Linnea Gräf Date: Wed, 12 Jun 2024 18:01:37 +0200 Subject: Use EnumMap and fields for config (#2033) --- .../at/hannibal2/skyhanni/config/ConfigManager.kt | 35 ++++++++++++---------- 1 file changed, 19 insertions(+), 16 deletions(-) (limited to 'src/main/java/at/hannibal2/skyhanni/config/ConfigManager.kt') diff --git a/src/main/java/at/hannibal2/skyhanni/config/ConfigManager.kt b/src/main/java/at/hannibal2/skyhanni/config/ConfigManager.kt index 6be199cb7..db70f7e89 100644 --- a/src/main/java/at/hannibal2/skyhanni/config/ConfigManager.kt +++ b/src/main/java/at/hannibal2/skyhanni/config/ConfigManager.kt @@ -36,7 +36,10 @@ import java.io.OutputStreamWriter import java.nio.charset.StandardCharsets import java.nio.file.Files import java.nio.file.StandardCopyOption +import java.util.EnumMap import kotlin.concurrent.fixedRateTimer +import kotlin.reflect.KMutableProperty0 +import kotlin.reflect.KMutableProperty1 private fun GsonBuilder.registerIfBeta(create: TypeAdapterFactory): GsonBuilder { return if (LorenzUtils.isBetaVersion()) { @@ -54,20 +57,20 @@ class ConfigManager { var configDirectory = File("config/skyhanni") } - val features get() = jsonHolder[ConfigFileType.FEATURES] as Features - val sackData get() = jsonHolder[ConfigFileType.SACKS] as SackData - val friendsData get() = jsonHolder[ConfigFileType.FRIENDS] as FriendsJson - val knownFeaturesData get() = jsonHolder[ConfigFileType.KNOWN_FEATURES] as KnownFeaturesJson - val jacobContestData get() = jsonHolder[ConfigFileType.JACOB_CONTESTS] as JacobContestsJson - val visualWordsData get() = jsonHolder[ConfigFileType.VISUAL_WORDS] as VisualWordsJson - private val logger = LorenzLogger("config_manager") - private val jsonHolder = mutableMapOf() + private val jsonHolder: Map = EnumMap(ConfigFileType::class.java) lateinit var processor: MoulConfigProcessor private var disableSaving = false + private fun setConfigHolder(type: ConfigFileType, value: Any) { + require(value.javaClass == type.clazz) + @Suppress("UNCHECKED_CAST") + (type.property as KMutableProperty0).set(value) + (jsonHolder as MutableMap)[type] = value + } + fun firstLoad() { if (jsonHolder.isNotEmpty()) { logger.log("Loading config despite config being already loaded?") @@ -76,7 +79,7 @@ class ConfigManager { for (fileType in ConfigFileType.entries) { - jsonHolder[fileType] = firstLoadFile(fileType.file, fileType, fileType.clazz.newInstance()) + setConfigHolder(fileType, firstLoadFile(fileType.file, fileType, fileType.clazz.newInstance())) } // TODO use SecondPassedEvent @@ -265,13 +268,13 @@ class ConfigManager { } } -enum class ConfigFileType(val fileName: String, val clazz: Class<*>) { - FEATURES("config", Features::class.java), - SACKS("sacks", SackData::class.java), - FRIENDS("friends", FriendsJson::class.java), - KNOWN_FEATURES("known_features", KnownFeaturesJson::class.java), - JACOB_CONTESTS("jacob_contests", JacobContestsJson::class.java), - VISUAL_WORDS("visual_words", VisualWordsJson::class.java), +enum class ConfigFileType(val fileName: String, val clazz: Class<*>, val property: KMutableProperty0<*>) { + FEATURES("config", Features::class.java, SkyHanniMod::feature), + SACKS("sacks", SackData::class.java, SkyHanniMod::sackData), + FRIENDS("friends", FriendsJson::class.java, SkyHanniMod::friendsData), + KNOWN_FEATURES("known_features", KnownFeaturesJson::class.java, SkyHanniMod::knownFeaturesData), + JACOB_CONTESTS("jacob_contests", JacobContestsJson::class.java, SkyHanniMod::jacobContestsData), + VISUAL_WORDS("visual_words", VisualWordsJson::class.java, SkyHanniMod::visualWordsData), ; val file by lazy { File(ConfigManager.configDirectory, "$fileName.json") } -- cgit