From 05613b5bce4352f5e3d9b7255d9492b32bb26735 Mon Sep 17 00:00:00 2001 From: andi-makes Date: Wed, 14 Feb 2024 21:39:06 +0100 Subject: [PATCH] Config migration, better config errors --- .../dev/schmarrn/lighty/config/Config.java | 22 ++++-- .../lighty/config/compat/Lighty2Config.java | 67 +++++++++++++++++++ 2 files changed, 84 insertions(+), 5 deletions(-) create mode 100644 common/src/main/java/dev/schmarrn/lighty/config/compat/Lighty2Config.java diff --git a/common/src/main/java/dev/schmarrn/lighty/config/Config.java b/common/src/main/java/dev/schmarrn/lighty/config/Config.java index eff9a0a..2c593b6 100644 --- a/common/src/main/java/dev/schmarrn/lighty/config/Config.java +++ b/common/src/main/java/dev/schmarrn/lighty/config/Config.java @@ -16,15 +16,15 @@ import dev.schmarrn.lighty.Lighty; import dev.schmarrn.lighty.UtilDefinition; +import dev.schmarrn.lighty.config.compat.Lighty2Config; import net.minecraft.resources.ResourceLocation; import java.io.*; import java.util.HashMap; import java.util.Map; -import java.util.function.BiConsumer; public class Config { - private static final String PATH = UtilDefinition.INSTANCE.getConfigDir().toString() + "/lighty3.config"; + private static final String PATH = UtilDefinition.INSTANCE.getConfigDir().toString() + "/lighty/base.config"; private static final Map fileState = new HashMap<>(); private static final Map configValues = new HashMap<>(); @@ -70,7 +70,7 @@ public static void reloadFromDisk() { } catch (FileNotFoundException e) { Lighty.LOGGER.warn("No Lighty config found at {}, using defaults.", PATH); } catch (IOException e) { - Lighty.LOGGER.error("Could not close Lighty config at {}. This should not happen, please report on GitHub. Abort.", PATH); + Lighty.LOGGER.error("Could not close Lighty config at {}. This should not happen, please report on GitHub. Abort. {}", PATH, e); throw new RuntimeException(e); } @@ -87,18 +87,30 @@ public static void save() { } File file = new File(PATH); + // Create the lighty config folder if it doesn't already exist + file.getParentFile().mkdirs(); + try { BufferedWriter bw = new BufferedWriter(new FileWriter(file)); bw.write(content.toString()); bw.flush(); bw.close(); } catch (IOException e) { - Lighty.LOGGER.warn("Could not write Lighty config file at {}. Config changes are not saved.", PATH); + Lighty.LOGGER.warn("Could not write Lighty config file at {}. Config changes are not saved. {}", PATH, e); } } public static void init() { - reloadFromDisk(); + // Old config + if (Lighty2Config.exists()) { + Lighty2Config.migrate(); + // save the old config values immediately. the old config file is no longer available and + // we need to persist them! + save(); + } else { + // Try to read the normal config from disk. + reloadFromDisk(); + } } } diff --git a/common/src/main/java/dev/schmarrn/lighty/config/compat/Lighty2Config.java b/common/src/main/java/dev/schmarrn/lighty/config/compat/Lighty2Config.java new file mode 100644 index 0000000..9b06022 --- /dev/null +++ b/common/src/main/java/dev/schmarrn/lighty/config/compat/Lighty2Config.java @@ -0,0 +1,67 @@ +package dev.schmarrn.lighty.config.compat; + +import dev.schmarrn.lighty.Lighty; +import dev.schmarrn.lighty.UtilDefinition; +import dev.schmarrn.lighty.config.Config; +import dev.schmarrn.lighty.config.IntegerConfig; +import net.minecraft.resources.ResourceLocation; + +import java.io.*; +import java.util.Properties; +import java.util.ResourceBundle; + +public class Lighty2Config { + private static final String PATH = UtilDefinition.INSTANCE.getConfigDir().toString() + "/lighty.config"; + + private static final String LAST_USED_MODE = "lighty.last_used_mode"; + private static final String SKY_THRESHOLD = "lighty.sky_threshold"; + private static final String BLOCK_THRESHOLD = "lighty.block_threshold"; + private static final String OVERLAY_DISTANCE = "lighty.overlay_distance"; + private static final String OVERLAY_BRIGHTNESS = "lighty.overlay_brightness"; + private static final String SHOW_SAFE = "lighty.show_safe"; + + private static final String OVERLAY_GREEN = "lighty.overlay_green"; + private static final String OVERLAY_ORANGE = "lighty.overlay_orange"; + private static final String OVERLAY_RED = "lighty.overlay_red"; + + public static boolean exists() { + return new File(PATH).exists(); + } + + /// Migrates the old Lighty Config to the new System. + public static void migrate() { + Properties properties = new Properties(); + + try (Reader reader = new FileReader(PATH)) { + properties.load(reader); + + // Set old Defaults if no values are set + properties.putIfAbsent(LAST_USED_MODE, "lighty:carpet_mode"); + properties.putIfAbsent(SKY_THRESHOLD, "0"); + properties.putIfAbsent(BLOCK_THRESHOLD, "0"); + properties.putIfAbsent(OVERLAY_DISTANCE, "2"); + properties.putIfAbsent(OVERLAY_BRIGHTNESS, "10"); + properties.putIfAbsent(SHOW_SAFE, String.valueOf(true)); + properties.putIfAbsent(OVERLAY_GREEN, Integer.toHexString(0x00FF00)); + properties.putIfAbsent(OVERLAY_ORANGE, Integer.toHexString(0xFF6600)); + properties.putIfAbsent(OVERLAY_RED, Integer.toHexString(0xFF0000)); + + // Set the new stuff + Config.LAST_USED_MODE.setValue(new ResourceLocation(properties.getProperty(LAST_USED_MODE))); + Config.SKY_THRESHOLD.setValue(Integer.valueOf(properties.getProperty(SKY_THRESHOLD))); + Config.BLOCK_THRESHOLD.setValue(Integer.valueOf(properties.getProperty(BLOCK_THRESHOLD))); + Config.OVERLAY_DISTANCE.setValue(Integer.valueOf(properties.getProperty(OVERLAY_DISTANCE))); + Config.OVERLAY_BRIGHTNESS.setValue(Integer.valueOf(properties.getProperty(OVERLAY_BRIGHTNESS))); + Config.SHOW_SAFE.setValue(Boolean.valueOf(properties.getProperty(SHOW_SAFE))); + Config.OVERLAY_GREEN.setValue(Integer.valueOf(properties.getProperty(OVERLAY_GREEN), 16)); + Config.OVERLAY_ORANGE.setValue(Integer.valueOf(properties.getProperty(OVERLAY_ORANGE), 16)); + Config.OVERLAY_RED.setValue(Integer.valueOf(properties.getProperty(OVERLAY_RED), 16)); + } catch (FileNotFoundException e) { + Lighty.LOGGER.warn("No Lighty config found at {}, loading defaults and saving config file.", PATH); + } catch (IOException e) { + Lighty.LOGGER.error("Error while reading from Lighty config at {}: {}", PATH, e); + } + + new File(PATH).delete(); + } +}