Merge pull request #233 from rom1504/play_numerical_ids

transform play numerical ids to names
This commit is contained in:
Robin Lambertz 2015-09-14 14:35:31 +02:00
commit 74e4d415fb
6 changed files with 36 additions and 38 deletions

View File

@ -45,7 +45,7 @@ function createClient(options) {
var client = new Client(false);
client.on('connect', onConnect);
if(keepAlive) client.on([states.PLAY, 0x00], onKeepAlive);
if(keepAlive) client.on('keep_alive', onKeepAlive);
client.once([states.LOGIN, 0x01], onEncryptionKeyRequest);
client.once([states.LOGIN, 0x02], onLogin);
client.once("compress", onCompressionRequest);
@ -92,7 +92,7 @@ function createClient(options) {
}
function onKeepAlive(packet) {
client.write(0x00, {
client.write('keep_alive', {
keepAliveId: packet.keepAliveId
});
}

View File

@ -64,7 +64,7 @@ function createServer(options) {
client.end('KeepAliveTimeout');
return;
}
client.write(0x00, {
client.write('keep_alive', {
keepAliveId: Math.floor(Math.random() * 2147483648)
});
}
@ -77,7 +77,7 @@ function createServer(options) {
keepAlive = true;
lastKeepAlive = new Date();
keepAliveTimer = setInterval(keepAliveLoop, checkTimeoutInterval);
client.on(0x00, onKeepAlive);
client.on('keep_alive', onKeepAlive);
}
function onEnd() {

View File

@ -26,7 +26,7 @@ Server.prototype.listen = function(port, host) {
client._end = client.end;
client.end = function end(endReason) {
if(client.state === states.PLAY) {
client.write(0x40, {reason: endReason});
client.write('kick_disconnect', {reason: endReason});
} else if(client.state === states.LOGIN) {
client.write(0x00, {reason: endReason});
}

View File

@ -62,22 +62,26 @@ var packetStates = packetIndexes.packetStates;
// TODO : This does NOT contain the length prefix anymore.
function createPacketBuffer(packetId, state, params, isServer) {
var length = 0;
var direction=!isServer ? 'toServer' : 'toClient';
if(typeof packetId === 'string' && typeof state !== 'string' && !params) {
// simplified two-argument usage, createPacketBuffer(name, params)
params = state;
state = packetStates[!isServer ? 'toServer' : 'toClient'][packetId];
state = packetStates[direction][packetId];
}
if(typeof packetId === 'string') packetId = packetIds[state][!isServer ? 'toServer' : 'toClient'][packetId];
if(typeof packetId === 'string') packetId = packetIds[state][direction][packetId];
assert.notEqual(packetId, undefined);
var packet = get(packetId, state, !isServer);
var packetName = packetNames[state][!isServer ? 'toServer' : 'toClient'][packetId];
var packetName = packetNames[state][direction][packetId];
assert.notEqual(packet, null);
packet.forEach(function(fieldInfo) {
tryCatch(() => {
length += proto.sizeOf(params[fieldInfo.name], fieldInfo.type, params);
}, (e) => {
e.message = "sizeOf error for " + packetName + "." + e.field + " : " + e.message;
addErrorField(e, fieldInfo.name);
e.message = "sizeOf error for "+[state,direction,packetName,e.field].join(".")+"\n"+
" in packet 0x" + packetId.toString(16)+" "+JSON.stringify(params)+"\n"
+ e.message;
throw e;
});
});

View File

@ -5,9 +5,9 @@ var mc = require("../");
states = mc.states;
var testDataWrite = [
{id: 0x00, params: {keepAliveId: 957759560}},
{id: 0x01, params: {message: '<Bob> Hello World!'}},
{id: 0x06, params: {x: 6.5, y: 65.62, stance: 67.24, z: 7.5, yaw: 0, pitch: 0, onGround: true}},
{name: 'keep_alive', params: {keepAliveId: 957759560}},
{name: 'chat', params: {message: '<Bob> Hello World!'}},
{name: 'position_look', params: {x: 6.5, y: 65.62, stance: 67.24, z: 7.5, yaw: 0, pitch: 0, onGround: true}},
// TODO: add more packets for better quality data
];
@ -18,7 +18,8 @@ console.log('Beginning write test');
start = Date.now();
for(i = 0; i < ITERATIONS; i++) {
for(j = 0; j < testDataWrite.length; j++) {
inputData.push(mc.createPacketBuffer(testDataWrite[j].id, states.PLAY, testDataWrite[j].params, false));
var id=mc.packetIds['play']['toServer'][testDataWrite[j].name];
inputData.push(mc.createPacketBuffer(id, states.PLAY, testDataWrite[j].params, false));
}
}
console.log('Finished write test in ' + (Date.now() - start) / 1000 + ' seconds');

View File

@ -115,14 +115,7 @@ var values = {
},
'long': [0, 1],
'entityMetadata': [
{key: 17, value: 0, type: 0},
{key: 0, value: 257, type: 1},
{key: 16, value: 626, type: 2},
{key: 1, value: 0.15, type: 3},
{key: 19, value: "Some string", type: 4},
//{key: 18, value: {}, type: 5}, Slot is a pain, I'll do it later
{key: 18, value: { x: 0, y: 0, z: 0 }, type: 6},
{key: 18, value: { pitch: 0.5, yaw: 0.7, roll: 12.4 }, type: 7},
{key: 17, value: 0, type: 0}
],
'objectData': {
intField: 9,
@ -408,7 +401,7 @@ describe("client", function() {
assert.strictEqual(packet.difficulty, 1);
assert.strictEqual(packet.dimension, 0);
assert.strictEqual(packet.gameMode, 0);
client.write(0x01, {
client.write('chat', {
message: "hello everyone; I have logged in."
});
});
@ -460,12 +453,12 @@ describe("client", function() {
var client = mc.createClient({
username: 'Player',
});
client.on([states.PLAY, 0x01], function(packet) {
client.write(0x01, {
client.on("login", function(packet) {
client.write("chat", {
message: "hello everyone; I have logged in."
});
});
client.on([states.PLAY, 0x02], function(packet) {
client.on("chat", function(packet) {
var message = JSON.parse(packet.message);
assert.strictEqual(message.translate, "chat.type.text");
/*assert.deepEqual(message["with"][0], {
@ -598,7 +591,7 @@ describe("mc-server", function() {
broadcast(client.username + ' left the game.', client);
if(client.username === 'player2') server.close();
});
client.write(0x01, {
client.write('login', {
entityId: client.id,
levelType: 'default',
gameMode: 1,
@ -607,7 +600,7 @@ describe("mc-server", function() {
maxPlayers: server.maxPlayers,
reducedDebugInfo: 0
});
client.on([states.PLAY, 0x01], function(packet) {
client.on('chat', function(packet) {
var message = '<' + client.username + '>' + ' ' + packet.message;
broadcast(message);
});
@ -615,32 +608,32 @@ describe("mc-server", function() {
server.on('close', done);
server.on('listening', function() {
var player1 = mc.createClient({username: 'player1', host: '127.0.0.1'});
player1.on([states.PLAY, 0x01], function(packet) {
player1.on('login', function(packet) {
assert.strictEqual(packet.gameMode, 1);
assert.strictEqual(packet.levelType, 'default');
assert.strictEqual(packet.dimension, 0);
assert.strictEqual(packet.difficulty, 2);
player1.once(0x02, function(packet) {
player1.once('chat', function(packet) {
assert.strictEqual(packet.message, '{"text":"player2 joined the game."}');
player1.once(0x02, function(packet) {
player1.once('chat', function(packet) {
assert.strictEqual(packet.message, '{"text":"<player2> hi"}');
player2.once(0x02, fn);
player2.once('chat', fn);
function fn(packet) {
if(/<player2>/.test(packet.message)) {
player2.once(0x02, fn);
player2.once('chat', fn);
return;
}
assert.strictEqual(packet.message, '{"text":"<player1> hello"}');
player1.once(0x02, function(packet) {
player1.once('chat', function(packet) {
assert.strictEqual(packet.message, '{"text":"player2 left the game."}');
player1.end();
});
player2.end();
}
player1.write(0x01, {message: "hello"});
player1.write('chat', {message: "hello"});
});
player2.write(0x01, {message: "hi"});
player2.write('chat', {message: "hi"});
});
var player2 = mc.createClient({username: 'player2', host: '127.0.0.1'});
});
@ -652,7 +645,7 @@ describe("mc-server", function() {
if(!server.clients.hasOwnProperty(clientId)) continue;
client = server.clients[clientId];
if(client !== exclude) client.write(0x02, {message: JSON.stringify({text: message}), position: 0});
if(client !== exclude) client.write('chat', {message: JSON.stringify({text: message}), position: 0});
}
}
});
@ -692,7 +685,7 @@ describe("mc-server", function() {
assert.strictEqual(reason, 'ServerShutdown');
resolve();
});
client.write(0x01, {
client.write('login', {
entityId: client.id,
levelType: 'default',
gameMode: 1,
@ -707,7 +700,7 @@ describe("mc-server", function() {
});
server.on('listening', function() {
var client = mc.createClient({username: 'lalalal', host: '127.0.0.1'});
client.on([states.PLAY, 0x01], function() {
client.on('login', function() {
server.close();
});
});