veldrid icon indicating copy to clipboard operation
veldrid copied to clipboard

NativeLibraryLoader throws exception on .NET 6 Mac Catalyst application

Open hevey opened this issue 3 years ago • 2 comments
trafficstars

When trying to build a GraphicsDevice in a .NET 6 Mac Catalyst application the following exception is thrown.

System.TypeInitializationException: The type initializer for 'NativeLibraryLoader.NativeLibrary' threw an exception. ---> System.PlatformNotSupportedException: This platform cannot load native libraries.

Stack trace below

System.TypeInitializationException: The type initializer for 'NativeLibraryLoader.NativeLibrary' threw an exception. ---> System.PlatformNotSupportedException: This platform cannot load native libraries. at NativeLibraryLoader.LibraryLoader.GetPlatformDefaultLoader() at NativeLibraryLoader.NativeLibrary..cctor() --- End of inner exception stack trace --- at Veldrid.MTL.MTLGraphicsDevice..ctor(GraphicsDeviceOptions options, Nullable`1 swapchainDesc) at Veldrid.GraphicsDevice.CreateMetal(GraphicsDeviceOptions options, SwapchainDescription swapchainDescription) at Cade.PlatformManager.GetGraphicsDevice() in /Users/hevey/Development/PlayCade/Cade/src/Cade/Platforms/MacCatalyst/PlatformManager.cs:line 33 at Cade.LibraryPage.VeldridSetup() in /Users/hevey/Development/PlayCade/Cade/src/Cade/Pages/LibraryPage.cs:line 73 at Cade.LibraryPage.<>c.

b__4_0() in /Users/hevey/Development/PlayCade/Cade/src/Cade/Pages/LibraryPage.c s:line 28 at Comet.Button.Microsoft.Maui.IButton.Clicked() at Microsoft.Maui.Handlers.ButtonHandler.OnButtonTouchUpInside(Object sender, EventArgs e) at UIKit.UIControlEventProxy.Activated() --- End of stack trace from previous location --- at UIKit.UIApplication.Main(String[] args, Type principalClass, Type delegateClass) at Cade.Program.Main(String[] args) in /Users/hevey/Development/PlayCade/Cade/src/Cade/Platforms/MacCatalyst/Program.cs:line 13

hevey avatar Jan 19 '22 10:01 hevey

I'm not very familiar with what a "Mac Catalyst Application" is, but it seems to have broken the platform detection logic over here: https://github.com/mellinoe/nativelibraryloader/blob/586f9738ff12688df8f0662027da8c319aee3841/NativeLibraryLoader/LibraryLoader.cs#L179. Presumably it should be using the OSPlatform.OSX codepath, but I'm not sure what signal should be used in this case.

mellinoe avatar Mar 17 '22 04:03 mellinoe

I suspect something similar to this will need to be implemented

  • NativeLibraryLoader to multitarget with .net6.0

then change the code to the following. Though I haven't been able to test this as of yet. If I get a working implementation I will open a PR.

#if NET6_0_OR_GREATER
            if (OperatingSystem.IsWindows())
            {
                return new Win32LibraryLoader();
            }
            if (OperatingSystem.IsLinux() || OperatingSystem.IsMacOS() || OperatingSystem.IsMacCatalyst())
            {
                return new UnixLibraryLoader();
            }
            
            throw new PlatformNotSupportedException("This platform cannot load native libraries.");
#else
            if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
            {
                return new Win32LibraryLoader();
            }
            else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux) || RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
            {
                return new UnixLibraryLoader();
            }

            throw new PlatformNotSupportedException("This platform cannot load native libraries.");
#endif

hevey avatar May 06 '22 03:05 hevey