-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.js
440 lines (369 loc) · 14.5 KB
/
index.js
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
'use strict'
const PRIVATE_CHANNEL_INDEX = 7,
PRIVATE_CHANNEL_ID = -2,
PUBLIC_MATCH = /^!([^!].*)$/;
class CommandBase {
constructor(mod) {
this.mod = mod;
this.loaded = false;
this.queue = [];
this.hooks = new Map();
mod.hook('S_LOGIN', 'event', () => { this.loaded = false; });
mod.hook('S_SPAWN_ME', 'event', () => {
if (this.loaded)
return;
this.loaded = true;
process.nextTick(() => {
mod.send('S_JOIN_PRIVATE_CHANNEL', 2, {
index: PRIVATE_CHANNEL_INDEX,
channelId: PRIVATE_CHANNEL_ID,
unk: [],
name: this.mod.settings.private_channel_name,
});
if (this.mod.settings.login_message)
this.message(null, `${this.mod.publisher.toUpperCase()}-${this.mod.majorPatchVersion}.${this.mod.minorPatchVersion} ${this.mod.environment !== 'live' ? `${this.mod.environment} ` : ''}(${this.mod.dispatch.protocolVersion})`);
mod.setTimeout(() => {
if (this.queue) {
const queue = this.queue;
this.queue = null;
queue.forEach(entry => this.message(...entry));
}
}, 2000);
});
})
mod.hook('S_JOIN_PRIVATE_CHANNEL', 2, event => event.index === PRIVATE_CHANNEL_INDEX ? false : undefined);
mod.hook('C_LEAVE_PRIVATE_CHANNEL', 1, event => event.index === PRIVATE_CHANNEL_INDEX ? false : undefined);
if (mod.majorPatchVersion >= 35) {
mod.hook('C_REQUEST_PRIVATE_CHANNEL_INFO', 2, event => {
if (event.channelId === PRIVATE_CHANNEL_ID) {
mod.send('S_REQUEST_PRIVATE_CHANNEL_INFO', 2, {
owner: true,
password: 0,
members: [],
friends: []
});
return false;
}
});
}
let lastError,
hookCommand = message => {
let args = null;
try {
args = parseArgs(stripOuterHTML(message));
} catch (e) {
return `Syntax error: ${e.message}`;
}
try {
if (!this.exec(args))
return `Unknown command "${args[0]}"`;
} catch (e) {
this.message(null, `Error running callback for command "${args[0]}"`);
mod.error(e);
}
}
mod.hook('C_CHAT', 1, { order: -10 }, event => {
if (event.channel === 11 + PRIVATE_CHANNEL_INDEX) {
lastError = hookCommand(event.message);
if (!lastError)
return false;
} else if (this.mod.settings.public_enable) {
const str = PUBLIC_MATCH.exec(stripOuterHTML(event.message));
if (str) {
lastError = hookCommand(str[1]);
if (!lastError)
return false;
}
}
});
// Let other modules handle possible commands before we silence them
mod.hook('C_CHAT', 1, { order: 10, filter: { silenced: null } }, event => {
if (lastError) {
if (!event.$silenced)
this.message(null, lastError);
lastError = undefined;
return false;
}
});
mod.hook('C_WHISPER', mod.majorPatchVersion >= 108 ? 2 : 1, { order: -10 }, event => {
if (!this.mod.settings.public_enable)
return;
const str = PUBLIC_MATCH.exec(stripOuterHTML(event.message));
if (str) {
lastError = hookCommand(str[1]);
if (!lastError)
return false;
}
});
// Let other modules handle possible commands before we silence them
mod.hook('C_WHISPER', mod.majorPatchVersion >= 108 ? 2 : 1, { order: 10, filter: { silenced: null } }, event => {
if (!this.mod.settings.public_enable)
return;
if (lastError) {
if (!event.$silenced)
this.message(null, lastError);
lastError = undefined;
return false;
}
});
// Add own commands
this.add(['toolbox', 'proxy'], {
$default() {
this.message(null, `TERA Toolbox commands:`);
this.message(null, `silent - Toggles ability to hide command messages from game chat`);
this.message(null, `onlychannel - Toggles ability to enter commands in Toolbox channel only (recommended) or all channels`);
this.message(null, `loginmessage - Toggles the status message shown on login`);
this.message(null, `load [module name] [0(default) - load network part, 1 - load fully] - Loads the given module`);
this.message(null, `unload [module name] [0(default) - unload network part, 1 - unload fully] - Unloads the given module`);
this.message(null, `reload [module name] - Reloads the given module`);
},
silent() {
this.mod.settings.silent_mode = !this.mod.settings.silent_mode;
this.message(null, `Command messages in game chat will be ${this.mod.settings.silent_mode ? 'hidden' : 'visible'}`);
},
onlychannel() {
this.mod.settings.public_enable = !this.mod.settings.public_enable;
this.message(null, `Commands can now be entered in ${this.mod.settings.public_enable ? 'all chat channels' : 'Toolbox channel only'}`);
},
loginmessage() {
this.mod.settings.login_message = !this.mod.settings.login_message;
this.message(null, `Toolbox login message ${this.mod.settings.login_message ? 'enabled' : 'disabled'}`);
},
load(name, mode = "0") {
if (!name) {
this.message(null, 'No module name specified!');
return;
}
if(mode == "0") {
let modRef = mod.manager.get(name);
if (modRef) {
modRef.loadNetworkInstance(this.mod.dispatch);
this.message(null, `Loaded network instance for mod "${name}" in current connection!`);
}
else {
this.message(null, `Unable to load network instance for mod "${name}"!`);
}
return;
}
const result = this.mod.manager.load(name);
if (result)
this.message(null, `Loaded "${name}"!`);
else
this.message(null, `Unable to load "${name}", check log for details!`);
},
unload(name, mode = "0") {
if (!name) {
this.message(null, 'No module name specified!');
return;
}
if(mode == "0") {
let modRef = mod.manager.get(name);
if (modRef) {
modRef.unloadNetworkInstance(this.mod.dispatch);
this.message(null, `Unloaded network instance for mod "${name}" in current connection!`);
}
else {
this.message(null, `Unable to unload network instance for mod "${name}"!`);
}
return;
}
const result = this.mod.manager.unload(name);
if (result)
this.message(null, `Unloaded "${name}"!`);
else
this.message(null, `Unable to unload "${name}", check log for details!`);
},
reload(name) {
if (!name) {
this.message(null, 'No module name specified!');
return;
}
const result = this.mod.manager.reload(name);
if (result)
this.message(null, `Reloaded "${name}"!`);
else
this.message(null, `Unable to reload "${name}", check log for details!`);
},
}, this);
}
exec(str) {
const args = Array.isArray(str) ? str : parseArgs(str);
if (args.length === 0)
return false;
const cb = this.hooks.get(args[0].toLowerCase());
if (cb) {
cb.call(...args);
return true;
}
return false;
}
add(cmd, cb, ctx) {
if (typeof cb === 'function') {
if (ctx !== undefined)
cb = cb.bind(ctx);
} else if (typeof cb === 'object') {
cb = makeSubCommandHandler(cb, ctx);
} else {
throw new Error('Callback must be a function or object');
}
if (Array.isArray(cmd)) {
for (let c of cmd)
this.add(c, cb);
return;
}
if (typeof cmd !== 'string')
throw new Error('Command must be a string or array of strings');
if (cmd === '')
throw new Error('Command must not be an empty string');
cmd = cmd.toLowerCase();
if (this.hooks.has(cmd))
throw new Error(`Command already registered: ${cmd}`);
this.hooks.set(cmd, cb);
}
remove(cmd) {
if (Array.isArray(cmd)) {
for (let c of cmd)
this.remove(c);
return;
}
if (typeof cmd !== 'string')
throw new Error('Command must be a string or array of strings');
if (cmd === '')
throw new Error('Command must not be an empty string');
this.hooks.delete(cmd.toLowerCase());
}
message(modName, msg) {
const showModName = modName && !this.mod.settings.hide_module_names;
if (this.queue) {
// Not ready yet, delay sending the message
this.queue.push([modName, msg]);
} else {
if (this.mod.settings.silent_mode)
this.mod.log(showModName ? `[${modName}] ${msg}` : msg);
else
this.mod.send('S_PRIVATE_CHAT', 1, {
channel: PRIVATE_CHANNEL_ID,
authorID: 0,
authorName: '',
message: (showModName) ? `[${modName}] ${msg}` : ` ${msg}`,
});
}
}
}
function makeSubCommandHandler(_obj, ctx) {
const obj = {};
for (let cmd in _obj) {
const cb = _obj[cmd];
cmd = cmd.toLowerCase();
if (typeof cb === 'function')
obj[cmd] = ctx !== undefined ? cb.bind(ctx) : cb;
else if (typeof cb === 'object')
obj[cmd] = makeSubCommandHandler(cb, ctx);
else
throw new Error('Sub-command callback must be a function or object');
}
return function subCommandHandler(cmd) {
const cb = (cmd !== undefined ? obj[cmd.toLowerCase()] : obj.$none) || obj.$default;
if (cb)
cb.apply(null, (arguments && cb !== obj.$default) ? Array.prototype.slice.call(arguments, 1) : arguments);
}
}
function stripOuterHTML(str) {
return str.replace(/^<[^>]+>|<\/[^>]+><[^\/][^>]*>|<\/[^>]+>$/g, '');
}
function parseArgs(str) {
const parseHTML = /.*?<\/.*?>/g,
args = [];
let arg = '',
quote = '';
for (let i = 0, c = ''; i < str.length; i++) {
c = str[i];
switch (c) {
case '<':
parseHTML.lastIndex = i + 1;
let len = parseHTML.exec(str);
if (!len)
throw new Error('HTML parsing failure');
len = len[0].length;
arg += str.substr(i, len + 1);
i += len;
break;
case '\\':
c = str[++i];
if (c === undefined)
throw new Error('Unexpected end of line');
arg += c;
break;
case '\'':
case '"':
if (arg === '' && quote === '') {
quote = c;
break;
}
if (quote === c) {
quote = '';
break;
}
arg += c;
break
case ' ':
if (quote === '') {
if (arg !== '') {
args.push(arg);
arg = '';
}
break;
}
default:
arg += c;
}
}
if (arg !== '') {
if (quote !== '')
throw new Error('Expected ' + quote);
args.push(arg);
}
return args;
}
class Command {
constructor(mod, base) {
this.mod = mod;
this.base = base || new CommandBase(mod);
this.commands = new Set();
}
destructor() {
this.base.remove(Array.from(this.commands));
this.commands.clear();
}
exec(str) {
return this.base.exec(str);
}
add(cmd, cb, ctx) {
this.base.add(cmd, cb, ctx);
if (Array.isArray(cmd)) {
for (let c of cmd)
this.commands.add(c);
} else {
this.commands.add(cmd);
}
}
remove(cmd) {
this.base.remove(cmd);
if (Array.isArray(cmd)) {
for (let c of cmd)
this.commands.delete(c);
} else {
this.commands.delete(cmd);
}
}
message(msg) {
return this.base.message(this.mod.info.options.cliName || this.mod.info.rawName, msg);
}
createInstance(mod) {
return new Command(mod, this.base);
}
}
module.exports = {
NetworkMod: Command,
RequireInterface: (globalMod, clientMod, networkMod, requiredBy) => networkMod,
};