Skip to content

Commit

Permalink
feat: add ex plugin id support for config management
Browse files Browse the repository at this point in the history
  • Loading branch information
duruer committed Feb 22, 2024
1 parent ab1436c commit 2e79ace
Showing 1 changed file with 41 additions and 2 deletions.
43 changes: 41 additions & 2 deletions src/main/kotlin/co/statu/parsek/api/config/PluginConfigManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ class PluginConfigManager<T : PluginConfig>(
plugin: ParsekPlugin,
private val pluginConfigClass: Class<T>,
private val logger: Logger,
private val migrations: List<PluginConfigMigration> = listOf()
private val migrations: List<PluginConfigMigration> = listOf(),
private val exPluginIds: List<String> = listOf()
) {
companion object {
private val gson = Gson()
Expand Down Expand Up @@ -50,10 +51,48 @@ class PluginConfigManager<T : PluginConfig>(
.getJsonObject("plugins")
.getJsonObject(pluginId) == null
) {
logger.warn("Couldn't find config for \"${pluginId}\". Saving default config")
val pluginConfigs = configManager.getConfig().getJsonObject("plugins")

val config = JsonObject(gson.toJson(config))

if (exPluginIds.isNotEmpty()) {
val foundExId = exPluginIds.reversed().firstOrNull {
pluginConfigs.getJsonObject(it) != null
}

if (foundExId != null) {
val currentConfigVersion = if (migrations.isNotEmpty()) {
migrations.maxBy { it.VERSION }.VERSION
} else {
1
}

val exConfig = pluginConfigs.getJsonObject(foundExId)

var shouldMigrate = false

if (exConfig.getInteger("version") == currentConfigVersion) {
val keysAreSame = config.map.keys.none { !exConfig.map.containsKey(it) }

if (keysAreSame) {
shouldMigrate = true
}
} else {
shouldMigrate = true
}

if (shouldMigrate) {
pluginConfigs.remove(foundExId)

saveConfig(exConfig)
}

return
}
}

logger.warn("Couldn't find config for \"${pluginId}\". Saving default config")

if (migrations.isNotEmpty()) {
val highestVersion = migrations.maxBy { it.VERSION }.VERSION

Expand Down

0 comments on commit 2e79ace

Please sign in to comment.