/
parse.js
65 lines (56 loc) 路 1.36 KB
/
parse.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
// Module import
const fs = require('fs');
const Parser = require('binary-parser').Parser;
const readComment = Parser.start().array('comment', {
type: 'uint8',
readUntil: function (a, b) {
return b.readUInt16LE() === 0 || b.length < 3;
},
formatter: function (a) {
return a.map(v => String.fromCharCode(parseInt(v, 10).toString(10))).join('');
}
}).skip(2);
const readMove = Parser.start()
.uint8('move', {
formatter: (flag) => [flag % 16 - 1, Math.ceil(flag / 16) - 1]
})
.bit1('down') // 128 Has Siblings
.bit1('right') // 64 Has a child node, into the subtree
.bit1('hz4')
.bit1('mark')
.bit1('hasComment')
.bit1('hz2')
.bit1('hz1',)
.bit1('extension')
.choice('comment', {
tag: 'hasComment',
choices: {
0: Parser.start(),
1: readComment
}
}
);
const header = Parser.start()
.endianess('little')
.uint8('open', {assert: 255})
.string('type', {
length: 6,
assert: 'RenLib'
})
.uint8('open', {assert: 255})
.int8('major', {assert: 3})
.int8('minor')
.skip(10);
p = new Parser()
.nest('header', {
type: header
})
.array('moves', {
type: readMove,
readUntil: 'eof'
});
require('fs').readFile('ss.lib', function (err, data) {
const v = p.parse(data);
console.log(JSON.stringify(v));
fs.writeFileSync('moves.json', JSON.stringify(v), 'UTF-8');
});