diff options
author | Linnea Gräf <nea@nea.moe> | 2024-06-12 18:01:37 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-06-12 18:01:37 +0200 |
commit | 488c0e3de136943604e21bee4f49969bd3121895 (patch) | |
tree | ba4e3d11a78dabc7169e033120ec697f1fc3c72d /src/main/java/at/hannibal2/skyhanni/config/ConfigManager.kt | |
parent | 584f7cd9597aeda0eb44c7d65387d255c592f94c (diff) | |
download | skyhanni-488c0e3de136943604e21bee4f49969bd3121895.tar.gz skyhanni-488c0e3de136943604e21bee4f49969bd3121895.tar.bz2 skyhanni-488c0e3de136943604e21bee4f49969bd3121895.zip |
Use EnumMap and fields for config (#2033)
Diffstat (limited to 'src/main/java/at/hannibal2/skyhanni/config/ConfigManager.kt')
-rw-r--r-- | src/main/java/at/hannibal2/skyhanni/config/ConfigManager.kt | 35 |
1 files changed, 19 insertions, 16 deletions
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<ConfigFileType, Any>() + private val jsonHolder: Map<ConfigFileType, Any> = EnumMap(ConfigFileType::class.java) lateinit var processor: MoulConfigProcessor<Features> private var disableSaving = false + private fun setConfigHolder(type: ConfigFileType, value: Any) { + require(value.javaClass == type.clazz) + @Suppress("UNCHECKED_CAST") + (type.property as KMutableProperty0<Any>).set(value) + (jsonHolder as MutableMap<ConfigFileType, Any>)[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") } |