add debug in plugin channels, fix #433

This commit is contained in:
Romain Beaumont 2017-07-13 14:27:44 +02:00
parent 216b4d2da3
commit d9111d982f
No known key found for this signature in database
GPG Key ID: DB60E388B3BCF286

View File

@ -1,10 +1,11 @@
var ProtoDef = require('protodef').ProtoDef; const ProtoDef = require('protodef').ProtoDef;
var minecraft = require('../datatypes/minecraft'); const minecraft = require('../datatypes/minecraft');
const debug = require('debug')('minecraft-protocol');
module.exports = function(client, options) { module.exports = function(client, options) {
var mcdata = require('minecraft-data')(options.version || require("../version").defaultVersion); const mcdata = require('minecraft-data')(options.version || require("../version").defaultVersion);
var channels = []; const channels = [];
var proto = new ProtoDef(); const proto = new ProtoDef();
proto.addTypes(mcdata.protocol.types); proto.addTypes(mcdata.protocol.types);
proto.addTypes(minecraft); proto.addTypes(minecraft);
proto.addType('registerarr',[readDumbArr, writeDumbArr, sizeOfDumbArr]); proto.addType('registerarr',[readDumbArr, writeDumbArr, sizeOfDumbArr]);
@ -31,7 +32,7 @@ module.exports = function(client, options) {
if(custom) { if(custom) {
client.writeChannel("UNREGISTER",channel); client.writeChannel("UNREGISTER",channel);
} }
var index = channels.find(function(name) { const index = channels.find(function(name) {
return channel === name; return channel === name;
}); });
if (index) { if (index) {
@ -43,17 +44,19 @@ module.exports = function(client, options) {
} }
function onCustomPayload(packet) { function onCustomPayload(packet) {
var channel = channels.find(function(channel) { const channel = channels.find(function(channel) {
return channel === packet.channel; return channel === packet.channel;
}); });
if (channel) { if (channel) {
if (proto.types[channel]) if (proto.types[channel])
packet.data = proto.parsePacketBuffer(channel, packet.data).data; packet.data = proto.parsePacketBuffer(channel, packet.data).data;
debug("read custom payload "+channel+" "+packet.data);
client.emit(channel, packet.data); client.emit(channel, packet.data);
} }
} }
function writeChannel(channel,params) { function writeChannel(channel,params) {
debug("write custom payload "+channel+" "+params);
client.write("custom_payload",{ client.write("custom_payload",{
channel:channel, channel:channel,
data:proto.createPacketBuffer(channel,params) data:proto.createPacketBuffer(channel,params)
@ -61,13 +64,13 @@ module.exports = function(client, options) {
} }
function readDumbArr(buf, offset) { function readDumbArr(buf, offset) {
var ret = { const ret = {
value: [], value: [],
size: 0 size: 0
}; };
let results; let results;
while (offset < buf.length) { while (offset < buf.length) {
if (buf.indexOf(0x0, offset) == -1) if (buf.indexOf(0x0, offset) === -1)
results = this.read(buf, offset, "restBuffer", {}); results = this.read(buf, offset, "restBuffer", {});
else else
results = this.read(buf, offset, "cstring", {}); results = this.read(buf, offset, "cstring", {});