-
Notifications
You must be signed in to change notification settings - Fork 0
/
Packet.py
142 lines (112 loc) · 3.91 KB
/
Packet.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
import struct
class Packet:
def __init__(self):
self.data = bytearray()
self.index = 0
self.send = True
def reset(self):
self.index = 0
self.data = bytearray()
def advance(self, amt):
self.index += amt
return self.index
def read_int32(self):
t = struct.unpack(">i", self.data[self.index:self.advance(4)])
return t[0]
def write_int64(self, i):
d = struct.pack(">q", i)
self.data[self.index:self.advance(8)] = d
def read_int64(self):
t = struct.unpack(">q", self.data[self.index:self.advance(8)])
return t[0]
def write_uint64(self, i):
d = struct.pack(">Q", i)
self.data[self.index:self.advance(8)] = d
def read_uint64(self):
t = struct.unpack(">Q", self.data[self.index:self.advance(8)])
return t[0]
def write_int32(self, i):
d = struct.pack(">i", i)
self.data[self.index:self.advance(4)] = d
def read_uint32(self):
t = struct.unpack(">I", self.data[self.index:self.advance(4)])
return t[0]
def write_uint32(self, i):
d = struct.pack(">I", i)
self.data[self.index:self.advance(4)] = d
def read_int16(self):
t = struct.unpack(">h", self.data[self.index:self.advance(2)])
return t[0]
def write_int16(self, i):
d = struct.pack(">h", i)
self.data[self.index:self.advance(2)] = d
def read_uint16(self):
t = struct.unpack(">H", self.data[self.index:self.advance(2)])
return t[0]
def write_uint16(self, i):
d = struct.pack(">H", i)
self.data[self.index:self.advance(2)] = d
def write_byte(self, i):
d = struct.pack(">b", i)
self.data[self.index:self.advance(1)] = d
def read_byte(self):
t = struct.unpack(">b", self.data[self.index:self.advance(1)])
return t[0]
def write_unsignedbyte(self, i):
d = struct.pack(">B", i)
self.data[self.index:self.advance(1)] = d
def read_unsignedbyte(self):
t = struct.unpack(">B", self.data[self.index:self.advance(1)])
return t[0]
def read_float(self):
t = struct.unpack(">f", self.data[self.index:self.advance(4)])
return t[0]
def write_float(self, f):
d = struct.pack(">f", f)
self.data[self.index:self.advance(4)] = d
def read_boolean(self):
d = struct.unpack(">?", self.data[self.index:self.advance(1)])
return d[0]
def write_boolean(self, bool):
d = struct.pack(">?", bool)
self.data[self.index:self.advance(1)] = d
def write_string(self, string):
length = len(string)
self.write_uint16(length)
if length == 0:
return
self.data[self.index:self.advance(length)] = struct.pack(f">{length}s", string.encode("utf8"))
def read_string(self):
length = self.read_uint16()
if length == 0:
return ""
d = struct.unpack(f">{length}s", self.data[self.index:self.advance(length)])[0].decode(
"utf8") # convert the byte from byte type to int/String type
return d
def write_bytestring(self, bytes):
length = len(bytes)
self.write_int16(length)
if length == 0:
return
self.data[self.index:self.advance(length)] = bytes
def read_bytestring(self):
length = self.read_int16()
if length == 0:
return b''
d = bytes(self.data[self.index:self.advance(length)])
return d
def write_booleanarray(self, bytes):
length = len(bytes)
self.write_int32(length)
if length == 0:
return
for i in bytes:
self.write_boolean(i)
def read_booleanarray(self):
length = self.read_int16()
if length == 0:
return []
d = []
for _ in range(length):
d.append(self.read_boolean())
return d