|
1 |
| -var zlib = require('zlib'); |
2 |
| -var ProtoDef=require("protodef").ProtoDef; |
3 |
| -var compound=require("./compound").compound; |
| 1 | +const zlib = require('zlib') |
| 2 | +const ProtoDef = require('protodef').ProtoDef |
| 3 | +const compound = require('./compound').compound |
4 | 4 |
|
| 5 | +const nbtJson = JSON.stringify(require('./nbt.json')) |
| 6 | +const leNbtJson = nbtJson.replace(/(i[0-9]+)/g, 'l$1') |
5 | 7 |
|
6 |
| -var nbtJson=JSON.stringify(require('./nbt.json')); |
7 |
| -var leNbtJson=nbtJson.replace(/(i[0-9]+)/g,"l$1"); |
8 |
| - |
9 |
| -function createProto(le) { |
10 |
| - var proto = new ProtoDef(); |
11 |
| - proto.addType("compound",compound); |
12 |
| - proto.addTypes(JSON.parse(le ? leNbtJson : nbtJson)); |
13 |
| - return proto; |
| 8 | +function createProto (le) { |
| 9 | + const proto = new ProtoDef() |
| 10 | + proto.addType('compound', compound) |
| 11 | + proto.addTypes(JSON.parse(le ? leNbtJson : nbtJson)) |
| 12 | + return proto |
14 | 13 | }
|
15 | 14 |
|
16 |
| -var proto=createProto(false); |
17 |
| -var protoLE=createProto(true); |
| 15 | +const proto = createProto(false) |
| 16 | +const protoLE = createProto(true) |
18 | 17 |
|
19 |
| -function writeUncompressed(value,le) { |
20 |
| - return (le ? protoLE : proto).createPacketBuffer("nbt",value); |
| 18 | +function writeUncompressed (value, le) { |
| 19 | + return (le ? protoLE : proto).createPacketBuffer('nbt', value) |
21 | 20 | }
|
22 | 21 |
|
23 |
| -function parseUncompressed(data,le) { |
24 |
| - return (le ? protoLE : proto).parsePacketBuffer("nbt",data).data; |
| 22 | +function parseUncompressed (data, le) { |
| 23 | + return (le ? protoLE : proto).parsePacketBuffer('nbt', data).data |
25 | 24 | }
|
26 | 25 |
|
| 26 | +const hasGzipHeader = function (data) { |
| 27 | + let result = true |
| 28 | + if (data[0] !== 0x1f) result = false |
| 29 | + if (data[1] !== 0x8b) result = false |
| 30 | + return result |
| 31 | +} |
27 | 32 |
|
28 |
| -var hasGzipHeader = function(data){ |
29 |
| - var result=true; |
30 |
| - if(data[0]!=0x1f) result=false; |
31 |
| - if(data[1]!=0x8b) result=false; |
32 |
| - return result; |
33 |
| -}; |
34 |
| - |
35 |
| -function parse(data, le, callback) { |
36 |
| - var isLe=false; |
37 |
| - if (typeof le === "function") { |
38 |
| - callback=le; |
39 |
| - } |
40 |
| - else { |
41 |
| - isLe=le; |
| 33 | +function parse (data, le, callback) { |
| 34 | + let isLe = false |
| 35 | + if (typeof le === 'function') { |
| 36 | + callback = le |
| 37 | + } else { |
| 38 | + isLe = le |
42 | 39 | }
|
43 | 40 | if (hasGzipHeader(data)) {
|
44 |
| - zlib.gunzip(data, function(error, uncompressed) { |
| 41 | + zlib.gunzip(data, function (error, uncompressed) { |
45 | 42 | if (error) {
|
46 |
| - callback(error, data); |
| 43 | + callback(error, data) |
47 | 44 | } else {
|
48 |
| - callback(null, parseUncompressed(uncompressed,isLe)); |
| 45 | + callback(null, parseUncompressed(uncompressed, isLe)) |
49 | 46 | }
|
50 |
| - }); |
| 47 | + }) |
51 | 48 | } else {
|
52 |
| - callback(null, parseUncompressed(data,isLe)); |
| 49 | + callback(null, parseUncompressed(data, isLe)) |
53 | 50 | }
|
54 | 51 | }
|
55 | 52 |
|
56 |
| -function simplify(data) |
57 |
| -{ |
58 |
| - function transform(value,type) |
59 |
| - { |
60 |
| - if(type=="compound") { |
61 |
| - return Object.keys(value).reduce(function(acc,key){ |
62 |
| - acc[key]=simplify(value[key]); |
63 |
| - return acc; |
64 |
| - },{}); |
| 53 | +function simplify (data) { |
| 54 | + function transform (value, type) { |
| 55 | + if (type === 'compound') { |
| 56 | + return Object.keys(value).reduce(function (acc, key) { |
| 57 | + acc[key] = simplify(value[key]) |
| 58 | + return acc |
| 59 | + }, {}) |
65 | 60 | }
|
66 |
| - if(type=="list") { |
67 |
| - return value.value.map(function(v){return transform(v,value.type)}); |
| 61 | + if (type === 'list') { |
| 62 | + return value.value.map(function (v) { return transform(v, value.type) }) |
68 | 63 | }
|
69 |
| - return value; |
| 64 | + return value |
70 | 65 | }
|
71 |
| - return transform(data.value,data.type); |
| 66 | + return transform(data.value, data.type) |
72 | 67 | }
|
73 | 68 |
|
74 |
| -module.exports={ |
75 |
| - writeUncompressed:writeUncompressed, |
76 |
| - parseUncompressed:parseUncompressed, |
77 |
| - simplify:simplify, |
78 |
| - parse:parse, |
79 |
| - proto:proto, |
80 |
| - protoLE:protoLE |
81 |
| -}; |
| 69 | +module.exports = { |
| 70 | + writeUncompressed: writeUncompressed, |
| 71 | + parseUncompressed: parseUncompressed, |
| 72 | + simplify: simplify, |
| 73 | + parse: parse, |
| 74 | + proto: proto, |
| 75 | + protoLE: protoLE |
| 76 | +} |
0 commit comments