public async Task DownloadPart(string uri, string filename) { var progressReporter = _patcherContext.CreateProgressIndicator(); var fileDirectory = Path.GetDirectoryName(filename); if (!Directory.Exists(fileDirectory)) { Directory.CreateDirectory(fileDirectory); } Task.Run(async() => { await AsyncDownloader.DownloadFileWithCallbackAsync(uri, filename, (d, s) => { progressReporter.SetLeftText(Path.GetFileName(filename)); progressReporter.SetRightText(s); progressReporter.SetProgressBar(d); }, true); }).Wait(); progressReporter.SetIsIndeterminate(true); progressReporter.SetRightText(Properties.Resources.Decompressing); using (var fs = new FileStream(filename, FileMode.Open, FileAccess.ReadWrite)) using (var ms = new MemoryStream()) { fs.CopyTo(ms); var buffer = ms.ToArray(); buffer = ZlibStream.UncompressBuffer(buffer); fs.SetLength(buffer.Length); fs.Position = 0; fs.Write(buffer, 0, buffer.Length); fs.Flush(); } _patcherContext.DestroyProgressIndicator(progressReporter); }
private void Download(List <PatchFileInfo> files) { // Log the beginning of the download Log.Info(Properties.Resources.BeginningDownload); _patcherContext.UpdateMainProgress(Properties.Resources.BeginningDownload, isIndeterminate: true, isProgressbarVisible: true); // create the actions that will run in parrallel var list = new List <Action>(); var completed = 0; int failedCount = 0; foreach (var patchFileInfo in files) { var ftpAddress = OfficialPatchInfo.MainFtp; if (!ftpAddress.EndsWith("/")) { ftpAddress += "/"; } // Im kinda stuck on how I wanna do this haha //var downloader = new FileDownloader(new ProgressReporterViewModel(), patchFileInfo.RemoteName, // patchFileInfo.Filename, patchFileInfo.Size, CancellationToken.None); list.Add(() => { try { var progressIndicator = _patcherContext.CreateProgressIndicator(); var fileDirectory = Path.GetDirectoryName(Path.Combine(PatchInfo.PatchName, patchFileInfo.Filename)); if (!Directory.Exists(fileDirectory)) { Directory.CreateDirectory(fileDirectory); } Task.Run(async() => { await AsyncDownloader.DownloadFileWithCallbackAsync(string.Concat(ftpAddress, PatchInfo.EndVersion, "/", patchFileInfo.RemoteName), Path.Combine(PatchInfo.PatchName, patchFileInfo.Filename), (d, s) => { progressIndicator.SetLeftText(Path.GetFileName(patchFileInfo.Filename)); progressIndicator.SetRightText(s); progressIndicator.SetProgressBar(d); }); }).Wait(); _patcherContext.DestroyProgressIndicator(progressIndicator); } catch (Exception ex) { Log.Exception(ex); Interlocked.Increment(ref failedCount); } finally { Interlocked.Increment(ref completed); _patcherContext.UpdateMainProgress(Properties.Resources.DownloadingFiles, $"{completed}/{files.Count}", completed / (double)files.Count * 100.0, isProgressbarVisible: true); } }); } Parallel.Invoke(new ParallelOptions { MaxDegreeOfParallelism = 10 }, list.ToArray()); Log.Info(Properties.Resources.DownloadComplete); _patcherContext.UpdateMainProgress(Properties.Resources.DownloadComplete); }