mirror of
https://github.com/Facepunch/sbox-public.git
synced 2026-01-01 19:08:19 -05:00
This commit imports the C# engine code and game files, excluding C++ source code. [Source-Commit: ceb3d758046e50faa6258bc3b658a30c97743268]
46 lines
1.2 KiB
C#
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 );
|
|
}
|
|
}
|