// ============================================================================ // RynthCore.Engine - Hooking/LoaderServices.cs // // The native loader (native/Loader/Services.c, loader 2.0+) owns the permanent native // entry points the engine used to hand out itself: the game window's subclass proc and // the plugin API table's function pointers. With them, nothing native ever points into // an engine generation after its shutdown, so the CoreCLR host can unload it // (docs/UNLOADABLE_ENGINE_PLAN.md). Under an older loader (or none) Available is false // and callers keep their old behaviour. // ============================================================================ using System; using System.Runtime.InteropServices; namespace RynthCore.Engine.Hooking; internal static unsafe class LoaderServices { private static readonly delegate* unmanaged[Stdcall] s_subclassInstall; private static readonly delegate* unmanaged[Stdcall] s_subclassRelease; private static readonly delegate* unmanaged[Stdcall] s_apiStub; static LoaderServices() { IntPtr loader = GetModuleHandleW("RynthCore.Loader.dll"); if (loader == IntPtr.Zero || GetProcAddress(loader, "RcServices_Version") == IntPtr.Zero) return; s_subclassInstall = (delegate* unmanaged[Stdcall])GetProcAddress(loader, "RcSubclass_Install"); s_subclassRelease = (delegate* unmanaged[Stdcall])GetProcAddress(loader, "RcSubclass_Release"); s_apiStub = (delegate* unmanaged[Stdcall])GetProcAddress(loader, "RcApiStub"); } public static bool Available => s_subclassInstall != null && s_subclassRelease != null && s_apiStub != null; /// /// Routes 's messages to through the /// loader's permanent subclass. is the proc to forward to. /// public static bool SubclassInstall(IntPtr hwnd, IntPtr handler, out IntPtr previous) { IntPtr prev = IntPtr.Zero; bool ok = s_subclassInstall(hwnd, handler, &prev) != 0; previous = prev; return ok; } /// The loader's subclass passes everything straight to the previous proc again. public static void SubclassRelease(IntPtr hwnd) => s_subclassRelease(hwnd); /// A permanent stub for plugin API slot that calls . public static IntPtr ApiStub(int index, IntPtr fn) => s_apiStub(index, fn); [DllImport("kernel32.dll", CharSet = CharSet.Unicode)] private static extern IntPtr GetModuleHandleW(string lpModuleName); [DllImport("kernel32.dll", CharSet = CharSet.Ansi, ExactSpelling = true)] private static extern IntPtr GetProcAddress(IntPtr hModule, string lpProcName); }