Disable all CEF calls to get it to compile

This commit is contained in:
Timothy Baldridge 2022-03-13 17:08:27 -06:00
parent 2cdc61debd
commit 91462dbd20
8 changed files with 72 additions and 391 deletions

View File

@ -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;

View File

@ -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<string> _schemeStream = new();
public IObservable<string> SchemeStream => _schemeStream;
public Func<IBrowser, IFrame, string, IRequest, IResourceHandler>? SchemeHandler { get; set; }
public CefService(ILogger<CefService> 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();
}
}
}

View File

@ -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)

View File

@ -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);
}

View File

@ -21,37 +21,46 @@
UseLayoutRounding="True"
WindowTitleBrush="{StaticResource MahApps.Brushes.Accent}"
mc:Ignorable="d">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
</Grid.RowDefinitions>
<ContentPresenter Grid.Row="0" Content="{Binding ActivePane}">
<ContentPresenter.Resources>
<DataTemplate DataType="{x:Type local:CompilerVM}">
<local:CompilerView ViewModel="{Binding}" />
</DataTemplate>
<DataTemplate DataType="{x:Type local:InstallerVM}">
<local:InstallationView ViewModel="{Binding}" />
</DataTemplate>
<DataTemplate DataType="{x:Type local:ModeSelectionVM}">
<local:ModeSelectionView ViewModel="{Binding}" />
</DataTemplate>
<DataTemplate DataType="{x:Type local:ModListGalleryVM}">
<local:ModListGalleryView ViewModel="{Binding}" />
</DataTemplate>
<DataTemplate DataType="{x:Type local:WebBrowserVM}">
<local:WebBrowserView />
</DataTemplate>
<DataTemplate DataType="{x:Type local:SettingsVM}">
<local:SettingsView ViewModel="{Binding}" />
</DataTemplate>
<DataTemplate DataType="{x:Type viewModels:ModListContentsVM}">
<local:ModListContentsView ViewModel="{Binding}" />
</DataTemplate>
</ContentPresenter.Resources>
</ContentPresenter>
<TextBlock Grid.Row="1" Margin="5, 0" Name="ResourceUsage" HorizontalAlignment="Right"></TextBlock>
<Grid Background="#121212" MouseDown="UIElement_OnMouseDown">
<TabControl x:Name="Tabs">
<TabItem>
<TabItem.Header>
<TextBlock FontSize="16" Margin="0, 0, 8, 0">WABBAJACK 3.0.0</TextBlock>
</TabItem.Header>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
</Grid.RowDefinitions>
<ContentPresenter Grid.Row="0" Content="{Binding ActivePane}">
<ContentPresenter.Resources>
<DataTemplate DataType="{x:Type local:CompilerVM}">
<local:CompilerView ViewModel="{Binding}" />
</DataTemplate>
<DataTemplate DataType="{x:Type local:InstallerVM}">
<local:InstallationView ViewModel="{Binding}" />
</DataTemplate>
<DataTemplate DataType="{x:Type local:ModeSelectionVM}">
<local:ModeSelectionView ViewModel="{Binding}" />
</DataTemplate>
<DataTemplate DataType="{x:Type local:ModListGalleryVM}">
<local:ModListGalleryView ViewModel="{Binding}" />
</DataTemplate>
<DataTemplate DataType="{x:Type local:WebBrowserVM}">
<local:WebBrowserView />
</DataTemplate>
<DataTemplate DataType="{x:Type local:SettingsVM}">
<local:SettingsView ViewModel="{Binding}" />
</DataTemplate>
<DataTemplate DataType="{x:Type viewModels:ModListContentsVM}">
<local:ModListContentsView ViewModel="{Binding}" />
</DataTemplate>
</ContentPresenter.Resources>
</ContentPresenter>
<TextBlock Grid.Row="1" Margin="5, 0" Name="ResourceUsage" HorizontalAlignment="Right"></TextBlock>
</Grid>
</TabItem>
</TabControl>
</Grid>
<mahapps:MetroWindow.RightWindowCommands>

View File

@ -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();
}
}
}

View File

@ -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<Uri>? 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<bool>();
EventHandler<LoadingStateChangedEventArgs>? 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 =
{
"<h1>Temporarily Unavailable</h1>",
"<center>Request Header Or Cookie Too Large</center>",
//"<html><head></head><body></body></html>"
"<span class=\"cf-error-code\">525</span>",
"<span class=\"cf-error-code\">522</span>",
};
private readonly (string, int)[] KnownErrorStrings =
{
("<h1>400 Bad Request</h1>", 400),
("We could not locate the item you are trying to view.", 404),
};
public async Task<long> 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<string> EvaluateJavaScript(string text)
{
var result = await _browser.EvaluateScriptAsync(text);
if (!result.Success)
throw new Exception(result.Message);
return (string)result.Result;
}
public Task<Cookie[]> 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<Uri> predicate)
{
return new SchemeHandler(predicate, _service);
}
private class SchemeHandler : ISchemeHandler
{
private readonly TaskCompletionSource<Uri> _tcs;
private readonly IDisposable _disposable;
public SchemeHandler(Predicate<Uri> predicate, CefService service)
{
_tcs = new TaskCompletionSource<Uri>();
_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<Uri> 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<long> _tcs = new TaskCompletionSource<long>();
private bool _quickMode;
private CancellationToken? _cancelationToken;
public Task<long> 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();
}
}
}

View File

@ -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<Uri?> 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<long> 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<Uri?> GetLocation()
{
/*
try
{
return new Uri(_browser.Address);
@ -72,12 +68,14 @@ namespace Wabbajack.WebAutomation
catch (UriFormatException)
{
return null;
}
}*/
return null;
}
public async ValueTask<string> GetSourceAsync()
{
return await _browser.GetSourceAsync();
//return await _browser.GetSourceAsync();
return "";
}
public async ValueTask<HtmlDocument> 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<DTOs.Logins.Cookie, bool> filter)
{
await Helpers.DeleteCookiesWhere(filter);
//await Helpers.DeleteCookiesWhere(filter);
}
}
}