Use headless WebViewManager on headless clients.

This commit is contained in:
Pieter-Jan Briers
2021-11-03 15:26:52 +01:00
parent d4a7e8f3e0
commit dee8a87acd
6 changed files with 144 additions and 12 deletions

View File

@@ -0,0 +1,127 @@
using System;
using Robust.Client.Graphics;
using Robust.Client.UserInterface;
namespace Robust.Client.WebView.Headless
{
internal sealed class WebViewManagerHeadless : IWebViewManagerImpl
{
public IWebViewWindow CreateBrowserWindow(BrowserWindowCreateParameters createParams)
{
return new WebViewWindowDummy();
}
public IWebViewControlImpl MakeControlImpl(WebViewControl owner)
{
return new WebViewControlImplDummy();
}
public void Initialize()
{
// Nop
}
public void Update()
{
// Nop
}
public void Shutdown()
{
// Nop
}
private abstract class DummyBase : IWebViewControl
{
public string Url { get; set; } = "about:blank";
public bool IsLoading => true;
public void StopLoad()
{
}
public void Reload()
{
}
public bool GoBack()
{
return false;
}
public bool GoForward()
{
return false;
}
public void ExecuteJavaScript(string code)
{
}
public void AddResourceRequestHandler(Action<IRequestHandlerContext> handler)
{
}
public void RemoveResourceRequestHandler(Action<IRequestHandlerContext> handler)
{
}
}
private sealed class WebViewControlImplDummy : DummyBase, IWebViewControlImpl
{
public void EnteredTree()
{
}
public void ExitedTree()
{
}
public void MouseMove(GUIMouseMoveEventArgs args)
{
}
public void MouseExited()
{
}
public void MouseWheel(GUIMouseWheelEventArgs args)
{
}
public bool RawKeyEvent(in GuiRawKeyEvent guiRawEvent)
{
return false;
}
public void TextEntered(GUITextEventArgs args)
{
}
public void Resized()
{
}
public void Draw(DrawingHandleScreen handle)
{
}
public void AddBeforeBrowseHandler(Action<IBeforeBrowseContext> handler)
{
}
public void RemoveBeforeBrowseHandler(Action<IBeforeBrowseContext> handler)
{
}
}
private sealed class WebViewWindowDummy : DummyBase, IWebViewWindow
{
public void Dispose()
{
}
public bool Closed => false;
}
}
}

View File

@@ -5,8 +5,10 @@ namespace Robust.Client.WebView
/// <summary>
/// Internal implementation of WebViewManager that is switched out by <see cref="IWebViewManagerHook"/>.
/// </summary>
internal interface IWebViewManagerImpl : IWebViewManagerInternal, IWebViewManagerHook
internal interface IWebViewManagerImpl : IWebViewManagerInternal
{
void Initialize();
void Update();
void Shutdown();
}
}

View File

@@ -1,5 +1,6 @@
using Robust.Client.WebView;
using Robust.Client.WebView.Cef;
using Robust.Client.WebView.Headless;
using Robust.Client.WebViewHook;
using Robust.Shared.IoC;
using Robust.Shared.Utility;
@@ -12,14 +13,18 @@ namespace Robust.Client.WebView
{
private IWebViewManagerImpl? _impl;
public void Initialize()
public void Initialize(GameController.DisplayMode mode)
{
DebugTools.Assert(_impl == null, "WebViewManager has already been initialized!");
IoCManager.RegisterInstance<IWebViewManager>(this);
IoCManager.RegisterInstance<IWebViewManagerInternal>(this);
_impl = new WebViewManagerCef();
if (mode == GameController.DisplayMode.Headless)
_impl = new WebViewManagerHeadless();
else
_impl = new WebViewManagerCef();
_impl.Initialize();
}

View File

@@ -8,7 +8,7 @@ namespace Robust.Client
{
internal sealed partial class GameController
{
private void LoadOptionalRobustModules()
private void LoadOptionalRobustModules(GameController.DisplayMode mode)
{
// In the future, this manifest should be loaded somewhere else and used for more parts of init.
// For now, this is fine.
@@ -19,7 +19,7 @@ namespace Robust.Client
switch (module)
{
case "Robust.Client.WebView":
LoadRobustWebView();
LoadRobustWebView(mode);
break;
default:
Logger.Error($"Unknown Robust module: {module}");
@@ -28,7 +28,7 @@ namespace Robust.Client
}
}
private void LoadRobustWebView()
private void LoadRobustWebView(GameController.DisplayMode mode)
{
Logger.Debug("Loading Robust.Client.WebView");
@@ -38,7 +38,7 @@ namespace Robust.Client
var managerType = attribute.ImplementationType;
_webViewHook = (IWebViewManagerHook)Activator.CreateInstance(managerType)!;
_webViewHook.Initialize();
_webViewHook.Initialize(mode);
Logger.Debug("Done initializing Robust.Client.WebView");
}

View File

@@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Runtime;
@@ -11,7 +10,6 @@ using Robust.Client.GameStates;
using Robust.Client.Graphics;
using Robust.Client.Input;
using Robust.Client.Placement;
using Robust.Client.Player;
using Robust.Client.ResourceManagement;
using Robust.Client.State;
using Robust.Client.UserInterface;
@@ -112,7 +110,7 @@ namespace Robust.Client
IoCManager.Resolve<ISerializationManager>().Initialize();
// Load optional Robust modules.
LoadOptionalRobustModules();
LoadOptionalRobustModules(displayMode);
// Call Init in game assemblies.
_modLoader.BroadcastRunLevel(ModRunLevel.PreInit);

View File

@@ -18,7 +18,7 @@ namespace Robust.Client.WebViewHook
internal interface IWebViewManagerHook
{
void Initialize();
void Initialize(GameController.DisplayMode mode);
void Update();
void Shutdown();
}