forked from noah-/d2bs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScript.h
199 lines (176 loc) · 5.63 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
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
#pragma once
#include <windows.h>
#include <string>
#include <map>
#include <list>
#include "js32.h"
#include "JSGlobalClasses.h"
#include "AutoRoot.h"
#include "JSUnit.h"
#include "Events.h"
enum ScriptState {
InGame,
OutOfGame,
Command
};
class Script;
typedef std::map<std::string, bool> IncludeList;
typedef std::list<AutoRoot*> FunctionList;
typedef std::map<std::string, FunctionList> FunctionMap;
typedef std::list<Script*> ScriptList;
struct Event {
Event(): count(0){ };
Script* owner;
JSObject* object;
FunctionList functions;
JSAutoStructuredCloneBuffer** argv;
uintN argc;
char* name;
void* arg1;
void* arg2;
void* arg3;
void* arg4;
void* arg5;
volatile long count;
inline void threadFinished(){
// clean up after both threads are done with the event
char* evtName = (char*) name;
InterlockedIncrement(&count);
if (count > 1)
{
Event *evt = this;
if (strcmp(evtName,"itemaction") == 0){
delete arg1;
free (arg2);
delete arg3;
delete arg4;
}
if (strcmp(evtName, "gameevent") == 0){
delete evt->arg1;
delete evt->arg2;
delete evt->arg3;
free(evt->arg4);
free(evt->arg5);
}
if (strcmp(evtName, "copydata") == 0){
delete evt->arg1;
free (evt->arg2);
}
if (strcmp(evtName, "chatmsg") == 0 || strcmp(evtName, "whispermsg") == 0 || strcmp(evtName, "chatmsgblocker") == 0 || strcmp(evtName, "whispermsgblocker") == 0){
free(evt->arg1);
free(evt->arg2);
free(evt->name);
}
if (strcmp(evtName, "mousemove") == 0 || strcmp(evtName, "ScreenHookHover") == 0) {
delete evt->arg1;
delete evt->arg2;
}
if (strcmp(evtName, "mouseclick") == 0) {
delete evt->arg1;
delete evt->arg2;
delete evt->arg3;
delete evt->arg4;
}
if (strcmp(evtName, "keyup") == 0 || strcmp(evtName, "keydownblocker") == 0 || strcmp(evtName, "keydown") == 0 || strcmp(evtName, "memana") == 0 || strcmp(evtName, "melife" ) == 0 || strcmp(evtName, "playerassign") == 0) {
delete evt->arg1;
free (evt->name);
}
if (strcmp(evtName, "ScreenHookClick") == 0 ) {
delete evt->arg1;
delete evt->arg2;
delete evt->arg3;
delete evt->arg4;
}
if (strcmp(evtName, "Command") == 0){
//cleaned up in ExecScriptEvent
}
if (strcmp(evtName, "scriptmsg") == 0) {
delete evt->arg1;
delete evt;
}
if (strcmp(evtName, "gamepacket") == 0 || strcmp(evtName, "gamepacketsent") == 0 || strcmp(evtName, "realmpacket") == 0) {
delete evt->arg1;
delete evt->arg2;
delete evt->arg4;
}
Event::~Event();
}
};
};
class Script
{
private:
std::string fileName;
int execCount;
ScriptState scriptState;
JSContext* context;
JSScript* script;
JSRuntime* runtime;
myUnit* me;
uintN argc;
JSAutoStructuredCloneBuffer** argv;
JSObject *globalObject, *scriptObject;
bool isLocked, isPaused, isReallyPaused, isAborted;
IncludeList includes, inProgress;
HANDLE threadHandle;
CRITICAL_SECTION lock;
Script(const char* file, ScriptState state, uintN argc = 0, JSAutoStructuredCloneBuffer** argv = NULL);
Script(const Script&);
Script& operator=(const Script&);
~Script(void);
public:
DWORD threadId;
friend class ScriptEngine;
FunctionMap functions;
void Run(void);
void Join(void);
void Pause(void);
void Resume(void);
bool IsPaused(void);
bool BeginThread(LPTHREAD_START_ROUTINE ThreadFunc);
void RunCommand(const char* command);
inline void SetPauseState(bool reallyPaused) { isReallyPaused = reallyPaused; }
inline bool IsReallyPaused(void) { return isReallyPaused; }
void Stop(bool force = false, bool reallyForce = false);
inline const char* GetFilename(void) { return fileName.c_str(); }
const char* GetShortFilename(void);
inline JSContext* GetContext(void) { return context; }
inline JSRuntime* GetRuntime(void) { return runtime; }
inline JSObject* GetGlobalObject(void) { return globalObject; }
inline JSObject* GetScriptObject(void) { return scriptObject; }
inline ScriptState GetState(void) { return scriptState; }
inline void TriggerOperationCallback(void) {if (hasActiveCX) JS_TriggerOperationCallback(runtime);}
int GetExecutionCount(void);
DWORD GetThreadId(void);
DWORD LastGC;
bool hasActiveCX; //hack to get away from JS_IsRunning
HANDLE eventSignal;
// UGLY HACK to fix up the player gid on game join for cached scripts/oog scripts
void UpdatePlayerGid(void);
// Hack. Include from console needs to run on the RunCommandThread / cx.
// a better solution may be to keep a list of threadId / cx and have a GetCurrentThreadCX()
inline void SetContext(JSContext* cx) { context = cx;}
bool IsRunning(void);
bool IsAborted(void);
void Lock() { EnterCriticalSection(&lock);} // needed for events walking function list
void Unlock() { LeaveCriticalSection(&lock);}
bool IsIncluded(const char* file);
bool Include(const char* file);
bool IsListenerRegistered(const char* evtName);
void RegisterEvent(const char* evtName, jsval evtFunc);
bool IsRegisteredEvent(const char* evtName, jsval evtFunc);
void UnregisterEvent(const char* evtName, jsval evtFunc);
void ClearEvent(const char* evtName);
void ClearAllEvents(void);
void FireEvent(Event* );
std::list<Event*> EventList;
};
struct RUNCOMMANDSTRUCT {
Script* script;
const char* command;
};
DWORD WINAPI RunCommandThread(void* data);
DWORD WINAPI ScriptThread(void* data);
DWORD WINAPI FuncThread(void* data);
DWORD WINAPI EventThread(LPVOID lpParam);
bool callEventFunction(JSContext* cx ,Event* evt);