Skip to content

Commit

Permalink
Merge pull request #139 from IceCruelStuff/master
Browse files Browse the repository at this point in the history
Update bedrock assets download
  • Loading branch information
kennyvv authored Dec 31, 2022
2 parents 324b919 + 089aaaa commit a3a20c9
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 53 deletions.
16 changes: 15 additions & 1 deletion src/Alex/ResourceManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
96 changes: 44 additions & 52 deletions src/Alex/Utils/Assets/MCBedrockAssetUtils.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
using System;
using System.Collections.Generic;

This comment has been minimized.

Copy link
@Del-Nachos

Del-Nachos Oct 14, 2023

minecraft is now intergrating to unity real (joke btw)

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;
Expand All @@ -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_(?<version>[\d\.]+).zip", RegexOptions.Compiled);

private IStorageSystem Storage { get; }

public MCBedrockAssetUtils(IStorageSystem storage)
Expand Down Expand Up @@ -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<Dictionary<string, Dictionary<string, string>>>(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;
Expand Down

0 comments on commit a3a20c9

Please sign in to comment.