Skip to content

Commit bd1105d

Browse files
authored
Make NetworkedVector support Vector and QAngle (#728)
1 parent 9b4ee72 commit bd1105d

File tree

2 files changed

+10
-6
lines changed

2 files changed

+10
-6
lines changed

managed/CounterStrikeSharp.API/Core/Model/NetworkedVector.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,12 @@ namespace CounterStrikeSharp.API.Core;
1212

1313
public partial class NetworkedVector<T> : NativeObject, IReadOnlyCollection<T>
1414
{
15+
private readonly bool IsValidType;
16+
1517
public NetworkedVector(IntPtr pointer) : base(pointer)
1618
{
19+
Type t = typeof(T);
20+
IsValidType = (t.IsGenericType && t.GetGenericTypeDefinition() == typeof(CHandle<>)) || t == typeof(Vector) || t == typeof(QAngle);
1721
}
1822

1923
public unsafe uint Size => Unsafe.Read<uint>((void*)Handle);
@@ -24,11 +28,11 @@ public T this[int index]
2428
{
2529
get
2630
{
27-
if (!typeof(T).IsGenericType || typeof(T).GetGenericTypeDefinition() != typeof(CHandle<>))
31+
if (IsValidType)
2832
{
29-
throw new NotSupportedException("Networked vectors currently only support CHandle<T>");
33+
throw new NotSupportedException("Networked vectors currently only support CHandle<T>, Vector, or QAngle");
3034
}
31-
35+
3236
return (T)Activator.CreateInstance(typeof(T), NativeAPI.GetNetworkVectorElementAt(Handle, index));
3337
}
3438
}
@@ -50,4 +54,4 @@ IEnumerator IEnumerable.GetEnumerator()
5054
{
5155
return GetEnumerator();
5256
}
53-
}
57+
}

src/scripting/natives/natives_memory.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,15 +140,15 @@ int GetNetworkVectorSize(ScriptContext& script_context)
140140

141141
void* GetNetworkVectorElementAt(ScriptContext& script_context)
142142
{
143-
auto vec = script_context.GetArgument<CUtlVector<CEntityHandle>*>(0);
143+
auto vec = script_context.GetArgument<CUtlVector<void*>*>(0);
144144
auto index = script_context.GetArgument<int>(1);
145145

146146
return &vec->Element(index);
147147
}
148148

149149
void RemoveAllNetworkVectorElements(ScriptContext& script_context)
150150
{
151-
auto vec = script_context.GetArgument<CUtlVector<CEntityHandle>*>(0);
151+
auto vec = script_context.GetArgument<CUtlVector<void*>*>(0);
152152

153153
vec->RemoveAll();
154154
}

0 commit comments

Comments
 (0)