Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make NetworkedVector support Vector and QAngle #728

Merged
merged 3 commits into from
Jan 8, 2025
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
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
Loading