-
Notifications
You must be signed in to change notification settings - Fork 5
/
nbt.js
244 lines (211 loc) · 5.66 KB
/
nbt.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
'use strict';
const fs = require('fs');
const zlib = require('zlib');
const Tag = require('./lib/base_tag');
const { Bvffer } = require('./lib/bvffer');
/**
* The NBT class
* @see http://minecraft.gamepedia.com/NBT_Format
*/
class NBT {
constructor() {
this.root = {};
}
/**
* Load from buffer
* @param {Buffer} buff The buffer to load from
* @param {(err?: Error) => void} callback The callback to call when done
*/
loadFromBuffer(buff, callback) {
try {
this._buff = buff;
let offset = 0;
while (offset < buff.length) {
const wrapper = Tag.getNextTag(buff, offset);
const tag = wrapper.tag;
const len = wrapper.length;
this.root[tag.id] = tag;
offset += len;
}
} catch (e) {
return callback(e);
}
callback();
}
/**
* Load from compressed buffer
* @param {Buffer} buff The buffer to load from
* @param {(err?: Error) => void} callback The callback to call when done
*/
loadFromZlibCompressedBuffer(buff, callback) {
zlib.unzip(buff, (err, buff) => {
if (err) {
return callback(err);
}
this.loadFromBuffer(buff, callback);
});
}
/**
* Write to compressed buffer
* @param {(err?: Error, buff?: Buffer) => void} callback The callback to
* call when done
* @param {'gzip'|'deflate'} [method='gzip'] The compression method to use
*/
writeToCompressedBuffer(callback, method = 'gzip') {
try {
const _buff = this.writeToBuffer();
zlib[method](_buff, callback);
} catch (e) {
return callback(e);
}
}
/**
* Write to buffer
* @return {Buffer} The buffer
*/
writeToBuffer() {
const buff = new Bvffer(this.calcBufferLength());
for (const key in this.root) {
if (!this.root.hasOwnProperty(key)) continue;
const object = this.root[key];
buff.writeUInt8(object.getTypeId());
const nameBuff = Buffer.from(object.id, 'utf8');
buff.writeBuffer(nameBuff);
object.writeBuffer(buff);
}
return buff.buff;
}
/**
* Select a tag
* @param {string} tagName the tag name in root
* @return {BaseTag|null} The tag which matches `tagName`
*/
select(tagName) {
if (!this.root || !Object.keys(this.root).length) return null;
if (undefined === this.root[tagName]) return null;
return this.root[tagName];
}
/**
* Alias for `select()`
* @param {string} tagName the tag name in root
* @return {BaseTag|null} The tag which matches `tagName`
*/
get(tagName) {
return this.select(tagName);
}
/**
* Get root object's length
* @return {number} root length
*/
count() {
if (!this.root) return null;
return Object.keys(this.root).length;
}
/**
* Get root's keys
* @return {string[]} root's keys
*/
keys() {
if (!this.root) return null;
return Object.keys(this.root);
}
/**
* Inspect
* @return {string} Inspect string
*/
inspect() {
return `<NBT ${JSON.stringify(this.keys())} >`;
}
/**
* toString
* @return {string} String
*/
toString() {
return JSON.stringify(this.toJSON(), true, 2);
}
/**
* Load NBT structure from file
* @param {string} filename NBT filename
* @param {(err?: Error) => void} callback callback function
*/
loadFromFile(filename, callback) {
// eslint-disable-next-line node/prefer-promises/fs
fs.readFile(filename, (err, buff) => {
if (err) {
return callback(err);
}
this.loadFromBuffer(buff, callback);
});
}
/**
* Load NBT structure from zlib compressed file
* @param {string} filename NBT filename
* @param {(err?: Error) => void} callback callback function
*/
loadFromZlibCompressedFile(filename, callback) {
// eslint-disable-next-line node/prefer-promises/fs
fs.readFile(filename, (err, buff) => {
if (err) {
return callback(err);
}
this.loadFromZlibCompressedBuffer(buff, callback);
});
}
/**
* Write NBT structure to file
* @param {string} filename NBT filename
* @param {(err?: Error) => void} callback callback function
*/
writeToFile(filename, callback) {
try {
// eslint-disable-next-line node/prefer-promises/fs
fs.writeFile(filename, this.writeToBuffer(), callback);
} catch (e) {
return callback(e);
}
}
/**
* Write NBT structure to zlib compressed file
* @param {string} filename NBT filename
* @param {(err?: Error) => void} callback callback function
* @param {'gzip'|'deflate'} [method='gzip'] The compression method to use
*/
writeToCompressedFile(filename, callback, method = 'gzip') {
this.writeToCompressedBuffer((err, buff) => {
if (err) return callback(err);
// eslint-disable-next-line node/prefer-promises/fs
fs.writeFile(filename, buff, callback);
}, method);
}
/**
* Calculate buffer length
* @return {number} buffer length
*/
calcBufferLength() {
let len = 0;
for (const key in this.root) {
if (!this.root.hasOwnProperty(key)) continue;
// child type id for 1 byte, child name length for 2 bytes and child
// name for (child name length) byte(s).
len += 3;
len += Buffer.byteLength(this.root[key].id, 'utf8');
// add the child body's length
len += this.root[key].calcBufferLength();
}
return len;
}
/**
* toJSON
* @return {Object} JSON object
*/
toJSON() {
const res = {};
for (const key in this.root) {
if (!this.root.hasOwnProperty(key)) continue;
res[key] = this.root[key].toJSON();
}
return res;
}
}
NBT.Tags = require('./lib/tags');
module.exports = NBT;