Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 64 additions & 39 deletions example-complex.js
Original file line number Diff line number Diff line change
@@ -1,58 +1,83 @@
#!/usr/bin/env node
"use strict";

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);
class Parser extends Dissolve {
*parser() {
var data;
while (true) {
data = {
pid: yield this.uint8()
};

switch (data.pid) {
case 0x00:
data.token = yield this.uint32be();
break;

case 0x01:
data.eid = yield this.uint32be();
data.level_type = yield *this.mcstring16();
data.game_mode = yield this.uint8();
data.dimension = yield this.uint8();
data.difficulty = yield this.uint8();
data.junk = yield this.uint8();
data.max_players = yield this.uint8();
break;

Parser.prototype.mcstring16 = function string16(name) {
var len = [name, "len"].join("_");
case 0x02:
data.protocol_version = yield this.uint8();
data.username = yield *this.mcstring16();
data.server_host = yield *this.mcstring16();
data.server_port = yield this.uint32be();
break;

return this.uint16be(len).tap(function() {
this.buffer(name, this.vars[len] * 2).tap(function() {
delete this.vars[len];
case 0x03:
data.message = yield *this.mcstring16();
break;

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;
case 0x04:
data.time = yield this.uint64be();
break;

case 0xfe:
break;
}

this.vars[name] = this.vars[name].toString("ucs2");
});
});
};
this.push(data);
}
}

*mcstring16() {
var length = yield this.uint16be();

var data = yield this.buffer(length * 2);

for (var i = 0; i < (data.length / 2); ++i) {
var t = data[i*2];
data[i*2] = data[i*2+1];
data[i*2+1] = t;
}

return data;
}
}

var parser = new Parser();

parser.on("readable", function() {
var e;
while (e = parser.read()) {
console.log(e);
//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]));
for (var i = 0; i < 50000; i++) {
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]));
}
35 changes: 19 additions & 16 deletions example-loop.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,31 @@

var Dissolve = require("./index");

var parser = Dissolve().tap(function() {
var data_i = 0;
var parser = Dissolve()
parser.parser = function*() {
var data_count;
var data, elements, i, j;

this.uint8("data_count").loop("data", function(end) {
if (data_i++ === this.vars.data_count) {
return end(true);
}
while (true) {
data = [];
data_count = yield this.uint8();

var elements_i = 0;
for (i = 0; i < data_count; i++) {
elements = []
element_count = yield this.uint8();

this.uint8("element_count").loop("elements", function(end) {
if (elements_i++ === this.vars.element_count) {
return end(true);
for (j = 0; j < element_count; j++) {
elements.push({ element: yield this.uint8() });
}

this.uint8("element");
data.push({ elements: elements });
}

this.push({
data: data
});
}).tap(function() {
this.push(this.vars);
this.vars = Object.create(null);
});
});
}
};

parser.on("readable", function() {
var e;
Expand Down
44 changes: 30 additions & 14 deletions example.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,38 @@

var Dissolve = require("./index");

var parser = Dissolve().loop(function(end) {
this.uint8("id").tap("payload", function() {
this.tap("asdf", function() {
if (this.vars.id === 0x01) {
this.uint16be("a").uint16be("b");
} else if (this.vars.id === 0x02) {
this.uint32be("x").uint32be("y");
} else if (this.vars.id === 0x03) {
this.floatbe("l").doublebe("m");
var parser = new Dissolve();
parser.parser = function*() {
var id, payload;

while (true) {
id = yield this.uint8();

if (id === 0x01) {
payload = {
"a": yield this.uint16be(),
"b": yield this.uint16be()
};
} else if (id === 0x02) {
payload = {
"x": yield this.uint32be(),
"y": yield this.uint32be()
};
} else if (id === 0x03) {
payload = {
"l": yield this.floatbe(),
"m": yield this.doublebe()
};
}

this.push({
id: id,
payload: {
asdf: payload
}
});
}).tap(function() {
this.push(this.vars);
this.vars = Object.create(null);
});
});
}
};

parser.on("readable", function() {
var e;
Expand Down
Loading