From 37984f063125b097f5091da9edb987eb5ccbaf53 Mon Sep 17 00:00:00 2001 From: Tomi Eckert Date: Wed, 10 Dec 2025 13:38:45 +0100 Subject: [PATCH] fix downloader --- BlueberryUpdater/Program.cs | 40 ++++++++++++++++++++++++------------- 1 file changed, 26 insertions(+), 14 deletions(-) diff --git a/BlueberryUpdater/Program.cs b/BlueberryUpdater/Program.cs index d7c3b95..9f9bdf8 100644 --- a/BlueberryUpdater/Program.cs +++ b/BlueberryUpdater/Program.cs @@ -1,9 +1,5 @@ using System.Diagnostics; using System.IO.Compression; -using System.Net.Http.Json; -using System.Reflection; -using System.Text.Json; -using Windows.Media.Protection.PlayReady; namespace BlueberryUpdater { @@ -74,14 +70,12 @@ namespace BlueberryUpdater string releaseUrl = "https://git.technopunk.space/api/v1/repos/tomi/Blueberry/releases/latest"; // 1. Fetch JSON - var root = client.GetFromJsonAsync(releaseUrl).ConfigureAwait(false).GetAwaiter().GetResult(); + var json = client.GetStringAsync(releaseUrl).ConfigureAwait(false).GetAwaiter().GetResult(); + json = json.Split("\"name\":\"payload.zip\",")[1]; + // 2. Find URL for "payload.zip" - string downloadUrl = root.GetProperty("assets") - .EnumerateArray() - .First(x => x.GetProperty("name").GetString() == "payload.zip") - .GetProperty("browser_download_url") - .GetString() ?? throw new NullReferenceException(); + string downloadUrl = json.Split("\"browser_download_url\":\"")[1].Split("\"")[0]; Console.WriteLine("Downloading Blueberry..."); var stream = DownloadFileWithProgressAsync(client, downloadUrl).GetAwaiter().GetResult(); @@ -146,6 +140,8 @@ namespace BlueberryUpdater static void DrawProgressBar(int percent, string filename) { + percent = Math.Clamp(percent, 0, 100); + // Move cursor to start of line Console.CursorLeft = 0; @@ -153,7 +149,7 @@ namespace BlueberryUpdater string shortName = filename.Length > 20 ? filename.Substring(0, 17) + "..." : filename.PadRight(20); Console.Write("["); - int width = Console.WindowWidth - 21; // Width of the bar + int width = Console.WindowWidth - 31; // Width of the bar int progress = (int)((percent / 100.0) * width); // Draw filled part @@ -161,7 +157,7 @@ namespace BlueberryUpdater // Draw empty part Console.Write(new string('-', width - progress)); - Console.Write($"] {percent}% {shortName}"); + Console.Write($"] {percent}% {shortName} "); } static async Task DownloadFileWithProgressAsync(HttpClient client, string url) @@ -174,7 +170,7 @@ namespace BlueberryUpdater var canReportProgress = totalBytes != -1; using var contentStream = await response.Content.ReadAsStreamAsync(); - using var memoryStream = new MemoryStream(); + var memoryStream = new MemoryStream(); var buffer = new byte[8192]; long totalRead = 0; @@ -189,11 +185,13 @@ namespace BlueberryUpdater DrawProgressBar((int)((double)totalRead / totalBytes * 100), "Downloading..."); } DrawProgressBar(100, "Done"); + Console.WriteLine(); return memoryStream; } static void ExtractWithProgress(MemoryStream stream) { - using (ZipArchive archive = new(stream)) + stream.Position = 0; + using (ZipArchive archive = new(stream, ZipArchiveMode.Read)) { int totalEntries = archive.Entries.Count; int currentEntry = 0; @@ -229,6 +227,20 @@ namespace BlueberryUpdater } } DrawProgressBar(100, "Done"); + Console.WriteLine(); + } + } + + public class Dto + { + public class Item + { + public Assets[] assets { get; set; } + } + public class Assets + { + public string name { get; set; } + public string browser_download_url { get; set; } } } }