From 9f974da00d4e6ce846c55119ab0bb99121c18af1 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Mon, 4 May 2026 12:21:28 +0000 Subject: [PATCH] chore: finalize resource pack translation support Agent-Logs-Url: https://github.com/MCCTeam/Minecraft-Console-Client/sessions/d7467a13-d1ff-4cd6-a332-1ac389c445da Co-authored-by: milutinke <441903+milutinke@users.noreply.github.com> --- .../Protocol/Handlers/Protocol18.cs | 2 ++ .../Protocol/Message/ChatParser.cs | 19 ++++++++++--------- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/MinecraftClient/Protocol/Handlers/Protocol18.cs b/MinecraftClient/Protocol/Handlers/Protocol18.cs index 7438faf6..5973178d 100644 --- a/MinecraftClient/Protocol/Handlers/Protocol18.cs +++ b/MinecraftClient/Protocol/Handlers/Protocol18.cs @@ -705,6 +705,8 @@ namespace MinecraftClient.Protocol.Handlers var url = dataTypes.ReadNextString(packetData); var hash = dataTypes.ReadNextString(packetData); + // Use the server-provided UUID when available, then fall back to the legacy SHA-1 hash, + // and finally the URL so pre-UUID resource packs can still be replaced or cleared locally. string packIdentifier = uuid != Guid.Empty ? uuid.ToString("D") : (hash.Length == 40 ? hash : url); if (protocolVersion >= MC_1_17_Version) diff --git a/MinecraftClient/Protocol/Message/ChatParser.cs b/MinecraftClient/Protocol/Message/ChatParser.cs index c40db0f5..9e7d52b2 100644 --- a/MinecraftClient/Protocol/Message/ChatParser.cs +++ b/MinecraftClient/Protocol/Message/ChatParser.cs @@ -262,8 +262,10 @@ namespace MinecraftClient.Protocol.Message } private const long MaxResourcePackDownloadBytes = 256L * 1024 * 1024; + private const int ResourcePackDownloadBufferSize = 81920; private static readonly List ResourcePackTranslationLayers = []; + private static readonly HttpClient ResourcePackHttpClient = new(); /// /// Initialize translation rules. @@ -521,16 +523,15 @@ namespace MinecraftClient.Protocol.Message private static void DownloadResourcePack(Uri resourcePackUri, string hash, string temporaryFilePath) { - using HttpClient httpClient = new(); using HttpResponseMessage response = - httpClient.GetAsync(resourcePackUri, HttpCompletionOption.ResponseHeadersRead).GetAwaiter().GetResult(); + ResourcePackHttpClient.GetAsync(resourcePackUri, HttpCompletionOption.ResponseHeadersRead).GetAwaiter().GetResult(); response.EnsureSuccessStatusCode(); using Stream resourcePackStream = response.Content.ReadAsStream(); using FileStream temporaryFile = File.Create(temporaryFilePath); using IncrementalHash incrementalHash = IncrementalHash.CreateHash(HashAlgorithmName.SHA1); - byte[] buffer = new byte[81920]; + byte[] buffer = new byte[ResourcePackDownloadBufferSize]; long totalBytes = 0; while (true) @@ -553,13 +554,13 @@ namespace MinecraftClient.Protocol.Message { string downloadedHash = Convert.ToHexString(incrementalHash.GetHashAndReset()); if (!downloadedHash.Equals(hash, StringComparison.OrdinalIgnoreCase)) - throw new InvalidDataException(); + throw new InvalidDataException($"Resource pack hash mismatch for {resourcePackUri}. Expected {hash}, got {downloadedHash}."); } } private static void LoadResourcePackTranslations(string packIdentifier, Stream resourcePackStream) { - var fallbackTranslations = new Dictionary(StringComparer.Ordinal); + var mergedTranslations = new Dictionary(StringComparer.Ordinal); var selectedLanguageTranslations = new Dictionary(StringComparer.Ordinal); string selectedLanguage = Config.Main.Advanced.Language; @@ -572,7 +573,7 @@ namespace MinecraftClient.Protocol.Message if (language.Equals("en_us", StringComparison.OrdinalIgnoreCase)) { - MergeResourcePackTranslations(entry, fallbackTranslations); + MergeResourcePackTranslations(entry, mergedTranslations); } else if (language.Equals(selectedLanguage, StringComparison.OrdinalIgnoreCase)) { @@ -581,12 +582,12 @@ namespace MinecraftClient.Protocol.Message } foreach (var entry in selectedLanguageTranslations) - fallbackTranslations[entry.Key] = entry.Value; + mergedTranslations[entry.Key] = entry.Value; RemoveResourcePackTranslations(packIdentifier); - if (fallbackTranslations.Count > 0) - ResourcePackTranslationLayers.Add(new ResourcePackTranslationLayer(packIdentifier, fallbackTranslations)); + if (mergedTranslations.Count > 0) + ResourcePackTranslationLayers.Add(new ResourcePackTranslationLayer(packIdentifier, mergedTranslations)); } private static bool TryGetResourcePackLanguage(string entryPath, out string? language)