2019-12-29 22:57:01 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading.Tasks;
|
2020-04-06 20:48:54 +00:00
|
|
|
|
using Newtonsoft.Json;
|
2020-03-25 22:30:43 +00:00
|
|
|
|
using Wabbajack.Common;
|
2019-10-16 03:10:34 +00:00
|
|
|
|
using Wabbajack.Lib.Validation;
|
2019-10-12 18:03:45 +00:00
|
|
|
|
|
2019-10-16 03:10:34 +00:00
|
|
|
|
namespace Wabbajack.Lib.Downloaders
|
2019-10-12 18:03:45 +00:00
|
|
|
|
{
|
2020-03-04 12:10:49 +00:00
|
|
|
|
public interface IMetaState
|
|
|
|
|
{
|
2020-03-22 21:55:31 +00:00
|
|
|
|
Uri URL { get; }
|
2020-04-09 20:20:34 +00:00
|
|
|
|
string? Name { get; set; }
|
|
|
|
|
string? Author { get; set; }
|
|
|
|
|
string? Version { get; set; }
|
2020-04-15 12:05:05 +00:00
|
|
|
|
Uri? ImageURL { get; set; }
|
2020-03-04 12:10:49 +00:00
|
|
|
|
bool IsNSFW { get; set; }
|
2020-04-09 20:20:34 +00:00
|
|
|
|
string? Description { get; set; }
|
2020-03-04 12:10:49 +00:00
|
|
|
|
|
|
|
|
|
Task<bool> LoadMetaData();
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-13 22:10:05 +00:00
|
|
|
|
public abstract class AbstractDownloadState : IUpgradingState
|
2019-10-12 18:03:45 +00:00
|
|
|
|
{
|
2020-03-04 12:10:49 +00:00
|
|
|
|
public static List<Type> KnownSubTypes = new List<Type>
|
2019-12-29 22:57:01 +00:00
|
|
|
|
{
|
|
|
|
|
typeof(HTTPDownloader.State),
|
|
|
|
|
typeof(GameFileSourceDownloader.State),
|
|
|
|
|
typeof(GoogleDriveDownloader.State),
|
|
|
|
|
typeof(LoversLabDownloader.State),
|
|
|
|
|
typeof(ManualDownloader.State),
|
|
|
|
|
typeof(MediaFireDownloader.State),
|
|
|
|
|
typeof(MegaDownloader.State),
|
|
|
|
|
typeof(ModDBDownloader.State),
|
|
|
|
|
typeof(NexusDownloader.State),
|
2020-01-06 15:08:54 +00:00
|
|
|
|
typeof(SteamWorkshopDownloader.State),
|
|
|
|
|
typeof(VectorPlexusDownloader.State),
|
2020-01-22 09:41:28 +00:00
|
|
|
|
typeof(DeadlyStreamDownloader.State),
|
2020-01-29 04:17:24 +00:00
|
|
|
|
typeof(TESAllianceDownloader.State),
|
2020-05-28 23:44:58 +00:00
|
|
|
|
typeof(TESAllDownloader.State),
|
2020-05-28 22:31:01 +00:00
|
|
|
|
typeof(YandexDownloader.State),
|
2020-05-10 15:29:52 +00:00
|
|
|
|
typeof(WabbajackCDNDownloader.State)
|
2019-12-29 22:57:01 +00:00
|
|
|
|
};
|
|
|
|
|
public static Dictionary<string, Type> NameToType { get; set; }
|
|
|
|
|
public static Dictionary<Type, string> TypeToName { get; set; }
|
|
|
|
|
|
|
|
|
|
static AbstractDownloadState()
|
|
|
|
|
{
|
2020-04-09 20:20:34 +00:00
|
|
|
|
NameToType = KnownSubTypes.ToDictionary(t => t.FullName!.Substring(t.Namespace!.Length + 1), t => t);
|
2019-12-29 22:57:01 +00:00
|
|
|
|
TypeToName = NameToType.ToDictionary(k => k.Value, k => k.Key);
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-06 20:48:54 +00:00
|
|
|
|
[JsonIgnore]
|
2020-01-01 16:19:06 +00:00
|
|
|
|
public abstract object[] PrimaryKey { get; }
|
2020-02-02 12:15:29 +00:00
|
|
|
|
|
2020-01-10 13:25:01 +00:00
|
|
|
|
public string PrimaryKeyString
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
var pk = new List<object>();
|
|
|
|
|
pk.Add(AbstractDownloadState.TypeToName[GetType()]);
|
|
|
|
|
pk.AddRange(PrimaryKey);
|
|
|
|
|
var pk_str = string.Join("|",pk.Select(p => p.ToString()));
|
|
|
|
|
return pk_str;
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-12-29 22:57:01 +00:00
|
|
|
|
|
2019-10-12 18:03:45 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Returns true if this file is allowed to be downloaded via whitelist
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="whitelist"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public abstract bool IsWhitelisted(ServerWhitelist whitelist);
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Downloads this file to the given destination location
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="destination"></param>
|
2020-03-25 22:30:43 +00:00
|
|
|
|
public abstract Task<bool> Download(Archive a, AbsolutePath destination);
|
2019-10-12 18:03:45 +00:00
|
|
|
|
|
2020-03-25 22:30:43 +00:00
|
|
|
|
public async Task<bool> Download(AbsolutePath destination)
|
2019-10-29 21:30:27 +00:00
|
|
|
|
{
|
2020-03-25 22:30:43 +00:00
|
|
|
|
destination.Parent.CreateDirectory();
|
2020-04-15 12:05:05 +00:00
|
|
|
|
return await Download(new Archive(this) {Name = (string)destination.FileName}, destination);
|
2019-10-29 21:30:27 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-10-12 18:03:45 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Returns true if this link is still valid
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns></returns>
|
2020-01-13 22:55:55 +00:00
|
|
|
|
public abstract Task<bool> Verify(Archive archive);
|
2019-10-12 22:15:20 +00:00
|
|
|
|
|
|
|
|
|
public abstract IDownloader GetDownloader();
|
2019-10-12 22:54:25 +00:00
|
|
|
|
|
2020-04-09 20:20:34 +00:00
|
|
|
|
public abstract string? GetManifestURL(Archive a);
|
2020-01-11 04:15:53 +00:00
|
|
|
|
public abstract string[] GetMetaIni();
|
2020-05-02 21:09:29 +00:00
|
|
|
|
|
|
|
|
|
public string GetMetaIniString()
|
|
|
|
|
{
|
2020-09-09 22:50:43 +00:00
|
|
|
|
return string.Join("\n", GetMetaIni());
|
2020-05-02 21:09:29 +00:00
|
|
|
|
}
|
2020-07-13 22:10:05 +00:00
|
|
|
|
|
|
|
|
|
public async Task<(Archive? Archive, TempFile NewFile)> ServerFindUpgrade(Archive a)
|
|
|
|
|
{
|
|
|
|
|
var alternatives = await ClientAPI.GetModUpgrades(a.Hash);
|
|
|
|
|
if (alternatives == default)
|
|
|
|
|
return default;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
await DownloadDispatcher.PrepareAll(alternatives.Select(r => r.State));
|
|
|
|
|
Archive? selected = null;
|
|
|
|
|
foreach (var result in alternatives)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if (!await result.State.Verify(result)) continue;
|
|
|
|
|
|
|
|
|
|
selected = result;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
Utils.Log($"Verification error for failed for possible upgrade {result.State.PrimaryKeyString}");
|
|
|
|
|
Utils.Log(ex.ToString());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (selected == null) return default;
|
|
|
|
|
|
|
|
|
|
var tmpFile = new TempFile();
|
|
|
|
|
if (await selected.State.Download(selected, tmpFile.Path))
|
|
|
|
|
{
|
|
|
|
|
return (selected, tmpFile);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await tmpFile.DisposeAsync();
|
|
|
|
|
return default;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-12 22:23:02 +00:00
|
|
|
|
public virtual async Task<(Archive? Archive, TempFile NewFile)> FindUpgrade(Archive a, Func<Archive, Task<AbsolutePath>> downloadResolver)
|
2020-07-13 22:10:05 +00:00
|
|
|
|
{
|
|
|
|
|
return await ServerFindUpgrade(a);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public virtual async Task<bool> ServerValidateUpgrade(Hash srcHash, AbstractDownloadState newArchiveState)
|
|
|
|
|
{
|
|
|
|
|
var alternatives = await ClientAPI.GetModUpgrades(srcHash);
|
|
|
|
|
return alternatives?.Any(a => a.State.PrimaryKeyString == newArchiveState.PrimaryKeyString) ?? default;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public virtual async Task<bool> ValidateUpgrade(Hash srcHash, AbstractDownloadState newArchiveState)
|
|
|
|
|
{
|
|
|
|
|
return await ServerValidateUpgrade(srcHash, newArchiveState);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2019-10-12 18:03:45 +00:00
|
|
|
|
}
|
|
|
|
|
}
|