Index packetFields by name. Use packet names in test.js

This commit is contained in:
roblabla 2015-09-20 00:41:03 +02:00
parent 4a1b2bf052
commit 56c9f3ed9a
3 changed files with 26 additions and 34 deletions

View File

@ -24,12 +24,12 @@ function readPackets(packets, states) {
assert(fields !== undefined, 'missing fields for packet ' + name); assert(fields !== undefined, 'missing fields for packet ' + name);
assert(!packetNames[state][direction].hasOwnProperty(id), 'duplicate packet id ' + id + ' for ' + 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(!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'); assert(!packetStates[direction].hasOwnProperty(name), 'duplicate packet name ' + name + ' for ' + id + ', must be unique across all states');
packetNames[state][direction][id] = name; packetNames[state][direction][id] = name;
packetIds[state][direction][name] = id; packetIds[state][direction][name] = id;
packetFields[state][direction][id] = fields; packetFields[state][direction][name] = fields;
packetStates[direction][name] = state; packetStates[direction][name] = state;
} }
}); });

View File

@ -71,8 +71,8 @@ function createPacketBuffer(packetId, state, params, isServer) {
if(typeof packetId === 'string') packetId = packetIds[state][direction][packetId]; if(typeof packetId === 'string') packetId = packetIds[state][direction][packetId];
assert.notEqual(packetId, undefined); assert.notEqual(packetId, undefined);
var packet = get(packetId, state, !isServer);
var packetName = packetNames[state][direction][packetId]; var packetName = packetNames[state][direction][packetId];
var packet = get(packetName, state, !isServer);
assert.notEqual(packet, null); assert.notEqual(packet, null);
packet.forEach(function(fieldInfo) { packet.forEach(function(fieldInfo) {
tryCatch(() => { 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 direction = toServer ? "toServer" : "toClient";
var packetInfo = packetFields[state][direction][packetId]; var packetInfo = packetFields[state][direction][packetName];
if(!packetInfo) { if(!packetInfo) {
return null; return null;
} }
@ -123,7 +123,7 @@ function parsePacketData(buffer, state, isServer, packetsToParse = {"packet": tr
var results = {id: packetId, state: state}; 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 // 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) var shouldParse = (!packetsToParse.hasOwnProperty(name) || packetsToParse[name] <= 0)
&& (!packetsToParse.hasOwnProperty("packet") || packetsToParse["packet"] <= 0); && (!packetsToParse.hasOwnProperty("packet") || packetsToParse["packet"] <= 0);
if(shouldParse) { 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) { if(packetInfo === null) {
throw new Error("Unrecognized packetId: " + packetId + " (0x" + packetId.toString(16) + ")") throw new Error("Unrecognized packetId: " + packetId + " (0x" + packetId.toString(16) + ")")
} else { } else {
var packetName = packetNames[state][isServer ? "toServer" : "toClient"][packetId];
debug("read packetId " + state + "." + packetName + " (0x" + packetId.toString(16) + ")"); debug("read packetId " + state + "." + packetName + " (0x" + packetId.toString(16) + ")");
} }
var packetName = packetNames[state][!isServer ? 'toClient' : 'toServer'][packetId];
var i, fieldInfo, readResults; var i, fieldInfo, readResults;
for(i = 0; i < packetInfo.length; ++i) { for(i = 0; i < packetInfo.length; ++i) {
fieldInfo = packetInfo[i]; fieldInfo = packetInfo[i];

View File

@ -133,42 +133,38 @@ describe("packets", function() {
}); });
client.end(); client.end();
}); });
var packetId, packetInfo, field; var packetName, packetInfo, field;
for(state in mc.packetFields) { for(state in mc.packetFields) {
if(!mc.packetFields.hasOwnProperty(state)) continue; if(!mc.packetFields.hasOwnProperty(state)) continue;
for(packetId in mc.packetFields[state].toServer) { for(packetName in mc.packetFields[state].toServer) {
if(!mc.packetFields[state].toServer.hasOwnProperty(packetId)) continue; if(!mc.packetFields[state].toServer.hasOwnProperty(packetName)) continue;
packetId = parseInt(packetId, 10); packetInfo = mc.get(packetName, state, true);
packetInfo = mc.get(packetId, state, true); it(state + ",ServerBound," + packetName,
it(state + ",ServerBound,0x" + zfill(parseInt(packetId, 10).toString(16), 2), callTestPacket(packetName, packetInfo, state, true));
callTestPacket(packetId, packetInfo, state, true));
} }
for(packetId in mc.packetFields[state].toClient) { for(packetName in mc.packetFields[state].toClient) {
if(!mc.packetFields[state].toClient.hasOwnProperty(packetId)) continue; if(!mc.packetFields[state].toClient.hasOwnProperty(packetName)) continue;
packetId = parseInt(packetId, 10); packetInfo = mc.get(packetName, state, false);
packetInfo = mc.get(packetId, state, false); it(state + ",ClientBound," + packetName,
it(state + ",ClientBound,0x" + zfill(parseInt(packetId, 10).toString(16), 2), callTestPacket(packetName, packetInfo, state, false));
callTestPacket(packetId, packetInfo, state, false));
} }
} }
function callTestPacket(packetId, packetInfo, state, toServer) { function callTestPacket(packetName, packetInfo, state, toServer) {
return function(done) { return function(done) {
client.state = state; client.state = state;
serverClient.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 // empty object uses default values
var packet = {}; var packet = {};
packetInfo.forEach(function(field) { packetInfo.forEach(function(field) {
packet[field.name] = getValue(field.type, packet); packet[field.name] = getValue(field.type, packet);
}); });
if(toServer) { if(toServer) {
serverClient.once([state, packetId], function(receivedPacket) { serverClient.once(packetName, function(receivedPacket) {
delete receivedPacket.id;
delete receivedPacket.state;
try { try {
assertPacketsMatch(packet, receivedPacket); assertPacketsMatch(packet, receivedPacket);
} catch (e) { } catch (e) {
@ -177,15 +173,13 @@ describe("packets", function() {
} }
done(); done();
}); });
client.write(packetId, packet); client.write(packetName, packet);
} else { } else {
client.once([state, packetId], function(receivedPacket) { client.once(packetName, function(receivedPacket) {
delete receivedPacket.id;
delete receivedPacket.state;
assertPacketsMatch(packet, receivedPacket); assertPacketsMatch(packet, receivedPacket);
done(); done();
}); });
serverClient.write(packetId, packet); serverClient.write(packetName, packet);
} }
} }
@ -327,7 +321,7 @@ describe("client", function() {
username: 'Player', username: 'Player',
}); });
var gotKicked = false; 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); assert.ok(packet.reason.indexOf('"Failed to verify username!"')!=-1);
gotKicked = true; gotKicked = true;
}); });