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

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);