Skip to content

Commit 121ee19

Browse files
committed
update(managed): Various Fixes
1 parent 9e606be commit 121ee19

File tree

12 files changed

+83191
-83161
lines changed

12 files changed

+83191
-83161
lines changed

src/managed/API/Plugin.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public void Initialize(IntPtr ctx)
2727
PluginContext = ctx;
2828
Cacher.PrepareCache();
2929
Console.SetOut(new Scripting.ConsoleWriter(Console.Out));
30-
Events.Listener.StartListening();
30+
EventsListener.StartListening();
3131

3232
RegisterEventAttribute();
3333
RegisterCommandAttribute();

src/managed/API/SDK/CS2/Classes.cs

Lines changed: 83119 additions & 83119 deletions
Large diffs are not rendered by default.

src/managed/API/Scripting/Configuration.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,14 @@ public static bool Exists(string key)
2121
InitializeContext();
2222
return Internal_API.Invoker.CallNative<bool>("Configuration", "Exists", Internal_API.CallKind.CoreClassFunction, _ctx, key);
2323
}
24-
public static T? Fetch<T>(string key)
24+
public static unsafe T? Fetch<T>(string key)
2525
{
2626
InitializeContext();
27-
return Internal_API.Invoker.CallNative<T>("Configuration", "Fetch", Internal_API.CallKind.CoreClassFunction, _ctx, key);
27+
IntPtr val = Internal_API.Invoker.CallNative<IntPtr>("Configuration", "Fetch", Internal_API.CallKind.CoreClassFunction, _ctx, key);
28+
byte* p = (byte*)val;
29+
byte* tp = (byte*)&p;
30+
Type t = typeof(T);
31+
return (T?)CallContext.ReadValue(ref t, ref tp);
2832
}
2933
public static int FetchArraySize(string key)
3034
{

src/managed/API/Scripting/Convars.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,14 @@ public static bool ExistsFake(string name)
4646
InitializeContext();
4747
return Internal_API.Invoker.CallNative<bool>("Convars", "ExistsFake", Internal_API.CallKind.CoreClassFunction, _ctx, name);
4848
}
49-
public static T? Get<T>(string name)
49+
public static unsafe T? Get<T>(string name)
5050
{
5151
InitializeContext();
52-
return Internal_API.Invoker.CallNative<T>("Convars", "Get", Internal_API.CallKind.CoreClassFunction, _ctx, name);
52+
IntPtr val = Internal_API.Invoker.CallNative<IntPtr>("Convars", "Get", Internal_API.CallKind.CoreClassFunction, _ctx, name);
53+
byte* p = (byte*)val;
54+
byte* tp = (byte*)&p;
55+
Type t = typeof(T);
56+
return (T?)CallContext.ReadValue(ref t, ref tp);
5357
}
5458
public static long GetFlags(string name)
5559
{

src/managed/API/Scripting/Database.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ private static void InitializeContext()
3030

3131
public class DB : ClassData
3232
{
33-
public DB(string connection_name): base(Internal_API.Invoker.CallNative<IntPtr>("DB", "DB", CallKind.ClassFunction, connection_name))
33+
public DB(string connection_name, bool? skip_default_connection): base(Internal_API.Invoker.CallNative<IntPtr>("DB", "DB", CallKind.ClassFunction, connection_name, skip_default_connection))
3434
{
3535
InitializeContext();
3636
}

src/managed/API/Scripting/Events.cs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,12 @@ public static List<EventHandler> GetEventCallbacks(string event_name)
8181

8282
public static unsafe (EventResult, Event) TriggerEvent(string event_name, params object[] args)
8383
{
84-
IntPtr[] a = Invoker.CallNative<IntPtr[]>("_G", "TriggerEventInternal", CallKind.Function, event_name, JsonSerializer.Serialize(args));
84+
IntPtr[] a = Invoker.CallNative<IntPtr[]>("_G", "TriggerEventInternal", CallKind.Function, event_name, JsonSerializer.Serialize(args)) ?? [IntPtr.Zero, IntPtr.Zero];
8585

8686
byte* p = (byte*)a[0];
87+
byte* pptr = (byte*)&p;
8788
Type evResult = typeof(EventResult);
88-
EventResult res = (EventResult)CallContext.ReadValue(ref evResult, ref p);
89+
EventResult res = (EventResult)CallContext.ReadValue(ref evResult, ref pptr);
8990
Event ev = new(a[1]);
9091

9192
return (res, ev);
@@ -154,9 +155,13 @@ public string GetString(string key)
154155
{
155156
return Internal_API.Invoker.CallNative<string>("Event", "GetString", Internal_API.CallKind.ClassFunction, m_classData, key) ?? "";
156157
}
157-
public T? GetReturn<T>()
158+
public unsafe T? GetReturn<T>()
158159
{
159-
return Internal_API.Invoker.CallNative<T>("Event", "GetReturn", Internal_API.CallKind.ClassFunction, m_classData);
160+
IntPtr val = Internal_API.Invoker.CallNative<IntPtr>("Event", "GetReturn", Internal_API.CallKind.ClassFunction, m_classData);
161+
byte* p = (byte*)val;
162+
byte* tp = (byte*)&p;
163+
Type t = typeof(T);
164+
return (T?)CallContext.ReadValue(ref t, ref tp);
160165
}
161166
public void SetReturn(object value)
162167
{
@@ -242,9 +247,13 @@ public Memory GetHookPointer(uint index)
242247
{
243248
return Internal_API.Invoker.CallNative<Memory>("Event", "GetHookPointer", Internal_API.CallKind.ClassFunction, m_classData, index) ?? new();
244249
}
245-
public T? GetHookReturn<T>()
250+
public unsafe T? GetHookReturn<T>()
246251
{
247-
return Internal_API.Invoker.CallNative<T>("Event", "GetHookReturn", Internal_API.CallKind.ClassFunction, m_classData);
252+
IntPtr val = Internal_API.Invoker.CallNative<IntPtr>("Event", "GetHookReturn", Internal_API.CallKind.ClassFunction, m_classData);
253+
byte* p = (byte*)val;
254+
byte* tp = (byte*)&p;
255+
Type t = typeof(T);
256+
return (T?)CallContext.ReadValue(ref t, ref tp);
248257
}
249258
public void SetHookReturn(object value)
250259
{

src/managed/API/Scripting/Exports.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public static void Call(string plugin_name, string name, params object[] args)
1818
var output = Events.TriggerEvent($"export:{plugin_name}:{name}", args);
1919
if (output.Item1 != EventResult.Stop)
2020
{
21-
Console.WriteLine($"Export '{name}' is '{plugin_name}' doesn't exists.");
21+
Console.WriteLine($"Export '{name}' in '{plugin_name}' doesn't exists.");
2222
}
2323
}
2424

@@ -27,7 +27,7 @@ public static void Call(string plugin_name, string name, params object[] args)
2727
var output = Events.TriggerEvent($"export:{plugin_name}:{name}", args);
2828
if (output.Item1 != EventResult.Stop)
2929
{
30-
Console.WriteLine($"Export '{name}' is '{plugin_name}' doesn't exists.");
30+
Console.WriteLine($"Export '{name}' in '{plugin_name}' doesn't exists.");
3131
}
3232
return output.Item2.GetReturn<T>();
3333
}

src/managed/API/Scripting/Hooks.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,14 @@ public static void CallHook(HookHandler hookHandler, params object[] args)
6363
Invoker.CallNative("Hooks", "Call", Internal_API.CallKind.CoreClassFunction, _ctx, hookHandler.HookId, (object)args);
6464
}
6565

66-
public static T? CallHook<T>(HookHandler hookHandler, params object[] args)
66+
public static unsafe T? CallHook<T>(HookHandler hookHandler, params object[] args)
6767
{
6868
InitializeContext();
69-
return Invoker.CallNative<T>("Hooks", "Call", Internal_API.CallKind.CoreClassFunction, _ctx, hookHandler.HookId, (object)args);
69+
IntPtr val = Internal_API.Invoker.CallNative<IntPtr>("Hooks", "Call", Internal_API.CallKind.CoreClassFunction, _ctx, hookHandler.HookId, (object)args);
70+
byte* p = (byte*)val;
71+
byte* tp = (byte*)&p;
72+
Type t = typeof(T);
73+
return (T?)CallContext.ReadValue(ref t, ref tp);
7074
}
7175
}
7276
}

