wabbajack/Wabbajack.Lib/Downloaders/DownloadDispatcher.cs

169 lines
5.7 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
2020-05-20 03:25:41 +00:00
using System.IO;
using System.Linq;
2020-05-20 03:25:41 +00:00
using System.Net.Http;
2019-12-07 03:50:50 +00:00
using System.Threading.Tasks;
using Alphaleonis.Win32.Filesystem;
2019-11-04 23:21:58 +00:00
using Wabbajack.Common;
using Wabbajack.Lib.Downloaders.UrlDownloaders;
namespace Wabbajack.Lib.Downloaders
{
public static class DownloadDispatcher
{
public static readonly List<IDownloader> Downloaders = new List<IDownloader>()
{
2019-12-14 17:30:52 +00:00
new GameFileSourceDownloader(),
new MegaDownloader(),
new DropboxDownloader(),
new GoogleDriveDownloader(),
2019-10-12 22:31:07 +00:00
new ModDBDownloader(),
new NexusDownloader(),
new MediaFireDownloader(),
2019-12-08 17:00:22 +00:00
new LoversLabDownloader(),
new VectorPlexusDownloader(),
new DeadlyStreamDownloader(),
new BethesdaNetDownloader(),
2020-01-22 09:50:09 +00:00
new TESAllianceDownloader(),
new YouTubeDownloader(),
2020-05-09 22:16:16 +00:00
new WabbajackCDNDownloader(),
new HTTPDownloader(),
2019-10-12 22:31:07 +00:00
new ManualDownloader(),
};
public static readonly List<IUrlInferencer> Inferencers = new List<IUrlInferencer>()
{
new BethesdaNetInferencer(),
2020-05-09 22:16:16 +00:00
new YoutubeInferencer(),
new WabbajackCDNInfluencer()
};
2019-10-12 22:31:07 +00:00
private static readonly Dictionary<Type, IDownloader> IndexedDownloaders;
static DownloadDispatcher()
{
2019-10-12 22:31:07 +00:00
IndexedDownloaders = Downloaders.ToDictionary(d => d.GetType());
}
2020-04-10 01:29:53 +00:00
public static async Task<AbstractDownloadState?> Infer(Uri uri)
{
foreach (var inf in Inferencers)
{
var state = await inf.Infer(uri);
if (state != null)
return state;
}
return null;
}
2019-12-08 17:00:22 +00:00
public static T GetInstance<T>() where T : IDownloader
{
2019-12-08 17:00:22 +00:00
var inst = (T)IndexedDownloaders[typeof(T)];
return inst;
}
2020-04-03 03:57:59 +00:00
public static async Task<AbstractDownloadState> ResolveArchive(dynamic ini, bool quickMode = false)
{
2020-04-03 03:57:59 +00:00
var states = await Task.WhenAll(Downloaders.Select(d => (Task<AbstractDownloadState>)d.GetDownloaderState(ini, quickMode)));
2019-12-07 03:50:50 +00:00
return states.FirstOrDefault(result => result != null);
}
/// <summary>
/// Reduced version of Resolve archive that requires less information, but only works
/// with a single URL string
/// </summary>
/// <param name="ini"></param>
/// <returns></returns>
2020-04-10 01:29:53 +00:00
public static AbstractDownloadState? ResolveArchive(string url)
{
return Downloaders.OfType<IUrlDownloader>().Select(d => d.GetDownloaderState(url)).FirstOrDefault(result => result != null);
}
2020-04-10 19:06:32 +00:00
public static async Task PrepareAll(IEnumerable<AbstractDownloadState> states)
2019-11-04 23:21:58 +00:00
{
2020-04-10 19:06:32 +00:00
await Task.WhenAll(states.Select(s => s.GetDownloader().GetType())
2019-11-04 23:21:58 +00:00
.Distinct()
2020-04-10 19:06:32 +00:00
.Select(t => Downloaders.First(d => d.GetType() == t).Prepare()));
2019-11-04 23:21:58 +00:00
}
2020-03-25 22:30:43 +00:00
public static async Task<bool> DownloadWithPossibleUpgrade(Archive archive, AbsolutePath destination)
{
var success = await Download(archive, destination);
if (success)
{
await destination.FileHashCachedAsync();
return true;
}
2020-05-20 03:25:41 +00:00
if (!(archive.State is IUpgradingState))
{
2020-05-20 03:25:41 +00:00
Utils.Log($"Download failed for {archive.Name} and no upgrade from this download source is possible");
return false;
}
2020-05-20 03:25:41 +00:00
var upgrade = (IUpgradingState)archive.State;
2020-05-20 03:25:41 +00:00
Utils.Log($"Trying to find solution to broken download for {archive.Name}");
var result = await upgrade.FindUpgrade(archive);
if (result == default)
{
2020-05-20 03:25:41 +00:00
Utils.Log(
$"No solution for broken download {archive.Name} {archive.State.PrimaryKeyString} could be found");
return false;
2020-05-20 03:25:41 +00:00
}
Utils.Log($"Looking for patch for {archive.Name}");
var patchResult = await ClientAPI.GetModUpgrade(archive, result.Archive!);
2020-05-20 03:25:41 +00:00
Utils.Log($"Downloading patch for {archive.Name}");
var tempFile = new TempFile();
using var response = await (new Common.Http.Client()).GetAsync(patchResult);
await tempFile.Path.WriteAllAsync(await response.Content.ReadAsStreamAsync());
response.Dispose();
Utils.Log($"Applying patch to {archive.Name}");
await using(var src = result.NewFile.Path.OpenShared())
await using (var final = destination.Create())
{
2020-05-20 03:25:41 +00:00
Utils.ApplyPatch(src, () => tempFile.Path.OpenShared(), final);
}
2020-05-20 03:25:41 +00:00
var hash = await destination.FileHashCachedAsync();
if (hash != archive.Hash && archive.Hash != default)
{
Utils.Log("Archive hash didn't match after patching");
return false;
}
return true;
}
2020-03-25 22:30:43 +00:00
private static async Task<bool> Download(Archive archive, AbsolutePath destination)
{
try
{
var result = await archive.State.Download(archive, destination);
if (!result) return false;
2020-03-27 03:10:23 +00:00
if (!archive.Hash.IsValid) return true;
var hash = await destination.FileHashCachedAsync();
if (hash == archive.Hash) return true;
Utils.Log($"Hashed download is incorrect");
return false;
}
catch (Exception)
{
return false;
}
}
}
}