3
0
Mirror von https://github.com/ViaVersion/ViaVersion.git synchronisiert 2024-12-27 08:30:09 +01:00

Slightly improve logging on config loading errors

Dieser Commit ist enthalten in:
Nassim Jahnke 2024-07-21 12:12:06 +02:00
Ursprung 6fb072120b
Commit 1476adb281
Es konnte kein GPG-Schlüssel zu dieser Signatur gefunden werden
GPG-Schlüssel-ID: EF6771C01F6EF02F

Datei anzeigen

@ -86,8 +86,8 @@ public abstract class Config {
private synchronized Map<String, Object> loadConfig(File location, InputStreamSupplier configSupplier) { private synchronized Map<String, Object> loadConfig(File location, InputStreamSupplier configSupplier) {
List<String> unsupported = getUnsupportedOptions(); List<String> unsupported = getUnsupportedOptions();
try { try (InputStream inputStream = configSupplier.get()) {
commentStore.storeComments(configSupplier.get()); commentStore.storeComments(inputStream);
for (String option : unsupported) { for (String option : unsupported) {
List<String> comments = commentStore.header(option); List<String> comments = commentStore.header(option);
if (comments != null) { if (comments != null) {
@ -95,42 +95,46 @@ public abstract class Config {
} }
} }
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); throw new RuntimeException("Failed to load default config comments", e);
} }
// Load actual file data
Map<String, Object> config = null; Map<String, Object> config = null;
if (location.exists()) { if (location.exists()) {
try (FileInputStream input = new FileInputStream(location)) { try (FileInputStream input = new FileInputStream(location)) {
//noinspection unchecked
config = (Map<String, Object>) YAML.get().load(input); config = (Map<String, Object>) YAML.get().load(input);
} catch (IOException e) { } 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) { if (config == null) {
config = new HashMap<>(); config = new HashMap<>();
} }
Map<String, Object> defaults = config; // Load default config and merge with current to make sure we always have an up-to-date/correct config
Map<String, Object> mergedConfig;
try (InputStream stream = configSupplier.get()) { try (InputStream stream = configSupplier.get()) {
defaults = (Map<String, Object>) YAML.get().load(stream); //noinspection unchecked
for (String option : unsupported) { mergedConfig = (Map<String, Object>) YAML.get().load(stream);
defaults.remove(option);
}
// Merge with defaultLoader
for (Map.Entry<String, Object> 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());
}
}
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); throw new RuntimeException("Failed to load default config", e);
}
for (String option : unsupported) {
mergedConfig.remove(option);
}
for (Map.Entry<String, Object> 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<String, Object> config); protected abstract void handleConfig(Map<String, Object> config);