Skip to content

Commit

Permalink
Merge branch 'feature/engine-cleanup' into 'master'
Browse files Browse the repository at this point in the history
CEF Engine Cleanup

See merge request voltstro-studios/uwb/unitywebbrowser!2
  • Loading branch information
Voltstro committed Jul 3, 2024
2 parents 53aa974 + 03d213f commit 5c72038
Show file tree
Hide file tree
Showing 50 changed files with 791 additions and 911 deletions.
2 changes: 1 addition & 1 deletion src/.editorconfig
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
[*]
file_header_template=UnityWebBrowser (UWB)\nCopyright (c) 2021-2024 Voltstro-Studios\n\nThis project is under the MIT license. See the LICENSE.md file for more details.
file_header_template = UnityWebBrowser (UWB)\nCopyright (c) 2021-2024 Voltstro-Studios\n\nThis project is under the MIT license.See the LICENSE.md file for more details.
14 changes: 2 additions & 12 deletions src/DevScripts/publish-cef-engine-linux.ps1
Original file line number Diff line number Diff line change
@@ -1,12 +1,2 @@
# Any platform can compile Linux Ready to Run
# https://docs.microsoft.com/en-us/dotnet/core/deploying/ready-to-run#cross-platformarchitecture-restrictions

dotnet publish ../UnityWebBrowser.Engine.Cef/UnityWebBrowser.Engine.Cef.csproj -c ReleaseUnity -r linux-x64 -p:PublishReadyToRun=true --self-contained true --nologo

#Create Engine~/ directory if it doesn't exist
if (-Not (Test-Path "../Packages/UnityWebBrowser.Engine.Cef.Linux-x64/Engine~/")) {
New-Item -Path "../Packages/UnityWebBrowser.Engine.Cef.Linux-x64/Engine~/" -ItemType Directory
New-Item -Path "../Packages/UnityWebBrowser.Engine.Cef.Linux-x64/Engine~/swiftshader/" -ItemType Directory
}

Copy-Item -Path "../UnityWebBrowser.Engine.Cef/bin/ReleaseUnity/linux-x64/publish/*" -Destination "../Packages/UnityWebBrowser.Engine.Cef.Linux-x64/Engine~/" -Recurse -Force -PassThru
dotnet publish ../UnityWebBrowser.Engine.Cef/SubProcess/UnityWebBrowser.Engine.Cef.SubProcess.csproj -r linux-x64 -p:PublishDir=../../Packages/UnityWebBrowser.Engine.Cef.Linux-x64/Engine~ --nologo
dotnet publish ../UnityWebBrowser.Engine.Cef/Main/UnityWebBrowser.Engine.Cef.csproj -r linux-x64 -p:PublishDir=../../Packages/UnityWebBrowser.Engine.Cef.Linux-x64/Engine~ --nologo
21 changes: 2 additions & 19 deletions src/DevScripts/publish-cef-engine-windows.ps1
Original file line number Diff line number Diff line change
@@ -1,19 +1,2 @@
# Only Windows can compile Windows Ready to Run
# https://docs.microsoft.com/en-us/dotnet/core/deploying/ready-to-run#cross-platformarchitecture-restrictions

if($IsWindows)
{
dotnet publish ../UnityWebBrowser.Engine.Cef/UnityWebBrowser.Engine.Cef.csproj -c ReleaseUnity -r win-x64 -p:PublishReadyToRun=true --self-contained true --nologo
}
else
{
dotnet publish ../UnityWebBrowser.Engine.Cef/UnityWebBrowser.Engine.Cef.csproj -c ReleaseUnity -r win-x64 --self-contained true --nologo
}

#Create Engine~/ directory if it doesn't exist
if (-Not (Test-Path "../Packages/UnityWebBrowser.Engine.Cef.Win-x64/Engine~/")) {
New-Item -Path "../Packages/UnityWebBrowser.Engine.Cef.Win-x64/Engine~/" -ItemType Directory
New-Item -Path "../Packages/UnityWebBrowser.Engine.Cef.Win-x64/Engine~/swiftshader/" -ItemType Directory
}

