-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathScriptEngine.cpp
More file actions
609 lines (507 loc) · 15 KB
/
ScriptEngine.cpp
File metadata and controls
609 lines (507 loc) · 15 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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
#include <vector>
#include <assert.h>
#include "ScriptEngine.h"
#include "D2Boot.h"
#include "Helpers.h"
#include "Events.h"
#include "ScreenHook.h"
Script *ScriptEngine::console = nullptr;
EngineState ScriptEngine::state = Stopped;
// CRITICAL_SECTION ScriptEngine::lock = {0};
CRITICAL_SECTION ScriptEngine::scriptListLock = {0};
ScriptMap ScriptEngine::scripts = ScriptMap();
std::atomic<int> ScriptEngine::actives = {0};
HANDLE ScriptEngine::execEventSignal = nullptr;
wchar_t *ScriptEngine::consoleName = (wchar_t *)L"console";
// bool __fastcall DisposeScript(Script *script, void *, unsigned int);
bool __fastcall StopScript(Script *script, void *argv, unsigned int argc);
ScriptEngine::~ScriptEngine()
{
}
void ScriptEngine::LockScriptList(const char *loc)
{
EnterCriticalSection(&scriptListLock);
}
void ScriptEngine::UnLockScriptList(const char *loc)
{
LeaveCriticalSection(&scriptListLock);
}
Script *ScriptEngine::CompileFile(const wchar_t *file, ScriptState state, unsigned int argc, void **argv, bool recompile)
{
if (GetState() != Running)
return nullptr;
wchar_t *fileName = _wcsdup(file);
// format filename first!
_wcslwr_s(fileName, wcslen(fileName) + 1);
// StringReplaceChar(fileName, L'/', L'\\', wcslen(fileName));
// DEBUG_LOG("--CCC--CompileFile key:[%ls]---state:[%d]-actives:[%d]--count[%d]", fileName, state, actives.load(), scripts.count(fileName));
try
{
if (scripts.count(fileName))
scripts[fileName]->Stop();
Script *script = new Script(fileName, state, argc, argv);
scripts[fileName] = script;
free(fileName);
return script;
}
catch (std::exception &ex)
{
wchar_t *wzWhat = AnsiToUnicode(ex.what());
Print(L"[\u00FFc1D2Boot :: exception\u00FFc0] compile file error: %ls", wzWhat);
free(wzWhat);
free(fileName);
return nullptr;
}
}
bool ScriptEngine::EvalScript(Script *script)
{
if (script && script->BeginThread(ScriptThread))
{
if (script->scriptState != Command)
actives++;
// DEBUG_LOG("--EEE--CompileFile:[%ls]---state:[%d]-actives:[%d]", script->GetShortFilename(), state, actives.load());
return true;
}
Print(L"[\u00FFc1D2Boot :: error\u00FFc0] eval script error: script is null or thread fail.");
return false;
}
void ScriptEngine::RunCommand(const wchar_t *command)
{
if (GetState() != Running)
return;
try
{
// EnterCriticalSection(&lock);
console->RunCommand(command);
// LeaveCriticalSection(&lock);
}
catch (std::exception &e)
{
// LeaveCriticalSection(&lock);
wchar_t *what = AnsiToUnicode(e.what());
Print(what);
free(what);
}
}
unsigned int ScriptEngine::GetCount(bool active, bool unexecuted)
{
if (GetState() != Running)
return 0;
LockScriptList("getCount");
// EnterCriticalSection(&lock);
int count = scripts.size();
for (ScriptMap::iterator it = scripts.begin(); it != scripts.end(); it++)
{
if (!active && it->second->IsRunning() && !it->second->IsAborted())
count--;
if (!unexecuted && it->second->GetExecutionCount() == 0 && !it->second->IsRunning())
count--;
}
assert(count >= 0);
UnLockScriptList("getCount");
return count;
}
unsigned int ScriptEngine::GetExecCount()
{
if (GetState() != Running)
return 0;
LockScriptList("getExecCount");
int count = 0;
for (ScriptMap::iterator it = scripts.begin(); it != scripts.end(); it++)
{
if (it->second->execState == ExecuteState::executing)
count++;
}
UnLockScriptList("getExecCount");
return count;
}
bool ScriptEngine::WaitForDisposed(const bool isShutdown)
{
int tries = 20;
while ((actives > 0 || (isShutdown && scripts.count(consoleName) > 0)) && tries > 0)
{
WaitForSingleObject(execEventSignal, 50);
// DWORD dwCode = WaitForSingleObject(execEventSignal, 50);
// DEBUG_LOG("--WaitForDisposed----try:%d---actives:%d--console:[%d]--WaitFor:-%ld\n", tries, actives.load(), scripts.count(L"console"), dwCode);
tries--;
}
if (actives == 0)
return true;
Log(L"waiting for dispose error, actives:\"%d\".", actives.load());
return false;
}
bool ScriptEngine::Startup(void)
{
bool result = false;
if (GetState() == Stopped)
{
state = Starting;
InitializeCriticalSection(&scriptListLock);
execEventSignal = CreateEvent(nullptr, FALSE, FALSE, L"ExecEventSignal");
// InitializeCriticalSection(&lock);
// EnterCriticalSection(&lock);
LockScriptList("startup - enter");
if (wcslen(Vars.szConsole) > 0)
{
wchar_t file[_MAX_FNAME + _MAX_PATH];
swprintf_s(file, _countof(file), L"%ls/%ls", Vars.szScriptPath, Vars.szConsole);
console = new Script(file, Command);
}
else
{
console = new Script(L"", Command);
}
if (console)
{
scripts[consoleName] = console;
result = EvalScript(console);
state = Running;
}
// LeaveCriticalSection(&lock);
UnLockScriptList("startup - leave");
}
return result;
}
void ScriptEngine::Shutdown(void)
{
if (GetState() == Running)
{
// bring the engine down properly
// EnterCriticalSection(&lock);
LockScriptList("ShutdownStop");
// set engine state to stopping
state = Stopping;
StopAll(true);
console->Stop(true, true);
UnLockScriptList("ShutdownStop");
WaitForDisposed(true);
LockScriptList("Shutdown");
// clear all scripts now that they're stopped
if (!scripts.empty())
scripts.clear();
// clean all hook, the hook should deleted when disposed
Genhook::Clean();
UnLockScriptList("Shutdown");
CloseHandle(execEventSignal);
// LeaveCriticalSection(&lock);
// DeleteCriticalSection(&lock);
state = Stopped;
Vars.bShutdown = TRUE;
}
}
// void ScriptEngine::FlushCache(void)
// {
// if (GetState() != Running)
// return;
// static bool isFlushing = false;
// if (isFlushing || Vars.bDisableCache)
// return;
// // EnterCriticalSection(&lock);
// // TODO: examine if this lock is necessary any more
// EnterCriticalSection(&Vars.cFlushCacheSection);
// isFlushing = true;
// DisposeScriptAll();
// isFlushing = false;
// LeaveCriticalSection(&Vars.cFlushCacheSection);
// // LeaveCriticalSection(&lock);
// }
bool ScriptEngine::ForEachScript(ScriptCallback callback, EachHelper *helper, unsigned int argc, bool extra)
{
if (callback == nullptr || scripts.size() < 1)
return false;
bool block = false;
LockScriptList("forEachScript");
for (const auto &[name, script] : scripts)
{
// only callback the script that owner this event
if (helper == nullptr ||
(helper->meta && (helper->meta->every || script->IsListenerRegistered(helper->meta->name, extra))))
{
// DEBUG_LOG("--III--ForEachScript--callback--script name:[%ls]--script shortname:[%ls]--event:[%s]", name.c_str(), script->GetShortFilename(), helper ? helper->meta->name : "NULLname");
if (callback(script, helper, argc))
block = true;
}
}
UnLockScriptList("forEachScript");
return block;
}
void ScriptEngine::StopAll(const bool forceStop)
{
if (GetState() != Running && !forceStop)
return;
// the StopScript no FireEvent, the stack variable is safe.
const EventMeta meta = {(unsigned int)-1, "stop", false, true};
StopEventHelper helper = {&meta, forceStop};
ForEachScript(StopScript, &helper, 1);
}
bool ScriptEngine::ToggleScript()
{
bool resumed = false;
LockScriptList("ResumeScript");
for (const auto &[name, script] : scripts)
{
if (!script->isPausable)
continue;
if (script->IsPaused())
{
script->Resume();
resumed = true;
}
else
script->Pause();
}
UnLockScriptList("ResumeScript");
return resumed;
}
void ScriptEngine::UpdateConsole()
{
console->UpdatePlayerGid();
}
void ScriptEngine::DisposeScriptAll()
{
LockScriptList("DisposeScriptAll");
for (ScriptMap::iterator it = scripts.begin(); it != scripts.end();) // NOTE: cant it++ in here! erase will broken it
{
if (DisposeScript(it->second, false))
scripts.erase(it++);
}
UnLockScriptList("DisposeScriptAll");
}
bool ScriptEngine::DisposeScript(Script *script, bool erased)
{
bool bConsole = script->scriptState == Command;
wchar_t *fileName;
bool res = false;
if (bConsole)
fileName = _wcsdup(consoleName);
else
fileName = _wcsdup(script->GetFilename());
// clean all event
script->hasActiveCX = false;
while (script->eventList.size() > 0)
{
EnterCriticalSection(&Vars.cEventSection);
Event *evt = script->eventList.back();
script->eventList.pop_back();
LeaveCriticalSection(&Vars.cEventSection);
ExecScriptEvent(evt, true); // clean list and pop events
}
// clear all
script->ClearAllEvents();
if (erased)
{
LockScriptList("DisposeScript");
if (scripts.count(fileName))
scripts.erase(fileName);
UnLockScriptList("DisposeScript");
}
if (GetCurrentThreadId() == script->threadId)
{
delete script;
script = nullptr;
if (!bConsole)
{
actives--;
SetEvent(execEventSignal);
res = true;
}
}
else
{
Log(L"WARNING dispose script:\"%ls\" error.", fileName);
// bad things happen if we delete from another thread
// must a heap variable!
const EventMeta *meta = new EventMeta{(unsigned int)-1, "DisposeMe", false, false};
Event *evt = new Event;
evt->owner = script;
evt->meta = meta;
script->FireEvent(evt);
}
free(fileName);
return res;
}
int InterruptHandler(JSRuntime *rt, void *opaque)
{
Script *script = (Script *)opaque;
while (script->IsPaused())
{
Sleep(200);
JS_RunGC(rt);
}
// InterruptHandler will be called by TriggerInterruptHandler, or by QuickJS itself
if (!!!(script->IsAborted() || (script->GetState() == InGame && ClientState() == ClientStateMenu)))
{
script->ExecEvent();
// safe return, should be FALSE, continue execution function in Quickjs
return (script->IsAborted() || ((script->GetState() == InGame) && ClientState() == ClientStateMenu));
}
else
{
// if aborted or exiting the game status, it must return TRUE!, QuickJS exception and interrupted the script.
return true;
}
}
static bool EventCallInner(JSContext *ctx, Script *script, const char *eventName, const int argc, JSValue *argv)
{
JSValue ret = JS_UNDEFINED;
bool block = false;
for (const Caller &ec : script->functions[eventName])
{
// DEBUG_LOG("--ECI-->>>--caller:[%lld]--function name:[%s]--this:[%s]", ec.action, JS_ToCString(ctx, JS_GetPropertyStr(ctx, ec.action, "name")), JS_ToCString(ctx, JS_JSONStringify(ctx, ec.owner, JS_UNDEFINED, JS_UNDEFINED)));
ret = JS_Call(ctx, ec.action, ec.owner, argc, argv);
if (JS_IsException(ret))
{
JS_ReportError(ctx);
JS_FreeValue(ctx, ret);
return false;
}
block |= (JS_IsBool(ret) && JS_ToBool(ctx, ret));
JS_FreeValue(ctx, ret);
}
return block;
}
bool ExecScriptEvent(Event *evt, bool clearList)
{
// DEBUG_LOG("--EES-->>>--Event id:[%d] name:[%s] blocked:[%d] argv[%s]--GetShortFilename:%ls",
// evt->meta->id, evt->meta->name, evt->meta->blocked, (char *)evt->argv, evt->owner->GetShortFilename());
// only free event, included blocked
if (clearList)
{
evt->owner = nullptr;
evt->meta = nullptr;
free(evt->argv);
evt->argv = nullptr;
delete (DWORD *)evt->result;
evt->result = nullptr;
delete evt;
evt = nullptr;
// return now!
return true;
}
JSContext *ctx = nullptr;
JSValue jsArgv = JS_UNDEFINED;
int argc = 0;
ctx = evt->owner->GetContext();
if (!evt->meta || !evt->meta->name)
return false;
if (evt->argv)
{
argc = 1;
switch (evt->meta->mode)
{
case EAM_BYTE:
// return a ArrayBuffer, let package=new Uint8Array(bytes); package[0] in js
// jsArgv = JS_NewArrayBuffer(ctx, (uint8_t *)evt->argv, evt->size, NULL, NULL, FALSE);
// return a array
jsArgv = JS_NewArray(ctx);
for (size_t i = 0; i < evt->size; i++)
{
JS_DefinePropertyValueUint32(ctx, jsArgv, i, JS_NewUint32(ctx, ((BYTE *)evt->argv)[i]), JS_PROP_C_W_E);
}
break;
default:
jsArgv = JS_ParseJSON(ctx, (char *)evt->argv, strlen((char *)evt->argv), "EventArgv");
break;
}
if (JS_IsException(jsArgv))
{
JS_ReportError(ctx, "event name:[%s] the argv string:[%s]", evt->meta->name, (char *)evt->argv);
JS_FreeValue(ctx, jsArgv);
// FREE evt's argv NOW, even if it's blocked!
free(evt->argv);
evt->argv = nullptr;
if (!evt->meta->blocked)
{
delete evt;
evt = nullptr;
}
return false;
}
// FREE evt's argv NOW, even if it's blocked!
free(evt->argv);
evt->argv = nullptr;
}
// Exec script's event
if (evt->meta->id > EM_FIRST && evt->meta->id < EM_LAST)
{
bool succeed = false;
succeed = EventCallInner(ctx, evt->owner, evt->meta->name, argc, &jsArgv);
JS_FreeValue(ctx, jsArgv);
// set return value
if (evt->meta->blocked)
{
// DEBUG_LOG("--EES--ExecScriptEvent---blocker event, setEvent, name:[%s] blocked:[%d] argv:[%s]", evt->meta->name, evt->meta->blocked, (char *)evt->argv);
// *(DWORD *)evt->result = succeed;
evt->result = new DWORD(succeed);
SetEvent(Vars.eventSignal);
}
else
{
// only free normal event (NOT blocker)!
delete evt;
evt = nullptr;
}
return true;
}
if (strcmp(evt->meta->name, "DisposeMe") == 0)
{
ScriptEngine::DisposeScript(evt->owner);
JS_FreeValue(ctx, jsArgv);
// must free by self
delete evt->meta;
evt->meta = nullptr;
delete evt;
evt = nullptr;
return true;
}
// safe free
JS_FreeValue(ctx, jsArgv);
return false;
}
bool __fastcall StopScript(Script *script, void *helper, unsigned argc)
{
script->TriggerInterruptHandler();
if (script->GetState() != Command)
script->Stop(((StopEventHelper *)helper)->force, ScriptEngine::GetState() == Stopping);
return true;
}
bool __fastcall StopIngameScript(Script *script, void *, unsigned int)
{
if (script->GetState() == InGame)
script->Stop(true);
return true;
}
// bool SetSkill(JSContext *ctx, uint16_t wSkillId, bool bLeft, uint32_t dwItemId = 0xFFFFFFFF);
bool SetSkill(JSContext *ctx, WORD wSkillId, bool bLeft, DWORD dwItemId)
{
if (ClientState() != ClientStateInGame)
return false;
if (!GetSkill(wSkillId))
return false;
BYTE aPacket[9];
aPacket[0] = 0x3C;
*(WORD *)&aPacket[1] = wSkillId;
aPacket[3] = 0;
aPacket[4] = (bLeft) ? 0x80 : 0;
*(DWORD *)&aPacket[5] = dwItemId;
D2CLIENT_SendGamePacket(9, aPacket);
UnitAny *me = D2CLIENT_GetPlayerUnit();
int timeout = 0;
Skill *hand = nullptr;
while (ClientState() == ClientStateInGame)
{
hand = (bLeft ? me->pInfo->pLeftSkill : me->pInfo->pRightSkill);
if (hand->pSkillInfo->wSkillId != wSkillId)
{
if (timeout > 10)
return false;
timeout++;
}
else
return true;
// run events to avoid packet block deadlock
Script *script = (Script *)JS_GetContextOpaque(ctx);
script->WaitForExecEvent(100);
}
return false;
}