forked from hereisderek/SourceMod.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSMJS_Netprops.cpp
More file actions
430 lines (339 loc) · 14.6 KB
/
SMJS_Netprops.cpp
File metadata and controls
430 lines (339 loc) · 14.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
#include "SMJS_Netprops.h"
#include "SMJS_Entity.h"
#include "SMJS_Plugin.h"
#include "k7v8macros.h"
#include "modules/MEntities.h"
#include "boost/bind.hpp"
WRAPPED_CLS_CPP(SMJS_Netprops, SMJS_BaseWrapped)
struct SMJS_Netprops_CachedValueData {
void *ent;
void *addr;
edict_t *edict;
size_t actual_offset;
SendProp *prop;
};
void DestroyDataCallback(Isolate* isolate, v8::Persistent<v8::Value> object, void *parameter){
delete parameter;
}
void SMJS_NPValueCacher::InsertCachedValue(PLUGIN_ID plId, std::string key, v8::Persistent<v8::Value> value){
auto &vec = cachedValues.find(plId)->second;
vec.insert(std::make_pair(key, value));
}
v8::Persistent<v8::Value> SMJS_NPValueCacher::FindCachedValue(PLUGIN_ID plId, std::string key){
auto &vec = cachedValues.find(plId)->second;
auto it = vec.find(key);
if(it != vec.end()){
return it->second;
}
return v8::Persistent<v8::Value>();
}
void SMJS_NPValueCacher::InitCacheForPlugin(SMJS_Plugin *pl){
cachedValues.insert(std::make_pair(pl->id, std::map<std::string, v8::Persistent<v8::Value>>()));
}
SMJS_Netprops::SMJS_Netprops() {
}
SMJS_Netprops::~SMJS_Netprops(){
for(auto plIt = cachedValues.begin(); plIt != cachedValues.end(); ++plIt){
auto pl = GetPlugin(plIt->first);
for(auto it = plIt->second.begin(); it != plIt->second.end(); ++it){
auto obj = v8::Handle<v8::Object>::Cast(it->second);
if(obj.IsEmpty()) continue;
auto hiddenValue = obj->GetHiddenValue(v8::String::New("SMJS::dataPtr"));
if(hiddenValue.IsEmpty() || hiddenValue->IsUndefined() || hiddenValue->IsNull()) continue;
SMJS_Netprops_CachedValueData *data = (SMJS_Netprops_CachedValueData *) v8::Handle<External>::Cast(hiddenValue)->Value();
it->second.MakeWeak(pl->GetIsolate(), data, DestroyDataCallback);
}
}
}
void SMJS_Netprops::OnWrapperAttached(SMJS_Plugin *plugin, v8::Persistent<v8::Value> wrapper){
SMJS_BaseWrapped::OnWrapperAttached(plugin, wrapper);
InitCacheForPlugin(plugin);
cachedValues.insert(std::make_pair(plugin->id, std::map<std::string, v8::Persistent<v8::Value>>()));
}
v8::Handle<v8::Value> VectorGetter(Local<String> prop, const AccessorInfo& info){
SMJS_Netprops_CachedValueData *data = (SMJS_Netprops_CachedValueData *) v8::Handle<External>::Cast(info.Data())->Value();
Vector *vec = (Vector*)data->addr;
if(prop == v8::String::New("x")){
return v8::Number::New(vec->x);
}else if(prop == v8::String::New("y")){
return v8::Number::New(vec->y);
}else if(prop == v8::String::New("z")){
return v8::Number::New(vec->z);
}
return v8::Undefined();
}
void VectorSetter(Local<String> prop, Local<Value> value, const AccessorInfo& info){
SMJS_Netprops_CachedValueData *data = (SMJS_Netprops_CachedValueData *) v8::Handle<External>::Cast(info.Data())->Value();
Vector *vec = (Vector*)data->addr;
if(prop == v8::String::New("x")){
vec->x = value->ToNumber()->NumberValue();
}else if(prop == v8::String::New("y")){
vec->y = value->ToNumber()->NumberValue();
}else if(prop == v8::String::New("z")){
vec->z = value->ToNumber()->NumberValue();
}
if(data->edict != NULL) gamehelpers->SetEdictStateChanged(data->edict, data->actual_offset);
}
v8::Handle<v8::Value> VectorToString(const v8::Arguments& args) {
char buffer[256];
auto t = args.This();
snprintf(buffer, sizeof(buffer), "[Vector %f %f %f]",
(float) t->Get(v8::String::New("x"))->ToNumber()->NumberValue(),
(float) t->Get(v8::String::New("y"))->ToNumber()->NumberValue(),
(float) t->Get(v8::String::New("z"))->ToNumber()->NumberValue()
);
return v8::String::New(buffer);
}
unsigned int strncopy(char *dest, const char *src, size_t count){
if (!count){
return 0;
}
char *start = dest;
while ((*src) && (--count)){
*dest++ = *src++;
}
*dest = '\0';
return (dest - start);
}
v8::Persistent<v8::Value> SMJS_Netprops::GenerateThenFindCachedValue(PLUGIN_ID plId, std::string key, void *ent, edict_t *edict, SendProp *p, size_t offset){
bool isCacheable;
auto res = v8::Persistent<v8::Value>::New(SMJS_Netprops::SGetNetProp(ent, edict, p, offset, &isCacheable));
if(isCacheable){
InsertCachedValue(plId, key, res);
}
return res;
}
bool SMJS_Netprops::GetEntityPropInfo(void *ent, const char *propName, sm_sendprop_info_t *propInfo){
IServerUnknown *pUnk = (IServerUnknown *)ent;
IServerNetworkable *pNet = pUnk->GetNetworkable();
if(!pNet) return false;
return gamehelpers->FindSendPropInfo(pNet->GetServerClass()->GetName(), propName, propInfo);
}
v8::Handle<v8::Value> SMJS_Netprops::SGetNetProp(v8::Local<v8::String> prop, const v8::AccessorInfo &info){
Local<Value> _intfld = info.This()->GetInternalField(0);
SMJS_Netprops *self = dynamic_cast<SMJS_Netprops*>((SMJS_BaseWrapped*)Handle<External>::Cast(_intfld)->Value());
if(!self->entWrapper->valid) return v8::Undefined();
v8::String::AsciiValue str(prop);
std::string propNameStdString(*str);
auto cachedValue = self->FindCachedValue(GetPluginRunning()->id, propNameStdString);
if(!cachedValue.IsEmpty()) return cachedValue;
sm_sendprop_info_t propInfo;
IServerUnknown *pUnk = (IServerUnknown *)self->entWrapper->ent;
IServerNetworkable *pNet = pUnk->GetNetworkable();
if(!pNet){
THROW("Entity is not networkable");
}
if(!gamehelpers->FindSendPropInfo(pNet->GetServerClass()->GetName(), *str, &propInfo)){
return v8::Undefined();
}
bool isCacheable;
auto ret = SGetNetProp(self->entWrapper->ent, self->entWrapper->edict, propInfo.prop, propInfo.actual_offset, &isCacheable, NULL);
if(isCacheable){
self->InsertCachedValue(GetPluginRunning()->id, propNameStdString, v8::Persistent<v8::Value>::New(ret));
}
return ret;
}
v8::Handle<v8::Value> SMJS_Netprops::SSetNetProp(v8::Local<v8::String> prop, v8::Local<v8::Value> value, const v8::AccessorInfo &info){
Local<Value> _intfld = info.This()->GetInternalField(0);
SMJS_Netprops *self = dynamic_cast<SMJS_Netprops*>((SMJS_BaseWrapped*)Handle<External>::Cast(_intfld)->Value());
if(!self->entWrapper->valid) return v8::Undefined();
std::string propNameStdString(*v8::String::AsciiValue(prop));
sm_sendprop_info_t propInfo;
IServerUnknown *pUnk = (IServerUnknown *)self->entWrapper->ent;
IServerNetworkable *pNet = pUnk->GetNetworkable();
if(!pNet){
THROW("Entity is not networkable");
}
if(!gamehelpers->FindSendPropInfo(pNet->GetServerClass()->GetName(), *v8::String::AsciiValue(prop), &propInfo)){
return v8::Undefined();
}
boost::function<v8::Persistent<v8::Value> ()> f(boost::bind(&SMJS_Netprops::GenerateThenFindCachedValue, self, GetPluginRunning()->id, propNameStdString, self->entWrapper->ent, self->entWrapper->edict, propInfo.prop, propInfo.actual_offset));
return SSetNetProp(self->entWrapper->ent, self->entWrapper->edict, propInfo.prop, propInfo.actual_offset, value, f);
}
v8::Handle<v8::Value> SMJS_Netprops::SSetNetProp(void *ent, edict_t *edict, SendProp *p, size_t offset, v8::Local<v8::Value> value, boost::function<v8::Persistent<v8::Value> ()> getCache, const char *name){
std::string propNameStdString(name != NULL ? name : p->GetName());
int bit_count = p->m_nBits;
bool is_unsigned = ((p->GetFlags() & SPROP_UNSIGNED) == SPROP_UNSIGNED);
void *addr = (void*) ((intptr_t) ent + offset);
switch(p->GetType()){
case DPT_Int: {
// If it's an integer with 21 bits and starts with m_h, it MUST be an entity, if it's not...
// oh well... return an integer
if(bit_count == 21 && strlen(propNameStdString.c_str()) >= 3 && std::strncmp(propNameStdString.c_str(), "m_h", 3) == 0){
CBaseHandle &hndl = *(CBaseHandle *)addr;
if(value->IsNull() || value->IsUndefined()){
hndl.Set(NULL);
}else{
auto obj = value->ToObject();
if(!obj.IsEmpty()){
Local<Value> _intfld = obj->GetInternalField(0);
SMJS_Entity* ent = dynamic_cast<SMJS_Entity*>((SMJS_BaseWrapped*)Handle<External>::Cast(_intfld)->Value());
if(ent == NULL){
return v8::ThrowException(v8::Exception::TypeError(v8::String::New("Entity field can only contain an entity")));
}
IHandleEntity *pHandleEnt = (IHandleEntity *)ent->ent;
hndl.Set(pHandleEnt);
}
}
}
if (bit_count < 1){
*(int32_t * )addr = value->ToInt32()->Int32Value();
}else if (bit_count > 32){
if(!value->IsArray()) THROW("Value must be an array");
auto obj = value->ToObject();
if(obj->Get(v8::String::New("length"))->Uint32Value() != 2) THROW("Value must be an array with 2 elements");
auto lowBits = obj->Get(0);
auto highBits = obj->Get(1);
if(!lowBits->IsInt32() || !highBits->IsInt32()) THROW("Value must be an array with 2 int32 elements");
*(uint32_t * )addr = lowBits->Uint32Value();
*(uint32_t * )((intptr_t) addr + 4) = highBits->ToInt32()->Uint32Value();
}else if (bit_count >= 17){
*(int32_t * )addr = value->ToInt32()->Int32Value();
}else if (bit_count >= 9){
if (is_unsigned){
*(uint16_t * )addr = value->ToInt32()->Int32Value();
}else{
*(int16_t * )addr = value->ToInt32()->Int32Value();
}
}else if (bit_count >= 2){
if (is_unsigned){
*(uint8_t * )addr = value->ToInt32()->Int32Value();
}else{
*(int8_t * )addr = value->ToInt32()->Int32Value();
}
}else{
*(bool *)addr = value->BooleanValue();
}
}; break;
case DPT_Float:
*(float*)addr = (float) value->ToNumber()->NumberValue();
break;
case DPT_Vector: {
auto obj = value->ToObject();
if(obj.IsEmpty()) return v8::ThrowException(v8::Exception::TypeError(v8::String::New("You can only set a vector field to a vector object")));
if(!obj->Has(v8::String::New("x")) || !obj->Has(v8::String::New("y")) || !obj->Has(v8::String::New("z"))){
return v8::ThrowException(v8::Exception::TypeError(v8::String::New("You can only set a vector field to a vector object that has the properties x, y and z")));
}
auto tmp = getCache();
if(tmp.IsEmpty()){
SGetNetProp(ent, edict, p, offset, NULL);
tmp = getCache();
}
auto vec = tmp->ToObject();
vec->Set(v8::String::New("x"), obj->Get(v8::String::New("x")));
vec->Set(v8::String::New("y"), obj->Get(v8::String::New("y")));
vec->Set(v8::String::New("z"), obj->Get(v8::String::New("z")));
}; break;
case DPT_String: {
int maxlen = DT_MAX_STRING_BUFFERSIZE;
v8::String::AsciiValue param1(value->ToString());
strncopy((char*)addr, *param1, maxlen);
}; break;
default: THROW("Invalid Netprop Type");
}
if(edict != NULL) gamehelpers->SetEdictStateChanged(edict, offset);
return value;
}
v8::Handle<v8::Value> SMJS_Netprops::SGetNetProp(void *ent, edict_t *edict, SendProp *p, size_t offset, bool *isCacheable, const char *name){
std::string propNameStdString(name != NULL ? name : p->GetName());
int bit_count = p->m_nBits;
bool is_unsigned = ((p->GetFlags() & SPROP_UNSIGNED) == SPROP_UNSIGNED);
void *addr = (void*) ((intptr_t) ent + offset);
if(isCacheable != NULL) *isCacheable = false;
switch(p->GetType()){
case DPT_Int: {
int v;
// If it's an integer with 21 bits and starts with m_h, it MUST be an entity, if it's not...
// oh well... return an integer
if(bit_count == 21 && strlen(propNameStdString.c_str()) >= 3 && std::strncmp(propNameStdString.c_str(), "m_h", 3) == 0){
CBaseHandle &hndl = *(CBaseHandle *)addr;
CBaseEntity *pHandleEntity = gamehelpers->ReferenceToEntity(hndl.GetEntryIndex());
if (pHandleEntity && hndl == reinterpret_cast<IHandleEntity *>(pHandleEntity)->GetRefEHandle()){
SMJS_Entity *entity = GetEntityWrapper(pHandleEntity);
return entity->GetWrapper(GetPluginRunning());
}else{
return v8::Null();
}
}
if (bit_count < 1){
v = *(int32_t * )addr;
}else if(bit_count > 32){
auto arr = v8::Array::New(2);
arr->Set(0, v8::Int32::New(*(uint32_t * )((intptr_t) addr + 4)));
arr->Set(1, v8::Int32::New(*(uint32_t * )addr));
return arr;
}else if (bit_count >= 17){
v = *(int32_t *) addr;
}else if (bit_count >= 9){
if (is_unsigned){
v = *(uint16_t *) addr;
}else{
v = *(int16_t *) addr;
}
}else if (bit_count >= 2){
if (is_unsigned){
v = *(uint8_t *) addr;
}else{
v = *(int8_t *) addr;
}
}else{
return v8::Boolean::New((*(bool *)addr));
}
return v8::Int32::New(v);
}; break;
case DPT_Float: return v8::Number::New(*(float*)addr);
case DPT_Vector: {
auto obj = v8::Object::New();
auto data = new SMJS_Netprops_CachedValueData();
data->ent = ent;
data->addr = addr;
data->actual_offset = offset;
data->edict = edict;
data->prop = p;
auto ext = v8::External::New(data);
obj->SetHiddenValue(v8::String::New("SMJS::dataPtr"), ext);
obj->SetAccessor(v8::String::New("x"), VectorGetter, VectorSetter, ext);
obj->SetAccessor(v8::String::New("y"), VectorGetter, VectorSetter, ext);
obj->SetAccessor(v8::String::New("z"), VectorGetter, VectorSetter, ext);
obj->Set(v8::String::New("toString"), v8::FunctionTemplate::New(VectorToString)->GetFunction());
if(isCacheable != NULL) *isCacheable = true;
return obj;
}; break;
case DPT_String: return v8::String::New((char *)addr);
case DPT_DataTable: {
SendTable *pTable = p->GetDataTable();
if (!pTable){
THROW_VERB("Error looking up DataTable for prop %s", name != NULL ? name : p->GetName());
}
SMJS_DataTable *table = new SMJS_DataTable(GetPluginRunning(), ent, edict, pTable, offset);
if(isCacheable != NULL) *isCacheable = true;
return table->GetWrapper();
}break;
default: THROW("Invalid Netprop Type");
}
return v8::Undefined();
}
SIMPLE_WRAPPED_CLS_CPP(SMJS_DataTable, SMJS_SimpleWrapped);
v8::Handle<v8::Value> SMJS_DataTable::DTGetter(uint32_t index, const AccessorInfo& info){
SMJS_DataTable *self = (SMJS_DataTable*) v8::Handle<v8::External>::Cast(info.This()->GetInternalField(0))->Value();
// Index can't be < 0, don't even test for it
if(index >= (uint32_t) self->pTable->GetNumProps()) THROW_VERB("Index %d out of bounds", index);
auto it = self->cachedValues.find(index);
if(it != self->cachedValues.end()){
return it->second;
}
auto pProp = self->pTable->GetProp(index);
bool isCacheable;
auto res = SMJS_Netprops::SGetNetProp(self->ent, self->edict, pProp, self->offset + pProp->GetOffset(), &isCacheable, self->pTable->GetName());
if(isCacheable){
self->cachedValues.insert(std::make_pair(index, res));
}
return res;
}
v8::Handle<v8::Value> SMJS_DataTable::DTSetter(uint32_t index, Local<Value> value, const AccessorInfo& info){
SMJS_DataTable *self = (SMJS_DataTable*) v8::Handle<v8::External>::Cast(info.This()->GetInternalField(0))->Value();
if(index >= (uint32_t) self->pTable->GetNumProps()) THROW_VERB("Index %d out of bounds", index);
auto pProp = self->pTable->GetProp(index);
boost::function<v8::Persistent<v8::Value> ()> f(boost::bind(&SMJS_DataTable::GenerateThenFindCachedValue, self, index, pProp, self->offset + pProp->GetOffset()));
return SMJS_Netprops::SSetNetProp(self->ent, self->edict, pProp, self->offset + pProp->GetOffset(), value, f, self->pTable->GetName());
}