Copy-Item -Path "../UnityWebBrowser.Engine.Cef/bin/ReleaseUnity/win-x64/publish/*" -Destination "../Packages/UnityWebBrowser.Engine.Cef.Win-x64/Engine~/" -Recurse -Force -PassThru
dotnet publish ../UnityWebBrowser.Engine.Cef/SubProcess/UnityWebBrowser.Engine.Cef.SubProcess.csproj -r win-x64 -p:PublishDir=../../Packages/UnityWebBrowser.Engine.Cef.Win-x64/Engine~ --nologo
dotnet publish ../UnityWebBrowser.Engine.Cef/Main/UnityWebBrowser.Engine.Cef.csproj -r win-x64 -p:PublishDir=../../Packages/UnityWebBrowser.Engine.Cef.Win-x64/Engine~ --nologo
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace VoltstroStudios.UnityWebBrowser.Logging
/// </summary>
public sealed class DefaultUnityWebBrowserLogger : IWebBrowserLogger
{
private const string LoggingTag = "[Web Browser]";
private const string LoggingTag = "[UWB]";

private readonly ILogger logger;

Expand Down
15 changes: 11 additions & 4 deletions src/Packages/UnityWebBrowser/Runtime/Logging/JsonLogStructure.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,23 @@ namespace VoltstroStudios.UnityWebBrowser.Logging
[Preserve]
internal class JsonLogStructure
{
[JsonProperty("@t")] public DateTime Timestamp { get; set; }
[JsonProperty("@t")]
public DateTime Timestamp { get; set; }

[JsonProperty("@m")] public string Message { get; set; }
[JsonProperty("@m")]
public string Message { get; set; }

[JsonConverter(typeof(JsonLogSeverityConverter))]
[JsonProperty("@l")]
public LogSeverity Level { get; set; } = LogSeverity.Info;

[JsonProperty("@x")] public string Exception { get; set; }
[JsonProperty("@x")]
public string Exception { get; set; }

[JsonProperty("@i")] public string EventId { get; set; }
[JsonProperty("@i")]
public string EventId { get; set; }

[JsonProperty("SourceContext")]
public string Category { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,13 @@ internal void HandleOutputProcessLog(object sender, DataReceivedEventArgs e)
JsonLogStructure logStructure = ReadJsonLog(e.Data);

if (logStructure.Level is LogSeverity.Debug or LogSeverity.Info)
logger.Debug(logStructure.Message);
logger.Debug($"[{logStructure.Category}]: {logStructure.Message}");
else if (logStructure.Level == LogSeverity.Warn)
logger.Warn(logStructure.Message);
logger.Warn($"[{logStructure.Category}]: {logStructure.Message}");
else if (logStructure.Level == LogSeverity.Error)
logger.Error($"{logStructure.Message}\n{logStructure.Exception}");
logger.Error($"[{logStructure.Category}]: {logStructure.Message}\n{logStructure.Exception}");
else if (logStructure.Level == LogSeverity.Fatal)
logger.Error($"{logStructure.Message}\n{logStructure.Exception}");
logger.Error($"[{logStructure.Category}]: {logStructure.Message}\n{logStructure.Exception}");
}
catch (Exception ex)
{
Expand Down
10 changes: 5 additions & 5 deletions src/Packages/UnityWebBrowser/Tests/Runtime/LoggerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public void DebugLogTest()
Assert.AreEqual(LogSeverity.Debug, logStructure.Level);
Assert.AreEqual(null, logStructure.Exception);
StringAssert.IsMatch("Some message...", logStructure.Message);
StringAssert.IsMatch("82d21ca4", logStructure.EventId);
//StringAssert.IsMatch("82d21ca4", logStructure.EventId);
}

[Test]
Expand All @@ -32,7 +32,7 @@ public void InfoLogTest()
Assert.AreEqual(LogSeverity.Info, logStructure.Level);
Assert.AreEqual(null, logStructure.Exception);
StringAssert.IsMatch("Some message...", logStructure.Message);
StringAssert.IsMatch("82d21ca4", logStructure.EventId);
//StringAssert.IsMatch("82d21ca4", logStructure.EventId);
}

[Test]
Expand All @@ -44,7 +44,7 @@ public void WarnLogTest()
Assert.AreEqual(LogSeverity.Warn, logStructure.Level);
Assert.AreEqual(null, logStructure.Exception);
StringAssert.IsMatch("Some message...", logStructure.Message);
StringAssert.IsMatch("82d21ca4", logStructure.EventId);
//StringAssert.IsMatch("82d21ca4", logStructure.EventId);
}

[Test]
Expand All @@ -56,7 +56,7 @@ public void ErrorLogTest()
Assert.AreEqual(LogSeverity.Error, logStructure.Level);
Assert.AreEqual(null, logStructure.Exception);
StringAssert.IsMatch("Some message...", logStructure.Message);
StringAssert.IsMatch("82d21ca4", logStructure.EventId);
//StringAssert.IsMatch("82d21ca4", logStructure.EventId);
}

