2019-10-12 21:37:16 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
2019-12-07 03:50:50 +00:00
|
|
|
|
using System.Threading.Tasks;
|
2020-02-27 13:46:34 +00:00
|
|
|
|
using Alphaleonis.Win32.Filesystem;
|
2019-11-04 23:21:58 +00:00
|
|
|
|
using Wabbajack.Common;
|
2020-02-08 23:53:11 +00:00
|
|
|
|
using Wabbajack.Lib.Downloaders.UrlDownloaders;
|
2019-10-12 21:37:16 +00:00
|
|
|
|
|
2019-10-16 03:10:34 +00:00
|
|
|
|
namespace Wabbajack.Lib.Downloaders
|
2019-10-12 21:37:16 +00:00
|
|
|
|
{
|
|
|
|
|
public static class DownloadDispatcher
|
|
|
|
|
{
|
2019-10-13 14:18:21 +00:00
|
|
|
|
public static readonly List<IDownloader> Downloaders = new List<IDownloader>()
|
2019-10-12 21:37:16 +00:00
|
|
|
|
{
|
2019-12-14 17:30:52 +00:00
|
|
|
|
new GameFileSourceDownloader(),
|
2019-10-12 21:37:16 +00:00
|
|
|
|
new MegaDownloader(),
|
|
|
|
|
new DropboxDownloader(),
|
|
|
|
|
new GoogleDriveDownloader(),
|
2019-10-12 22:31:07 +00:00
|
|
|
|
new ModDBDownloader(),
|
2019-10-12 22:15:20 +00:00
|
|
|
|
new NexusDownloader(),
|
2019-10-24 01:19:11 +00:00
|
|
|
|
new MediaFireDownloader(),
|
2019-12-08 17:00:22 +00:00
|
|
|
|
new LoversLabDownloader(),
|
2020-01-06 00:21:05 +00:00
|
|
|
|
new VectorPlexusDownloader(),
|
2020-01-06 15:08:54 +00:00
|
|
|
|
new DeadlyStreamDownloader(),
|
2020-01-29 04:17:24 +00:00
|
|
|
|
new BethesdaNetDownloader(),
|
2020-01-22 09:41:28 +00:00
|
|
|
|
new AFKModsDownloader(),
|
2020-01-22 09:50:09 +00:00
|
|
|
|
new TESAllianceDownloader(),
|
2019-11-07 00:29:53 +00:00
|
|
|
|
new HTTPDownloader(),
|
2019-10-12 22:31:07 +00:00
|
|
|
|
new ManualDownloader(),
|
2019-10-12 21:37:16 +00:00
|
|
|
|
};
|
|
|
|
|
|
2020-02-08 23:53:11 +00:00
|
|
|
|
public static readonly List<IUrlInferencer> Inferencers = new List<IUrlInferencer>()
|
|
|
|
|
{
|
|
|
|
|
new BethesdaNetInferencer()
|
|
|
|
|
};
|
|
|
|
|
|
2019-10-12 22:31:07 +00:00
|
|
|
|
private static readonly Dictionary<Type, IDownloader> IndexedDownloaders;
|
2019-10-12 21:37:16 +00:00
|
|
|
|
|
|
|
|
|
static DownloadDispatcher()
|
|
|
|
|
{
|
2019-10-12 22:31:07 +00:00
|
|
|
|
IndexedDownloaders = Downloaders.ToDictionary(d => d.GetType());
|
2019-10-12 21:37:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-02-08 23:53:11 +00:00
|
|
|
|
public static AbstractDownloadState Infer(Uri uri)
|
|
|
|
|
{
|
|
|
|
|
return Inferencers.Select(infer => infer.Infer(uri)).FirstOrDefault(result => result != null);
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-08 17:00:22 +00:00
|
|
|
|
public static T GetInstance<T>() where T : IDownloader
|
2019-10-12 21:37:16 +00:00
|
|
|
|
{
|
2019-12-08 17:00:22 +00:00
|
|
|
|
var inst = (T)IndexedDownloaders[typeof(T)];
|
|
|
|
|
inst.Prepare();
|
|
|
|
|
return inst;
|
2019-10-12 21:37:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-12-07 03:50:50 +00:00
|
|
|
|
public static async Task<AbstractDownloadState> ResolveArchive(dynamic ini)
|
2019-10-12 21:37:16 +00:00
|
|
|
|
{
|
2019-12-07 03:50:50 +00:00
|
|
|
|
var states = await Task.WhenAll(Downloaders.Select(d => (Task<AbstractDownloadState>)d.GetDownloaderState(ini)));
|
|
|
|
|
return states.FirstOrDefault(result => result != null);
|
2019-10-12 21:37:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-10-16 11:44:45 +00:00
|
|
|
|
/// <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>
|
|
|
|
|
public static AbstractDownloadState ResolveArchive(string url)
|
|
|
|
|
{
|
|
|
|
|
return Downloaders.OfType<IUrlDownloader>().Select(d => d.GetDownloaderState(url)).FirstOrDefault(result => result != null);
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-04 23:21:58 +00:00
|
|
|
|
public static void PrepareAll(IEnumerable<AbstractDownloadState> states)
|
|
|
|
|
{
|
|
|
|
|
states.Select(s => s.GetDownloader().GetType())
|
|
|
|
|
.Distinct()
|
|
|
|
|
.Do(t => Downloaders.First(d => d.GetType() == t).Prepare());
|
|
|
|
|
}
|
2020-02-27 13:46:34 +00:00
|
|
|
|
|
|
|
|
|
public static async Task<bool> DownloadWithPossibleUpgrade(Archive archive, string destination)
|
|
|
|
|
{
|
|
|
|
|
var success = await Download(archive, destination);
|
|
|
|
|
if (success)
|
|
|
|
|
{
|
|
|
|
|
await destination.FileHashCachedAsync();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Utils.Log($"Download failed, looking for upgrade");
|
|
|
|
|
var upgrade = await ClientAPI.GetModUpgrade(archive.Hash);
|
|
|
|
|
if (upgrade == null)
|
|
|
|
|
{
|
|
|
|
|
Utils.Log($"No upgrade found for {archive.Hash}");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Utils.Log($"Upgrading {archive.Hash}");
|
|
|
|
|
var upgradePath = Path.Combine(Path.GetDirectoryName(destination), "_Upgrade_" + archive.Name);
|
|
|
|
|
var upgradeResult = await Download(upgrade, upgradePath);
|
|
|
|
|
if (!upgradeResult) return false;
|
|
|
|
|
|
|
|
|
|
var patchName = $"{archive.Hash.FromBase64().ToHex()}_{upgrade.Hash.FromBase64().ToHex()}";
|
|
|
|
|
var patchPath = Path.Combine(Path.GetDirectoryName(destination), "_Patch_" + patchName);
|
|
|
|
|
|
|
|
|
|
var patchState = new Archive
|
|
|
|
|
{
|
|
|
|
|
Name = patchName,
|
|
|
|
|
State = new HTTPDownloader.State
|
|
|
|
|
{
|
|
|
|
|
Url = $"https://wabbajackcdn.b-cdn.net/updates/{patchName}"
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var patchResult = await Download(patchState, patchPath);
|
|
|
|
|
if (!patchResult) return false;
|
|
|
|
|
|
|
|
|
|
Utils.Status($"Applying Upgrade to {archive.Hash}");
|
|
|
|
|
await using (var patchStream = File.OpenRead(patchPath))
|
|
|
|
|
await using (var srcStream = File.OpenRead(upgradePath))
|
|
|
|
|
await using (var destStream = File.Create(destination))
|
|
|
|
|
{
|
|
|
|
|
OctoDiff.Apply(srcStream, patchStream, destStream);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await destination.FileHashCachedAsync();
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static async Task<bool> Download(Archive archive, string destination)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var result = await archive.State.Download(archive, destination);
|
|
|
|
|
if (!result) return false;
|
|
|
|
|
|
|
|
|
|
if (archive.Hash == null) return true;
|
|
|
|
|
var hash = await destination.FileHashCachedAsync();
|
|
|
|
|
if (hash == archive.Hash) return true;
|
|
|
|
|
|
|
|
|
|
Utils.Log($"Hashed download is incorrect");
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-10-12 21:37:16 +00:00
|
|
|
|
}
|
|
|
|
|
}
|