From 1642af9bacc88c20614234296fd596932b14d8df Mon Sep 17 00:00:00 2001 From: Ben Kerllenevich Date: Fri, 21 Jan 2022 15:53:51 -0500 Subject: BukkitServerConfigProvider improvements (#159) --- .../spark/bukkit/BukkitServerConfigProvider.java | 103 +++++++++++++++++---- .../serverconfig/AbstractServerConfigProvider.java | 14 +-- 2 files changed, 95 insertions(+), 22 deletions(-) diff --git a/spark-bukkit/src/main/java/me/lucko/spark/bukkit/BukkitServerConfigProvider.java b/spark-bukkit/src/main/java/me/lucko/spark/bukkit/BukkitServerConfigProvider.java index b1d2a60..5fdd178 100644 --- a/spark-bukkit/src/main/java/me/lucko/spark/bukkit/BukkitServerConfigProvider.java +++ b/spark-bukkit/src/main/java/me/lucko/spark/bukkit/BukkitServerConfigProvider.java @@ -20,8 +20,8 @@ package me.lucko.spark.bukkit; -import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; +import com.google.common.collect.ImmutableSet; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.google.gson.JsonElement; @@ -32,31 +32,31 @@ import me.lucko.spark.common.platform.serverconfig.AbstractServerConfigProvider; import org.bukkit.configuration.MemorySection; import org.bukkit.configuration.file.YamlConfiguration; +import co.aikar.timings.TimingsManager; + import java.io.BufferedReader; import java.io.IOException; import java.nio.charset.StandardCharsets; import java.nio.file.Files; +import java.nio.file.Path; import java.nio.file.Paths; +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Properties; public class BukkitServerConfigProvider extends AbstractServerConfigProvider { private static final Gson GSON = new GsonBuilder() .registerTypeAdapter(MemorySection.class, (JsonSerializer) (obj, type, ctx) -> ctx.serialize(obj.getValues(false))) .create(); - private static final Map FILES = ImmutableMap.of( - "bukkit.yml", FileType.YAML, - "spigot.yml", FileType.YAML, - "paper.yml", FileType.YAML - ); - - // todo: make configurable? - private static final List HIDDEN_PATHS = ImmutableList.of( - "database", - "settings.bungeecord-addresses", - "settings.velocity-support.secret" - ); + /** A map of provided files and their type */ + private static final Map FILES; + /** A collection of paths to be excluded from the files */ + private static final Collection HIDDEN_PATHS; public BukkitServerConfigProvider() { super(FILES, HIDDEN_PATHS); @@ -64,15 +64,86 @@ public class BukkitServerConfigProvider extends AbstractServerConfigProvider values = config.getValues(false); + Path filePath = Paths.get(path); + if (!Files.exists(filePath)) { + return null; + } + + try (BufferedReader reader = Files.newBufferedReader(filePath, StandardCharsets.UTF_8)) { + Map values; + + if (type == FileType.PROPERTIES) { + Properties properties = new Properties(); + properties.load(reader); + + values = new HashMap<>(); + properties.forEach((k, v) -> { + String key = k.toString(); + String value = v.toString(); + + if ("true".equals(value) || "false".equals(value)) { + values.put(key, Boolean.parseBoolean(value)); + } else if (value.matches("\\d+")) { + values.put(key, Integer.parseInt(value)); + } else { + values.put(key, value); + } + }); + } else if (type == FileType.YAML) { + YamlConfiguration config = YamlConfiguration.loadConfiguration(reader); + values = config.getValues(false); + } else { + throw new IllegalArgumentException("Unknown file type: " + type); + } + return GSON.toJsonTree(values); } } enum FileType { + PROPERTIES, YAML } + static { + ImmutableMap.Builder files = ImmutableMap.builder() + .put("server.properties", FileType.PROPERTIES) + .put("bukkit.yml", FileType.YAML) + .put("spigot.yml", FileType.YAML) + .put("paper.yml", FileType.YAML) + .put("purpur.yml", FileType.YAML); + + for (String config : getSystemPropertyList("spark.serverconfigs.extra")) { + files.put(config, FileType.YAML); + } + + ImmutableSet.Builder hiddenPaths = ImmutableSet.builder() + .add("database") + .add("settings.bungeecord-addresses") + .add("settings.velocity-support.secret") + .add("server-ip") + .add("motd") + .add("resource-pack") + .addAll(getTimingsHiddenConfigs()) + .addAll(getSystemPropertyList("spark.serverconfigs.hiddenpaths")); + + FILES = files.build(); + HIDDEN_PATHS = hiddenPaths.build(); + } + + private static List getSystemPropertyList(String property) { + String value = System.getProperty(property); + return value == null + ? Collections.emptyList() + : Arrays.asList(value.split(",")); + } + + private static List getTimingsHiddenConfigs() { + try { + return TimingsManager.hiddenConfigs; + } catch (NoClassDefFoundError e) { + return Collections.emptyList(); + } + } + } diff --git a/spark-common/src/main/java/me/lucko/spark/common/platform/serverconfig/AbstractServerConfigProvider.java b/spark-common/src/main/java/me/lucko/spark/common/platform/serverconfig/AbstractServerConfigProvider.java index 98db960..1e6f1b4 100644 --- a/spark-common/src/main/java/me/lucko/spark/common/platform/serverconfig/AbstractServerConfigProvider.java +++ b/spark-common/src/main/java/me/lucko/spark/common/platform/serverconfig/AbstractServerConfigProvider.java @@ -26,9 +26,9 @@ import com.google.gson.JsonObject; import java.io.IOException; import java.util.Arrays; +import java.util.Collection; import java.util.Deque; import java.util.LinkedList; -import java.util.List; import java.util.Map; /** @@ -41,9 +41,9 @@ import java.util.Map; */ public abstract class AbstractServerConfigProvider> implements ServerConfigProvider { private final Map files; - private final List hiddenPaths; + private final Collection hiddenPaths; - protected AbstractServerConfigProvider(Map files, List hiddenPaths) { + protected AbstractServerConfigProvider(Map files, Collection hiddenPaths) { this.files = files; this.hiddenPaths = hiddenPaths; } @@ -55,8 +55,10 @@ public abstract class AbstractServerConfigProvider> implements this.files.forEach((path, type) -> { try { JsonElement json = load(path, type); - delete(json, this.hiddenPaths); - builder.put(path, json); + if (json != null) { + delete(json, this.hiddenPaths); + builder.put(path, json); + } } catch (Exception e) { e.printStackTrace(); } @@ -81,7 +83,7 @@ public abstract class AbstractServerConfigProvider> implements * @param json the json element * @param paths the paths to delete */ - private static void delete(JsonElement json, List paths) { + private static void delete(JsonElement json, Collection paths) { for (String path : paths) { Deque pathDeque = new LinkedList<>(Arrays.asList(path.split("\\."))); delete(json, pathDeque); -- cgit