// 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);
// Seams for the engine's web panel.
private bool PanelAvailable => false;
private bool TryOpenPanel(WebOpenRequest r, out string error)
{
error = "The web panel is not available.";
return false;
}
/// Opens the page (pump thread). `done` gets the route taken and a message without the URL.
internal void OpenWeb(WebOpenRequest r, Action done)
{
WebOpenRoute route = ServerWebOpen.Route(r, PanelAvailable);
if (route == WebOpenRoute.Refused)
{
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)
{
if (TryOpenPanel(r, out string error))
{
done(new WebOpenOutcome(route, true, "Opened in the web panel."));
return;
}
// The panel failed: the browser is the fallback, and the report below says it was the browser.
LogTagged("web panel did not open: " + error);
route = WebOpenRoute.Browser;
}
string url = r.Url;
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).";
}
done(new WebOpenOutcome(WebOpenRoute.Browser, ok, message));
})
{
IsBackground = true,
Name = "ServerPack web open",
};
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
}
/// Closes the engine's web panel if this plugin opened one. Nothing to close until the panel API is wired.
internal void CloseWeb()
{
}
}