From 90ceffdfca6d863f80adc399ed3aa6f57010ec29 Mon Sep 17 00:00:00 2001 From: "waffle.lord" Date: Thu, 21 Sep 2023 18:52:33 -0400 Subject: [PATCH] delete old files before downloaing, fix download task --- SPTInstaller/Helpers/DownloadCacheHelper.cs | 8 ++++++++ SPTInstaller/Installer Tasks/DownloadTask.cs | 9 ++++----- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/SPTInstaller/Helpers/DownloadCacheHelper.cs b/SPTInstaller/Helpers/DownloadCacheHelper.cs index d6f8e34..a11a055 100644 --- a/SPTInstaller/Helpers/DownloadCacheHelper.cs +++ b/SPTInstaller/Helpers/DownloadCacheHelper.cs @@ -69,12 +69,16 @@ public static class DownloadCacheHelper /// The url to download the file from /// A provider for progress updates /// A object of the cached file + /// If the file exists, it is deleted before downloading public static async Task DownloadFileAsync(string outputFileName, string targetLink, IProgress progress) { var outputFile = new FileInfo(Path.Join(CachePath, outputFileName)); try { + if (outputFile.Exists) + outputFile.Delete(); + // Use the provided extension method using (var file = new FileStream(outputFile.FullName, FileMode.Create, FileAccess.Write, FileShare.None)) await _httpClient.DownloadDataAsync(targetLink, file, progress); @@ -102,12 +106,16 @@ public static class DownloadCacheHelper /// The file name to save the file as /// The stream the download the file from /// A object of the cached file + /// If the file exists, it is deleted before downloading public static async Task DownloadFileAsync(string outputFileName, Stream downloadStream) { var outputFile = new FileInfo(Path.Join(CachePath, outputFileName)); try { + if (outputFile.Exists) + outputFile.Delete(); + using var patcherFileStream = outputFile.Open(FileMode.Create); { await downloadStream.CopyToAsync(patcherFileStream); diff --git a/SPTInstaller/Installer Tasks/DownloadTask.cs b/SPTInstaller/Installer Tasks/DownloadTask.cs index 011be57..374f44e 100644 --- a/SPTInstaller/Installer Tasks/DownloadTask.cs +++ b/SPTInstaller/Installer Tasks/DownloadTask.cs @@ -1,5 +1,4 @@ -using CG.Web.MegaApiClient; -using Newtonsoft.Json; +using Newtonsoft.Json; using SPTInstaller.Interfaces; using SPTInstaller.Models; using System.Collections.Generic; @@ -7,7 +6,6 @@ using System.Threading.Tasks; using SPTInstaller.Helpers; using SPTInstaller.Models.Mirrors; using SPTInstaller.Models.Mirrors.Downloaders; -using Microsoft.CodeAnalysis.CSharp.Syntax; using Serilog; namespace SPTInstaller.Installer_Tasks; @@ -39,9 +37,10 @@ public class DownloadTask : InstallerTaskBase if (mirrorsList == null) return Result.FromError("Failed to deserialize mirrors list"); - foreach (var mirror in mirrorsList) { + _expectedPatcherHash = mirror.Hash; + switch (mirror.Link) { case string l when l.StartsWith("https://mega"): @@ -58,7 +57,7 @@ public class DownloadTask : InstallerTaskBase private async Task DownloadPatcherFromMirrors(IProgress progress) { - SetStatus("Downloading Patcher", "Checking cache ...", progressStyle: ProgressStyle.Indeterminate); + SetStatus("Downloading Patcher", "Verifying cached patcher ...", progressStyle: ProgressStyle.Indeterminate); if (DownloadCacheHelper.CheckCache("patcher.zip", _expectedPatcherHash, out var cacheFile)) {