This repository has been archived by the owner on Jun 8, 2022. It is now read-only.
forked from Itsyuka/osu-buffer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
osu-buffer.js
489 lines (431 loc) · 10.9 KB
/
osu-buffer.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
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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
'use strict';
class OsuBuffer {
/**
* @param input
*/
constructor(input) {
this.buffer = Buffer.from((input instanceof Buffer) ? input : []);
this.position = 0;
}
/**
* Returns the full length of the buffer
* @returns {Number}
*/
get length() {
return this.buffer.length;
}
/**
* Returns buffer to a binary string
* @returns {String}
*/
async toString(type = 'binary') {
return this.buffer.toString(type);
}
/**
* Creates a new OsuBuffer from arguments
* @returns {OsuBuffer}
*/
static from() {
if (arguments[0] instanceof OsuBuffer) {
arguments[0] = arguments[0].buffer;
}
return new OsuBuffer(Buffer.from.apply(Buffer, arguments));
}
/**
* Returns boolean if can read from defined length from buffer
* @param {Number} length
* @return {boolean}
*/
canRead(length) {
return length + this.position <= this.buffer.length;
}
/**
* Returns boolean if at end of the buffer
* @return {boolean}
*/
EOF() {
return this.position >= this.buffer.length;
}
/**
* Slices and returns buffer
* @param {Number} length
* @param {Boolean?} asOsuBuffer
* @return {OsuBuffer|Buffer}
*/
Slice(length, asOsuBuffer = true) {
this.position += length;
return asOsuBuffer ? OsuBuffer.from(this.buffer.slice(this.position - length, this.position))
: this.buffer.slice(this.position - length, this.position);
}
// Reading
/**
* Peeks the next byte in the buffer without shifting the position
* @return {Number|undefined}
*/
Peek() {
return this.buffer[this.position+1];
}
/**
* Reads a byte from the buffer
* Does the same thing as ReadUInt8()
* @return {Number}
*/
async ReadByte() {
return this.ReadUInt8();
}
/**
* Reads a signed integer from the Buffer
* @param {Number} byteLength
* @return {Number}
*/
async ReadInt(byteLength) {
this.position += byteLength;
return this.buffer.readIntLE(this.position - byteLength, byteLength);
}
/**
* Reads a unsigned integer from the Buffer
* @param {Number} byteLength
* @return {Number}
*/
async ReadUInt(byteLength) {
this.position += byteLength;
return this.buffer.readUIntLE(this.position - byteLength, byteLength);
}
/**
* Reads a 8-bit signed integer from the buffer
* @return {Number}
*/
async ReadInt8() {
return this.ReadInt(1);
}
/**
* Reads a 8-bit unsigned integer from the buffer
* @return {Number}
*/
async ReadUInt8() {
return this.ReadUInt(1);
}
/**
* Reads a 16-bit signed integer from the buffer
* @return {Number}
*/
async ReadInt16() {
return this.ReadInt(2);
}
/**
* Reads a 16-bit unsigned integer from the buffer
* @return {Number}
*/
async ReadUInt16() {
return this.ReadUInt(2);
}
/**
* Reads a 32-bit signed integer from the buffer
* @return {Number}
*/
async ReadInt32() {
return this.ReadInt(4);
}
/**
* Reads a 32-bit signed unsigned from the buffer
* @return {Number}
*/
async ReadUInt32() {
return this.ReadUInt(4);
}
/**
* Reads a 64-bit signed integer from the buffer
* @return {Number}
*/
async ReadInt64() {
return (this.ReadInt(4) << 8) + this.ReadInt(4);
}
/**
* Reads a 64-bit signed unsigned from the buffer
* @return {Number}
*/
async ReadUInt64() {
return (this.ReadUInt(4) << 8) + this.ReadUInt(4);
}
/**
* Reads a 32-bit Float from the buffer
* @returns {Number}
*/
async ReadFloat() {
this.position += 4;
return this.buffer.readFloatLE(this.position - 4);
}
/**
* Reads a 64-bit Double from the buffer
* @returns {Number}
*/
async ReadDouble() {
this.position += 8;
return this.buffer.readDoubleLE(this.position - 8);
}
/**
* Reads a string from the buffer
* @param {Number} length
* @returns {String}
*/
async ReadString(length) {
return this.Slice(length, false).toString();
}
/**
* Decodes a 7-bit encoded integer from the buffer
* @returns {Number}
*/
async ReadVarint() {
let total = 0;
let shift = 0;
let byte = this.ReadUInt8();
if((byte & 0x80) === 0) {
total |= ((byte & 0x7F) << shift);
} else {
let end = false;
do {
if(shift) {
byte = this.ReadUInt8();
}
total |= ((byte & 0x7F) << shift);
if((byte & 0x80) === 0) end = true;
shift += 7;
} while (!end);
}
return total;
}
/**
* Decodes a 7-bit encoded integer from the buffer
* @deprecated Use ReadVarint instead
* @returns {Number}
*/
async ReadULeb128() {
return this.ReadVarint();
}
/**
* Reads a byte from buffer and converts to boolean
* @return {boolean}
*/
async ReadBoolean() {
return Boolean(this.ReadInt(1));
}
/**
* Reads an osu! encoded string from the Buffer
* @returns {string}
*/
async ReadOsuString() {
let isString = this.ReadByte() === 11;
if(isString) {
let len = this.ReadVarint();
return this.ReadString(len);
} else {
return '';
}
}
// Writing
/**
* Concats a buffer to the current buffer
* @param {Buffer} value
* @return {OsuBuffer}
*/
WriteBuffer(value) {
this.buffer = Buffer.concat([this.buffer, value]);
return this;
}
/**
* Writes an unsinged integer of any byte length
* @param {Number} value
* @param {Number} byteLength
* @return {OsuBuffer}
*/
WriteUInt(value, byteLength) {
let buff = Buffer.alloc(byteLength);
buff.writeUIntLE(value, 0, byteLength);
return this.WriteBuffer(buff);
}
/**
* Writes an integer of any byte length
* @param {Number} value
* @param {Number} byteLength
* @return {OsuBuffer}
*/
WriteInt(value, byteLength) {
let buff = Buffer.alloc(byteLength);
buff.writeIntLE(value, 0, byteLength);
return this.WriteBuffer(buff);
}
/**
* Writes a 8-bit integer to the Buffer
* @param {Number} value
* @return {OsuBuffer}
*/
WriteByte(value) {
return this.WriteBuffer(Buffer.alloc(1, value));
}
/**
*
* @param {Array} value
* @return {OsuBuffer}
*/
WriteBytes(value) {
return this.WriteBuffer(Buffer.from(value));
}
/**
* Writes a 8-bit integer to the Buffer
* @param {Number} value
* @return {OsuBuffer}
*/
WriteUInt8(value) {
return this.WriteUInt(value, 1);
}
/**
* Writes a 8-bit integer to the Buffer
* @param {Number} value
* @return {OsuBuffer}
*/
WriteInt8(value) {
return this.WriteInt(value, 1);
}
/**
* Writes a 16-bit unsigned integer to the Buffer
* @param {Number} value
* @return {OsuBuffer}
*/
WriteUInt16(value) {
return this.WriteUInt(value, 2);
}
/**
* Writes a 16-bit signed integer to the Buffer
* @param {Number} value
* @return {OsuBuffer}
*/
WriteInt16(value) {
return this.WriteInt(value, 2);
}
/**
* Writes a 32-bit unsigned integer to the Buffer
* @param {Number} value
* @return {OsuBuffer}
*/
WriteUInt32(value) {
return this.WriteUInt(value, 4);
}
/**
* Writes a 32-bit signed integer to the Buffer
* @param {Number} value
* @return {OsuBuffer}
*/
WriteInt32(value) {
return this.WriteInt(value, 4);
}
/**
* Writes a 64-bit unsigned integer to the Buffer
* @param {Number} value
* @return {OsuBuffer}
*/
WriteUInt64(value) {
let buff = Buffer.alloc(8);
// High
buff.writeUInt32LE(value >> 8, 0);
// Low
buff.writeUInt32LE(value & 0x00ff, 4);
return this.WriteBuffer(buff);
}
/**
* Writes a 64-bit signed integer to the Buffer
* @param {Number} value
* @return {OsuBuffer}
*/
WriteInt64(value) {
let buff = Buffer.alloc(8);
// High
buff.writeInt32LE(value >> 8, 0);
// Low
buff.writeInt32LE(value & 0x00ff, 4);
return this.WriteBuffer(buff);
}
/**
* Writes a 32-bit float to the buffer
* @param {Number} value
* @return {OsuBuffer}
*/
WriteFloat(value) {
let buff = Buffer.alloc(4);
buff.writeFloatLE(value, 0);
return this.WriteBuffer(buff);
}
/**
* Writes a 64-bit double to the buffer
* @param {Number} value
* @return {OsuBuffer}
*/
WriteDouble(value) {
let buff = Buffer.alloc(8);
buff.writeDoubleLE(value, 0);
return this.WriteBuffer(buff);
}
/**
* Writes a string to the Buffer
* @param {string} value
* @return {OsuBuffer}
*/
WriteString(value) {
let buff = Buffer.alloc(Buffer.byteLength(value, 'utf8'));
buff.write(value);
return this.WriteBuffer(buff);
}
/**
* Writes a boolean to the buffer
* @param {boolean} value
* @return {OsuBuffer}
*/
WriteBoolean(value) {
return this.WriteByte(value ? 1 : 0);
}
/**
* Writes an osu! encoded string to the Buffer
* @param {string?} value
* @param nullable
* @return {OsuBuffer}
*/
WriteOsuString(value, nullable = false) {
if(value.length === 0 && nullable)
{
this.WriteByte(0);
} else if(value.length === 0)
{
this.WriteByte(11);
this.WriteByte(0);
} else {
this.WriteByte(11);
this.WriteVarint(Buffer.byteLength(value, 'utf8'));
this.WriteString(value);
}
return this;
}
/**
* Writes an unsigned 7-bit encoded integer to the Buffer
* @param {Number} value
* @return {OsuBuffer}
*/
WriteVarint(value) {
let arr = [];
let len = 0;
do {
arr[len] = value & 0x7F;
if ((value >>= 7) !== 0) arr[len] |= 0x80;
len++;
} while (value > 0);
return this.WriteBuffer(Buffer.from(arr));
}
/**
* Writes an unsigned 7-bit encoded integer to the Buffer
* @deprecated Use WriteUVarint instead
* @param {Number} value
* @return {OsuBuffer}
*/
WriteULeb128(value) {
return this.WriteVarint(value);
}
}
module.exports = OsuBuffer;