-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
97 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
paperktlib-config/src/main/kotlin/com/filkond/paperktlib/config/ConfigManager.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package com.filkond.paperktlib.config | ||
|
||
import com.charleskorn.kaml.Yaml | ||
import com.filkond.paperktlib.config.ext.loadConfigFromFileOrDefault | ||
import com.filkond.paperktlib.config.ext.update | ||
import kotlinx.serialization.StringFormat | ||
import kotlinx.serialization.json.Json | ||
import java.io.File | ||
import kotlin.reflect.KClass | ||
import kotlin.reflect.full.createInstance | ||
|
||
abstract class ConfigManager( | ||
protected val folder: File, | ||
private val formatter: StringFormat | ||
) { | ||
val configs: MutableMap<File, Config> = mutableMapOf() | ||
|
||
protected inline fun <reified T : Config> config(fileName: String): T { | ||
val file = File(folder, fileName) | ||
return config(file, T::class) | ||
} | ||
|
||
protected fun <T : Config> config(file: File, clazz: KClass<T>): T { | ||
val config = loadConfig(file, clazz) | ||
configs[file] = config | ||
return config | ||
} | ||
|
||
inline fun <reified T : Config> T.reload() { | ||
configs.values.first { T::class == it::class }.update(this) | ||
} | ||
|
||
fun reload(fileName: String) { | ||
val file = File(folder, fileName) | ||
configs[file]!!.update(file) | ||
} | ||
|
||
fun reloadAll() { | ||
configs.forEach { it.value.update(it.key) } | ||
} | ||
|
||
private inline fun <reified T : Config> T.update(file: File) = loadConfig(file, T::class).update(this) | ||
private fun <T : Config> loadConfig(file: File, clazz: KClass<T>): T = | ||
loadConfigFromFileOrDefault(formatter, file, clazz) { | ||
clazz.createInstance() | ||
} | ||
} | ||
class JsonConfigManager(folder: File, json: Json = Json) : ConfigManager(folder, json) | ||
class YamlConfigManager(folder: File, yaml: Yaml = Yaml()) : ConfigManager(folder, yaml) | ||
|
||
interface Config |
39 changes: 39 additions & 0 deletions
39
paperktlib-config/src/main/kotlin/com/filkond/paperktlib/config/ext/Config.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
@file:OptIn(InternalSerializationApi::class) | ||
package com.filkond.paperktlib.config.ext | ||
|
||
import com.filkond.paperktlib.config.Config | ||
import kotlinx.serialization.InternalSerializationApi | ||
import kotlinx.serialization.StringFormat | ||
import kotlinx.serialization.serializer | ||
import org.apache.logging.log4j.LogManager | ||
import java.io.File | ||
import java.time.LocalDate | ||
import kotlin.reflect.KClass | ||
import kotlin.reflect.KMutableProperty | ||
|
||
private val logger = LogManager.getLogger() | ||
|
||
fun <T : Config> T.update(newObject: T) { | ||
val clazz = this::class | ||
clazz.members.filterIsInstance<KMutableProperty<*>>().forEach { | ||
it.setter.call(this, it.getter.call(newObject)) | ||
} | ||
} | ||
|
||
fun <T : Config> loadConfigFromFileOrDefault(formatter: StringFormat, file: File, clazz: KClass<T>, getDefault: () -> T): T { | ||
return try { | ||
formatter.decodeFromString(clazz.serializer(), file.readText()) | ||
} catch (e: Exception) { | ||
logger.warn("Failed to load config ${file.name}, using default: $e") | ||
file.copyTo(File(file.parentFile, "${file.nameWithoutExtension}-backup-${LocalDate.now()}-.${file.extension}")) | ||
writeAndGetDefaultConfig(formatter, file, getDefault, clazz) | ||
} | ||
} | ||
|
||
private fun <T : Config> writeAndGetDefaultConfig(formatter: StringFormat, file: File, getDefault: () -> T, clazz: KClass<T>): T { | ||
file.parentFile.mkdirs() | ||
file.createNewFile() | ||
return getDefault().also { | ||
file.writeText(formatter.encodeToString(clazz.serializer(), it)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters