Fix library resolving

This commit is contained in:
WNP78 2024-08-11 23:24:24 +01:00
parent 1ebbbb4481
commit a56e9b7a00
3 changed files with 107 additions and 12 deletions

View File

@ -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;
}
}

View File

@ -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);

View File

@ -1,7 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<LangVersion>7.3</LangVersion>
<PackageVersion>0.5.0</PackageVersion>
<Nullable>disable</Nullable>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
@ -10,16 +9,12 @@
<PackageProjectUrl>https://github.com/milleniumbug/NativeFileDialogSharp</PackageProjectUrl>
<RepositoryUrl>https://github.com/milleniumbug/NativeFileDialogSharp</RepositoryUrl>
<Description>Cross-platform native file dialog controls for Windows, Linux and macOS</Description>
<TargetFramework>netstandard2.0</TargetFramework>
<TargetFramework>net8</TargetFramework>
</PropertyGroup>
<ItemGroup>
<Content Include="runtimes\**" PackagePath="runtimes" Visible="false" />
<Content Include="nfd.dll">
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
</Content>
<Content Include="nfd_x86.dll">
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
<Content Include="runtimes\**">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>