-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbyteStream.js
215 lines (188 loc) · 4.22 KB
/
byteStream.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
const uuidParse = require("uuid-parse");
const Item = require("./player/item.js");
class ByteStream {
constructor(buffer) {
this.buffer = buffer
this.i = 0
}
readByte() {
if(this.empty()){
throw new Error("Buffer is empty")
}
var result = this.buffer[this.i]
this.i++
return result
}
writeByte(data) {
if(this.empty()){
throw new Error("Buffer too small")
}
this.buffer[this.i] = data
this.i++
}
readVarIntAndSize() {
var numRead = 0;
var result = 0;
do {
var read = this.readByte();
var value = (read & 0b01111111);
result |= (value << (7 * numRead));
numRead++;
if (numRead > 5) {
throw new Error("VarInt is too big");
}
} while ((read & 0b10000000) != 0);
return {
result: result,
size: numRead
}
}
readVarInt() {
return this.readVarIntAndSize().result;
}
writeVarInt(value) {
do {
var temp = value & 0b01111111;
value >>>= 7;
if (value != 0) {
temp |= 0b10000000;
}
this.writeByte(temp);
} while (value != 0);
}
amendVarInt(f) {
var value = this.readVarIntAndSize()
this.i -= value.size
this.writeVarInt(f(value.result))
}
writeBlocks(values, blockBits) {
if(values.length % 64 != 0){
throw new Error(`Size of value array must be divisible by 64`)
}
var nextByte = 0
var bitsRemaining = 8
for(var i = 0; i < values.length; i++) {
var v = values[i]
for(var j = blockBits-1; j >= 0; j--) {
nextByte <<= 1
bitsRemaining--
if(v >= (1 << j)){
nextByte++
v -= (1 << j)
}
if(bitsRemaining == 0){
this.writeByte(nextByte)
nextByte = 0
bitsRemaining = 8
}
}
}
}
readInt() {
var value = this.buffer.readInt32BE(this.i)
this.i += 4
return value
}
writeInt(val) {
this.buffer.writeInt32BE(val, this.i)
this.i += 4
}
readUuid(uuid) {
var uuidBuffer = Buffer.alloc(16)
this.buffer.copy(uuidBuffer, 0, this.i, this.i + 16)
return uuidParse.unparse(uuidBuffer)
}
writeUuid(uuid) {
Buffer.from(uuidParse.parse(uuid)).copy(this.buffer, this.i)
this.i += 16
}
readFloat() {
var value = this.buffer.readFloatBE(this.i)
this.i += 4
return value
}
writeFloat(val) {
this.buffer.writeFloatBE(val, this.i)
this.i += 4
}
readShort() {
var value = this.buffer.readInt16BE(this.i)
this.i += 2
return value
}
readDouble() {
var value = this.buffer.readDoubleBE(this.i)
this.i += 8
return value
}
writeDouble(val) {
this.buffer.writeDoubleBE(val, this.i)
this.i += 8
}
amendDouble(f) {
var d = this.readDouble()
this.i -= 8
this.writeDouble(f(d))
}
readByteArray() {
var result = []
while(!this.empty()){
result.push(this.readByte());
}
return result
}
readString() {
var result = []
var len = this.readByte()
for(var i = 0; i < len; i++){
result.push(this.readByte())
}
return Buffer.from(result).toString()
}
readPosition() {
var x, y, z
var first = this.buffer.slice(this.i).readInt32BE()
var second = this.buffer.slice(this.i+4).readInt32BE()
x = first >> 6
y = ((first & 0x3F) << 3) | (second >>> 26)
z = second & 0x03FFFFFF
this.i += 8
return {
x: x,
y: y,
z: z
}
}
writePosition(position) {
var value = position.x << 6
value += (position.y & 0xFC) >> 6
this.buffer.writeInt32BE(value, this.i)
this.i += 4
var value = (position.y & 0x3F) << 26
value += position.z
this.buffer.writeInt32BE(value, this.i)
this.i += 4
}
readItem() {
var present = this.readByte()
if(present){
var itemID = this.readVarInt()
var itemCount = this.readVarInt()
return new Item(present, itemID, itemCount)
} else {
return new Item(present)
}
}
tail(upTo) {
if(this.empty()){
return Buffer.alloc(0)
}
var result = this.buffer.slice(this.i, upTo)
this.i = upTo
return result
}
empty() {
return this.i >= this.buffer.length
}
}
module.exports = ByteStream