[Test]
Expand All @@ -67,7 +67,7 @@ public void ExceptionLogTest()
JsonLogStructure logStructure = ProcessLogHandler.ReadJsonLog(json);
Assert.AreEqual(LogSeverity.Error, logStructure.Level);
StringAssert.IsMatch("Error setting up IPC!", logStructure.Message);
StringAssert.IsMatch("3418fa09", logStructure.EventId);
//StringAssert.IsMatch("3418fa09", logStructure.EventId);
}
}
}
2 changes: 1 addition & 1 deletion src/ThirdParty/CefGlue
Submodule CefGlue updated from 1f5a4f to 5392d3
24 changes: 0 additions & 24 deletions src/UnityWebBrowser.Engine.Cef/Core/CefSafeBuffer.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@

using System;
using System.IO;
using System.Linq;
using System.Numerics;
using System.Runtime.CompilerServices;
using UnityWebBrowser.Engine.Cef.Browser;
using Microsoft.Extensions.Logging;
using UnityWebBrowser.Engine.Cef.Shared.Browser;
using VoltstroStudios.UnityWebBrowser.Engine.Shared;
using VoltstroStudios.UnityWebBrowser.Engine.Shared.Core;
using VoltstroStudios.UnityWebBrowser.Engine.Shared.Core.Logging;
using VoltstroStudios.UnityWebBrowser.Engine.Shared.Popups;
using VoltstroStudios.UnityWebBrowser.Shared;
using VoltstroStudios.UnityWebBrowser.Shared.Core;
Expand All @@ -25,6 +24,9 @@ namespace UnityWebBrowser.Engine.Cef.Core;
/// </summary>
internal class CefEngineControlsManager : IEngineControls, IDisposable
{
private readonly ILogger mainLogger;
private readonly ILogger browserConsoleLogger;

private string[] args;
private UwbCefApp cefApp;

Expand All @@ -38,10 +40,16 @@ internal class CefEngineControlsManager : IEngineControls, IDisposable
/// <exception cref="DllNotFoundException"></exception>
/// <exception cref="CefVersionMismatchException"></exception>
/// <exception cref="Exception"></exception>
public CefEngineControlsManager()
public CefEngineControlsManager(LoggerManager loggerManagerManager)
{
//Setup CEF
CefRuntime.Load();

//this.loggerManager = loggerManager;
mainLogger = loggerManagerManager.CreateLogger("CEF Engine");
browserConsoleLogger = loggerManagerManager.CreateLogger("CEF Engine Browser Console");

CefLoggerWrapper.Init(mainLogger);
}

/// <summary>
Expand All @@ -66,23 +74,6 @@ public void EarlyInit(LaunchArguments arguments, string[] rawArguments)
//Set up CEF args and the CEF app
cefMainArgs = new CefMainArgs(argv);
cefApp = new UwbCefApp(launchArguments);

//Run our sub-processes
int exitCode = CefRuntime.ExecuteProcess(cefMainArgs, cefApp, IntPtr.Zero);
if (exitCode != -1)
{
CefLoggerWrapper.Debug("Sub-Process exit: {ExitCode}", exitCode);
Environment.Exit(exitCode);
return;
}

//Backup
if (argv.Any(arg => arg.StartsWith("--type=")))
{
CefLoggerWrapper.Error("Invalid process type!");
Environment.Exit(-2);
throw new Exception("Invalid process type!");
}
}

/// <summary>
Expand All @@ -108,6 +99,13 @@ public void Init(ClientControlsActions clientControlsActions, EnginePopupManager
};

//Setup the CEF settings
#if WINDOWS
const string subprocessName = "UnityWebBrowser.Engine.Cef.SubProcess.exe";
#else
const string subprocessName = "UnityWebBrowser.Engine.Cef.SubProcess";
#endif


