add new condition type :

* protocol.js : new read, write and sizeOf methods and add check for condition type inside read
* update protocol.json
* update protocol_schema.json
* some fixes in test.js to handle the new condition type
This commit is contained in:
Romain Beaumont 2015-05-09 05:58:12 +02:00
parent 9c9f358a5d
commit faed176631
4 changed files with 243 additions and 153 deletions

View file

@ -476,8 +476,9 @@
},
{
"name": "velocityX",
"type": "short",
"condition": {
"type": "condition",
"typeArgs": {
"type": "short",
"field": "intField",
"values": [
0
@ -488,8 +489,9 @@
},
{
"name": "velocityY",
"type": "short",
"condition": {
"type": "condition",
"typeArgs": {
"type": "short",
"field": "intField",
"values": [
0
@ -500,8 +502,9 @@
},
{
"name": "velocityZ",
"type": "short",
"condition": {
"type": "condition",
"typeArgs": {
"type": "short",
"field": "intField",
"values": [
0
@ -1369,12 +1372,13 @@
},
{
"name": "entityId",
"type": "int",
"condition": {
"type": "condition",
"typeArgs": {
"field": "inventoryType",
"values": [
11
]
],
"type": "int"
}
}
]
@ -1538,8 +1542,9 @@
},
{
"name": "rows",
"type": "byte",
"condition": {
"type":"condition",
"typeArgs":{
"type":"byte",
"field": "columns",
"values": [
0
@ -1549,8 +1554,9 @@
},
{
"name": "x",
"type": "byte",
"condition": {
"type": "condition",
"typeArgs": {
"type": "byte",
"field": "columns",
"values": [
0
@ -1560,8 +1566,9 @@
},
{
"name": "y",
"type": "byte",
"condition": {
"type": "condition",
"typeArgs": {
"type": "byte",
"field": "columns",
"values": [
0
@ -1571,12 +1578,13 @@
},
{
"name": "dataLength",
"type": "count",
"type":"condition",
"typeArgs": {
"countFor": "data",
"type": "varint"
},
"condition": {
"type": "count",
"typeArgs": {
"countFor": "data",
"type": "varint"
},
"field": "columns",
"values": [
0
@ -1586,11 +1594,12 @@
},
{
"name": "data",
"type": "buffer",
"typeArgs": {
"count": "dataLength"
},
"condition": {
"type": "condition",
"typeArgs":{
"type": "buffer",
"typeArgs": {
"count": "dataLength"
},
"field": "columns",
"values": [
0
@ -1688,8 +1697,9 @@
},
{
"name": "name",
"type": "string",
"condition": {
"type": "condition",
"typeArgs": {
"type": "string",
"field": "action",
"values": [
0
@ -1698,63 +1708,67 @@
},
{
"name": "propertiesLength",
"type": "count",
"condition": {
"type": "condition",
"typeArgs": {
"type": "count",
"field": "action",
"values": [
0
]
},
"typeArgs": {
"countFor": "this.properties",
"type": "varint"
],
"typeArgs": {
"countFor": "this.properties",
"type": "varint"
}
}
},
{
"name": "properties",
"type": "array",
"condition": {
"type": "condition",
"typeArgs": {
"type": "array",
"field": "action",
"values": [
0
]
},
"typeArgs": {
"count": "this.propertiesLength",
"type": "container",
],
"typeArgs": {
"fields": [
{
"name": "name",
"type": "string"
},
{
"name": "value",
"type": "ustring"
},
{
"name": "isSigned",
"type": "bool"
},
{
"name": "signature",
"type": "ustring",
"condition": {
"field": "isSigned",
"values": [
true
],
"this": true
"count": "this.propertiesLength",
"type": "container",
"typeArgs": {
"fields": [
{
"name": "name",
"type": "string"
},
{
"name": "value",
"type": "ustring"
},
{
"name": "isSigned",
"type": "bool"
},
{
"name": "signature",
"type": "condition",
"typeArgs": {
"type": "ustring",
"field": "isSigned",
"values": [
true
],
"this": true
}
}
}
]
]
}
}
}
},
{
"name": "gamemode",
"type": "varint",
"condition": {
"type": "condition",
"typeArgs": {
"type": "varint",
"field": "action",
"values": [
0,
@ -1764,8 +1778,9 @@
},
{
"name": "ping",
"type": "varint",
"condition": {
"type":"condition",
"typeArgs": {
"type": "varint",
"field": "action",
"values": [
0,
@ -1775,8 +1790,9 @@
},
{
"name": "hasDisplayName",
"type": "bool",
"condition": {
"type":"condition",
"typeArgs": {
"type": "bool",
"field": "action",
"values": [
0,
@ -1786,8 +1802,9 @@
},
{
"name": "displayName",
"type": "string",
"condition": {
"type": "condition",
"typeArgs": {
"type": "string",
"field": "hasDisplayName",
"values": [
true
@ -1851,8 +1868,9 @@
},
{
"name": "displayText",
"type": "string",
"condition": {
"type": "condition",
"typeArgs": {
"type": "string",
"field": "action",
"values": [
0,
@ -1862,8 +1880,9 @@
},
{
"name": "type",
"type": "string",
"condition": {
"type":"condition",
"typeArgs":{
"type": "string",
"field": "action",
"values": [
0,
@ -1890,8 +1909,9 @@
},
{
"name": "value",
"type": "varint",
"condition": {
"type": "condition",
"typeArgs":{
"type": "varint",
"field": "action",
"values": [
1
@ -1927,8 +1947,9 @@
},
{
"name": "name",
"type": "string",
"condition": {
"type": "condition",
"typeArgs": {
"type": "string",
"field": "mode",
"values": [
0,
@ -1938,8 +1959,9 @@
},
{
"name": "prefix",
"type": "string",
"condition": {
"type": "condition",
"typeArgs": {
"type": "string",
"field": "mode",
"values": [
0,
@ -1949,8 +1971,9 @@
},
{
"name": "suffix",
"type": "string",
"condition": {
"type": "condition",
"typeArgs": {
"type": "string",
"field": "mode",
"values": [
0,
@ -1960,8 +1983,9 @@
},
{
"name": "friendlyFire",
"type": "byte",
"condition": {
"type": "condition",
"typeArgs": {
"type": "byte",
"field": "mode",
"values": [
0,
@ -1971,8 +1995,9 @@
},
{
"name": "nameTagVisibility",
"type": "string",
"condition": {
"type": "condition",
"typeArgs": {
"type": "string",
"field": "mode",
"values": [
0,
@ -1982,8 +2007,9 @@
},
{
"name": "color",
"type": "byte",
"condition": {
"type": "condition",
"typeArgs": {
"type": "byte",
"field": "mode",
"values": [
0,
@ -1993,34 +2019,36 @@
},
{
"name": "playerCount",
"type": "condition",
"typeArgs": {
"type": "count",
"condition": {
"field": "mode",
"values": [
0,
3,
4
]
},
"typeArgs": {
"type": "short",
"countFor": "players"
],
"typeArgs": {
"type": "short",
"countFor": "players"
}
}
},
{
"name": "players",
"type": "array",
"condition": {
"type": "condition",
"typeArgs": {
"type": "array",
"field": "mode",
"values": [
0,
3,
4
]
},
"typeArgs": {
"type": "string",
"count": "playerCount"
],
"typeArgs": {
"type": "string",
"count": "playerCount"
}
}
}
]
@ -2065,8 +2093,9 @@
},
{
"name": "duration",
"type": "varint",
"condition": {
"type": "condition",
"typeArgs": {
"type": "varint",
"field": "event",
"values": [
1
@ -2075,8 +2104,9 @@
},
{
"name": "playerId",
"type": "varint",
"condition": {
"type": "condition",
"typeArgs": {
"type": "varint",
"field": "event",
"values": [
2
@ -2085,8 +2115,9 @@
},
{
"name": "entityId",
"type": "int",
"condition": {
"type": "condition",
"typeArgs": {
"type": "int",
"field": "event",
"values": [
1,
@ -2096,8 +2127,9 @@
},
{
"name": "message",
"type": "string",
"condition": {
"type": "condition",
"typeArgs": {
"type": "string",
"field": "event",
"values": [
2
@ -2124,8 +2156,9 @@
},
{
"name": "radius",
"type": "double",
"condition": {
"type": "condition",
"typeArgs": {
"type": "double",
"field": "action",
"values": [
0
@ -2134,8 +2167,9 @@
},
{
"name": "x",
"type": "double",
"condition": {
"type": "condition",
"typeArgs": {
"type": "double",
"field": "action",
"values": [
2,
@ -2145,8 +2179,9 @@
},
{
"name": "z",
"type": "double",
"condition": {
"type": "condition",
"typeArgs": {
"type": "double",
"field": "action",
"values": [
2,
@ -2156,8 +2191,9 @@
},
{
"name": "old_radius",
"type": "double",
"condition": {
"type": "condition",
"typeArgs": {
"type": "double",
"field": "action",
"values": [
1,
@ -2167,8 +2203,9 @@
},
{
"name": "new_radius",
"type": "double",
"condition": {
"type": "condition",
"typeArgs": {
"type": "double",
"field": "action",
"values": [
1,
@ -2178,8 +2215,9 @@
},
{
"name": "speed",
"type": "varint",
"condition": {
"type": "condition",
"typeArgs": {
"type": "varint",
"field": "action",
"values": [
1,
@ -2189,8 +2227,9 @@
},
{
"name": "portalBoundary",
"type": "varint",
"condition": {
"type": "condition",
"typeArgs": {
"type": "varint",
"field": "action",
"values": [
3
@ -2199,8 +2238,9 @@
},
{
"name": "warning_time",
"type": "varint",
"condition": {
"type": "condition",
"typeArgs": {
"type": "varint",
"field": "action",
"values": [
4,
@ -2210,8 +2250,9 @@
},
{
"name": "warning_blocks",
"type": "varint",
"condition": {
"type": "condition",
"typeArgs": {
"type": "varint",
"field": "action",
"values": [
5,
@ -2230,8 +2271,9 @@
},
{
"name": "text",
"type": "string",
"condition": {
"type": "condition",
"typeArgs": {
"type": "string",
"field": "action",
"values": [
0,
@ -2241,8 +2283,9 @@
},
{
"name": "fadeIn",
"type": "int",
"condition": {
"type": "condition",
"typeArgs": {
"type": "int",
"field": "action",
"values": [
2
@ -2251,8 +2294,9 @@
},
{
"name": "stay",
"type": "int",
"condition": {
"type": "condition",
"typeArgs": {
"type": "int",
"field": "action",
"values": [
2
@ -2261,8 +2305,9 @@
},
{
"name": "fadeOut",
"type": "int",
"condition": {
"type": "condition",
"typeArgs": {
"type": "int",
"field": "action",
"values": [
2
@ -2352,8 +2397,9 @@
},
{
"name": "x",
"type": "float",
"condition": {
"type": "condition",
"typeArgs": {
"type": "float",
"field": "mouse",
"values": [
2
@ -2362,8 +2408,9 @@
},
{
"name": "y",
"type": "float",
"condition": {
"type": "condition",
"typeArgs": {
"type": "float",
"field": "mouse",
"values": [
2
@ -2372,8 +2419,9 @@
},
{
"name": "z",
"type": "float",
"condition": {
"type": "condition",
"typeArgs": {
"type": "float",
"field": "mouse",
"values": [
2
@ -2687,8 +2735,9 @@
},
{
"name": "block",
"type": "position",
"condition": {
"type": "condition",
"typeArgs": {
"type": "position",
"field": "hasPosition",
"values": [
true
@ -2768,4 +2817,4 @@
}
}
}
}
}

View file

@ -63,7 +63,22 @@
"countFor": {"$ref" : "#/definitions/fieldTypeArgsCountFor"},
"count": {"$ref" : "#/definitions/fieldTypeArgsCount"},
"fields": {"$ref" : "#/definitions/fields"},
"typeArgs": {"$ref" : "#/definitions/fieldTypeArgs"}
"typeArgs": {"$ref" : "#/definitions/fieldTypeArgs"},
"field": {"$ref" : "#/definitions/fieldName"},
"values": {
"type": "array",
"items": {
"type": ["integer","boolean"]
},
"additionalItems": false,
"minItems": 1
},
"different": {
"type": "boolean"
},
"this": {
"type": "boolean"
}
},
"additionalProperties": false
},

View file

@ -76,6 +76,7 @@ var types = {
'slot': [readSlot, writeSlot, sizeOfSlot],
'nbt': [readNbt, writeBuffer, sizeOfBuffer],
'entityMetadata': [readEntityMetadata, writeEntityMetadata, sizeOfEntityMetadata],
'condition': [readCondition, writeCondition, sizeOfCondition]
};
var debug;
@ -119,6 +120,28 @@ for (var n in entityMetadataTypes) {
entityMetadataTypeBytes[entityMetadataTypes[n].type] = n;
}
function readCondition(buffer,offset,typeArgs, rootNode)
{
if(!evalCondition(typeArgs,rootNode))
return null;
return read(buffer, offset, { type: typeArgs.type, typeArgs:typeArgs.typeArgs }, rootNode);
}
function writeCondition(value, buffer, offset, typeArgs, rootNode) {
if(!evalCondition(typeArgs,rootNode))
return offset;
return write(value, buffer, offset, { type: typeArgs.type, typeArgs:typeArgs.typeArgs }, rootNode);
}
function sizeOfCondition(value, fieldInfo, rootNode) {
if(!evalCondition(fieldInfo,rootNode))
return 0;
return sizeOf(value,fieldInfo, rootNode);
}
function evalCondition(condition,field_values)
{
var field_value_to_test="this" in condition && condition["this"] ? field_values["this"][condition.field] : field_values[condition.field];
@ -645,8 +668,6 @@ function writeCount(value, buffer, offset, typeArgs, rootNode) {
function sizeOfCount(value, typeArgs, rootNode) {
// TODO : should I use value or getField().length ?
/*console.log(rootNode);
console.log(typeArgs);*/
return sizeOf(getField(typeArgs.countFor, rootNode).length, { type: typeArgs.type }, rootNode);
}
@ -661,7 +682,7 @@ function read(buffer, cursor, fieldInfo, rootNodes) {
};
}
var readResults = type[0](buffer, cursor, fieldInfo.typeArgs, rootNodes);
if (readResults == null) {
if (readResults == null && fieldInfo.type!=="condition") {
throw new Error("Reader returned null : " + JSON.stringify(fieldInfo));
}
if (readResults && readResults.error) return { error: readResults.error };

View file

@ -120,7 +120,12 @@ var values = {
},
'UUID': [42, 42, 42, 42],
'position': { x: 12, y: 332, z: 4382821 },
'restBuffer': new Buffer(0)
'restBuffer': new Buffer(0),
'condition' : function(typeArgs) {
// check whether this should return undefined if needed instead ? (using evalCondition)
// probably not since we only send values that respect the condition
return values[typeArgs.type];
}
};
describe("packets", function() {
@ -173,7 +178,7 @@ describe("packets", function() {
// empty object uses default values
var packet = {};
packetInfo.forEach(function(field) {
if (!field.hasOwnProperty("condition") || protocol.evalCondition(field.condition,packet)) {
if ((!field.hasOwnProperty("condition") || protocol.evalCondition(field.condition,packet)) && (field.type!=="condition" || protocol.evalCondition(field.typeArgs,packet))) {
var fieldVal = values[field.type];
if (typeof fieldVal === "undefined") {
throw new Error("No value for type " + field.type);