diff --git a/Wabbajack.Lib/AInstaller.cs b/Wabbajack.Lib/AInstaller.cs index 78fb4f2b..3fdb9b7f 100644 --- a/Wabbajack.Lib/AInstaller.cs +++ b/Wabbajack.Lib/AInstaller.cs @@ -8,6 +8,7 @@ using System.Threading.Tasks; using Wabbajack.Common; using Wabbajack.ImageHashing; using Wabbajack.Lib.Downloaders; +using Wabbajack.Lib.Downloaders.DTOs.ModListValidation; using Wabbajack.Lib.Validation; using Wabbajack.VirtualFileSystem; using Path = Alphaleonis.Win32.Filesystem.Path; @@ -230,15 +231,21 @@ namespace Wabbajack.Lib public async Task DownloadMissingArchives(List missing, bool download = true) { + var client = new Http.Client(); + Utils.Log("Getting upgrades list"); + var upgrades = (await client.GetJsonAsync(Consts.UpgradedFilesURL)); + if (download) { var result = SendDownloadMetrics(missing); foreach (var a in missing.Where(a => a.State.GetType() == typeof(ManualDownloader.State))) { var outputPath = DownloadFolder.Combine(a.Name); - await a.State.Download(a, outputPath); + await DownloadArchive(a, download, upgrades, outputPath); } } + + await missing.Where(a => a.State.GetType() != typeof(ManualDownloader.State)) .PMap(Queue, UpdateTracker, async archive => @@ -258,7 +265,7 @@ namespace Wabbajack.Lib } } - return await DownloadArchive(archive, download, outputPath); + return await DownloadArchive(archive, download, upgrades, outputPath); }); } @@ -271,13 +278,13 @@ namespace Wabbajack.Lib } } - public async Task DownloadArchive(Archive archive, bool download, AbsolutePath? destination = null) + public async Task DownloadArchive(Archive archive, bool download, ValidatedArchive[] validatedArchives, AbsolutePath? destination = null) { try { destination ??= DownloadFolder.Combine(archive.Name); - var result = await DownloadDispatcher.DownloadWithPossibleUpgrade(archive, destination.Value); + var result = await DownloadDispatcher.DownloadWithPossibleUpgrade(archive, destination.Value, validatedArchives); if (result == DownloadDispatcher.DownloadResult.Update) { await destination.Value.MoveToAsync(destination.Value.Parent.Combine(archive.Hash.ToHex())); diff --git a/Wabbajack.Lib/Downloaders/DownloadDispatcher.cs b/Wabbajack.Lib/Downloaders/DownloadDispatcher.cs index 3e986f68..17d8af82 100644 --- a/Wabbajack.Lib/Downloaders/DownloadDispatcher.cs +++ b/Wabbajack.Lib/Downloaders/DownloadDispatcher.cs @@ -103,19 +103,30 @@ namespace Wabbajack.Lib.Downloaders Success } - public static async Task DownloadWithPossibleUpgrade(Archive archive, AbsolutePath destination) + public static async Task DownloadWithPossibleUpgrade(Archive archive, AbsolutePath destination, ValidatedArchive[]? upgrades = null) { - if (await Download(archive, destination)) + bool ShouldTry(Archive archive) + { + return upgrades == null || upgrades.All(a => a.Original.Hash != archive.Hash); + } + + + if (ShouldTry(archive) && await Download(archive, destination)) { var downloadedHash = await destination.FileHashCachedAsync(); if (downloadedHash == archive.Hash || archive.Hash == default) return DownloadResult.Success; } - var client = new Http.Client(); + Utils.Log($"Loading for alternative to {archive.Hash}"); - var replacementMeta = (await client.GetJsonAsync(Consts.UpgradedFilesURL)) - .FirstOrDefault(a => a.Original.Hash == archive.Hash); + if (upgrades == null) + { + var client = new Http.Client(); + upgrades = (await client.GetJsonAsync(Consts.UpgradedFilesURL)); + } + + var replacementMeta = upgrades.FirstOrDefault(a => a.Original.Hash == archive.Hash); if (replacementMeta == null) { @@ -144,6 +155,7 @@ namespace Wabbajack.Lib.Downloaders await Download(replacementMeta.PatchedFrom!, newFile.Path); { + var client = new Http.Client(); using var response = await client.GetAsync(replacementMeta.PatchUrl!); await using var strm = await response.Content.ReadAsStreamAsync(); await tempFile.Path.WriteAllAsync(await response.Content.ReadAsStreamAsync());