diff --git a/src/Alex/ResourceManager.cs b/src/Alex/ResourceManager.cs index 052058fab..98a5e6c96 100644 --- a/src/Alex/ResourceManager.cs +++ b/src/Alex/ResourceManager.cs @@ -419,7 +419,21 @@ private bool CheckBedrockAssets(IProgressReceiver progressReceiver, out string b { using (ZipArchive zipArchive = new ZipArchive(Storage.OpenFileStream(bedrockPath, FileMode.Open))) { - zipArchive.ExtractToDirectory(di.FullName); + var root = "bedrock-samples-main/resource_pack/"; + var resourcePackFiles = from currentEntry in zipArchive.Entries + where currentEntry.FullName.StartsWith(root) + where !String.IsNullOrWhiteSpace(currentEntry.Name) + select currentEntry; + + foreach (ZipArchiveEntry entry in resourcePackFiles) + { + var path = Path.Combine(di.FullName, entry.FullName.Substring(root.Length)); + if (!Directory.Exists(path)) + { + Storage.TryCreateDirectory(Path.GetDirectoryName(path)); + } + entry.ExtractToFile(path); + } } Storage.Delete(bedrockPath); diff --git a/src/Alex/Utils/Assets/MCBedrockAssetUtils.cs b/src/Alex/Utils/Assets/MCBedrockAssetUtils.cs index 7751a580c..a4aaf54a4 100644 --- a/src/Alex/Utils/Assets/MCBedrockAssetUtils.cs +++ b/src/Alex/Utils/Assets/MCBedrockAssetUtils.cs @@ -1,9 +1,11 @@ using System; +using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Net; using System.Net.Http; using System.Text.RegularExpressions; +using System.Text.Json; using System.Threading; using Alex.Common; using Alex.Common.Services; @@ -16,12 +18,10 @@ public class MCBedrockAssetUtils { private static readonly Logger Log = LogManager.GetCurrentClassLogger(typeof(MCBedrockAssetUtils)); - private const string DownloadURL = "https://aka.ms/resourcepacktemplate"; + private const string VersionURL = "https://raw.githubusercontent.com/Mojang/bedrock-samples/main/version.json"; + private const string DownloadURL = "https://codeload.github.com/Mojang/bedrock-samples/zip/refs/heads/main"; private static readonly string CurrentBedrockVersionStorageKey = Path.Combine("assets", "bedrock-version.txt"); - private static readonly Regex ExtractVersionFromFilename = new Regex( - @"Vanilla_.*Pack_(?[\d\.]+).zip", RegexOptions.Compiled); - private IStorageSystem Storage { get; } public MCBedrockAssetUtils(IStorageSystem storage) @@ -67,71 +67,63 @@ public bool CheckUpdate(IProgressReceiver progressReceiver, string targetPath, o try { - var zipDownloadHeaders = httpClient.Send( - new HttpRequestMessage(HttpMethod.Get, DownloadURL), HttpCompletionOption.ResponseHeadersRead); - - var fileName = zipDownloadHeaders.Content?.Headers?.ContentDisposition?.FileName - ?? zipDownloadHeaders.RequestMessage?.RequestUri?.LocalPath; + var versionRequest = httpClient.Send(new HttpRequestMessage(HttpMethod.Get, VersionURL), HttpCompletionOption.ResponseContentRead); + var versionStream = versionRequest.Content.ReadAsStreamAsync().Result; - var versionMatch = ExtractVersionFromFilename.Match(fileName); + var latestVersion = JsonSerializer.Deserialize>>(versionStream)["latest"]["version"]; - if (versionMatch.Success) + if (latestVersion != currentVersion) { - var latestVersion = versionMatch.Groups["version"].Value; - - if (latestVersion != currentVersion) - { - if (Storage.Exists(path)) - Storage.Delete(path); + if (Storage.Exists(path)) + Storage.Delete(path); - progressReceiver?.UpdateProgress( - 0, "Downloading latest bedrock assets...", "This could take a while..."); + progressReceiver?.UpdateProgress( + 0, "Downloading latest bedrock assets...", "This could take a while..."); - Stopwatch sw = new Stopwatch(); + Stopwatch sw = new Stopwatch(); - using (HttpClient c = new HttpClient()) + using (HttpClient c = new HttpClient()) + { + string archivePath = string.Empty; + + var downloadTask = c.DownloadDataAsync(new Uri(DownloadURL), (p) => { - string archivePath = string.Empty; + var downloadSpeed = + $"Download speed: {FormattingUtils.GetBytesReadable((long)(Convert.ToDouble(p.BytesReceived) / sw.Elapsed.TotalSeconds), 2)}/s"; + + var totalSize = p.TotalBytesToReceive ?? 1; + var progressPercentage = (int)Math.Ceiling((100d / totalSize) * p.BytesReceived); + + progressReceiver?.UpdateProgress( + progressPercentage, $"Downloading latest bedrock assets...", downloadSpeed); - var downloadTask = c.DownloadDataAsync(zipDownloadHeaders.RequestMessage.RequestUri, (p) => + }, CancellationToken.None).ContinueWith( + r => { - var downloadSpeed = - $"Download speed: {FormattingUtils.GetBytesReadable((long)(Convert.ToDouble(p.BytesReceived) / sw.Elapsed.TotalSeconds), 2)}/s"; - - var totalSize = p.TotalBytesToReceive ?? 1; - var progressPercentage = (int)Math.Ceiling((100d / totalSize) * p.BytesReceived); - - progressReceiver?.UpdateProgress( - progressPercentage, $"Downloading latest bedrock assets...", downloadSpeed); - - }, CancellationToken.None).ContinueWith( - r => + if (r.IsFaulted) { - if (r.IsFaulted) - { - Log.Error(r.Exception, "Failed to download bedrock assets..."); - return; - } + Log.Error(r.Exception, "Failed to download bedrock assets..."); + return; + } - var content = r.Result; + var content = r.Result; - archivePath = Path.Combine("assets", $"bedrock-{latestVersion}.zip"); + archivePath = Path.Combine("assets", $"bedrock-{latestVersion}.zip"); - // save locally + // save locally - Storage.TryWriteString(CurrentBedrockVersionStorageKey, latestVersion); + Storage.TryWriteString(CurrentBedrockVersionStorageKey, latestVersion); - Storage.TryWriteBytes(archivePath, content); - }); - - sw.Restart(); - - SpinWait.SpinUntil(() => downloadTask.IsCompleted); - path = archivePath; - } + Storage.TryWriteBytes(archivePath, content); + }); + + sw.Restart(); - return true; + SpinWait.SpinUntil(() => downloadTask.IsCompleted); + path = archivePath; } + + return true; } return false;