-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathScript.h
149 lines (122 loc) · 2.9 KB
/
Script.h
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
#pragma once
#include <windows.h>
#include <string>
#include <map>
#include <list>
#include "JSHelpers.h"
#include "JSUnit.h"
#include "Events.h"
enum ScriptState
{
InGame,
OutOfGame,
Command
};
enum class ExecuteState
{
Standby,
Starting,
executing,
Stopped
};
struct Caller
{
JSValue action;
JSValue owner;
Caller &operator=(const Caller &source)
{
this->action = source.action;
this->owner = source.owner;
return *this;
}
bool operator==(const Caller &source) const
{
return this->action == source.action && this->owner == source.owner;
}
};
typedef std::list<Caller> FunctionList;
typedef std::map<std::string, FunctionList> FunctionMap;
class Script
{
private:
JSRuntime *runtime;
JSContext *context;
JSValue script;
myUnit *me;
unsigned int argc;
void **argv;
wchar_t *fileName;
char *szFileName;
int execCount;
ScriptState scriptState;
JSValue globalObject;
bool isLocked, isPaused, isReallyPaused, isAborted;
HANDLE threadHandle;
CRITICAL_SECTION lock;
ExecuteState execState;
Script(const wchar_t *file, ScriptState state, unsigned int argc = 0, void **argv = nullptr);
~Script();
bool BeginThread(LPTHREAD_START_ROUTINE ThreadFunc);
public:
friend class ScriptEngine;
DWORD threadId;
bool hasActiveCX; // hack to get away from JS_IsRunning
HANDLE eventSignal;
FunctionMap functions;
void Run(void);
void Join(void);
void Pause(void);
void Resume(void);
bool IsPaused(void);
void ExecEvent(void);
void WaitForExecEvent(int timeout);
inline JSContext *GetContext(void)
{
return context;
}
inline JSRuntime *GetRuntime(void)
{
return runtime;
}
inline void SetPauseState(bool reallyPaused)
{
isReallyPaused = reallyPaused;
}
inline bool IsReallyPaused(void)
{
return isReallyPaused;
}
inline const wchar_t *GetFilename(void)
{
return fileName;
}
inline ScriptState GetState(void)
{
return scriptState;
}
inline void TriggerInterruptHandler(void)
{
if (hasActiveCX)
JS_TriggerInterruptHandler(context);
}
inline int GetExecutionCount(void)
{
return execCount;
}
DWORD GetThreadId(void);
void Stop(bool force = false, bool reallyForce = false);
const wchar_t *GetShortFilename(void);
// UGLY HACK to fix up the player gid on game join for cached scripts/oog scripts
void UpdatePlayerGid(void);
void RunCommand(const wchar_t *command);
bool IsRunning(void);
bool IsAborted(void);
bool IsListenerRegistered(const char *evtName, bool extra = false);
void RegisterEvent(const char *evtName, const JSValue procVal, const JSValue thisVal = JS_UNDEFINED);
void UnregisterEvent(const char *evtName, const JSValue procVal, const JSValue thisVal = JS_UNDEFINED);
void ClearEvent(const char *evtName);
void ClearAllEvents(void);
void FireEvent(Event *);
std::list<Event *> EventList;
};
DWORD WINAPI ScriptThread(void *data);