Fix library resolving
This commit is contained in:
parent
1ebbbb4481
commit
a56e9b7a00
98
NativeFileDialogSharp/Native/LibResolver.cs
Normal file
98
NativeFileDialogSharp/Native/LibResolver.cs
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
@ -21,6 +21,8 @@ namespace NativeFileDialogSharp.Native
|
|||||||
{
|
{
|
||||||
public const string LibraryName = "nfd";
|
public const string LibraryName = "nfd";
|
||||||
|
|
||||||
|
static NativeFunctions() { LibResolver.EnsureInit(); }
|
||||||
|
|
||||||
[DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
|
[DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
|
||||||
public static extern unsafe nfdresult_t NFD_OpenDialog(byte* filterList, byte* defaultPath, out IntPtr outPath);
|
public static extern unsafe nfdresult_t NFD_OpenDialog(byte* filterList, byte* defaultPath, out IntPtr outPath);
|
||||||
|
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
<Project Sdk="Microsoft.NET.Sdk">
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<LangVersion>7.3</LangVersion>
|
|
||||||
<PackageVersion>0.5.0</PackageVersion>
|
<PackageVersion>0.5.0</PackageVersion>
|
||||||
<Nullable>disable</Nullable>
|
<Nullable>disable</Nullable>
|
||||||
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
|
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
|
||||||
@ -10,17 +9,13 @@
|
|||||||
<PackageProjectUrl>https://github.com/milleniumbug/NativeFileDialogSharp</PackageProjectUrl>
|
<PackageProjectUrl>https://github.com/milleniumbug/NativeFileDialogSharp</PackageProjectUrl>
|
||||||
<RepositoryUrl>https://github.com/milleniumbug/NativeFileDialogSharp</RepositoryUrl>
|
<RepositoryUrl>https://github.com/milleniumbug/NativeFileDialogSharp</RepositoryUrl>
|
||||||
<Description>Cross-platform native file dialog controls for Windows, Linux and macOS</Description>
|
<Description>Cross-platform native file dialog controls for Windows, Linux and macOS</Description>
|
||||||
<TargetFramework>netstandard2.0</TargetFramework>
|
<TargetFramework>net8</TargetFramework>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Content Include="runtimes\**" PackagePath="runtimes" Visible="false" />
|
<Content Include="runtimes\**">
|
||||||
<Content Include="nfd.dll">
|
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||||
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
|
</Content>
|
||||||
</Content>
|
</ItemGroup>
|
||||||
<Content Include="nfd_x86.dll">
|
|
||||||
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
|
|
||||||
</Content>
|
|
||||||
</ItemGroup>
|
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
Loading…
Reference in New Issue
Block a user