Skip to content

Commit cff24f4

Browse files
committed
Revert "Make NetworkedVector support Vector and QAngle (#728)"
This reverts commit bd1105d.
1 parent f05cc5e commit cff24f4

File tree

2 files changed

+25
-24
lines changed

2 files changed

+25
-24
lines changed

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

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

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

2319
public unsafe uint Size => Unsafe.Read<uint>((void*)Handle);
24-
20+
2521
public unsafe int Count => NativeAPI.GetNetworkVectorSize(Handle);
2622

2723
public T this[int index]
2824
{
2925
get
3026
{
31-
if (!IsValidType)
27+
if (!typeof(T).IsGenericType || typeof(T).GetGenericTypeDefinition() != typeof(CHandle<>))
3228
{
33-
throw new NotSupportedException("Networked vectors currently only support CHandle<T>, Vector, or QAngle");
29+
throw new NotSupportedException("Networked vectors currently only support CHandle<T>");
3430
}
35-
31+
3632
return (T)Activator.CreateInstance(typeof(T), NativeAPI.GetNetworkVectorElementAt(Handle, index));
3733
}
3834
}

src/scripting/natives/natives_memory.cpp

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@
1717
#include <ios>
1818
#include <sstream>
1919

20-
#include "scripting/autonative.h"
2120
#include "core/function.h"
22-
#include "scripting/script_engine.h"
23-
#include "core/memory.h"
2421
#include "core/log.h"
22+
#include "core/memory.h"
23+
#include "scripting/autonative.h"
24+
#include "scripting/script_engine.h"
2525

2626
namespace counterstrikesharp {
2727
std::vector<ValveFunction*> m_managed_ptrs;
@@ -44,21 +44,22 @@ ValveFunction* CreateVirtualFunctionBySignature(ScriptContext& script_context)
4444

4545
auto* function_addr = FindSignature(binary_name, signature_hex_string);
4646

47-
if (function_addr == nullptr) {
47+
if (function_addr == nullptr)
48+
{
4849
script_context.ThrowNativeError("Could not find signature %s", signature_hex_string);
4950
return nullptr;
5051
}
5152

5253
auto args = std::vector<DataType_t>();
53-
for (int i = 0; i < num_arguments; i++) {
54+
for (int i = 0; i < num_arguments; i++)
55+
{
5456
args.push_back(script_context.GetArgument<DataType_t>(5 + i));
5557
}
5658

5759
auto function = new ValveFunction(function_addr, CONV_CDECL, args, return_type);
5860
function->SetSignature(signature_hex_string);
5961

60-
CSSHARP_CORE_TRACE("Created virtual function, pointer found at {}, signature {}", function_addr,
61-
signature_hex_string);
62+
CSSHARP_CORE_TRACE("Created virtual function, pointer found at {}, signature {}", function_addr, signature_hex_string);
6263

6364
m_managed_ptrs.push_back(function);
6465
return function;
@@ -72,15 +73,17 @@ ValveFunction* CreateVirtualFunction(ScriptContext& script_context)
7273
auto return_type = script_context.GetArgument<DataType_t>(3);
7374

7475
void** vtable = *(void***)ptr;
75-
if (!vtable) {
76+
if (!vtable)
77+
{
7678
script_context.ThrowNativeError("Failed to get the virtual function table.");
7779
return nullptr;
7880
}
7981

8082
auto function_addr = (void*)vtable[vtable_offset];
8183

8284
auto args = std::vector<DataType_t>();
83-
for (int i = 0; i < num_arguments; i++) {
85+
for (int i = 0; i < num_arguments; i++)
86+
{
8487
args.push_back(script_context.GetArgument<DataType_t>(4 + i));
8588
}
8689

@@ -97,7 +100,8 @@ void HookFunction(ScriptContext& script_context)
97100
auto callback = script_context.GetArgument<CallbackT>(1);
98101
auto post = script_context.GetArgument<bool>(2);
99102

100-
if (!function) {
103+
if (!function)
104+
{
101105
script_context.ThrowNativeError("Invalid function pointer");
102106
return;
103107
}
@@ -111,7 +115,8 @@ void UnhookFunction(ScriptContext& script_context)
111115
auto callback = script_context.GetArgument<CallbackT>(1);
112116
auto post = script_context.GetArgument<bool>(2);
113117

114-
if (!function) {
118+
if (!function)
119+
{
115120
script_context.ThrowNativeError("Invalid function pointer");
116121
return;
117122
}
@@ -123,7 +128,8 @@ void ExecuteVirtualFunction(ScriptContext& script_context)
123128
{
124129
auto function = script_context.GetArgument<ValveFunction*>(0);
125130

126-
if (!function) {
131+
if (!function)
132+
{
127133
script_context.ThrowNativeError("Invalid function pointer");
128134
return;
129135
}
@@ -140,23 +146,22 @@ int GetNetworkVectorSize(ScriptContext& script_context)
140146

141147
void* GetNetworkVectorElementAt(ScriptContext& script_context)
142148
{
143-
auto vec = script_context.GetArgument<CUtlVector<void*>*>(0);
149+
auto vec = script_context.GetArgument<CUtlVector<CEntityHandle>*>(0);
144150
auto index = script_context.GetArgument<int>(1);
145151

146152
return &vec->Element(index);
147153
}
148154

149155
void RemoveAllNetworkVectorElements(ScriptContext& script_context)
150156
{
151-
auto vec = script_context.GetArgument<CUtlVector<void*>*>(0);
157+
auto vec = script_context.GetArgument<CUtlVector<CEntityHandle>*>(0);
152158

153159
vec->RemoveAll();
154160
}
155161

156162
REGISTER_NATIVES(memory, {
157163
ScriptEngine::RegisterNativeHandler("CREATE_VIRTUAL_FUNCTION", CreateVirtualFunction);
158-
ScriptEngine::RegisterNativeHandler("CREATE_VIRTUAL_FUNCTION_BY_SIGNATURE",
159-
CreateVirtualFunctionBySignature);
164+
ScriptEngine::RegisterNativeHandler("CREATE_VIRTUAL_FUNCTION_BY_SIGNATURE", CreateVirtualFunctionBySignature);
160165
ScriptEngine::RegisterNativeHandler("EXECUTE_VIRTUAL_FUNCTION", ExecuteVirtualFunction);
161166
ScriptEngine::RegisterNativeHandler("HOOK_FUNCTION", HookFunction);
162167
ScriptEngine::RegisterNativeHandler("UNHOOK_FUNCTION", UnhookFunction);

0 commit comments

Comments
 (0)