-
Notifications
You must be signed in to change notification settings - Fork 0
/
WritableBuffer.ts
335 lines (279 loc) · 10.9 KB
/
WritableBuffer.ts
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
import { Logging, LoggingLevel } from "../game/Logging.js";
import { UUID } from "../game/UUID.js";
import { ChatComponent } from "../game/chat/ChatComponent.js";
import { ReadableBuffer } from "./ReadableBuffer.js";
export class WritableBuffer {
private _Prepend: boolean;
private _Buffer: Buffer;
private _Ranges: Array<[number, string]>;
/**
* Returns the current buffer
* @returns {Buffer} The underlying buffer
*/
public get Buffer(): Buffer {
return this._Buffer;
}
/**
* Returns an array of annotated ranges for tracing
* @returns {Array<[Buffer, string]>}
*/
public get Ranges(): Array<[Buffer, string]> {
const buf = new ReadableBuffer(this._Buffer);
// Convert the ranges into a list of annotated buffers
return this._Ranges.map((range: [number, string]) => {
const [size, annotation] = range;
// Safely read the described ranges
return [buf.Read(size), annotation];
});
}
/**
* Returns a readable buffer of the current buffer
* @returns {ReadableBuffer} A readable version of the underlying buffer
*/
public get ReadableBuffer(): ReadableBuffer {
// Preserve range definitions
return new ReadableBuffer(this._Buffer, this._Ranges);
}
constructor(buf?: Buffer, ranges?: Array<[number, string]>) {
this._Prepend = false;
this._Buffer = buf || Buffer.alloc(0);
this._Ranges = ranges || [];
}
/**
* Enables prepend mode for a single write operation
* @returns {WritableBuffer} The current WritableBuffer with prepend mode enabled
*/
public Prepend(): WritableBuffer {
this._Prepend = true;
return this;
}
/**
* Writes multiple bytes to the buffer
* @param {Buffer} value The buffer to write
* @param {string} [annotation] The name of the region to be written
*/
public Write(value: Buffer, annotation?: string) {
const trace = Logging.Level() === LoggingLevel.TRACE;
if (this._Prepend) {
// Add range annotations to beginning
if (trace)
this._Ranges.unshift([ value.length, annotation ]);
this._Buffer = Buffer.concat([ value, this._Buffer ]);
this._Prepend = false;
} else {
// Add range annotations
if (trace)
this._Ranges.push([ value.length, annotation ]);
this._Buffer = Buffer.concat([ this._Buffer, value ]);
}
}
/**
* Writes a length-prefixed buffer to the buffer
* @param {Buffer} value The buffer to write
* @param {string} [annotation] The name of the region to be written
*/
public WriteBuffer(value: Buffer, annotation?: string) {
this.WriteVarInt(value.length, annotation ? annotation + " Length" : undefined);
this.Write(value, annotation);
}
/**
* Writes a single byte to the buffer
* @param {number} value The byte to write, in numerical form
* @param {string} [annotation] The name of the region to be written
*/
public WriteByte(value: number, annotation?: string) {
// Ensure only a single byte is written
const buf = Buffer.alloc(1);
buf.writeUInt8(value);
this.Write(buf, annotation);
}
/**
* Writes a signed byte to the buffer
* @param {number} value The byte to write, in numerical form
* @param {string} [annotation] The name of the region to be written
*/
public WriteSignedByte(value: number, annotation?: string) {
// Ensure only a single byte is written
const buf = Buffer.alloc(1);
buf.writeInt8(value);
this.Write(buf, annotation);
}
/**
* Writes a single-byte bool to the buffer
* @param {boolean} value The boolean to write
* @param {string} [annotation] The name of the region to be written
*/
public WriteBool(value: boolean, annotation?: string) {
this.WriteByte(value ? 0x1 : 0x0, annotation);
}
/**
* Writes a variable-length Minecraft VarInt to the buffer. (little-endian)
* @param {number} value The number to convert and write
* @param {string} [annotation] The name of the region to be written
*/
public WriteVarInt(value: number, annotation?: string) {
const temp: WritableBuffer = new WritableBuffer();
do {
// Get 7 lowest bits to write
let digit: number = value & 0b01111111;
// Shift away the lowest bits, moving the sign bit so that can be captured later
value >>>= 7;
// If there are more digits to write, set the continuation bit
if (value != 0)
digit |= 0b10000000;
temp.WriteByte(digit);
} while (value != 0);
this.Write(temp.Buffer, annotation);
}
/**
* Writes a variable-length Minecraft VarLong to the buffer (little-endian)
* @param {bigint} value The bigint to convert and write
* @param {string} [annotation] The name of the region to be written
*/
public WriteVarLong(value: bigint, annotation?: string) {
const temp: WritableBuffer = new WritableBuffer();
// Calculate the two's complement for negative bigints (since the sign will not be shifted below)
if (value < 0)
value = (BigInt(1) << BigInt(64)) + value;
do {
// Get the 7 lowest bits to write
let digit = Number(value & BigInt(0b01111111));
// Shift away the lowest bits
value = value >> BigInt(7);
// If there are more digits to write, set the continuation bit
if (value != BigInt(0))
digit |= 0b10000000;
temp.WriteByte(digit);
} while (value != BigInt(0));
this.Write(temp._Buffer, annotation);
}
/**
* Writes a single-byte character to the buffer
* @param {string} value The character to write
* @param {string} [annotation] The name of the region to be written
*/
public WriteChar(value: string, annotation?: string) {
this.WriteByte(value.charCodeAt(0), annotation);
}
/**
* Writes a length-prefixed string to the buffer
* @param {string} value The string to write
* @param {string} [annotation] The name of the region to be written
*/
public WriteVarChar(value: string, annotation?: string) {
this.WriteVarInt(value.length, annotation ? annotation + " Length" : undefined);
this.Write(Buffer.from(value), annotation);
}
/**
* Writes a two-byte positive integer to the buffer
* @param {number} value The integer to write
* @param {string} [annotation] The name of the region to be written
*/
public WriteUint16(value: number, annotation?: string) {
const buf: Buffer = Buffer.alloc(2);
buf.writeUInt16BE(value);
this.Write(buf, annotation);
}
/**
* Writes a four-byte positive integer to the buffer
* @param {number} value The integer to write
* @param {string} [annotation] The name of the region to be written
*/
public WriteUint32(value: number, annotation?: string) {
const buf: Buffer = Buffer.alloc(4);
buf.writeUInt32BE(value);
this.Write(buf, annotation);
}
/**
* Writes an eight-byte positive long integer to the buffer
* @param {number} value The long integer to write
* @param {string} [annotation] The name of the region to be written
*/
public WriteUint64(value: bigint, annotation?: string) {
const buf: Buffer = Buffer.alloc(8);
buf.writeBigUInt64BE(value);
this.Write(buf, annotation);
}
/**
* Writes a two-byte positive or negative integer to the buffer
* @param {number} value The integer to write
* @param {string} [annotation] The name of the region to be written
*/
public WriteInt16(value: number, annotation?: string) {
const buf: Buffer = Buffer.alloc(2);
buf.writeInt16BE(value);
this.Write(buf, annotation);
}
/**
* Writes a four-byte positive or negative integer to the buffer
* @param {number} value The integer to write
* @param {string} [annotation] The name of the region to be written
*/
public WriteInt32(value: number, annotation?: string) {
const buf: Buffer = Buffer.alloc(4);
buf.writeInt32BE(value);
this.Write(buf, annotation);
}
/**
* Writes an eight-byte positive or negative long integer to the buffer
* @param {number} value The long integer to write
* @param {string} [annotation] The name of the region to be written
*/
public WriteInt64(value: bigint, annotation?: string) {
const buf: Buffer = Buffer.alloc(8);
buf.writeBigInt64BE(value);
this.Write(buf, annotation);
}
/**
* Writes a four-byte float to the buffer
* @param {number} value The float to write
* @param {string} [annotation] The name of the region to be written
*/
public WriteSingle(value: number, annotation?: string) {
const buf: Buffer = Buffer.alloc(4);
buf.writeFloatBE(value);
this.Write(buf, annotation);
}
/**
* Writes an eight-byte double to the buffer
* @param {number} value The double to write
* @param {string} [annotation] The name of the region to be written
*/
public WriteDouble(value: number, annotation?: string) {
const buf: Buffer = Buffer.alloc(8);
buf.writeDoubleBE(value);
this.Write(buf, annotation);
}
/**
* Writes a stringified JSON object to the buffer as a VarChar
* @param {object} value The JavaScript object to stringify and write
* @param {string} [annotation] The name of the region to be written
*/
public WriteJSON<T extends object>(value: T, annotation?: string) {
this.WriteVarChar(JSON.stringify(value), annotation + " JSON");
}
/**
* Converts and writes a Chat message to the buffer using WriteJSON
* @param {ChatComponent} value The Chat message to convert and write
* @param {string} [annotation] The name of the region to be written
*/
public WriteChat(value: ChatComponent, annotation?: string) {
this.WriteJSON(value, annotation + " Chat");
}
/**
* Writes a Minecraft UUID to the buffer
* @param {UUID} value The UUID to write
* @param {string} [annotation] The name of the region to be written
*/
public WriteUUID(value: UUID, annotation?: string) {
const buf: WritableBuffer = new WritableBuffer();
// Split the UUID into two bigints
const uuid: string = value.Format();
const msb = BigInt("0x" + uuid.substring(0, uuid.length / 2));
const lsb = BigInt("0x" + uuid.substring(uuid.length / 2));
// Write the 128-bit UUID
buf.WriteUint64(msb);
buf.WriteUint64(lsb);
this.Write(buf.Buffer, annotation);
}
}