From b2ea5792be3ed103b8a14726f8034bbebd64397a Mon Sep 17 00:00:00 2001 From: Camotoy <20743703+Camotoy@users.noreply.github.com> Date: Thu, 26 Sep 2024 16:39:09 -0400 Subject: [PATCH] Better config JSON encoding (something is broken with Cloudflare; we'll figure it out --- core/build.gradle.kts | 4 +- .../geyser/command/defaults/DumpCommand.java | 4 +- .../org/geysermc/geyser/dump/DumpInfo.java | 41 +++++++++++++++++-- .../configuration/ConfigLoaderTest.java | 1 - 4 files changed, 43 insertions(+), 7 deletions(-) diff --git a/core/build.gradle.kts b/core/build.gradle.kts index f4f935b96..aa4ae3fef 100644 --- a/core/build.gradle.kts +++ b/core/build.gradle.kts @@ -32,7 +32,9 @@ dependencies { api(libs.bundles.protocol) - api(libs.minecraftauth) + api(libs.minecraftauth) { + exclude("com.google.code.gson", "gson") + } api(libs.mcprotocollib) { exclude("io.netty", "netty-all") exclude("net.raphimc", "MinecraftAuth") diff --git a/core/src/main/java/org/geysermc/geyser/command/defaults/DumpCommand.java b/core/src/main/java/org/geysermc/geyser/command/defaults/DumpCommand.java index f25c245f9..6a71ca695 100644 --- a/core/src/main/java/org/geysermc/geyser/command/defaults/DumpCommand.java +++ b/core/src/main/java/org/geysermc/geyser/command/defaults/DumpCommand.java @@ -28,7 +28,6 @@ package org.geysermc.geyser.command.defaults; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.google.gson.JsonObject; -import com.google.gson.JsonParser; import org.geysermc.geyser.GeyserImpl; import org.geysermc.geyser.api.util.TriState; import org.geysermc.geyser.command.GeyserCommand; @@ -37,6 +36,7 @@ import org.geysermc.geyser.dump.DumpInfo; import org.geysermc.geyser.text.AsteriskSerializer; import org.geysermc.geyser.text.ChatColor; import org.geysermc.geyser.text.GeyserLocale; +import org.geysermc.geyser.util.JsonUtils; import org.geysermc.geyser.util.WebUtils; import org.incendo.cloud.CommandManager; import org.incendo.cloud.context.CommandContext; @@ -145,7 +145,7 @@ public class DumpCommand extends GeyserCommand { JsonObject responseNode; try { response = WebUtils.post(DUMP_URL + "documents", dumpData); - responseNode = (JsonObject) new JsonParser().parse(response); + responseNode = JsonUtils.parseJson(response); } catch (IOException e) { source.sendMessage(ChatColor.RED + GeyserLocale.getPlayerLocaleString("geyser.commands.dump.upload_error", source.locale())); geyser.getLogger().error(GeyserLocale.getLocaleStringLog("geyser.commands.dump.upload_error_short"), e); diff --git a/core/src/main/java/org/geysermc/geyser/dump/DumpInfo.java b/core/src/main/java/org/geysermc/geyser/dump/DumpInfo.java index acb326d70..e4d71850f 100644 --- a/core/src/main/java/org/geysermc/geyser/dump/DumpInfo.java +++ b/core/src/main/java/org/geysermc/geyser/dump/DumpInfo.java @@ -28,18 +28,22 @@ package org.geysermc.geyser.dump; import com.google.common.hash.Hashing; import com.google.common.io.ByteSource; import com.google.common.io.Files; +import com.google.gson.JsonArray; +import com.google.gson.JsonElement; +import com.google.gson.JsonNull; import com.google.gson.JsonObject; import com.google.gson.JsonParser; +import com.google.gson.JsonPrimitive; import com.google.gson.annotations.SerializedName; import it.unimi.dsi.fastutil.objects.Object2IntMap; import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap; import lombok.Getter; +import org.checkerframework.checker.nullness.qual.Nullable; import org.cloudburstmc.protocol.bedrock.codec.BedrockCodec; import org.geysermc.floodgate.util.DeviceOs; import org.geysermc.geyser.GeyserImpl; import org.geysermc.geyser.api.GeyserApi; import org.geysermc.geyser.api.extension.Extension; -import org.geysermc.geyser.configuration.AdvancedConfig; import org.geysermc.geyser.network.GameProtocol; import org.geysermc.geyser.session.GeyserSession; import org.geysermc.geyser.text.AsteriskSerializer; @@ -106,12 +110,13 @@ public class DumpInfo { ConfigurationNode configNode = CommentedConfigurationNode.root(options); configNode.set(geyser.config()); - this.config = configNode.get(geyser.config().getClass()); + this.config = toGson(configNode); ConfigurationNode advancedConfigNode = CommentedConfigurationNode.root(options); advancedConfigNode.set(geyser.config().advanced()); - this.advancedConfig = advancedConfigNode.get(AdvancedConfig.class); + this.advancedConfig = toGson(advancedConfigNode); } catch (SerializationException e) { + e.printStackTrace(); if (geyser.config().debugMode()) { e.printStackTrace(); } @@ -164,6 +169,36 @@ public class DumpInfo { } } + private JsonElement toGson(ConfigurationNode node) { + if (node.isMap()) { + JsonObject object = new JsonObject(); + node.childrenMap().forEach((key, value) -> { + JsonElement json = toGson(value); + object.add(key.toString(), json); + }); + return object; + } else if (node.isList()) { + JsonArray array = new JsonArray(); + node.childrenList().forEach(childNode -> array.add(toGson(childNode))); + return array; + } else { + return convertRawScalar(node); + } + } + + private JsonElement convertRawScalar(ConfigurationNode node) { + final @Nullable Object value = node.rawScalar(); + if (value == null) { + return JsonNull.INSTANCE; + } else if (value instanceof Number n) { + return new JsonPrimitive(n); + } else if (value instanceof Boolean b) { + return new JsonPrimitive(b); + } else { + return new JsonPrimitive(value.toString()); + } + } + @Getter public static class VersionInfo { private final String name; diff --git a/core/src/test/java/org/geysermc/geyser/configuration/ConfigLoaderTest.java b/core/src/test/java/org/geysermc/geyser/configuration/ConfigLoaderTest.java index 098dce7dc..072427864 100644 --- a/core/src/test/java/org/geysermc/geyser/configuration/ConfigLoaderTest.java +++ b/core/src/test/java/org/geysermc/geyser/configuration/ConfigLoaderTest.java @@ -53,7 +53,6 @@ public class ConfigLoaderTest { File file = tempDirectory.resolve("config.yml").toFile(); - // Sorry Konicai... forAllConfigs(type -> { new ConfigLoader(file).transformer(n -> this.config1 = n.copy()).load(type); long initialModification = file.lastModified();