Restructure CEF stuff in preparation for launcher packaging.

Robust.Client.CEF Renamed to Robust.Client.WebView since CEF should really be an implementation detail.
Content is no longer responsible for initializing and managing the module, this is done automatically by the engine.
WebView is initialized by declaring it in a manifest.yml file in the game resources. In the future the launcher will read this same file to manage WebView module versions.
CefManager has been made private and the content-visible API is now IWebViewManager.
This commit is contained in:
Pieter-Jan Briers
2021-11-01 21:03:51 +01:00
parent 4210f30460
commit ba2f464249
25 changed files with 219 additions and 89 deletions

View File

@@ -1,9 +0,0 @@
using System;
namespace Robust.Client.CEF
{
public interface IBrowserWindow : IBrowserControl, IDisposable
{
bool Closed { get; }
}
}

View File

@@ -1,25 +0,0 @@
<Project Sdk="Microsoft.NET.Sdk">
<Import Project="..\MSBuild\Robust.Properties.targets" />
<Import Project="..\MSBuild\Robust.Engine.props" />
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<PlatformTarget>x64</PlatformTarget>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<OutputType>WinExe</OutputType>
</PropertyGroup>
<Import Project="..\MSBuild\Robust.DefineConstants.targets" />
<Target Name="RobustAfterBuild" AfterTargets="Build" />
<Import Project="..\MSBuild\Robust.Engine.targets" />
<ItemGroup>
<PackageReference Include="JetBrains.Annotations" Version="2020.3.0" />
<PackageReference Include="System.Drawing.Common" Version="5.0.2" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\cefglue\CefGlue\CefGlue.csproj" />
<ProjectReference Include="..\Robust.Client\Robust.Client.csproj" />
</ItemGroup>
</Project>

View File

