-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathexample-complex.js
executable file
·58 lines (48 loc) · 1.89 KB
/
example-complex.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
#!/usr/bin/env node
var Dissolve = require("./index"),
util = require("util");
function Parser() {
Dissolve.call(this);
this.loop(function(end) {
this.uint8("pid").tap(function() {
switch (this.vars.pid) {
case 0x00: this.uint32be("token"); break;
case 0x01: this.uint32be("eid").mcstring16("level_type").uint8("game_mode").uint8("dimension").uint8("difficulty").uint8("junk").uint8("max_players"); break;
case 0x02: this.uint8("protocol_version").mcstring16("username").mcstring16("server_host").uint32be("server_port"); break;
case 0x03: this.mcstring16("message"); break;
case 0x04: this.uint64be("time"); break;
case 0xfe: break;
}
}).tap(function() {
this.push(this.vars);
this.vars = {};
});
});
}
util.inherits(Parser, Dissolve);
Parser.prototype.mcstring16 = function string16(name) {
var len = [name, "len"].join("_");
return this.uint16be(len).tap(function() {
this.buffer(name, this.vars[len] * 2).tap(function() {
delete this.vars[len];
for (var i=0;i<this.vars[name].length/2;++i) {
var t = this.vars[name][i*2];
this.vars[name][i*2] = this.vars[name][i*2+1];
this.vars[name][i*2+1] = t;
}
this.vars[name] = this.vars[name].toString("ucs2");
});
});
};
var parser = new Parser();
parser.on("readable", function() {
var e;
while (e = parser.read()) {
console.log(e);
}
});
parser.write(new Buffer([0x00, 0x00, 0x00, 0x00, 0x01]));
parser.write(new Buffer([0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x02, 0x00, 0x61, 0x00, 0x62, 0x00, 0x00, 0x00, 0x00, 0x00]));
parser.write(new Buffer([0x02, 0x01, 0x00, 0x02, 0x00, 0x61, 0x00, 0x62, 0x00, 0x02, 0x00, 0x63, 0x00, 0x64, 0x00, 0x00, 0x00, 0x05]));
parser.write(new Buffer([0x03, 0x00, 0x02, 0x00, 0x65, 0x00, 0x66]));
parser.write(new Buffer([0x04, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x01]));