GLibrary.cs: introduce IsSupported
This commit is contained in:
parent
b3fd3f91bf
commit
6b4cf555e0
1 changed files with 79 additions and 63 deletions
|
@ -5,16 +5,19 @@ using System.Runtime.InteropServices;
|
||||||
|
|
||||||
class GLibrary
|
class GLibrary
|
||||||
{
|
{
|
||||||
|
|
||||||
[DllImport("kernel32.dll", SetLastError = true)]
|
[DllImport("kernel32.dll", SetLastError = true)]
|
||||||
private static extern bool SetDllDirectory(string lpPathName);
|
private static extern bool SetDllDirectory(string lpPathName);
|
||||||
|
|
||||||
private static Dictionary<Library, IntPtr> _libraries;
|
private static Dictionary<Library, IntPtr> _libraries;
|
||||||
|
private static HashSet<Library> _librariesNotFound;
|
||||||
private static Dictionary<string, IntPtr> _customlibraries;
|
private static Dictionary<string, IntPtr> _customlibraries;
|
||||||
private static Dictionary<Library, string[]> _libraryDefinitions;
|
private static Dictionary<Library, string[]> _libraryDefinitions;
|
||||||
|
|
||||||
static GLibrary()
|
static GLibrary()
|
||||||
{
|
{
|
||||||
_customlibraries = new Dictionary<string, IntPtr>();
|
_customlibraries = new Dictionary<string, IntPtr>();
|
||||||
|
_librariesNotFound = new HashSet<Library>();
|
||||||
_libraries = new Dictionary<Library, IntPtr>();
|
_libraries = new Dictionary<Library, IntPtr>();
|
||||||
_libraryDefinitions = new Dictionary<Library, string[]>();
|
_libraryDefinitions = new Dictionary<Library, string[]>();
|
||||||
_libraryDefinitions[Library.GLib] = new[] {"libglib-2.0-0.dll", "libglib-2.0.so.0", "libglib-2.0.0.dylib", "glib-2.dll"};
|
_libraryDefinitions[Library.GLib] = new[] {"libglib-2.0-0.dll", "libglib-2.0.so.0", "libglib-2.0.0.dylib", "glib-2.dll"};
|
||||||
|
@ -32,36 +35,49 @@ class GLibrary
|
||||||
|
|
||||||
public static IntPtr Load(Library library)
|
public static IntPtr Load(Library library)
|
||||||
{
|
{
|
||||||
var ret = IntPtr.Zero;
|
if (_libraries.TryGetValue(library, out var ret))
|
||||||
if (_libraries.TryGetValue(library, out ret))
|
|
||||||
return ret;
|
return ret;
|
||||||
|
|
||||||
if (FuncLoader.IsWindows)
|
if (TryGet(library, out ret)) return ret;
|
||||||
|
|
||||||
|
var err = library + ": " + string.Join(", ", _libraryDefinitions[library]);
|
||||||
|
|
||||||
|
throw new DllNotFoundException(err);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public static bool IsSupported(Library library) => TryGet(library, out var __);
|
||||||
|
|
||||||
|
static bool TryGet(Library library, out IntPtr ret)
|
||||||
{
|
{
|
||||||
|
ret = IntPtr.Zero;
|
||||||
|
|
||||||
|
if (_libraries.TryGetValue(library, out ret)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (_librariesNotFound.Contains(library)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (FuncLoader.IsWindows) {
|
||||||
ret = FuncLoader.LoadLibrary(_libraryDefinitions[library][0]);
|
ret = FuncLoader.LoadLibrary(_libraryDefinitions[library][0]);
|
||||||
|
|
||||||
if (ret == IntPtr.Zero)
|
if (ret == IntPtr.Zero) {
|
||||||
{
|
|
||||||
SetDllDirectory(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Gtk", "3.24.24"));
|
SetDllDirectory(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Gtk", "3.24.24"));
|
||||||
ret = FuncLoader.LoadLibrary(_libraryDefinitions[library][0]);
|
ret = FuncLoader.LoadLibrary(_libraryDefinitions[library][0]);
|
||||||
}
|
}
|
||||||
}
|
} else if (FuncLoader.IsOSX) {
|
||||||
else if (FuncLoader.IsOSX)
|
|
||||||
{
|
|
||||||
ret = FuncLoader.LoadLibrary(_libraryDefinitions[library][2]);
|
ret = FuncLoader.LoadLibrary(_libraryDefinitions[library][2]);
|
||||||
|
|
||||||
if (ret == IntPtr.Zero)
|
if (ret == IntPtr.Zero) {
|
||||||
{
|
|
||||||
ret = FuncLoader.LoadLibrary("/usr/local/lib/" + _libraryDefinitions[library][2]);
|
ret = FuncLoader.LoadLibrary("/usr/local/lib/" + _libraryDefinitions[library][2]);
|
||||||
}
|
}
|
||||||
}
|
} else
|
||||||
else
|
|
||||||
ret = FuncLoader.LoadLibrary(_libraryDefinitions[library][1]);
|
ret = FuncLoader.LoadLibrary(_libraryDefinitions[library][1]);
|
||||||
|
|
||||||
if (ret == IntPtr.Zero)
|
if (ret == IntPtr.Zero) {
|
||||||
{
|
for (var i = 0; i < _libraryDefinitions[library].Length; i++) {
|
||||||
for (int i = 0; i < _libraryDefinitions[library].Length; i++)
|
|
||||||
{
|
|
||||||
ret = FuncLoader.LoadLibrary(_libraryDefinitions[library][i]);
|
ret = FuncLoader.LoadLibrary(_libraryDefinitions[library][i]);
|
||||||
|
|
||||||
if (ret != IntPtr.Zero)
|
if (ret != IntPtr.Zero)
|
||||||
|
@ -69,13 +85,13 @@ class GLibrary
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ret == IntPtr.Zero)
|
if (ret != IntPtr.Zero) {
|
||||||
{
|
_libraries[library] = ret;
|
||||||
var err = library + ": " + string.Join(", ", _libraryDefinitions[library]);
|
} else {
|
||||||
throw new DllNotFoundException(err);
|
_librariesNotFound.Add(library);
|
||||||
}
|
}
|
||||||
|
|
||||||
_libraries[library] = ret;
|
return ret != IntPtr.Zero;
|
||||||
return ret;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
Loading…
Reference in a new issue