-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathJSScript.cpp
More file actions
362 lines (295 loc) · 8.87 KB
/
JSScript.cpp
File metadata and controls
362 lines (295 loc) · 8.87 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
#include "JSScript.h"
#include "ScriptEngine.h"
#include "Script.h"
#include "Events.h"
#include "Helpers.h"
#include "D2Boot.h"
enum script_id
{
SCRIPT_FILENAME,
SCRIPT_GAMETYPE,
SCRIPT_RUNNING,
SCRIPT_PAUSABLE,
SCRIPT_THREADID,
SCRIPT_MEMORY,
};
struct FindHelper : EachHelper
{
const DWORD tid;
const wchar_t *sname;
Script *script;
};
static bool __fastcall FindScriptByName(Script *script, void *helper, unsigned int argc)
{
FindHelper *findHelper = (FindHelper *)helper;
// static uint pathlen = wcslen(Vars.szScriptPath) + 1;
const wchar_t *fname = script->GetShortFilename();
if (_wcsicmp(fname, findHelper->sname) == 0)
{
findHelper->script = script;
return false;
}
return true;
}
static bool __fastcall FindScriptByTid(Script *script, void *helper, unsigned int argc)
{
FindHelper *findHelper = (FindHelper *)helper;
if (script->GetThreadId() == findHelper->tid)
{
findHelper->script = script;
return false;
}
return true;
}
static JSClassID script_class_id;
static void js_script_finalizer(JSRuntime *rt, JSValue val)
{
JS_SetOpaque(val, nullptr);
}
static JSClassDef js_script_class = {
"Script",
.finalizer = js_script_finalizer,
};
static JSValue js_script_ctor(JSContext *ctx, JSValueConst new_target, int argc, JSValueConst *argv)
{
return JS_EXCEPTION;
}
JSAPI_FUNC(my_getScript)
{
Script *script = nullptr;
if (argc == 1 && JS_IsBool(argv[0]) && JS_ToBool(ctx, argv[0]))
{
// script = (Script *)JS_GetOpaque(this_val, script_class_id);
script = (Script *)JS_GetContextOpaque(ctx);
}
else if (argc == 1 && JS_IsNumber(argv[0]))
{
// loop over the Scripts in ScriptEngine and find the one with the right threadid
uint32_t tid;
JS_ToUint32(ctx, &tid, argv[0]);
// the FindScriptByTid no FireEvent, the stack variable is safe.
const EventMeta meta = {(unsigned int)-1, "find", false, true};
FindHelper helper = {&meta, tid, nullptr, nullptr};
ScriptEngine::ForEachScript(FindScriptByTid, &helper, 1);
if (helper.script)
script = helper.script;
else
return JS_FALSE;
}
else if (argc == 1 && JS_IsString(argv[0]))
{
wchar_t *sname = nullptr;
JS_ToUnicodeString(ctx, &sname, argv[0]);
// if (sname)
// StringReplaceChar(sname, '/', '\\', wcslen(sname));
// the FindScriptByName no FireEvent, the stack variable is safe.
const EventMeta meta = {(unsigned int)-1, "find", false, true};
FindHelper helper = {&meta, 0, sname, nullptr};
ScriptEngine::ForEachScript(FindScriptByName, &helper, 1);
free(sname);
sname = nullptr;
if (helper.script)
script = helper.script;
else
return JS_FALSE;
}
else
{
if (ScriptEngine::scripts.size() > 0)
{
// EnterCriticalSection(&ScriptEngine::lock);
ScriptEngine::LockScriptList("getScript");
script = ScriptEngine::scripts.begin()->second;
ScriptEngine::UnLockScriptList("getScript");
// LeaveCriticalSection(&ScriptEngine::lock);
}
if (!script)
return JS_FALSE;
}
JSValue res = BuildObject(ctx, script_class_id, script);
if (JS_IsException(res))
JS_THROW_SINGLE_LINE(ctx, "Failed to build the script object");
return res;
}
JSAPI_FUNC(my_getScripts)
{
JSValue returnArray = JS_NewArray(ctx);
if (JS_IsException(returnArray))
return JS_FALSE;
DWORD dwArrayCount = 0;
ScriptEngine::LockScriptList("getScripts");
for (const auto &pair : ScriptEngine::scripts)
{
JSValue res = BuildObject(ctx, script_class_id, pair.second);
if (JS_IsException(res))
JS_THROW_SINGLE_LINE(ctx, "Failed to build the script object");
JS_DefinePropertyValueUint32(ctx, returnArray, dwArrayCount, res, JS_PROP_C_W_E);
dwArrayCount++;
}
ScriptEngine::UnLockScriptList("getScripts");
return returnArray;
}
JSAPI_PGM(get_script_property)
{
Script *pScript = (Script *)JS_GetOpaque(this_val, script_class_id);
if (!pScript)
return JS_FALSE;
switch (magic)
{
case SCRIPT_FILENAME:
{
JSValue res = JS_NewUTF8String(ctx, pScript->GetShortFilename());
return res;
}
case SCRIPT_GAMETYPE:
return JS_NewBool(ctx, pScript->GetState() == InGame ? false : true);
case SCRIPT_RUNNING:
return JS_NewBool(ctx, pScript->IsRunning());
case SCRIPT_PAUSABLE:
return JS_NewBool(ctx, pScript->IsPausable());
case SCRIPT_THREADID:
return JS_NewUint32(ctx, pScript->GetThreadId());
case SCRIPT_MEMORY:
{
JSMemoryUsage stats;
JSRuntime *rt = JS_GetRuntime(ctx);
JS_ComputeMemoryUsage(rt, &stats);
return JS_NewBigInt64(ctx, stats.memory_used_size);
}
default:
break;
}
return JS_FALSE;
}
JSAPI_PSM(set_script_property)
{
Script *pScript = (Script *)JS_GetOpaque(this_val, script_class_id);
if (!pScript)
return JS_FALSE;
switch (magic)
{
case SCRIPT_PAUSABLE:
pScript->SetPausable(!!JS_ToBool(ctx, val));
break;
default:
break;
}
return JS_TRUE;
}
JSAPI_FUNC(script_getNext)
{
Script *pScript = (Script *)JS_GetOpaque(this_val, script_class_id);
ScriptEngine::LockScriptList("scrip.getNext");
for (ScriptMap::iterator it = ScriptEngine::scripts.begin(); it != ScriptEngine::scripts.end(); it++)
{
if (it->second == pScript)
{
it++;
if (it == ScriptEngine::scripts.end())
break;
JS_SetOpaque(this_val, it->second);
ScriptEngine::UnLockScriptList("scrip.getNext");
return JS_TRUE;
}
}
ScriptEngine::UnLockScriptList("scrip.getNext");
return JS_FALSE;
}
JSAPI_FUNC(script_stop)
{
Script *pScript = (Script *)JS_GetOpaque(this_val, script_class_id);
if (pScript->IsRunning())
pScript->Stop();
return JS_TRUE;
}
JSAPI_FUNC(script_pause)
{
Script *pScript = (Script *)JS_GetOpaque(this_val, script_class_id);
if (pScript->IsRunning())
pScript->Pause();
return JS_TRUE;
}
JSAPI_FUNC(script_resume)
{
Script *pScript = (Script *)JS_GetOpaque(this_val, script_class_id);
if (pScript->IsPaused())
pScript->Resume();
return JS_TRUE;
}
JSAPI_FUNC(script_join)
{
Script *pScript = (Script *)JS_GetOpaque(this_val, script_class_id);
pScript->Join();
return JS_TRUE;
}
JSAPI_FUNC(script_send)
{
Script *pScript = (Script *)JS_GetOpaque(this_val, script_class_id);
if (!pScript || !pScript->IsRunning())
return JS_FALSE;
// only support one parameter, string or json string
if (argc > 1)
return JS_FALSE;
ScriptEngine::LockScriptList("script.send");
Event *evt = new Event;
evt->owner = pScript;
evt->meta = GetEventMeta("scriptmsg");
evt->argc = 1;
const char *msg = JS_ToCString(ctx, argv[0]);
evt->size = strlen(msg);
// the argv will free at ExecScriptEvent
// a json string
if (msg[0] == '{' || msg[0] == '"')
{
evt->argv = strdup(msg);
}
else
{
evt->size = evt->size + 2 + 1;
evt->argv = malloc(evt->size * sizeof(char));
sprintf((char *)evt->argv, "\"%s\"", msg);
}
JS_FreeCString(ctx, msg);
pScript->FireEvent(evt);
ScriptEngine::UnLockScriptList("script.send");
return JS_TRUE;
}
static const JSCFunctionListEntry js_script_module_funcs[] = {
JS_CFUNC_DEF("getScript", 1, my_getScript),
JS_CFUNC_DEF("getScripts", 0, my_getScripts),
};
static const JSCFunctionListEntry js_script_proto_funcs[] = {
JS_CGETSET_MAGIC_DEF("name", get_script_property, NULL, SCRIPT_FILENAME),
JS_CGETSET_MAGIC_DEF("type", get_script_property, NULL, SCRIPT_GAMETYPE),
JS_CGETSET_MAGIC_DEF("running", get_script_property, NULL, SCRIPT_RUNNING),
JS_CGETSET_MAGIC_DEF("threadid", get_script_property, NULL, SCRIPT_THREADID),
JS_CGETSET_MAGIC_DEF("memory", get_script_property, NULL, SCRIPT_MEMORY),
JS_CGETSET_MAGIC_DEF("pausable", get_script_property, set_script_property, SCRIPT_PAUSABLE),
JS_CFUNC_DEF("getNext", 0, script_getNext),
JS_CFUNC_DEF("pause", 0, script_pause),
JS_CFUNC_DEF("resume", 0, script_resume),
JS_CFUNC_DEF("stop", 0, script_stop),
JS_CFUNC_DEF("join", 0, script_join),
JS_CFUNC_DEF("send", 1, script_send),
};
int js_module_script_init(JSContext *ctx, JSModuleDef *m)
{
JSValue script_proto, script_class;
/* create the script class */
JS_NewClassID(&script_class_id);
JS_NewClass(JS_GetRuntime(ctx), script_class_id, &js_script_class);
script_proto = JS_NewObject(ctx);
script_class = JS_NewCFunction2(ctx, js_script_ctor, "Script", 0, JS_CFUNC_constructor, 0);
JS_SetPropertyFunctionList(ctx, script_proto, js_script_proto_funcs, ARRAYSIZE(js_script_proto_funcs));
JS_SetConstructor(ctx, script_class, script_proto);
JS_SetClassProto(ctx, script_class_id, script_proto);
JS_SetModuleExport(ctx, m, "Script", script_class);
JS_SetModuleExportList(ctx, m, js_script_module_funcs, ARRAYSIZE(js_script_module_funcs));
return TRUE;
}
int js_module_script_export(JSContext *ctx, JSModuleDef *m)
{
JS_AddModuleExportList(ctx, m, js_script_module_funcs, ARRAYSIZE(js_script_module_funcs));
JS_AddModuleExport(ctx, m, "Script");
return TRUE;
}