wabbajack/Wabbajack.Lib/Downloaders/DownloadDispatcher.cs

65 lines
2.2 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.Linq;
2019-12-07 03:50:50 +00:00
using System.Threading.Tasks;
2019-11-04 23:21:58 +00:00
using Wabbajack.Common;
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 HTTPDownloader(),
2019-10-12 22:31:07 +00:00
new ManualDownloader(),
};
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());
}
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)];
inst.Prepare();
return inst;
}
2019-12-07 03:50:50 +00:00
public static async Task<AbstractDownloadState> ResolveArchive(dynamic ini)
{
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);
}
/// <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());
}
}
}