-
Notifications
You must be signed in to change notification settings - Fork 47
/
datautils.py
261 lines (225 loc) · 7.46 KB
/
datautils.py
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
import json
import struct
from spockbot.mcp import nbt, proto
from spockbot.mcp.bbuff import BoundBuffer
from spockbot.mcp.proto import (MC_BYTE, MC_CHAT, MC_FLOAT, MC_FP_BYTE,
MC_FP_INT, MC_INT, MC_LONG, MC_META,
MC_POSITION, MC_SHORT, MC_SLOT, MC_STRING,
MC_UBYTE, MC_ULONG, MC_UUID, MC_VARINT,
MC_VARLONG)
def byte_to_hex(byte_str):
return ''.join(["%02X " % x for x in byte_str]).strip()
# Unpack/Pack functions return None on error
# Minecraft varints are 32-bit signed values
# packed into Google Protobuf varints
def unpack_varint(bbuff):
total = 0
shift = 0
val = 0x80
while val & 0x80:
val = struct.unpack('B', bbuff.read(1))[0]
total |= (val & 0x7F) << shift
shift += 7
if total >= (1 << 32):
return None
if total & (1 << 31):
total -= 1 << 32
return total
def pack_varint(val):
if val >= (1 << 31) or val < -(1 << 31):
return None
o = b''
if val < 0:
val += 1 << 32
while val >= 0x80:
bits = val & 0x7F
val >>= 7
o += struct.pack('B', (0x80 | bits))
bits = val & 0x7F
o += struct.pack('B', bits)
return o
# Like a varint, but a 64-bit signed value
def unpack_varlong(bbuff):
total = 0
shift = 0
val = 0x80
while val & 0x80:
val = struct.unpack('B', bbuff.read(1))[0]
total |= ((val & 0x7F) << shift)
shift += 7
if total >= 1 << 64:
return None
if total & (1 << 63):
total -= 1 << 64
return total
def pack_varlong(val):
if val >= (1 << 63) or val < -(1 << 63):
return None
o = b''
if val < 0:
val += 1 << 64
while val >= 0x80:
bits = val & 0x7F
val >>= 7
o += struct.pack('B', (0x80 | bits))
bits = val & 0x7F
o += struct.pack('B', bits)
return o
# Three values packed into one 64-bit long
# x: 26 MSBs, y: 12 bits, z: 26 LSBs
def unpack_position(bbuff):
position = {}
val = unpack(MC_LONG, bbuff)
position['x'] = val >> 38
position['y'] = (val >> 26) & 0xFFF
z = val & 0x3FFFFFF
if z & (1 << 25):
z -= (1 << 26)
position['z'] = z
return position
def pack_position(position):
val = (int(position['x']) & 0x3FFFFFF) << 38
val |= (int(position['y']) & 0xFFF) << 26
val |= (int(position['z']) & 0x3FFFFFF)
return pack(MC_ULONG, val)
# Fixed point number where the first 5 bits are dedicated to the decimal place
# Internally vanilla mc implements these as doubles, so we do the same thing
def unpack_fixed_point(mc_type, bbuff):
val = unpack(mc_type, bbuff)
val = float(val) / (1 << 5)
return val
def pack_fixed_point(mc_type, val):
val = int(val * (1 << 5))
return pack(mc_type, val)
# Slots are dictionaries that hold info about
# inventory items, they also have funky
# enchantment data
def unpack_slot(bbuff):
slot = {}
slot['id'] = unpack(MC_SHORT, bbuff)
if slot['id'] != -1:
slot['amount'] = unpack(MC_BYTE, bbuff)
slot['damage'] = unpack(MC_SHORT, bbuff)
nbt_start = unpack(MC_BYTE, bbuff)
if nbt_start > 0:
if nbt_start != nbt.TAG_COMPOUND:
return None
name = nbt.TagString(buffer=bbuff).value
ench = nbt.TagCompound(buffer=bbuff)
ench.name = name
slot['enchants'] = ench
return slot
def pack_slot(slot):
o = pack(MC_SHORT, slot['id'])
if slot['id'] != -1:
o += pack(MC_BYTE, slot['amount'])
o += pack(MC_SHORT, slot['damage'])
if 'enchants' in slot:
ench = slot['enchants']
bbuff = BoundBuffer()
nbt.TagByte(ench.id)._render_buffer(bbuff)
nbt.TagString(ench.name)._render_buffer(bbuff)
ench._render_buffer(bbuff)
o += bbuff.flush()
else:
o += pack(MC_BYTE, 0)
return o
# Metadata is a dictionary list thing that
# holds metadata about entities.
metadata_lookup = MC_BYTE, MC_SHORT, MC_INT, MC_FLOAT, MC_STRING, MC_SLOT
def unpack_metadata(bbuff):
metadata = {}
head = unpack(MC_UBYTE, bbuff)
while head != 0x7F:
key = head & 0x1F # Lower 5 bits
typ = head >> 5 # Upper 3 bits
assert key not in metadata, 'k=%s t=%s' % (key, typ)
if 0 <= typ < len(metadata_lookup):
val = unpack(metadata_lookup[typ], bbuff)
elif typ == 6:
x = unpack(MC_INT, bbuff)
y = unpack(MC_INT, bbuff)
z = unpack(MC_INT, bbuff)
val = [x, y, z]
elif typ == 7:
pitch = unpack(MC_FLOAT, bbuff)
yaw = unpack(MC_FLOAT, bbuff)
roll = unpack(MC_FLOAT, bbuff)
val = [pitch, yaw, roll]
else:
return None
metadata[key] = (typ, val)
head = unpack(MC_UBYTE, bbuff)
return metadata
def pack_metadata(data):
o = b''
for key, (typ, val) in data.items():
o += pack(MC_UBYTE, (typ << 5) | key)
if 0 <= typ < len(metadata_lookup):
o += pack(metadata_lookup[typ], val)
elif typ == 6:
for i in range(3):
o += pack(MC_INT, val[i])
elif typ == 7:
for i in range(3):
o += pack(MC_FLOAT, val[i])
else:
return None
o += pack(MC_BYTE, 0x7F)
return o
endian = '>'
def unpack(data_type, bbuff):
if data_type < len(proto.data_structs):
format = proto.data_structs[data_type]
return struct.unpack(endian + format[0], bbuff.recv(format[1]))[0]
elif data_type == MC_VARINT:
return unpack_varint(bbuff)
elif data_type == MC_VARLONG:
return unpack_varlong(bbuff)
elif data_type == MC_FP_INT:
return unpack_fixed_point(MC_INT, bbuff)
elif data_type == MC_FP_BYTE:
return unpack_fixed_point(MC_BYTE, bbuff)
elif data_type == MC_UUID:
a, b = struct.unpack('>QQ', bbuff.recv(16))
return (a << 64) | b
elif data_type == MC_POSITION:
return unpack_position(bbuff)
elif data_type == MC_STRING:
return bbuff.recv(unpack(MC_VARINT, bbuff)).decode('utf-8', 'replace')
elif data_type == MC_CHAT:
return json.loads(unpack(MC_STRING, bbuff))
elif data_type == MC_SLOT:
return unpack_slot(bbuff)
elif data_type == MC_META:
return unpack_metadata(bbuff)
else:
return None
def pack(data_type, data):
if data_type < len(proto.data_structs):
format = proto.data_structs[data_type]
return struct.pack(endian + format[0], data)
elif data_type == MC_VARINT:
return pack_varint(data)
elif data_type == MC_VARLONG:
return pack_varlong(data)
elif data_type == MC_FP_INT:
return pack_fixed_point(MC_INT, data)
elif data_type == MC_FP_BYTE:
return pack_fixed_point(MC_BYTE, data)
elif data_type == MC_UUID:
return struct.pack('>QQ', (data >> 64) & ((1 << 64) - 1),
data & ((1 << 64) - 1))
elif data_type == MC_POSITION:
return pack_position(data)
elif data_type == MC_STRING:
data = data.encode('utf-8', 'replace')
return pack(MC_VARINT, len(data)) + data
elif data_type == MC_CHAT:
return pack(MC_STRING, json.dumps(data))
elif data_type == MC_SLOT:
return pack_slot(data)
elif data_type == MC_META:
return pack_metadata(data)
else:
return None