@@ -1,6 +1,6 @@
using Xilium.CefGlue;
namespace Robust.Client.CEF
namespace Robust.Client.WebView
{
public sealed class BeforeBrowseContext
{

View File

@@ -1,4 +1,4 @@
namespace Robust.Client.CEF
namespace Robust.Client.WebView
{
public sealed class BrowserWindowCreateParameters
{

View File

@@ -1,7 +1,7 @@

using System.Diagnostics.CodeAnalysis;
namespace Robust.Client.CEF
namespace Robust.Client.WebView
{
[SuppressMessage("ReSharper", "InconsistentNaming")]
[SuppressMessage("ReSharper", "UnusedMember.Global")]

View File

@@ -6,17 +6,17 @@ using Robust.Shared.Log;
using Robust.Shared.ViewVariables;
using Xilium.CefGlue;
namespace Robust.Client.CEF
namespace Robust.Client.WebView
{
public partial class CefManager
internal partial class CefManager
{
[Dependency] private readonly IClydeInternal _clyde = default!;
private readonly List<BrowserWindowImpl> _browserWindows = new();
private readonly List<WebViewWindowImpl> _browserWindows = new();
public IEnumerable<IBrowserWindow> AllBrowserWindows => _browserWindows;
public IEnumerable<IWebViewWindow> AllBrowserWindows => _browserWindows;
public IBrowserWindow CreateBrowserWindow(BrowserWindowCreateParameters createParams)
public IWebViewWindow CreateBrowserWindow(BrowserWindowCreateParameters createParams)
{
var mainHWnd = (_clyde.MainWindow as IClydeWindowInternal)?.WindowsHWnd ?? 0;
@@ -25,7 +25,7 @@ namespace Robust.Client.CEF
info.Height = createParams.Height;
info.SetAsPopup(mainHWnd, "ss14cef");
var impl = new BrowserWindowImpl(this);
var impl = new WebViewWindowImpl(this);
var lifeSpanHandler = new WindowLifeSpanHandler(impl);
var reqHandler = new RobustRequestHandler(Logger.GetSawmill("root"));
@@ -40,7 +40,7 @@ namespace Robust.Client.CEF
return impl;
}
private sealed class BrowserWindowImpl : IBrowserWindow
private sealed class WebViewWindowImpl : IWebViewWindow
{
private readonly CefManager _manager;
internal CefBrowser Browser = default!;
@@ -73,7 +73,7 @@ namespace Robust.Client.CEF
}
}
public BrowserWindowImpl(CefManager manager)
public WebViewWindowImpl(CefManager manager)
{
_manager = manager;
}
@@ -168,9 +168,9 @@ namespace Robust.Client.CEF
private sealed class WindowLifeSpanHandler : CefLifeSpanHandler
{
private readonly BrowserWindowImpl _windowImpl;
private readonly WebViewWindowImpl _windowImpl;
public WindowLifeSpanHandler(BrowserWindowImpl windowImpl)
public WindowLifeSpanHandler(WebViewWindowImpl windowImpl)
{
_windowImpl = windowImpl;
}

View File

@@ -1,36 +1,37 @@
using System;
using System.IO;
using JetBrains.Annotations;
using Robust.Client.WebView;
using Robust.Client.WebViewHook;
using Robust.Shared.ContentPack;
using Robust.Shared.IoC;
using Robust.Shared.Log;
using Robust.Shared.Utility;
// The library we're using right now. TODO CEF: Do we want to use something else? We will need to ship it ourselves if so.
// The library we're using right now.
// TODO CEF: Do we want to use something else? We will need to ship it ourselves if so.
using Xilium.CefGlue;
namespace Robust.Client.CEF
[assembly: WebViewManagerImpl(typeof(CefManager))]
namespace Robust.Client.WebView
{
// Register this with IoC.
// TODO CEF: think if making this inherit CefApp is a good idea...
// TODO CEF: A way to handle external window browsers...
[UsedImplicitly]
public partial class CefManager
internal partial class CefManager : IWebViewManagerHook, IWebViewManager
{
private CefApp _app = default!;
private bool _initialized = false;
/// <summary>
/// Call this to initialize CEF.
/// </summary>
public void Initialize()
{
IoCManager.RegisterInstance<IWebViewManager>(this);
IoCManager.RegisterInstance<CefManager>(this);
DebugTools.Assert(!_initialized);
string subProcessName;
if (OperatingSystem.IsWindows())
subProcessName = "Robust.Client.CEF.exe";
subProcessName = "Robust.Client.WebView.exe";
else if (OperatingSystem.IsLinux())
subProcessName = "Robust.Client.CEF";
subProcessName = "Robust.Client.WebView";
else
throw new NotSupportedException("Unsupported platform for CEF!");
@@ -77,9 +78,6 @@ namespace Robust.Client.CEF
throw new InvalidOperationException("CefManager has not been initialized!");
}
/// <summary>
/// Needs to be called regularly for CEF to keep working.
/// </summary>
public void Update()
{
DebugTools.Assert(_initialized);
@@ -88,9 +86,6 @@ namespace Robust.Client.CEF
CefRuntime.DoMessageLoopWork();
}
/// <summary>
/// Call before program shutdown.
/// </summary>
public void Shutdown()
{
DebugTools.Assert(_initialized);

View File

@@ -1,8 +1,8 @@
using System;
namespace Robust.Client.CEF
namespace Robust.Client.WebView
{
public interface IBrowserControl
public interface IWebViewControl
{
/// <summary>
/// Current URL of the browser. Set to load a new page.

View File

@@ -0,0 +1,7 @@
namespace Robust.Client.WebView
{
public interface IWebViewManager
{
IWebViewWindow CreateBrowserWindow(BrowserWindowCreateParameters createParams);
}
}

View File

@@ -0,0 +1,9 @@
using System;
namespace Robust.Client.WebView
{
public interface IWebViewWindow : IWebViewControl, IDisposable
{
bool Closed { get; }
}
}

View File

@@ -6,7 +6,7 @@ using SixLabors.ImageSharp;
using SixLabors.ImageSharp.PixelFormats;
using Xilium.CefGlue;
namespace Robust.Client.CEF
namespace Robust.Client.WebView
{
internal sealed class ImageBuffer
{

View File

@@ -1,7 +1,7 @@
using System;
using Xilium.CefGlue;
namespace Robust.Client.CEF
namespace Robust.Client.WebView
{
public static class Program
{

View File

@@ -3,7 +3,7 @@ using System.IO;
using System.Net;
using Xilium.CefGlue;
namespace Robust.Client.CEF
namespace Robust.Client.WebView
{
public sealed class RequestHandlerContext
{

View File

@@ -3,7 +3,7 @@ using System.IO;
using System.Net;
using Xilium.CefGlue;
namespace Robust.Client.CEF
namespace Robust.Client.WebView
{
internal interface IRequestResult
{

View File

@@ -0,0 +1,25 @@
<Project Sdk="Microsoft.NET.Sdk">
<Import Project="..\MSBuild\Robust.Properties.targets" />
<Import Project="..\MSBuild\Robust.Engine.props" />
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<PlatformTarget>x64</PlatformTarget>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<OutputType>WinExe</OutputType>
</PropertyGroup>
<Import Project="..\MSBuild\Robust.DefineConstants.targets" />
<Target Name="RobustAfterBuild" AfterTargets="Build" />
<Import Project="..\MSBuild\Robust.Engine.targets" />
<ItemGroup>
<PackageReference Include="JetBrains.Annotations" Version="2020.3.0" />
<PackageReference Include="System.Drawing.Common" Version="5.0.2" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\cefglue\CefGlue\CefGlue.csproj" />
<ProjectReference Include="..\Robust.Client\Robust.Client.csproj" />
</ItemGroup>
</Project>

View File

@@ -3,7 +3,7 @@ using Robust.Shared.IoC;
using Robust.Shared.Log;
using Xilium.CefGlue;
namespace Robust.Client.CEF
namespace Robust.Client.WebView
{
internal class RobustCefApp : CefApp
{

View File

@@ -1,6 +1,6 @@
using Xilium.CefGlue;
namespace Robust.Client.CEF
namespace Robust.Client.WebView
{
// Simple CEF client.
internal class RobustCefClient : CefClient

View File

@@ -1,6 +1,6 @@
using Xilium.CefGlue;
namespace Robust.Client.CEF
namespace Robust.Client.WebView
{
public sealed class RobustLoadHandler : CefLoadHandler
{

View File

@@ -3,7 +3,7 @@ using System.Collections.Generic;
using Robust.Shared.Log;
using Xilium.CefGlue;
namespace Robust.Client.CEF
namespace Robust.Client.WebView
{
internal sealed class RobustRequestHandler : CefRequestHandler
{

View File

@@ -10,14 +10,16 @@ using Robust.Shared.Utility;
using Robust.Shared.ViewVariables;
using SixLabors.ImageSharp.PixelFormats;
using Xilium.CefGlue;
using static Robust.Client.CEF.CefKeyCodes;
using static Robust.Client.CEF.CefKeyCodes.ChromiumKeyboardCode;
using static Robust.Client.WebView.CefKeyCodes;
using static Robust.Client.WebView.CefKeyCodes.ChromiumKeyboardCode;
using static Robust.Client.Input.Keyboard;
namespace Robust.Client.CEF
namespace Robust.Client.WebView
{
// Funny browser control to integrate in UI.
public class BrowserControl : Control, IBrowserControl, IRawInputControl
/// <summary>
/// An UI control that presents web content.
/// </summary>
public sealed class WebView : Control, IWebViewControl, IRawInputControl
{
private const int ScrollSpeed = 50;
@@ -146,7 +148,7 @@ namespace Robust.Client.CEF
[Key.Pause] = VKEY_PAUSE,
};
public BrowserControl()
public WebView()
{
CanKeyboardFocus = true;
KeyboardFocusOnClick = true;

View File

@@ -0,0 +1,52 @@
using System;
using System.Reflection;
using Robust.Client.WebViewHook;
using Robust.Shared.Log;
using Robust.Shared.Utility;
namespace Robust.Client
{
internal sealed partial class GameController
{
private void LoadOptionalRobustModules()
{
// In the future, this manifest should be loaded somewhere else and used for more parts of init.
// For now, this is fine.
var manifest = LoadResourceManifest();
foreach (var module in manifest.Modules)
{
switch (module)
{
case "Robust.Client.WebView":
LoadRobustWebView();
break;
default:
Logger.Error($"Unknown Robust module: {module}");
return;
}
}
}
private void LoadRobustWebView()
{
Logger.Debug("Loading Robust.Client.WebView");
var assembly = LoadRobustModuleAssembly("Robust.Client.WebView");
var attribute = assembly.GetCustomAttribute<WebViewManagerImplAttribute>()!;
DebugTools.AssertNotNull(attribute);
var managerType = attribute.ImplementationType;
_webViewHook = (IWebViewManagerHook)Activator.CreateInstance(managerType)!;
_webViewHook.Initialize();
Logger.Debug("Done initializing Robust.Client.WebView");
}
private Assembly LoadRobustModuleAssembly(string assemblyName)
{
// TODO: Launcher distribution and all that stuff.
return Assembly.Load(assemblyName);
}
}
}

View File

@@ -17,6 +17,7 @@ using Robust.Client.State;
using Robust.Client.UserInterface;
using Robust.Client.Utility;
using Robust.Client.ViewVariables;
using Robust.Client.WebViewHook;
using Robust.LoaderApi;
using Robust.Shared;
using Robust.Shared.Asynchronous;
@@ -32,6 +33,7 @@ using Robust.Shared.Serialization;
using Robust.Shared.Serialization.Manager;
using Robust.Shared.Timing;
using Robust.Shared.Utility;
using YamlDotNet.RepresentationModel;
namespace Robust.Client
{
@@ -67,6 +69,8 @@ namespace Robust.Client
[Dependency] private readonly IMidiManager _midiManager = default!;
[Dependency] private readonly IEyeManager _eyeManager = default!;
private IWebViewManagerHook? _webViewHook;
private CommandLineArgs? _commandLineArgs;
// Arguments for loader-load. Not used otherwise.
@@ -87,7 +91,7 @@ namespace Robust.Client
_clyde.SetWindowTitle(Options.DefaultWindowTitle);
_taskManager.Initialize();
_fontManager.SetFontDpi((uint) _configurationManager.GetCVar(CVars.DisplayFontDpi));
_fontManager.SetFontDpi((uint)_configurationManager.GetCVar(CVars.DisplayFontDpi));
// Disable load context usage on content start.
// This prevents Content.Client being loaded twice and things like csi blowing up because of it.
@@ -107,6 +111,9 @@ namespace Robust.Client
IoCManager.Resolve<ISerializationManager>().Initialize();
// Load optional Robust modules.
LoadOptionalRobustModules();
// Call Init in game assemblies.
_modLoader.BroadcastRunLevel(ModRunLevel.PreInit);
_modLoader.BroadcastRunLevel(ModRunLevel.Init);
@@ -196,6 +203,39 @@ namespace Robust.Client
return true;
}
private ResourceManifestData LoadResourceManifest()
{
// Parses /manifest.yml for game-specific settings that cannot be exclusively set up by content code.
if (!_resourceCache.TryContentFileRead("/manifest.yml", out var stream))
return new ResourceManifestData(Array.Empty<string>());
var yamlStream = new YamlStream();
using (stream)
{
using var streamReader = new StreamReader(stream, EncodingHelpers.UTF8);
yamlStream.Load(streamReader);
}
if (yamlStream.Documents.Count != 1 || yamlStream.Documents[0].RootNode is not YamlMappingNode mapping)
{
throw new InvalidOperationException(
"Expected a single YAML document with root mapping for /manifest.yml");
}
var modules = Array.Empty<string>();
if (mapping.TryGetNode("modules", out var modulesMap))
{
var sequence = (YamlSequenceNode)modulesMap;
modules = new string[sequence.Children.Count];
for (var i = 0; i < modules.Length; i++)
{
modules[i] = sequence[i].AsString();
}
}
return new ResourceManifestData(modules);
}
internal bool StartupSystemSplash(GameControllerOptions options, Func<ILogHandler>? logHandlerFactory)
{
Options = options;
@@ -217,8 +257,10 @@ namespace Robust.Client
System.Console.WriteLine($"LogLevel {level} does not exist!");
continue;
}
logLevel = result;
}
_logManager.GetSawmill(sawmill).Level = logLevel;
}
}
@@ -259,7 +301,7 @@ namespace Robust.Client
{
// Handle GameControllerOptions implicit CVar overrides.
_configurationManager.OverrideConVars(new []
_configurationManager.OverrideConVars(new[]
{
(CVars.DisplayWindowIconSet.Name, options.WindowIconSet.ToString()),
(CVars.DisplaySplashLogo.Name, options.SplashLogo.ToString())
@@ -271,9 +313,11 @@ namespace Robust.Client
_resourceCache.Initialize(Options.LoadConfigAndUserData ? userDataDir : null);
var mountOptions = _commandLineArgs != null
? MountOptions.Merge(_commandLineArgs.MountOptions, Options.MountOptions) : Options.MountOptions;
? MountOptions.Merge(_commandLineArgs.MountOptions, Options.MountOptions)
: Options.MountOptions;
ProgramShared.DoMounts(_resourceCache, mountOptions, Options.ContentBuildDirectory, Options.AssemblyDirectory,
ProgramShared.DoMounts(_resourceCache, mountOptions, Options.ContentBuildDirectory,
Options.AssemblyDirectory,
Options.LoadContentResources, _loaderArgs != null && !Options.ResourceMountDisabled, ContentStart);
if (_loaderArgs != null)
@@ -394,6 +438,7 @@ namespace Robust.Client
private void Update(FrameEventArgs frameEventArgs)
{
_webViewHook?.Update();
_clyde.FrameProcess(frameEventArgs);
_modLoader.BroadcastUpdate(ModUpdateLevel.FramePreEngine, frameEventArgs);
_stateManager.FrameUpdate(frameEventArgs);
@@ -442,7 +487,7 @@ namespace Robust.Client
var uh = logManager.GetSawmill("unhandled");
AppDomain.CurrentDomain.UnhandledException += (sender, args) =>
{
var message = ((Exception) args.ExceptionObject).ToString();
var message = ((Exception)args.ExceptionObject).ToString();
uh.Log(args.IsTerminating ? LogLevel.Fatal : LogLevel.Error, message);
};
@@ -485,11 +530,15 @@ namespace Robust.Client
{
_modLoader.Shutdown();
_webViewHook?.Shutdown();
_networkManager.Shutdown("Client shutting down");
_midiManager.Shutdown();
IoCManager.Resolve<IEntityLookup>().Shutdown();
_entityManager.Shutdown();
_clyde.Shutdown();
}
private sealed record ResourceManifestData(string[] Modules);
}
}

View File

@@ -1,7 +1,7 @@
using System.Runtime.CompilerServices;
[assembly: InternalsVisibleTo("Robust.UnitTesting")]
[assembly: InternalsVisibleTo("Robust.Client.CEF")]
[assembly: InternalsVisibleTo("Robust.Client.WebView")]
[assembly: InternalsVisibleTo("Robust.Lite")]
[assembly: InternalsVisibleTo("DynamicProxyGenAssembly2")]
[assembly: InternalsVisibleTo("Robust.Benchmarks")]

View File

@@ -0,0 +1,25 @@
using System;
namespace Robust.Client.WebViewHook
{
/// <summary>
/// Used so that the IWebViewManager can be loaded when loading Robust.Client.WebView.
/// </summary>
[AttributeUsage(AttributeTargets.Assembly)]
internal sealed class WebViewManagerImplAttribute : Attribute
{
public readonly Type ImplementationType;
public WebViewManagerImplAttribute(Type implementationType)
{
ImplementationType = implementationType;
}
}
internal interface IWebViewManagerHook
{
void Initialize();
void Update();
void Shutdown();
}
}

View File

@@ -11,7 +11,7 @@
[assembly: InternalsVisibleTo("DynamicProxyGenAssembly2")] // Gives access to Castle(Moq)
[assembly: InternalsVisibleTo("Content.Benchmarks")]
[assembly: InternalsVisibleTo("Robust.Benchmarks")]
[assembly: InternalsVisibleTo("Robust.Client.CEF")]
[assembly: InternalsVisibleTo("Robust.Client.WebView")]
#if NET5_0_OR_GREATER
[module: SkipLocalsInit]