forked from quisquous/cactbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
timeline_parser.ts
462 lines (420 loc) · 15.2 KB
/
timeline_parser.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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
import { Lang } from '../../resources/languages';
import { UnreachableCode } from '../../resources/not_reached';
import Regexes from '../../resources/regexes';
import { translateRegex, translateText } from '../../resources/translations';
import { LooseTimelineTrigger, TriggerAutoConfig } from '../../types/trigger';
import defaultOptions, { RaidbossOptions, TimelineConfig } from './raidboss_options';
export type TimelineReplacement = {
locale: Lang;
missingTranslations?: boolean;
replaceSync?: { [regexString: string]: string };
replaceText?: { [timelineText: string]: string };
};
export type TimelineStyle = {
style: { [key: string]: string };
regex: RegExp;
};
export type Event = {
id: number;
time: number;
name: string;
text: string;
activeTime?: number;
lineNumber?: number;
duration?: number;
sortKey?: number;
isDur?: boolean;
style?: { [key: string]: string };
};
export type Error = {
lineNumber?: number;
line?: string;
error: string;
};
export type Sync = {
id: number;
origRegexStr: string;
regex: RegExp;
start: number;
end: number;
time: number;
lineNumber: number;
jump?: number;
};
type ParsedPopupText = {
type: 'info' | 'alert' | 'alarm' | 'tts';
secondsBefore?: number;
text: string;
};
type ParsedTriggerText = {
type: 'trigger';
secondsBefore?: number;
text?: string;
matches: RegExpExecArray | null;
trigger: LooseTimelineTrigger;
};
export type ParsedText = ParsedPopupText | ParsedTriggerText;
export type Text = ParsedText & { time: number };
// This class reads the format of ACT Timeline plugin, described in
// docs/TimelineGuide.md
export class TimelineParser {
protected options: RaidbossOptions;
protected perTriggerAutoConfig: { [triggerId: string]: TriggerAutoConfig };
protected replacements: TimelineReplacement[];
private timelineConfig: TimelineConfig;
public ignores: { [ignoreId: string]: boolean };
public events: Event[];
public texts: Text[];
public syncStarts: Sync[];
public syncEnds: Sync[];
public errors: Error[];
constructor(
text: string,
replacements: TimelineReplacement[],
triggers: LooseTimelineTrigger[],
styles?: TimelineStyle[],
options?: RaidbossOptions,
zoneId?: number,
) {
this.options = options ?? defaultOptions;
this.perTriggerAutoConfig = this.options.PerTriggerAutoConfig;
this.replacements = replacements;
// A set of names which will not be notified about.
this.ignores = {};
// Sorted by event occurrence time.
this.events = [];
// Sorted by event occurrence time.
this.texts = [];
// Sorted by sync.start time.
this.syncStarts = [];
// Sorted by sync.end time.
this.syncEnds = [];
// Sorted by line.
this.errors = [];
this.timelineConfig = typeof zoneId === 'number'
? this.options.PerZoneTimelineConfig[zoneId] ?? {}
: {};
for (const text of this.timelineConfig.Ignore ?? [])
this.ignores[text] = true;
let uniqueId = 0;
for (const event of this.timelineConfig.Add ?? []) {
this.events.push({
id: ++uniqueId,
time: event.time,
name: event.text,
text: event.text,
duration: event.duration,
activeTime: 0,
});
}
this.parse(text, triggers, styles ?? [], uniqueId);
}
private parse(
text: string,
triggers: LooseTimelineTrigger[],
styles: TimelineStyle[],
initialId: number,
): void {
let uniqueid = initialId;
const texts: { [id: string]: ParsedText[] } = {};
const regexes = {
comment: /^\s*#/,
commentLine: /#.*$/,
durationCommand: /(?:[^#]*?\s)?(?<text>duration\s+(?<seconds>[0-9]+(?:\.[0-9]+)?))(\s.*)?$/,
ignore: /^hideall\s+\"(?<id>[^"]+)\"(?:\s*#.*)?$/,
jumpCommand: /(?:[^#]*?\s)?(?<text>jump\s+(?<seconds>[0-9]+(?:\.[0-9]+)?))(?:\s.*)?$/,
line: /^(?<text>(?<time>[0-9]+(?:\.[0-9]+)?)\s+"(?<name>.*?)")(\s+(.*))?/,
popupText:
/^(?<type>info|alert|alarm)text\s+\"(?<id>[^"]+)\"\s+before\s+(?<beforeSeconds>-?[0-9]+(?:\.[0-9]+)?)(?:\s+\"(?<text>[^"]+)\")?$/,
soundAlert: /^define\s+soundalert\s+"[^"]*"\s+"[^"]*"$/,
speaker:
/define speaker "[^"]*"(\s+"[^"]*")?\s+(-?[0-9]+(?:\.[0-9]+)?)\s+(-?[0-9]+(?:\.[0-9]+)?)/,
syncCommand: /(?:[^#]*?\s)?(?<text>sync\s*\/(?<regex>.*)\/)(?<args>\s.*)?$/,
tts:
/^alertall\s+"(?<id>[^"]*)"\s+before\s+(?<beforeSeconds>-?[0-9]+(?:\.[0-9]+)?)\s+(?<command>sound|speak\s+"[^"]*")\s+"(?<text>[^"]*)"$/,
windowCommand:
/(?:[^#]*?\s)?(?<text>window\s+(?:(?<start>[0-9]+(?:\.[0-9]+)?),)?(?<end>[0-9]+(?:\.[0-9]+)?))(?:\s.*)?$/,
};
// Make all regexes case insensitive, and parse any special \y{} groups.
for (const trigger of triggers ?? []) {
if (trigger.regex)
trigger.regex = Regexes.parse(trigger.regex);
}
const lines = text.split('\n');
let lineNumber = 0;
for (let line of lines) {
++lineNumber;
line = line.trim();
// Drop comments and empty lines.
if (!line || regexes.comment.test(line))
continue;
const originalLine = line;
let match = regexes.ignore.exec(line);
if (match && match['groups']) {
const ignore = match['groups'];
if (ignore.id)
this.ignores[ignore.id] = true;
continue;
}
match = regexes.tts.exec(line);
if (match && match['groups']) {
const tts = match['groups'];
if (!tts.id || !tts.beforeSeconds || !tts.command)
throw new UnreachableCode();
// TODO: Support alert sounds?
if (tts.command === 'sound')
continue;
const ttsItems = texts[tts.id] || [];
texts[tts.id] = ttsItems;
ttsItems.push({
type: 'tts',
secondsBefore: parseFloat(tts.beforeSeconds),
text: tts.text ? tts.text : tts.id,
});
continue;
}
match = regexes.soundAlert.exec(line);
if (match)
continue;
match = regexes.speaker.exec(line);
if (match)
continue;
match = regexes.popupText.exec(line);
if (match && match['groups']) {
const popupText = match['groups'];
if (!popupText.type || !popupText.id || !popupText.beforeSeconds)
throw new UnreachableCode();
const popupTextItems = texts[popupText.id] || [];
texts[popupText.id] = popupTextItems;
const type = popupText.type;
if (type !== 'info' && type !== 'alert' && type !== 'alarm')
continue;
popupTextItems.push({
type: type,
secondsBefore: parseFloat(popupText.beforeSeconds),
text: popupText.text ? popupText.text : popupText.id,
});
continue;
}
match = regexes.line.exec(line);
if (!(match && match['groups'])) {
this.errors.push({
lineNumber: lineNumber,
line: originalLine,
error: 'Invalid format',
});
console.log('Unknown timeline: ' + originalLine);
continue;
}
const parsedLine = match['groups'];
// Technically the name can be empty
if (!parsedLine.text || !parsedLine.time || parsedLine.name === undefined)
throw new UnreachableCode();
line = line.replace(parsedLine.text, '').trim();
// There can be # in the ability name, but probably not in the regex.
line = line.replace(regexes.commentLine, '').trim();
const seconds = parseFloat(parsedLine.time);
const e: Event = {
id: ++uniqueid,
time: seconds,
// The original ability name in the timeline. Used for hideall, infotext, etc.
name: parsedLine.name,
// The text to display. Not used for any logic.
text: this.GetReplacedText(parsedLine.name),
activeTime: 0,
lineNumber: lineNumber,
};
if (line) {
let commandMatch = regexes.durationCommand.exec(line);
if (commandMatch && commandMatch['groups']) {
const durationCommand = commandMatch['groups'];
if (!durationCommand.text || !durationCommand.seconds)
throw new UnreachableCode();
line = line.replace(durationCommand.text, '').trim();
e.duration = parseFloat(durationCommand.seconds);
}
commandMatch = regexes.syncCommand.exec(line);
if (commandMatch && commandMatch['groups']) {
const syncCommand = commandMatch['groups'];
if (!syncCommand.text || !syncCommand.regex)
throw new UnreachableCode();
line = line.replace(syncCommand.text, '').trim();
const sync: Sync = {
id: uniqueid,
origRegexStr: syncCommand.regex,
regex: Regexes.parse(this.GetReplacedSync(syncCommand.regex)),
start: seconds - 2.5,
end: seconds + 2.5,
time: seconds,
lineNumber: lineNumber,
};
if (syncCommand.args) {
let argMatch = regexes.windowCommand.exec(syncCommand.args);
if (argMatch && argMatch['groups']) {
const windowCommand = argMatch['groups'];
if (!windowCommand.text || !windowCommand.end)
throw new UnreachableCode();
line = line.replace(windowCommand.text, '').trim();
if (windowCommand.start) {
sync.start = seconds - parseFloat(windowCommand.start);
sync.end = seconds + parseFloat(windowCommand.end);
} else {
sync.start = seconds - parseFloat(windowCommand.end) / 2;
sync.end = seconds + parseFloat(windowCommand.end) / 2;
}
}
argMatch = regexes.jumpCommand.exec(syncCommand.args);
if (argMatch && argMatch['groups']) {
const jumpCommand = argMatch['groups'];
if (!jumpCommand.text || !jumpCommand.seconds)
throw new UnreachableCode();
line = line.replace(jumpCommand.text, '').trim();
sync.jump = parseFloat(jumpCommand.seconds);
}
}
this.syncStarts.push(sync);
this.syncEnds.push(sync);
}
}
// If there's text left that isn't a comment then we didn't parse that text so report it.
if (line && !regexes.comment.exec(line)) {
console.log(`Unknown content '${line}' in timeline: ${originalLine}`);
this.errors.push({
lineNumber: lineNumber,
line: originalLine,
error: 'Extra text',
});
} else {
this.events.push(e);
}
}
// Validate that all timeline triggers match something.
for (const trigger of triggers ?? []) {
let found = false;
for (const event of this.events) {
if (trigger.regex && trigger.regex.test(event.name)) {
found = true;
break;
}
}
if (!found) {
const text = `No match for timeline trigger ${trigger.regex?.source ??
''} in ${trigger.id ?? ''}`;
this.errors.push({ error: text });
console.error(`*** ERROR: ${text}`);
}
}
for (const e of this.events) {
for (const matchedTextEvent of texts[e.name] ?? []) {
const type = matchedTextEvent.type;
if (type !== 'info' && type !== 'alert' && type !== 'alarm')
continue;
this.texts.push({
type: type,
time: e.time - (matchedTextEvent.secondsBefore || 0),
text: matchedTextEvent.text ?? '',
});
}
// Rather than matching triggers at run time, pre-match all the triggers
// against timeline text and insert them as text events to run.
for (const trigger of triggers ?? []) {
const m = trigger.regex?.exec(e.name);
if (!m)
continue;
// TODO: beforeSeconds should support being a function.
const autoConfig = trigger.id && this.perTriggerAutoConfig[trigger.id] || {};
const beforeSeconds = autoConfig['BeforeSeconds'] ?? trigger.beforeSeconds;
this.texts.push({
type: 'trigger',
time: e.time - (beforeSeconds || 0),
trigger: trigger,
matches: m,
});
}
for (const style of styles ?? []) {
if (!style.regex.test(e.name))
continue;
e.style = style.style;
}
}
// Sort by time, but when the time is the same, sort by file order.
// Then assign a sortKey to each event so that we can maintain that order.
this.events.sort((a, b) => {
if (a.time === b.time)
return a.id - b.id;
return a.time - b.time;
});
this.events.forEach((event, idx) => event.sortKey = idx);
this.texts.sort((a, b) => {
return a.time - b.time;
});
this.syncStarts.sort((a, b) => {
return a.start - b.start;
});
this.syncEnds.sort((a, b) => {
return a.end - b.end;
});
}
private GetReplacedText(text: string): string {
// Anything in the timeline config takes precedence over timelineReplace sections in
// the trigger file. It is also a full replacement, vs the regex-style GetReplacedHelper.
const rename = this.timelineConfig?.Rename?.[text];
if (rename !== undefined)
return rename;
const replaceLang = this.options.TimelineLanguage ?? this.options.ParserLanguage ?? 'en';
return translateText(text, replaceLang, this.replacements);
}
private GetReplacedSync(sync: string): string {
const replaceLang = this.options.ParserLanguage ?? 'en';
return translateRegex(sync, replaceLang, this.replacements);
}
public GetMissingTranslationsToIgnore(): RegExp[] {
return [
'--Reset--',
'--sync--',
'Start',
'^ ?21:',
'^(\\(\\?\\<timestamp\\>\\^\\.\\{14\\}\\)) (1B|21|23):',
'^(\\^\\.\\{14\\})? ?(1B|21|23):',
'^::\\y{AbilityCode}:$',
'^\\.\\*$',
].map((x) => Regexes.parse(x));
}
// Utility function. This could be a function on TimelineParser, but it seems weird to
// store all of the original timeline texts unnecessarily when only config/utilities need it.
public static Translate(
timeline: TimelineParser,
timelineText: string,
syncErrors?: { [lineNumber: number]: boolean },
textErrors?: { [lineNumber: number]: boolean },
): string[] {
const lineToText: { [lineNumber: number]: Event } = {};
const lineToSync: { [lineNumber: number]: Sync } = {};
for (const event of timeline.events) {
if (!event.lineNumber)
continue;
lineToText[event.lineNumber] = event;
}
for (const event of timeline.syncStarts)
lineToSync[event.lineNumber] = event;
// Combine replaced lines with errors.
const timelineLines = timelineText.split(/\n/);
const translatedLines: string[] = [];
timelineLines.forEach((timelineLine, idx) => {
const lineNumber = idx + 1;
let line = timelineLine.trim();
const lineText = lineToText[lineNumber];
if (lineText)
line = line.replace(` "${lineText.name}"`, ` "${lineText.text}"`);
const lineSync = lineToSync[lineNumber];
if (lineSync)
line = line.replace(`sync /${lineSync.origRegexStr}/`, `sync /${lineSync.regex.source}/`);
if (syncErrors?.[lineNumber])
line += ' #MISSINGSYNC';
if (textErrors?.[lineNumber])
line += ' #MISSINGTEXT';
translatedLines.push(line);
});
return translatedLines;
}
}