private async Task DoDownloadAndInstallProcdump(CancellationToken ct = new CancellationToken()) { const string downloadLink = @"https://download.sysinternals.com/files/Procdump.zip"; var tempFileName = Path.GetTempFileName(); try { var dowloadProgress = new Progress <DownloadFileProgress>(); dowloadProgress.ProgressChanged += (o, ea) => { progressBar1.Value = Convert.ToInt32(Math.Floor(75 * ((double)ea.ProgressPercentage / 100))); }; var downloadFileAsync = new DownloadFileUtils(new Uri(downloadLink), tempFileName, dowloadProgress); await downloadFileAsync.DoDownload(ct); var extractProgress = new Progress <ZipFileProgress>(); extractProgress.ProgressChanged += (o, ea) => { progressBar1.Value = 75 + Convert.ToInt32(Math.Floor(25 * ((double)ea.ProgressPercentage / 100))); }; await ZipUtils.DoExtractZipFile(tempFileName, AppDomain.CurrentDomain.BaseDirectory, extractProgress, ct); } finally { if (File.Exists(tempFileName)) { File.Delete(tempFileName); } } // DialogResult = DialogResult.OK; Close(); }
private static async Task <bool> ScanAndRepairFile(GameFileInfo fileInfo, string gameFilePath, IProgress <ScanAndRepairFileProgress> progress, CancellationToken ct) { try { var filePath = Path.Combine(gameFilePath, fileInfo.FileName); ct.ThrowIfCancellationRequested(); //#1 File Check progress?.Report(new ScanAndRepairFileProgress(fileInfo.FileName, 1, new ExLog(LogLevel.Info, "-------------------------\r\n" + $"[{fileInfo.FileName}]\r\n" + "-------------------------\r\n" + " - Checking file..."))); if (RunFileCheck(filePath, fileInfo.Size, fileInfo.Crc32)) { goto end; } progress?.Report(new ScanAndRepairFileProgress(fileInfo.FileName, 5, new ExLog(LogLevel.Warn, " Warning: File is missing or invalid."))); ct.ThrowIfCancellationRequested(); //#6 Download File progress?.Report(new ScanAndRepairFileProgress(fileInfo.FileName, 6, new ExLog(LogLevel.Info, " - File downloading..."))); var dowloadProgress = new Progress <DownloadFileProgress>(); if (progress != null) { dowloadProgress.ProgressChanged += (o, ea) => { progress.Report(new ScanAndRepairFileProgress(fileInfo.FileName, 6 + Convert.ToInt32(Math.Floor((double)ea.ProgressPercentage / 100 * (65 - 6))), ea)); } } ; var tempFileName = Path.Combine(GetTempPath(), Path.GetRandomFileName()); var x = new DownloadFileUtils(new Uri(fileInfo.HttpLink), tempFileName, dowloadProgress); await x.DoDownload(ct); ct.ThrowIfCancellationRequested(); //#65 Check Downloaded File progress?.Report(new ScanAndRepairFileProgress(fileInfo.FileName, 65, new ExLog(LogLevel.Info, " - Checking downloaded file..."))); if (!RunFileCheck(tempFileName, fileInfo.BinSize, fileInfo.BinCrc32)) { if (File.Exists(tempFileName)) { File.Delete(tempFileName); } throw new Exception("Downloaded file is invalid!"); } ct.ThrowIfCancellationRequested(); //#70 Extract downloaded file var tmpFilePath = tempFileName; var tempFileName2 = Path.Combine(GetTempPath(), Path.GetRandomFileName()); if (ZipUtils.IsL33TZipFile(tempFileName)) { ct.ThrowIfCancellationRequested(); // progress?.Report(new ScanAndRepairFileProgress(fileInfo.FileName, 70, new ExLog(LogLevel.Info, " - Extract downloaded file..."))); var extractProgress = new Progress <ZipFileProgress>(); if (progress != null) { extractProgress.ProgressChanged += (o, ea) => { progress.Report(new ScanAndRepairFileProgress(fileInfo.FileName, 70 + Convert.ToInt32(Math.Floor((double)ea.ProgressPercentage / 100 * (90 - 70))), ea)); } } ; await ZipUtils.DoExtractL33TZipFile(tempFileName, tempFileName2, extractProgress, ct); ct.ThrowIfCancellationRequested(); //#90 Check Downloaded File if (!RunFileCheck(tempFileName2, fileInfo.Size, fileInfo.Crc32)) { if (File.Exists(tempFileName)) { File.Delete(tempFileName); } if (File.Exists(tempFileName2)) { File.Delete(tempFileName2); } throw new Exception("Extracted file is invalid!"); } tmpFilePath = tempFileName2; } else if (ZipUtils.IsL66TZipFile(tempFileName)) { ct.ThrowIfCancellationRequested(); // progress?.Report(new ScanAndRepairFileProgress(fileInfo.FileName, 70, new ExLog(LogLevel.Info, " - Extract downloaded file..."))); var extractProgress = new Progress <ZipFileProgress>(); if (progress != null) { extractProgress.ProgressChanged += (o, ea) => { progress.Report(new ScanAndRepairFileProgress(fileInfo.FileName, 70 + Convert.ToInt32(Math.Floor((double)ea.ProgressPercentage / 100 * (90 - 70))), ea)); } } ; await ZipUtils.DoExtractL66TZipFile(tempFileName, tempFileName2, extractProgress, ct); ct.ThrowIfCancellationRequested(); //#90 Check Downloaded File if (!RunFileCheck(tempFileName2, fileInfo.Size, fileInfo.Crc32)) { if (File.Exists(tempFileName)) { File.Delete(tempFileName); } if (File.Exists(tempFileName2)) { File.Delete(tempFileName2); } throw new Exception("Extracted file is invalid!"); } tmpFilePath = tempFileName2; } ct.ThrowIfCancellationRequested(); //#95 Move new file to game folder progress?.Report(new ScanAndRepairFileProgress(fileInfo.FileName, 95, new ExLog(LogLevel.Info, " - Moving new file..."))); if (File.Exists(filePath)) { File.Delete(filePath); } var pathName = Path.GetDirectoryName(filePath); if (!string.IsNullOrEmpty(pathName) && !Directory.Exists(pathName)) { Directory.CreateDirectory(pathName); } File.Move(tmpFilePath, filePath); //#99 Removing temporary file progress?.Report(new ScanAndRepairFileProgress(fileInfo.FileName, 99, new ExLog(LogLevel.Info, " - Clean-up temporary files..."))); if (File.Exists(tmpFilePath)) { File.Delete(tmpFilePath); } if (File.Exists(tempFileName)) { File.Delete(tempFileName); } if (File.Exists(tempFileName2)) { File.Delete(tempFileName2); } end: //#100 progress?.Report(new ScanAndRepairFileProgress(fileInfo.FileName, 100)); } catch (AggregateException e) { progress?.Report(new ScanAndRepairFileProgress(fileInfo.FileName, 100, new ExLog(LogLevel.Info, "-------------------------\r\n" + "!!! Error !!!\r\n" + "-------------------------\r\n" + $"{e.Message}"))); return(false); } return(true); }
private static async Task DoDownloadAndInstallUpdate(bool isSteam, IProgress <int> progress, CancellationToken ct) { Misc.CleanUpFiles(Directory.GetCurrentDirectory(), "*.old"); var gitVersion = await GetGitHubVersion().ConfigureAwait(false); progress.Report(3); if (gitVersion <= Assembly.GetExecutingAssembly().GetName().Version) { return; } ct.ThrowIfCancellationRequested(); const string zipName = "Celeste_Launcher.zip"; var downloadLink = $"{ReleaseZipUrl}{gitVersion}/{zipName}"; //Download File progress.Report(5); var dowloadProgress = new Progress <DownloadFileProgress>(); dowloadProgress.ProgressChanged += (o, ea) => { progress.Report(5 + Convert.ToInt32(Math.Floor((65 - 5) * ((double)ea.ProgressPercentage / 100)))); }; var tempFileName = Path.GetTempFileName(); var downloadFileAsync = new DownloadFileUtils(new Uri(downloadLink), tempFileName, dowloadProgress); try { await downloadFileAsync.DoDownload(ct); } catch (AggregateException) { if (File.Exists(tempFileName)) { File.Delete(tempFileName); } throw; } //Extract File progress.Report(65); var extractProgress = new Progress <ZipFileProgress>(); extractProgress.ProgressChanged += (o, ea) => { progress.Report( 65 + Convert.ToInt32(Math.Floor((90 - 65) * ((double)ea.ProgressPercentage / 100)))); }; var tempDir = Path.Combine(Path.GetTempPath(), $"Celeste_Launcher_v{gitVersion}"); if (Directory.Exists(tempDir)) { Misc.CleanUpFiles(tempDir, "*.*"); } try { await ZipUtils.DoExtractZipFile(tempFileName, tempDir, extractProgress, ct); } catch (AggregateException) { Misc.CleanUpFiles(tempDir, "*.*"); throw; } finally { if (File.Exists(tempFileName)) { File.Delete(tempFileName); } } //Move File progress.Report(90); var destinationDir = Directory.GetCurrentDirectory() + Path.DirectorySeparatorChar; try { Misc.MoveFiles(tempDir, destinationDir); } finally { Misc.CleanUpFiles(tempDir, "*.*"); } //isSteam Version if (isSteam) { progress.Report(95); Steam.ConvertToSteam(destinationDir); } //Clean Old File progress.Report(97); Misc.CleanUpFiles(Directory.GetCurrentDirectory(), "*.old"); // progress.Report(100); }