Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions managed/CounterStrikeSharp.API/Core/Listeners.g.cs
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,15 @@ public class Listeners
[ListenerName("CheckTransmit")]
public delegate void CheckTransmit([CastFrom(typeof(nint))] CCheckTransmitInfoList infoList);

/// <summary>
/// Called in the entity pre-think when the player's buttons have changed.
/// </summary>
/// <param name="player"></param>
/// <param name="pressed"></param>
/// <param name="released"></param>
[ListenerName("OnPlayerButtonsChanged")]
public delegate void OnPlayerButtonsChanged(CCSPlayerController player, PlayerButtons pressed, PlayerButtons released);

/// <summary>
/// Called when all metamod plugins are loaded.
/// </summary>
Expand Down
170 changes: 84 additions & 86 deletions managed/CounterStrikeSharp.API/Core/ScriptContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,12 @@ public unsafe struct fxScriptContext

public ulong nativeIdentifier;
public fixed byte functionData[8 * 32];
public fixed byte result[8];
}
public fixed byte result[8];
}

public class ScriptContext
{
[ThreadStatic]
private static ScriptContext _globalScriptContext;
[ThreadStatic] private static ScriptContext _globalScriptContext;

public static ScriptContext GlobalScriptContext
{
Expand All @@ -67,14 +66,14 @@ public static ScriptContext GlobalScriptContext
}
}

public unsafe ScriptContext()
public unsafe ScriptContext()
{
}

public unsafe ScriptContext(fxScriptContext* context)
{
m_extContext = *context;
}
public unsafe ScriptContext(fxScriptContext* context)
{
m_extContext = *context;
}

private readonly ConcurrentQueue<Action> ms_finalizers = new ConcurrentQueue<Action>();

Expand All @@ -84,7 +83,7 @@ public unsafe ScriptContext(fxScriptContext* context)

internal fxScriptContext m_extContext = new fxScriptContext();

internal bool isCleanupLocked = false;
internal bool isCleanupLocked = false;

[SecuritySafeCritical]
public void Reset()
Expand All @@ -97,23 +96,23 @@ private void InternalReset()
{
m_extContext.numArguments = 0;
m_extContext.numResults = 0;
m_extContext.hasError = 0;
//CleanUp();
}
m_extContext.hasError = 0;
//CleanUp();
}

[SecuritySafeCritical]
public void Invoke()
{
if (!isCleanupLocked)
{
isCleanupLocked = true;
InvokeNativeInternal();
GlobalCleanUp();
isCleanupLocked = false;
return;
}
if (!isCleanupLocked)
{
isCleanupLocked = true;
InvokeNativeInternal();
GlobalCleanUp();
isCleanupLocked = false;
return;
}

InvokeNativeInternal();
InvokeNativeInternal();
}

[SecurityCritical]
Expand Down Expand Up @@ -153,13 +152,13 @@ public void Push(object arg)
PushInternal(arg);
}

[SecuritySafeCritical]
[SecuritySafeCritical]
public unsafe void SetResult(object arg, fxScriptContext* cxt)
{
SetResultInternal(cxt, arg);
SetResultInternal(cxt, arg);
}

[SecurityCritical]
[SecurityCritical]
private unsafe void PushInternal(object arg)
{
fixed (fxScriptContext* context = &m_extContext)
Expand Down Expand Up @@ -188,7 +187,6 @@ public unsafe void CheckErrors()
throw new NativeException(error);
}
}

}

[SecurityCritical]
Expand Down Expand Up @@ -217,15 +215,15 @@ internal unsafe void Push(fxScriptContext* context, object arg)

return;
}
else if (arg is IMarshalToNative marshalToNative)
{
foreach (var value in marshalToNative.GetNativeObject())
{
Push(context ,value);
}
else if (arg is IMarshalToNative marshalToNative)
{
foreach (var value in marshalToNative.GetNativeObject())
{
Push(context, value);
}

return;
}
return;
}
else if (arg is NativeObject nativeObject)
{
Push(context, (InputArgument)nativeObject);
Expand All @@ -245,38 +243,38 @@ internal unsafe void Push(fxScriptContext* context, object arg)
context->numArguments++;
}

