Skip to content

Commit

Permalink
feat(config): improve error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
UltimatChamp committed Dec 31, 2024
1 parent 3f4053d commit ee6335e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -140,16 +140,21 @@ public static BetterGrassifyConfig load() {
BetterGrassifyConfig config;

try {
var trimmedFileContent = Files.readString(CONFIG_PATH).trim();

if (!Files.exists(CONFIG_PATH) || !trimmedFileContent.startsWith("{") || !trimmedFileContent.endsWith("}")) {
BetterGrassify.LOGGER.info("[BetterGrassify] Config file is empty or invalid. Creating a new one...");
if (!Files.exists(CONFIG_PATH)) {
BetterGrassify.LOGGER.info("[BetterGrassify] Config file not found. Creating a new one...");
config = new BetterGrassifyConfig();
save(config);
} else {
var configContent = Files.readString(CONFIG_PATH);
var configJson = ensureDefaults(JANKSON.load(configContent));
config = JANKSON.fromJson(configJson, BetterGrassifyConfig.class);
var configContent = Files.readString(CONFIG_PATH).trim();

if (!configContent.startsWith("{") || !configContent.endsWith("}")) {
BetterGrassify.LOGGER.error("[BetterGrassify] Config file is empty or invalid. Creating a new one...");
config = new BetterGrassifyConfig();
save(config);
} else {
var configJson = ensureDefaults(JANKSON.load(configContent));
config = JANKSON.fromJson(configJson, BetterGrassifyConfig.class);
}
}
} catch (IOException | SyntaxError e) {
BetterGrassify.LOGGER.error("[BetterGrassify]", e);
Expand All @@ -173,7 +178,7 @@ public static void save(BetterGrassifyConfig config) {
}

private static JsonObject ensureDefaults(JsonObject configJson) {
var modified = new AtomicBoolean(false);
var modified = false;
var defaultConfig = new BetterGrassifyConfig();

for (var field : BetterGrassifyConfig.class.getDeclaredFields()) {
Expand All @@ -188,14 +193,14 @@ private static JsonObject ensureDefaults(JsonObject configJson) {
if (!configJson.containsKey(fieldName)) {
BetterGrassify.LOGGER.info("[BetterGrassify] Missing config field '{}'. Re-saving as default.", fieldName);
configJson.put(fieldName, JANKSON.toJson(defaultValue));
modified.set(true);
modified = true;
}
} catch (IllegalAccessException e) {
BetterGrassify.LOGGER.error("[BetterGrassify] Failed to access field '{}'", field.getName(), e);
}
}

if (modified.get()) {
if (modified) {
var config = JANKSON.fromJson(configJson, BetterGrassifyConfig.class);
save(config);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package dev.ultimatchamp.bettergrass.model;

import dev.ultimatchamp.bettergrass.BetterGrassify;
import dev.ultimatchamp.bettergrass.config.BetterGrassifyConfig;
import dev.ultimatchamp.bettergrass.util.SpriteCalculator;
import net.fabricmc.fabric.api.renderer.v1.mesh.MutableQuadView;
Expand Down

0 comments on commit ee6335e

Please sign in to comment.