forked from ammaraskar/pyCraft
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtypes.py
255 lines (191 loc) · 6.01 KB
/
types.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
"""Contains definitions for minecraft's different data types
Each type has a method which is used to read and write it.
These definitions and methods are used by the packet definitions
"""
import struct
import uuid
from collections import namedtuple
class Type(object):
__slots__ = ()
@staticmethod
def read(file_object):
raise NotImplementedError("Base data type not serializable")
@staticmethod
def send(value, socket):
raise NotImplementedError("Base data type not serializable")
# =========================================================
class Boolean(Type):
@staticmethod
def read(file_object):
return struct.unpack('?', file_object.read(1))[0]
@staticmethod
def send(value, socket):
socket.send(struct.pack('?', value))
class UnsignedByte(Type):
@staticmethod
def read(file_object):
return struct.unpack('>B', file_object.read(1))[0]
@staticmethod
def send(value, socket):
socket.send(struct.pack('>B', value))
class Byte(Type):
@staticmethod
def read(file_object):
return struct.unpack('>b', file_object.read(1))[0]
@staticmethod
def send(value, socket):
socket.send(struct.pack('>b', value))
class Short(Type):
@staticmethod
def read(file_object):
return struct.unpack('>h', file_object.read(2))[0]
@staticmethod
def send(value, socket):
socket.send(struct.pack('>h', value))
class UnsignedShort(Type):
@staticmethod
def read(file_object):
return struct.unpack('>H', file_object.read(2))[0]
@staticmethod
def send(value, socket):
socket.send(struct.pack('>H', value))
class Integer(Type):
@staticmethod
def read(file_object):
return struct.unpack('>i', file_object.read(4))[0]
@staticmethod
def send(value, socket):
socket.send(struct.pack('>i', value))
class VarInt(Type):
@staticmethod
def read(file_object):
number = 0
# Limit of 5 bytes, otherwise its possible to cause
# a DOS attack by sending VarInts that just keep
# going
bytes_encountered = 0
while True:
byte = file_object.read(1)
if len(byte) < 1:
raise EOFError("Unexpected end of message.")
byte = ord(byte)
number |= (byte & 0x7F) << 7 * bytes_encountered
if not byte & 0x80:
break
bytes_encountered += 1
if bytes_encountered > 5:
raise ValueError("Tried to read too long of a VarInt")
return number
@staticmethod
def send(value, socket):
out = bytes()
while True:
byte = value & 0x7F
value >>= 7
out += struct.pack("B", byte | (0x80 if value > 0 else 0))
if value == 0:
break
socket.send(out)
@staticmethod
def size(value):
for max_value, size in VARINT_SIZE_TABLE.items():
if value < max_value:
return size
raise ValueError("Integer too large")
# Maps (maximum integer value -> size of VarInt in bytes)
VARINT_SIZE_TABLE = {
2 ** 7: 1,
2 ** 14: 2,
2 ** 21: 3,
2 ** 28: 4,
2 ** 35: 5,
2 ** 42: 6,
2 ** 49: 7,
2 ** 56: 8,
2 ** 63: 9,
2 ** 70: 10,
2 ** 77: 11,
2 ** 84: 12
}
class Long(Type):
@staticmethod
def read(file_object):
return struct.unpack('>q', file_object.read(8))[0]
@staticmethod
def send(value, socket):
socket.send(struct.pack('>q', value))
class UnsignedLong(Type):
@staticmethod
def read(file_object):
return struct.unpack('>Q', file_object.read(8))[0]
@staticmethod
def send(value, socket):
socket.send(struct.pack('>Q', value))
class Float(Type):
@staticmethod
def read(file_object):
return struct.unpack('>f', file_object.read(4))[0]
@staticmethod
def send(value, socket):
socket.send(struct.pack('>f', value))
class Double(Type):
@staticmethod
def read(file_object):
return struct.unpack('>d', file_object.read(8))[0]
@staticmethod
def send(value, socket):
socket.send(struct.pack('>d', value))
class ShortPrefixedByteArray(Type):
@staticmethod
def read(file_object):
length = Short.read(file_object)
return struct.unpack(str(length) + "s", file_object.read(length))[0]
@staticmethod
def send(value, socket):
Short.send(len(value), socket)
socket.send(value)
class VarIntPrefixedByteArray(Type):
@staticmethod
def read(file_object):
length = VarInt.read(file_object)
return struct.unpack(str(length) + "s", file_object.read(length))[0]
@staticmethod
def send(value, socket):
VarInt.send(len(value), socket)
socket.send(struct.pack(str(len(value)) + "s", value))
class String(Type):
@staticmethod
def read(file_object):
length = VarInt.read(file_object)
return file_object.read(length).decode("utf-8")
@staticmethod
def send(value, socket):
value = value.encode('utf-8')
VarInt.send(len(value), socket)
socket.send(value)
class UUID(Type):
@staticmethod
def read(file_object):
return str(uuid.UUID(bytes=file_object.read(16)))
@staticmethod
def send(value, socket):
socket.send(uuid.UUID(value).bytes)
class Position(Type, namedtuple('Position', ('x', 'y', 'z'))):
__slots__ = ()
@staticmethod
def read(file_object):
location = UnsignedLong.read(file_object)
x = int(location >> 38)
y = int((location >> 26) & 0xFFF)
z = int(location & 0x3FFFFFF)
if x >= pow(2, 25):
x -= pow(2, 26)
if y >= pow(2, 11):
y -= pow(2, 12)
if z >= pow(2, 25):
z -= pow(2, 26)
return Position(x=x, y=y, z=z)
@staticmethod
def send(x, y, z, socket):
value = ((x & 0x3FFFFFF) << 38) | ((y & 0xFFF) << 26) | (z & 0x3FFFFFF)
UnsignedLong.send(value, socket)