brutal/Brutal.OpenAl/Manual/NativeLoader.cs
Rian Drake b578433e37 first
2026-10-15 17:10:50 +13:00

40 lines
1.4 KiB
C#

using System.Diagnostics.CodeAnalysis;
using System.Reflection;
namespace Brutal.OpenAlApi;
internal static class NativeLoader
{
// The logical name the generated [LibraryImport] entries request. Must match the `Dll` const.
private const string LogicalName = "openal";
[SuppressMessage(
"Usage",
"CA2255:The 'ModuleInitializer' attribute should not be used in libraries",
Justification = "Resolver must register before any nested-type P/Invoke; static ctor is not triggered by nested-type access."
)]
[ModuleInitializer]
internal static void Register()
{
NativeLibrary.SetDllImportResolver(typeof(NativeLoader).Assembly, Resolve);
}
private static nint Resolve(string libraryName, Assembly assembly, DllImportSearchPath? searchPath)
{
if (libraryName != LogicalName)
return nint.Zero;
// openal-soft installed file names:
// Windows : OpenAL32.dll
// Linux : libopenal.so.1 (SONAME)
// macOS : libopenal.1.dylib (install_name @rpath/libopenal.1.dylib)
var fileName = Platform.CurrentFamily switch
{
Platform.Family.Windows => "OpenAL32.dll",
Platform.Family.MacOS => "libopenal.1.dylib",
_ => "libopenal.so.1",
};
return NativeLibrary.Load(fileName, assembly, DllImportSearchPath.AssemblyDirectory);
}
}