From 6dc54f175e1f5db705403a6845fbf09af21912ec Mon Sep 17 00:00:00 2001 From: HorizonCode Date: Wed, 26 May 2021 08:09:30 +0200 Subject: [PATCH] Download progress is now shown in MBit/s --- EZPPClient Installer/DownloadUtil.cs | 50 +++++++++++++++++++++++++--- 1 file changed, 45 insertions(+), 5 deletions(-) diff --git a/EZPPClient Installer/DownloadUtil.cs b/EZPPClient Installer/DownloadUtil.cs index 0fe8491..9630b8c 100644 --- a/EZPPClient Installer/DownloadUtil.cs +++ b/EZPPClient Installer/DownloadUtil.cs @@ -12,7 +12,7 @@ namespace EZPPClient_Installer public class DownloadUtil { - public static List FilesToDownload(string pathToEZPP) + public static List FilesToDownload(string pathToEZPP, bool reinstall) { bool directoryExists = Directory.Exists(pathToEZPP); @@ -49,7 +49,7 @@ namespace EZPPClient_Installer }.ShowAsync(); } - if (!directoryExists) + if (!directoryExists || reinstall) { #if DEBUG MessageBox.Show("Folder does not exist: " + pathToEZPP); @@ -93,7 +93,7 @@ namespace EZPPClient_Installer if (!Directory.Exists(path)) Directory.CreateDirectory(path); - foreach(string file in files) + foreach (string file in files) if (File.Exists(path + @"\" + file)) File.Delete(path + @"\" + file); @@ -104,17 +104,57 @@ namespace EZPPClient_Installer } } + + private static async Task DownloadFile(string filename, string path) { + + long last = -1; + long lastTransferRate = -1; + + List byteTransfer = new List(); + try { using (WebClient wc = new WebClient()) { wc.Headers.Add("user-agent", "EZPPClientInstaller"); - wc.DownloadProgressChanged += (sender, value) => MainWindow.updateProgress(filename + " - " + value.ProgressPercentage + "% - " + Math.Round((value.BytesReceived / 1024000D), 2) + "MB/" + Math.Round((value.TotalBytesToReceive / 1024000D), 2) + "MB", value.ProgressPercentage); + wc.DownloadProgressChanged += (sender, value) => + { + + var bytesReceived = value.BytesReceived; + var totalBytes = value.TotalBytesToReceive; + var currentMillis = DateTimeOffset.Now.ToUnixTimeMilliseconds(); + var diffMillis = Math.Abs(currentMillis - last); + if (diffMillis >= 1000 || last == -1) + { + if (lastTransferRate != -1) + { + long transfer = Math.Abs(lastTransferRate - bytesReceived); + byteTransfer.Add(transfer); + if (byteTransfer.Count > 5) + byteTransfer.RemoveAt(0); + } + lastTransferRate = bytesReceived; + last = DateTimeOffset.Now.ToUnixTimeMilliseconds(); + } + + long averageTransferRate = 0; + if (byteTransfer.Count > 0) + { + foreach (long tr in byteTransfer) + { + averageTransferRate += tr; + } + averageTransferRate /= byteTransfer.Count; + } + + MainWindow.updateProgress(filename + " - " + value.ProgressPercentage + "% - " + Math.Round((averageTransferRate / 1024000D) * 1.8, 2) + "MBit/s", value.ProgressPercentage); + }; await wc.DownloadFileTaskAsync(@"https://new.ez-pp.farm/ezppclient?file=" + filename, path + @"\" + filename); } - }catch(WebException) + } + catch (WebException) { } }