From a56e9b7a00ca197d338b5240d6d98daa18967007 Mon Sep 17 00:00:00 2001 From: WNP78 Date: Sun, 11 Aug 2024 23:24:24 +0100 Subject: [PATCH] Fix library resolving --- NativeFileDialogSharp/Native/LibResolver.cs | 98 +++++++++++++++++++ .../Native/NativeFunctions.cs | 2 + .../NativeFileDialogSharp.csproj | 19 ++-- 3 files changed, 107 insertions(+), 12 deletions(-) create mode 100644 NativeFileDialogSharp/Native/LibResolver.cs diff --git a/NativeFileDialogSharp/Native/LibResolver.cs b/NativeFileDialogSharp/Native/LibResolver.cs new file mode 100644 index 0000000..6004fa2 --- /dev/null +++ b/NativeFileDialogSharp/Native/LibResolver.cs @@ -0,0 +1,98 @@ +using System.Collections.Generic; +using System.IO; +using System.Reflection; +using System.Runtime.InteropServices; +using System; + +namespace NativeFileDialogSharp.Native; + +internal unsafe static class LibResolver +{ + public const string LibName = NativeFunctions.LibraryName; + + internal static nint? CachedPtr; + + static LibResolver() + { + NativeLibrary.SetDllImportResolver(Assembly.GetExecutingAssembly(), OnDllImport); + } + + public static void EnsureInit() { } + + private static nint OnDllImport(string libraryName, Assembly assembly, DllImportSearchPath? searchPath) + { + if (libraryName == NativeFunctions.LibraryName) + { + if (CachedPtr is nint cached) return cached; + if (TryFindLib(libraryName, assembly, searchPath, out var lib)) + { + CachedPtr = lib; + return lib; + } + } + + return NativeLibrary.Load(libraryName, assembly, searchPath); + } + + private static bool TryFindLib(string name, Assembly assembly, DllImportSearchPath? searchPath, out nint lib) + { + string rid = RuntimeInformation.RuntimeIdentifier; + string nugetNativeLibsPath = Path.Combine(AppContext.BaseDirectory, "runtimes", rid, "native"); + bool isNuGetRuntimeLibrariesDirectoryPresent = Directory.Exists(nugetNativeLibsPath); + string dllName = name; + string altDllName = null; + + if (OperatingSystem.IsWindows()) + { + dllName = $"{name}.dll"; + + if (!isNuGetRuntimeLibrariesDirectoryPresent) + { + rid = RuntimeInformation.ProcessArchitecture switch + { + Architecture.X64 => "win-x64", + Architecture.X86 => "win-x86", + _ => throw new NotSupportedException() + }; + + nugetNativeLibsPath = Path.Combine(AppContext.BaseDirectory, "runtimes", rid, "native"); + isNuGetRuntimeLibrariesDirectoryPresent = Directory.Exists(nugetNativeLibsPath); + } + } + else if (OperatingSystem.IsLinux()) + { + dllName = $"{name}.so"; + altDllName = $"lib{name}.so"; + } + + if (isNuGetRuntimeLibrariesDirectoryPresent) + { + string nativeLibPath = Path.Combine(AppContext.BaseDirectory, "runtimes", rid, "native", dllName); + + if (NativeLibrary.TryLoad(nativeLibPath, out lib)) + { + return true; + } + + if (altDllName != null) + { + nativeLibPath = Path.Combine(AppContext.BaseDirectory, "runtimes", rid, "native", altDllName); + + if (NativeLibrary.TryLoad(nativeLibPath, out lib)) + { + return true; + } + } + } + else + { + if (NativeLibrary.TryLoad(dllName, assembly, searchPath, out lib)) + { + return true; + } + } + + lib = IntPtr.Zero; + return false; + } +} diff --git a/NativeFileDialogSharp/Native/NativeFunctions.cs b/NativeFileDialogSharp/Native/NativeFunctions.cs index 9b702ea..33f7861 100644 --- a/NativeFileDialogSharp/Native/NativeFunctions.cs +++ b/NativeFileDialogSharp/Native/NativeFunctions.cs @@ -21,6 +21,8 @@ namespace NativeFileDialogSharp.Native { public const string LibraryName = "nfd"; + static NativeFunctions() { LibResolver.EnsureInit(); } + [DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] public static extern unsafe nfdresult_t NFD_OpenDialog(byte* filterList, byte* defaultPath, out IntPtr outPath); diff --git a/NativeFileDialogSharp/NativeFileDialogSharp.csproj b/NativeFileDialogSharp/NativeFileDialogSharp.csproj index bbfcf40..3857e64 100644 --- a/NativeFileDialogSharp/NativeFileDialogSharp.csproj +++ b/NativeFileDialogSharp/NativeFileDialogSharp.csproj @@ -1,7 +1,6 @@ - + - 7.3 0.5.0 disable true @@ -10,17 +9,13 @@ https://github.com/milleniumbug/NativeFileDialogSharp https://github.com/milleniumbug/NativeFileDialogSharp Cross-platform native file dialog controls for Windows, Linux and macOS - netstandard2.0 + net8 - - - - PreserveNewest - - - PreserveNewest - - + + + PreserveNewest + +