Introduce switch and void, remove condition

This commit is contained in:
roblabla 2015-08-25 01:10:31 +00:00
parent 8d1705410b
commit 3282cf69c7
9 changed files with 73 additions and 73 deletions

View file

@ -47,7 +47,7 @@
"readable-stream": "^1.1.0",
"superagent": "~0.10.0",
"ursa-purejs": "0.0.3",
"minecraft-data": "0.6.0"
"minecraft-data": "0.7.0"
},
"optionalDependencies": {
"ursa": "~0.8.0"

View file

@ -16,5 +16,4 @@ module.exports = {
packetStates: packetIndexes.packetStates,
types: serializer.types,
get: serializer.get,
evalCondition: utils.evalCondition,
};

View file

@ -1,25 +1,41 @@
var evalCondition = require("../utils").evalCondition;
var { getField, getFieldInfo } = require('../utils');
module.exports = {
'condition': [readCondition, writeCondition, sizeOfCondition]
'switch': [readSwitch, writeSwitch, sizeOfSwitch],
};
function readCondition(buffer, offset, typeArgs, rootNode) {
if(!evalCondition(typeArgs, rootNode))
return {value: null, size: 0};
return this.read(buffer, offset, typeArgs.type, rootNode);
function readSwitch(buffer, offset, typeArgs, rootNode) {
var compareTo = getField(typeArgs.compareTo, rootNode);
var fieldInfo;
if (typeof typeArgs.fields[compareTo] === 'undefined' && typeof typeArgs.default === "undefined")
throw new Error(compareTo + " has no associated fieldInfo in switch");
else if (typeof typeArgs.fields[compareTo] === 'undefined')
fieldInfo = getFieldInfo(typeArgs.default);
else
fieldInfo = getFieldInfo(typeArgs.fields[compareTo]);
return this.read(buffer, offset, fieldInfo, rootNode);
}
function writeCondition(value, buffer, offset, typeArgs, rootNode) {
if(!evalCondition(typeArgs, rootNode))
return offset;
return this.write(value, buffer, offset, typeArgs.type, rootNode);
function writeSwitch(value, buffer, offset, typeArgs, rootNode) {
var compareTo = getField(typeArgs.compareTo, rootNode);
var fieldInfo;
if (typeof typeArgs.fields[compareTo] === 'undefined' && typeof typeArgs.default === "undefined")
throw new Error(compareTo + " has no associated fieldInfo in switch");
else if (typeof typeArgs.fields[compareTo] === 'undefined')
fieldInfo = getFieldInfo(typeArgs.default);
else
fieldInfo = getFieldInfo(typeArgs.fields[compareTo]);
return this.write(value, buffer, offset, fieldInfo, rootNode);
}
function sizeOfCondition(value, typeArgs, rootNode) {
if(!evalCondition(typeArgs, rootNode))
return 0;
return this.sizeOf(value, typeArgs.type, rootNode);
function sizeOfSwitch(value, typeArgs, rootNode) {
var compareTo = getField(typeArgs.compareTo, rootNode);
var fieldInfo;
if (typeof typeArgs.fields[compareTo] === 'undefined' && typeof typeArgs.default === "undefined")
throw new Error(compareTo + " has no associated fieldInfo in switch");
else if (typeof typeArgs.fields[compareTo] === 'undefined')
fieldInfo = getFieldInfo(typeArgs.default);
else
fieldInfo = getFieldInfo(typeArgs.fields[compareTo]);
return this.sizeOf(value, fieldInfo, rootNode);
}

View file

@ -1,6 +1,5 @@
var getField = require("../utils").getField;
var debug = require("../debug");
var evalCondition = require("../utils").evalCondition;
module.exports = {
'array': [readArray, writeArray, sizeOfArray],

View file

@ -6,7 +6,8 @@ module.exports = {
'varint': [readVarInt, writeVarInt, sizeOfVarInt],
'bool': [readBool, writeBool, 1],
'string': [readString, writeString, sizeOfString],
'buffer': [readBuffer, writeBuffer, sizeOfBuffer]
'buffer': [readBuffer, writeBuffer, sizeOfBuffer],
'void': [readVoid, writeVoid, 0],
};
function readVarInt(buffer, offset) {
@ -131,3 +132,14 @@ function sizeOfBuffer(value, typeArgs, rootNode) {
}
return size + value.length;
}
function readVoid() {
return {
value: undefined,
size: 0,
};
}
function writeVoid(value, buffer, offset) {
return offset;
}

View file

@ -24,7 +24,6 @@ module.exports = {
packetStates: packetIndexes.packetStates,
types: serializer.types,
get: serializer.get,
evalCondition: utils.evalCondition,
ping: require('./ping'),
yggdrasil: Yggdrasil,
version: version.version,

View file

@ -47,7 +47,6 @@ proto.addTypes(conditional);
module.exports.types = proto.types;
var evalCondition = require("../utils").evalCondition;
var version = require('../version');
var packets = require('minecraft-data')(version.majorVersion).protocol;
var readPackets = require("../packets").readPackets;
@ -88,8 +87,8 @@ function createPacketBuffer(packetId, state, params, isServer) {
offset = utils.varint[1](packetId, buffer, offset);
packet.forEach(function(fieldInfo) {
var value = params[fieldInfo.name];
// TODO : A better check is probably needed
if(typeof value === "undefined" && fieldInfo.type != "count" && (fieldInfo.type != "condition" || evalCondition(fieldInfo.typeArgs, params)))
// TODO : This check belongs to the respective datatype.
if(typeof value === "undefined" && fieldInfo.type != "count")
debug(new Error("Missing Property " + fieldInfo.name).stack);
offset = proto.write(value, buffer, offset, fieldInfo.type, params);
});

View file

@ -1,7 +1,6 @@
module.exports = {
getField: getField,
getFieldInfo: getFieldInfo,
evalCondition: evalCondition
};
function getField(countField, rootNode) {
@ -23,14 +22,3 @@ function getFieldInfo(fieldInfo) {
else
throw new Error("Not a fieldinfo");
}
function evalCondition(condition, field_values) {
var field_value_to_test = "this" in condition && condition["this"] ? field_values["this"][condition.field] : field_values[condition.field];
var b = condition.values.some(function(value) {
return field_value_to_test === value;
});
if("different" in condition && condition["different"])
return !b;
else
return b;
}

View file

@ -15,7 +15,7 @@ var mc = require('../')
, SURVIVE_TIME = 10000
, MC_SERVER_PATH = path.join(__dirname, 'server')
, getFieldInfo = require('../dist/utils').getFieldInfo
, evalCondition = require('../dist/utils').evalCondition
, getField = require('../dist/utils').getField
;
var defaultServerProps = {
@ -62,30 +62,16 @@ var values = {
'ubyte': 8,
'string': "hi hi this is my client string",
'buffer': new Buffer(8),
'array': function(_typeArgs, packet) {
var typeArgs = getFieldInfo(_typeArgs.type)
if(typeof values[typeArgs.type] === "undefined") {
throw new Error("No data type for " + typeArgs.type);
}
if(typeof values[typeArgs.type] === "function") {
return [values[typeArgs.type](typeArgs.typeArgs, packet)];
}
return [values[typeArgs.type]];
'array': function(typeArgs, packet) {
return [getValue(typeArgs.type, packet)];
},
'container': function(typeArgs, packet) {
var results = {};
for(var index in typeArgs) {
if(typeof values[getFieldInfo(typeArgs[index].type).type] === "undefined") {
throw new Error("No data type for " + typeArgs[index].type);
}
if(typeof values[getFieldInfo(typeArgs[index].type).type] === "function") {
var backupThis = packet.this;
packet.this = results;
results[typeArgs[index].name] = values[getFieldInfo(typeArgs[index].type).type](getFieldInfo(typeArgs[index].type).typeArgs, packet);
packet.this = backupThis;
} else {
results[typeArgs[index].name] = values[getFieldInfo(typeArgs[index].type).type];
}
var backupThis = packet.this;
packet.this = results;
results[typeArgs[index].name] = getValue(typeArgs[index].type, packet);
packet.this = backupThis;
}
return results;
},
@ -127,16 +113,25 @@ var values = {
'UUID': "00112233-4455-6677-8899-aabbccddeeff",
'position': {x: 12, y: 332, z: 4382821},
'restBuffer': new Buffer(0),
'condition': function(typeArgs, packet) {
if (evalCondition(typeArgs, packet)) {
if (typeof values[getFieldInfo(typeArgs.type).type] === "function")
return values[getFieldInfo(typeArgs.type).type](getFieldInfo(typeArgs.type).typeArgs, packet);
else
return values[getFieldInfo(typeArgs.type).type];
}
}
'switch': function(typeArgs, packet) {
var i = typeArgs.fields[getField(typeArgs.compareTo, packet)];
if (typeof i === "undefined")
return getValue(typeArgs.default, packet);
else
return getValue(i, packet);
},
};
function getValue(_type, packet) {
var fieldInfo = getFieldInfo(_type);
if (typeof values[fieldInfo.type] === "function")
return values[fieldInfo.type](fieldInfo.typeArgs, packet);
else if (values[fieldInfo.type] !== "undefined")
return values[fieldInfo.type];
else if (fieldInfo.type !== "void")
throw new Error("No value for type " + fieldInfo.type);
}
describe("packets", function() {
var client, server, serverClient;
before(function(done) {
@ -188,14 +183,7 @@ describe("packets", function() {
// empty object uses default values
var packet = {};
packetInfo.forEach(function(field) {
var fieldVal = values[getFieldInfo(field.type).type];
if(typeof fieldVal === "undefined") {
throw new Error("No value for type " + field.type);
}
if(typeof fieldVal === "function") {
fieldVal = fieldVal(getFieldInfo(field.type).typeArgs, packet);
}
packet[field.name] = fieldVal;
packet[field.name] = getValue(field.type, packet);
});
if(toServer) {
serverClient.once([state, packetId], function(receivedPacket) {