Skip to content

Commit

Permalink
Config migration, better config errors
Browse files Browse the repository at this point in the history
  • Loading branch information
andi-makes committed Feb 14, 2024
1 parent e92de7d commit 05613b5
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 5 deletions.
22 changes: 17 additions & 5 deletions common/src/main/java/dev/schmarrn/lighty/config/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, String> fileState = new HashMap<>();
private static final Map<String, ConfigSerDe> configValues = new HashMap<>();

Expand Down Expand Up @@ -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);
}

Expand All @@ -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();
}
}
}
Original file line number Diff line number Diff line change
@@ -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();
}
}

0 comments on commit 05613b5

Please sign in to comment.