Skip to content

Commit c182dc8

Browse files
committed
Added dynamically loading paths, changed namespace
1 parent f09a8cd commit c182dc8

File tree

6 files changed

+131
-81
lines changed

6 files changed

+131
-81
lines changed

src/Common.cs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
using System.Reflection;
2+
using System.Runtime.InteropServices;
3+
4+
namespace LuaNET;
5+
6+
public static class Imports
7+
{
8+
9+
internal static nint ImportResolver(string libName, Assembly assembly, DllImportSearchPath? searchPath)
10+
{
11+
nint libHandle = (nint) 0;
12+
if (libName != "lua51" && libName != "lua515" && libName != "lua524" && libName != "lua536" && libName != "lua546")
13+
return libHandle;
14+
15+
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
16+
{
17+
if (Environment.Is64BitProcess)
18+
{
19+
NativeLibrary.TryLoad($"./native/win-x64/{libName}.dll", out libHandle);
20+
}
21+
else
22+
{
23+
NativeLibrary.TryLoad($"./native/win-x86/{libName}.dll", out libHandle);
24+
}
25+
}
26+
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
27+
{
28+
NativeLibrary.TryLoad($"./native/linux-x64/{libName}.so", out libHandle);
29+
}
30+
else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
31+
{
32+
NativeLibrary.TryLoad($"./native/macos-x64/{libName}.dylib", out libHandle);
33+
}
34+
else if (RuntimeInformation.IsOSPlatform(OSPlatform.FreeBSD))
35+
{
36+
NativeLibrary.TryLoad($"./native/freebsd-x64/{libName}.so", out libHandle);
37+
}
38+
return libHandle;
39+
}
40+
41+
}

src/Lua51.cs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
using lua_Number = System.Double;
55
using lua_Integer = System.Int64;
66

7-
namespace Lua51;
7+
namespace LuaNET.Lua51;
88

99

1010
public struct lua_State
@@ -146,7 +146,7 @@ public static int lua_upvalueindex(int i)
146146
public static extern lua_State _lua_newstate(nint f, nuint ud);
147147
public static lua_State lua_newstate(lua_Alloc? f, nuint ud)
148148
{
149-
return _lua_newstate(f == null ? nint.Zero : Marshal.GetFunctionPointerForDelegate<lua_Alloc>(f), ud);
149+
return _lua_newstate(f == null ? ((nint) 0) : Marshal.GetFunctionPointerForDelegate<lua_Alloc>(f), ud);
150150
}
151151

152152
[DllImport(DllName, CallingConvention = Convention)]
@@ -159,8 +159,8 @@ public static lua_State lua_newstate(lua_Alloc? f, nuint ud)
159159
public static extern nint _lua_atpanic(lua_State L, nint panicf);
160160
public static lua_CFunction? lua_atpanic(lua_State L, lua_CFunction? panicf)
161161
{
162-
nint panic = _lua_atpanic(L, panicf == null ? nint.Zero : Marshal.GetFunctionPointerForDelegate<lua_CFunction>(panicf));
163-
return panic == nint.Zero ? null : Marshal.GetDelegateForFunctionPointer<lua_CFunction>(panic);
162+
nint panic = _lua_atpanic(L, panicf == null ? ((nint) 0) : Marshal.GetFunctionPointerForDelegate<lua_CFunction>(panicf));
163+
return panic == ((nint) 0) ? null : Marshal.GetDelegateForFunctionPointer<lua_CFunction>(panic);
164164
}
165165

166166
[DllImport(DllName, CallingConvention = Convention)]
@@ -242,7 +242,7 @@ public static lua_State lua_newstate(lua_Alloc? f, nuint ud)
242242
public static lua_CFunction? lua_tocfunction(lua_State L, int idx)
243243
{
244244
nint ret = _lua_tocfunction(L, idx);
245-
return ret == nint.Zero ? null : Marshal.GetDelegateForFunctionPointer<lua_CFunction>(ret);
245+
return ret == ((nint) 0) ? null : Marshal.GetDelegateForFunctionPointer<lua_CFunction>(ret);
246246
}
247247

