Skip to content

Commit 7039c43

Browse files
authored
Merge pull request #91 from SupinePandora43/libtier0_client
Use Correct tier0 on Linux || Use valid path to Tier0 on macos
2 parents 510299f + ba5d9dd commit 7039c43

File tree

2 files changed

+101
-6
lines changed

2 files changed

+101
-6
lines changed

gm_dotnet_managed/GmodNET/GameConsoleWriter.cs

Lines changed: 98 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using GmodNET.API;
12
using System;
23
using System.IO;
34
using System.Runtime.InteropServices;
@@ -13,9 +14,103 @@ internal class GameConsoleWriter : TextWriter
1314
public override string NewLine => "\n";
1415
public override Encoding Encoding => Encoding.UTF8;
1516

16-
#pragma warning disable CA2101
17-
[DllImport("tier0", CallingConvention = CallingConvention.Cdecl)]
18-
private static extern void Msg([MarshalAs(UnmanagedType.LPUTF8Str)] string msg);
17+
public GameConsoleWriter(ILua lua)
18+
{
19+
if (OperatingSystem.IsWindows())
20+
{
21+
Msg = Msg_Tier0;
22+
}
23+
else if (OperatingSystem.IsLinux())
24+
{
25+
lua.PushSpecial(SPECIAL_TABLES.SPECIAL_GLOB);
26+
lua.GetField(-1, "game");
27+
lua.GetField(-1, "IsDedicated");
28+
lua.MCall(0, 1);
29+
bool is_dedicated = lua.GetBool(-1);
30+
lua.Pop(3);
31+
32+
if (is_dedicated)
33+
{
34+
Msg = Msg_Tier0;
35+
}
36+
else
37+
{
38+
Msg = Msg_Tier0_Client;
39+
}
40+
}
41+
else if (OperatingSystem.IsMacOS())
42+
{
43+
unsafe
44+
{
45+
IntPtr lib_handle = NativeLibrary.Load(Directory.GetCurrentDirectory() + "/GarrysMod_Signed.app/Contents/MacOS/libtier0.dylib");
46+
delegate* unmanaged[Cdecl]<byte*, void> msg_func = (delegate* unmanaged[Cdecl]<byte*, void>)NativeLibrary.GetExport(lib_handle, "Msg");
47+
48+
Msg = (message) =>
49+
{
50+
byte* __message_gen_native = default;
51+
//
52+
// Setup
53+
//
54+
bool message__allocated = false;
55+
try
56+
{
57+
//
58+
// Marshal
59+
//
60+
if (message != null)
61+
{
62+
int message__bytelen = (message.Length + 1) * 3 + 1;
63+
if (message__bytelen > 260)
64+
{
65+
__message_gen_native = (byte*)System.Runtime.InteropServices.Marshal.StringToCoTaskMemUTF8(message);
66+
message__allocated = true;
67+
}
68+
else
69+
{
70+
byte* path__stackptr = stackalloc byte[message__bytelen];
71+
{
72+
message__bytelen = System.Text.Encoding.UTF8.GetBytes(message, new System.Span<byte>(path__stackptr, message__bytelen));
73+
path__stackptr[message__bytelen] = 0;
74+
}
75+
76+
__message_gen_native = (byte*)path__stackptr;
77+
}
78+
}
79+
80+
//
81+
// Invoke
82+
//
83+
msg_func(__message_gen_native);
84+
}
85+
finally
86+
{
87+
//
88+
// Cleanup
89+
//
90+
if (message__allocated)
91+
{
92+
System.Runtime.InteropServices.Marshal.FreeCoTaskMem((System.IntPtr)__message_gen_native);
93+
}
94+
}
95+
};
96+
}
97+
}
98+
else Msg = (string msg) => throw new PlatformNotSupportedException();
99+
}
100+
101+
#pragma warning disable CA2101 // workaround for https://github.com/dotnet/roslyn-analyzers/issues/5009
102+
103+
[DllImport("tier0", EntryPoint = "Msg", CallingConvention = CallingConvention.Cdecl)]
104+
private static extern void Msg_Tier0([MarshalAs(UnmanagedType.LPUTF8Str)] string msg);
105+
106+
[DllImport("tier0_client", EntryPoint = "Msg", CallingConvention = CallingConvention.Cdecl)]
107+
private static extern void Msg_Tier0_Client([MarshalAs(UnmanagedType.LPUTF8Str)] string msg);
108+
109+
#pragma warning restore CA2101
110+
111+
private delegate void MsgFunc(string str);
112+
113+
private static MsgFunc Msg;
19114

20115
public override void Write(string value)
21116
{

gm_dotnet_managed/GmodNET/Startup.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ internal static unsafe IntPtr Main(IntPtr lua_base, IntPtr native_version_string
4040
$"Native version: {native_version}");
4141
}
4242

43+
ILua lua = new Lua(lua_base);
44+
4345
if (FirstRun)
4446
{
4547
Span<IntPtr> params_from_native_code = new Span<IntPtr>((void*)param, 55);
@@ -158,15 +160,13 @@ internal static unsafe IntPtr Main(IntPtr lua_base, IntPtr native_version_string
158160

159161
ManagedFunctionMetaMethods.NativeDelegateExecutor = native_delegate_executor_ptr;
160162

161-
Console.SetOut(new GameConsoleWriter());
163+
Console.SetOut(new GameConsoleWriter(lua));
162164

163165
FirstRun = false;
164166
}
165167

166168
*managed_delegate_executor_ptr = (IntPtr)(delegate* unmanaged<IntPtr, int>)&ManagedFunctionMetaMethods.ManagedDelegateExecutor;
167169

168-
ILua lua = new Lua(lua_base);
169-
170170
lua.PushSpecial(SPECIAL_TABLES.SPECIAL_GLOB);
171171
lua.GetField(-1, "print");
172172
lua.PushString(

0 commit comments

Comments
 (0)