Fix forced healing with manual downloading

This commit is contained in:
Timothy Baldridge
2022-02-27 14:29:28 -07:00
parent ef907ee4c0
commit 0c75490055
2 changed files with 28 additions and 9 deletions

View File

@ -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<Archive> missing, bool download = true)
{
var client = new Http.Client();
Utils.Log("Getting upgrades list");
var upgrades = (await client.GetJsonAsync<ValidatedArchive[]>(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<bool> DownloadArchive(Archive archive, bool download, AbsolutePath? destination = null)
public async Task<bool> 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()));

View File

@ -103,19 +103,30 @@ namespace Wabbajack.Lib.Downloaders
Success
}
public static async Task<DownloadResult> DownloadWithPossibleUpgrade(Archive archive, AbsolutePath destination)
public static async Task<DownloadResult> 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<ValidatedArchive[]>(Consts.UpgradedFilesURL))
.FirstOrDefault(a => a.Original.Hash == archive.Hash);
if (upgrades == null)
{
var client = new Http.Client();
upgrades = (await client.GetJsonAsync<ValidatedArchive[]>(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());