Skip to content
This repository has been archived by the owner on Sep 19, 2023. It is now read-only.

Commit

Permalink
Added config upgrade system
Browse files Browse the repository at this point in the history
closes #22
  • Loading branch information
DaniFoldi committed May 14, 2021
1 parent 42d21b0 commit 0ae69fc
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 3 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ plugins {
}

group 'com.danifoldi'
version '1.0.5'
version '1.1.0'
sourceCompatibility = JavaVersion.VERSION_11

repositories {
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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();
Expand Down
72 changes: 72 additions & 0 deletions src/main/java/com/danifoldi/bungeegui/util/ConfigUtil.java
Original file line number Diff line number Diff line change
@@ -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();
}
}

0 comments on commit 0ae69fc

Please sign in to comment.