forked from quisquous/cactbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnetregexes.ts
439 lines (384 loc) · 14.9 KB
/
netregexes.ts
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
import { NetFieldsReverse } from '../types/net_fields';
import { NetParams } from '../types/net_props';
import { CactbotBaseRegExp } from '../types/net_trigger';
import {
logDefinitionsVersions,
LogDefinitionTypes,
LogDefinitionVersions,
ParseHelperFields,
} from './netlog_defs';
import Regexes from './regexes';
const separator = '\\|';
const matchDefault = '[^|]*';
// If NetRegexes.setFlagTranslationsNeeded is set to true, then any
// regex created that requires a translation will begin with this string
// and match the magicStringRegex. This is maybe a bit goofy, but is
// a pretty straightforward way to mark regexes for translations.
// If issue #1306 is ever resolved, we can remove this.
const magicTranslationString = `^^`;
const magicStringRegex = /^\^\^/;
// can't simply export this, see https://github.com/quisquous/cactbot/pull/4957#discussion_r1002590589
const keysThatRequireTranslationAsConst = [
'ability',
'name',
'source',
'target',
'line',
] as const;
export const keysThatRequireTranslation: readonly string[] = keysThatRequireTranslationAsConst;
export type KeysThatRequireTranslation = typeof keysThatRequireTranslationAsConst[number];
const defaultParams = <
T extends LogDefinitionTypes,
V extends LogDefinitionVersions,
>(type: T, version: V, include?: string[]): Partial<ParseHelperFields<T>> => {
include ??= Object.keys(logDefinitionsVersions[version][type].fields);
const params: { [index: number]: { field: string; value?: string; optional: boolean } } = {};
const firstOptionalField = logDefinitionsVersions[version][type].firstOptionalField;
for (const [prop, index] of Object.entries(logDefinitionsVersions[version][type].fields)) {
if (!include.includes(prop))
continue;
const param: { field: string; value?: string; optional: boolean } = {
field: prop,
optional: firstOptionalField !== undefined && index >= firstOptionalField,
};
if (prop === 'type')
param.value = logDefinitionsVersions[version][type].type;
params[index] = param;
}
return params as unknown as Partial<ParseHelperFields<T>>;
};
type ParseHelperType<T extends LogDefinitionTypes> =
& {
[field in Extract<keyof NetFieldsReverse[T], string>]?: string;
}
& { capture?: boolean };
const parseHelper = <T extends LogDefinitionTypes>(
params: ParseHelperType<T> | undefined,
funcName: string,
fields: Partial<ParseHelperFields<T>>,
): CactbotBaseRegExp<T> => {
params = params ?? {};
const validFields: string[] = [];
for (const index in fields) {
const field = fields[index];
if (field)
validFields.push(field.field);
}
Regexes.validateParams(params, funcName, ['capture', ...validFields]);
// Find the last key we care about, so we can shorten the regex if needed.
const capture = Regexes.trueIfUndefined(params.capture);
const fieldKeys = Object.keys(fields).sort((a, b) => parseInt(a) - parseInt(b));
let maxKeyStr: string;
if (capture) {
const keys: Extract<keyof NetFieldsReverse[T], string>[] = [];
for (const key in fields)
keys.push(key);
let tmpKey = keys.pop();
if (tmpKey === undefined) {
maxKeyStr = fieldKeys[fieldKeys.length - 1] ?? '0';
} else {
while (
fields[tmpKey]?.optional &&
!((fields[tmpKey]?.field ?? '') in params)
)
tmpKey = keys.pop();
maxKeyStr = tmpKey ?? '0';
}
} else {
maxKeyStr = '0';
for (const key in fields) {
const value = fields[key] ?? {};
if (typeof value !== 'object')
continue;
const fieldName = fields[key]?.field;
if (fieldName !== undefined && fieldName in params)
maxKeyStr = key;
}
}
const maxKey = parseInt(maxKeyStr);
// For testing, it's useful to know if this is a regex that requires
// translation. We test this by seeing if there are any specified
// fields, and if so, inserting a magic string that we can detect.
// This lets us differentiate between "regex that should be translated"
// e.g. a regex with `target` specified, and "regex that shouldn't"
// e.g. a gains effect with just effectId specified.
const transParams = Object.keys(params).filter((k) => keysThatRequireTranslation.includes(k));
const needsTranslations = NetRegexes.flagTranslationsNeeded && transParams.length > 0;
// Build the regex from the fields.
let str = needsTranslations ? magicTranslationString : '^';
let lastKey = -1;
for (const keyStr in fields) {
const key = parseInt(keyStr);
// Fill in blanks.
const missingFields = key - lastKey - 1;
if (missingFields === 1)
str += '\\y{NetField}';
else if (missingFields > 1)
str += `\\y{NetField}{${missingFields}}`;
lastKey = key;
const value = fields[keyStr];
if (typeof value !== 'object')
throw new Error(`${funcName}: invalid value: ${JSON.stringify(value)}`);
const fieldName = fields[keyStr]?.field;
const fieldValue = fields[keyStr]?.value?.toString() ?? matchDefault;
if (fieldName !== undefined) {
str += Regexes.maybeCapture(
// more accurate type instead of `as` cast
// maybe this function needs a refactoring
capture,
fieldName,
params[fieldName],
fieldValue,
) +
separator;
} else {
str += fieldValue + separator;
}
// Stop if we're not capturing and don't care about future fields.
if (key >= maxKey)
break;
}
return Regexes.parse(str) as CactbotBaseRegExp<T>;
};
export const buildRegex = <T extends keyof NetParams>(
type: T,
params?: ParseHelperType<T>,
): CactbotBaseRegExp<T> => {
return parseHelper(params, type, defaultParams(type, NetRegexes.logVersion));
};
export default class NetRegexes {
static logVersion: LogDefinitionVersions = 'latest';
static flagTranslationsNeeded = false;
static setFlagTranslationsNeeded(value: boolean): void {
NetRegexes.flagTranslationsNeeded = value;
}
static doesNetRegexNeedTranslation(regex: RegExp | string): boolean {
// Need to `setFlagTranslationsNeeded` before calling this function.
console.assert(NetRegexes.flagTranslationsNeeded);
const str = typeof regex === 'string' ? regex : regex.source;
return !!magicStringRegex.exec(str);
}
/**
* matches: https://github.com/quisquous/cactbot/blob/main/docs/LogGuide.md#line-20-0x14-networkstartscasting
*/
static startsUsing(params?: NetParams['StartsUsing']): CactbotBaseRegExp<'StartsUsing'> {
return buildRegex('StartsUsing', params);
}
/**
* matches: https://github.com/quisquous/cactbot/blob/main/docs/LogGuide.md#line-21-0x15-networkability
* matches: https://github.com/quisquous/cactbot/blob/main/docs/LogGuide.md#line-22-0x16-networkaoeability
*/
static ability(params?: NetParams['Ability']): CactbotBaseRegExp<'Ability'> {
return parseHelper(params, 'Ability', {
...defaultParams('Ability', NetRegexes.logVersion),
// Override type
0: { field: 'type', value: '2[12]', optional: false },
});
}
/**
* matches: https://github.com/quisquous/cactbot/blob/main/docs/LogGuide.md#line-21-0x15-networkability
* matches: https://github.com/quisquous/cactbot/blob/main/docs/LogGuide.md#line-22-0x16-networkaoeability
*
* @deprecated Use `ability` instead
*/
static abilityFull(params?: NetParams['Ability']): CactbotBaseRegExp<'Ability'> {
return this.ability(params);
}
/**
* matches: https://github.com/quisquous/cactbot/blob/main/docs/LogGuide.md#line-27-0x1b-networktargeticon-head-marker
*/
static headMarker(params?: NetParams['HeadMarker']): CactbotBaseRegExp<'HeadMarker'> {
return buildRegex('HeadMarker', params);
}
/**
* matches: https://github.com/quisquous/cactbot/blob/main/docs/LogGuide.md#line-03-0x03-addcombatant
*/
static addedCombatant(params?: NetParams['AddedCombatant']): CactbotBaseRegExp<'AddedCombatant'> {
return parseHelper(
params,
'AddedCombatant',
defaultParams('AddedCombatant', NetRegexes.logVersion),
);
}
/**
* matches: https://github.com/quisquous/cactbot/blob/main/docs/LogGuide.md#line-03-0x03-addcombatant
* @deprecated Use `addedCombatant` instead
*/
static addedCombatantFull(
params?: NetParams['AddedCombatant'],
): CactbotBaseRegExp<'AddedCombatant'> {
return NetRegexes.addedCombatant(params);
}
/**
* matches: https://github.com/quisquous/cactbot/blob/main/docs/LogGuide.md#line-04-0x04-removecombatant
*/
static removingCombatant(
params?: NetParams['RemovedCombatant'],
): CactbotBaseRegExp<'RemovedCombatant'> {
return buildRegex('RemovedCombatant', params);
}
/**
* matches: https://github.com/quisquous/cactbot/blob/main/docs/LogGuide.md#line-26-0x1a-networkbuff
*/
static gainsEffect(params?: NetParams['GainsEffect']): CactbotBaseRegExp<'GainsEffect'> {
return buildRegex('GainsEffect', params);
}
/**
* Prefer gainsEffect over this function unless you really need extra data.
* matches: https://github.com/quisquous/cactbot/blob/main/docs/LogGuide.md#line-38-0x26-networkstatuseffects
*/
static statusEffectExplicit(
params?: NetParams['StatusEffect'],
): CactbotBaseRegExp<'StatusEffect'> {
return buildRegex('StatusEffect', params);
}
/**
* matches: https://github.com/quisquous/cactbot/blob/main/docs/LogGuide.md#line-30-0x1e-networkbuffremove
*/
static losesEffect(params?: NetParams['LosesEffect']): CactbotBaseRegExp<'LosesEffect'> {
return buildRegex('LosesEffect', params);
}
/**
* matches: https://github.com/quisquous/cactbot/blob/main/docs/LogGuide.md#line-35-0x23-networktether
*/
static tether(params?: NetParams['Tether']): CactbotBaseRegExp<'Tether'> {
return buildRegex('Tether', params);
}
/**
* 'target' was defeated by 'source'
* matches: https://github.com/quisquous/cactbot/blob/main/docs/LogGuide.md#line-25-0x19-networkdeath
*/
static wasDefeated(params?: NetParams['WasDefeated']): CactbotBaseRegExp<'WasDefeated'> {
return buildRegex('WasDefeated', params);
}
/**
* matches: https://github.com/quisquous/cactbot/blob/main/docs/LogGuide.md#line-24-0x18-networkdot
*/
static networkDoT(params?: NetParams['NetworkDoT']): CactbotBaseRegExp<'NetworkDoT'> {
return buildRegex('NetworkDoT', params);
}
/**
* matches: https://github.com/quisquous/cactbot/blob/main/docs/LogGuide.md#line-00-0x00-logline
*/
static echo(params?: Omit<NetParams['GameLog'], 'code'>): CactbotBaseRegExp<'GameLog'> {
if (typeof params === 'undefined')
params = {};
Regexes.validateParams(
params,
'Echo',
['type', 'timestamp', 'code', 'name', 'line', 'capture'],
);
return NetRegexes.gameLog({ ...params, code: '0038' });
}
/**
* matches: https://github.com/quisquous/cactbot/blob/main/docs/LogGuide.md#line-00-0x00-logline
*/
static dialog(params?: Omit<NetParams['GameLog'], 'code'>): CactbotBaseRegExp<'GameLog'> {
if (typeof params === 'undefined')
params = {};
Regexes.validateParams(
params,
'Dialog',
['type', 'timestamp', 'code', 'name', 'line', 'capture'],
);
return NetRegexes.gameLog({ ...params, code: '0044' });
}
/**
* matches: https://github.com/quisquous/cactbot/blob/main/docs/LogGuide.md#line-00-0x00-logline
*/
static message(params?: Omit<NetParams['GameLog'], 'code'>): CactbotBaseRegExp<'GameLog'> {
if (typeof params === 'undefined')
params = {};
Regexes.validateParams(
params,
'Message',
['type', 'timestamp', 'code', 'name', 'line', 'capture'],
);
return NetRegexes.gameLog({ ...params, code: '0839' });
}
/**
* fields: code, name, line, capture
* matches: https://github.com/quisquous/cactbot/blob/main/docs/LogGuide.md#line-00-0x00-logline
*/
static gameLog(params?: NetParams['GameLog']): CactbotBaseRegExp<'GameLog'> {
return buildRegex('GameLog', params);
}
/**
* matches: https://github.com/quisquous/cactbot/blob/main/docs/LogGuide.md#line-00-0x00-logline
*/
static gameNameLog(params?: NetParams['GameLog']): CactbotBaseRegExp<'GameLog'> {
// Backwards compatability.
return NetRegexes.gameLog(params);
}
/**
* matches: https://github.com/quisquous/cactbot/blob/main/docs/LogGuide.md#line-12-0x0c-playerstats
*/
static statChange(params?: NetParams['PlayerStats']): CactbotBaseRegExp<'PlayerStats'> {
return buildRegex('PlayerStats', params);
}
/**
* matches: https://github.com/quisquous/cactbot/blob/main/docs/LogGuide.md#line-01-0x01-changezone
*/
static changeZone(params?: NetParams['ChangeZone']): CactbotBaseRegExp<'ChangeZone'> {
return buildRegex('ChangeZone', params);
}
/**
* matches: https://github.com/quisquous/cactbot/blob/main/docs/LogGuide.md#line-33-0x21-network6d-actor-control
*/
static network6d(params?: NetParams['ActorControl']): CactbotBaseRegExp<'ActorControl'> {
return buildRegex('ActorControl', params);
}
/**
* matches: https://github.com/quisquous/cactbot/blob/main/docs/LogGuide.md#line-34-0x22-networknametoggle
*/
static nameToggle(params?: NetParams['NameToggle']): CactbotBaseRegExp<'NameToggle'> {
return buildRegex('NameToggle', params);
}
/**
* matches: https://github.com/quisquous/cactbot/blob/main/docs/LogGuide.md#line-40-0x28-map
*/
static map(params?: NetParams['Map']): CactbotBaseRegExp<'Map'> {
return buildRegex('Map', params);
}
/**
* matches: https://github.com/quisquous/cactbot/blob/main/docs/LogGuide.md#line-41-0x29-systemlogmessage
*/
static systemLogMessage(
params?: NetParams['SystemLogMessage'],
): CactbotBaseRegExp<'SystemLogMessage'> {
return buildRegex('SystemLogMessage', params);
}
/**
* matches: https://github.com/quisquous/cactbot/blob/main/docs/LogGuide.md#line-257-0x101-mapeffect
*/
static mapEffect(params?: NetParams['MapEffect']): CactbotBaseRegExp<'MapEffect'> {
return buildRegex('MapEffect', params);
}
/**
* matches: https://github.com/quisquous/cactbot/blob/main/docs/LogGuide.md#line-258-0x102-fatedirector
*/
static fateDirector(params?: NetParams['FateDirector']): CactbotBaseRegExp<'FateDirector'> {
return buildRegex('FateDirector', params);
}
/**
* matches: https://github.com/quisquous/cactbot/blob/main/docs/LogGuide.md#line-259-0x103-cedirector
*/
static ceDirector(params?: NetParams['CEDirector']): CactbotBaseRegExp<'CEDirector'> {
return buildRegex('CEDirector', params);
}
}
export const commonNetRegex = {
// TODO(6.2): remove 40000010 after everybody is on 6.2.
wipe: NetRegexes.network6d({ command: ['40000010', '4000000F'] }),
cactbotWipeEcho: NetRegexes.echo({ line: 'cactbot wipe.*?' }),
userWipeEcho: NetRegexes.echo({ line: 'end' }),
} as const;
export const buildNetRegexForTrigger = <T extends keyof NetParams>(
type: T,
params?: ParseHelperType<T>,
): CactbotBaseRegExp<T> => {
if (type === 'Ability')
// ts can't narrow T to `Ability` here, need casting.
return NetRegexes.ability(params) as CactbotBaseRegExp<T>;
return buildRegex<T>(type, params);
};