248248
[DllImport(DllName, CallingConvention = Convention)]
@@ -285,7 +285,7 @@ public static lua_State lua_newstate(lua_Alloc? f, nuint ud)
285285
public static extern void _lua_pushcclosure(lua_State L, nint fn, int n);
286286
public static void lua_pushcclosure(lua_State L, lua_CFunction? fn, int n)
287287
{
288-
_lua_pushcclosure(L, fn == null ? nint.Zero : Marshal.GetFunctionPointerForDelegate(fn), n);
288+
_lua_pushcclosure(L, fn == null ? ((nint) 0) : Marshal.GetFunctionPointerForDelegate(fn), n);
289289
}
290290

291291
[DllImport(DllName, CallingConvention = Convention)]
@@ -349,21 +349,21 @@ public static void lua_pushcclosure(lua_State L, lua_CFunction? fn, int n)
349349
public static extern int _lua_cpcall(lua_State L, nint func, nuint ud);
350350
public static int lua_cpcall(lua_State L, lua_CFunction? func, nuint ud)
351351
{
352-
return _lua_cpcall(L, func == null ? nint.Zero : Marshal.GetFunctionPointerForDelegate<lua_CFunction>(func), ud);
352+
return _lua_cpcall(L, func == null ? ((nint) 0) : Marshal.GetFunctionPointerForDelegate<lua_CFunction>(func), ud);
353353
}
354354

355355
[DllImport(DllName, CallingConvention = Convention, EntryPoint = "lua_load")]
356356
public static extern int _lua_load(lua_State L, nint reader, nuint dt, string chunkname);
357357
public static int lua_load(lua_State L, lua_Reader? reader, nuint dt, string chunkname)
358358
{
359-
return _lua_load(L, reader == null ? nint.Zero : Marshal.GetFunctionPointerForDelegate<lua_Reader>(reader), dt, chunkname);
359+
return _lua_load(L, reader == null ? ((nint) 0) : Marshal.GetFunctionPointerForDelegate<lua_Reader>(reader), dt, chunkname);
360360
}
361361

362362
[DllImport(DllName, CallingConvention = Convention, EntryPoint = "lua_dump")]
363363
public static extern int _lua_dump(lua_State L, nint writer, nuint data);
364364
public static int lua_dump(lua_State L, lua_Writer? writer, nuint data)
365365
{
366-
return _lua_dump(L, writer == null ? nint.Zero : Marshal.GetFunctionPointerForDelegate<lua_Writer>(writer), data);
366+
return _lua_dump(L, writer == null ? ((nint) 0) : Marshal.GetFunctionPointerForDelegate<lua_Writer>(writer), data);
367367
}
368368

369369
[DllImport(DllName, CallingConvention = Convention)]
@@ -403,7 +403,7 @@ public static int lua_dump(lua_State L, lua_Writer? writer, nuint data)
403403
public static extern void _lua_setallocf(lua_State L, nint f, nuint ud);
404404
public static void lua_setallocf(lua_State L, lua_Alloc? f, nuint ud)
405405
{
406-
_lua_setallocf(L, f == null ? nint.Zero : Marshal.GetFunctionPointerForDelegate<lua_Alloc>(f), ud);
406+
_lua_setallocf(L, f == null ? ((nint) 0) : Marshal.GetFunctionPointerForDelegate<lua_Alloc>(f), ud);
407407
}
408408

409409
public static void lua_pop(lua_State L, int n)
@@ -560,15 +560,15 @@ public static int lua_getgccount(lua_State L)
560560
public static extern int _lua_sethook(lua_State L, nint func, int mask, int count);
561561
public static int lua_sethook(lua_State L, lua_Hook? func, int mask, int count)
562562
{
563-
return _lua_sethook(L, func == null ? nint.Zero : Marshal.GetFunctionPointerForDelegate<lua_Hook>(func), mask, count);
563+
return _lua_sethook(L, func == null ? ((nint) 0) : Marshal.GetFunctionPointerForDelegate<lua_Hook>(func), mask, count);
564564
}
565565

