-
Notifications
You must be signed in to change notification settings - Fork 7
/
debug.ts
87 lines (74 loc) · 2.53 KB
/
debug.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
import {execFileSync} from 'child_process';
import * as fs from 'fs';
import * as path from 'path';
import {Generation, Generations} from '@pkmn/data';
import * as data from '../pkg/data';
import * as gen1 from '../pkg/gen1';
import * as display from './display';
const ROOT = path.resolve(__dirname, '..', '..');
const ZIG = path.dirname(JSON.parse(execFileSync('zig', ['env'], {encoding: 'utf8'})).lib_dir);
export function error(err: string) {
return (err
.replaceAll(ROOT + path.sep, '')
.replaceAll(ZIG, '$ZIG')
.split('\n').slice(0, -3).join('\n'));
}
export function render(
gens: Generation | Generations,
buffer: Buffer,
err?: string,
seed?: bigint
) {
if (!buffer.length) throw new Error('Invalid input');
// Peek at the start of the data buffer just to figure out whether showdown is enabled, what
// generation it is, and the intial state of the Battle
const view = data.Data.view(buffer);
let offset = 0;
const showdown = !!view.getUint8(offset++);
const num = view.getUint8(offset++);
const gen = 'get' in gens ? gens.get(num) : gens;
if (gen.num !== num) {
throw new Error(`Require generation ${num} but was passed generation ${gen.num}`);
}
offset += 6;
const lookup = data.Lookup.get(gen);
const size = data.LAYOUT[gen.num - 1].sizes.Battle;
const deserialize = (buf: Buffer) => {
switch (gen.num) {
case 1: return new gen1.Battle(lookup, data.Data.view(buf), {inert: true, showdown});
default: throw new Error(`Unsupported gen: ${gen.num}`);
}
};
const battle = deserialize(buffer.subarray(offset, offset += size));
return display.render(path.join(ROOT, 'build', 'tools', 'display', 'debug.jsx'), {
gen: display.prune(gen, battle),
buf: buffer.toString('base64'),
error: err && error(err),
seed: seed?.toString(),
}, {styles: [path.join(ROOT, 'src', 'tools', 'display', 'debug.css')]});
}
export async function run(gens: Generations) {
let input;
if (process.argv.length > 2) {
if (process.argv.length > 3) {
console.error('Invalid input');
process.exit(1);
}
input = fs.readFileSync(process.argv[2]);
} else {
let length = 0;
const result: Uint8Array[] = [];
for await (const chunk of process.stdin) {
result.push(chunk);
length += chunk.length;
}
input = Buffer.concat(result, length);
}
process.stdout.write(render(gens, input));
}
if (require.main === module) {
run(new Generations(require('@pkmn/sim').Dex)).catch(err => {
console.error(err.message);
process.exit(1);
});
}