Fix tests, fix lots of bugs in protocol.js

This commit is contained in:
roblabla 2015-02-20 01:05:15 +00:00
parent 42de90c758
commit 66a6584a6a
2 changed files with 32 additions and 26 deletions

View file

@ -477,7 +477,7 @@ var packets = {
{ name: "action", type: "varint" },
{ name: "length", type: "count", typeArgs: { type: "varint", countFor: "data" }},
{ name: "data", type: "array", typeArgs: { count: "length", type: "container", typeArgs: { fields: [
{ name: "UUID", type: "uuid" },
{ name: "UUID", type: "UUID" },
{ name: "name", type: "string", condition: function(field_values) {
return field_values["action"] === 0;
}},
@ -559,8 +559,7 @@ var packets = {
]},
custom_payload: {id: 0x3f, fields: [
{ name: "channel", type: "string" },
{ name: "dataCount", type: 'count', typeArgs: { type: "short", countFor: "data" } },
{ name: "data", type: "buffer", typeArgs: { count: "dataCount" } }
{ name: "data", type: "restBuffer" }
]},
kick_disconnect: {id: 0x40, fields: [
{ name: "reason", type: "string" }
@ -606,7 +605,7 @@ var packets = {
{ name: "new_radius", type: "double", condition: function(field_values) {
return field_values['action'] == 1 || field_values['action'] == 3;
} },
{ name: "speed", type: "varlong", condition: function(field_values) {
{ name: "speed", type: "varint", condition: function(field_values) {
return field_values['action'] == 1 || field_values['action'] == 3;
} },
{ name: "portalBoundary", type: "varint", condition: function(field_values) {
@ -755,7 +754,7 @@ var packets = {
]},
tab_complete: {id: 0x14, fields: [
{ name: "text", type: "string" },
{ name: "hasPosition", type: "boolean" },
{ name: "hasPosition", type: "bool" },
{ name: "block", type: "position", condition: function(field_values) {
return field_values['hasPosition'];
} }
@ -772,7 +771,7 @@ var packets = {
]},
custom_payload: {id: 0x17, fields: [
{ name: "channel", type: "string" }, /* TODO: wiki.vg sats no dataLength is needed? */
{ name: "data", type: "buffer"}
{ name: "data", type: "restBuffer"}
]},
spectate: { id: 0x18, fields: [
{ name: "target", type: "UUID"}
@ -838,7 +837,7 @@ var types = {
'container': [readContainer, writeContainer, sizeOfContainer],
'array': [readArray, writeArray, sizeOfArray],
'buffer': [readBuffer, writeBuffer, sizeOfBuffer],
'restBuffer': [readRestBuffer, writeRestBuffer, sizeOfRestBuffer],
'restBuffer': [readRestBuffer, writeBuffer, sizeOfBuffer],
'count': [readCount, writeCount, sizeOfCount],
// TODO : remove type-specific, replace with generic containers and arrays.
'position': [readPosition, writePosition, 8],
@ -1294,9 +1293,6 @@ function readRestBuffer(buffer, offset, typeArgs, rootNode) {
};
}
var writeRestBuffer = writeBuffer;
var sizeOfRestBuffer = sizeOfBuffer;
function readArray(buffer, offset, typeArgs, rootNode) {
var results = {
value: [],
@ -1438,7 +1434,7 @@ function createPacketBuffer(packetId, state, params, isServer) {
function compressPacketBuffer(buffer, callback) {
var dataLength = buffer.size;
zlib.deflateRaw(buffer, function(compressedBuffer) {
zlib.deflate(buffer, function(compressedBuffer) {
var packetLength = sizeOfVarInt(dataLength) + compressedBuffer.length;
var size = sizeOfVarInt(packetLength) + packetLength;
var packetBuffer = new Buffer(size);
@ -1540,6 +1536,7 @@ function parsePacket(buffer, state, isServer, packetsToParse) {
function parseNewStylePacket(buffer, state, isServer, packetsToParse, cb) {
function finishParsing(buffer) {
var cursor = 0;
var packetIdField = readVarInt(buffer, cursor);
var packetId = packetIdField.value;
cursor += packetIdField.size;
@ -1606,9 +1603,11 @@ function parseNewStylePacket(buffer, state, isServer, packetsToParse, cb) {
var lengthField = readVarInt(buffer, 0);
if (!!!lengthField) return null;
var length = lengthField.value;
//console.log("Started reading compressed packet");
console.log("Started reading compressed packet");
cursor += lengthField.size;
if (length + lengthField.size > buffer.length) { console.log("Stopped reading"); return null };
if (cursor + length > buffer.length) { console.log("Stopped reading"); return null };
console.log("oldbuffer.length = " + buffer.length);
console.log("cursor + length = " + (cursor + length));
var buffer = buffer.slice(0, length + cursor); // fail early if too much is read.
var dataLengthField = readVarInt(buffer, cursor);
@ -1619,13 +1618,18 @@ function parseNewStylePacket(buffer, state, isServer, packetsToParse, cb) {
console.log(buffer.slice(cursor, cursor + dataLengthField.value).toString('hex'));
console.log("Buffer : " + buffer.toString('hex'));*/
debug("Cursor : " + cursor);
zlib.inflateRaw(buffer.slice(cursor, cursor + dataLengthField.value), function(err, buffer) {
if (err) console.log(err);
else console.log("Got a packet v2");
finishParsing(buffer);
zlib.inflate(buffer.slice(cursor, cursor + length - dataLengthField.value), function(err, buffer) {
if (err) {
console.log(err);
cb(err);
} else {
console.log("buffer.length = " + buffer.length);
console.log("dataLength = " + dataLengthField.value);
finishParsing(buffer);
}
});
} else {
finishParsing(buffer);
finishParsing(buffer.slice(cursor, cursor + length));
}
}

View file

@ -110,7 +110,9 @@ var values = {
velocityY: 2,
velocityZ: 3,
},
'UUID': [42, 42, 42, 42]
'UUID': [42, 42, 42, 42],
'position': { x: 12, y: 332, z: 4382821 },
'restBuffer': new Buffer(0)
};
describe("packets", function() {
@ -205,7 +207,7 @@ describe("packets", function() {
});
describe("client", function() {
this.timeout(40000);
this.timeout(4000000);
var mcServer;
function startServer(propOverrides, done) {
@ -238,7 +240,7 @@ describe("client", function() {
batch.end(function(err) {
if (err) return done(err);
//console.log(MC_SERVER_JAR);
mcServer = spawn('java', [ '-jar', MC_SERVER_JAR, 'nogui'], {
mcServer = spawn('java', [ '-Dlog4j.configurationFile=server/server_debug.xml', '-jar', MC_SERVER_JAR, 'nogui'], {
stdio: 'pipe',
cwd: MC_SERVER_PATH,
});
@ -320,16 +322,16 @@ describe("client", function() {
mcServer.stdin.write("say hello\n");
});
var chatCount = 0;
client.on([states.PLAY, 0x01], function(packet) {
client.on('login', function(packet) {
assert.strictEqual(packet.levelType, 'default');
assert.strictEqual(packet.difficulty, 1);
assert.strictEqual(packet.dimension, 0);
assert.strictEqual(packet.gameMode, 0);
client.write(0x01, {
client.write('chat', {
message: "hello everyone; I have logged in."
});
});
client.on([states.PLAY, 0x02], function(packet) {
client.on('chat', function(packet) {
chatCount += 1;
assert.ok(chatCount <= 2);
var message = JSON.parse(packet.message);
@ -367,7 +369,7 @@ describe("client", function() {
mcServer.stdin.write("say hello\n");
});
var chatCount = 0;
client.on([states.PLAY, 0x01], function(packet) {
client.on('login', function(packet) {
assert.strictEqual(packet.levelType, 'default');
assert.strictEqual(packet.difficulty, 1);
assert.strictEqual(packet.dimension, 0);
@ -376,7 +378,7 @@ describe("client", function() {
message: "hello everyone; I have logged in."
});
});
client.on([states.PLAY, 0x02], function(packet) {
client.on('chat', function(packet) {
chatCount += 1;
assert.ok(chatCount <= 2);
var message = JSON.parse(packet.message);