Skip to content

Commit

Permalink
Make NetworkedVector support Vector and QAngle (#728)
Browse files Browse the repository at this point in the history
  • Loading branch information
Yarukon authored Jan 8, 2025
1 parent 9b4ee72 commit bd1105d
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
12 changes: 8 additions & 4 deletions managed/CounterStrikeSharp.API/Core/Model/NetworkedVector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,12 @@ namespace CounterStrikeSharp.API.Core;

public partial class NetworkedVector<T> : NativeObject, IReadOnlyCollection<T>
{
private readonly bool IsValidType;

public NetworkedVector(IntPtr pointer) : base(pointer)
{
Type t = typeof(T);
IsValidType = (t.IsGenericType && t.GetGenericTypeDefinition() == typeof(CHandle<>)) || t == typeof(Vector) || t == typeof(QAngle);
}

public unsafe uint Size => Unsafe.Read<uint>((void*)Handle);
Expand All @@ -24,11 +28,11 @@ public T this[int index]
{
get
{
if (!typeof(T).IsGenericType || typeof(T).GetGenericTypeDefinition() != typeof(CHandle<>))
if (IsValidType)
{
throw new NotSupportedException("Networked vectors currently only support CHandle<T>");
throw new NotSupportedException("Networked vectors currently only support CHandle<T>, Vector, or QAngle");
}

return (T)Activator.CreateInstance(typeof(T), NativeAPI.GetNetworkVectorElementAt(Handle, index));
}
}
Expand All @@ -50,4 +54,4 @@ IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
}
4 changes: 2 additions & 2 deletions src/scripting/natives/natives_memory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,15 +140,15 @@ int GetNetworkVectorSize(ScriptContext& script_context)

void* GetNetworkVectorElementAt(ScriptContext& script_context)
{
auto vec = script_context.GetArgument<CUtlVector<CEntityHandle>*>(0);
auto vec = script_context.GetArgument<CUtlVector<void*>*>(0);
auto index = script_context.GetArgument<int>(1);

return &vec->Element(index);
}

void RemoveAllNetworkVectorElements(ScriptContext& script_context)
{
auto vec = script_context.GetArgument<CUtlVector<CEntityHandle>*>(0);
auto vec = script_context.GetArgument<CUtlVector<void*>*>(0);

vec->RemoveAll();
}
Expand Down

0 comments on commit bd1105d

Please sign in to comment.