2019-12-08 17:00:22 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
2019-12-08 19:46:30 +00:00
|
|
|
|
using System.Threading;
|
2019-12-08 17:00:22 +00:00
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using System.Windows;
|
|
|
|
|
using System.Windows.Threading;
|
|
|
|
|
using ReactiveUI;
|
2019-12-08 19:46:30 +00:00
|
|
|
|
using Wabbajack.Common;
|
2020-01-07 13:03:46 +00:00
|
|
|
|
using Wabbajack.Lib;
|
2019-12-08 17:00:22 +00:00
|
|
|
|
using Wabbajack.Lib.Downloaders;
|
|
|
|
|
using Wabbajack.Lib.NexusApi;
|
2019-12-26 23:26:53 +00:00
|
|
|
|
using Wabbajack.Lib.WebAutomation;
|
2019-12-08 17:00:22 +00:00
|
|
|
|
|
|
|
|
|
namespace Wabbajack
|
|
|
|
|
{
|
|
|
|
|
public class UserInterventionHandlers
|
|
|
|
|
{
|
2019-12-08 19:46:30 +00:00
|
|
|
|
public MainWindowVM MainWindow { get; }
|
2019-12-08 17:00:22 +00:00
|
|
|
|
|
2019-12-08 19:46:30 +00:00
|
|
|
|
public UserInterventionHandlers(MainWindowVM mvm)
|
|
|
|
|
{
|
|
|
|
|
MainWindow = mvm;
|
2019-12-08 17:00:22 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-12-08 19:46:30 +00:00
|
|
|
|
private async Task WrapBrowserJob(IUserIntervention intervention, Func<WebBrowserVM, CancellationTokenSource, Task> toDo)
|
2019-12-08 17:00:22 +00:00
|
|
|
|
{
|
2019-12-08 19:46:30 +00:00
|
|
|
|
CancellationTokenSource cancel = new CancellationTokenSource();
|
|
|
|
|
var oldPane = MainWindow.ActivePane;
|
2019-12-19 01:09:45 +00:00
|
|
|
|
var vm = await WebBrowserVM.GetNew();
|
2020-01-05 02:50:05 +00:00
|
|
|
|
MainWindow.NavigateTo(vm);
|
2019-12-08 19:46:30 +00:00
|
|
|
|
vm.BackCommand = ReactiveCommand.Create(() =>
|
2019-12-08 17:00:22 +00:00
|
|
|
|
{
|
2019-12-08 19:46:30 +00:00
|
|
|
|
cancel.Cancel();
|
2020-01-05 02:50:05 +00:00
|
|
|
|
MainWindow.NavigateTo(oldPane);
|
2019-12-08 19:46:30 +00:00
|
|
|
|
intervention.Cancel();
|
2019-12-08 17:00:22 +00:00
|
|
|
|
});
|
2019-12-08 19:46:30 +00:00
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
await toDo(vm, cancel);
|
|
|
|
|
}
|
|
|
|
|
catch (TaskCanceledException)
|
|
|
|
|
{
|
|
|
|
|
intervention.Cancel();
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
Utils.Error(ex);
|
|
|
|
|
intervention.Cancel();
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-05 02:50:05 +00:00
|
|
|
|
MainWindow.NavigateTo(oldPane);
|
2019-12-08 17:00:22 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-12-08 19:46:30 +00:00
|
|
|
|
public async Task Handle(IUserIntervention msg)
|
2019-12-08 17:00:22 +00:00
|
|
|
|
{
|
|
|
|
|
switch (msg)
|
|
|
|
|
{
|
|
|
|
|
case RequestNexusAuthorization c:
|
2019-12-08 19:46:30 +00:00
|
|
|
|
await WrapBrowserJob(msg, async (vm, cancel) =>
|
|
|
|
|
{
|
2019-12-26 23:26:53 +00:00
|
|
|
|
await vm.Driver.WaitForInitialized();
|
|
|
|
|
var key = await NexusApiClient.SetupNexusLogin(new CefSharpWrapper(vm.Browser), m => vm.Instructions = m, cancel.Token);
|
2019-12-08 19:46:30 +00:00
|
|
|
|
c.Resume(key);
|
|
|
|
|
});
|
2019-12-08 17:00:22 +00:00
|
|
|
|
break;
|
2020-01-05 05:38:08 +00:00
|
|
|
|
case AbstractNeedsLoginDownloader.RequestSiteLogin c:
|
2019-12-08 19:46:30 +00:00
|
|
|
|
await WrapBrowserJob(msg, async (vm, cancel) =>
|
|
|
|
|
{
|
2019-12-26 23:26:53 +00:00
|
|
|
|
await vm.Driver.WaitForInitialized();
|
2020-01-05 05:38:08 +00:00
|
|
|
|
var data = await c.Downloader.GetAndCacheCookies(new CefSharpWrapper(vm.Browser), m => vm.Instructions = m, cancel.Token);
|
2019-12-08 19:46:30 +00:00
|
|
|
|
c.Resume(data);
|
|
|
|
|
});
|
2019-12-08 17:00:22 +00:00
|
|
|
|
break;
|
2020-01-07 13:03:46 +00:00
|
|
|
|
case YesNoIntervention c:
|
|
|
|
|
var result = MessageBox.Show(c.ExtendedDescription, c.ShortDescription, MessageBoxButton.YesNo,
|
|
|
|
|
MessageBoxImage.Question);
|
|
|
|
|
if (result == MessageBoxResult.Yes)
|
|
|
|
|
c.Confirm();
|
|
|
|
|
else
|
|
|
|
|
c.Cancel();
|
|
|
|
|
break;
|
|
|
|
|
case CriticalFailureIntervention c:
|
|
|
|
|
MessageBox.Show(c.ExtendedDescription, c.ShortDescription, MessageBoxButton.OK,
|
|
|
|
|
MessageBoxImage.Error);
|
|
|
|
|
c.Cancel();
|
|
|
|
|
break;
|
2019-12-09 00:19:36 +00:00
|
|
|
|
case ConfirmationIntervention c:
|
|
|
|
|
break;
|
2019-12-08 17:00:22 +00:00
|
|
|
|
default:
|
|
|
|
|
throw new NotImplementedException($"No handler for {msg}");
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-12-20 20:51:10 +00:00
|
|
|
|
|
|
|
|
|
|
2019-12-08 17:00:22 +00:00
|
|
|
|
}
|
|
|
|
|
}
|