From c45c4d3cf4e9ba35147f5fdda8ca7b79483eafeb Mon Sep 17 00:00:00 2001 From: roblabla Date: Tue, 3 Mar 2015 02:00:34 +0000 Subject: [PATCH] more protocol.js fixes --- lib/protocol.js | 57 +++++++++++++++++++++++++++++-------------------- 1 file changed, 34 insertions(+), 23 deletions(-) diff --git a/lib/protocol.js b/lib/protocol.js index f7c9d8c..b0e2e28 100644 --- a/lib/protocol.js +++ b/lib/protocol.js @@ -306,9 +306,8 @@ var packets = { multi_block_change: {id: 0x22, fields: [ { name: "chunkX", type: "int" }, { name: "chunkZ", type: "int" }, - { name: "recordCount", type: "varint" }, - /* TODO: Is dataLength needed? */ - { name: "records", type: "array", typeArgs: { count: "recordsCount", type: "container", typeArgs: { fields: [ + { name: "recordCount", type: "count", typeArgs: { type: "varint", countFor: "records" } }, + { name: "records", type: "array", typeArgs: { count: "recordCount", type: "container", typeArgs: { fields: [ { name: "horizontalPos", type: "ubyte" }, { name: "y", type: "ubyte" }, { name: "blockId", type: "varint" } @@ -883,6 +882,11 @@ var entityMetadataTypes = { { name: 'x', type: 'int' }, { name: 'y', type: 'int' }, { name: 'z', type: 'int' } + ]}}, + 7: { type: 'container', typeArgs: { fields: [ + { name: 'pitch', type: 'float' }, + { name: 'yaw', type: 'float' }, + { name: 'roll', type: 'float' } ]}} }; @@ -942,7 +946,7 @@ function readEntityMetadata(buffer, offset) { type = item >> 5; dataType = entityMetadataTypes[type]; typeName = dataType.type; - debug("Reading entity metadata type " + dataType + " (" + ( typeName || "unknown" ) + ")"); + //debug("Reading entity metadata type " + dataType + " (" + ( typeName || "unknown" ) + ")"); if (!dataType) { return { error: new Error("unrecognized entity metadata type " + type) @@ -1099,36 +1103,30 @@ function readSlot(buffer, offset) { var results = readShort(buffer, offset); if (! results) return null; value.blockId = results.value; - var cursor = offset + results.size; if (value.blockId === -1) { return { value: value, - size: cursor - offset, + size: 2, }; } - var cursorEnd = cursor + 4; + var cursorEnd = offset + 6; if (cursorEnd > buffer.length) return null; - value.itemCount = buffer.readInt8(cursor); - value.itemDamage = buffer.readInt16BE(cursor + 1); - var nbtData = buffer.readInt8(cursor + 3); + value.itemCount = buffer.readInt8(offset + 2); + value.itemDamage = buffer.readInt16BE(offset + 3); + var nbtData = buffer.readInt8(offset + 5); if (nbtData == 0) { return { value: value, - size: cursor + 4 - offset + size: 6 } } - var nbtData = readNbt(buffer, offset); - + var nbtData = readNbt(buffer, offset + 5); + value.nbtData = nbtData.value; return { - value: { - id: blockId, - itemCount: itemCount, - itemDamage: itemDamage, - nbtData: nbtData, - }, - size: nbtDataEnd - offset, + value: value, + size: nbtData.size + 5 }; } @@ -1296,11 +1294,15 @@ function readContainer(buffer, offset, typeArgs, rootNode) { } function writeContainer(value, buffer, offset, typeArgs, rootNode) { + var context = value.this ? value.this : value; rootNode.this = value; for (var index in typeArgs.fields) { - if (!value.hasOwnProperty(typeArgs.fields[index].name && typeArgs.fields[index].type != "count" && !typeArgs.fields[index].condition)) + if (!context.hasOwnProperty(typeArgs.fields[index].name) && typeArgs.fields[index].type != "count" && !typeArgs.fields[index].condition) + { debug(new Error("Missing Property " + typeArgs.fields[index].name).stack); - offset = write(value[typeArgs.fields[index].name], buffer, offset, typeArgs.fields[index], rootNode); + console.log(context); + } + offset = write(context[typeArgs.fields[index].name], buffer, offset, typeArgs.fields[index], rootNode); } delete rootNode.this; return offset; @@ -1308,9 +1310,10 @@ function writeContainer(value, buffer, offset, typeArgs, rootNode) { function sizeOfContainer(value, typeArgs, rootNode) { var size = 0; + var context = value.this ? value.this : value; rootNode.this = value; for (var index in typeArgs.fields) { - size += sizeOf(value[typeArgs.fields[index].name], typeArgs.fields[index], rootNode); + size += sizeOf(context[typeArgs.fields[index].name], typeArgs.fields[index], rootNode); } delete rootNode.this; return size; @@ -1471,7 +1474,13 @@ function createPacketBuffer(packetId, state, params, isServer) { var packet = get(packetId, state, !isServer); assert.notEqual(packet, null); packet.forEach(function(fieldInfo) { + try { length += sizeOf(params[fieldInfo.name], fieldInfo, params); + } catch (e) { + console.log(`fieldInfo : ${JSON.stringify(fieldInfo)}`); + console.log(`params : ${JSON.stringify(params)}`); + throw e; + } }); length += sizeOfVarInt(packetId); var size = length;// + sizeOfVarInt(length); @@ -1569,6 +1578,8 @@ function parsePacketData(buffer, state, isServer, packetsToParse) { results[fieldInfo.name] = readResults.value; cursor += readResults.size; } + if (buffer.length > cursor) + console.log("DID NOT PARSE THE WHOLE THING!"); debug(results); return { results: results,