Files
sbox-public/engine/Sandbox.Engine/Core/Interop/CreateInterface.cs
s&box team 71f266059a Open source release
This commit imports the C# engine code and game files, excluding C++ source code.

[Source-Commit: ceb3d758046e50faa6258bc3b658a30c97743268]
2025-11-24 09:05:18 +00:00

46 lines
1.2 KiB
C#

using System.Runtime.InteropServices;
namespace NativeEngine;
/// <summary>
/// Mimmicks the engine internal CreateInterface system, allowing us to
/// get the interfaces without asking native.
/// </summary>
internal static class CreateInterface
{
static Dictionary<string, IntPtr> loadedModules = new();
static IntPtr LoadModule( string dll )
{
if ( loadedModules.TryGetValue( dll, out var module ) )
return module;
if ( !NativeLibrary.TryLoad( dll, out module ) )
return default;
loadedModules[dll] = module;
return module;
}
[UnmanagedFunctionPointer( CallingConvention.Cdecl )]
public delegate IntPtr CreateInterfaceFn( string pName, IntPtr pReturnCode );
public static IntPtr GetCreateInterface( string dll )
{
IntPtr module = LoadModule( dll );
if ( module == IntPtr.Zero ) return default;
return NativeLibrary.GetExport( module, "CreateInterface" );
}
internal static IntPtr LoadInterface( string dll, string interfacename )
{
var createInterface = GetCreateInterface( dll );
if ( createInterface == IntPtr.Zero )
return default;
CreateInterfaceFn fn = Marshal.GetDelegateForFunctionPointer<CreateInterfaceFn>( createInterface );
return fn( interfacename, default );
}
}