From 9abe7997156152928944d270df8079eeef137779 Mon Sep 17 00:00:00 2001 From: Romain Beaumont Date: Fri, 8 May 2015 03:22:37 +0200 Subject: [PATCH] transform typeArgs function into object --- src/protocol.js | 12 ++++++++++-- src/protocol_def.js | 11 +++-------- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/src/protocol.js b/src/protocol.js index f3b9632..2b154ec 100644 --- a/src/protocol.js +++ b/src/protocol.js @@ -591,14 +591,22 @@ function readRestBuffer(buffer, offset, typeArgs, rootNode) { }; } +function evalCount(count,fields) +{ + if(fields[count["field"]] in count["map"]) + return count["map"][fields[count["field"]]]; + return count["default"]; +} + function readArray(buffer, offset, typeArgs, rootNode) { var results = { value: [], size: 0 } var count; - if (typeof typeArgs.count === "function") - count = typeArgs.count(rootNode); + if (typeof typeArgs.count === "object") { + count = evalCount(typeArgs.count,rootNode); + } else count = getField(typeArgs.count, rootNode); for (var i = 0; i < count; i++) { diff --git a/src/protocol_def.js b/src/protocol_def.js index 528d6db..3ff565f 100644 --- a/src/protocol_def.js +++ b/src/protocol_def.js @@ -358,14 +358,9 @@ module.exports= { { name: "offsetZ", type: "float" }, { name: "particleData", type: "float" }, { name: "particles", type: "int" }, - { name: "data", type: "array", typeArgs: { count: function(fields) { - if (fields.particleId === 36) - return 2; - else if (fields.particleId === 37 || fields.particleId === 38) - return 1; - else - return 0; - }, type: "varint" } } + { name: "data", type: "array", + typeArgs: { count: {"field":"particleId","map":{"36":2,"37":1,"38":1},"default":0}, + type: "varint" } } ]}, game_state_change: {id: 0x2b, fields: [ { name: "reason", type: "ubyte" },