RynthServers/Plugins/Shared/ServerPack/ServerPackWeb.cs
Daniel Wallace c085c16e15 ServerPack web open: fall back only before the panel navigates
PanelResult + pure FallbackToBrowser; guard TryOpenPanel throws; guard done callbacks;
hand ShellExecuteW the validated absolute URI.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-08 13:18:45 -08:00

93 lines
3.8 KiB
C#

// ServerPackWeb.cs - the host side of ServerWebOpen: open a server's web page in the engine's web
// panel when it has one, else in the system browser. A server's own code calls OpenWeb on the pump
// (ServerChatCommand, a RunOnTick action); the answer goes to `done`, which may run on a worker
// thread (Reply is any-thread). The answer never contains the URL.
//
// Never Process.Start inside acclient: the browser opens with ShellExecuteW, as the engine does
// (ProblemReport.cs), on its own short-lived STA thread so the shell call never blocks the pump.
// The panel seams (PanelAvailable, TryOpenPanel) are stubs until the engine API is wired.
using System;
using System.Runtime.InteropServices;
using System.Threading;
namespace RynthCore.Plugin.ServerPack;
public sealed partial class ServerPackPlugin
{
[DllImport("shell32.dll", CharSet = CharSet.Unicode, EntryPoint = "ShellExecuteW")]
private static extern IntPtr ShellExecuteW(IntPtr hwnd, string? operation, string file, string? parameters, string? directory, int showCmd);
// Seam for the engine's web panel (a stub until the engine API is wired).
private bool PanelAvailable => false;
private PanelResult TryOpenPanel(WebOpenRequest r) => PanelResult.RefusedBeforeNavigate;
private static void Finish(Action<WebOpenOutcome> done, WebOpenOutcome outcome)
{
try { done(outcome); } catch { }
}
/// <summary>Opens the page (pump thread). `done` gets the route taken and a message without the URL.</summary>
internal void OpenWeb(WebOpenRequest r, Action<WebOpenOutcome> done)
{
WebOpenRoute route = ServerWebOpen.Route(r, PanelAvailable);
if (route == WebOpenRoute.Refused)
{
Finish(done, new WebOpenOutcome(route, false, "That page is not on this server's list of pages, so it was not opened."));
return;
}
if (route == WebOpenRoute.Panel)
{
PanelResult result;
try { result = TryOpenPanel(r); }
catch (Exception) { result = PanelResult.RefusedBeforeNavigate; }
if (result == PanelResult.Opened)
{
Finish(done, new WebOpenOutcome(WebOpenRoute.Panel, true, "Opened in the web panel."));
return;
}
if (!ServerWebOpen.FallbackToBrowser(result))
{
LogTagged("web panel: " + result);
Finish(done, new WebOpenOutcome(WebOpenRoute.Panel, false, "The web panel couldn't open that page."));
return;
}
LogTagged("web panel: " + result + ", using the browser");
}
if (!ServerWebOpen.TryGetAllowedUri(r.Url, r.AllowedHosts, out string url))
{
Finish(done, new WebOpenOutcome(WebOpenRoute.Refused, false, "That page is not on this server's list of pages, so it was not opened."));
return;
}
var thread = new Thread(() =>
{
string message;
bool ok;
try
{
long code = ShellExecuteW(IntPtr.Zero, "open", url, null, null, 1).ToInt64();
ok = code > 32;
message = ok ? "Opened in your browser." : $"Couldn't open your browser (code {code}).";
}
catch (Exception)
{
ok = false;
message = "Couldn't open your browser (code 0).";
}
Finish(done, new WebOpenOutcome(WebOpenRoute.Browser, ok, message));
})
{
IsBackground = true,
Name = "ServerPack web open",
};
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
}
/// <summary>Closes the engine's web panel if this plugin opened one. Nothing to close until the panel API is wired.</summary>
internal void CloseWeb()
{
}
}