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

View file

@ -110,7 +110,9 @@ var values = {
velocityY: 2, velocityY: 2,
velocityZ: 3, 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() { describe("packets", function() {
@ -205,7 +207,7 @@ describe("packets", function() {
}); });
describe("client", function() { describe("client", function() {
this.timeout(40000); this.timeout(4000000);
var mcServer; var mcServer;
function startServer(propOverrides, done) { function startServer(propOverrides, done) {
@ -238,7 +240,7 @@ describe("client", function() {
batch.end(function(err) { batch.end(function(err) {
if (err) return done(err); if (err) return done(err);
//console.log(MC_SERVER_JAR); //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', stdio: 'pipe',
cwd: MC_SERVER_PATH, cwd: MC_SERVER_PATH,
}); });
@ -320,16 +322,16 @@ describe("client", function() {
mcServer.stdin.write("say hello\n"); mcServer.stdin.write("say hello\n");
}); });
var chatCount = 0; var chatCount = 0;
client.on([states.PLAY, 0x01], function(packet) { client.on('login', function(packet) {
assert.strictEqual(packet.levelType, 'default'); assert.strictEqual(packet.levelType, 'default');
assert.strictEqual(packet.difficulty, 1); assert.strictEqual(packet.difficulty, 1);
assert.strictEqual(packet.dimension, 0); assert.strictEqual(packet.dimension, 0);
assert.strictEqual(packet.gameMode, 0); assert.strictEqual(packet.gameMode, 0);
client.write(0x01, { client.write('chat', {
message: "hello everyone; I have logged in." message: "hello everyone; I have logged in."
}); });
}); });
client.on([states.PLAY, 0x02], function(packet) { client.on('chat', function(packet) {
chatCount += 1; chatCount += 1;
assert.ok(chatCount <= 2); assert.ok(chatCount <= 2);
var message = JSON.parse(packet.message); var message = JSON.parse(packet.message);
@ -367,7 +369,7 @@ describe("client", function() {
mcServer.stdin.write("say hello\n"); mcServer.stdin.write("say hello\n");
}); });
var chatCount = 0; var chatCount = 0;
client.on([states.PLAY, 0x01], function(packet) { client.on('login', function(packet) {
assert.strictEqual(packet.levelType, 'default'); assert.strictEqual(packet.levelType, 'default');
assert.strictEqual(packet.difficulty, 1); assert.strictEqual(packet.difficulty, 1);
assert.strictEqual(packet.dimension, 0); assert.strictEqual(packet.dimension, 0);
@ -376,7 +378,7 @@ describe("client", function() {
message: "hello everyone; I have logged in." message: "hello everyone; I have logged in."
}); });
}); });
client.on([states.PLAY, 0x02], function(packet) { client.on('chat', function(packet) {
chatCount += 1; chatCount += 1;
assert.ok(chatCount <= 2); assert.ok(chatCount <= 2);
var message = JSON.parse(packet.message); var message = JSON.parse(packet.message);