-
Notifications
You must be signed in to change notification settings - Fork 7
/
SMJS_DataMaps.cpp
72 lines (52 loc) · 2.3 KB
/
SMJS_DataMaps.cpp
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
#include "SMJS_DataMaps.h"
#include "SMJS_Plugin.h"
#include "SMJS_Entity.h"
WRAPPED_CLS_CPP(SMJS_DataMaps, SMJS_BaseWrapped);
v8::Handle<v8::Value> SMJS_DataMaps::SGetDataMap(v8::Local<v8::String> prop, const v8::AccessorInfo &info){
Local<Value> _intfld = info.This()->GetInternalField(0);
SMJS_DataMaps *self = dynamic_cast<SMJS_DataMaps*>((SMJS_BaseWrapped*)Handle<External>::Cast(_intfld)->Value());
auto ent = self->entWrapper->ent;
datamap_t *datamap = gamehelpers->GetDataMap(ent);
if(datamap == NULL) THROW("Entity does not have a datamap");
v8::String::AsciiValue str(prop);
typedescription_t *desc = gamehelpers->FindInDataMap(datamap, *str);
if(desc == NULL) return v8::Undefined();
switch(desc->fieldType){
case FIELD_INTEGER: return v8::Int32::New(*(int32_t *)((intptr_t)ent + desc->fieldOffset));
case FIELD_FLOAT: return v8::Number::New(*(float *)((intptr_t)ent + desc->fieldOffset));
case FIELD_STRING:
case FIELD_MODELNAME:
case FIELD_SOUNDNAME: {
string_t idx = *(string_t *)((intptr_t)ent + desc->fieldOffset);
return v8::String::New((idx == NULL_STRING) ? "" : STRING(idx));
} break;
default: THROW("Unsupported datamap type");
}
}
v8::Handle<v8::Value> SMJS_DataMaps::SSetDataMap(v8::Local<v8::String> prop, v8::Local<v8::Value> value, const v8::AccessorInfo &info){
Local<Value> _intfld = info.This()->GetInternalField(0);
SMJS_DataMaps *self = dynamic_cast<SMJS_DataMaps*>((SMJS_BaseWrapped*)Handle<External>::Cast(_intfld)->Value());
auto ent = self->entWrapper->ent;
datamap_t *datamap = gamehelpers->GetDataMap(ent);
if(datamap == NULL) THROW("Entity does not have a datamap");
v8::String::AsciiValue str(prop);
typedescription_t *desc = gamehelpers->FindInDataMap(datamap, *str);
if(desc == NULL) return v8::Undefined();
switch(desc->fieldType){
case FIELD_INTEGER:
*(int32_t *)((intptr_t) ent + desc->fieldOffset) = value->ToInt32()->Int32Value();
break;
case FIELD_FLOAT:
*(float *)((intptr_t) ent + desc->fieldOffset) = (float) value->ToNumber()->NumberValue();
break;
case FIELD_STRING:
case FIELD_MODELNAME:
case FIELD_SOUNDNAME:
{
v8::String::Utf8Value str(value);
strncpy((char *)((intptr_t)ent + desc->fieldOffset), *str, desc->fieldSize);
} break;
default: THROW("Unsupported datamap type");
}
return value;
}