pomelo-protobuf
Advanced tools
Comparing version 0.1.0 to 0.3.0
@@ -11,14 +11,15 @@ var Encoder = module.exports; | ||
var result = []; | ||
while(n !== 0){ | ||
do{ | ||
var tmp = n % 128; | ||
var next = Math.floor(n/128); | ||
if(next !== 0) | ||
if(next !== 0){ | ||
tmp = tmp + 128; | ||
} | ||
result.push(tmp); | ||
n = next; | ||
} | ||
} while(n !== 0); | ||
return result; | ||
} | ||
}; | ||
@@ -25,0 +26,0 @@ Encoder.encodeSInt32 = function(n){ |
@@ -150,3 +150,3 @@ var protoParser = require('./protoParser'); | ||
pos++; | ||
}while(b > 128); | ||
}while(b >= 128); | ||
@@ -153,0 +153,0 @@ if(!flag){ |
@@ -10,5 +10,10 @@ var protoParser = require('./protoParser'); | ||
this.protos = protos || {}; | ||
} | ||
}; | ||
MsgEncoder.encode = function(route, msg){ | ||
if(!route || !msg){ | ||
console.warn('Route or msg can not be null! route : %j, msg %j', route, msg); | ||
return null; | ||
} | ||
//Get protos from protos map use the route as key | ||
@@ -35,6 +40,8 @@ var protos = this.protos[route]; | ||
} | ||
//console.error('encode fail ! offset : %j', offset); | ||
} | ||
return null; | ||
} | ||
}; | ||
@@ -46,2 +53,3 @@ /** | ||
if(!protos){ | ||
console.warn('no protos exist for msg : %j', msg); | ||
return false; | ||
@@ -57,3 +65,3 @@ } | ||
if(typeof(msg[name]) === 'undefined'){ | ||
//console.log('no property msg : %j, name : %j', msg[name], name); | ||
console.warn('no property exist for required! name : %j ,proto : %j, msg : %j, ', name, proto); | ||
return false; | ||
@@ -86,2 +94,3 @@ } | ||
for(var name in msg){ | ||
//console.warn('name : %j, protos : %j', name, protos[name]); | ||
if(!!protos[name]){ | ||
@@ -100,3 +109,3 @@ var proto = protos[name]; | ||
case 'repeated' : | ||
if(msg[name].length > 0){ | ||
if(!!msg[name] && msg[name].length > 0){ | ||
offset = encodeArray(msg[name], proto, offset, buffer, protos); | ||
@@ -103,0 +112,0 @@ } |
@@ -7,5 +7,24 @@ var encoder = require('./msgEncoder'); | ||
Protobuf.encode = function(key, msg){ | ||
return encoder.encode(key, msg); | ||
} | ||
try{ | ||
return encoder.encode(key, msg); | ||
}catch (e){ | ||
console.trace(); | ||
console.log('encode msg failed! key : %j, msg :%j', key, msg); | ||
} | ||
}; | ||
Protobuf.encode2Bytes = function(key, msg){ | ||
var buffer = this.encode(key, msg); | ||
if(!buffer || !buffer.length){ | ||
console.warn('encode msg failed! key : %j, msg : %j', key, msg); | ||
return null; | ||
} | ||
var bytes = new Uint8Array(buffer.length); | ||
for(var offset = 0; offset < buffer.length; offset++){ | ||
bytes[offset] = buffer.readUInt8(offset); | ||
} | ||
return bytes; | ||
}; | ||
Protobuf.encodeStr = function(key, msg, code){ | ||
@@ -15,7 +34,7 @@ code = code || 'base64'; | ||
return !!buffer?buffer.toString(code):buffer; | ||
} | ||
}; | ||
Protobuf.decode = function(key, msg){ | ||
return decoder.decode(key, msg); | ||
} | ||
}; | ||
@@ -25,18 +44,17 @@ Protobuf.decodeStr = function(key, str, code){ | ||
var buffer = new Buffer(str, code); | ||
return !!buffer?Protobuf.decode(key, buffer):buffer; | ||
} | ||
}; | ||
Protobuf.parse = function(json){ | ||
return parser.parse(json); | ||
}; | ||
Protobuf.init = function(opts){ | ||
var encoderProtos = parser.parse(opts.encoderProtos); | ||
var decoderProtos = parser.parse(opts.decoderProtos); | ||
console.log(encoderProtos['onMove']); | ||
//On the serverside, use serverProtos to encode messages send to client | ||
encoder.init(encoderProtos); | ||
encoder.init(opts.encoderProtos); | ||
//On the serverside, user clientProtos to decode messages receive from clients | ||
decoder.init(decoderProtos); | ||
} | ||
decoder.init(opts.decoderProtos); | ||
}; |
{ | ||
"name": "pomelo-protobuf", | ||
"version": "0.1.0", | ||
"version": "0.3.0", | ||
"main": "./lib/protobuf", | ||
@@ -5,0 +5,0 @@ "dependencies": {}, |
@@ -1,4 +0,5 @@ | ||
var encoder = require('../../lib/client/msgEncoder'); | ||
var decoder = require('../../lib/client/msgDecoder'); | ||
var coder = require('../../lib/client/encoder'); | ||
var protobuf = require('../../lib/client/protobuf'); | ||
var encoder = protobuf.encoder; | ||
var decoder = protobuf.decoder; | ||
var codec = protobuf.codec; | ||
var parser = require('../../lib/protoParser'); | ||
@@ -11,28 +12,28 @@ var util = require('../../lib/util'); | ||
entityId : 1, | ||
paths : [{x : 1, y : 2, c : {a : 1}}, | ||
{x : 3, y : 4, c : {a : 2}, tests : [{a:1}, {a:2}]}], | ||
paths : [{x : 1, y : 2, c : {a : 1}}, | ||
{x : 3, y : 4, c : {a : 2}, tests : [{a:1}, {a:2}]}], | ||
speed : 110, | ||
speed1 : 'ss', | ||
a : 111.1111 | ||
} | ||
}; | ||
var protos = parser.parse(require('../protos.json')); | ||
encoder.init(protos); | ||
decoder.init(protos); | ||
//protobuf.init({encoderProtos:protos, decoderProtos:protos}); | ||
describe('encodeTest', function(){ | ||
var buffer = encoder.encode('onMove', msg); | ||
console.log(buffer); | ||
var decodeMsg = decoder.decode('onMove', buffer); | ||
//msg.should.equal(decodeMsg); | ||
util.equal(msg, decodeMsg).should.equal(true); | ||
var l1 = coder.byteLength(JSON.stringify(msg)); | ||
util.equal(msg, decodeMsg).should.equal(true); | ||
var l1 = codec.byteLength(JSON.stringify(msg)); | ||
var l2 = buffer.length; | ||
console.log('old Length : %j, new length : %j, compress rate : %j%', l1, l2, Math.floor(l2/l1*10000)/100); | ||
}) | ||
}) | ||
}); | ||
}); |
var protobuf = require('../lib/protobuf'); | ||
var util = require('../lib/util'); | ||
var should = require('should'); | ||
var tc = require('./testMsg'); | ||
describe('msgEncoderTest', function(){ | ||
var msg = { | ||
entityId : 1, | ||
paths : [{x : 1, y : 2, c : {a : 1}}, | ||
{x : 3, y : 4, c : {a : 2}, tests : [{a:1}, {a:2}]}], | ||
speed : 110, | ||
speed1 : 'ss', | ||
a : 111.1111 | ||
}; | ||
var protos = require('./protos.json'); | ||
var protos = protobuf.parse(require('./protos.json')); | ||
protobuf.init({encoderProtos:protos, decoderProtos:protos}); | ||
describe('encodeTest', function(){ | ||
var str = protobuf.encodeStr('onMove', msg); | ||
for(var route in tc){ | ||
var msg = tc[route]; | ||
var buffer = protobuf.encode(route, msg); | ||
var decodeMsg = protobuf.decodeStr('onMove', str); | ||
//console.log(str); | ||
var decodeMsg = protobuf.decode(route, buffer); | ||
//msg.should.equal(decodeMsg); | ||
util.equal(msg, decodeMsg).should.equal(true); | ||
//msg.should.equal(decodeMsg); | ||
//console.log(decodeMsg); | ||
if(route === 'area.playerHandler.enterScene'){ | ||
var map = decodeMsg.map.weightMap; | ||
for(var key in map) | ||
console.log(map[key]); | ||
} | ||
util.equal(msg, decodeMsg).should.equal(true); | ||
var oldStr = (JSON.stringify(msg)); | ||
console.log('old Length : %j, new length : %j, compress rate : %j%', Buffer.byteLength(oldStr), Buffer.byteLength(str), Math.floor(str.length/oldStr.length*10000)/100); | ||
//console.log(protobuf.encode(route, msg)); | ||
} | ||
//console.log('old Length : %j, new length : %j, compress rate : %j%', Buffer.byteLength(oldStr), Buffer.byteLength(str), Math.floor(str.length/oldStr.length*10000)/100); | ||
}); | ||
}); |
{ | ||
"onMove" : { | ||
"required uInt32 entityId" : 1, | ||
"message Path": { | ||
"message Test": { | ||
"required uInt32 a" : 1 | ||
}, | ||
"required uInt32 x" : 1, | ||
"required uInt32 y" : 2, | ||
"required Test c" : 3, | ||
"repeated Test tests" : 4 | ||
}, | ||
"repeated Path paths" : 2, | ||
"optional uInt32 speed" : 3, | ||
"required string speed1" : 4, | ||
"required double a" : 5 | ||
}, | ||
"onAttack" : { | ||
"required uInt32 attacker" : 1, | ||
"required uInt32 target" : 2, | ||
"message AttackResult" : { | ||
"required uInt32 result" : 1, | ||
"required uInt32 damage" : 2, | ||
"required uInt32 mpUse" : 3, | ||
"repeated Item items" : 4, | ||
"message Item" : { | ||
"required uInt32 kindId" : 1, | ||
"required uInt32 x" : 2, | ||
"required uInt32 y" : 3, | ||
"required uInt32 entityId" : 4, | ||
"required uInt32 playerId" : 5 | ||
} | ||
}, | ||
"required AttackResult result" : 3, | ||
"optional uInt32 exp" : 4, | ||
"optional uInt32 reviveTime" : 5, | ||
}, | ||
"onUpgrade" : { | ||
"" | ||
}, | ||
"onPickItem" : { | ||
"required uInt32 player" : 1, | ||
"required uInt32 item" : 2, | ||
"required uInt32 index" : 3 | ||
}, | ||
"onNPCTalk" : { | ||
"required uInt32 npc" : 1, | ||
"required string npcword" : 2, | ||
"required string myword" : 3 | ||
}, | ||
"onRevive" : { | ||
"required uInt32 entityId" : 1, | ||
"required uInt32 x" : 2, | ||
"required uInt32 y" : 3, | ||
"required uInt32 hp" : 4 | ||
}, | ||
"onRemoveEntities" : { | ||
}, | ||
"onPathCheckOut" : { | ||
"required uInt32 entityId" : 1, | ||
"message Position" : { | ||
"required uInt32 x" : 1, | ||
"required uInt32 y" : 2 | ||
}, | ||
"required Position position" : 2 | ||
}, | ||
"area.playerHandler.enterScene" : { | ||
"required uInt32 code" : 1, | ||
"message Player" : { | ||
}, | ||
"required Player curPlayer" : 2, | ||
"message Map" : { | ||
"required uInt32 mapWeight" : 1, | ||
"required uInt32 mapHeight" : 2, | ||
} | ||
} | ||
"onMove" : { | ||
"required uInt32 entityId" : 1, | ||
"message Path": { | ||
"required uInt32 x" : 1, | ||
"required uInt32 y" : 2 | ||
}, | ||
"repeated Path path" : 2, | ||
"required uInt32 speed" : 3 | ||
}, | ||
"onAttack" : { | ||
"required uInt32 attacker" : 1, | ||
"required uInt32 target" : 2, | ||
"message AttackResult" : { | ||
"required uInt32 result" : 1, | ||
"required uInt32 damage" : 2, | ||
"repeated Item items" : 4, | ||
"message Item" : { | ||
"required uInt32 kindId" : 1, | ||
"required uInt32 x" : 2, | ||
"required uInt32 y" : 3, | ||
"required uInt32 entityId" : 4, | ||
"required uInt32 playerId" : 5, | ||
"required string type" : 6 | ||
} | ||
}, | ||
"required AttackResult result" : 3, | ||
"optional uInt32 exp" : 4, | ||
"optional uInt32 reviveTime" : 5, | ||
"required uInt32 skillId" : 6 | ||
}, | ||
"onUpgrade" : { | ||
"required uInt32 entityId" : 1, | ||
"required uInt32 kindId" : 2, | ||
"required uInt32 x" : 3, | ||
"required uInt32 y" : 4, | ||
"required uInt32 level" : 5, | ||
"required uInt32 walkSpeed" : 6, | ||
"required uInt32 hp" : 7, | ||
"required uInt32 maxHp" : 8, | ||
"required uInt32 mp" : 9, | ||
"required uInt32 maxMp" : 10, | ||
"required uInt32 id" : 11, | ||
"required string name" : 12, | ||
"required uInt32 experience" : 13, | ||
"required uInt32 attackValue" : 14, | ||
"required uInt32 defenceValue" : 15, | ||
"required double attackSpeed" : 16, | ||
"required uInt32 areaId" : 17, | ||
"required uInt32 hitRate" : 18, | ||
"required uInt32 dodgeRate" : 19, | ||
"required uInt32 nextLevelExp" : 20, | ||
"required uInt32 skillPoint" : 21, | ||
"required string kindName" : 22, | ||
"required string type" : 23 | ||
}, | ||
"onPickItem" : { | ||
"required uInt32 player" : 1, | ||
"required uInt32 item" : 2, | ||
"required uInt32 index" : 3 | ||
}, | ||
"onNPCTalk" : { | ||
"required uInt32 npc" : 1, | ||
"required string npcword" : 2, | ||
"required string myword" : 3 | ||
}, | ||
"onRevive" : { | ||
"required uInt32 entityId" : 1, | ||
"required uInt32 x" : 2, | ||
"required uInt32 y" : 3, | ||
"required uInt32 hp" : 4 | ||
}, | ||
"onAddEntities" : { | ||
"message NPC" : { | ||
"required uInt32 entityId" : 1, | ||
"required uInt32 kindId" : 2, | ||
"required uInt32 x" : 3, | ||
"required uInt32 y" : 4 | ||
}, | ||
"message Mob" : { | ||
"required uInt32 entityId" : 1, | ||
"required uInt32 kindId" : 2, | ||
"required uInt32 x" : 3, | ||
"required uInt32 y" : 4, | ||
"required uInt32 level" : 5, | ||
"required uInt32 walkSpeed" : 6, | ||
"required uInt32 hp" : 7, | ||
"required uInt32 maxHp" : 8 | ||
}, | ||
"message Item" : { | ||
"required uInt32 entityId" : 1, | ||
"required uInt32 kindId" : 2, | ||
"required uInt32 x" : 3, | ||
"required uInt32 y" : 4, | ||
"required uInt32 playerId" : 5 | ||
}, | ||
"message Equipment" : { | ||
"required uInt32 entityId" : 1, | ||
"required uInt32 kindId" : 2, | ||
"required uInt32 x" : 3, | ||
"required uInt32 y" : 4, | ||
"required uInt32 playerId" : 5 | ||
}, | ||
"message Player" : { | ||
"required uInt32 entityId" : 1, | ||
"required uInt32 kindId" : 2, | ||
"required uInt32 x" : 3, | ||
"required uInt32 y" : 4, | ||
"required uInt32 level" : 5, | ||
"required uInt32 walkSpeed" : 6, | ||
"required uInt32 hp" : 7, | ||
"required uInt32 maxHp" : 8, | ||
"required uInt32 mp" : 9, | ||
"required uInt32 maxMp" : 10, | ||
"required uInt32 id" : 11, | ||
"required string name" : 12 | ||
}, | ||
"repeated NPC npc" : 1, | ||
"repeated Mob mob" : 2, | ||
"repeated Item item" : 3, | ||
"repeated Equipment euipment" : 4, | ||
"repeated Player player" : 5 | ||
}, | ||
"onRemoveEntities" : { | ||
"repeated uInt32 entities" : 1 | ||
}, | ||
"onPathCheckOut" : { | ||
"required uInt32 entityId" : 1, | ||
"message Position" : { | ||
"required uInt32 x" : 1, | ||
"required uInt32 y" : 2 | ||
}, | ||
"required Position position" : 2 | ||
}, | ||
"area.playerHandler.enterScene" : { | ||
"message Entities" : { | ||
"message NPC" : { | ||
"required uInt32 entityId" : 1, | ||
"required uInt32 kindId" : 2, | ||
"required uInt32 x" : 3, | ||
"required uInt32 y" : 4 | ||
}, | ||
"message Mob" : { | ||
"required uInt32 entityId" : 1, | ||
"required uInt32 kindId" : 2, | ||
"required uInt32 x" : 3, | ||
"required uInt32 y" : 4, | ||
"required uInt32 level" : 5, | ||
"required uInt32 walkSpeed" : 6, | ||
"required uInt32 hp" : 7, | ||
"required uInt32 maxHp" : 8 | ||
}, | ||
"message Item" : { | ||
"required uInt32 entityId" : 1, | ||
"required uInt32 kindId" : 2, | ||
"required uInt32 x" : 3, | ||
"required uInt32 y" : 4, | ||
"required uInt32 playerId" : 5 | ||
}, | ||
"message Equipment" : { | ||
"required uInt32 entityId" : 1, | ||
"required uInt32 kindId" : 2, | ||
"required uInt32 x" : 3, | ||
"required uInt32 y" : 4, | ||
"required uInt32 playerId" : 5 | ||
}, | ||
"message Player" : { | ||
"required uInt32 entityId" : 1, | ||
"required uInt32 kindId" : 2, | ||
"required uInt32 x" : 3, | ||
"required uInt32 y" : 4, | ||
"required uInt32 level" : 5, | ||
"required uInt32 walkSpeed" : 6, | ||
"required uInt32 hp" : 7, | ||
"required uInt32 maxHp" : 8, | ||
"required uInt32 mp" : 9, | ||
"required uInt32 maxMp" : 10, | ||
"required uInt32 id" : 11, | ||
"required string name" : 12 | ||
}, | ||
"repeated NPC npc" : 1, | ||
"repeated Mob mob" : 2, | ||
"repeated Item item" : 3, | ||
"repeated Equipment euipment" : 4, | ||
"repeated Player player" : 5 | ||
}, | ||
"message Player" : { | ||
"message Bag" : { | ||
"message Item" : { | ||
"required uInt32 key" : 1, | ||
"required uInt32 id" : 2, | ||
"required string type" : 3 | ||
}, | ||
"required uInt32 itemCount" : 1, | ||
"repeated Item items" : 2 | ||
}, | ||
"message Equipments" : { | ||
"required uInt32 weapon" : 1, | ||
"required uInt32 armor" : 2, | ||
"required uInt32 helmet" : 3, | ||
"required uInt32 necklace" : 4, | ||
"required uInt32 ring" : 5, | ||
"required uInt32 belt" : 6, | ||
"required uInt32 shoes" : 7, | ||
"required uInt32 legguard" : 8, | ||
"required uInt32 amulet" : 9 | ||
}, | ||
"message FightSkill" : { | ||
"required uInt32 id" : 1, | ||
"required uInt32 level" : 2 | ||
}, | ||
"required uInt32 entityId" : 1, | ||
"required uInt32 kindId" : 2, | ||
"required uInt32 x" : 3, | ||
"required uInt32 y" : 4, | ||
"required uInt32 level" : 5, | ||
"required uInt32 walkSpeed" : 6, | ||
"required uInt32 hp" : 7, | ||
"required uInt32 maxHp" : 8, | ||
"required uInt32 mp" : 9, | ||
"required uInt32 maxMp" : 10, | ||
"required uInt32 id" : 11, | ||
"required string name" : 12, | ||
"required uInt32 experience" : 13, | ||
"required uInt32 attackValue" : 14, | ||
"required uInt32 defenceValue" : 15, | ||
"required double attackSpeed" : 16, | ||
"required uInt32 areaId" : 17, | ||
"required uInt32 hitRate" : 18, | ||
"required uInt32 dodgeRate" : 19, | ||
"required uInt32 nextLevelExp" : 20, | ||
"required uInt32 skillPoint" : 21, | ||
"required string type" : 22, | ||
"required Bag bag" : 23, | ||
"required Equipments equipments" : 24, | ||
"repeated FightSkill fightSkills" : 25 | ||
}, | ||
"optional Entities entities" : 1, | ||
"optional Player curPlayer" : 2, | ||
"message Map" : { | ||
"message Collisions" : { | ||
"message Collison" : { | ||
"required uInt32 start" : 1, | ||
"required uInt32 length" : 2 | ||
}, | ||
"repeated Collison collisions" : 1 | ||
}, | ||
"required string name" : 1, | ||
"required uInt32 width" : 2, | ||
"required uInt32 height" : 3, | ||
"required uInt32 tileW" : 4, | ||
"required uInt32 tileH" : 5, | ||
"repeated Collisions weightMap" : 6 | ||
}, | ||
"required Map map" : 3 | ||
} | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No README
QualityPackage does not have a README. This may indicate a failed publish or a low quality package.
Found 1 instance in 1 package
46072
21
1527
0
3
1