Make code more DRY

This commit is contained in:
roblabla 2015-02-21 21:04:41 +00:00
parent 79cf229835
commit 863f329ac0

View file

@ -1462,16 +1462,8 @@ function newStylePacket(buffer) {
return packet;
}
function parsePacket(buffer, state, isServer, packetsToParse) {
if (state == null) state = states.PLAY;
function parsePacketData(buffer, state, isServer, packetsToParse) {
var cursor = 0;
var lengthField = readVarInt(buffer, 0);
if (!!!lengthField) return null;
var length = lengthField.value;
cursor += lengthField.size;
if (length + lengthField.size > buffer.length) return null;
var buffer = buffer.slice(0, length + cursor); // fail early if too much is read.
var packetIdField = readVarInt(buffer, cursor);
var packetId = packetIdField.value;
cursor += packetIdField.size;
@ -1483,7 +1475,6 @@ function parsePacket(buffer, state, isServer, packetsToParse) {
&& (!packetsToParse.hasOwnProperty("packet") || packetsToParse["packet"] <= 0);
if (shouldParse) {
return {
size: length + lengthField.size,
buffer: buffer,
results: results
};
@ -1493,7 +1484,6 @@ function parsePacket(buffer, state, isServer, packetsToParse) {
if (packetInfo === null) {
return {
error: new Error("Unrecognized packetId: " + packetId + " (0x" + packetId.toString(16) + ")"),
size: length + lengthField.size,
buffer: buffer,
results: results
};
@ -1527,111 +1517,49 @@ function parsePacket(buffer, state, isServer, packetsToParse) {
}
debug(results);
return {
size: length + lengthField.size,
results: results,
buffer: buffer
};
}
function parseNewStylePacket(buffer, state, isServer, packetsToParse, cb) {
function finishParsing(buffer) {
var cursor = 0;
var packetIdField = readVarInt(buffer, cursor);
var packetId = packetIdField.value;
cursor += packetIdField.size;
var results = { id: packetId };
// 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 shouldParse = (!packetsToParse.hasOwnProperty(name) || packetsToParse[name] <= 0) &&
(!packetsToParse.hasOwnProperty("packet") || packetsToParse["packet"] <= 0);
if (shouldParse) {
cb(null, {
size: length + lengthField.size,
buffer: buffer,
results: results
});
}
var packetInfo = get(packetId, state, isServer);
if (packetInfo === null) {
cb(null, {
error: new Error("Unrecognized packetId: " + packetId + " (0x" + packetId.toString(16) + ")"),
size: length + lengthField.size,
buffer: buffer,
results: results
});
} else {
var packetName = packetNames[state][isServer ? "toServer" : "toClient"][packetId];
debug("read packetId " + state + "." + packetName + " (0x" + packetId.toString(16) + ")");
}
var i, fieldInfo, readResults;
for (i = 0; i < packetInfo.length; ++i) {
fieldInfo = packetInfo[i];
readResults = read(buffer, cursor, fieldInfo, results);
/* A deserializer cannot return null anymore. Besides, read() returns
* null when the condition is not fulfilled.
if (!!!readResults) {
var error = new Error("A deserializer returned null");
error.packetId = packetId;
error.fieldInfo = fieldInfo.name;
return {
size: length + lengthField.size,
error: error,
results: results
};
}*/
if (readResults === null) continue;
if (readResults.error) {
cb(null, readResults);
}
results[fieldInfo.name] = readResults.value;
cursor += readResults.size;
}
debug(results);
cb(null, {
size: length + lengthField.size,
results: results,
buffer: buffer
});
};
function parsePacket(buffer, state, isServer, packetsToParse) {
if (state == null) state = states.PLAY;
var cursor = 0;
var lengthField = readVarInt(buffer, 0);
if (!!!lengthField) return null;
var length = lengthField.value;
console.log("Started reading compressed packet");
cursor += lengthField.size;
if (cursor + length > buffer.length) { console.log("Stopped reading"); return null };
console.log("oldbuffer.length = " + buffer.length);
console.log("cursor + length = " + (cursor + length));
var buffer = buffer.slice(0, length + cursor); // fail early if too much is read.
if (length + lengthField.size > buffer.length) return null; // fail early
var result = parsePacketData(buffer.slice(cursor, length + cursor), state, isServer, packetsToParse);
result.size = lengthField.size + length;
return result;
}
function parseNewStylePacket(buffer, state, isServer, packetsToParse, cb) {
if (state == null) state = states.PLAY;
var cursor = 0;
var lengthField = readVarInt(buffer, 0);
if (!!!lengthField) return null;
var length = lengthField.value;
cursor += lengthField.size;
if (length >= buffer.length - cursor) { return null };
var dataLengthField = readVarInt(buffer, cursor);
cursor += dataLengthField.size;
if(dataLengthField.value != 0) {
/*console.log("Cursor : " + cursor);
console.log("DataLength : " + dataLengthField.value);
console.log(buffer.slice(cursor, cursor + dataLengthField.value).toString('hex'));
console.log("Buffer : " + buffer.toString('hex'));*/
debug("Cursor : " + cursor);
zlib.inflate(buffer.slice(cursor, cursor + length - dataLengthField.value), function(err, buffer) {
var bufToDecompress = buffer.slice(cursor);
zlib.inflate(bufToDecompress, function(err, newbuf) {
if (err) {
console.log(err);
cb(err);
} else {
console.log("buffer.length = " + buffer.length);
console.log("dataLength = " + dataLengthField.value);
finishParsing(buffer);
var result = parsePacketData(newbuf, state, isServer, packetsToParse);
result.size = lengthField.size + length;
cb(null, parsePacketData(newbuf, state, isServer, packetsToParse));
}
});
} else {
finishParsing(buffer.slice(cursor, cursor + length));
cb(null, parsePacketData(buffer.slice(cursor, cursor + length), state, isServer, packetsToParse));
}
}
module.exports = {