From afb2dedb213a6ec333f9bb5b10f33c6d2f1fd3b5 Mon Sep 17 00:00:00 2001 From: bluefireoly Date: Thu, 15 Oct 2020 19:23:03 +0200 Subject: [PATCH] Create ConfigManager.kt --- .../net/axay/kspigot/config/ConfigManager.kt | 108 ++++++++++++++++++ 1 file changed, 108 insertions(+) create mode 100644 src/main/kotlin/net/axay/kspigot/config/ConfigManager.kt diff --git a/src/main/kotlin/net/axay/kspigot/config/ConfigManager.kt b/src/main/kotlin/net/axay/kspigot/config/ConfigManager.kt new file mode 100644 index 00000000..727c104d --- /dev/null +++ b/src/main/kotlin/net/axay/kspigot/config/ConfigManager.kt @@ -0,0 +1,108 @@ +@file:Suppress("MemberVisibilityCanBePrivate", "unused") + +package net.axay.kspigot.config + +import net.axay.kspigot.kotlinextensions.createIfNotExists +import net.axay.kspigot.main.ValueHolder.getGson +import java.io.File +import java.io.FileReader +import java.io.FileWriter +import kotlin.reflect.KClass +import kotlin.reflect.KProperty + +/** + * Creates a new ConfigDelegate object. + * + * You can use this as a delegate (with **by**) or + * a normal object. + * (Delegate allows direct access to the config + * object, but does not provide save() or reload() + * methods.) + * + * @param T The class type of the config. + * @param file The path to the config. + * @param default Optional default config, which will be + * used if there is no config file and a new one should + * be created. + * @throws java.io.FileNotFoundException If the file does not + * exist and no default config is specified. + */ +inline fun kSpigotJsonConfig( + file: File, + noinline default: (() -> T)? = null, +) = ConfigDelegate(T::class, file, default) + +/** + * @see kSpigotJsonConfig + */ +class ConfigDelegate ( + private val configClass: KClass, + private val file: File, + private val defaultCallback: (() -> T)? +) { + + private var internalConfig: T = loadIt() + + var data: T + get() = internalConfig + set(value) = saveIt(value) + + operator fun getValue(thisRef: Any?, property: KProperty<*>) = internalConfig + + operator fun setValue(thisRef: Any?, property: KProperty<*>, config: T): Boolean { + saveIt(config) + return true + } + + /** + * Saves the config object in its current state to disk. + */ + fun save() = saveIt(internalConfig) + + /** + * Loads the current state of the config on disk to the config object. + */ + fun reload() { loadIt() } + + private fun saveIt(toSave: T) { + GsonConfigManager.saveConfig(file, toSave, true) + internalConfig = toSave + } + + private fun loadIt() + = if (defaultCallback == null) + GsonConfigManager.loadConfig(file, configClass) + else + GsonConfigManager.loadOrCreateDefault(file, configClass, true, defaultCallback) + +} + +internal object GsonConfigManager { + + fun loadConfig(file: File, configClass: KClass): T + = FileReader(file).use { reader -> return getGson(false).fromJson(reader, configClass.java) } + + fun saveConfig(file: File, config: T, pretty: Boolean = true) { + file.createIfNotExists() + FileWriter(file).use { writer -> + getGson(pretty).toJson(config, writer) + } + } + + fun loadOrCreateDefault( + file: File, + configClass: KClass, + pretty: Boolean = true, + default: () -> T + ): T { + try { + return loadConfig(file, configClass) + } catch (exc: Exception) { + default.invoke().let { + saveConfig(file, it, pretty) + return it + } + } + } + +} \ No newline at end of file