-
Notifications
You must be signed in to change notification settings - Fork 7
/
SMJS_GameRulesProps.cpp
69 lines (50 loc) · 2.39 KB
/
SMJS_GameRulesProps.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
#include "SMJS_GameRulesProps.h"
#include "SMJS_Netprops.h"
#include "SMJS_Plugin.h"
WRAPPED_CLS_CPP(SMJS_GameRulesProps, SMJS_BaseWrapped);
SMJS_GameRulesProps *g_GameRulesProp;
SMJS_GameRulesProps::SMJS_GameRulesProps(){
g_GameRulesProp = this;
}
void SMJS_GameRulesProps::OnWrapperAttached(SMJS_Plugin *plugin, v8::Persistent<v8::Value> wrapper){
SMJS_BaseWrapped::OnWrapperAttached(plugin, wrapper);
InitCacheForPlugin(plugin);
}
Handle<Value> SMJS_GameRulesProps::GetRulesProp(Local<String> propName, const AccessorInfo& info){
v8::String::AsciiValue str(propName);
std::string propNameStdString(*str);
auto cachedValue = g_GameRulesProp->FindCachedValue(GetPluginRunning()->id, propNameStdString);
if(!cachedValue.IsEmpty()) return cachedValue;
sm_sendprop_info_t propInfo;
if(!SMJS_Netprops::GetEntityPropInfo(g_GameRulesProp->proxy, *str, &propInfo)){
return v8::Undefined();
}
bool isCacheable;
auto ret = SMJS_Netprops::SGetNetProp(g_GameRulesProp->gamerules, NULL, propInfo.prop, propInfo.actual_offset, &isCacheable, NULL);
if(isCacheable){
g_GameRulesProp->InsertCachedValue(GetPluginRunning()->id, propNameStdString, v8::Persistent<v8::Value>::New(ret));
}
return ret;
}
Handle<Value> SMJS_GameRulesProps::SetRulesProp(Local<String> propName, Local<Value> value, const AccessorInfo& info){
v8::String::AsciiValue str(propName);
std::string propNameStdString(*str);
sm_sendprop_info_t propInfo;
if(!SMJS_Netprops::GetEntityPropInfo(g_GameRulesProp->proxy, *str, &propInfo)){
return v8::Undefined();
}
boost::function<v8::Persistent<v8::Value> ()> f(boost::bind(&SMJS_GameRulesProps::GenerateThenFindCachedValue, g_GameRulesProp, GetPluginRunning()->id, propNameStdString, propInfo.prop, propInfo.actual_offset));
IServerUnknown *pUnk = (IServerUnknown *)g_GameRulesProp->proxy;
IServerNetworkable *pNet = pUnk->GetNetworkable();
edict_t *edict;
if(!pNet){
throw "Invalid game rules proxy";
}
edict = pNet->GetEdict();
auto res = SMJS_Netprops::SSetNetProp(g_GameRulesProp->gamerules, NULL, propInfo.prop, propInfo.actual_offset, value, f);
// FIXME: This may actually not send the changes if the prop is a vector or datatable
// because it's going to get the cached value from the real gamerules and change the gamerules
// offsets instead of the proxy
SMJS_Netprops::SSetNetProp(g_GameRulesProp->proxy, edict, propInfo.prop, propInfo.actual_offset, value, f);
return res;
}