-
Notifications
You must be signed in to change notification settings - Fork 0
/
scripts.nut
263 lines (193 loc) · 6.04 KB
/
scripts.nut
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
scripts <- [];
function scripts_load(name) {
local path = "./scripts/" + name;
print("Loading " + path);
local context = {};
dofile.call(context, path, true);
// Add all our global symbols to context, unless it's an event handler
local globals = getroottable();
foreach(key, value in globals) {
//print(" * " + key + ": " + value);
if (key in Hooks) {
//print(" # Skipping " + key);
continue;
}
if (type(value) == "function") {
value = value.bindenv(globals);
}
context[key] <- value;
}
scripts.append([name, context]);
}
function scripts_handle(name, ...) {
//print("Handling " + name + " in " + scripts.len() + " script(s)")
local final_result = 1;
// Add a dummy for the `this` pointer
local a = vargv;
a.insert(0, null);
foreach(script in scripts) {
local script_name = script[0];
local script_context = script[1];
if (!(name in script_context)) {
//print(" - " + script_name);
continue;
} else {
//print(" + " + script_name);
}
local f = script_context[name];
// Fixup `this` pointer
a[0] = script_context;
try {
local result = f.acall(a);
//FIXME: handle result conflicts
final_result = result;
}
catch(exception) {
print(exception);
print(getstackinfos(1).locals);
throw exception;
}
}
return final_result;
}
class Hooks {
function onServerStart() {
scripts_handle("onServerStart");
}
function onServerStop() {
scripts_handle("onServerStop");
}
function onScriptLoad() {
scripts_handle("onScriptLoad");
}
function onScriptUnload() {
scripts_handle("onScriptUnload");
}
// =========================================== P L A Y E R E V E N T S ==============================================
function onPlayerJoin( player ) {
scripts_handle("onPlayerJoin", player);
}
function onPlayerPart( player, reason ) {
scripts_handle("onPlayerPart", player, reason);
}
function onPlayerRequestClass( player, classID, team, skin ) {
return scripts_handle("onPlayerRequestClass", player, classID, team, skin);
}
function onPlayerRequestSpawn( player ) {
return scripts_handle("onPlayerRequestSpawn", player);
}
function onPlayerSpawn( player ) {
scripts_handle("onPlayerSpawn", player);
}
function onPlayerDeath( player, reason ) {
scripts_handle("onPlayerDeath", player, reason);
}
function onPlayerKill( player, killer, reason, bodypart ) {
scripts_handle("onPlayerKill", player, killer, reason, bodypart);
}
function onPlayerTeamKill( player, killer, reason, bodypart ) {
scripts_handle("onPlayerTeamKill", player, killer, reason, bodypart);
}
function onPlayerChat( player, text ) {
return scripts_handle("onPlayerChat", player, text);
}
function onPlayerCommand( player, cmd, text ) {
return scripts_handle("onPlayerCommand", player, cmd, text);
}
function onPlayerPM( player, playerTo, message ) {
return scripts_handle("onPlayerPM", player, playerTo, message);
}
function onPlayerBeginTyping( player ) {
}
function onPlayerEndTyping( player ) {
}
/*
function onLoginAttempt( player ) {
return 1;
}
*/
function onNameChangeable( player ) {
}
function onPlayerSpectate( player, target ) {
}
function onPlayerCrashDump( player, crash ) {
}
function onPlayerMove( player, lastX, lastY, lastZ, newX, newY, newZ ) {
scripts_handle("onPlayerMove", player, lastX, lastY, lastZ, newX, newY, newZ);
}
function onPlayerHealthChange( player, lastHP, newHP ) {
scripts_handle("onPlayerHealthChange", player, lastHP, newHP);
}
function onPlayerArmourChange( player, lastArmour, newArmour ) {
}
function onPlayerWeaponChange( player, oldWep, newWep ) {
}
function onPlayerAwayChange( player, status ) {
}
function onPlayerNameChange( player, oldName, newName ) {
}
function onPlayerActionChange( player, oldAction, newAction ) {
}
function onPlayerStateChange( player, oldState, newState ) {
}
function onPlayerOnFireChange( player, IsOnFireNow ) {
}
function onPlayerCrouchChange( player, IsCrouchingNow ) {
}
function onPlayerGameKeysChange( player, oldKeys, newKeys ) {
return scripts_handle("onPlayerGameKeysChange", player, oldKeys, newKeys);
}
// ========================================== V E H I C L E E V E N T S =============================================
function onPlayerEnteringVehicle( player, vehicle, door ) {
return scripts_handle("onPlayerEnteringVehicle", player, vehicle, door);
}
function onPlayerEnterVehicle( player, vehicle, door ) {
scripts_handle("onPlayerEnterVehicle", player, vehicle, door);
}
function onPlayerExitVehicle( player, vehicle ) {
scripts_handle("onPlayerExitVehicle", player, vehicle);
}
function onVehicleExplode( vehicle ) {
scripts_handle("onVehicleExplode", vehicle);
}
function onVehicleRespawn( vehicle ) {
scripts_handle("onVehicleRespawn", vehicle);
}
function onVehicleHealthChange( vehicle, oldHP, newHP ) {
scripts_handle("onVehicleHealthChange", vehicle, oldHP, newHP);
}
function onVehicleMove( vehicle, lastX, lastY, lastZ, newX, newY, newZ ) {
}
// =========================================== P I C K U P E V E N T S ==============================================
function onPickupClaimPicked( player, pickup ) {
return 1;
}
function onPickupPickedUp( player, pickup ) {
}
function onPickupRespawn( pickup ) {
}
// ========================================== O B J E C T E V E N T S ==============================================
function onObjectShot( object, player, weapon ) {
}
function onObjectBump( object, player ) {
}
// =========================================== B I N D E V E N T S ==============================================
function onKeyDown( player, key ) {
scripts_handle("onKeyDown", player, key);
}
function onKeyUp( player, key ) {
scripts_handle("onKeyUp", player, key);
}
// Forgotten stuff
function onCheckpointEntered( player, checkpoint ) {
scripts_handle("onCheckpointEntered", player, checkpoint);
}
}
// Add all hooks to global table
local globals = getroottable();
foreach(key, value in Hooks) {
if (key in globals) {
print(key + " already hooked");
}
globals[key] <- value;
}