[SecurityCritical]
internal unsafe void SetResultInternal(fxScriptContext* context, object arg)
{
if (arg == null)
{
arg = 0;
}
[SecurityCritical]
internal unsafe void SetResultInternal(fxScriptContext* context, object arg)
{
if (arg == null)
{
arg = 0;
}

if (arg.GetType().IsEnum)
{
arg = Convert.ChangeType(arg, arg.GetType().GetEnumUnderlyingType());
}
if (arg.GetType().IsEnum)
{
arg = Convert.ChangeType(arg, arg.GetType().GetEnumUnderlyingType());
}

if (arg is string)
{
var str = (string)Convert.ChangeType(arg, typeof(string));
SetResultString(context, str);
if (arg is string)
{
var str = (string)Convert.ChangeType(arg, typeof(string));
SetResultString(context, str);

return;
}
else if (arg is InputArgument ia)
{
SetResultInternal(context, ia.Value);
return;
}
else if (arg is InputArgument ia)
{
SetResultInternal(context, ia.Value);

return;
}
return;
}

if (Marshal.SizeOf(arg.GetType()) <= 8)
{
SetResultUnsafe(context, arg);
}
}
if (Marshal.SizeOf(arg.GetType()) <= 8)
{
SetResultUnsafe(context, arg);
}
}

[SecurityCritical]
internal unsafe void PushUnsafe(fxScriptContext* cxt, object arg)
Expand All @@ -285,14 +283,14 @@ internal unsafe void PushUnsafe(fxScriptContext* cxt, object arg)
Marshal.StructureToPtr(arg, new IntPtr(cxt->functionData + (8 * cxt->numArguments)), true);
}

[SecurityCritical]
internal unsafe void SetResultUnsafe(fxScriptContext* cxt, object arg)
{
*(long*)(&cxt->result[0]) = 0;
Marshal.StructureToPtr(arg, new IntPtr(cxt->result), true);
}
[SecurityCritical]
internal unsafe void SetResultUnsafe(fxScriptContext* cxt, object arg)
{
*(long*)(&cxt->result[0]) = 0;
Marshal.StructureToPtr(arg, new IntPtr(cxt->result), true);
}

[SecurityCritical]
[SecurityCritical]
internal unsafe void PushString(string str)
{
fixed (fxScriptContext* cxt = &m_extContext)
Expand Down Expand Up @@ -326,28 +324,28 @@ internal unsafe void PushString(fxScriptContext* cxt, string str)
cxt->numArguments++;
}

[SecurityCritical]
internal unsafe void SetResultString(fxScriptContext* cxt, string str)
{
var ptr = IntPtr.Zero;
[SecurityCritical]
internal unsafe void SetResultString(fxScriptContext* cxt, string str)
{
var ptr = IntPtr.Zero;

if (str != null)
{
var b = Encoding.UTF8.GetBytes(str);
if (str != null)
{
var b = Encoding.UTF8.GetBytes(str);

ptr = Marshal.AllocHGlobal(b.Length + 1);
ptr = Marshal.AllocHGlobal(b.Length + 1);

Marshal.Copy(b, 0, ptr, b.Length);
Marshal.WriteByte(ptr, b.Length, 0);
Marshal.Copy(b, 0, ptr, b.Length);
Marshal.WriteByte(ptr, b.Length, 0);

ms_finalizers.Enqueue(() => Free(ptr));
}
ms_finalizers.Enqueue(() => Free(ptr));
}

unsafe
{
*(IntPtr*)(&cxt->result[8]) = ptr;
}
}
unsafe
{
*(IntPtr*)(&cxt->result[8]) = ptr;
}
}

[SecuritySafeCritical]
private void Free(IntPtr ptr)
Expand Down Expand Up @@ -464,7 +462,7 @@ internal unsafe object GetResult(Type type, byte* ptr)

if (type.IsEnum)
{
return Enum.ToObject(type, (int)GetResult(typeof(int), ptr));
return Enum.ToObject(type, GetResult(type.GetEnumUnderlyingType(), ptr));
}

if (Marshal.SizeOf(type) <= 8)
Expand Down
Loading
Loading