566566
[DllImport(DllName, CallingConvention = Convention, EntryPoint = "lua_gethook")]
567567
public static extern nint _lua_gethook(lua_State L);
568568
public static lua_Hook? lua_gethook(lua_State L)
569569
{
570570
nint ret = _lua_gethook(L);
571-
return ret == nint.Zero ? null : Marshal.GetDelegateForFunctionPointer<lua_Hook>(ret);
571+
return ret == ((nint) 0) ? null : Marshal.GetDelegateForFunctionPointer<lua_Hook>(ret);
572572
}
573573

574574
[DllImport(DllName, CallingConvention = Convention)]
@@ -763,7 +763,7 @@ public static T luaL_opt<T>(lua_State L, luaL_Function<T> f, int n, T d)
763763

764764
public static void luaL_addchar(luaL_Buffer B, byte c)
765765
{
766-
if (B.p.ToInt64() >= B.buffer.Length + LUAL_BUFFERSIZE)
766+
if (B.p >= B.buffer.Length + LUAL_BUFFERSIZE)
767767
luaL_prepbuffer(B);
768768
Marshal.WriteByte(B.p, c);
769769
B.p += 1;

src/Lua52.cs

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
using lua_Integer = System.Int64;
66
using lua_Unsigned = System.UInt64;
77

8-
namespace Lua52;
8+
namespace LuaNET.Lua52;
99

1010

1111
public struct lua_State
@@ -169,7 +169,7 @@ public static int lua_upvalueindex(int i)
169169
public static extern lua_State _lua_newstate(nint f, nuint ud);
170170
public static lua_State lua_newstate(lua_Alloc? f, nuint ud)
171171
{
172-
return _lua_newstate(f == null ? nint.Zero : Marshal.GetFunctionPointerForDelegate<lua_Alloc>(f), ud);
172+
return _lua_newstate(f == null ? ((nint) 0) : Marshal.GetFunctionPointerForDelegate<lua_Alloc>(f), ud);
173173
}
174174

175175
[DllImport(DllName, CallingConvention = Convention)]
@@ -182,16 +182,16 @@ public static lua_State lua_newstate(lua_Alloc? f, nuint ud)
182182
public static extern nint _lua_atpanic(lua_State L, nint panicf);
183183
public static lua_CFunction? lua_atpanic(lua_State L, lua_CFunction? panicf)
184184
{
185-
nint panic = _lua_atpanic(L, panicf == null ? nint.Zero : Marshal.GetFunctionPointerForDelegate<lua_CFunction>(panicf));
186-
return panic == nint.Zero ? null : Marshal.GetDelegateForFunctionPointer<lua_CFunction>(panic);
185+
nint panic = _lua_atpanic(L, panicf == null ? ((nint) 0) : Marshal.GetFunctionPointerForDelegate<lua_CFunction>(panicf));
186+
return panic == ((nint) 0) ? null : Marshal.GetDelegateForFunctionPointer<lua_CFunction>(panic);
187187
}
188188

189189
[DllImport(DllName, CallingConvention = Convention, EntryPoint = "lua_version")]
190190
public static extern nint _lua_version(lua_State L);
191191
public static double lua_version(lua_State L)
192192
{
193193
nint mem = _lua_version(L);
194-
if (mem == nint.Zero)
194+
if (mem == ((nint) 0))
195195
return 0;
196196
byte[] arr = new byte[8];
197197
for (int i=0; i<arr.Length; i++)
@@ -278,7 +278,7 @@ public static double lua_version(lua_State L)
278278
public static lua_CFunction? lua_tocfunction(lua_State L, int idx)
279279
{
280280
nint ret = _lua_tocfunction(L, idx);
281-
return ret == nint.Zero ? null : Marshal.GetDelegateForFunctionPointer<lua_CFunction>(ret);
281+
return ret == ((nint) 0) ? null : Marshal.GetDelegateForFunctionPointer<lua_CFunction>(ret);
282282
}
283283

284284
[DllImport(DllName, CallingConvention = Convention)]
@@ -353,7 +353,7 @@ public static double lua_version(lua_State L)
353353
public static extern void _lua_pushcclosure(lua_State L, nint fn, int n);
354354
public static void lua_pushcclosure(lua_State L, lua_CFunction? fn, int n)
355355
{
356-
_lua_pushcclosure(L, fn == null ? nint.Zero : Marshal.GetFunctionPointerForDelegate(fn), n);
356+
_lua_pushcclosure(L, fn == null ? ((nint) 0) : Marshal.GetFunctionPointerForDelegate(fn), n);
357357
}
358358

359359
[DllImport(DllName, CallingConvention = Convention)]
@@ -423,7 +423,7 @@ public static void lua_pushcclosure(lua_State L, lua_CFunction? fn, int n)
423423
public static extern void _lua_callk(lua_State L, int nargs, int nresults, int ctx, nint k);
424424
public static void lua_callk(lua_State L, int nargs, int nresults, int ctx, lua_CFunction? k)
425425
{
426-
_lua_callk(L, nargs, nresults, ctx, k == null ? nint.Zero : Marshal.GetFunctionPointerForDelegate<lua_CFunction>(k));
426+
_lua_callk(L, nargs, nresults, ctx, k == null ? ((nint) 0) : Marshal.GetFunctionPointerForDelegate<lua_CFunction>(k));
427427
}
428428

429429
public static void lua_call(lua_State L, int n, int r)
@@ -438,7 +438,7 @@ public static void lua_call(lua_State L, int n, int r)
438438
public static extern int _lua_pcallk(lua_State L, int nargs, int nresults, int errfunc, int ctx, nint k);
439439
public static int lua_pcallk(lua_State L, int nargs, int nresults, int errfunc, int ctx, lua_CFunction? k)
440440
{
441-
return _lua_pcallk(L, nargs, nresults, errfunc, ctx, k == null ? nint.Zero : Marshal.GetFunctionPointerForDelegate<lua_CFunction>(k));
441+
return _lua_pcallk(L, nargs, nresults, errfunc, ctx, k == null ? ((nint) 0) : Marshal.GetFunctionPointerForDelegate<lua_CFunction>(k));
442442
}
443443

444444
public static int lua_pcall(lua_State L, int n, int r, int f)
@@ -450,21 +450,21 @@ public static int lua_pcall(lua_State L, int n, int r, int f)
450450
public static extern int _lua_load(lua_State L, nint reader, nuint dt, string chunkname, string? mode);
451451
public static int lua_load(lua_State L, lua_Reader? reader, nuint dt, string chunkname, string? mode)
452452
{
453-
return _lua_load(L, reader == null ? nint.Zero : Marshal.GetFunctionPointerForDelegate<lua_Reader>(reader), dt, chunkname, mode);
453+
return _lua_load(L, reader == null ? ((nint) 0) : Marshal.GetFunctionPointerForDelegate<lua_Reader>(reader), dt, chunkname, mode);
454454
}
455455

456456
[DllImport(DllName, CallingConvention = Convention, EntryPoint = "lua_dump")]
457457
public static extern int _lua_dump(lua_State L, nint writer, nuint data);
458458
public static int lua_dump(lua_State L, lua_Writer? writer, nuint data)
459459
{
460-
return _lua_dump(L, writer == null ? nint.Zero : Marshal.GetFunctionPointerForDelegate<lua_Writer>(writer), data);
460+
return _lua_dump(L, writer == null ? ((nint) 0) : Marshal.GetFunctionPointerForDelegate<lua_Writer>(writer), data);
461461
}
462462

463463
[DllImport(DllName, CallingConvention = Convention, EntryPoint = "lua_yieldk")]
464464
public static extern int _lua_yieldk(lua_State L, int nresults, int ctx, nint k);
465465
public static int lua_yieldk(lua_State L, int nresults, int ctx, lua_CFunction? k)
466466
{
467-
return _lua_yieldk(L, nresults, ctx, k == null ? nint.Zero : Marshal.GetFunctionPointerForDelegate<lua_CFunction>(k));
467+
return _lua_yieldk(L, nresults, ctx, k == null ? ((nint) 0) : Marshal.GetFunctionPointerForDelegate<lua_CFunction>(k));
468468
}
469469

470470
public static int lua_yield(lua_State L, int n)
@@ -513,7 +513,7 @@ public static int lua_yield(lua_State L, int n)
513513
public static extern void _lua_setallocf(lua_State L, nint f, nuint ud);
514514
public static void lua_setallocf(lua_State L, lua_Alloc? f, nuint ud)
515515
{
516-
_lua_setallocf(L, f == null ? nint.Zero : Marshal.GetFunctionPointerForDelegate<lua_Alloc>(f), ud);
516+
_lua_setallocf(L, f == null ? ((nint) 0) : Marshal.GetFunctionPointerForDelegate<lua_Alloc>(f), ud);
517517
}
518518

519519
public static double lua_tonumber(lua_State L, int i)
@@ -666,15 +666,15 @@ public static void lua_pushglobaltable(lua_State L)
666666
public static extern int _lua_sethook(lua_State L, nint func, int mask, int count);
667667
public static int lua_sethook(lua_State L, lua_Hook? func, int mask, int count)
668668
{
669-
return _lua_sethook(L, func == null ? nint.Zero : Marshal.GetFunctionPointerForDelegate<lua_Hook>(func), mask, count);
669+
return _lua_sethook(L, func == null ? ((nint) 0) : Marshal.GetFunctionPointerForDelegate<lua_Hook>(func), mask, count);
670670
}
671671

672672
[DllImport(DllName, CallingConvention = Convention, EntryPoint = "lua_gethook")]
673673
public static extern nint _lua_gethook(lua_State L);
674674
public static lua_Hook? lua_gethook(lua_State L)
675675
{
676676
nint ret = _lua_gethook(L);
677-
return ret == nint.Zero ? null : Marshal.GetDelegateForFunctionPointer<lua_Hook>(ret);
677+
return ret == ((nint) 0) ? null : Marshal.GetDelegateForFunctionPointer<lua_Hook>(ret);
678678
}
679679

680680
[DllImport(DllName, CallingConvention = Convention)]
@@ -827,7 +827,7 @@ public static int luaL_loadfile(lua_State L, string f)
827827
public static extern void _luaL_requiref(lua_State L, string modname, nint openf, int glb);
828828
public static void luaL_requiref(lua_State L, string modname, lua_CFunction? openf, int glb)
829829
{
830-
_luaL_requiref(L, modname, openf == null ? nint.Zero : Marshal.GetFunctionPointerForDelegate<lua_CFunction>(openf), glb);
830+
_luaL_requiref(L, modname, openf == null ? ((nint) 0) : Marshal.GetFunctionPointerForDelegate<lua_CFunction>(openf), glb);
831831
}
832832

833833
public static void luaL_newlibtable(lua_State L, luaL_Reg[] l)
@@ -921,7 +921,7 @@ public static void luaL_addchar(luaL_Buffer B, byte c)
921921
{
922922
if (B.n >= B.size)
923923
luaL_prepbuffsize(B, 1);
924-
Marshal.WriteByte(new nint(B.b.ToInt64() + (long) B.n), c);
924+
Marshal.WriteByte(B.b + (nint) B.n, c);
925925
B.n += 1;
926926
}
927927

0 commit comments

Comments
 (0)