From 0ae69fcf828699151fa6e9050645515e9d0724f2 Mon Sep 17 00:00:00 2001 From: DaniFoldi Date: Fri, 14 May 2021 14:45:08 +0200 Subject: [PATCH] Added config upgrade system closes #22 --- build.gradle | 2 +- .../bungeegui/main/BungeeGuiLoader.java | 9 ++- .../danifoldi/bungeegui/util/ConfigUtil.java | 72 +++++++++++++++++++ 3 files changed, 80 insertions(+), 3 deletions(-) create mode 100644 src/main/java/com/danifoldi/bungeegui/util/ConfigUtil.java diff --git a/build.gradle b/build.gradle index f3eadc4..f43cad7 100644 --- a/build.gradle +++ b/build.gradle @@ -4,7 +4,7 @@ plugins { } group 'com.danifoldi' -version '1.0.5' +version '1.1.0' sourceCompatibility = JavaVersion.VERSION_11 repositories { diff --git a/src/main/java/com/danifoldi/bungeegui/main/BungeeGuiLoader.java b/src/main/java/com/danifoldi/bungeegui/main/BungeeGuiLoader.java index 8c0a866..9ed0665 100644 --- a/src/main/java/com/danifoldi/bungeegui/main/BungeeGuiLoader.java +++ b/src/main/java/com/danifoldi/bungeegui/main/BungeeGuiLoader.java @@ -1,6 +1,7 @@ package com.danifoldi.bungeegui.main; import com.danifoldi.bungeegui.command.ReloadCommand; +import com.danifoldi.bungeegui.util.ConfigUtil; import com.danifoldi.bungeegui.util.FileUtil; import com.danifoldi.bungeegui.util.Message; import com.danifoldi.bungeegui.util.StringUtil; @@ -46,8 +47,12 @@ void load() { guiHandler.load(config); Message.setMessageProvider(config); - if (config.getIntOrElse("configVersion", 0) != 1) { - StringUtil.blockPrint("BungeeGUI config.yml is built with a different version. Please see the plugin page on how to update.").forEach(logger::warning); + if (config.getIntOrElse("configVersion", 0) < ConfigUtil.LATEST) { + StringUtil.blockPrint("BungeeGUI config.yml is built with an older version. Please see the plugin page for changes. Attempting automatic upgrade (backup saved as {file})".replace("{file}", ConfigUtil.backupAndUpgrade(config))).forEach(logger::warning); + } + + if (config.getIntOrElse("configVersion", 0) > ConfigUtil.LATEST) { + StringUtil.blockPrint("BungeeGUI config.yml is built with a newer version. Compatibility is not guaranteed.").forEach(logger::warning); } guiHandler.registerCommands(); diff --git a/src/main/java/com/danifoldi/bungeegui/util/ConfigUtil.java b/src/main/java/com/danifoldi/bungeegui/util/ConfigUtil.java new file mode 100644 index 0000000..b5fa30a --- /dev/null +++ b/src/main/java/com/danifoldi/bungeegui/util/ConfigUtil.java @@ -0,0 +1,72 @@ +package com.danifoldi.bungeegui.util; + +import com.electronwill.nightconfig.core.Config; +import com.electronwill.nightconfig.core.file.FileConfig; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.Collections; + +public class ConfigUtil { + public static final int LATEST = 2; + + public static String backupAndUpgrade(FileConfig config) throws IOException { + Path folder = config.getFile().toPath().getParent(); + String backup = getNextFileName(folder); + + Files.copy(folder.resolve(config.getFile().getName()), folder.resolve(backup)); + + int currentVersion = config.getIntOrElse("configVersion", 0); + ConfigUtil.upgrade(config, currentVersion, LATEST); + + return backup; + } + + private static void upgrade(Config config, int oldVersion, int newVersion) { + if (oldVersion >= newVersion) { + return; + } + + if (oldVersion <= 0 && newVersion >= 1) { + ensureValue(config, "messages", Collections.emptyList()); + ensureValue(config, "guis", Collections.emptyList()); + + ensureValue(config, "messages.playerOnly", Message.PLAYER_ONLY); + ensureValue(config, "messages.targetRequired", Message.TARGET_REQUIRED); + ensureValue(config, "messages.noSelfTarget", Message.NO_SELF_TARGET); + ensureValue(config, "messages.serverDisabled", Message.SERVER_DISABLED); + ensureValue(config, "messages.targetNotFound", Message.TARGET_NOT_FOUND); + ensureValue(config, "messages.reloadSuccess", Message.RELOAD_SUCCESS); + } + + if (oldVersion <= 1 && newVersion >= 2) { + ensureValue(config, "messages.targetBypass", Message.TARGET_BYPASS); + } + + config.set("configVersion", newVersion); + } + + private static void ensureValue(Config config, String path, Object value) { + if (!config.contains(path)) { + config.add(path, value); + } + } + + private static String getNextFileName(Path folder) { + if (!Files.exists(folder.resolve("config_backup.yml"))) { + return "config_backup.yml"; + } + + int backup = 1; + while (Files.exists(folder.resolve("config_backup" + backup + ".yml"))) { + backup += 1; + } + + return "config_backup" + backup + ".yml"; + } + + private ConfigUtil() { + throw new UnsupportedOperationException(); + } +}