src/managed/API/Scripting/Player.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,13 @@ public string GetSteamID2()
107107

108108
return Internal_API.Invoker.CallNative<string>("Player", "GetSteamID2", Internal_API.CallKind.ClassFunction, m_classData) ?? "";
109109
}
110-
public T? GetVar<T>(string key)
110+
public unsafe T? GetVar<T>(string key)
111111
{
112-
return Internal_API.Invoker.CallNative<T>("Player", "GetVar", Internal_API.CallKind.ClassFunction, m_classData, key);
112+
IntPtr val = Internal_API.Invoker.CallNative<IntPtr>("Player", "GetVar", Internal_API.CallKind.CoreClassFunction, m_classData, key);
113+
byte* p = (byte*)val;
114+
byte* tp = (byte*)&p;
115+
Type t = typeof(T);
116+
return (T?)CallContext.ReadValue(ref t, ref tp);
113117
}
114118
public ulong GetVoiceFlags()
115119
{

src/managed/Internal_API/CallContext.cs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -141,10 +141,14 @@ public unsafe CallContext(CallData* data)
141141
private readonly object ms_lock = new();
142142
internal object Lock => ms_lock;
143143
internal bool isCleanupLocked = false;
144+
internal int lockedCount = 0;
144145

145146
public void SetCleanupLock(bool state)
146147
{
147-
isCleanupLocked = state;
148+
if (state) lockedCount++;
149+
else lockedCount--;
150+
151+
isCleanupLocked = (lockedCount > 0);
148152
}
149153

150154
public void Reset()
@@ -629,17 +633,17 @@ internal unsafe object GetArgument(Type type, int index)
629633
}
630634

