mirror of
https://github.com/PrismarineJS/node-minecraft-protocol.git
synced 2024-11-14 19:04:59 -05:00
Index packetFields by name. Use packet names in test.js
This commit is contained in:
parent
4a1b2bf052
commit
56c9f3ed9a
3 changed files with 26 additions and 34 deletions
|
@ -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;
|
||||
}
|
||||
});
|
||||
|
|
|
@ -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];
|
||||
|
|
44
test/test.js
44
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;
|
||||
});
|
||||
|
|
Loading…
Reference in a new issue