From 1476adb2812dbb94b843236b98ba1e42d1bc8981 Mon Sep 17 00:00:00 2001 From: Nassim Jahnke Date: Sun, 21 Jul 2024 12:12:06 +0200 Subject: [PATCH] Slightly improve logging on config loading errors --- .../viaversion/viaversion/util/Config.java | 48 ++++++++++--------- 1 file changed, 26 insertions(+), 22 deletions(-) diff --git a/common/src/main/java/com/viaversion/viaversion/util/Config.java b/common/src/main/java/com/viaversion/viaversion/util/Config.java index 79b683793..e5d27d7f3 100644 --- a/common/src/main/java/com/viaversion/viaversion/util/Config.java +++ b/common/src/main/java/com/viaversion/viaversion/util/Config.java @@ -86,8 +86,8 @@ public abstract class Config { private synchronized Map loadConfig(File location, InputStreamSupplier configSupplier) { List unsupported = getUnsupportedOptions(); - try { - commentStore.storeComments(configSupplier.get()); + try (InputStream inputStream = configSupplier.get()) { + commentStore.storeComments(inputStream); for (String option : unsupported) { List comments = commentStore.header(option); if (comments != null) { @@ -95,42 +95,46 @@ public abstract class Config { } } } catch (IOException e) { - e.printStackTrace(); + throw new RuntimeException("Failed to load default config comments", e); } + + // Load actual file data Map config = null; if (location.exists()) { try (FileInputStream input = new FileInputStream(location)) { + //noinspection unchecked config = (Map) YAML.get().load(input); } catch (IOException e) { - e.printStackTrace(); + throw new RuntimeException("Failed to load config", e); + } catch (Exception e) { + logger.severe("Failed to load config, make sure your input is valid"); + throw new RuntimeException("Failed to load config (malformed input?)", e); } } if (config == null) { config = new HashMap<>(); } - Map defaults = config; + // Load default config and merge with current to make sure we always have an up-to-date/correct config + Map mergedConfig; try (InputStream stream = configSupplier.get()) { - defaults = (Map) YAML.get().load(stream); - for (String option : unsupported) { - defaults.remove(option); - } - // Merge with defaultLoader - for (Map.Entry entry : config.entrySet()) { - // Set option in new conf if exists - if (defaults.containsKey(entry.getKey()) && !unsupported.contains(entry.getKey())) { - defaults.put(entry.getKey(), entry.getValue()); - } - } + //noinspection unchecked + mergedConfig = (Map) YAML.get().load(stream); } catch (IOException e) { - e.printStackTrace(); + throw new RuntimeException("Failed to load default config", e); + } + for (String option : unsupported) { + mergedConfig.remove(option); + } + for (Map.Entry entry : config.entrySet()) { + // Set option in new conf if it exists + mergedConfig.computeIfPresent(entry.getKey(), (key, value) -> entry.getValue()); } - // Call Handler - handleConfig(defaults); - // Save - save(location, defaults); - return defaults; + handleConfig(mergedConfig); + save(location, mergedConfig); + + return mergedConfig; } protected abstract void handleConfig(Map config);