From d3d687ea2f545463a81e65fa768b8c42224a777c Mon Sep 17 00:00:00 2001 From: Wizzy69 Date: Thu, 5 May 2022 20:39:31 +0300 Subject: [PATCH] Updated download system --- DiscordBot/Program.cs | 71 +------------------ .../Online/Helpers/OnlineFunctions.cs | 9 ++- PluginManager/Online/ServerCom.cs | 71 ++++++++++++++++++- PluginManager/Others/Console Utilities.cs | 15 ++-- 4 files changed, 90 insertions(+), 76 deletions(-) diff --git a/DiscordBot/Program.cs b/DiscordBot/Program.cs index 289baef..92902d4 100644 --- a/DiscordBot/Program.cs +++ b/DiscordBot/Program.cs @@ -138,29 +138,8 @@ namespace DiscordBot } string path = "./Data/Plugins/" + info[0] + "s/" + name + ".dll"; - progress = new Progress(percent => - { - prg = (int)percent; - /*Console.Title = $"Downloading: {Lname} ({MathF.Round(percent, 2)}%)";*/ - }); - pbar = new Console_Utilities.ProgressBar(100, "Downloading Plugin"); - Console.WriteLine(); - t = new Task(async () => - { - while (true) - { - if (prg == 100) break; - pbar.Update(prg); - Thread.Sleep(500); - } - - }); - new Thread(t.Start).Start(); - await ServerCom.DownloadFileAsync(info[1], path, progress); - //Console.WriteLine(); - pbar.Update(100); - prg = 100; + await ServerCom.DownloadFileAsync(info[1], path); Console.WriteLine("\n"); // check requirements if any @@ -176,31 +155,9 @@ namespace DiscordBot { string[] split = line.Split(','); Console.WriteLine($"\nDownloading item: {split[1]}"); - progress = new Progress(percent => - { - prg = (int)percent; - }); - - pbar = new Console_Utilities.ProgressBar(100, "Downloading Requirements"); - bool finish = false; - t = new Task(async () => - { - while (!finish) - { - pbar.Update(prg); - //if (prg == 100) break; - Thread.Sleep(500); - } - - }); - new Thread(t.Start).Start(); - await ServerCom.DownloadFileAsync(split[0], "./" + split[1], progress); - // prg = 100; - finish = true; - pbar.Update(100); - + await ServerCom.DownloadFileAsync(split[0], "./" + split[1]); Console.WriteLine(); i++; } @@ -241,29 +198,7 @@ namespace DiscordBot string path2 = Functions.langFolder + Lname + ".lng"; - progress = new Progress(percent => - { - prg = (int)percent; - /*Console.Title = $"Downloading: {Lname} ({MathF.Round(percent, 2)}%)";*/ - }); - pbar = new Console_Utilities.ProgressBar(100, "Downloading Language"); - - t = new Task(async () => - { - while (true) - { - if (prg == 100) break; - pbar.Update(prg); - - Thread.Sleep(500); - } - - }); - new Thread(t.Start).Start(); - - await ServerCom.DownloadFileAsync(link[0], path2, progress); - pbar.Update(100); - prg = 100; + await ServerCom.DownloadFileAsync(link[0], path2); Console.WriteLine("\n"); } else Console_Utilities.WriteColorText("The language you are trying to download (&b" + Lname + "&c) is not compatible with the version of this bot. User &glistlang &ccommand in order to see all available languages for your current version !\n" + link[1]); diff --git a/PluginManager/Online/Helpers/OnlineFunctions.cs b/PluginManager/Online/Helpers/OnlineFunctions.cs index 8754f15..cf6cfab 100644 --- a/PluginManager/Online/Helpers/OnlineFunctions.cs +++ b/PluginManager/Online/Helpers/OnlineFunctions.cs @@ -18,7 +18,8 @@ namespace PluginManager.Online.Helpers /// The that is used to track the download progress /// The cancellation token /// - internal static async Task DownloadFileAsync(this HttpClient client, string url, Stream destination, IProgress progress = null, CancellationToken cancellation = default) + internal static async Task DownloadFileAsync(this HttpClient client, string url, Stream destination, + IProgress progress = null, IProgress downloadedBytes = null, CancellationToken cancellation = default) { using (var response = await client.GetAsync(url, HttpCompletionOption.ResponseHeadersRead)) { @@ -36,7 +37,11 @@ namespace PluginManager.Online.Helpers } // Convert absolute progress (bytes downloaded) into relative progress (0% - 100%) - var relativeProgress = new Progress(totalBytes => progress.Report((float)totalBytes / contentLength.Value * 100)); + var relativeProgress = new Progress(totalBytes => + { + progress.Report((float)totalBytes / contentLength.Value * 100); + downloadedBytes.Report(totalBytes); + }); // Use extension method to report progress while downloading await download.CopyToOtherStreamAsync(destination, 81920, relativeProgress, cancellation); progress.Report(1); diff --git a/PluginManager/Online/ServerCom.cs b/PluginManager/Online/ServerCom.cs index 2dbc85e..d53e833 100644 --- a/PluginManager/Online/ServerCom.cs +++ b/PluginManager/Online/ServerCom.cs @@ -33,7 +33,7 @@ namespace PluginManager.Online /// The location where to store the downloaded data /// The to track the download /// - public static async Task DownloadFileAsync(string URL, string location, IProgress progress) + public static async Task DownloadFileAsync(string URL, string location, IProgress progress, IProgress downloadedBytes) { using (var client = new System.Net.Http.HttpClient()) { @@ -41,9 +41,76 @@ namespace PluginManager.Online using (var file = new FileStream(location, FileMode.Create, FileAccess.Write, FileShare.None)) { - await client.DownloadFileAsync(URL, file, progress); + await client.DownloadFileAsync(URL, file, progress, downloadedBytes); } } } + + /// + /// Download file from url + /// + /// The url to the file + /// The location where to store the downloaded data + /// + public static async Task DownloadFileAsync(string URL, string location) + { + bool isDownloading = true; + int c_progress = 0; + + long secondsPast = 0; + long m_dwBytes = 0; + double c_downloadSpeed = 0f; + string c_downloadUnit = "MB"; + + Others.Console_Utilities.ProgressBar pbar = new Others.Console_Utilities.ProgressBar(100, ""); + + IProgress progress = new Progress(percent => + { + c_progress = (int)percent; + }); + + IProgress progress_downloaded = new Progress(downloadedBytes => + { + m_dwBytes = downloadedBytes; + }); + + Task updateProgressBarTask = new Task(async () => + { + while (isDownloading) + { + //pbar.Update(c_progress, c_downloadSpeed, c_downloadUnit); + pbar.Update(c_progress); + if (c_progress == 100) + break; + System.Threading.Thread.Sleep(500); + } + }); + + Task calculateDownloadSpeed = new Task(async () => + { + + while (isDownloading) + { + secondsPast++; + c_downloadSpeed = m_dwBytes / secondsPast; + + c_downloadSpeed /= 1024; // in KB + c_downloadSpeed /= 1024; // in MB + + c_downloadSpeed = Math.Round(c_downloadSpeed, 2, MidpointRounding.AwayFromZero); + + System.Threading.Thread.Sleep(1000); + } + }); + + // new System.Threading.Thread(calculateDownloadSpeed.Start).Start(); + new System.Threading.Thread(updateProgressBarTask.Start).Start(); + await DownloadFileAsync(URL, location, progress, progress_downloaded); + + isDownloading = false; + c_progress = 100; + pbar.Update(100); + + } } } diff --git a/PluginManager/Others/Console Utilities.cs b/PluginManager/Others/Console Utilities.cs index 3d46a35..8c0f230 100644 --- a/PluginManager/Others/Console Utilities.cs +++ b/PluginManager/Others/Console Utilities.cs @@ -24,7 +24,7 @@ namespace PluginManager.Others Message = message; } - public async void Update(int progress, bool r = false) + public async void Update(int progress, double speed = -1, string unit = null) { //progress bar @@ -53,10 +53,17 @@ namespace PluginManager.Others Console.CursorLeft = 35; Console.BackgroundColor = ConsoleColor.Black; - Console.Write(progress.ToString() + " of " + Max.ToString() + " "); + if (speed == -1 || unit == null) + { + if (progress == Max) + Console.Write(progress.ToString() + " % ✓"); + else Console.Write(progress.ToString() + " % "); + } + else + Console.Write(progress.ToString() + $"{speed} {unit}/s "); - if (r == false) - Update(progress, true); + //if (r == false) + //Update(progress, true); }