From dee8a87acd9ab01eaa3199b51ba1bc9270cfcdcf Mon Sep 17 00:00:00 2001 From: Pieter-Jan Briers Date: Wed, 3 Nov 2021 15:26:52 +0100 Subject: [PATCH] Use headless WebViewManager on headless clients. --- .../Headless/WebViewManagerHeadless.cs | 127 ++++++++++++++++++ Robust.Client.WebView/IWebViewManagerImpl.cs | 6 +- Robust.Client.WebView/WebViewManager.cs | 9 +- .../GameController.RobustModules.cs | 8 +- .../GameController/GameController.cs | 4 +- .../WebViewHook/IWebViewManagerHook.cs | 2 +- 6 files changed, 144 insertions(+), 12 deletions(-) create mode 100644 Robust.Client.WebView/Headless/WebViewManagerHeadless.cs diff --git a/Robust.Client.WebView/Headless/WebViewManagerHeadless.cs b/Robust.Client.WebView/Headless/WebViewManagerHeadless.cs new file mode 100644 index 000000000..6500b9056 --- /dev/null +++ b/Robust.Client.WebView/Headless/WebViewManagerHeadless.cs @@ -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 handler) + { + } + + public void RemoveResourceRequestHandler(Action 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 handler) + { + } + + public void RemoveBeforeBrowseHandler(Action handler) + { + } + } + + private sealed class WebViewWindowDummy : DummyBase, IWebViewWindow + { + public void Dispose() + { + } + + public bool Closed => false; + } + } +} diff --git a/Robust.Client.WebView/IWebViewManagerImpl.cs b/Robust.Client.WebView/IWebViewManagerImpl.cs index 498f6a4ee..87b5df4ef 100644 --- a/Robust.Client.WebView/IWebViewManagerImpl.cs +++ b/Robust.Client.WebView/IWebViewManagerImpl.cs @@ -5,8 +5,10 @@ namespace Robust.Client.WebView /// /// Internal implementation of WebViewManager that is switched out by . /// - internal interface IWebViewManagerImpl : IWebViewManagerInternal, IWebViewManagerHook + internal interface IWebViewManagerImpl : IWebViewManagerInternal { - + void Initialize(); + void Update(); + void Shutdown(); } } diff --git a/Robust.Client.WebView/WebViewManager.cs b/Robust.Client.WebView/WebViewManager.cs index 799a3d1fa..58480dee6 100644 --- a/Robust.Client.WebView/WebViewManager.cs +++ b/Robust.Client.WebView/WebViewManager.cs @@ -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(this); IoCManager.RegisterInstance(this); - _impl = new WebViewManagerCef(); + if (mode == GameController.DisplayMode.Headless) + _impl = new WebViewManagerHeadless(); + else + _impl = new WebViewManagerCef(); + _impl.Initialize(); } diff --git a/Robust.Client/GameController/GameController.RobustModules.cs b/Robust.Client/GameController/GameController.RobustModules.cs index 640771aad..cca8790fe 100644 --- a/Robust.Client/GameController/GameController.RobustModules.cs +++ b/Robust.Client/GameController/GameController.RobustModules.cs @@ -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"); } diff --git a/Robust.Client/GameController/GameController.cs b/Robust.Client/GameController/GameController.cs index 76859ccac..989e27170 100644 --- a/Robust.Client/GameController/GameController.cs +++ b/Robust.Client/GameController/GameController.cs @@ -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().Initialize(); // Load optional Robust modules. - LoadOptionalRobustModules(); + LoadOptionalRobustModules(displayMode); // Call Init in game assemblies. _modLoader.BroadcastRunLevel(ModRunLevel.PreInit); diff --git a/Robust.Client/WebViewHook/IWebViewManagerHook.cs b/Robust.Client/WebViewHook/IWebViewManagerHook.cs index 455aab622..78ec47945 100644 --- a/Robust.Client/WebViewHook/IWebViewManagerHook.cs +++ b/Robust.Client/WebViewHook/IWebViewManagerHook.cs @@ -18,7 +18,7 @@ namespace Robust.Client.WebViewHook internal interface IWebViewManagerHook { - void Initialize(); + void Initialize(GameController.DisplayMode mode); void Update(); void Shutdown(); }