Web panel host: STA thread, WebView2 lifetime, allowlist, focus, visibility; offline harness

WebPanelHost (CoreCLR engine only): one unowned WS_POPUP over the game client area on its own STA thread and GetMessage loop, lifted from the 2026-11-07 spike's popup-free path. Open/Navigate/Close/GetState never block: they validate, update the caller-visible state under a lock, queue work and post WM_APP+1 to the host window. WebView2Loader.dll is loaded by full path from the shim's Runtime folder; the runtime probe on the panel thread decides RuntimeMissing, any later creation failure is CreateFailed. Navigation guard (NavigationStarting incl. redirects, NewWindowRequested, LaunchingExternalUriScheme): off-list http/https goes to the system browser, anything else is dropped; logs carry only Redact()ed URLs or hosts. Escape via AcceleratorKeyPressed unless PageKeepsEscape. Visibility by the PID rule on a 50 ms timer plus a foreground WinEvent; raises only when below the game; layout remembered in layout.json. Shutdown unhooks, closes the controller, destroys the window and unregisters the class before the collectible context unloads.

WebPanelNativeArgs copies every string out of the caller's (stack or pooled) buffers before anything is queued. WebPanelApi is the ABI glue (AOT keeps the Unavailable stub). engine.json "WebPanel" (default true) is the kill switch. EngineLifecycle shuts the panel down after EndSceneHook.Uninstall. The WebView2 package's WinForms/WPF references are dropped before reference resolution.

Deviations from the brief: IWebPanelEnvironment gains BrowserExecutableFolderForTests (the harness forces a creation failure with it) and is excluded from the AOT build with the host; Show only takes the foreground when it is already in this process (or none).

tools\WebPanelHarness (x86 exe) + tools\WebPanelHarness.Payload: fake game thread, fake 16 ms pump, collectible context mirroring EngineLoadContext, local virtual hosts, posted input only. 17 ok, 0 FAIL, 4 UNVERIFIED offline (Escape via posted input never raises AcceleratorKeyPressed; visibility needs the foreground): owner in-game checks.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Daniel Wallace 2026-11-07 15:04:01 -08:00
commit f130be3d2b
14 changed files with 2682 additions and 9 deletions

View file

@ -0,0 +1,63 @@
using System;
using System.IO;
using System.Reflection;
using System.Threading;
using Microsoft.Web.WebView2.Core;
namespace RynthCore.Engine.WebPanel;
/// <summary>The web panel host's surroundings inside acclient.exe (CoreCLR engine only).</summary>
internal sealed class EngineWebPanelEnvironment : IWebPanelEnvironment
{
private readonly string _runtimeDir = FindRuntimeDir();
public IntPtr GameHwnd
{
get
{
IntPtr h = ImGuiBackend.Win32Backend.GameHwnd;
return h != IntPtr.Zero ? h : EntryPoint.GameHwnd;
}
}
/// <summary>
/// Runtime\: the folder of the loaded RynthCore.Shim assembly, as ManagedPlugins.FindSharedDir finds it.
/// Never this assembly's Location: the engine is stream-loaded, so that is empty.
/// </summary>
public string RuntimeDir => _runtimeDir;
public string LocalAppData => Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
public string WorldName => Compatibility.AccountHooks.TryGetWorldName(out string name) ? name : "";
public void Log(string line) => RynthLog.Info("WebPanel: " + line);
public void Chat(string line) => Compatibility.ClientHelperHooks.WriteToChat(line, 1);
public bool OpenInBrowser(string url)
{
if (!WebPanelUrlPolicy.IsBrowserSafe(url)) return false;
IntPtr r = WebPanelNative.ShellExecuteW(IntPtr.Zero, "open", url, null, null, WebPanelNative.SW_SHOWNORMAL);
return r.ToInt64() > 32;
}
public Thread StartStaThread(string name, ThreadStart body) => EngineThreads.Start(name, body, ApartmentState.STA);
public Action<CoreWebView2>? ConfigureForTests => null;
public int EnvironmentDelayMsForTests => 0;
public string? BrowserExecutableFolderForTests => null;
private static string FindRuntimeDir()
{
try
{
foreach (Assembly a in AppDomain.CurrentDomain.GetAssemblies())
{
if (a.GetName().Name != "RynthCore.Shim" || string.IsNullOrEmpty(a.Location)) continue;
return Path.GetDirectoryName(a.Location) ?? "";
}
}
catch { }
return "";
}
}