-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnetcomp.pyx
147 lines (107 loc) · 9.51 KB
/
netcomp.pyx
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
class Match:
def __init__(self, start, end, offset):
self.start = start
self.end = end
self.offset = offset
def overlaps(self, m):
return not (self.start >= m.end or self.end <= m.start)
def contains(self, p):
return not (self.start > p or self.end <= p)
def getLength(self):
return self.end - self.start
def getEndOffset(self):
return self.offset + self.getLength()
class MatchFinder:
def __init__(self, buf):
self.buf = buf
self.matches = []
def addMatches(self, buf):
windowSize = 20
windowStart = 0
windowEnd = min(windowSize, len(self.buf))
for i in range(len(buf)):
if len(self.matches) > 0 and not self.matches[-1].contains(i):
matchEnd = self.matches[-1].getEndOffset()
windowStart = max(matchEnd - windowSize, 0)
windowEnd = min(matchEnd + windowSize, len(self.buf))
for j in range(windowStart, windowEnd):
j = self.buf.find(buf[i], j, windowEnd)
if j == -1:
break
k = 1
while i + k < len(buf) and j + k < len(self.buf):
if buf[i + k] != self.buf[j + k]:
break
k += 1
if k >= 3:
self.addMatch(Match(i, i + k, j))
def addMatch(self, match):
if len(self.matches) > 0 and self.matches[-1].overlaps(match):
if match.getLength() <= self.matches[-1].getLength():
return False
self.matches[-1] = match
return True
self.matches.append(match)
return True
class OffsetPair:
def __init__(self, a, b):
self.a = a
self.b = b
def write(self, v):
if v < 0xf:
return []
out = []
c = 0
while v >= 0xff << (c * 8):
out.append(0xff)
if c == 3:
out.append(0)
break
c += 1
i = c
while i >= 0:
out.append((v >> (i * 8)) & 0xff)
i -= 1
return out
def encode(self):
first = self.a << 4 if self.a < 0xf else 0xf0
first |= self.b if self.b < 0xf else 0xf
return bytes([first] + self.write(self.a) + self.write(self.b))
class PacketCompressor:
def __init__(self):
self.lastPacket = None
def compress(self, packet):
if self.lastPacket is None:
self.lastPacket = packet
return packet
finder = MatchFinder(self.lastPacket)
finder.addMatches(packet)
readPos = 0
writePos = 0
out = b''
for m in finder.matches:
if writePos < m.start:
matchLength = m.start - writePos
out += OffsetPair(matchLength - 1, 0).encode()
out += packet[writePos:writePos + matchLength]
writePos = m.start
matchOffset = m.offset - readPos
if matchOffset < 0:
matchOffset = 127 - matchOffset
out += OffsetPair(matchOffset, m.getLength() - 2).encode()
readPos = m.offset + m.getLength()
writePos += m.getLength()
if writePos < len(packet):
matchLength = len(packet) - writePos
out += OffsetPair(matchLength - 1, 0).encode()
out += packet[writePos:writePos + matchLength]
self.lastPacket = packet
return out
if 1:
pc = PacketCompressor()
pc.compress(b'\x80\x04\x95:\x05\x00\x00\x00\x00\x00\x00\x8c\tgamestate\x94\x8c\tGameState\x94\x93\x94)\x81\x94}\x94(\x8c\x07objects\x94}\x94(J\xff\xff\xff\xff\x8c\x0cplayerobject\x94\x8c\x06Player\x94\x93\x94)\x81\x94}\x94(\x8c\x01x\x94G@7\x00\x00\x00\x00\x00\x00\x8c\x01y\x94G@R\x80\x00\x00\x00\x00\x00\x8c\x04tile\x94\x8c\x011\x94\x8c\x04xdir\x94K\x00\x8c\x04ydir\x94K\x00\x8c\x07facedir\x94K\x00\x8c\x05speed\x94K\x01\x8c\x05width\x94K\x08\x8c\x06height\x94K\x08\x8c\x08playerid\x94K\x00\x8c\nplayertile\x94K\x01\x8c\x04tick\x94M\x94\x0b\x8c\x04anim\x94K\x00\x8c\nis_walking\x94\x89\x8c\x06status\x94\x8c\x05ALIVE\x94\x8c\ndeath_time\x94K\x00\x8c\rtime_to_alive\x94K\x02\x8c\x05old_x\x94G@7\x00\x00\x00\x00\x00\x00\x8c\x05old_y\x94G@R\x80\x00\x00\x00\x00\x00\x8c\tkick_mode\x94\x89\x8c\nkick_angle\x94K\x00K\x00\x86\x94ubJ\xfe\xff\xff\xff\x8c\x04ball\x94\x8c\x04Ball\x94\x93\x94)\x81\x94}\x94(h\x0cG@U\x90\xfd\xaaY\xeb\xech\rG@[`\x85\x82\x14aPh\x0e\x8c\x01o\x94h\x10G\x00\x00\x00\x00\x00\x00\x00\x00h\x11G\x00\x00\x00\x00\x00\x00\x00\x00h\x12K\x00h\x13K\x01h\x14K\x05h\x15K\x04\x8c\x01z\x94K\x00\x8c\x04zdir\x94G\x00\x00\x00\x00\x00\x00\x00\x00ubJ\xfd\xff\xff\xff\x8c\x04bird\x94\x8c\x04Bird\x94\x93\x94)\x81\x94}\x94(\x8c\tSPEED_DIV\x94K\x10\x8c\x1bSPEED_COLLISION_MULT_GROUND\x94KP\x8c\x0bWALL_HEIGHT\x94K\x0ch\x0cG@g \x00\x00\x00\x00\x00h\rG@_\xe0\x00\x00\x00\x00\x00h\x0e\x8c\x01v\x94h\x10J\xf6\xff\xff\xffh\x11K\nh\x12K\x00h\x13K\x01h\x14K\x06h\x15K\x02h*K\x00\x8c\x04size\x94K\x08h+K\x0fubJ\xebe\x0b\x00\x8c\x04worm\x94\x8c\x04Worm\x94\x93\x94)\x81\x94}\x94(h\x0cK\x13h\rK\th\x0eNh\x10J\xff\xff\xff\xffh\x11K\x00h\x12K\x00h\x13K\x01h\x14K\x08h\x15K\x08\x8c\x04head\x94]\x94(K\x13K\te\x8c\x04body\x94]\x94(]\x94(K\x14K\te]\x94(K\x15K\te]\x94(K\x16K\te]\x94(K\x16K\x08e]\x94(K\x16K\x07e]\x94(K\x16K\x06e]\x94(K\x15K\x06e]\x94(K\x14K\x06e]\x94(K\x13K\x06e]\x94(K\x12K\x06e]\x94(K\x11K\x06e]\x94(K\x10K\x06e]\x94(K\x10K\x06e]\x94(K\x0fK\x06e]\x94(K\x0fK\x06e]\x94(K\x0eK\x06ee\x8c\x05tiles\x94]\x94(\x8c\x01H\x94\x8c\x01B\x94e\x8c\x0elast_move_time\x94GA\xd8.=I\r\xaf\x19\x8c\tmove_time\x94G?\xc9\x99\x99\x99\x99\x99\x9a\x8c\x13steps_till_addition\x94K\x01\x8c\x0cstep_counter\x94K\x0c\x8c\x08move_dir\x94]\x94(K\x00K\x00e\x8c\tdebugList\x94]\x94\x8c\n_MOVEMENTS\x94]\x94(]\x94(J\xff\xff\xff\xffK\x00e]\x94(K\x01K\x00e]\x94(K\x00J\xff\xff\xff\xffe]\x94(K\x00K\x01ee\x8c\x08_dir_ops\x94]\x94(K\x02K\x01K\x04K\x03e\x8c\x05state\x94h\x1c\x8c\rtime_of_death\x94GA\xd8.=Fz7\n\x8c\x0ftime_to_respawn\x94G?\xf8\x00\x00\x00\x00\x00\x00\x8c\x0cgrow_counter\x94K\x00ubJ\x99\xe1\x07\x00h\t)\x81\x94}\x94(h\x0cG@T`\x00\x00\x00\x00\x00h\rG@Z\xa0\x00\x00\x00\x00\x00h\x0e\x8c\x012\x94h\x10K\x00h\x11K\x01h\x12K\x00h\x13K\x01h\x14K\x08h\x15K\x08h\x16K\x01h\x17K\x02h\x18M\xc7\x05h\x19K\x02h\x1a\x88h\x1bh\x1ch\x1dK\x00h\x1eK\x02h\x1fG@T`\x00\x00\x00\x00\x00h G@Z\x80\x00\x00\x00\x00\x00h!\x88h"G?\xef\xff\xf9xs\xee\x02G?dq8.3U\xd2\x86\x94ubu\x8c\tlevelname\x94\x8c\x04LEV3\x94\x8c\x06points\x94K\x00ub.')
import time
start = time.time()
res = pc.compress(b'\x80\x04\x95:\x05\x00\x00\x00\x00\x00\x00\x8c\tgamestate\x94\x8c\tGameState\x94\x93\x94)\x81\x94}\x94(\x8c\x07objects\x94}\x94(J\xff\xff\xff\xff\x8c\x0cplayerobject\x94\x8c\x06Player\x94\x93\x94)\x81\x94}\x94(\x8c\x01x\x94G@7\x00\x00\x00\x00\x00\x00\x8c\x01y\x94G@S@\x00\x00\x00\x00\x00\x8c\x04tile\x94\x8c\x011\x94\x8c\x04xdir\x94K\x00\x8c\x04ydir\x94K\x01\x8c\x07facedir\x94K\x00\x8c\x05speed\x94K\x01\x8c\x05width\x94K\x08\x8c\x06height\x94K\x08\x8c\x08playerid\x94K\x00\x8c\nplayertile\x94K\x01\x8c\x04tick\x94M\x98\x0b\x8c\x04anim\x94K\x01\x8c\nis_walking\x94\x88\x8c\x06status\x94\x8c\x05ALIVE\x94\x8c\ndeath_time\x94K\x00\x8c\rtime_to_alive\x94K\x02\x8c\x05old_x\x94G@7\x00\x00\x00\x00\x00\x00\x8c\x05old_y\x94G@S\x00\x00\x00\x00\x00\x00\x8c\tkick_mode\x94\x89\x8c\nkick_angle\x94K\x00K\x00\x86\x94ubJ\xfe\xff\xff\xff\x8c\x04ball\x94\x8c\x04Ball\x94\x93\x94)\x81\x94}\x94(h\x0cG@U\x90\xfd\xaaY\xeb\xech\rG@[`\x85\x82\x14aPh\x0e\x8c\x01o\x94h\x10G\x00\x00\x00\x00\x00\x00\x00\x00h\x11G\x00\x00\x00\x00\x00\x00\x00\x00h\x12K\x00h\x13K\x01h\x14K\x05h\x15K\x04\x8c\x01z\x94K\x00\x8c\x04zdir\x94G\x00\x00\x00\x00\x00\x00\x00\x00ubJ\xfd\xff\xff\xff\x8c\x04bird\x94\x8c\x04Bird\x94\x93\x94)\x81\x94}\x94(\x8c\tSPEED_DIV\x94K\x10\x8c\x1bSPEED_COLLISION_MULT_GROUND\x94KP\x8c\x0bWALL_HEIGHT\x94K\x0ch\x0cG@f\xd0\x00\x00\x00\x00\x00h\rG@`@\x00\x00\x00\x00\x00h\x0e\x8c\x01v\x94h\x10J\xf6\xff\xff\xffh\x11K\nh\x12K\x00h\x13K\x01h\x14K\x06h\x15K\x02h*K\x00\x8c\x04size\x94K\x08h+K\x0fubJ\xebe\x0b\x00\x8c\x04worm\x94\x8c\x04Worm\x94\x93\x94)\x81\x94}\x94(h\x0cK\x12h\rK\th\x0eNh\x10J\xff\xff\xff\xffh\x11K\x00h\x12K\x00h\x13K\x01h\x14K\x08h\x15K\x08\x8c\x04head\x94]\x94(K\x12K\te\x8c\x04body\x94]\x94(]\x94(K\x13K\te]\x94(K\x14K\te]\x94(K\x15K\te]\x94(K\x16K\te]\x94(K\x16K\x08e]\x94(K\x16K\x07e]\x94(K\x16K\x06e]\x94(K\x15K\x06e]\x94(K\x14K\x06e]\x94(K\x13K\x06e]\x94(K\x12K\x06e]\x94(K\x11K\x06e]\x94(K\x10K\x06e]\x94(K\x10K\x06e]\x94(K\x0fK\x06e]\x94(K\x0fK\x06ee\x8c\x05tiles\x94]\x94(\x8c\x01H\x94\x8c\x01B\x94e\x8c\x0elast_move_time\x94GA\xd8.=I\x1bK6\x8c\tmove_time\x94G?\xc9\x99\x99\x99\x99\x99\x9a\x8c\x13steps_till_addition\x94K\x01\x8c\x0cstep_counter\x94K\r\x8c\x08move_dir\x94]\x94(K\x00K\x00e\x8c\tdebugList\x94]\x94\x8c\n_MOVEMENTS\x94]\x94(]\x94(J\xff\xff\xff\xffK\x00e]\x94(K\x01K\x00e]\x94(K\x00J\xff\xff\xff\xffe]\x94(K\x00K\x01ee\x8c\x08_dir_ops\x94]\x94(K\x02K\x01K\x04K\x03e\x8c\x05state\x94h\x1c\x8c\rtime_of_death\x94GA\xd8.=Fz7\n\x8c\x0ftime_to_respawn\x94G?\xf8\x00\x00\x00\x00\x00\x00\x8c\x0cgrow_counter\x94K\x00ubJ\x99\xe1\x07\x00h\t)\x81\x94}\x94(h\x0cG@T`\x00\x00\x00\x00\x00h\rG@[ \x00\x00\x00\x00\x00h\x0e\x8c\x012\x94h\x10K\x00h\x11K\x01h\x12K\x00h\x13K\x01h\x14K\x08h\x15K\x08h\x16K\x01h\x17K\x02h\x18M\xcb\x05h\x19K\x01h\x1a\x88h\x1bh\x1ch\x1dK\x00h\x1eK\x02h\x1fG@T`\x00\x00\x00\x00\x00h G@[\x00\x00\x00\x00\x00\x00h!\x88h"G?\xebQ\xa0\xe7\xe2\x06jG\xbf\xe0\xa9\xea\x08LW\xcb\x86\x94ubu\x8c\tlevelname\x94\x8c\x04LEV3\x94\x8c\x06points\x94K\x00ub.')
print(len(res), res)
print('time', (time.time() - start) * 1000, 'ms')