From 15b8b93d4442dace4a6ef5815ecd086758c58adc Mon Sep 17 00:00:00 2001 From: onebeastchris Date: Thu, 9 Nov 2023 12:44:27 +0100 Subject: [PATCH] We need to ensure no invalid packs end up being loaded - otherwise, chaos ensues since the client will either show bogus resource pack size values (invalid sizes), or skip resource packs altogether (if not properly zipped). --- .../geyser/network/UpstreamPacketHandler.java | 16 +++++++++------- .../geyser/pack/url/GeyserUrlPackCodec.java | 8 +++++--- .../registry/loader/ResourcePackLoader.java | 2 +- .../java/org/geysermc/geyser/util/WebUtils.java | 14 ++++++-------- 4 files changed, 21 insertions(+), 19 deletions(-) diff --git a/core/src/main/java/org/geysermc/geyser/network/UpstreamPacketHandler.java b/core/src/main/java/org/geysermc/geyser/network/UpstreamPacketHandler.java index bf38b4e49..a6b9f216e 100644 --- a/core/src/main/java/org/geysermc/geyser/network/UpstreamPacketHandler.java +++ b/core/src/main/java/org/geysermc/geyser/network/UpstreamPacketHandler.java @@ -55,15 +55,13 @@ import org.geysermc.geyser.util.WebUtils; import java.io.IOException; import java.nio.ByteBuffer; import java.nio.channels.SeekableByteChannel; -import java.util.ArrayDeque; -import java.util.Deque; -import java.util.HashMap; -import java.util.OptionalInt; +import java.util.*; public class UpstreamPacketHandler extends LoggingPacketHandler { private boolean networkSettingsRequested = false; private final Deque packsToSent = new ArrayDeque<>(); + private final List brokenResourcePacks = new ArrayList<>(); private SessionLoadResourcePacksEventImpl resourcePackLoadEvent; @@ -281,9 +279,13 @@ public class UpstreamPacketHandler extends LoggingPacketHandler { // Check for packs that the client should normally download on its own. If the client cannot find the pack, we provide it instead. if (codec instanceof UrlPackCodec urlPackCodec) { if (!GameProtocol.isPre1_20_30(this.session)) { - GeyserImpl.getInstance().getLogger().warning("Received a request for a remote pack that the client should have already downloaded!" + - "Is the pack at the URL " + urlPackCodec.url() + " still available?"); - WebUtils.checkUrlAndDownloadRemotePack(urlPackCodec.url()); + // Ensure we don't a. spam console, and b. spam download/check requests + if (!brokenResourcePacks.contains(packet.getPackId())) { + brokenResourcePacks.add(packet.getPackId()); + GeyserImpl.getInstance().getLogger().warning("Received a request for a remote pack that the client should have already downloaded!" + + "Is the pack at the URL " + urlPackCodec.url() + " still available?"); + WebUtils.checkUrlAndDownloadRemotePack(urlPackCodec.url()); + } } } diff --git a/core/src/main/java/org/geysermc/geyser/pack/url/GeyserUrlPackCodec.java b/core/src/main/java/org/geysermc/geyser/pack/url/GeyserUrlPackCodec.java index 0a60f2ce9..f1bb8eb3a 100644 --- a/core/src/main/java/org/geysermc/geyser/pack/url/GeyserUrlPackCodec.java +++ b/core/src/main/java/org/geysermc/geyser/pack/url/GeyserUrlPackCodec.java @@ -35,6 +35,7 @@ import org.geysermc.geyser.registry.loader.ResourcePackLoader; import java.io.IOException; import java.nio.channels.SeekableByteChannel; +import java.nio.file.Path; public class GeyserUrlPackCodec extends UrlPackCodec { private final String url; @@ -80,16 +81,17 @@ public class GeyserUrlPackCodec extends UrlPackCodec { public ResourcePack create() { if (this.fallback == null) { try { - this.fallback = new GeyserPathPackCodec(ResourcePackLoader.downloadPack(url).whenComplete((pack, throwable) -> { + final Path downloadedPack = ResourcePackLoader.downloadPack(url).whenComplete((pack, throwable) -> { if (throwable != null) { GeyserImpl.getInstance().getLogger().error("Failed to download pack from " + url, throwable); if (GeyserImpl.getInstance().getConfig().isDebugMode()) { throwable.printStackTrace(); } } - }).join()); + }).join(); + this.fallback = new GeyserPathPackCodec(downloadedPack); } catch (Exception e) { - throw new IllegalArgumentException("Unable to download pack from " + url, e); + throw new IllegalArgumentException("Failed to download pack from " + url, e); } } return ResourcePackLoader.readPack(this); diff --git a/core/src/main/java/org/geysermc/geyser/registry/loader/ResourcePackLoader.java b/core/src/main/java/org/geysermc/geyser/registry/loader/ResourcePackLoader.java index 79933264e..8f387ba8d 100644 --- a/core/src/main/java/org/geysermc/geyser/registry/loader/ResourcePackLoader.java +++ b/core/src/main/java/org/geysermc/geyser/registry/loader/ResourcePackLoader.java @@ -254,7 +254,7 @@ public class ResourcePackLoader implements RegistryLoader