Skip to content

Commit 929856c

Browse files
committed
Did the following:
- Added an ImportResolver for Dynamic Library Loading - Renamed DllName in each version to support OS independent loading of SO (not supported yet)
1 parent c182dc8 commit 929856c

File tree

5 files changed

+47
-15
lines changed

5 files changed

+47
-15
lines changed

src/Common.cs

Lines changed: 43 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,36 +6,68 @@ namespace LuaNET;
66
public static class Imports
77
{
88

9-
internal static nint ImportResolver(string libName, Assembly assembly, DllImportSearchPath? searchPath)
9+
private static nint _libHandle_luajit = (nint) 0;
10+
private static nint _libHandle_lua51 = (nint) 0;
11+
private static nint _libHandle_lua52 = (nint) 0;
12+
private static nint _libHandle_lua53 = (nint) 0;
13+
private static nint _libHandle_lua54 = (nint) 0;
14+
15+
private static nint Resolve(string libName, out nint handle)
1016
{
11-
nint libHandle = (nint) 0;
12-
if (libName != "lua51" && libName != "lua515" && libName != "lua524" && libName != "lua536" && libName != "lua546")
13-
return libHandle;
14-
17+
handle = (nint) 0;
1518
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
1619
{
1720
if (Environment.Is64BitProcess)
1821
{
19-
NativeLibrary.TryLoad($"./native/win-x64/{libName}.dll", out libHandle);
22+
NativeLibrary.TryLoad($"./native/win-x64/{libName}.dll", out handle);
2023
}
2124
else
2225
{
23-
NativeLibrary.TryLoad($"./native/win-x86/{libName}.dll", out libHandle);
26+
NativeLibrary.TryLoad($"./native/win-x86/{libName}.dll", out handle);
2427
}
2528
}
2629
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
2730
{
28-
NativeLibrary.TryLoad($"./native/linux-x64/{libName}.so", out libHandle);
31+
NativeLibrary.TryLoad($"./native/linux-x64/{libName}.so", out handle);
2932
}
3033
else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
3134
{
32-
NativeLibrary.TryLoad($"./native/macos-x64/{libName}.dylib", out libHandle);
35+
NativeLibrary.TryLoad($"./native/macos-x64/{libName}.dylib", out handle);
3336
}
3437
else if (RuntimeInformation.IsOSPlatform(OSPlatform.FreeBSD))
3538
{
36-
NativeLibrary.TryLoad($"./native/freebsd-x64/{libName}.so", out libHandle);
39+
NativeLibrary.TryLoad($"./native/freebsd-x64/{libName}.so", out handle);
40+
}
41+
return handle;
42+
}
43+
44+
internal static nint ImportResolver(string libName, Assembly assembly, DllImportSearchPath? searchPath)
45+
{
46+
switch (libName)
47+
{
48+
case "lua51.dll":
49+
if (_libHandle_luajit != 0)
50+
return _libHandle_luajit;
51+
return Resolve(libName, out _libHandle_luajit);
52+
case "lua546.dll":
53+
if (_libHandle_lua54 != 0)
54+
return _libHandle_lua54;
55+
return Resolve(libName, out _libHandle_lua54);
56+
case "lua515.dll":
57+
if (_libHandle_lua51 != 0)
58+
return _libHandle_lua51;
59+
return Resolve(libName, out _libHandle_lua51);
60+
case "lua536.dll":
61+
if (_libHandle_lua53 != 0)
62+
return _libHandle_lua53;
63+
return Resolve(libName, out _libHandle_lua53);
64+
case "lua524.dll":
65+
if (_libHandle_lua52 != 0)
66+
return _libHandle_lua52;
67+
return Resolve(libName, out _libHandle_lua52);
3768
}
38-
return libHandle;
69+
70+
return (nint) 0;
3971
}
4072

4173
}

src/Lua51.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public struct lua_State
1515
public static class Lua
1616
{
1717

18-
private const string DllName = "Lua515.dll";
18+
private const string DllName = "Lua515";
1919
private const CallingConvention Convention = CallingConvention.Cdecl;
2020

2121
[StructLayout(LayoutKind.Sequential, Pack = 1, CharSet = CharSet.Ansi)]

src/Lua52.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public struct lua_State
1616
public static class Lua
1717
{
1818

19-
private const string DllName = "Lua524.dll";
19+
private const string DllName = "Lua524";
2020
private const CallingConvention Convention = CallingConvention.Cdecl;
2121

2222
[StructLayout(LayoutKind.Sequential, Pack = 1, CharSet = CharSet.Ansi)]

src/Lua53.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public struct lua_KContext
2121
public static class Lua
2222
{
2323

24-
private const string DllName = "Lua536.dll";
24+
private const string DllName = "Lua536";
2525
private const CallingConvention Convention = CallingConvention.Cdecl;
2626

2727
[StructLayout(LayoutKind.Sequential, Pack = 1, CharSet = CharSet.Ansi)]

src/Lua54.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public struct lua_KContext
2121
public static class Lua
2222
{
2323

24-
private const string DllName = "Lua546.dll";
24+
private const string DllName = "Lua546";
2525
private const CallingConvention Convention = CallingConvention.Cdecl;
2626

2727
[StructLayout(LayoutKind.Sequential, Pack = 1, CharSet = CharSet.Ansi)]

0 commit comments

Comments
 (0)