-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjagbuf.js
More file actions
91 lines (76 loc) · 2.67 KB
/
Copy pathjagbuf.js
File metadata and controls
91 lines (76 loc) · 2.67 KB
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
/**
* @file Wrapper for buffers that facilitate operations useful for
* interacting with RuneScape and its files.
*/
/**
* A wrapper for buffers that keeps a caret of the next byte to read in
* the buffer. It also has functions to get unsigned bytes, shorts, and ints
* from the buffer beginning at the caret.
* @param {Uint8Array} data The buffer to wrap.
*/
function JagBuffer(data) {
this.caret = 0;
this.data = data;
/**
* Gets an unsigned byte from where the caret is in the buffer, and
* increments the caret by one.
*/
this.getUByte = function () {
if (this.caret + 1 >= this.size()) {
return -1;
}
/// The >>> 0 part is to keep things unsigned.
const out = this.data[this.caret] >>> 0;
this.caret += 1;
return out;
};
/**
* Gets an unsigned short from two bytes in the buffer, starting at and
* including the caret of the JagBuffer, and increments the caret by two.
* @returns {number} The unsigned short.
*/
this.getUShort = function () {
if (this.caret + 2 >= this.size()) {
return -1;
}
let out = ((this.data[this.caret] & 0xff) << 8) >>> 0;
out = (out | (this.data[this.caret + 1] & 0xff)) >>> 0;
this.caret += 2;
return out;
}
/**
* Gets an unsigned int from three bytes in the buffer, starting at and
* including the caret of the JagBuffer, and increments the caret by three.
* @returns {number} The unsigned int.
*/
this.getUInt3 = function () {
if (this.caret + 3 >= this.size()) {
return -1;
}
let out = ((this.data[this.caret] & 0xff) << 16) >>> 0;
out = (out | ((this.data[this.caret + 1] & 0xff) << 8)) >>> 0;
out = (out | (this.data[this.caret + 2] & 0xff)) >>> 0;
this.caret += 3;
return out;
};
/**
* Gets an unsigned int from four bytes in the buffer, starting at and
* including the caret of the JagBuffer, and increments the caret by four.
* @returns {number} The unsigned int.
*/
this.getUInt4 = function () {
if (this.caret + 4 >= this.size()) {
return -1;
}
let out = ((this.data[this.caret] & 0xff) << 24) >>> 0;
out = (out | ((this.data[this.caret + 1] & 0xff) << 16)) >>> 0;
out = (out | ((this.data[this.caret + 2] & 0xff) << 8)) >>> 0;
out = (out | (this.data[this.caret + 3] & 0xff)) >>> 0;
this.caret += 4;
return out;
};
this.size = function () {
return this.data.byteLength
};
}
module.exports.JagBuffer = JagBuffer;