using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Wabbajack.Common; namespace Wabbajack.Lib.Downloaders { public static class DownloadDispatcher { public static readonly List Downloaders = new List() { //new GameFileSourceDownloader(), new MegaDownloader(), new DropboxDownloader(), new GoogleDriveDownloader(), new ModDBDownloader(), new NexusDownloader(), new MediaFireDownloader(), new LoversLabDownloader(), new HTTPDownloader(), new ManualDownloader(), }; private static readonly Dictionary IndexedDownloaders; static DownloadDispatcher() { IndexedDownloaders = Downloaders.ToDictionary(d => d.GetType()); } public static T GetInstance() where T : IDownloader { var inst = (T)IndexedDownloaders[typeof(T)]; inst.Prepare(); return inst; } public static async Task ResolveArchive(dynamic ini) { var states = await Task.WhenAll(Downloaders.Select(d => (Task)d.GetDownloaderState(ini))); return states.FirstOrDefault(result => result != null); } /// /// Reduced version of Resolve archive that requires less information, but only works /// with a single URL string /// /// /// public static AbstractDownloadState ResolveArchive(string url) { return Downloaders.OfType().Select(d => d.GetDownloaderState(url)).FirstOrDefault(result => result != null); } public static void PrepareAll(IEnumerable states) { states.Select(s => s.GetDownloader().GetType()) .Distinct() .Do(t => Downloaders.First(d => d.GetType() == t).Prepare()); } } }