CefSettings cefSettings = new()
{
WindowlessRenderingEnabled = true,
Expand All @@ -121,12 +119,9 @@ public void Init(ClientControlsActions clientControlsActions, EnginePopupManager
RemoteDebuggingPort = launchArguments.RemoteDebugging,
PersistSessionCookies = true,
PersistUserPreferences = true,
#if LINUX || MACOS
//On Linux we need to tell CEF where everything is, this will assume that the working directory is where everything is!
ResourcesDirPath = Path.Combine(Environment.CurrentDirectory),
LocalesDirPath = Path.Combine(Environment.CurrentDirectory, "locales"),
BrowserSubprocessPath = Environment.ProcessPath
#endif
BrowserSubprocessPath = Path.Combine(Environment.CurrentDirectory, subprocessName)
};

//Init CEF
Expand All @@ -146,7 +141,7 @@ public void Init(ClientControlsActions clientControlsActions, EnginePopupManager
LocalStorage = launchArguments.LocalStorage ? CefState.Enabled : CefState.Disabled
};

Logger.Debug($"{CefLoggerWrapper.FullCefMessageTag} Starting CEF with these options:" +
mainLogger.LogDebug($"Starting CEF with these options:" +
$"\nProcess Path: {Environment.ProcessPath}" +
$"\nJS: {launchArguments.JavaScript}" +
$"\nLocal Storage: {launchArguments.LocalStorage}" +
Expand All @@ -155,21 +150,20 @@ public void Init(ClientControlsActions clientControlsActions, EnginePopupManager
$"\nPopup Action: {launchArguments.PopupAction}" +
$"\nLog Path: {launchArguments.LogPath.FullName}" +
$"\nLog Severity: {launchArguments.LogSeverity}");
Logger.Info($"{CefLoggerWrapper.FullCefMessageTag} Starting CEF client...");
mainLogger.LogInformation($"Starting CEF client...");

//Create cef browser
cefClient = new UwbCefClient(new CefSize(launchArguments.Width, launchArguments.Height),
launchArguments.PopupAction, popupManager,
new ProxySettings(launchArguments.ProxyUsername, launchArguments.ProxyPassword,
launchArguments.ProxyEnabled), clientControlsActions);
cefClient = new UwbCefClient(
new CefSize(launchArguments.Width, launchArguments.Height),
launchArguments.PopupAction,
popupManager,
new ProxySettings(launchArguments.ProxyUsername, launchArguments.ProxyPassword, launchArguments.ProxyEnabled),
clientControlsActions,
mainLogger,
browserConsoleLogger);
CefBrowserHost.CreateBrowser(cefWindowInfo, cefClient, cefBrowserSettings, launchArguments.InitialUrl);
}

public static void PostTask(CefThreadId threadId, Action action)
{
CefRuntime.PostTask(threadId, new CefActionTask(action));
}

#region Engine Actions

[MethodImpl(MethodImplOptions.AggressiveInlining)]
Expand All @@ -186,11 +180,11 @@ public void Shutdown()
//We can only quit the message loop on the UI (main) thread
if (!CefRuntime.CurrentlyOn(CefThreadId.UI))
{
PostTask(CefThreadId.UI, Shutdown);
CefActionTask.PostTask(CefThreadId.UI, Shutdown);
return;
}

Logger.Debug("Quitting message loop...");
mainLogger.LogDebug($"Quitting message loop...");
CefRuntime.QuitMessageLoop();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@

using System;
using System.Linq;
using Microsoft.Extensions.Logging;
using VoltstroStudios.UnityWebBrowser.Engine.Shared.Core;
using VoltstroStudios.UnityWebBrowser.Engine.Shared.Core.Logging;
using Xilium.CefGlue;

namespace UnityWebBrowser.Engine.Cef.Core;
Expand All @@ -17,20 +17,11 @@ namespace UnityWebBrowser.Engine.Cef.Core;
internal class UwbCefEngineEntry : EngineEntryPoint
{
private CefEngineControlsManager cefEngineControlsManager;

protected override bool ShouldInitLogger(LaunchArguments launchArguments, string[] args)
{
return !args.Any(arg => arg.StartsWith("--type="));
}

protected override void EarlyInit(LaunchArguments launchArguments, string[] args)
{
cefEngineControlsManager = new CefEngineControlsManager();
cefEngineControlsManager.EarlyInit(launchArguments, args);
}


protected override void EntryPoint(LaunchArguments launchArguments, string[] args)
{
cefEngineControlsManager = new CefEngineControlsManager(LoggerManagerFactory);
cefEngineControlsManager.EarlyInit(launchArguments, args);
cefEngineControlsManager.Init(ClientControlsActions, PopupManager);

SetupIpc(cefEngineControlsManager, launchArguments);
Expand All @@ -40,7 +31,6 @@ protected override void EntryPoint(LaunchArguments launchArguments, string[] arg
CefRuntime.RunMessageLoop();

//If the message loop quits
Logger.Debug($"{CefLoggerWrapper.FullCefMessageTag} Message loop quit.");
Dispose();
Environment.Exit(0);
}
Expand Down
File renamed without changes.
Loading

0 comments on commit 5c72038

Please sign in to comment.