Skip to content

Commit

Permalink
Replace custom library load with net8 NativeLibrary (#4)
Browse files Browse the repository at this point in the history
* replace custom library load with net8 NativeLibrary

* Minor typo fix.

---------

Co-authored-by: jcant0n <javiercantonferrero@gmail.com>
  • Loading branch information
danielcaceresm and jcant0n authored Sep 16, 2024
1 parent ebbcdde commit 498d12b
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 265 deletions.
9 changes: 4 additions & 5 deletions OpenXRGen/Evergine.Bindings.OpenXR/Commands.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.Runtime.InteropServices;
using static Evergine.Bindings.OpenXR.OperatingSystemHelper;

namespace Evergine.Bindings.OpenXR
{
Expand All @@ -15,7 +14,7 @@ public static unsafe partial class OpenXRNative
static OpenXRNative()
{
nativeLib = LoadNativeLibrary();
LoadFuncionPointers();
LoadFunctionPointers();
}

private static NativeLibrary LoadNativeLibrary()
Expand All @@ -25,16 +24,16 @@ private static NativeLibrary LoadNativeLibrary()

private static string GetOpenXRName()
{
if (IsOSPlatform(PlatformType.Windows))
if (OperatingSystem.IsWindows())
{
return "openxr_loader.dll";
}
else if (IsOSPlatform(PlatformType.Android))
else if (OperatingSystem.IsAndroid())
{
// Android
return "libopenxr_loader.so";
}
else if (IsOSPlatform(PlatformType.Linux))
else if (OperatingSystem.IsLinux())
{
// Desktop Linux
return "libopenxr_loader.so.1";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<Copyright>Copyright (c) Evergine 2024</Copyright>
<Authors>Evergine Team</Authors>
<Company>Plain Concepts</Company>
<Description>Low-level bindings for OpenXR used in Evergine</Description>
<RepositoryUrl>https://github.com/EvergineTeam/OpenXR.NET</RepositoryUrl>
<PackageIcon>icon.png</PackageIcon>
<PackageReadmeFile>README.md</PackageReadmeFile>
Expand All @@ -16,10 +20,8 @@
</PropertyGroup>

<ItemGroup>
<None Include="..\..\icon.png" Pack="true" PackagePath="\"/>
</ItemGroup>
<ItemGroup>
<None Include="..\..\README.md" Pack="true" PackagePath="\"/>
<None Include="..\..\icon.png" Pack="true" PackagePath="\" Visible="false" />
<None Include="..\..\README.md" Pack="true" PackagePath="\" Visible="false" />
</ItemGroup>

</Project>
2 changes: 1 addition & 1 deletion OpenXRGen/Evergine.Bindings.OpenXR/Generated/Commands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1715,7 +1715,7 @@ public static XrResult xrPollFutureEXT(XrInstance instance, XrFuturePollInfoEXT*
public static XrResult xrCancelFutureEXT(XrInstance instance, XrFutureCancelInfoEXT* cancelInfo)
=> xrCancelFutureEXT_ptr(instance, cancelInfo);

public static void LoadFuncionPointers(XrInstance instance = default)
public static void LoadFunctionPointers(XrInstance instance = default)
{
if (instance != default)
{
Expand Down
19 changes: 0 additions & 19 deletions OpenXRGen/Evergine.Bindings.OpenXR/Kernel32.cs

This file was deleted.

24 changes: 0 additions & 24 deletions OpenXRGen/Evergine.Bindings.OpenXR/Libdl.cs

This file was deleted.

101 changes: 23 additions & 78 deletions OpenXRGen/Evergine.Bindings.OpenXR/NativeLibrary.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
using System;
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
using SysNativeLibrary = System.Runtime.InteropServices.NativeLibrary;

namespace Evergine.Bindings.OpenXR
{
public abstract class NativeLibrary : IDisposable
public class NativeLibrary : IDisposable
{
private readonly string libraryName;
private readonly IntPtr libraryHandle;
internal XrInstance instance;

public IntPtr NativeHandle => libraryHandle;

public NativeLibrary(string libraryName)
protected NativeLibrary(string libraryName)
{
this.libraryName = libraryName;
libraryHandle = LoadLibrary(this.libraryName);
Expand All @@ -23,13 +23,28 @@ public NativeLibrary(string libraryName)
}
}

protected abstract IntPtr LoadLibrary(string libraryName);
protected abstract void FreeLibrary(IntPtr libraryHandle);
protected abstract IntPtr LoadFunction(string functionName);
protected IntPtr LoadLibrary(string libraryName)
{
if (SysNativeLibrary.TryLoad(libraryName, typeof(NativeLibrary).Assembly, null, out var lib))
{
return lib;
}

Debug.WriteLine($" ===> Error loading native library {libraryName}");
return IntPtr.Zero;
}

protected void FreeLibrary(IntPtr libraryHandle)
{
if (libraryHandle != IntPtr.Zero)
{
SysNativeLibrary.Free(libraryHandle);
}
}

public unsafe void LoadFunction<T>(string name, out T field)
{
IntPtr funcPtr = LoadFunction(name);
SysNativeLibrary.TryGetExport(libraryHandle, name, out IntPtr funcPtr);
if (funcPtr == IntPtr.Zero)
{
OpenXRNative.xrGetInstanceProcAddr(instance, (byte*)Marshal.StringToHGlobalAnsi(name), new IntPtr(&funcPtr));
Expand All @@ -53,77 +68,7 @@ public void Dispose()

public static NativeLibrary Load(string libraryName)
{
if (OperatingSystemHelper.IsOSPlatform(OperatingSystemHelper.PlatformType.Windows))
{
return new WindowsNativeLibrary(libraryName);
}
else if (OperatingSystemHelper.IsOSPlatform(OperatingSystemHelper.PlatformType.Android)
|| OperatingSystemHelper.IsOSPlatform(OperatingSystemHelper.PlatformType.Linux)
|| OperatingSystemHelper.IsOSPlatform(OperatingSystemHelper.PlatformType.MacOS))
{
return new UnixNativeLibrary(libraryName);
}
else
{
throw new PlatformNotSupportedException("Cannot load native libraries on this platform: " + RuntimeInformation.OSDescription);
}
}

private class WindowsNativeLibrary : NativeLibrary
{
public WindowsNativeLibrary(string libraryName) : base(libraryName)
{
}

protected override IntPtr LoadLibrary(string libraryName)
{
return Kernel32.LoadLibrary(libraryName);
}

protected override void FreeLibrary(IntPtr libraryHandle)
{
Kernel32.FreeLibrary(libraryHandle);
}

protected override IntPtr LoadFunction(string functionName)
{
Debug.WriteLine("Loading " + functionName);
return Kernel32.GetProcAddress(NativeHandle, functionName);
}
}

private class UnixNativeLibrary : NativeLibrary
{
public UnixNativeLibrary(string libraryName) : base(libraryName)
{
}

protected override IntPtr LoadLibrary(string libraryName)
{
Libdl.dlerror();
IntPtr handle = Libdl.dlopen(libraryName, Libdl.RTLD_NOW);
if (handle == IntPtr.Zero && !Path.IsPathRooted(libraryName))
{
string baseDir = AppContext.BaseDirectory;
if (!string.IsNullOrWhiteSpace(baseDir))
{
string localPath = Path.Combine(baseDir, libraryName);
handle = Libdl.dlopen(localPath, Libdl.RTLD_NOW);
}
}

return handle;
}

protected override void FreeLibrary(IntPtr libraryHandle)
{
Libdl.dlclose(libraryHandle);
}

protected override IntPtr LoadFunction(string functionName)
{
return Libdl.dlsym(NativeHandle, functionName);
}
return new NativeLibrary(libraryName);
}
}
}
133 changes: 0 additions & 133 deletions OpenXRGen/Evergine.Bindings.OpenXR/OperatingSystemHelper.cs

This file was deleted.

2 changes: 1 addition & 1 deletion OpenXRGen/OpenXRGen/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ static void Main(string[] args)
file.WriteLine($"\t\t\t=> {command.Prototype.Name}_ptr({command.GetParametersSignature(openXRSpec, useTypes: false)});\n");
}

file.WriteLine($"\t\tpublic static void LoadFuncionPointers(XrInstance instance = default)");
file.WriteLine($"\t\tpublic static void LoadFunctionPointers(XrInstance instance = default)");
file.WriteLine("\t\t{");
file.WriteLine("\t\t\tif (instance != default)");
file.WriteLine("\t\t\t{");
Expand Down

0 comments on commit 498d12b

Please sign in to comment.