Add automatic converter for animations with old format

This commit is contained in:
filoghost 2021-08-03 00:18:30 +02:00
parent 47c98100ff
commit d94cb681a8
4 changed files with 130 additions and 1 deletions

View File

@ -20,6 +20,7 @@ import me.filoghost.holographicdisplays.plugin.commands.HologramCommandManager;
import me.filoghost.holographicdisplays.plugin.config.ConfigManager;
import me.filoghost.holographicdisplays.plugin.config.HologramDatabase;
import me.filoghost.holographicdisplays.plugin.config.Settings;
import me.filoghost.holographicdisplays.plugin.config.upgrade.LegacyAnimationsUpgrade;
import me.filoghost.holographicdisplays.plugin.config.upgrade.LegacySymbolsUpgrade;
import me.filoghost.holographicdisplays.plugin.hologram.api.APIHologramManager;
import me.filoghost.holographicdisplays.plugin.hologram.internal.InternalHologramManager;
@ -42,6 +43,7 @@ import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.entity.Player;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
public class HolographicDisplays extends FCommonsPlugin {
@ -108,6 +110,11 @@ public class HolographicDisplays extends FCommonsPlugin {
} catch (ConfigException e) {
errorCollector.add(e, "couldn't automatically convert symbols file to the new format");
}
try {
LegacyAnimationsUpgrade.run(configManager, errorCollector);
} catch (IOException e) {
errorCollector.add(e, "couldn't automatically convert animation files to the new format");
}
// Load the configuration
load(true, errorCollector);

View File

@ -0,0 +1,80 @@
/*
* Copyright (C) filoghost and contributors
*
* SPDX-License-Identifier: GPL-3.0-or-later
*/
package me.filoghost.holographicdisplays.plugin.config.upgrade;
import me.filoghost.fcommons.config.Config;
import me.filoghost.fcommons.logging.ErrorCollector;
import me.filoghost.holographicdisplays.plugin.config.ConfigManager;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.stream.Stream;
public class LegacyAnimationsUpgrade {
private static final String SPEED_PREFIX = "speed:";
public static void run(ConfigManager configManager, ErrorCollector errorCollector) throws IOException {
Path animationFolder = configManager.getAnimationsFolder();
if (!Files.isDirectory(animationFolder)) {
return;
}
try (Stream<Path> animationFiles = Files.list(animationFolder)) {
animationFiles.filter(Files::isRegularFile).forEach(file -> convertFile(file, configManager, errorCollector));
}
}
private static void convertFile(Path oldFile, ConfigManager configManager, ErrorCollector errorCollector) {
if (LegacyUpgradeUtils.isBackupFile(oldFile)) {
return; // Ignore backup files
}
try {
List<String> lines = Files.readAllLines(oldFile);
if (lines.size() == 0) {
return;
}
// Remove the first line that only contains the speed
String firstLine = lines.remove(0).trim();
if (!firstLine.toLowerCase().startsWith(SPEED_PREFIX)) {
return; // Not a valid animation
}
String newFileName = oldFile.getFileName().toString();
if (LegacyUpgradeUtils.hasFileExtension(newFileName, "txt")) {
newFileName = LegacyUpgradeUtils.removeFileExtension(newFileName);
}
newFileName += ".yml";
Path newFile = oldFile.resolveSibling(newFileName);
if (Files.isRegularFile(newFile)) {
return; // Already created, do not override
}
double speed;
try {
speed = Double.parseDouble(firstLine.substring(SPEED_PREFIX.length()).trim());
} catch (NumberFormatException e) {
speed = 0.5;
}
Config config = new Config();
config.setDouble("interval-seconds", speed);
config.setStringList("animation-frames", lines);
configManager.getConfigLoader(newFile).save(config);
Files.move(oldFile, LegacyUpgradeUtils.getBackupFile(oldFile));
} catch (Exception e) {
errorCollector.add(e, "couldn't automatically convert animation file \"" + oldFile.getFileName() + "\" to the new format");
}
}
}

View File

@ -67,7 +67,7 @@ public class LegacySymbolsUpgrade {
}
try {
Files.move(oldFile, oldFile.resolveSibling("symbols.yml.backup"));
Files.move(oldFile, LegacyUpgradeUtils.getBackupFile(oldFile));
} catch (IOException e) {
errorCollector.add(e, "couldn't rename " + oldFile.getFileName());
}

View File

@ -0,0 +1,42 @@
/*
* Copyright (C) filoghost and contributors
*
* SPDX-License-Identifier: GPL-3.0-or-later
*/
package me.filoghost.holographicdisplays.plugin.config.upgrade;
import java.nio.file.Path;
class LegacyUpgradeUtils {
private static final String BACKUP_FILE_EXTENSION = ".backup";
static Path getBackupFile(Path file) {
return file.resolveSibling(file.getFileName() + BACKUP_FILE_EXTENSION);
}
static boolean isBackupFile(Path file) {
return file.getFileName().toString().toLowerCase().endsWith(BACKUP_FILE_EXTENSION);
}
static boolean hasFileExtension(String fileName, String extension) {
int extensionBeginIndex = fileName.lastIndexOf('.');
if (extensionBeginIndex < 0) {
return false;
}
return fileName.substring(extensionBeginIndex + 1).equalsIgnoreCase(extension);
}
static String removeFileExtension(String fileName) {
int extensionBeginIndex = fileName.lastIndexOf('.');
if (extensionBeginIndex < 0) {
return fileName;
}
return fileName.substring(0, extensionBeginIndex);
}
}