mirror of
https://github.com/PrismarineJS/node-minecraft-protocol.git
synced 2024-11-29 18:55:40 -05:00
Make code more DRY
This commit is contained in:
parent
79cf229835
commit
863f329ac0
1 changed files with 21 additions and 93 deletions
114
lib/protocol.js
114
lib/protocol.js
|
@ -1462,16 +1462,8 @@ function newStylePacket(buffer) {
|
||||||
return packet;
|
return packet;
|
||||||
}
|
}
|
||||||
|
|
||||||
function parsePacket(buffer, state, isServer, packetsToParse) {
|
function parsePacketData(buffer, state, isServer, packetsToParse) {
|
||||||
if (state == null) state = states.PLAY;
|
|
||||||
var cursor = 0;
|
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 packetIdField = readVarInt(buffer, cursor);
|
||||||
var packetId = packetIdField.value;
|
var packetId = packetIdField.value;
|
||||||
cursor += packetIdField.size;
|
cursor += packetIdField.size;
|
||||||
|
@ -1483,7 +1475,6 @@ function parsePacket(buffer, state, isServer, packetsToParse) {
|
||||||
&& (!packetsToParse.hasOwnProperty("packet") || packetsToParse["packet"] <= 0);
|
&& (!packetsToParse.hasOwnProperty("packet") || packetsToParse["packet"] <= 0);
|
||||||
if (shouldParse) {
|
if (shouldParse) {
|
||||||
return {
|
return {
|
||||||
size: length + lengthField.size,
|
|
||||||
buffer: buffer,
|
buffer: buffer,
|
||||||
results: results
|
results: results
|
||||||
};
|
};
|
||||||
|
@ -1493,7 +1484,6 @@ function parsePacket(buffer, state, isServer, packetsToParse) {
|
||||||
if (packetInfo === null) {
|
if (packetInfo === null) {
|
||||||
return {
|
return {
|
||||||
error: new Error("Unrecognized packetId: " + packetId + " (0x" + packetId.toString(16) + ")"),
|
error: new Error("Unrecognized packetId: " + packetId + " (0x" + packetId.toString(16) + ")"),
|
||||||
size: length + lengthField.size,
|
|
||||||
buffer: buffer,
|
buffer: buffer,
|
||||||
results: results
|
results: results
|
||||||
};
|
};
|
||||||
|
@ -1527,111 +1517,49 @@ function parsePacket(buffer, state, isServer, packetsToParse) {
|
||||||
}
|
}
|
||||||
debug(results);
|
debug(results);
|
||||||
return {
|
return {
|
||||||
size: length + lengthField.size,
|
|
||||||
results: results,
|
results: results,
|
||||||
buffer: buffer
|
buffer: buffer
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
function parseNewStylePacket(buffer, state, isServer, packetsToParse, cb) {
|
function parsePacket(buffer, state, isServer, packetsToParse) {
|
||||||
|
|
||||||
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
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
if (state == null) state = states.PLAY;
|
if (state == null) state = states.PLAY;
|
||||||
var cursor = 0;
|
var cursor = 0;
|
||||||
var lengthField = readVarInt(buffer, 0);
|
var lengthField = readVarInt(buffer, 0);
|
||||||
if (!!!lengthField) return null;
|
if (!!!lengthField) return null;
|
||||||
var length = lengthField.value;
|
var length = lengthField.value;
|
||||||
console.log("Started reading compressed packet");
|
|
||||||
cursor += lengthField.size;
|
cursor += lengthField.size;
|
||||||
if (cursor + length > buffer.length) { console.log("Stopped reading"); return null };
|
if (length + lengthField.size > buffer.length) return null; // fail early
|
||||||
console.log("oldbuffer.length = " + buffer.length);
|
var result = parsePacketData(buffer.slice(cursor, length + cursor), state, isServer, packetsToParse);
|
||||||
console.log("cursor + length = " + (cursor + length));
|
result.size = lengthField.size + length;
|
||||||
var buffer = buffer.slice(0, length + cursor); // fail early if too much is read.
|
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);
|
var dataLengthField = readVarInt(buffer, cursor);
|
||||||
cursor += dataLengthField.size;
|
cursor += dataLengthField.size;
|
||||||
if(dataLengthField.value != 0) {
|
if(dataLengthField.value != 0) {
|
||||||
/*console.log("Cursor : " + cursor);
|
var bufToDecompress = buffer.slice(cursor);
|
||||||
console.log("DataLength : " + dataLengthField.value);
|
zlib.inflate(bufToDecompress, function(err, newbuf) {
|
||||||
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) {
|
|
||||||
if (err) {
|
if (err) {
|
||||||
console.log(err);
|
console.log(err);
|
||||||
cb(err);
|
cb(err);
|
||||||
} else {
|
} else {
|
||||||
console.log("buffer.length = " + buffer.length);
|
var result = parsePacketData(newbuf, state, isServer, packetsToParse);
|
||||||
console.log("dataLength = " + dataLengthField.value);
|
result.size = lengthField.size + length;
|
||||||
finishParsing(buffer);
|
cb(null, parsePacketData(newbuf, state, isServer, packetsToParse));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
finishParsing(buffer.slice(cursor, cursor + length));
|
cb(null, parsePacketData(buffer.slice(cursor, cursor + length), state, isServer, packetsToParse));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
|
|
Loading…
Reference in a new issue