diff --git a/src/RynthCore.Engine/Plugins/ManagedPlugins.cs b/src/RynthCore.Engine/Plugins/ManagedPlugins.cs index e6ec2d4..0c33dba 100644 --- a/src/RynthCore.Engine/Plugins/ManagedPlugins.cs +++ b/src/RynthCore.Engine/Plugins/ManagedPlugins.cs @@ -253,6 +253,16 @@ internal static class ManagedPlugins protected override IntPtr LoadUnmanagedDll(string unmanagedDllName) { + // ImGui.NET's "cimgui" is the engine's own module (RynthCore.cimgui.dll), so a plugin draws in + // the engine's ImGui and whatever context it has current, a pop-out's included. Each plugin's + // own ImGui.NET used to reach a second cimgui.dll whose current context stayed the main one: + // a section drawn in a popped-out Mini Remote ran outside any frame and faulted in native code, + // killing the client (RynthDreamWeave's server card, 2026-10-19). Found and fixed by + // SilentKelpie (rynth PR #6); re-implemented here. + if (EntryPoint.ImGuiNativeHandle != IntPtr.Zero + && (unmanagedDllName.Equals("cimgui", StringComparison.OrdinalIgnoreCase) + || unmanagedDllName.Equals("cimgui.dll", StringComparison.OrdinalIgnoreCase))) + return EntryPoint.ImGuiNativeHandle; string? path = Pick(unmanagedDllName.EndsWith(".dll", StringComparison.OrdinalIgnoreCase) ? unmanagedDllName : unmanagedDllName + ".dll"); diff --git a/src/RynthCore.Engine/Plugins/PluginManager.cs b/src/RynthCore.Engine/Plugins/PluginManager.cs index 05b4bd4..e28494e 100644 --- a/src/RynthCore.Engine/Plugins/PluginManager.cs +++ b/src/RynthCore.Engine/Plugins/PluginManager.cs @@ -1353,7 +1353,10 @@ internal static partial class PluginManager surfaceUtf8[..nameLen].CopyTo(popOut); PopOutSurfaceSuffix.CopyTo(popOut[nameLen..]); popOut[^1] = 0; - return RenderEmbedCore(pluginName, popOut, size, out want); + // A plugin on a second cimgui copy must see the pop-out's context there too (StrayImGuiContext). + StrayImGuiContext.Scope stray = StrayImGuiContext.Enter(current); + try { return RenderEmbedCore(pluginName, popOut, size, out want); } + finally { StrayImGuiContext.Restore(stray); } } return RenderEmbedCore(pluginName, surfaceUtf8, size, out want); } diff --git a/src/RynthCore.Engine/Plugins/StrayImGuiContext.cs b/src/RynthCore.Engine/Plugins/StrayImGuiContext.cs new file mode 100644 index 0000000..88cdc3a --- /dev/null +++ b/src/RynthCore.Engine/Plugins/StrayImGuiContext.cs @@ -0,0 +1,76 @@ +// ============================================================================ +// RynthCore.Engine - Plugins/StrayImGuiContext.cs +// +// A plugin whose ImGui.NET isn't bound to the engine's cimgui (RynthCore.cimgui.dll) calls a second +// copy, cimgui.dll, with an ImGui of its own and so its own "current context". Plugins set that to the +// engine's context before drawing, which works in the main frame. A popped-out window draws in a +// context of its own, and the engine makes it current only in ITS cimgui: the stray copy still names +// the main context, which is not in a frame then, and the plugin's first call faults in native code. +// No try/catch stops an access violation there: the client dies (RynthDreamWeave's server card in a +// popped-out Mini Remote, 2026-10-19). +// +// So around a plugin call made inside a pop-out, the pop-out's context is made current in the stray +// copy too, and the copy's own current context is put back afterwards. Nothing happens when no second +// cimgui is loaded (ManagedPlugins binds managed plugins to the engine's). Render thread only. +// +// Found and fixed by SilentKelpie (rynth PR #6); re-implemented here. +// ============================================================================ + +using System; +using System.Runtime.InteropServices; + +namespace RynthCore.Engine.Plugins; + +internal static unsafe class StrayImGuiContext +{ + [DllImport("kernel32.dll", CharSet = CharSet.Unicode, ExactSpelling = true, EntryPoint = "GetModuleHandleW")] + private static extern IntPtr GetModuleHandleW(string name); + + [DllImport("kernel32.dll", CharSet = CharSet.Ansi, ExactSpelling = true, EntryPoint = "GetProcAddress")] + private static extern IntPtr GetProcAddress(IntPtr module, string name); + + // The stray module these belong to (it can load after the first pop-out frame). + private static IntPtr _module; + private static delegate* unmanaged[Cdecl] _get; + private static delegate* unmanaged[Cdecl] _set; + + /// What changed, for . + public readonly struct Scope + { + public readonly bool Entered; + public readonly IntPtr Previous; + public Scope(bool entered, IntPtr previous) { Entered = entered; Previous = previous; } + } + + /// Makes current in the stray cimgui, if one is loaded. + public static Scope Enter(IntPtr context) + { + if (context == IntPtr.Zero || !Resolve()) return default; + IntPtr previous = _get(); + _set(context); + return new Scope(true, previous); + } + + /// Puts back the stray copy's own current context (zero included: a pop-out's must not linger there). + public static void Restore(Scope scope) + { + if (!scope.Entered || _set == null) return; + _set(scope.Previous); + } + + /// True with the stray copy's two calls ready: a cimgui.dll loaded that isn't the engine's. + private static bool Resolve() + { + IntPtr module = GetModuleHandleW("cimgui.dll"); + if (module == IntPtr.Zero || module == EntryPoint.ImGuiNativeHandle) return false; + if (module == _module && _get != null && _set != null) return true; + IntPtr get = GetProcAddress(module, "igGetCurrentContext"); + IntPtr set = GetProcAddress(module, "igSetCurrentContext"); + if (get == IntPtr.Zero || set == IntPtr.Zero) return false; + _get = (delegate* unmanaged[Cdecl])get; + _set = (delegate* unmanaged[Cdecl])set; + _module = module; + RynthLog.Plugin($"PluginManager: a second cimgui.dll is loaded (0x{module:X8}); popped-out plugin surfaces get the pop-out's context in it too."); + return true; + } +}