From e4c78ebed98a8390f55e1fc8d5431b52df5d4e00 Mon Sep 17 00:00:00 2001 From: Timothy Baldridge Date: Thu, 26 Dec 2019 17:41:33 -0700 Subject: [PATCH] Clean up some references try to get tests to work --- .runsettings | 1 + Wabbajack.Lib/FodyWeavers.xml | 1 + Wabbajack.Lib/FodyWeavers.xsd | 1 + Wabbajack.Lib/LibCefHelpers/Init.cs | 29 ++++--- Wabbajack.Lib/Wabbajack.Lib.csproj | 13 +-- Wabbajack.Lib/WebAutomation/WebAutomation.cs | 83 +++++-------------- .../WebAutomation/WebAutomationWindow.xaml | 14 ---- .../WebAutomation/WebAutomationWindow.xaml.cs | 17 ---- .../WebAutomationWindowViewModel.cs | 36 -------- Wabbajack.Test/ACompilerTest.cs | 3 +- Wabbajack.Test/DownloaderTests.cs | 9 +- Wabbajack.Test/Wabbajack.Test.csproj | 39 +++++---- Wabbajack/View Models/WebBrowserVM.cs | 2 - Wabbajack/Views/MainWindow.xaml.cs | 2 +- Wabbajack/Views/WebBrowserView.xaml | 1 - 15 files changed, 83 insertions(+), 168 deletions(-) delete mode 100644 Wabbajack.Lib/WebAutomation/WebAutomationWindow.xaml delete mode 100644 Wabbajack.Lib/WebAutomation/WebAutomationWindow.xaml.cs delete mode 100644 Wabbajack.Lib/WebAutomation/WebAutomationWindowViewModel.cs diff --git a/.runsettings b/.runsettings index 3a1dfb82..288b7af3 100644 --- a/.runsettings +++ b/.runsettings @@ -5,5 +5,6 @@ 0 MethodLevel + True \ No newline at end of file diff --git a/Wabbajack.Lib/FodyWeavers.xml b/Wabbajack.Lib/FodyWeavers.xml index 63fc1484..95eaad52 100644 --- a/Wabbajack.Lib/FodyWeavers.xml +++ b/Wabbajack.Lib/FodyWeavers.xml @@ -1,3 +1,4 @@  + \ No newline at end of file diff --git a/Wabbajack.Lib/FodyWeavers.xsd b/Wabbajack.Lib/FodyWeavers.xsd index f3ac4762..4ccb1404 100644 --- a/Wabbajack.Lib/FodyWeavers.xsd +++ b/Wabbajack.Lib/FodyWeavers.xsd @@ -5,6 +5,7 @@ + diff --git a/Wabbajack.Lib/LibCefHelpers/Init.cs b/Wabbajack.Lib/LibCefHelpers/Init.cs index 307044de..b25ca09e 100644 --- a/Wabbajack.Lib/LibCefHelpers/Init.cs +++ b/Wabbajack.Lib/LibCefHelpers/Init.cs @@ -9,18 +9,17 @@ using System.Threading; using System.Threading.Tasks; using Alphaleonis.Win32.Filesystem; using CefSharp; +using CefSharp.OffScreen; using Wabbajack.Common; namespace Wabbajack.Lib.LibCefHelpers { public static class Helpers { - private static readonly Task _initTask; - /// /// We bundle the cef libs inside the .exe, we need to extract them before loading any wpf code that requires them /// - private static async Task ExtractLibs() + private static void ExtractLibs() { if (File.Exists("cefsharp.7z") && File.Exists("libcef.dll")) return; @@ -32,18 +31,15 @@ namespace Wabbajack.Lib.LibCefHelpers } using (var wq = new WorkQueue(1)) { - await FileExtractor.ExtractAll(wq, "cefsharp.7z", "."); + FileExtractor.ExtractAll(wq, "cefsharp.7z", ".").Wait(); } } static Helpers() { - _initTask = Task.Run(ExtractLibs); - } - - public static Task Initialize() - { - return _initTask; + ExtractLibs(); + if (!Cef.IsInitialized) + Cef.Initialize(new CefSettings { MultiThreadedMessageLoop = true }); } public static HttpClient GetClient(IEnumerable cookies, string referer) @@ -112,5 +108,18 @@ namespace Wabbajack.Lib.LibCefHelpers public string Domain { get; set; } public string Path { get; set; } } + + public static void Init() + { + // does nothing, but kicks off the static constructor + } + } + + public static class ModuleInitializer + { + public static void Initialize() + { + Helpers.Init(); + } } } diff --git a/Wabbajack.Lib/Wabbajack.Lib.csproj b/Wabbajack.Lib/Wabbajack.Lib.csproj index d58c9492..9447a0b6 100644 --- a/Wabbajack.Lib/Wabbajack.Lib.csproj +++ b/Wabbajack.Lib/Wabbajack.Lib.csproj @@ -156,10 +156,6 @@ - - WebAutomationWindow.xaml - - @@ -208,6 +204,9 @@ 6.0.0 + + 2.1.0 + 12.0.3 @@ -233,12 +232,6 @@ 8.0.0 - - - MSBuild:Compile - Designer - - \ No newline at end of file diff --git a/Wabbajack.Lib/WebAutomation/WebAutomation.cs b/Wabbajack.Lib/WebAutomation/WebAutomation.cs index bca33e28..09eba17a 100644 --- a/Wabbajack.Lib/WebAutomation/WebAutomation.cs +++ b/Wabbajack.Lib/WebAutomation/WebAutomation.cs @@ -2,93 +2,54 @@ using System.Threading; using System.Threading.Tasks; using System.Windows; +using CefSharp; +using CefSharp.OffScreen; namespace Wabbajack.Lib.WebAutomation { - public enum DisplayMode - { - Visible, - Hidden - } - public class Driver : IDisposable { - private WebAutomationWindow _window; - private WebAutomationWindowViewModel _ctx; - private Task _windowTask; + private IWebBrowser _browser; + private CefSharpWrapper _driver; - private Driver(DisplayMode displayMode = DisplayMode.Hidden) + public Driver() { - var windowTask = new TaskCompletionSource(); - - var t = new Thread(() => - { - _window = new WebAutomationWindow(); - _ctx = (WebAutomationWindowViewModel)_window.DataContext; - // Initiates the dispatcher thread shutdown when the window closes - - _window.Closed += (s, e) => _window.Dispatcher.InvokeShutdown(); - - if (displayMode == DisplayMode.Hidden) - { - _window.WindowState = WindowState.Minimized; - _window.ShowInTaskbar = false; - } - - _window.Show(); - - windowTask.SetResult(_window); - // Makes the thread support message pumping - System.Windows.Threading.Dispatcher.Run(); - }); - _windowTask = windowTask.Task; - - t.SetApartmentState(ApartmentState.STA); - t.Start(); + _browser = new ChromiumWebBrowser(); + _driver = new CefSharpWrapper(_browser); } - public static async Task Create() { var driver = new Driver(); - driver._window = await driver._windowTask; - driver._ctx = (WebAutomationWindowViewModel) driver._window.DataContext; + await driver._driver.WaitForInitialized(); return driver; } - public Task NavigateTo(Uri uri) + public async Task NavigateTo(Uri uri) { - return _ctx.NavigateTo(uri); + await _driver.NavigateTo(uri); + return await GetLocation(); } - public Task GetLocation() + public async ValueTask GetLocation() { - var tcs = new TaskCompletionSource(); - _window.Dispatcher.Invoke(() => tcs.SetResult(_window.WebView.Source)); - return tcs.Task; + try + { + return new Uri(_browser.Address); + } + catch (UriFormatException) + { + return null; + } } public Task GetAttr(string selector, string attr) { - var tcs = new TaskCompletionSource(); - _window.Dispatcher.Invoke(() => - { - try - { - var script = $"document.querySelector(\"{selector}\").{attr}"; - var result = _window.WebView.InvokeScript("eval", script); - tcs.SetResult(result); - } - catch (Exception ex) - { - tcs.SetException(ex); - } - }); - return tcs.Task; + return _driver.EvaluateJavaScript($"document.querySelector(\"{selector}\").{attr}"); } public void Dispose() { - _window.Dispatcher.Invoke(_window.Close); + _browser.Dispose(); } } } diff --git a/Wabbajack.Lib/WebAutomation/WebAutomationWindow.xaml b/Wabbajack.Lib/WebAutomation/WebAutomationWindow.xaml deleted file mode 100644 index 5a13ea57..00000000 --- a/Wabbajack.Lib/WebAutomation/WebAutomationWindow.xaml +++ /dev/null @@ -1,14 +0,0 @@ - - - - - - diff --git a/Wabbajack.Lib/WebAutomation/WebAutomationWindow.xaml.cs b/Wabbajack.Lib/WebAutomation/WebAutomationWindow.xaml.cs deleted file mode 100644 index 091dc88e..00000000 --- a/Wabbajack.Lib/WebAutomation/WebAutomationWindow.xaml.cs +++ /dev/null @@ -1,17 +0,0 @@ -using System.Windows; -using Wabbajack.Lib.WebAutomation; - -namespace Wabbajack -{ - /// - /// Interaction logic for WebAutomationWindow.xaml - /// - public partial class WebAutomationWindow : Window - { - public WebAutomationWindow() - { - InitializeComponent(); - DataContext = new WebAutomationWindowViewModel(this); - } - } -} diff --git a/Wabbajack.Lib/WebAutomation/WebAutomationWindowViewModel.cs b/Wabbajack.Lib/WebAutomation/WebAutomationWindowViewModel.cs deleted file mode 100644 index 5ccd55bd..00000000 --- a/Wabbajack.Lib/WebAutomation/WebAutomationWindowViewModel.cs +++ /dev/null @@ -1,36 +0,0 @@ -using System; -using System.Threading.Tasks; -using Microsoft.Toolkit.Win32.UI.Controls.Interop.WinRT; -using Microsoft.Toolkit.Wpf.UI.Controls; - -namespace Wabbajack.Lib.WebAutomation -{ - public class WebAutomationWindowViewModel : ViewModel - { - private WebAutomationWindow _window; - - private WebView Browser => _window.WebView; - - public WebAutomationWindowViewModel(WebAutomationWindow window) - { - _window = window; - } - - public Task NavigateTo(Uri uri) - { - var tcs = new TaskCompletionSource(); - - EventHandler handler = null; - handler = (s, e) => - { - Browser.NavigationCompleted -= handler; - tcs.SetResult(uri); - }; - Browser.NavigationCompleted += handler; - Browser.Source = uri; - return tcs.Task; - } - - - } -} diff --git a/Wabbajack.Test/ACompilerTest.cs b/Wabbajack.Test/ACompilerTest.cs index 77ce1409..58f2d5cd 100644 --- a/Wabbajack.Test/ACompilerTest.cs +++ b/Wabbajack.Test/ACompilerTest.cs @@ -1,5 +1,6 @@ using System; using System.Threading.Tasks; +using System.Windows.Forms; using Microsoft.VisualStudio.TestTools.UnitTesting; using Wabbajack.Common; using Wabbajack.Lib; @@ -15,7 +16,7 @@ namespace Wabbajack.Test [TestInitialize] public async Task TestInitialize() { - await Helpers.Initialize(); + Helpers.Init(); Consts.TestMode = true; utils = new TestUtils(); diff --git a/Wabbajack.Test/DownloaderTests.cs b/Wabbajack.Test/DownloaderTests.cs index 75144bac..03cc47a6 100644 --- a/Wabbajack.Test/DownloaderTests.cs +++ b/Wabbajack.Test/DownloaderTests.cs @@ -4,6 +4,8 @@ using System.Threading.Tasks; using System.Reactive.Disposables; using System.Reactive.Linq; using Alphaleonis.Win32.Filesystem; +using CefSharp; +using CefSharp.OffScreen; using Microsoft.VisualStudio.TestTools.UnitTesting; using Wabbajack.Common; using Wabbajack.Common.StatusFeed; @@ -12,6 +14,7 @@ using Wabbajack.Lib.Downloaders; using Wabbajack.Lib.LibCefHelpers; using Wabbajack.Lib.NexusApi; using Wabbajack.Lib.Validation; +using Wabbajack.Lib.WebAutomation; using File = Alphaleonis.Win32.Filesystem.File; using Game = Wabbajack.Common.Game; @@ -20,13 +23,17 @@ namespace Wabbajack.Test [TestClass] public class DownloaderTests { + static DownloaderTests() + { + Helpers.Init(); + } public TestContext TestContext { get; set; } [TestInitialize] public async Task Setup() { - await Helpers.Initialize(); + Helpers.Init(); Utils.LogMessages.OfType().Subscribe(onNext: msg => TestContext.WriteLine(msg.ShortDescription)); Utils.LogMessages.OfType().Subscribe(msg => TestContext.WriteLine("ERROR: User intervetion required: " + msg.ShortDescription)); diff --git a/Wabbajack.Test/Wabbajack.Test.csproj b/Wabbajack.Test/Wabbajack.Test.csproj index 47f5659b..1fd503d4 100644 --- a/Wabbajack.Test/Wabbajack.Test.csproj +++ b/Wabbajack.Test/Wabbajack.Test.csproj @@ -32,8 +32,8 @@ x64 CS1998 CS4014 - true - true + true + true pdbonly @@ -44,8 +44,8 @@ 4 CS1998 CS4014 - true - true + true + true true @@ -55,8 +55,8 @@ x64 prompt MinimumRecommendedRules.ruleset - true - true + true + true bin\x64\Release\ @@ -66,8 +66,8 @@ x64 prompt MinimumRecommendedRules.ruleset - true - true + true + true true @@ -78,8 +78,8 @@ 7.3 prompt MinimumRecommendedRules.ruleset - true - true + true + true bin\x86\Release\ @@ -90,10 +90,18 @@ 7.3 prompt MinimumRecommendedRules.ruleset - true - true + true + true + + False + ..\..\..\Users\tbald\.nuget\packages\cefsharp.common\75.1.143\CefSharp\x64\CefSharp.Core.dll + + + False + ..\..\..\Users\tbald\.nuget\packages\cefsharp.offscreen\75.1.143\CefSharp\x64\CefSharp.OffScreen.dll + @@ -161,8 +169,11 @@ 2.2.6 - - 75.1.28 + + 75.1.143 + + + 75.1.143 2.0.0 diff --git a/Wabbajack/View Models/WebBrowserVM.cs b/Wabbajack/View Models/WebBrowserVM.cs index 55872cfe..1a1094ab 100644 --- a/Wabbajack/View Models/WebBrowserVM.cs +++ b/Wabbajack/View Models/WebBrowserVM.cs @@ -10,7 +10,6 @@ using ReactiveUI.Fody.Helpers; using Wabbajack.Lib; using Wabbajack.Lib.LibCefHelpers; using Wabbajack.Lib.WebAutomation; -using Xilium.CefGlue.WPF; namespace Wabbajack { @@ -33,7 +32,6 @@ namespace Wabbajack public static async Task GetNew(string url = "http://www.wabbajack.org") { // Make sure libraries are extracted first - await Helpers.Initialize(); return new WebBrowserVM(url); } } diff --git a/Wabbajack/Views/MainWindow.xaml.cs b/Wabbajack/Views/MainWindow.xaml.cs index 64d7e27b..5cf16005 100644 --- a/Wabbajack/Views/MainWindow.xaml.cs +++ b/Wabbajack/Views/MainWindow.xaml.cs @@ -20,6 +20,7 @@ namespace Wabbajack public MainWindow() { + Helpers.Init(); // Wire any unhandled crashing exceptions to log before exiting AppDomain.CurrentDomain.UnhandledException += (sender, e) => { @@ -32,7 +33,6 @@ namespace Wabbajack // Run some init tasks in background Task.Run(async () => { - await Helpers.Initialize(); var appPath = System.Reflection.Assembly.GetExecutingAssembly().Location; try { diff --git a/Wabbajack/Views/WebBrowserView.xaml b/Wabbajack/Views/WebBrowserView.xaml index 324b3cce..bdb63ebe 100644 --- a/Wabbajack/Views/WebBrowserView.xaml +++ b/Wabbajack/Views/WebBrowserView.xaml @@ -5,7 +5,6 @@ xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:iconPacks="http://metro.mahapps.com/winfx/xaml/iconpacks" xmlns:wabbajack="clr-namespace:Wabbajack" - xmlns:wpf="clr-namespace:Xilium.CefGlue.WPF;assembly=Xilium.CefGlue.WPF" mc:Ignorable="d" d:DesignHeight="450" d:DesignWidth="800">