From 57fdc42ea4dfbaa9812e34c8e1099366028728ef Mon Sep 17 00:00:00 2001 From: Timothy Baldridge Date: Sun, 27 Feb 2022 13:28:04 -0700 Subject: [PATCH] Fix manual downloading for LoversLab files --- Wabbajack.Lib/Downloaders/ManualDownloader.cs | 6 +++ .../ManuallyDownloadLoversLabFile.cs | 40 +++++++++++++++ .../View Models/UserInterventionHandlers.cs | 50 ++++++++++++++++--- 3 files changed, 90 insertions(+), 6 deletions(-) create mode 100644 Wabbajack.Lib/StatusMessages/ManuallyDownloadLoversLabFile.cs diff --git a/Wabbajack.Lib/Downloaders/ManualDownloader.cs b/Wabbajack.Lib/Downloaders/ManualDownloader.cs index 6ff497b9..95d647f2 100644 --- a/Wabbajack.Lib/Downloaders/ManualDownloader.cs +++ b/Wabbajack.Lib/Downloaders/ManualDownloader.cs @@ -63,6 +63,12 @@ namespace Wabbajack.Lib.Downloaders await Utils.Log(await ManuallyDownloadMegaFile.Create(this, destination)).Task; return true; } + + if ((new Uri(Url)).Host.EndsWith("loverslab.com")) + { + await Utils.Log(await ManuallyDownloadLoversLabFile.Create(this, destination)).Task; + return true; + } else { var (uri, client) = await Utils.Log(await ManuallyDownloadFile.Create(this)).Task; diff --git a/Wabbajack.Lib/StatusMessages/ManuallyDownloadLoversLabFile.cs b/Wabbajack.Lib/StatusMessages/ManuallyDownloadLoversLabFile.cs new file mode 100644 index 00000000..06a5c28b --- /dev/null +++ b/Wabbajack.Lib/StatusMessages/ManuallyDownloadLoversLabFile.cs @@ -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 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(); + } + } +} diff --git a/Wabbajack/View Models/UserInterventionHandlers.cs b/Wabbajack/View Models/UserInterventionHandlers.cs index bf42be88..16f223f9 100644 --- a/Wabbajack/View Models/UserInterventionHandlers.cs +++ b/Wabbajack/View Models/UserInterventionHandlers.cs @@ -83,6 +83,9 @@ namespace Wabbajack case ManuallyDownloadMegaFile c: await WrapBrowserJob(c, (vm, cancel) => HandleManualMegaDownload(vm, cancel, c)); break; + case ManuallyDownloadLoversLabFile c: + await WrapBrowserJob(c, (vm, cancel) => HandleManualLoversLabDownload(vm, cancel, c)); + break; case AbstractNeedsLoginDownloader.RequestSiteLogin c: await WrapBrowserJob(c, async (vm, cancel) => { @@ -140,7 +143,7 @@ namespace Wabbajack if (parsed.Contains("code")) { Helpers.SchemeHandler = oldHandler; - oa.Resume(parsed.Get("code")); + oa.Resume(parsed.Get("code")!).FireAndForget(); } else { @@ -226,7 +229,38 @@ namespace Wabbajack await vm.Driver.WaitForInitialized(); 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)); @@ -240,23 +274,27 @@ namespace Wabbajack private class BlobDownloadHandler : IDownloadHandler { - private readonly ManuallyDownloadMegaFile _manualFile; + private readonly AbsolutePath _destination; private readonly TaskCompletionSource _tcs; + private readonly Action _progress; - public BlobDownloadHandler(ManuallyDownloadMegaFile f, TaskCompletionSource tcs) + public BlobDownloadHandler(AbsolutePath f, TaskCompletionSource tcs, Action progress = null) { - _manualFile = f; + _progress = progress; + _destination = f; _tcs = tcs; } public void OnBeforeDownload(IWebBrowser chromiumWebBrowser, IBrowser browser, DownloadItem downloadItem, IBeforeDownloadCallback callback) { - callback.Continue(_manualFile.Destination.ToString(), false); + callback.Continue(_destination.ToString(), false); } public void OnDownloadUpdated(IWebBrowser chromiumWebBrowser, IBrowser browser, DownloadItem downloadItem, IDownloadItemCallback callback) { + _progress?.Invoke(Percent.FactoryPutInRange(downloadItem.PercentComplete, 100)); + if (downloadItem.IsComplete) { _tcs.TrySetResult();