forked from noah-/d2bs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
JSScript.cpp
228 lines (192 loc) · 7.22 KB
/
JSScript.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
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
#include "JSScript.h"
#include "Script.h"
#include "ScriptEngine.h"
#include "D2BS.h"
#include "Helpers.h"
EMPTY_CTOR(script)
struct FindHelper {
DWORD tid;
wchar_t* name;
Script* script;
};
bool __fastcall FindScriptByTid(Script* script, void* argv, uint argc);
bool __fastcall FindScriptByName(Script* script, void* argv, uint argc);
JSAPI_PROP(script_getProperty) {
Script* script = (Script*)JS_GetInstancePrivate(cx, obj, &script_class, NULL);
// TODO: make this check stronger
if (!script)
return JS_TRUE;
jsval ID;
JS_IdToValue(cx, id, &ID);
char* nShortFilename = NULL;
switch (JSVAL_TO_INT(ID)) {
case SCRIPT_FILENAME:
vp.setString(JS_InternUCString(cx, script->GetShortFilename()));
break;
case SCRIPT_GAMETYPE:
vp.setBoolean(script->GetState() == InGame ? false : true);
break;
case SCRIPT_RUNNING:
vp.setBoolean(script->IsRunning());
break;
case SCRIPT_THREADID:
vp.setInt32(script->GetThreadId());
break;
case SCRIPT_MEMORY:
vp.setInt32(JS_GetGCParameter(script->GetRuntime(), JSGC_BYTES));
break;
default:
break;
}
return JS_TRUE;
}
JSAPI_FUNC(script_getNext) {
Script* iterp = (Script*)JS_GetInstancePrivate(cx, JS_THIS_OBJECT(cx, vp), &script_class, NULL);
ScriptEngine::LockScriptList("scrip.getNext");
// EnterCriticalSection(&ScriptEngine::lock);
for (ScriptMap::iterator it = ScriptEngine::scripts.begin(); it != ScriptEngine::scripts.end(); it++) {
if (it->second == iterp) {
it++;
if (it == ScriptEngine::scripts.end())
break;
iterp = it->second;
JS_SetPrivate(cx, JS_THIS_OBJECT(cx, vp), iterp);
JS_SET_RVAL(cx, vp, JSVAL_TRUE);
ScriptEngine::UnLockScriptList("scrip.getNext");
// LeaveCriticalSection(&ScriptEngine::lock);
return JS_TRUE;
}
}
// LeaveCriticalSection(&ScriptEngine::lock);
ScriptEngine::UnLockScriptList("scrip.getNext");
JS_SET_RVAL(cx, vp, JSVAL_VOID);
return JS_TRUE;
}
JSAPI_FUNC(script_stop) {
JS_SET_RVAL(cx, vp, JSVAL_NULL);
Script* script = (Script*)JS_GetInstancePrivate(cx, JS_THIS_OBJECT(cx, vp), &script_class, NULL);
if (script->IsRunning())
script->Stop();
return JS_TRUE;
}
JSAPI_FUNC(script_pause) {
JS_SET_RVAL(cx, vp, JSVAL_NULL);
Script* script = (Script*)JS_GetInstancePrivate(cx, JS_THIS_OBJECT(cx, vp), &script_class, NULL);
if (script->IsRunning())
script->Pause();
return JS_TRUE;
}
JSAPI_FUNC(script_resume) {
JS_SET_RVAL(cx, vp, JSVAL_NULL);
Script* script = (Script*)JS_GetInstancePrivate(cx, JS_THIS_OBJECT(cx, vp), &script_class, NULL);
if (script->IsPaused())
script->Resume();
return JS_TRUE;
}
JSAPI_FUNC(script_send) {
JS_SET_RVAL(cx, vp, JSVAL_NULL);
Script* script = (Script*)JS_GetInstancePrivate(cx, JS_THIS_OBJECT(cx, vp), &script_class, NULL);
Event* evt = new Event;
if (!script || !script->IsRunning())
return JS_TRUE;
ScriptEngine::LockScriptList("script.send");
evt->owner = script;
evt->argc = argc;
evt->name = _strdup("scriptmsg");
evt->arg1 = new DWORD(argc);
evt->argv = new JSAutoStructuredCloneBuffer*[argc];
for (uint i = 0; i < argc; i++) {
evt->argv[i] = new JSAutoStructuredCloneBuffer;
evt->argv[i]->write(cx, JS_ARGV(cx, vp)[i]);
}
EnterCriticalSection(&Vars.cEventSection);
evt->owner->EventList.push_front(evt);
LeaveCriticalSection(&Vars.cEventSection);
evt->owner->TriggerOperationCallback();
ScriptEngine::UnLockScriptList("script.send");
return JS_TRUE;
}
JSAPI_FUNC(script_join) {
JS_SET_RVAL(cx, vp, JSVAL_NULL);
Script* script = (Script*)JS_GetInstancePrivate(cx, JS_THIS_OBJECT(cx, vp), &script_class, NULL);
script->Join();
return JS_TRUE;
}
JSAPI_FUNC(my_getScript) {
JS_SET_RVAL(cx, vp, JSVAL_NULL);
Script* iterp = NULL;
if (argc == 1 && JSVAL_IS_BOOLEAN(JS_ARGV(cx, vp)[0]) && JSVAL_TO_BOOLEAN(JS_ARGV(cx, vp)[0]) == JS_TRUE)
iterp = (Script*)JS_GetContextPrivate(cx);
else if (argc == 1 && JSVAL_IS_INT(JS_ARGV(cx, vp)[0])) {
// loop over the Scripts in ScriptEngine and find the one with the right threadid
DWORD tid = (DWORD)JSVAL_TO_INT(JS_ARGV(cx, vp)[0]);
FindHelper args = {tid, NULL, NULL};
ScriptEngine::ForEachScript(FindScriptByTid, &args, 1);
if (args.script != NULL)
iterp = args.script;
else
return JS_TRUE;
} else if (argc == 1 && JSVAL_IS_STRING(JS_ARGV(cx, vp)[0])) {
wchar_t* name = _wcsdup(JS_GetStringCharsZ(cx, JSVAL_TO_STRING(JS_ARGV(cx, vp)[0])));
if (name)
StringReplace(name, L'/', L'\\', wcslen(name));
FindHelper args = {0, name, NULL};
ScriptEngine::ForEachScript(FindScriptByName, &args, 1);
free(name);
if (args.script != NULL)
iterp = args.script;
else
return JS_TRUE;
} else {
if (ScriptEngine::scripts.size() > 0) {
// EnterCriticalSection(&ScriptEngine::lock);
ScriptEngine::LockScriptList("getScript");
iterp = ScriptEngine::scripts.begin()->second;
ScriptEngine::UnLockScriptList("getScript");
// LeaveCriticalSection(&ScriptEngine::lock);
}
if (iterp == NULL)
return JS_TRUE;
}
JSObject* res = BuildObject(cx, &script_class, script_methods, script_props, iterp);
if (!res)
THROW_ERROR(cx, "Failed to build the script object");
JS_SET_RVAL(cx, vp, OBJECT_TO_JSVAL(res));
return JS_TRUE;
}
JSAPI_FUNC(my_getScripts) {
DWORD dwArrayCount = NULL;
JSObject* pReturnArray = JS_NewArrayObject(cx, 0, NULL);
JS_BeginRequest(cx);
JS_AddRoot(cx, &pReturnArray);
ScriptEngine::LockScriptList("getScripts");
for (ScriptMap::iterator it = ScriptEngine::scripts.begin(); it != ScriptEngine::scripts.end(); it++) {
JSObject* res = BuildObject(cx, &script_class, script_methods, script_props, it->second);
jsval a = OBJECT_TO_JSVAL(res);
JS_SetElement(cx, pReturnArray, dwArrayCount, &a);
dwArrayCount++;
}
ScriptEngine::UnLockScriptList("getScripts");
JS_SET_RVAL(cx, vp, OBJECT_TO_JSVAL(pReturnArray));
JS_RemoveRoot(cx, &pReturnArray);
JS_EndRequest(cx);
return JS_TRUE;
}
bool __fastcall FindScriptByName(Script* script, void* argv, uint argc) {
FindHelper* helper = (FindHelper*)argv;
// static uint pathlen = wcslen(Vars.szScriptPath) + 1;
const wchar_t* fname = script->GetShortFilename();
if (_wcsicmp(fname, helper->name) == 0) {
helper->script = script;
return false;
}
return true;
}
bool __fastcall FindScriptByTid(Script* script, void* argv, uint argc) {
FindHelper* helper = (FindHelper*)argv;
if (script->GetThreadId() == helper->tid) {
helper->script = script;
return false;
}
return true;
}