Skip to content

Commit c922ddd

Browse files
committed
修改
1 parent 87bda8b commit c922ddd

File tree

4 files changed

+122
-1
lines changed

4 files changed

+122
-1
lines changed

src/Demos/SerialPort/SerialPort.csproj

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
</PropertyGroup>
1212

1313
<ItemGroup>
14-
<PackageReference Include="LVGLSharp.Native" Version="0.0.3" />
14+
<PackageReference Include="LVGLSharp.Native" Version="0.0.3" GeneratePathProperty="true" />
1515
<PackageReference Include="System.IO.Ports" Version="10.0.5" />
1616
</ItemGroup>
1717

@@ -22,4 +22,13 @@
2222
<ProjectReference Include="..\..\LVGLSharp.Runtime.Linux\LVGLSharp.Runtime.Linux.csproj" />
2323
</ItemGroup>
2424

25+
<ItemGroup Condition="'$(PkgLVGLSharp_Native)' != ''">
26+
<Content Include="$(PkgLVGLSharp_Native)\runtimes\**\*">
27+
<Link>runtimes\%(RecursiveDir)%(Filename)%(Extension)</Link>
28+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
29+
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
30+
<Visible>false</Visible>
31+
</Content>
32+
</ItemGroup>
33+
2534
</Project>
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
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+
}

src/LVGLSharp.Runtime.Linux/LinuxView.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ public LinuxView(string fbdev = "/dev/fb0", string indev = "/dev/input/event0",
5252

5353
public void Init()
5454
{
55+
LvglNativeLibraryResolver.EnsureRegistered();
5556
startTick = Environment.TickCount;
5657
lv_init();
5758
lv_tick_set_cb(&my_tick);

src/LVGLSharp.Windows/Win32Window.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,7 @@ public Win32Window(string title, uint width, uint height)
433433

434434
public void Init()
435435
{
436+
LvglNativeLibraryResolver.EnsureRegistered();
436437
startTick = Environment.TickCount;
437438

438439
wndProcDelegate = MyWndProc;

0 commit comments

Comments
 (0)