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

@ -2,18 +2,139 @@ using RynthCore.Engine.Plugins;
namespace RynthCore.Engine.WebPanel;
#if ENGINE_CORECLR
/// <summary>
/// The engine side of the v93 web panel calls (PluginContract.cs WebPanelOpenFn..WebPanelGetStateFn).
/// Task 2 stub: every call answers Unavailable; the WebView2 host arrives in a later task.
/// No call here may block its caller (AC's main thread, the plugin pump and ImGui draws all call these).
/// The engine side of the v93 web panel calls (PluginContract.cs WebPanelOpenFn..WebPanelGetStateFn): reads
/// the native arguments (every string copied before the call returns) and hands them to the one
/// WebPanelHost. No call here blocks its caller (AC's main thread, the plugin pump and ImGui draws all call
/// these). engine.json "WebPanel": false makes Open answer Unavailable (plugins fall back to the browser).
/// </summary>
internal static class WebPanelApi
internal static unsafe class WebPanelApi
{
public static unsafe int Open(WebPanelOpenNative* open) => (int)WebPanelCode.Unavailable;
private const int ShutdownBudgetMs = 2000;
private static readonly object Sync = new();
private static WebPanelHost? s_host;
private static EngineWebPanelEnvironment? s_env;
private static volatile bool s_shutdown;
public static unsafe int Navigate(int handle, byte* url) => (int)WebPanelCode.Unavailable;
internal static int Open(WebPanelOpenNative* open)
{
try
{
if (s_shutdown || EngineLifecycle.IsShuttingDown) return (int)WebPanelCode.ShuttingDown;
if (!EngineSettings.WebPanel) return (int)WebPanelCode.Unavailable;
EngineWebPanelEnvironment env;
WebPanelHost host;
lock (Sync)
{
if (s_shutdown) return (int)WebPanelCode.ShuttingDown;
env = s_env ??= new EngineWebPanelEnvironment();
host = s_host ??= new WebPanelHost(env);
}
int rc = WebPanelNativeArgs.TryBuildRequest(open, env.LocalAppData, env.WorldName, out WebPanelRequest? request);
if (rc != 0) return rc;
return host.Open(request!);
}
catch (System.Exception ex)
{
RynthLog.Info("WebPanel: Open failed: " + ex.GetType().Name);
return (int)WebPanelCode.Unavailable;
}
}
public static int Close(int handle) => (int)WebPanelCode.Unavailable;
internal static int Navigate(int handle, byte* urlUtf8)
{
try
{
if (s_shutdown) return (int)WebPanelCode.ShuttingDown;
if (!WebPanelNativeArgs.TryReadUtf8(urlUtf8, WebPanelUrlPolicy.MaxUrlBytes, true, out string url)) return (int)WebPanelCode.BadArgs;
WebPanelHost? host = s_host;
return host == null ? (int)WebPanelCode.StaleHandle : host.Navigate(handle, url);
}
catch (System.Exception ex)
{
RynthLog.Info("WebPanel: Navigate failed: " + ex.GetType().Name);
return (int)WebPanelCode.Unavailable;
}
}
public static unsafe int GetState(int handle, WebPanelStateNative* state) => (int)WebPanelCode.Unavailable;
internal static int Close(int handle)
{
try
{
if (s_shutdown) return (int)WebPanelCode.ShuttingDown;
WebPanelHost? host = s_host;
return host == null ? (int)WebPanelCode.StaleHandle : host.Close(handle);
}
catch (System.Exception ex)
{
RynthLog.Info("WebPanel: Close failed: " + ex.GetType().Name);
return (int)WebPanelCode.Unavailable;
}
}
internal static int GetState(int handle, WebPanelStateNative* state)
{
try
{
if (state == null || state->Size < (uint)((byte*)state->Reserved - (byte*)state)) return (int)WebPanelCode.BadArgs;
if (s_shutdown) return (int)WebPanelCode.ShuttingDown;
WebPanelHost? host = s_host;
WebPanelSnapshot s;
int r;
if (host == null)
{
s = new WebPanelSnapshot(handle, (int)WebPanelStatusCode.Superseded, 0, 0, 0, 0);
r = (int)WebPanelStatusCode.Superseded;
}
else
{
r = host.GetState(handle, out s);
}
state->Handle = s.Handle;
state->Status = s.Status;
state->Flags = s.Flags;
state->Error = s.Error;
state->LastHttpStatus = s.LastHttpStatus;
state->NavigationCount = s.NavigationCount;
return r;
}
catch (System.Exception ex)
{
RynthLog.Info("WebPanel: GetState failed: " + ex.GetType().Name);
return (int)WebPanelCode.Unavailable;
}
}
/// <summary>EngineLifecycle, after the plugins' own Shutdowns (they may still Close). Idempotent.</summary>
internal static void Shutdown()
{
WebPanelHost? host;
lock (Sync)
{
s_shutdown = true;
host = s_host;
}
host?.Shutdown(ShutdownBudgetMs);
}
/// <summary>Redacted one-liner (no query, fragment or userinfo).</summary>
internal static string Describe()
{
WebPanelHost? host = s_host;
string enabled = EngineSettings.WebPanel ? "on" : "off (engine.json WebPanel)";
return host == null ? "web panel: " + enabled + ", not started" : host.Describe() + "; setting " + enabled;
}
}
#else
/// <summary>The NativeAOT engine has no web panel: PluginManager leaves the v93 slots zero (HasWebPanel false).</summary>
internal static unsafe class WebPanelApi
{
internal static int Open(WebPanelOpenNative* open) => (int)WebPanelCode.Unavailable;
internal static int Navigate(int handle, byte* url) => (int)WebPanelCode.Unavailable;
internal static int Close(int handle) => (int)WebPanelCode.Unavailable;
internal static int GetState(int handle, WebPanelStateNative* state) => (int)WebPanelCode.Unavailable;
internal static void Shutdown() { }
internal static string Describe() => "web panel: not available in this engine build";
}
#endif