diff --git a/src/RynthCore.Engine/Plugins/PluginContract.cs b/src/RynthCore.Engine/Plugins/PluginContract.cs index 8ad0b04..1e2d842 100644 --- a/src/RynthCore.Engine/Plugins/PluginContract.cs +++ b/src/RynthCore.Engine/Plugins/PluginContract.cs @@ -1069,6 +1069,74 @@ internal struct RynthCoreAPI /// reading, 0 without (never read; *ratio = -1, *ageMs = 0xFFFFFFFF). Either pointer may be null. /// Read-only, any thread, no game calls. Requires API v93+. public IntPtr GetMonsterHealthFn; + + //── v93: a WebView2 web panel over the game window (WebPanel\WebPanelApi.cs) ── + // No call blocks its caller (AC's main thread, the plugin pump and ImGui draws all call these). + + /// v93: int WebPanelOpen(WebPanelOpenNative* open): opens (or replaces) the web panel. + /// Returns a handle > 0, or BadArgs / UrlNotAllowed / NoGameWindow / Unavailable / ShuttingDown + /// (WebPanelCode). Requires API v93+ (provisional number). + public IntPtr WebPanelOpenFn; + /// v93: int WebPanelNavigate(int handle, byte* url): 1 queued; -2 off the allowlist + /// (opened in the system browser when it is browser-safe); -6 stale handle. + public IntPtr WebPanelNavigateFn; + /// v93: int WebPanelClose(int handle): 1 queued, 0 already closed, -6 stale handle. + public IntPtr WebPanelCloseFn; + /// v93: int WebPanelGetState(int handle, WebPanelStateNative* state): the WebPanelStatusCode + /// (a handle that is not the live one reads 6 Superseded), -1 bad args. + public IntPtr WebPanelGetStateFn; +} + +/// v93 WebPanelOpen argument (Pack 4; 48 bytes on x86). Mirrors RynthCore.PluginSdk/WebPanelTypes.cs. +[StructLayout(LayoutKind.Sequential, Pack = 4)] +internal unsafe struct WebPanelOpenNative +{ + /// sizeof the caller's struct; the engine accepts Size >= OffsetOf(Reserved) (32 on x86). + public uint Size; + /// WebPanelFlags; unknown bits ignored. + public uint Flags; + /// UTF-8 NUL-terminated, at most 4096 bytes, must pass the URL policy's IsAllowed. + public byte* Url; + /// UTF-8, '\n'-separated allowlist (WebPanelUrlPolicy.TryParseAllowlist). + public byte* Allowlist; + /// UTF-8, may be null; at most 256 bytes. + public byte* ProfileKey; + /// UTF-8, may be null; at most 128 bytes. + public byte* Title; + /// 0 = 88, else 30..100 (only when no remembered layout). + public int WidthPercent; + public int HeightPercent; + public fixed uint Reserved[4]; +} + +/// v93 WebPanelGetState result (Pack 4; 48 bytes). Mirrors RynthCore.PluginSdk/WebPanelTypes.cs. +[StructLayout(LayoutKind.Sequential, Pack = 4)] +internal unsafe struct WebPanelStateNative +{ + public uint Size; + public int Handle; + /// WebPanelStatusCode. + public int Status; + /// 1 Visible, 2 Focused, 4 OpenedInBrowser. + public uint Flags; + /// 0 or a negative WebPanelCode (RuntimeMissing / CreateFailed / ProcessFailed). + public int Error; + public int LastHttpStatus; + public uint NavigationCount; + public fixed uint Reserved[5]; +} + +/// v93 return codes of the web panel calls. Append only. +internal enum WebPanelCode +{ + Queued = 1, AlreadyClosed = 0, BadArgs = -1, UrlNotAllowed = -2, NoGameWindow = -3, + Unavailable = -4, ShuttingDown = -5, StaleHandle = -6, RuntimeMissing = -7, CreateFailed = -8, ProcessFailed = -9, +} + +/// v93 WebPanelStateNative.Status values. Append only. +internal enum WebPanelStatusCode +{ + None = 0, Starting = 1, Loading = 2, Ready = 3, Closed = 4, Failed = 5, Superseded = 6, } /// v89 GetUiColor tokens. Append only: plugins compiled against these numbers keep them. @@ -1930,6 +1998,18 @@ internal unsafe delegate int GetAmbientLightCallbackDelegate(uint* client, uint* [UnmanagedFunctionPointer(CallingConvention.Cdecl)] internal unsafe delegate int GetMonsterHealthCallbackDelegate(uint id, float* ratio, uint* ageMs); +[UnmanagedFunctionPointer(CallingConvention.Cdecl)] +internal unsafe delegate int WebPanelOpenCallbackDelegate(WebPanelOpenNative* open); + +[UnmanagedFunctionPointer(CallingConvention.Cdecl)] +internal unsafe delegate int WebPanelNavigateCallbackDelegate(int handle, byte* url); + +[UnmanagedFunctionPointer(CallingConvention.Cdecl)] +internal delegate int WebPanelCloseCallbackDelegate(int handle); + +[UnmanagedFunctionPointer(CallingConvention.Cdecl)] +internal unsafe delegate int WebPanelGetStateCallbackDelegate(int handle, WebPanelStateNative* state); + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] internal unsafe delegate uint VendorBuyCallbackDelegate(uint vendorId, VendorTradeEntryNative* entries, int count); diff --git a/src/RynthCore.Engine/Plugins/PluginContractVersion.cs b/src/RynthCore.Engine/Plugins/PluginContractVersion.cs index 627dcad..774d6ba 100644 --- a/src/RynthCore.Engine/Plugins/PluginContractVersion.cs +++ b/src/RynthCore.Engine/Plugins/PluginContractVersion.cs @@ -47,5 +47,8 @@ internal static class PluginContractVersion // any thread (feature/hud-health-for-plugins, 2026-11-07). Note: feature/reload-state, // feature/engine-events and the branches built on them (unmerged) also use 93 and 94; whichever // merges second renumbers. + // v93: WebPanelOpen, WebPanelNavigate, WebPanelClose and WebPanelGetState: a WebView2 web panel over the + // game window (WebPanel\WebPanelApi.cs; DreamWeave's in-game market and tools). The number is + // PROVISIONAL: upstream may take 93 first, in which case this renumbers on merge. public const uint Current = 93; } diff --git a/src/RynthCore.Engine/Plugins/PluginManager.cs b/src/RynthCore.Engine/Plugins/PluginManager.cs index d939a73..e12b819 100644 --- a/src/RynthCore.Engine/Plugins/PluginManager.cs +++ b/src/RynthCore.Engine/Plugins/PluginManager.cs @@ -304,6 +304,12 @@ internal static partial class PluginManager private static SetAmbientLightFloorCallbackDelegate? _setAmbientLightFloorCallback; // v91 private static GetAmbientLightCallbackDelegate? _getAmbientLightCallback; // v91 private static GetMonsterHealthCallbackDelegate? _getMonsterHealthCallback; // v93 +#if ENGINE_CORECLR + private static WebPanelOpenCallbackDelegate? _webPanelOpenCallback; // v93 + private static WebPanelNavigateCallbackDelegate? _webPanelNavigateCallback; // v93 + private static WebPanelCloseCallbackDelegate? _webPanelCloseCallback; // v93 + private static WebPanelGetStateCallbackDelegate? _webPanelGetStateCallback; // v93 +#endif private static ForceResetBusyCountCallbackDelegate? _forceResetBusyCountCallback; private static GetObjectSpellIdsCallbackDelegate? _getObjectSpellIdsCallback; private static GetObjectSkillLevelCallbackDelegate? _getObjectSkillBuffedCallback; @@ -3254,6 +3260,13 @@ internal static partial class PluginManager _setAmbientLightFloorCallback ??= AmbientLight.SetFloor; // v91 unsafe { _getAmbientLightCallback ??= AmbientLight.Get; } unsafe { _getMonsterHealthCallback ??= GetMonsterHealthAction; } // v93 +#if ENGINE_CORECLR + // v93: the AOT engine leaves these slots zero, so HasWebPanel is false there. + unsafe { _webPanelOpenCallback ??= WebPanel.WebPanelApi.Open; } + unsafe { _webPanelNavigateCallback ??= WebPanel.WebPanelApi.Navigate; } + _webPanelCloseCallback ??= WebPanel.WebPanelApi.Close; + unsafe { _webPanelGetStateCallback ??= WebPanel.WebPanelApi.GetState; } +#endif _api.Version = PluginContractVersion.Current; _api.LogFn = Marshal.GetFunctionPointerForDelegate(_logCallback); @@ -3427,6 +3440,12 @@ internal static partial class PluginManager _api.SetAmbientLightFloorFn = Marshal.GetFunctionPointerForDelegate(_setAmbientLightFloorCallback); _api.GetAmbientLightFn = Marshal.GetFunctionPointerForDelegate(_getAmbientLightCallback); _api.GetMonsterHealthFn = Marshal.GetFunctionPointerForDelegate(_getMonsterHealthCallback); +#if ENGINE_CORECLR + _api.WebPanelOpenFn = Marshal.GetFunctionPointerForDelegate(_webPanelOpenCallback!); + _api.WebPanelNavigateFn = Marshal.GetFunctionPointerForDelegate(_webPanelNavigateCallback!); + _api.WebPanelCloseFn = Marshal.GetFunctionPointerForDelegate(_webPanelCloseCallback!); + _api.WebPanelGetStateFn = Marshal.GetFunctionPointerForDelegate(_webPanelGetStateCallback!); +#endif RouteApiThroughLoader(); } diff --git a/src/RynthCore.Engine/WebPanel/WebPanelApi.cs b/src/RynthCore.Engine/WebPanel/WebPanelApi.cs new file mode 100644 index 0000000..ae2c011 --- /dev/null +++ b/src/RynthCore.Engine/WebPanel/WebPanelApi.cs @@ -0,0 +1,19 @@ +using RynthCore.Engine.Plugins; + +namespace RynthCore.Engine.WebPanel; + +/// +/// 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). +/// +internal static class WebPanelApi +{ + public static unsafe int Open(WebPanelOpenNative* open) => (int)WebPanelCode.Unavailable; + + public static unsafe int Navigate(int handle, byte* url) => (int)WebPanelCode.Unavailable; + + public static int Close(int handle) => (int)WebPanelCode.Unavailable; + + public static unsafe int GetState(int handle, WebPanelStateNative* state) => (int)WebPanelCode.Unavailable; +} diff --git a/src/RynthCore.Engine/WebPanel/WebPanelProfile.cs b/src/RynthCore.Engine/WebPanel/WebPanelProfile.cs index f875638..b09ca79 100644 --- a/src/RynthCore.Engine/WebPanel/WebPanelProfile.cs +++ b/src/RynthCore.Engine/WebPanel/WebPanelProfile.cs @@ -1,3 +1,5 @@ +using System.IO; +using System; using System.Security.Cryptography; using System.Text; diff --git a/src/RynthCore.Engine/WebPanel/WebPanelUrlPolicy.cs b/src/RynthCore.Engine/WebPanel/WebPanelUrlPolicy.cs index b572b54..230bd9b 100644 --- a/src/RynthCore.Engine/WebPanel/WebPanelUrlPolicy.cs +++ b/src/RynthCore.Engine/WebPanel/WebPanelUrlPolicy.cs @@ -1,3 +1,5 @@ +using System; +using System.Collections.Generic; using System.Text; namespace RynthCore.Engine.WebPanel; diff --git a/src/RynthCore.Engine/WebPanel/WebPanelWindowPolicy.cs b/src/RynthCore.Engine/WebPanel/WebPanelWindowPolicy.cs index 654edaa..405eaac 100644 --- a/src/RynthCore.Engine/WebPanel/WebPanelWindowPolicy.cs +++ b/src/RynthCore.Engine/WebPanel/WebPanelWindowPolicy.cs @@ -1,3 +1,4 @@ +using System; namespace RynthCore.Engine.WebPanel; internal readonly record struct PanelRect(int Left, int Top, int Right, int Bottom) diff --git a/src/RynthCore.PluginSdk/RynthCoreApiNative.cs b/src/RynthCore.PluginSdk/RynthCoreApiNative.cs index 7a81114..fc6031d 100644 --- a/src/RynthCore.PluginSdk/RynthCoreApiNative.cs +++ b/src/RynthCore.PluginSdk/RynthCoreApiNative.cs @@ -204,6 +204,11 @@ public struct RynthCoreApiNative public IntPtr GetAmbientLightFn; // int GetAmbientLight(uint* client, uint* applied) -> 1 hook live, 0 not // v93: the monster plates' last server health reading per object public IntPtr GetMonsterHealthFn; // int GetMonsterHealth(uint id, float* ratio, uint* ageMs) -> 1 reading, 0 none + // v93: a WebView2 web panel over the game window (APPEND-AT-END (ABI); number provisional) + public IntPtr WebPanelOpenFn; // int WebPanelOpen(WebPanelOpenNative*) -> handle > 0, or BadArgs/UrlNotAllowed/NoGameWindow/Unavailable/ShuttingDown + public IntPtr WebPanelNavigateFn; // int WebPanelNavigate(int handle, byte* url) -> 1 queued; -2 off-list; -6 stale + public IntPtr WebPanelCloseFn; // int WebPanelClose(int handle) -> 1 queued, 0 already closed, -6 stale + public IntPtr WebPanelGetStateFn; // int WebPanelGetState(int handle, WebPanelStateNative*) -> Status (non-live handle -> 6 Superseded), -1 bad args } /// diff --git a/src/RynthCore.PluginSdk/RynthCoreHost.cs b/src/RynthCore.PluginSdk/RynthCoreHost.cs index 00fc926..223bf4d 100644 --- a/src/RynthCore.PluginSdk/RynthCoreHost.cs +++ b/src/RynthCore.PluginSdk/RynthCoreHost.cs @@ -1877,6 +1877,119 @@ public readonly unsafe struct RynthCoreHost return true; } + // ── v93: a web panel over the game window (number provisional) ─────────── + + /// + /// v93: , , and + /// . True only on an engine at API 93 or newer that fills all four. + /// + public bool HasWebPanel => _api.Version >= 93 && _api.WebPanelOpenFn != IntPtr.Zero && _api.WebPanelNavigateFn != IntPtr.Zero + && _api.WebPanelCloseFn != IntPtr.Zero && _api.WebPanelGetStateFn != IntPtr.Zero; + + /// + /// v93: opens the web panel over the game window (a second call replaces the first). Returns a handle + /// > 0, or a (BadArgs for an empty URL or allowlist, more than 16 + /// entries or a newline inside one; Unavailable on an older engine). Never blocks; the panel loads + /// after this returns - poll . Any thread. + /// + public int OpenWebPanel(WebPanelOptions options) + { + if (options == null || string.IsNullOrEmpty(options.Url) || options.Allowlist == null + || options.Allowlist.Count < 1 || options.Allowlist.Count > 16) + return (int)WebPanelResult.BadArgs; + foreach (string entry in options.Allowlist) + if (string.IsNullOrEmpty(entry) || entry.Contains('\n')) return (int)WebPanelResult.BadArgs; + if (!HasWebPanel) return (int)WebPanelResult.Unavailable; + + string allowlist = string.Join("\n", options.Allowlist); + string profile = options.ProfileKey ?? ""; + string title = options.Title ?? ""; + var enc = System.Text.Encoding.UTF8; + int nUrl = enc.GetByteCount(options.Url), nAllow = enc.GetByteCount(allowlist); + int nProfile = enc.GetByteCount(profile), nTitle = enc.GetByteCount(title); + int total = nUrl + nAllow + nProfile + nTitle + 4; + + byte[]? rented = null; + Span buf = total <= 1024 ? stackalloc byte[total] : (rented = System.Buffers.ArrayPool.Shared.Rent(total)).AsSpan(0, total); + try + { + int o = 0; + int oUrl = o; o += enc.GetBytes(options.Url, buf[o..]); buf[o++] = 0; + int oAllow = o; o += enc.GetBytes(allowlist, buf[o..]); buf[o++] = 0; + int oProfile = o; o += enc.GetBytes(profile, buf[o..]); buf[o++] = 0; + int oTitle = o; o += enc.GetBytes(title, buf[o..]); buf[o++] = 0; + fixed (byte* b = buf) + { + var open = new WebPanelOpenNative + { + Size = (uint)sizeof(WebPanelOpenNative), + Flags = (uint)options.Flags, + Url = b + oUrl, + Allowlist = b + oAllow, + ProfileKey = nProfile == 0 ? null : b + oProfile, + Title = nTitle == 0 ? null : b + oTitle, + WidthPercent = options.WidthPercent, + HeightPercent = options.HeightPercent, + }; + return ((delegate* unmanaged[Cdecl])_api.WebPanelOpenFn)(&open); + } + } + finally + { + if (rented != null) System.Buffers.ArrayPool.Shared.Return(rented); + } + } + + /// + /// v93: points the panel at . 1 queued; -2 + /// (UrlNotAllowed) when it is off the allowlist (the engine opens it in the system browser when that is + /// safe); -6 (StaleHandle) when the handle is not the live panel. Never blocks. Any thread. + /// + public int NavigateWebPanel(int handle, string url) + { + if (string.IsNullOrEmpty(url)) return (int)WebPanelResult.BadArgs; + if (!HasWebPanel) return (int)WebPanelResult.Unavailable; + int n = System.Text.Encoding.UTF8.GetByteCount(url); + byte[]? rented = null; + Span buf = n + 1 <= 1024 ? stackalloc byte[n + 1] : (rented = System.Buffers.ArrayPool.Shared.Rent(n + 1)).AsSpan(0, n + 1); + try + { + System.Text.Encoding.UTF8.GetBytes(url, buf); + buf[n] = 0; + fixed (byte* p = buf) + return ((delegate* unmanaged[Cdecl])_api.WebPanelNavigateFn)(handle, p); + } + finally + { + if (rented != null) System.Buffers.ArrayPool.Shared.Return(rented); + } + } + + /// v93: closes the panel . 1 queued, 0 already closed, -6 stale handle. Never blocks. Any thread. + public int CloseWebPanel(int handle) + { + if (!HasWebPanel) return (int)WebPanelResult.Unavailable; + return ((delegate* unmanaged[Cdecl])_api.WebPanelCloseFn)(handle); + } + + /// + /// v93: where the panel is. A handle that is not the live panel reads + /// Superseded. None (and a default ) on an older engine or bad arguments. + /// Any thread. + /// + public WebPanelStatus GetWebPanelState(int handle, out WebPanelState state) + { + state = default; + if (!HasWebPanel) return WebPanelStatus.None; + WebPanelStateNative native = default; + native.Size = (uint)sizeof(WebPanelStateNative); + int r = ((delegate* unmanaged[Cdecl])_api.WebPanelGetStateFn)(handle, &native); + if (r < 0) return WebPanelStatus.None; + var status = (WebPanelStatus)native.Status; + state = new WebPanelState(native.Handle, status, (WebPanelStateFlags)native.Flags, (WebPanelResult)native.Error, native.LastHttpStatus, native.NavigationCount); + return status; + } + private int CallSetActionKey(string action, string key, int op, out string message) { message = string.Empty; diff --git a/src/RynthCore.PluginSdk/WebPanelTypes.cs b/src/RynthCore.PluginSdk/WebPanelTypes.cs new file mode 100644 index 0000000..b05d4e0 --- /dev/null +++ b/src/RynthCore.PluginSdk/WebPanelTypes.cs @@ -0,0 +1,95 @@ +using System; +using System.Collections.Generic; +using System.Runtime.InteropServices; + +namespace RynthCore.PluginSdk; + +// v93: a WebView2 web panel over the game window. The native structs mirror +// RynthCore.Engine/Plugins/PluginContract.cs field for field (tools\PluginManifestTests checks sizes and +// offsets). The API number is provisional. + +/// v93 WebPanelOpen argument (Pack 4; 48 bytes on x86). Mirrors the engine's WebPanelOpenNative. +[StructLayout(LayoutKind.Sequential, Pack = 4)] +public unsafe struct WebPanelOpenNative +{ + /// sizeof this struct; the engine accepts Size >= OffsetOf(Reserved) (32 on x86). + public uint Size; + /// ; unknown bits ignored. + public uint Flags; + /// UTF-8 NUL-terminated, at most 4096 bytes. + public byte* Url; + /// UTF-8, '\n'-separated allowlist. + public byte* Allowlist; + /// UTF-8, may be null; at most 256 bytes. + public byte* ProfileKey; + /// UTF-8, may be null; at most 128 bytes. + public byte* Title; + /// 0 = 88, else 30..100 (only when no remembered layout). + public int WidthPercent; + public int HeightPercent; + public fixed uint Reserved[4]; +} + +/// v93 WebPanelGetState result (Pack 4; 48 bytes). Mirrors the engine's WebPanelStateNative. +[StructLayout(LayoutKind.Sequential, Pack = 4)] +public unsafe struct WebPanelStateNative +{ + public uint Size; + public int Handle; + /// . + public int Status; + /// . + public uint Flags; + /// 0 or a negative (RuntimeMissing / CreateFailed / ProcessFailed). + public int Error; + public int LastHttpStatus; + public uint NavigationCount; + public fixed uint Reserved[5]; +} + +/// Options for . +[Flags] +public enum WebPanelFlags : uint +{ + None = 0, + /// When the panel cannot open (no WebView2 runtime, creation failed), open the URL in the system browser. + FallbackToBrowser = 1, + /// Escape goes to the page instead of closing the panel. + PageKeepsEscape = 2, + // 4 is reserved: phase 2 EnableMessages. +} + +/// Results of the web panel calls; an open that succeeds returns a handle > 0 instead. +public enum WebPanelResult : int +{ + Queued = 1, AlreadyClosed = 0, BadArgs = -1, UrlNotAllowed = -2, NoGameWindow = -3, + Unavailable = -4, ShuttingDown = -5, StaleHandle = -6, RuntimeMissing = -7, CreateFailed = -8, ProcessFailed = -9, +} + +/// Where the web panel is in its life. +public enum WebPanelStatus : int +{ + None = 0, Starting = 1, Loading = 2, Ready = 3, Closed = 4, Failed = 5, Superseded = 6, +} + +[Flags] +public enum WebPanelStateFlags : uint +{ + None = 0, Visible = 1, Focused = 2, OpenedInBrowser = 4, +} + +/// A snapshot of the web panel from . +public readonly record struct WebPanelState(int Handle, WebPanelStatus Status, WebPanelStateFlags Flags, WebPanelResult Error, int LastHttpStatus, uint NavigationCount); + +public sealed class WebPanelOptions +{ + public required string Url { get; init; } + /// 1..16 entries, no '\n' inside an entry. + public required IReadOnlyList Allowlist { get; init; } + /// Opaque; the engine lower-cases, hashes and adds the world. + public string ProfileKey { get; init; } = ""; + public string Title { get; init; } = ""; + public int WidthPercent { get; init; } = 0; + public int HeightPercent { get; init; } = 0; + public WebPanelFlags Flags { get; init; } = WebPanelFlags.FallbackToBrowser; +} diff --git a/tools/PluginManifestTests/Program.cs b/tools/PluginManifestTests/Program.cs index 93b6dc3..c083089 100644 --- a/tools/PluginManifestTests/Program.cs +++ b/tools/PluginManifestTests/Program.cs @@ -59,6 +59,10 @@ internal static class Program Run(nameof(SdkApiVersionCannotDrift), SdkApiVersionCannotDrift); Run(nameof(TrainingStructsMirror), TrainingStructsMirror); Run(nameof(ContainerOrderSlots), ContainerOrderSlots); + Run(nameof(WebPanelFieldsAppended), WebPanelFieldsAppended); + Run(nameof(WebPanelStructsMatch), WebPanelStructsMatch); + Run(nameof(WebPanelCodesMatch), WebPanelCodesMatch); + Run(nameof(HasWebPanel_FalseOnOldOrPartialTable), HasWebPanel_FalseOnOldOrPartialTable); Run(nameof(ManifestAttributeDrivesRuntimeMinimum), ManifestAttributeDrivesRuntimeMinimum); Run(nameof(DedupeSamePathTwice), DedupeSamePathTwice); Run(nameof(DedupeCaseAndSlashes), DedupeCaseAndSlashes); @@ -589,6 +593,91 @@ internal static class Program "targets: the manifest and the runtime attribute both use the derived minimum"); } + // v93: the web panel's four slots are appended right after GetMonsterHealthFn, in order, on both sides. + private static void WebPanelFieldsAppended() + { + string[] slots = { "WebPanelOpenFn", "WebPanelNavigateFn", "WebPanelCloseFn", "WebPanelGetStateFn" }; + string prev = "GetMonsterHealthFn"; + foreach (string slot in slots) + { + Eq((int)Marshal.OffsetOf(slot), + (int)Marshal.OffsetOf(prev) + IntPtr.Size, "engine " + slot + " right after " + prev); + Eq((int)Marshal.OffsetOf(slot), + (int)Marshal.OffsetOf(prev) + IntPtr.Size, "SDK " + slot + " right after " + prev); + prev = slot; + } + Eq(typeof(RynthCore.Engine.Plugins.RynthCoreAPI).GetFields().Select(f => f.Name).SkipLast(4).Last(), "GetMonsterHealthFn", "engine table: GetMonsterHealthFn precedes the four"); + Eq(typeof(RynthCore.PluginSdk.RynthCoreApiNative).GetFields().Select(f => f.Name).SkipLast(4).Last(), "GetMonsterHealthFn", "SDK table: GetMonsterHealthFn precedes the four"); + } + + private static void StructMirror(Type engine, Type sdk, int size) + { + Eq(Marshal.SizeOf(engine), size, "engine " + engine.Name + " size"); + Eq(Marshal.SizeOf(sdk), size, "SDK " + sdk.Name + " size"); + var ef = engine.GetFields().Select(f => f.Name).ToArray(); + var sf = sdk.GetFields().Select(f => f.Name).ToArray(); + Check(ef.SequenceEqual(sf), engine.Name + " field names/order match: " + string.Join(",", ef) + " vs " + string.Join(",", sf)); + foreach (string f in ef) + Eq((int)Marshal.OffsetOf(sdk, f), (int)Marshal.OffsetOf(engine, f), engine.Name + "." + f + " offset"); + } + + // v93: the SDK's WebPanelOpenNative/WebPanelStateNative are the engine's, byte for byte. + private static void WebPanelStructsMatch() + { + int p = IntPtr.Size; + StructMirror(typeof(RynthCore.Engine.Plugins.WebPanelOpenNative), typeof(RynthCore.PluginSdk.WebPanelOpenNative), 8 + 4 * p + 8 + 16); + StructMirror(typeof(RynthCore.Engine.Plugins.WebPanelStateNative), typeof(RynthCore.PluginSdk.WebPanelStateNative), 48); + Eq((int)Marshal.OffsetOf("Reserved"), 8 + 4 * p + 8, "engine WebPanelOpenNative.Reserved offset (32 on x86)"); + } + + // v93: every code value agrees between the engine's enums and the SDK's. + private static void WebPanelCodesMatch() + { + Dictionary Vals(Type t) => Enum.GetNames(t).ToDictionary(n => n, n => Convert.ToInt64(Enum.Parse(t, n))); + var engine = Vals(typeof(RynthCore.Engine.Plugins.WebPanelCode)); + var sdk = Vals(typeof(RynthCore.PluginSdk.WebPanelResult)); + Eq(sdk.Count, engine.Count, "WebPanelCode vs WebPanelResult member count"); + foreach (var kv in engine) + Check(sdk.TryGetValue(kv.Key, out long v) && v == kv.Value, "WebPanelResult." + kv.Key + " = " + kv.Value); + var es = Vals(typeof(RynthCore.Engine.Plugins.WebPanelStatusCode)); + var ss = Vals(typeof(RynthCore.PluginSdk.WebPanelStatus)); + Eq(ss.Count, es.Count, "WebPanelStatusCode vs WebPanelStatus member count"); + foreach (var kv in es) + Check(ss.TryGetValue(kv.Key, out long v) && v == kv.Value, "WebPanelStatus." + kv.Key + " = " + kv.Value); + Eq((uint)RynthCore.PluginSdk.WebPanelStateFlags.Visible, 1u, "state flag Visible"); + Eq((uint)RynthCore.PluginSdk.WebPanelStateFlags.Focused, 2u, "state flag Focused"); + Eq((uint)RynthCore.PluginSdk.WebPanelStateFlags.OpenedInBrowser, 4u, "state flag OpenedInBrowser"); + Eq((uint)RynthCore.PluginSdk.WebPanelFlags.FallbackToBrowser, 1u, "open flag FallbackToBrowser"); + Eq((uint)RynthCore.PluginSdk.WebPanelFlags.PageKeepsEscape, 2u, "open flag PageKeepsEscape"); + } + + // v93: HasWebPanel needs Version >= 93 AND all four slots (an old engine's table is shorter and the + // runtime copies the full new-size struct, so slots past its end are garbage, not zero). + private static void HasWebPanel_FalseOnOldOrPartialTable() + { + IntPtr x = (IntPtr)0xDEAD; + RynthCoreHost Host(uint version, IntPtr a, IntPtr b, IntPtr c, IntPtr d) + => new RynthCoreHost(new RynthCoreApiNative { Version = version, WebPanelOpenFn = a, WebPanelNavigateFn = b, WebPanelCloseFn = c, WebPanelGetStateFn = d }); + Check(!Host(92, x, x, x, x).HasWebPanel, "Version 92 with garbage slots: false"); + Check(!Host(93, IntPtr.Zero, x, x, x).HasWebPanel, "Open zero: false"); + Check(!Host(93, x, IntPtr.Zero, x, x).HasWebPanel, "Navigate zero: false"); + Check(!Host(93, x, x, IntPtr.Zero, x).HasWebPanel, "Close zero: false"); + Check(!Host(93, x, x, x, IntPtr.Zero).HasWebPanel, "GetState zero: false"); + Check(Host(93, x, x, x, x).HasWebPanel, "Version 93, all four: true"); + var old = Host(92, x, x, x, x); + Eq(old.OpenWebPanel(new WebPanelOptions { Url = "https://a.example/", Allowlist = new[] { "a.example" } }), (int)WebPanelResult.Unavailable, "Open without the API: Unavailable"); + Eq(old.NavigateWebPanel(1, "https://a.example/"), (int)WebPanelResult.Unavailable, "Navigate without the API: Unavailable"); + Eq(old.CloseWebPanel(1), (int)WebPanelResult.Unavailable, "Close without the API: Unavailable"); + Eq(old.GetWebPanelState(1, out var st), WebPanelStatus.None, "GetState without the API: None"); + Eq(st.Handle, 0, "GetState without the API: default state"); + // Bad arguments are refused before the engine is called (the slots hold garbage, so a call would crash). + var live = Host(93, x, x, x, x); + Eq(live.OpenWebPanel(new WebPanelOptions { Url = "", Allowlist = new[] { "a" } }), (int)WebPanelResult.BadArgs, "empty URL: BadArgs"); + Eq(live.OpenWebPanel(new WebPanelOptions { Url = "https://a/", Allowlist = Array.Empty() }), (int)WebPanelResult.BadArgs, "empty allowlist: BadArgs"); + Eq(live.OpenWebPanel(new WebPanelOptions { Url = "https://a/", Allowlist = new[] { "a\nb" } }), (int)WebPanelResult.BadArgs, "newline in an entry: BadArgs"); + Eq(live.NavigateWebPanel(1, ""), (int)WebPanelResult.BadArgs, "navigate empty URL: BadArgs"); + } + // v79: the SDK's copies of the training structs must be the engine's, byte for byte. private static void TrainingStructsMirror() { @@ -603,11 +692,11 @@ internal static class Program Eq(RynthCore.PluginSdk.TrainingInfoFlags.Busy, RynthCore.Engine.Plugins.TrainingInfoFlags.Busy, "info flag bits"); Eq(RynthCore.PluginSdk.TrainingEntryFlags.Trainable, RynthCore.Engine.Plugins.TrainingEntryFlags.Trainable, "entry flag bits"); // The three v79 slots follow v78's, then v80's two and v81's GetMergeStackResultFn (v82-v91 follow: - // eleven slots to v89, then v90's thirteen, v91's two and v93's one). - var names = typeof(RynthCore.Engine.Plugins.RynthCoreAPI).GetFields().Select(f => f.Name).SkipLast(27).TakeLast(6).ToArray(); + // eleven slots to v89, then v90's thirteen, v91's two, v93's one and the web panel's four). + var names = typeof(RynthCore.Engine.Plugins.RynthCoreAPI).GetFields().Select(f => f.Name).SkipLast(31).TakeLast(6).ToArray(); Check(names.SequenceEqual(new[] { "GetTrainingInfoFn", "RaiseFn", "TrainSkillFn", "GetContainerOrderFn", "PutItemInContainerFn", "GetMergeStackResultFn" }), "v79, v80 then v81 slots last: " + string.Join(",", names)); - var sdkNames = typeof(RynthCore.PluginSdk.RynthCoreApiNative).GetFields().Select(f => f.Name).SkipLast(27).TakeLast(6).ToArray(); + var sdkNames = typeof(RynthCore.PluginSdk.RynthCoreApiNative).GetFields().Select(f => f.Name).SkipLast(31).TakeLast(6).ToArray(); Check(sdkNames.SequenceEqual(names), "SDK v79, v80 and v81 slots in the same order"); } @@ -618,6 +707,12 @@ internal static class Program { var all = typeof(RynthCore.Engine.Plugins.RynthCoreAPI).GetFields().Select(f => f.Name).ToArray(); var sdkAll = typeof(RynthCore.PluginSdk.RynthCoreApiNative).GetFields().Select(f => f.Name).ToArray(); + // v93: the web panel's four slots end the table; the checks after these are on the table before them. + string[] v93 = { "WebPanelOpenFn", "WebPanelNavigateFn", "WebPanelCloseFn", "WebPanelGetStateFn" }; + Check(all.TakeLast(v93.Length).SequenceEqual(v93), "v93 slots last: " + string.Join(",", all.TakeLast(v93.Length))); + Check(sdkAll.TakeLast(v93.Length).SequenceEqual(v93), "SDK v93 slots last: " + string.Join(",", sdkAll.TakeLast(v93.Length))); + all = all.SkipLast(v93.Length).ToArray(); + sdkAll = sdkAll.SkipLast(v93.Length).ToArray(); // v93: the monster plates' health readings end the table (2026-11-07), right after v91's two. Check(all.Last() == "GetMonsterHealthFn", "v93 slot last: " + all.Last()); Check(sdkAll.Last() == "GetMonsterHealthFn", "SDK v93 slot last: " + sdkAll.Last());