using ModernWpf.Controls; using System; using System.Collections.Generic; using System.IO; using System.Net; using System.Threading.Tasks; namespace EZPPClient_Installer { public class DownloadUtil { public static async Task> FilesToDownload(string pathToEZPP, bool reinstall) { bool directoryExists = Directory.Exists(pathToEZPP); List downloadList = new List(); List md5Files = new List(); try { using (WebClient wc = new WebClient()) { wc.Headers.Add("user-agent", "EZPPClientInstaller"); var data = await wc.DownloadStringTaskAsync("https://new.ez-pp.farm/ezppclient?list&release=" + MainWindow.RELEASESTREAM.ToLower()); string[] dataList = data.Split('\n'); foreach (string datastring in dataList) { if (string.IsNullOrWhiteSpace(datastring) || !datastring.Contains("#")) continue; string[] se = datastring.Split('#'); string md5 = StripHTML(se[0]); string name = StripHTML(se[1]); md5Files.Add(new MD5File(name, md5)); } } } catch (WebException error) { _ = new ContentDialog() { Title = "Oops...", Content = "A error occurred while trying to fetch all needed Files.\n" + error.Message, PrimaryButtonText = "Okay" }.ShowAsync(); } if (!directoryExists || reinstall) { #if DEBUG MessageBox.Show("Folder does not exist: " + pathToEZPP); #endif foreach (MD5File md5File in md5Files) downloadList.Add(md5File.Name); } else { foreach (MD5File md5file in md5Files) { string file = pathToEZPP + @"\" + md5file.Name; bool doesExistOnPC = File.Exists(file); if (!doesExistOnPC) { downloadList.Add(md5file.Name); } else { string onlineHash = md5file.MD5; string pcHash = MD5Util.CalculateMD5(file); #if DEBUG MessageBox.Show("Filename: " + md5file.Name + "\nOnline-Hash: " + onlineHash + "\n PC-Hash: " + pcHash); #endif if (!string.Equals(onlineHash, pcHash)) { downloadList.Add(md5file.Name); } } } } #if DEBUG MessageBox.Show(downloadList.Count + "/" + md5Files.Count + " Files need for update"); #endif return downloadList; } public static async Task DownloadFiles(List files, string path) { if (!Directory.Exists(path)) Directory.CreateDirectory(path); foreach (string file in files) if (File.Exists(path + @"\" + file)) File.Delete(path + @"\" + file); foreach (string filename in files) { await DownloadFile(filename, path); } } 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) => { 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 + "&release=" + MainWindow.RELEASESTREAM.ToLower(), path + @"\" + filename); ; } } catch (WebException) { } } private static string StripHTML(string input) { return input.Replace("\r\n", "").Replace("\r", "").Replace("\n", ""); } } }