From 91462dbd20e3b28efa2f1bff6a9695cf74a4e89c Mon Sep 17 00:00:00 2001 From: Timothy Baldridge Date: Sun, 13 Mar 2022 17:08:27 -0600 Subject: [PATCH] Disable all CEF calls to get it to compile --- Wabbajack.App.Wpf/App.xaml.cs | 4 - Wabbajack.App.Wpf/Models/CefService.cs | 47 +-- .../WebUserInterventionBase.cs | 2 +- Wabbajack.App.Wpf/View Models/WebBrowserVM.cs | 18 +- Wabbajack.App.Wpf/Views/MainWindow.xaml | 71 +++-- Wabbajack.App.Wpf/Views/MainWindow.xaml.cs | 9 +- .../WebAutomation/CefSharpWrapper.cs | 274 ------------------ .../WebAutomation/WebAutomation.cs | 38 ++- 8 files changed, 72 insertions(+), 391 deletions(-) delete mode 100644 Wabbajack.App.Wpf/WebAutomation/CefSharpWrapper.cs diff --git a/Wabbajack.App.Wpf/App.xaml.cs b/Wabbajack.App.Wpf/App.xaml.cs index ef171bf3..d1aa71e9 100644 --- a/Wabbajack.App.Wpf/App.xaml.cs +++ b/Wabbajack.App.Wpf/App.xaml.cs @@ -3,14 +3,10 @@ using System.Reactive.Concurrency; using System.Reactive.Disposables; using System.Windows; using System.Windows.Threading; -using CefSharp.DevTools.Debugger; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; using ReactiveUI; -using Splat; -using Wabbajack.Common; -using Wabbajack; using Wabbajack.DTOs; using Wabbajack.LoginManagers; using Wabbajack.Models; diff --git a/Wabbajack.App.Wpf/Models/CefService.cs b/Wabbajack.App.Wpf/Models/CefService.cs index 3328d169..deb74e0a 100644 --- a/Wabbajack.App.Wpf/Models/CefService.cs +++ b/Wabbajack.App.Wpf/Models/CefService.cs @@ -1,7 +1,5 @@ using System; using System.Reactive.Subjects; -using CefSharp; -using CefSharp.Wpf; using Microsoft.Extensions.Logging; namespace Wabbajack.Models; @@ -13,7 +11,6 @@ public class CefService private readonly Subject _schemeStream = new(); public IObservable SchemeStream => _schemeStream; - public Func? SchemeHandler { get; set; } public CefService(ILogger logger) { @@ -22,51 +19,13 @@ public class CefService Init(); } - public IWebBrowser CreateBrowser() + public dynamic CreateBrowser() { - return new ChromiumWebBrowser(); + return 0; } private void Init() { - if (Inited || Cef.IsInitialized) return; - Inited = true; - var settings = new CefSettings - { - CachePath = Consts.CefCacheLocation.ToString(), - UserAgent = "Wabbajack In-App Browser" - }; - settings.RegisterScheme(new CefCustomScheme() - { - SchemeName = "wabbajack", - SchemeHandlerFactory = new SchemeHandlerFactor(_logger, this) - }); - - _logger.LogInformation("Initializing Cef"); - if (!Cef.Initialize(settings)) - { - _logger.LogError("Cannot initialize CEF"); - } + } - private class SchemeHandlerFactor : ISchemeHandlerFactory - { - private readonly ILogger _logger; - private readonly CefService _service; - - internal SchemeHandlerFactor(ILogger logger, CefService service) - { - _logger = logger; - _service = service; - } - - public IResourceHandler Create(IBrowser browser, IFrame frame, string schemeName, IRequest request) - { - _logger.LogInformation("Scheme handler Got: {Scheme} : {Url}", schemeName, request.Url); - if (schemeName == "wabbajack") - { - _service._schemeStream.OnNext(request.Url); - } - return new ResourceHandler(); - } - } } \ No newline at end of file diff --git a/Wabbajack.App.Wpf/UserIntervention/WebUserInterventionBase.cs b/Wabbajack.App.Wpf/UserIntervention/WebUserInterventionBase.cs index 2e29a1c6..8918fa08 100644 --- a/Wabbajack.App.Wpf/UserIntervention/WebUserInterventionBase.cs +++ b/Wabbajack.App.Wpf/UserIntervention/WebUserInterventionBase.cs @@ -22,7 +22,7 @@ where T : IUserIntervention { Logger = logger; Browser = browser; - Driver = new CefSharpWrapper(logger, browser.Browser, service); + //Driver = new CefSharpWrapper(logger, browser.Browser, service); } public void Configure(ViewModel prevPane, T message) diff --git a/Wabbajack.App.Wpf/View Models/WebBrowserVM.cs b/Wabbajack.App.Wpf/View Models/WebBrowserVM.cs index fd009ff5..3be45cfb 100644 --- a/Wabbajack.App.Wpf/View Models/WebBrowserVM.cs +++ b/Wabbajack.App.Wpf/View Models/WebBrowserVM.cs @@ -1,21 +1,11 @@ using System; -using System.Collections.Generic; -using System.Linq; using System.Reactive; -using System.Reactive.Linq; using System.Reactive.Subjects; -using System.Text; -using System.Threading.Tasks; -using CefSharp; -using CefSharp.Wpf; using Microsoft.Extensions.Logging; using ReactiveUI; using ReactiveUI.Fody.Helpers; -using Wabbajack; -using Wabbajack.LibCefHelpers; using Wabbajack.Messages; using Wabbajack.Models; -using Wabbajack.WebAutomation; namespace Wabbajack { @@ -27,8 +17,8 @@ namespace Wabbajack [Reactive] public string Instructions { get; set; } - public IWebBrowser Browser { get; } - public CefSharpWrapper Driver { get; set; } + public dynamic Browser { get; } + public dynamic Driver { get; set; } [Reactive] public ViewModel NavigateBackTarget { get; set; } @@ -47,8 +37,8 @@ namespace Wabbajack Instructions = "Wabbajack Web Browser"; BackCommand = ReactiveCommand.Create(NavigateBack.Send); - Browser = cefService.CreateBrowser(); - Driver = new CefSharpWrapper(_logger, Browser, cefService); + //Browser = cefService.CreateBrowser(); + //Driver = new CefSharpWrapper(_logger, Browser, cefService); } diff --git a/Wabbajack.App.Wpf/Views/MainWindow.xaml b/Wabbajack.App.Wpf/Views/MainWindow.xaml index 62b70dab..a683ef55 100644 --- a/Wabbajack.App.Wpf/Views/MainWindow.xaml +++ b/Wabbajack.App.Wpf/Views/MainWindow.xaml @@ -21,37 +21,46 @@ UseLayoutRounding="True" WindowTitleBrush="{StaticResource MahApps.Brushes.Accent}" mc:Ignorable="d"> - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + WABBAJACK 3.0.0 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/Wabbajack.App.Wpf/Views/MainWindow.xaml.cs b/Wabbajack.App.Wpf/Views/MainWindow.xaml.cs index a91c3807..d3ac4656 100644 --- a/Wabbajack.App.Wpf/Views/MainWindow.xaml.cs +++ b/Wabbajack.App.Wpf/Views/MainWindow.xaml.cs @@ -3,14 +3,12 @@ using System.ComponentModel; using System.Reactive.Linq; using System.Threading.Tasks; using System.Windows; +using System.Windows.Input; using MahApps.Metro.Controls; using Microsoft.Extensions.Logging; using ReactiveUI; using Wabbajack.Common; -using Wabbajack; -using Wabbajack.LibCefHelpers; using Wabbajack.Messages; -using Wabbajack.Paths; using Wabbajack.Paths.IO; using Wabbajack.Util; @@ -185,5 +183,10 @@ namespace Wabbajack { _mwvm.ShutdownApplication().Wait(); } + + private void UIElement_OnMouseDown(object sender, MouseButtonEventArgs e) + { + this.DragMove(); + } } } diff --git a/Wabbajack.App.Wpf/WebAutomation/CefSharpWrapper.cs b/Wabbajack.App.Wpf/WebAutomation/CefSharpWrapper.cs deleted file mode 100644 index fe47ea82..00000000 --- a/Wabbajack.App.Wpf/WebAutomation/CefSharpWrapper.cs +++ /dev/null @@ -1,274 +0,0 @@ -using System; -using System.Threading; -using System.Threading.Tasks; -using CefSharp; -using Microsoft.Extensions.Logging; -using Wabbajack.LibCefHelpers; -using Wabbajack.Models; -using Wabbajack.Networking.Http; -using Wabbajack.Paths; -using Cookie = Wabbajack.DTOs.Logins.Cookie; - -namespace Wabbajack.WebAutomation -{ - public class CefSharpWrapper : IWebDriver - { - private readonly IWebBrowser _browser; - - private static readonly Random RetryRandom = new Random(); - private readonly ILogger _logger; - private readonly CefService _service; - public Action? DownloadHandler { get; set; } - public CefSharpWrapper(ILogger logger, IWebBrowser browser, CefService service) - { - _logger = logger; - _browser = browser; - _service = service; - - _browser.DownloadHandler = new DownloadHandler(this); - _browser.LifeSpanHandler = new PopupBlocker(this); - } - - public Task NavigateTo(Uri uri, CancellationToken? token = null) - { - var tcs = new TaskCompletionSource(); - - EventHandler? handler = null; - handler = (sender, e) => - { - if (e.IsLoading) return; - - _browser.LoadingStateChanged -= handler; - tcs.SetResult(true); - }; - - _browser.LoadingStateChanged += handler; - _browser.Load(uri.ToString()); - token?.Register(() => tcs.TrySetCanceled()); - - return tcs.Task; - } - - private readonly string[] KnownServerLoadStrings = - { - "

Temporarily Unavailable

", - "
Request Header Or Cookie Too Large
", - //"" - "525", - "522", - }; - private readonly (string, int)[] KnownErrorStrings = - { - ("

400 Bad Request

", 400), - ("We could not locate the item you are trying to view.", 404), - }; - - - public async Task NavigateToAndDownload(Uri uri, AbsolutePath dest, bool quickMode = false, CancellationToken? token = null) - { - var oldCB = _browser.DownloadHandler; - - var handler = new ReroutingDownloadHandler(this, dest, quickMode: quickMode, token); - _browser.DownloadHandler = handler; - - try - { - int retryCount = 0; - RETRY: - await NavigateTo(uri, token); - var source = await _browser.GetSourceAsync(); - foreach (var err in KnownServerLoadStrings) - { - if (!source.Contains(err)) - continue; - - if ((token?.IsCancellationRequested) == true) - { - throw new TimeoutException(); - } - else - { - retryCount += 1; - var retry = RetryRandom.Next(retryCount * 5000, retryCount * 5000 * 2); - _logger.LogWarning("Got server load error from {Uri} retying in {Retry}ms [{Error}]", uri, retry, err); - await Task.Delay(TimeSpan.FromMilliseconds(retry)); - goto RETRY; - } - } - - foreach (var (err, httpCode) in KnownErrorStrings) - { - if (source.Contains(err)) - throw new HttpException(httpCode,$"Web driver failed: {err}"); - } - - _logger.LogInformation("Loaded page {Uri} starting download", uri); - return await handler.TaskResult; - } - finally { - _browser.DownloadHandler = oldCB; - - } - } - - public async Task EvaluateJavaScript(string text) - { - var result = await _browser.EvaluateScriptAsync(text); - if (!result.Success) - throw new Exception(result.Message); - - return (string)result.Result; - } - - public Task GetCookies(string domainPrefix) - { - return Helpers.GetCookies(domainPrefix); - } - - private const string CefStateName = "cef-state"; - - public async Task WaitForInitialized() - { - while (!_browser.IsBrowserInitialized) - await Task.Delay(100); - } - - - public ISchemeHandler WithSchemeHandler(Predicate predicate) - { - return new SchemeHandler(predicate, _service); - } - - private class SchemeHandler : ISchemeHandler - { - private readonly TaskCompletionSource _tcs; - private readonly IDisposable _disposable; - - public SchemeHandler(Predicate predicate, CefService service) - { - _tcs = new TaskCompletionSource(); - _disposable = service.SchemeStream.Subscribe(s => - { - if (Uri.TryCreate(s, UriKind.Absolute, out var result) && predicate(result)) - { - _tcs.TrySetResult(result); - } - }); - } - - public void Dispose() - { - - _tcs.TrySetCanceled(); - _disposable.Dispose(); - } - - public Task Task => _tcs.Task; - } - - - public async Task RegisterSchemeCallback() - { - var frame = _browser.GetFocusedFrame(); - } - - public string Location => _browser.Address; - - } - - public class PopupBlocker : ILifeSpanHandler - { - private readonly CefSharpWrapper _wrapper; - - public PopupBlocker(CefSharpWrapper cefSharpWrapper) - { - _wrapper = cefSharpWrapper; - } - - public bool OnBeforePopup(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, string targetUrl, - string targetFrameName, WindowOpenDisposition targetDisposition, bool userGesture, IPopupFeatures popupFeatures, - IWindowInfo windowInfo, IBrowserSettings browserSettings, ref bool noJavascriptAccess, out IWebBrowser? newBrowser) - { - // Block popups - newBrowser = null; - return true; - } - - public void OnAfterCreated(IWebBrowser chromiumWebBrowser, IBrowser browser) - { - } - - public bool DoClose(IWebBrowser chromiumWebBrowser, IBrowser browser) - { - return false; - } - - public void OnBeforeClose(IWebBrowser chromiumWebBrowser, IBrowser browser) - { - } - } - - public class ReroutingDownloadHandler : IDownloadHandler - { - private CefSharpWrapper _wrapper; - private AbsolutePath _path; - public TaskCompletionSource _tcs = new TaskCompletionSource(); - private bool _quickMode; - private CancellationToken? _cancelationToken; - public Task TaskResult => _tcs.Task; - - public ReroutingDownloadHandler(CefSharpWrapper wrapper, AbsolutePath path, bool quickMode, CancellationToken? token) - { - _wrapper = wrapper; - _path = path; - _quickMode = quickMode; - _cancelationToken = token; - token?.Register(() => _tcs.TrySetCanceled()); - } - - public void OnBeforeDownload(IWebBrowser chromiumWebBrowser, IBrowser browser, DownloadItem downloadItem, - IBeforeDownloadCallback callback) - { - if (_quickMode) return; - - callback.Continue(_path.ToString(), false); - } - - public void OnDownloadUpdated(IWebBrowser chromiumWebBrowser, IBrowser browser, DownloadItem downloadItem, - IDownloadItemCallback callback) - { - if (_quickMode) - { - callback.Cancel(); - _tcs.TrySetResult(downloadItem.TotalBytes); - return; - } - - if (downloadItem.IsComplete) - _tcs.TrySetResult(downloadItem.TotalBytes); - callback.Resume(); - } - } - - public class DownloadHandler : IDownloadHandler - { - private CefSharpWrapper _wrapper; - - public DownloadHandler(CefSharpWrapper wrapper) - { - _wrapper = wrapper; - } - - public void OnBeforeDownload(IWebBrowser chromiumWebBrowser, IBrowser browser, DownloadItem downloadItem, - IBeforeDownloadCallback callback) - { - _wrapper.DownloadHandler?.Invoke(new Uri(downloadItem.Url)); - } - - public void OnDownloadUpdated(IWebBrowser chromiumWebBrowser, IBrowser browser, DownloadItem downloadItem, - IDownloadItemCallback callback) - { - callback.Cancel(); - } - } -} diff --git a/Wabbajack.App.Wpf/WebAutomation/WebAutomation.cs b/Wabbajack.App.Wpf/WebAutomation/WebAutomation.cs index a3baa417..d7e5ad68 100644 --- a/Wabbajack.App.Wpf/WebAutomation/WebAutomation.cs +++ b/Wabbajack.App.Wpf/WebAutomation/WebAutomation.cs @@ -1,14 +1,8 @@ using System; -using System.Linq; using System.Threading; using System.Threading.Tasks; -using System.Windows; -using CefSharp; -using CefSharp.OffScreen; using HtmlAgilityPack; using Microsoft.Extensions.Logging; -using Wabbajack.Common; -using Wabbajack.LibCefHelpers; using Wabbajack.Models; using Wabbajack.Paths; using Wabbajack.Paths.IO; @@ -17,20 +11,20 @@ namespace Wabbajack.WebAutomation { public class Driver : IDisposable { - private readonly IWebBrowser _browser; - private readonly CefSharpWrapper _driver; + //private readonly IWebBrowser _browser; + private readonly dynamic _driver; public Driver(ILogger logger, CefService service) { - _browser = new ChromiumWebBrowser(); - _driver = new CefSharpWrapper(logger, _browser, service); + //_browser = new ChromiumWebBrowser(); + //_driver = new CefSharpWrapper(logger, _browser, service); } public async Task NavigateTo(Uri uri, CancellationToken? token = null) { try { - await _driver.NavigateTo(uri, token); + //await _driver.NavigateTo(uri, token); return await GetLocation(); } catch (TaskCanceledException ex) @@ -46,16 +40,17 @@ namespace Wabbajack.WebAutomation .WithExtension(new Extension(".html")); file.Parent.CreateDirectory(); var source = await GetSourceAsync(); - var cookies = await Helpers.GetCookies(); - var cookiesString = string.Join('\n', cookies.Select(c => c.Name + " - " + c.Value)); - await file.WriteAllTextAsync(uri + "\n " + source + "\n" + ex + "\n" + cookiesString); + //var cookies = await Helpers.GetCookies(); + //var cookiesString = string.Join('\n', cookies.Select(c => c.Name + " - " + c.Value)); + //await file.WriteAllTextAsync(uri + "\n " + source + "\n" + ex + "\n" + cookiesString); } public async Task NavigateToAndDownload(Uri uri, AbsolutePath absolutePath, bool quickMode = false, CancellationToken? token = null) { try { - return await _driver.NavigateToAndDownload(uri, absolutePath, quickMode: quickMode, token: token); + //return await _driver.NavigateToAndDownload(uri, absolutePath, quickMode: quickMode, token: token); + return 0; } catch (TaskCanceledException ex) { await DumpState(uri, ex); @@ -65,6 +60,7 @@ namespace Wabbajack.WebAutomation public async ValueTask GetLocation() { + /* try { return new Uri(_browser.Address); @@ -72,12 +68,14 @@ namespace Wabbajack.WebAutomation catch (UriFormatException) { return null; - } + }*/ + return null; } public async ValueTask GetSourceAsync() { - return await _browser.GetSourceAsync(); + //return await _browser.GetSourceAsync(); + return ""; } public async ValueTask GetHtmlAsync() @@ -104,17 +102,17 @@ namespace Wabbajack.WebAutomation public void Dispose() { - _browser.Dispose(); + //_browser.Dispose(); } public static void ClearCache() { - Helpers.ClearCookies(); + //Helpers.ClearCookies(); } public async Task DeleteCookiesWhere(Func filter) { - await Helpers.DeleteCookiesWhere(filter); + //await Helpers.DeleteCookiesWhere(filter); } } }