diff --git a/src/packets.js b/src/packets.js index 49d6a72..8cc3d70 100644 --- a/src/packets.js +++ b/src/packets.js @@ -24,12 +24,12 @@ function readPackets(packets, states) { assert(fields !== undefined, 'missing fields for packet ' + name); assert(!packetNames[state][direction].hasOwnProperty(id), 'duplicate packet id ' + id + ' for ' + name); assert(!packetIds[state][direction].hasOwnProperty(name), 'duplicate packet name ' + name + ' for ' + id); - assert(!packetFields[state][direction].hasOwnProperty(id), 'duplicate packet id ' + id + ' for ' + name); + assert(!packetFields[state][direction].hasOwnProperty(name), 'duplicate packet id ' + id + ' for ' + name); assert(!packetStates[direction].hasOwnProperty(name), 'duplicate packet name ' + name + ' for ' + id + ', must be unique across all states'); packetNames[state][direction][id] = name; packetIds[state][direction][name] = id; - packetFields[state][direction][id] = fields; + packetFields[state][direction][name] = fields; packetStates[direction][name] = state; } }); diff --git a/src/transforms/serializer.js b/src/transforms/serializer.js index c463a6b..eb328a9 100644 --- a/src/transforms/serializer.js +++ b/src/transforms/serializer.js @@ -71,8 +71,8 @@ function createPacketBuffer(packetId, state, params, isServer) { if(typeof packetId === 'string') packetId = packetIds[state][direction][packetId]; assert.notEqual(packetId, undefined); - var packet = get(packetId, state, !isServer); var packetName = packetNames[state][direction][packetId]; + var packet = get(packetName, state, !isServer); assert.notEqual(packet, null); packet.forEach(function(fieldInfo) { tryCatch(() => { @@ -106,9 +106,9 @@ function createPacketBuffer(packetId, state, params, isServer) { } -function get(packetId, state, toServer) { +function get(packetName, state, toServer) { var direction = toServer ? "toServer" : "toClient"; - var packetInfo = packetFields[state][direction][packetId]; + var packetInfo = packetFields[state][direction][packetName]; if(!packetInfo) { return null; } @@ -123,7 +123,7 @@ function parsePacketData(buffer, state, isServer, packetsToParse = {"packet": tr var results = {id: packetId, state: state}; // Only parse the packet if there is a need for it, AKA if there is a listener attached to it - var name = packetNames[state][isServer ? "toServer" : "toClient"][packetId]; + var packetName = packetNames[state][isServer ? "toServer" : "toClient"][packetId]; var shouldParse = (!packetsToParse.hasOwnProperty(name) || packetsToParse[name] <= 0) && (!packetsToParse.hasOwnProperty("packet") || packetsToParse["packet"] <= 0); if(shouldParse) { @@ -133,15 +133,13 @@ function parsePacketData(buffer, state, isServer, packetsToParse = {"packet": tr }; } - var packetInfo = get(packetId, state, isServer); + var packetInfo = get(packetName, state, isServer); if(packetInfo === null) { throw new Error("Unrecognized packetId: " + packetId + " (0x" + packetId.toString(16) + ")") } else { - var packetName = packetNames[state][isServer ? "toServer" : "toClient"][packetId]; debug("read packetId " + state + "." + packetName + " (0x" + packetId.toString(16) + ")"); } - var packetName = packetNames[state][!isServer ? 'toClient' : 'toServer'][packetId]; var i, fieldInfo, readResults; for(i = 0; i < packetInfo.length; ++i) { fieldInfo = packetInfo[i]; diff --git a/test/test.js b/test/test.js index 3eff6d8..9c7a9ae 100644 --- a/test/test.js +++ b/test/test.js @@ -133,42 +133,38 @@ describe("packets", function() { }); client.end(); }); - var packetId, packetInfo, field; + var packetName, packetInfo, field; for(state in mc.packetFields) { if(!mc.packetFields.hasOwnProperty(state)) continue; - for(packetId in mc.packetFields[state].toServer) { - if(!mc.packetFields[state].toServer.hasOwnProperty(packetId)) continue; - packetId = parseInt(packetId, 10); - packetInfo = mc.get(packetId, state, true); - it(state + ",ServerBound,0x" + zfill(parseInt(packetId, 10).toString(16), 2), - callTestPacket(packetId, packetInfo, state, true)); + for(packetName in mc.packetFields[state].toServer) { + if(!mc.packetFields[state].toServer.hasOwnProperty(packetName)) continue; + packetInfo = mc.get(packetName, state, true); + it(state + ",ServerBound," + packetName, + callTestPacket(packetName, packetInfo, state, true)); } - for(packetId in mc.packetFields[state].toClient) { - if(!mc.packetFields[state].toClient.hasOwnProperty(packetId)) continue; - packetId = parseInt(packetId, 10); - packetInfo = mc.get(packetId, state, false); - it(state + ",ClientBound,0x" + zfill(parseInt(packetId, 10).toString(16), 2), - callTestPacket(packetId, packetInfo, state, false)); + for(packetName in mc.packetFields[state].toClient) { + if(!mc.packetFields[state].toClient.hasOwnProperty(packetName)) continue; + packetInfo = mc.get(packetName, state, false); + it(state + ",ClientBound," + packetName, + callTestPacket(packetName, packetInfo, state, false)); } } - function callTestPacket(packetId, packetInfo, state, toServer) { + function callTestPacket(packetName, packetInfo, state, toServer) { return function(done) { client.state = state; serverClient.state = state; - testPacket(packetId, packetInfo, state, toServer, done); + testPacket(packetName, packetInfo, state, toServer, done); }; } - function testPacket(packetId, packetInfo, state, toServer, done) { + function testPacket(packetName, packetInfo, state, toServer, done) { // empty object uses default values var packet = {}; packetInfo.forEach(function(field) { packet[field.name] = getValue(field.type, packet); }); if(toServer) { - serverClient.once([state, packetId], function(receivedPacket) { - delete receivedPacket.id; - delete receivedPacket.state; + serverClient.once(packetName, function(receivedPacket) { try { assertPacketsMatch(packet, receivedPacket); } catch (e) { @@ -177,15 +173,13 @@ describe("packets", function() { } done(); }); - client.write(packetId, packet); + client.write(packetName, packet); } else { - client.once([state, packetId], function(receivedPacket) { - delete receivedPacket.id; - delete receivedPacket.state; + client.once(packetName, function(receivedPacket) { assertPacketsMatch(packet, receivedPacket); done(); }); - serverClient.write(packetId, packet); + serverClient.write(packetName, packet); } } @@ -327,7 +321,7 @@ describe("client", function() { username: 'Player', }); var gotKicked = false; - client.on([states.LOGIN, 0x00], function(packet) { + client.on('disconnect', function(packet) { assert.ok(packet.reason.indexOf('"Failed to verify username!"')!=-1); gotKicked = true; });