wabbajack/Wabbajack.Lib/Downloaders/ManualDownloader.cs

106 lines
3.2 KiB
C#
Raw Normal View History

using System;
using System.IO;
using System.Linq;
using System.Reactive.Subjects;
using System.Threading;
using System.Threading.Tasks;
using Newtonsoft.Json;
2019-10-12 22:15:32 +00:00
using Wabbajack.Common;
2020-01-07 13:50:11 +00:00
using Wabbajack.Common.IO;
using Wabbajack.Common.Serialization.Json;
using Wabbajack.Lib.Validation;
2019-10-12 22:15:32 +00:00
namespace Wabbajack.Lib.Downloaders
2019-10-12 22:15:32 +00:00
{
public class ManualDownloader : IDownloader
2019-10-12 22:15:32 +00:00
{
class FileEvent
{
public string FullPath { get; set; } = string.Empty;
public string Name { get; set; } = string.Empty;
public long Size { get; set; }
}
public ManualDownloader()
{
}
public async Task<AbstractDownloadState?> GetDownloaderState(dynamic archiveINI, bool quickMode)
2019-10-12 22:15:32 +00:00
{
2019-11-21 15:51:57 +00:00
var url = archiveINI?.General?.manualURL;
2022-02-23 23:11:25 +00:00
var prompt = archiveINI?.General?.prompt;
return url != null ? new State(url, prompt ?? "") : null;
2019-10-12 22:15:32 +00:00
}
2019-12-07 02:45:13 +00:00
public async Task Prepare()
2019-10-12 22:15:32 +00:00
{
}
[JsonName("ManualDownloader")]
2019-10-12 22:15:32 +00:00
public class State : AbstractDownloadState
{
public string Url { get; }
2022-02-23 23:11:25 +00:00
public string Prompt { get; }
[JsonIgnore]
public override object[] PrimaryKey => new object[] { Url };
2022-02-23 23:11:25 +00:00
public State(string url, string prompt)
{
Url = url;
2022-02-23 23:11:25 +00:00
Prompt = prompt;
}
2019-10-12 22:15:32 +00:00
public override bool IsWhitelisted(ServerWhitelist whitelist)
{
return Url == "<TESTING>" || whitelist.AllowedPrefixes.Any(p => Url.StartsWith(p));
2019-10-12 22:15:32 +00:00
}
2020-03-25 22:30:43 +00:00
public override async Task<bool> Download(Archive a, AbsolutePath destination)
2019-10-12 22:15:32 +00:00
{
if ((new Uri(Url)).Host == "mega.nz")
{
await Utils.Log(await ManuallyDownloadMegaFile.Create(this, destination)).Task;
return true;
}
else if ((new Uri(Url)).Host.EndsWith("loverslab.com"))
{
await Utils.Log(await ManuallyDownloadLoversLabFile.Create(this, destination, a)).Task;
return true;
}
else
{
var (uri, client) = await Utils.Log(await ManuallyDownloadFile.Create(this)).Task;
var state = new HTTPDownloader.State(uri.ToString()) {Client = client};
return await state.Download(a, destination);
}
2019-10-12 22:15:32 +00:00
}
public override async Task<bool> Verify(Archive a, CancellationToken? token)
2019-10-12 22:15:32 +00:00
{
return true;
}
public override IDownloader GetDownloader()
{
return DownloadDispatcher.GetInstance<ManualDownloader>();
}
public override string GetManifestURL(Archive a)
{
return Url;
}
public override string[] GetMetaIni()
{
return new []
{
"[General]",
$"manualURL={Url}",
2022-02-23 23:11:25 +00:00
$"prompt={Prompt}"
};
}
2019-10-12 22:15:32 +00:00
}
}
}