using RynthCore.Engine.Plugins;
namespace RynthCore.Engine.WebPanel;
/// What WebPanelGetState reports for one handle.
internal readonly record struct WebPanelSnapshot(int Handle, int Status, uint Flags, int Error, int LastHttpStatus, uint NavigationCount);
///
/// The pure decisions of the v94 ABI glue (WebPanelApi.cs), kept apart so tools\WebPanelTests can check them
/// without the engine: refusals, the GetState argument check, and writing the result struct.
///
internal static unsafe class WebPanelApiRules
{
/// 0 to go on, else the refusal. Shutting down wins over the engine.json "WebPanel" kill switch.
public static int OpenRefusal(bool shuttingDown, bool enabled) =>
shuttingDown ? (int)WebPanelCode.ShuttingDown : enabled ? 0 : (int)WebPanelCode.Unavailable;
/// Navigate, Close and GetState: 0 to go on, else ShuttingDown.
public static int CallRefusal(bool shuttingDown) => shuttingDown ? (int)WebPanelCode.ShuttingDown : 0;
/// OffsetOf(Reserved): the smallest Size a caller's WebPanelStateNative may declare (28 on x86 and x64: the struct has no pointer fields).
public static uint StateMinSize
{
get
{
WebPanelStateNative s = default;
return (uint)((byte*)s.Reserved - (byte*)&s);
}
}
public static bool StateArgsOk(WebPanelStateNative* state) => state != null && state->Size >= StateMinSize;
/// What a handle reads when no panel was ever opened.
public static WebPanelSnapshot NoPanel(int handle) => new(handle, (int)WebPanelStatusCode.Superseded, 0, 0, 0, 0);
/// Writes the fields before Reserved only (the caller's Size covers at least those).
public static void WriteState(WebPanelStateNative* state, in WebPanelSnapshot 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;
}
}