|
| 1 | +using LVGLSharp.Interop; |
| 2 | +using System.Reflection; |
| 3 | +using System.Runtime.InteropServices; |
| 4 | +using System.Threading; |
| 5 | + |
| 6 | +namespace LVGLSharp |
| 7 | +{ |
| 8 | + public static class LvglNativeLibraryResolver |
| 9 | + { |
| 10 | + private const string NativeLibraryName = "lvgl"; |
| 11 | + private static IntPtr _handle; |
| 12 | + private static int _isRegistered; |
| 13 | + |
| 14 | + /// <summary> |
| 15 | + /// Registers LVGL native library probing from the application output directory. |
| 16 | + /// </summary> |
| 17 | + public static void EnsureRegistered() |
| 18 | + { |
| 19 | + if (Interlocked.Exchange(ref _isRegistered, 1) != 0) |
| 20 | + { |
| 21 | + return; |
| 22 | + } |
| 23 | + |
| 24 | + NativeLibrary.SetDllImportResolver(typeof(LVGL).Assembly, Resolve); |
| 25 | + } |
| 26 | + |
| 27 | + private static IntPtr Resolve(string libraryName, Assembly assembly, DllImportSearchPath? searchPath) |
| 28 | + { |
| 29 | + if (!string.Equals(libraryName, NativeLibraryName, StringComparison.Ordinal)) |
| 30 | + { |
| 31 | + return IntPtr.Zero; |
| 32 | + } |
| 33 | + |
| 34 | + if (_handle != IntPtr.Zero) |
| 35 | + { |
| 36 | + return _handle; |
| 37 | + } |
| 38 | + |
| 39 | + foreach (var candidatePath in GetCandidatePaths()) |
| 40 | + { |
| 41 | + if (NativeLibrary.TryLoad(candidatePath, out var handle)) |
| 42 | + { |
| 43 | + _handle = handle; |
| 44 | + return handle; |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + if (NativeLibrary.TryLoad(libraryName, assembly, searchPath, out var fallbackHandle)) |
| 49 | + { |
| 50 | + _handle = fallbackHandle; |
| 51 | + return fallbackHandle; |
| 52 | + } |
| 53 | + |
| 54 | + return IntPtr.Zero; |
| 55 | + } |
| 56 | + |
| 57 | + private static IEnumerable<string> GetCandidatePaths() |
| 58 | + { |
| 59 | + var baseDirectory = AppContext.BaseDirectory; |
| 60 | + var fileName = GetLibraryFileName(); |
| 61 | + |
| 62 | + yield return Path.Combine(baseDirectory, fileName); |
| 63 | + |
| 64 | + foreach (var runtimeIdentifier in GetCandidateRuntimeIdentifiers()) |
| 65 | + { |
| 66 | + yield return Path.Combine(baseDirectory, "runtimes", runtimeIdentifier, "native", fileName); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + private static IEnumerable<string> GetCandidateRuntimeIdentifiers() |
| 71 | + { |
| 72 | + if (OperatingSystem.IsWindows()) |
| 73 | + { |
| 74 | + yield return RuntimeInformation.ProcessArchitecture switch |
| 75 | + { |
| 76 | + Architecture.X86 => "win-x86", |
| 77 | + Architecture.Arm64 => "win-arm64", |
| 78 | + _ => "win-x64" |
| 79 | + }; |
| 80 | + |
| 81 | + yield break; |
| 82 | + } |
| 83 | + |
| 84 | + if (OperatingSystem.IsLinux()) |
| 85 | + { |
| 86 | + yield return RuntimeInformation.ProcessArchitecture switch |
| 87 | + { |
| 88 | + Architecture.Arm => "linux-arm", |
| 89 | + Architecture.Arm64 => "linux-arm64", |
| 90 | + _ => "linux-x64" |
| 91 | + }; |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + private static string GetLibraryFileName() |
| 96 | + { |
| 97 | + if (OperatingSystem.IsWindows()) |
| 98 | + { |
| 99 | + return "lvgl.dll"; |
| 100 | + } |
| 101 | + |
| 102 | + if (OperatingSystem.IsLinux()) |
| 103 | + { |
| 104 | + return "liblvgl.so"; |
| 105 | + } |
| 106 | + |
| 107 | + return NativeLibraryName; |
| 108 | + } |
| 109 | + } |
| 110 | +} |
0 commit comments