From 0c86f02c498b731e83683c80f2dacce5f680482b Mon Sep 17 00:00:00 2001 From: Mark Vainomaa Date: Sun, 28 Oct 2018 18:15:32 +0200 Subject: [PATCH 1/2] Save configuration after loading to ensure new options being present in file For example if you had `[forced-hosts]` section missing, and `[servers]` section not having entries similar to default forced hosts ones, then new configuration would fail at validation and new options wouldn't be saved to file at all. --- .../java/com/velocitypowered/proxy/VelocityServer.java | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/proxy/src/main/java/com/velocitypowered/proxy/VelocityServer.java b/proxy/src/main/java/com/velocitypowered/proxy/VelocityServer.java index 10c54c201..29d5dec2e 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/VelocityServer.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/VelocityServer.java @@ -147,16 +147,15 @@ public class VelocityServer implements ProxyServer { Path configPath = Paths.get("velocity.toml"); configuration = VelocityConfiguration.read(configPath); + AnnotatedConfig + .saveConfig(configuration.dumpConfig(), configPath); // Resave config to add new values + if (!configuration.validate()) { logger.error( "Your configuration is invalid. Velocity will refuse to start up until the errors are resolved."); LogManager.shutdown(); System.exit(1); } - - AnnotatedConfig - .saveConfig(configuration.dumpConfig(), configPath); //Resave config to add new values - } catch (Exception e) { logger.error("Unable to read/load/save your velocity.toml. The server will shut down.", e); LogManager.shutdown(); From e0330e9f278144348268360a82ca0e69e4c195c9 Mon Sep 17 00:00:00 2001 From: Mark Vainomaa Date: Sun, 28 Oct 2018 18:18:44 +0200 Subject: [PATCH 2/2] Move and rename key escaping methods while I'm here --- .../proxy/config/AnnotatedConfig.java | 14 +++++++++++--- .../proxy/config/VelocityConfiguration.java | 12 ++---------- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/proxy/src/main/java/com/velocitypowered/proxy/config/AnnotatedConfig.java b/proxy/src/main/java/com/velocitypowered/proxy/config/AnnotatedConfig.java index da05586d8..2d5ea5932 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/config/AnnotatedConfig.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/config/AnnotatedConfig.java @@ -125,7 +125,7 @@ public abstract class AnnotatedConfig { // Get a key name for config. Use field name if @ConfigKey annotation is not present. ConfigKey key = field.getAnnotation(ConfigKey.class); - final String name = safeKey(key == null ? field.getName() : key.value()); + final String name = escapeKeyIfNeeded(key == null ? field.getName() : key.value()); Object value = field.get(dumpable); @@ -142,7 +142,7 @@ public abstract class AnnotatedConfig { Map map = (Map) value; for (Entry entry : map.entrySet()) { lines.add( - safeKey(entry.getKey()) + " = " + serialize(entry.getValue())); // Save map data + escapeKeyIfNeeded(entry.getKey()) + " = " + serialize(entry.getValue())); // Save map data } lines.add(""); // Add empty line continue; @@ -203,7 +203,7 @@ public abstract class AnnotatedConfig { return value != null ? value.toString() : "null"; } - private static String safeKey(String key) { + protected static String escapeKeyIfNeeded(String key) { if (key.contains(".") && !(key.indexOf('"') == 0 && key.lastIndexOf('"') == (key.length() - 1))) { return '"' + key + '"'; @@ -211,6 +211,14 @@ public abstract class AnnotatedConfig { return key; } + protected static String unesacpeKeyIfNeeded(String key) { + int lastIndex; + if (key.indexOf('"') == 0 && (lastIndex = key.lastIndexOf('"')) == (key.length() - 1)) { + return key.substring(1, lastIndex); + } + return key; + } + /** * Writes list of strings to file. * diff --git a/proxy/src/main/java/com/velocitypowered/proxy/config/VelocityConfiguration.java b/proxy/src/main/java/com/velocitypowered/proxy/config/VelocityConfiguration.java index d33df5f20..7316f45b9 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/config/VelocityConfiguration.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/config/VelocityConfiguration.java @@ -487,9 +487,9 @@ public class VelocityConfiguration extends AnnotatedConfig implements ProxyConfi for (Map.Entry entry : toml.entrySet()) { if (entry.getValue() instanceof String) { forcedHosts - .put(stripQuotes(entry.getKey()), ImmutableList.of((String) entry.getValue())); + .put(unesacpeKeyIfNeeded(entry.getKey()), ImmutableList.of((String) entry.getValue())); } else if (entry.getValue() instanceof List) { - forcedHosts.put(stripQuotes(entry.getKey()), + forcedHosts.put(unesacpeKeyIfNeeded(entry.getKey()), ImmutableList.copyOf((List) entry.getValue())); } else { throw new IllegalStateException( @@ -512,14 +512,6 @@ public class VelocityConfiguration extends AnnotatedConfig implements ProxyConfi this.forcedHosts = forcedHosts; } - private static String stripQuotes(String key) { - int lastIndex; - if (key.indexOf('"') == 0 && (lastIndex = key.lastIndexOf('"')) == (key.length() - 1)) { - return key.substring(1, lastIndex); - } - return key; - } - @Override public String toString() { return "ForcedHosts{"