Gistrec

ProtoDef TEST

Jul 26th, 2017
221
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const ProtoDef = require("protodef").ProtoDef;
  2. const Serializer = require("protodef").Serializer;
  3. const Parser = require("protodef").Parser;
  4.  
  5. // the protocol can be in a separate json file
  6. const example_protocol={
  7.   "container": "native",
  8.   "varint": "native",
  9.   "byte": "native",
  10.   "bool": "native",
  11.   "switch": "native",
  12.   "entity_look": [
  13.         "container",
  14.         [
  15.             {
  16.                 "name": "entityId",
  17.                 "type": "varint"
  18.             },
  19.             {
  20.                 "name": "yaw",
  21.                 "type": "i8"
  22.             },
  23.             {
  24.                 "name": "pitch",
  25.                 "type": "i8"
  26.             },
  27.             {
  28.                 "name": "onGround",
  29.                 "type": "bool"
  30.             }
  31.         ]
  32.   ],
  33.   "packet": [
  34.         "container",
  35.         [
  36.             {
  37.                 "name": "name",
  38.                 "type": [
  39.                     "mapper",
  40.                     {
  41.                         "type": "varint",
  42.                         "mappings": {
  43.                             "22": "entity_look"
  44.                         }
  45.                     }
  46.                 ]
  47.             },
  48.             {
  49.                 "name": "params",
  50.                 "type": [
  51.                     "switch",
  52.                     {
  53.                         "compareTo": "name",
  54.                         "fields": {
  55.                             "entity_look": "entity_look"
  56.                         }
  57.                     }
  58.                 ]
  59.             }
  60.         ]
  61.   ]
  62. };
  63.  
  64. const proto = new ProtoDef();
  65. proto.addTypes(example_protocol);
  66. const parser = new Parser(proto, "packet");
  67. const serializer = new Serializer(proto, "packet");
  68.  
  69. serializer.write({
  70.   name: "entity_look",
  71.   params: {
  72.     "entityId": 1,
  73.     "yaw": 1,
  74.     "pitch": 1,
  75.     "onGround": true
  76.   }
  77. });
  78. serializer.pipe(parser);
  79.  
  80. parser.on('data', function (chunk) {
  81.   console.log(JSON.stringify(chunk, null, 2));
  82. });
  83.  
  84.  
  85. ##############################################
  86. #               Результат:                   #
  87. ##############################################
  88.  
  89. {
  90.     "data": {
  91.         "name": "entity_look",
  92.         "params": {
  93.             "entityId": 1,
  94.             "yaw": 1,
  95.             "pitch": 1,
  96.             "onGround": true
  97.         }
  98.     },
  99.     "metadata": {
  100.         "size": 5
  101.     },
  102.     "buffer": {
  103.         "type": "Buffer",
  104.         "data": [
  105.             22,
  106.             1,
  107.             1,
  108.             1,
  109.             1
  110.         ]
  111.     }
  112. }
Comments
  • User was banned
Add Comment
Please, Sign In to add comment