private void CopyFile(IProgressIndicator progressReporter) { progressReporter.SetIsIndeterminate(true); progressReporter.SetRightText(Properties.Resources.Copying); // Even though there is a patch reason DoesNotExist check anyways if (File.Exists(_copyFilename)) { // Just in case things need to be reverted before it is finished File.Move(_copyFilename, _downloadFilename + ".old"); } // In case the target file is missing... if (!File.Exists(_downloadFilename)) { Log.Info(Properties.Resources.FileFilenameIsMissing, _downloadFilename); File.Move(_downloadFilename + ".old", _copyFilename); } if (!Directory.Exists(Path.GetDirectoryName(_copyFilename))) { Directory.CreateDirectory(Path.GetDirectoryName(_copyFilename)); } File.Move(_downloadFilename, _copyFilename); File.SetLastWriteTime(_copyFilename, Patch.FileDownloadInfo.LastModifiedDateTime); File.SetLastAccessTime(_copyFilename, Patch.FileDownloadInfo.LastModifiedDateTime); _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); }