Fix manual downloading for LoversLab files

This commit is contained in:
Timothy Baldridge 2022-02-27 13:28:04 -07:00
parent bd5ac82436
commit 57fdc42ea4
3 changed files with 90 additions and 6 deletions

View File

@ -63,6 +63,12 @@ namespace Wabbajack.Lib.Downloaders
await Utils.Log(await ManuallyDownloadMegaFile.Create(this, destination)).Task; await Utils.Log(await ManuallyDownloadMegaFile.Create(this, destination)).Task;
return true; return true;
} }
if ((new Uri(Url)).Host.EndsWith("loverslab.com"))
{
await Utils.Log(await ManuallyDownloadLoversLabFile.Create(this, destination)).Task;
return true;
}
else else
{ {
var (uri, client) = await Utils.Log(await ManuallyDownloadFile.Create(this)).Task; var (uri, client) = await Utils.Log(await ManuallyDownloadFile.Create(this)).Task;

View File

@ -0,0 +1,40 @@
using System.Threading.Tasks;
using Wabbajack.Common;
using Wabbajack.Lib.Downloaders;
namespace Wabbajack.Lib
{
public class ManuallyDownloadLoversLabFile : AUserIntervention
{
public ManualDownloader.State State { get; }
public override string ShortDescription { get; } = string.Empty;
public override string ExtendedDescription { get; } = string.Empty;
private TaskCompletionSource _tcs = new();
public Task Task => _tcs.Task;
public AbsolutePath Destination { get; }
private ManuallyDownloadLoversLabFile(ManualDownloader.State state, AbsolutePath destination)
{
State = state;
Destination = destination;
}
public static async Task<ManuallyDownloadLoversLabFile> Create(ManualDownloader.State state, AbsolutePath destination)
{
var result = new ManuallyDownloadLoversLabFile(state, destination);
return result;
}
public override void Cancel()
{
_tcs.SetCanceled();
}
public void Resume()
{
_tcs.SetResult();
}
}
}

View File

@ -83,6 +83,9 @@ namespace Wabbajack
case ManuallyDownloadMegaFile c: case ManuallyDownloadMegaFile c:
await WrapBrowserJob(c, (vm, cancel) => HandleManualMegaDownload(vm, cancel, c)); await WrapBrowserJob(c, (vm, cancel) => HandleManualMegaDownload(vm, cancel, c));
break; break;
case ManuallyDownloadLoversLabFile c:
await WrapBrowserJob(c, (vm, cancel) => HandleManualLoversLabDownload(vm, cancel, c));
break;
case AbstractNeedsLoginDownloader.RequestSiteLogin c: case AbstractNeedsLoginDownloader.RequestSiteLogin c:
await WrapBrowserJob(c, async (vm, cancel) => await WrapBrowserJob(c, async (vm, cancel) =>
{ {
@ -140,7 +143,7 @@ namespace Wabbajack
if (parsed.Contains("code")) if (parsed.Contains("code"))
{ {
Helpers.SchemeHandler = oldHandler; Helpers.SchemeHandler = oldHandler;
oa.Resume(parsed.Get("code")); oa.Resume(parsed.Get("code")!).FireAndForget();
} }
else else
{ {
@ -226,7 +229,38 @@ namespace Wabbajack
await vm.Driver.WaitForInitialized(); await vm.Driver.WaitForInitialized();
var tcs = new TaskCompletionSource(); var tcs = new TaskCompletionSource();
using var _ = browser.SetDownloadHandler(new BlobDownloadHandler(manuallyDownloadFile, tcs)); using var _ = browser.SetDownloadHandler(new BlobDownloadHandler(manuallyDownloadFile.Destination, tcs));
await browser.NavigateTo(new Uri(manuallyDownloadFile.State.Url));
while (!cancel.IsCancellationRequested && !tcs.Task.IsCompleted)
{
await Task.Delay(100);
}
manuallyDownloadFile.Resume();
}
private async Task HandleManualLoversLabDownload(WebBrowserVM vm, CancellationTokenSource cancel, ManuallyDownloadLoversLabFile manuallyDownloadFile)
{
var browser = new CefSharpWrapper(vm.Browser);
var prompt = manuallyDownloadFile.State.Prompt;
if (string.IsNullOrWhiteSpace(prompt))
{
prompt = $"Please locate and download {manuallyDownloadFile.State.Url}";
}
vm.Instructions = prompt;
await vm.Driver.WaitForInitialized();
var tcs = new TaskCompletionSource();
using var _ = browser.SetDownloadHandler(new BlobDownloadHandler(manuallyDownloadFile.Destination, tcs,
p =>
{
vm.Instructions = $"Downloading: {p}";
}));
await browser.NavigateTo(new Uri(manuallyDownloadFile.State.Url)); await browser.NavigateTo(new Uri(manuallyDownloadFile.State.Url));
@ -240,23 +274,27 @@ namespace Wabbajack
private class BlobDownloadHandler : IDownloadHandler private class BlobDownloadHandler : IDownloadHandler
{ {
private readonly ManuallyDownloadMegaFile _manualFile; private readonly AbsolutePath _destination;
private readonly TaskCompletionSource _tcs; private readonly TaskCompletionSource _tcs;
private readonly Action<Percent> _progress;
public BlobDownloadHandler(ManuallyDownloadMegaFile f, TaskCompletionSource tcs) public BlobDownloadHandler(AbsolutePath f, TaskCompletionSource tcs, Action<Percent> progress = null)
{ {
_manualFile = f; _progress = progress;
_destination = f;
_tcs = tcs; _tcs = tcs;
} }
public void OnBeforeDownload(IWebBrowser chromiumWebBrowser, IBrowser browser, DownloadItem downloadItem, public void OnBeforeDownload(IWebBrowser chromiumWebBrowser, IBrowser browser, DownloadItem downloadItem,
IBeforeDownloadCallback callback) IBeforeDownloadCallback callback)
{ {
callback.Continue(_manualFile.Destination.ToString(), false); callback.Continue(_destination.ToString(), false);
} }
public void OnDownloadUpdated(IWebBrowser chromiumWebBrowser, IBrowser browser, DownloadItem downloadItem, public void OnDownloadUpdated(IWebBrowser chromiumWebBrowser, IBrowser browser, DownloadItem downloadItem,
IDownloadItemCallback callback) IDownloadItemCallback callback)
{ {
_progress?.Invoke(Percent.FactoryPutInRange(downloadItem.PercentComplete, 100));
if (downloadItem.IsComplete) if (downloadItem.IsComplete)
{ {
_tcs.TrySetResult(); _tcs.TrySetResult();