631635
[SecuritySafeCritical]
632-
public T GetReturn<T>()
636+
public T? GetReturn<T>()
633637
{
634-
return (T)GetReturn(typeof(T));
638+
return (T?)GetReturn(typeof(T));
635639
}
636640

637641
[SecurityCritical]
638-
internal unsafe object GetReturn(Type type)
642+
internal unsafe object? GetReturn(Type type)
639643
{
640644
fixed (CallData* data = &m_cdata)
641645
{
642-
if (data->has_return == 0) return null;
646+
if (data->has_return == 0) return Activator.CreateInstance(type);
643647
byte* p = data->return_value;
644648
return ReadValue(ref type, ref p);
645649
}
@@ -655,7 +659,8 @@ public static unsafe object ReadValue(ref Type type, ref byte* ptr)
655659
StringData* data = (StringData*)p;
656660

657661
if (data->len == 0) return "";
658-
return Encoding.UTF8.GetString(new ReadOnlySpan<byte>(data->ptr, data->len));
662+
string outs = Encoding.UTF8.GetString(new ReadOnlySpan<byte>(data->ptr, data->len));
663+
return outs;
659664
}
660665
else if(type == typeof(ClassData) || typeof(ClassData).IsAssignableFrom(type))
661666
{
@@ -739,8 +744,7 @@ public static unsafe object ReadValue(ref Type type, ref byte* ptr)
739744
}
740745
else if(type == typeof(IntPtr))
741746
{
742-
ref byte start = ref ptr[0];
743-
return Unsafe.ReadUnaligned<IntPtr>(ref start);
747+
return *(IntPtr*)ptr;
744748
}
745749

746750
return ReadPointerToPrimitive(ref ptr, ref type);

0 commit comments

Comments
 (0)