-
Notifications
You must be signed in to change notification settings - Fork 0
/
GameTester.py
458 lines (371 loc) · 12.2 KB
/
GameTester.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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
# Copyright (C) 2015 and beyond by Jeremiah Morris
# and contributing developers.
#
# This file is part of Metaserver.
#
# Metaserver is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Metaserver is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Metaserver. If not, see <http://www.gnu.org/licenses/>.
import struct
from twisted.internet.protocol import ClientFactory, DatagramProtocol
from twisted.internet import reactor, task
from MetaProtocol import MetaProtocol
from MetaPackets import unpack_strings
import random
import crcmod
### packets
class HelloPacket:
code = 700
def __init__(self, data):
(self.version, stringlen) = unpack_strings(data)
class JoinerInfoPacket:
code = 701
_fmt_start = struct.Struct('>H')
_fmt_end = struct.Struct('>HH')
def __init__(self, stream_id, player_name, version, color, team):
self.data = self._fmt_start.pack(stream_id)
self.data += player_name + b'\x00'
self.data += version + b'\x00'
self.data += self._fmt_end.pack(color, team)
class JoinPlayerPacket:
code = 702
def __init__(self, data):
pass
class CapabilitiesPacket:
code = 703
_fmt_value = struct.Struct('>L')
def __init__(self, data):
self.data = data
self.capabilities = {}
offset = 0
max = len(data) - 4
while offset < max:
pos = data.find(b'\x00', offset, max)
if pos < 0:
offset = max
else:
key = data[offset:pos].decode('mac_roman')
val, = self._fmt_value.unpack_from(data, pos + 1)
self.capabilities[key] = val
offset = pos + 5
class AcceptJoinPacket:
code = 704
_fmt_start = struct.Struct('>BLHLHHHB')
_fmt_end = struct.Struct('>HHH10s')
def __init__(self, player_name):
# accepted, dspAddress.host, dspAddress.port, ddpAddress.host, ddpAddress.port,
# identifier, stream_id, net_dead, player_name, desired_color, team, color, serial_number
self.data = self._fmt_start.pack(1, 0, 0, 0, 0, 0, 0, 0)
self.data += player_name + b'\x00'
self.data += self._fmt_end.pack(0, 0, 0, b'')
class TopologyPacket:
code = 705
def __init__(self, data):
pass
class MapPacket:
code = 706
def __init__(self, data):
pass
class PhysicsPacket:
code = 707
def __init__(self, data):
pass
class LuaPacket:
code = 708
def __init__(self, data):
pass
class NetworkChatPacket:
code = 709
_fmt = struct.Struct('>HHH')
def __init__(self, data):
self.sender_id, self.target, self.target_id, = self._fmt.unpack_from(data)
self.message, stringlen = unpack_strings(data, 1, self._fmt.size)
class EndGameDataPacket:
code = 711
def __init__(self, data):
pass
class ChangeColorsPacket:
code = 712
class ServerWarningPacket:
code = 713
class ClientInfoPacket:
code = 714
_fmt = struct.Struct('>HHHH')
def __init__(self, data):
self.stream_id, self.action, self.color, self.team, = self._fmt.unpack_from(data)
self.player_name, stringlen = unpack_strings(data, 1, self._fmt.size)
class ZippedMapPacket:
code = 715
def __init__(self, data):
pass
class ZippedPhysicsPacket:
code = 716
def __init__(self, data):
pass
class ZippedLuaPacket:
code = 717
def __init__(self, data):
pass
class NetworkStatsPacket:
code = 718
def __init__(self, data):
pass
class GameSessionPacket:
code = 719
def __init__(self, data):
pass
### connector
class JoinerConnector(MetaProtocol):
_join_recv_packets = [
HelloPacket,
CapabilitiesPacket,
ServerWarningPacket,
ClientInfoPacket,
NetworkChatPacket,
JoinPlayerPacket,
GameSessionPacket,
TopologyPacket,
MapPacket,
PhysicsPacket,
LuaPacket,
ZippedMapPacket,
ZippedPhysicsPacket,
ZippedLuaPacket,
EndGameDataPacket,
NetworkStatsPacket
]
def __init__(self, tester):
MetaProtocol.__init__(self)
self.tester = tester
self.player_name = b'joinerbot'
self.color = 0
self.team = 0
self.connected = False
self.succeeded = False
def connectionMade(self):
MetaProtocol.connectionMade(self)
self.tester.joinConnectSucceeded(self)
self.connected = True
def connectionLost(self, reason):
if self.connected and not self.succeeded:
self.tester.joinDisconnected(self, reason)
pass
def messageAllowed(self, code):
for p in self._join_recv_packets:
if p.code == code:
return True
return False
def packMessage(self, code, data):
for p in self._join_recv_packets:
if p.code == code:
return p(data)
return None
def packetReceived(self, packet):
if isinstance(packet, HelloPacket):
self.tester.joinGotHello(self)
self.sendPacket(JoinerInfoPacket(0, self.player_name, packet.version, self.color, self.team))
return True
if isinstance(packet, CapabilitiesPacket):
# self.sendPacket(CapabilitiesPacket(packet.data))
# return True
self.tester.joinGotCapabilities(self, packet)
return False
if isinstance(packet, ClientInfoPacket):
return True
if isinstance(packet, JoinPlayerPacket):
self.sendPacket(AcceptJoinPacket(self.player_name))
return True
if isinstance(packet, GameSessionPacket):
return True
if isinstance(packet, TopologyPacket):
return True
if isinstance(packet, NetworkChatPacket):
return True
if isinstance(packet, MapPacket):
return True
if isinstance(packet, PhysicsPacket):
return True
if isinstance(packet, LuaPacket):
return True
if isinstance(packet, ZippedMapPacket):
return True
if isinstance(packet, ZippedPhysicsPacket):
return True
if isinstance(packet, ZippedLuaPacket):
return True
if isinstance(packet, EndGameDataPacket):
return True
if isinstance(packet, NetworkStatsPacket):
return True
return False
class JoinerConnectorFactory(ClientFactory):
def __init__(self, tester):
self.tester = tester
def startedConnecting(self, connector):
pass
def buildProtocol(self, addr):
return JoinerConnector(self.tester)
def clientConnectionLost(self, connector, reason):
pass
def clientConnectionFailed(self, connector, reason):
self.tester.joinConnectFailed(reason)
## UDP connector
class GameConnector(DatagramProtocol):
def __init__(self, tester, host, port):
self.tester = tester
self.gather_host = host
self.gather_port = port
def startProtocol(self):
self.ping_id = int(random.uniform(1, 65535))
self.ping_failed = 0
self.timeout = reactor.callLater(0.35, self.pingTimeout)
self.sendPing(self.ping_id)
def pingTimeout(self):
self.timeout = None
if self.ping_failed < 2:
self.ping_failed += 1
self.sendPing(self.ping_id)
self.timeout = reactor.callLater(0.35, self.pingTimeout)
else:
self.stopRetrying()
self.tester.gameConnectFailed(self)
def stopRetrying(self):
if self.timeout:
self.timeout.cancel()
self.timeout = None
def connectionRefused(self):
self.tester.gameConnectFailed(self)
def datagramReceived(self, data, addr_tuple):
(host, port) = addr_tuple
_fmt = struct.Struct('>2sH')
_magic, _crc, = _fmt.unpack_from(data)
_dataOffset = _fmt.size
crc16 = crcmod.predefined.Crc('crc-ccitt-false')
crc16.update(_magic)
crc16.update(b'\x00\x00')
crc16.update(data[_dataOffset:])
if crc16.crcValue != _crc:
self.stopRetrying()
self.tester.gamePacketFailed(self, "CRC failed")
return
if _magic != b'PR':
self.stopRetrying()
self.tester.gamePacketFailed(self, "bad packet type")
return
_fmt2 = struct.Struct('>H')
_ping_id, = _fmt2.unpack_from(data, _dataOffset)
if _ping_id != self.ping_id:
self.stopRetrying()
self.tester.gamePacketFailed(self, "wrong ping ID")
return
self.stopRetrying()
self.tester.gamePingSucceeded(self)
def sendPing(self, ping_id):
_magic = b'PQ'
_payload = struct.Struct('>H').pack(ping_id)
crc16 = crcmod.predefined.Crc('crc-ccitt-false')
crc16.update(_magic)
crc16.update(b'\x00\x00')
crc16.update(_payload)
_data = _magic + crc16.digest() + _payload
self.transport.write(_data, (self.gather_host, self.gather_port))
class GameTester:
def __init__(self, host, port):
self.test_host = host
self.test_port = port
self.message_callback = None
self.finished_callback = None
self.cancelled = False
self.warned = False
self.join_running = False
self.game_running = False
def setMessageCallback(self, cb=None):
self.message_callback = cb
def setFinishedCallback(self, cb=None):
self.finished_callback = cb
def run(self):
self.join_running = True
reactor.connectTCP(self.test_host, self.test_port, JoinerConnectorFactory(self), 3)
def cancel(self):
if not self.cancelled:
self.cancelled = True
self.finished_callback = None
self.message_callback = None
def _finished(self, success):
if self.finished_callback is not None:
self.finished_callback(self, success, self.warned)
self.finished_callback = None
self.message_callback = None
def _sendMessage(self, msg):
if self.message_callback is not None:
self.message_callback(self, msg)
def joinConnectFailed(self, reason):
if self.join_running:
self._sendMessage("TCP connection to port %d failed." % (self.test_port))
self._finished(False)
def joinDisconnected(self, connector, reason):
if self.join_running:
self._sendMessage("Bad response on TCP port %d." % (self.test_port))
self._sendMessage(reason.getErrorMessage())
self._finished(False)
def joinConnectSucceeded(self, connector):
# if self.join_running:
# self._sendMessage("TCP connection to port %d succeeded." % (self.test_port))
pass
def joinGotHello(self, connector):
# if self.join_running:
# self._sendMessage("Hello packet received.")
pass
def joinGotCapabilities(self, connector, packet):
if self.join_running:
# self._sendMessage("Capabilities packet received.")
self.join_running = False
caps = packet.capabilities
self.testCapabilities(caps)
if "Gameworld" in caps and caps["Gameworld"] >= 3 and "Star" in caps:
self.testUDP()
else:
self._finished(True)
def testCapabilities(self, caps):
if "Ring" in caps and "Star" not in caps:
self._sendMessage("The ring protocol is deprecated. Please use star.")
self.warned = True
elif ("Gameworld" not in caps or caps["Gameworld"] < 2 or
"Lua" not in caps or caps["Lua"] < 2 or
"Star" not in caps or caps["Star"] < 6 or
"ZippedData" not in caps or caps["ZippedData"] < 1):
self._sendMessage("You are not network compatible with the latest version.")
self.warned = True
def testUDP(self):
self.game_running = True
gc = GameConnector(self, self.test_host, self.test_port)
reactor.listenUDP(0, gc)
def gameConnectSucceeded(self, connector):
# if self.game_running:
# self._sendMessage("UDP connection to port %d succeeded." % (self.test_port))
pass
def gameConnectFailed(self, connector):
if self.game_running:
self._sendMessage("UDP connection to port %d failed." % (self.test_port))
self.game_running = False
self._finished(False)
def gamePacketFailed(self, connector, reason):
if self.game_running:
self._sendMessage("Bad response on UDP port %d." % (self.test_port))
self.game_running = False
connector.transport.stopListening()
self._finished(False)
def gamePingSucceeded(self, connector):
if self.game_running:
# self._sendMessage("UDP ping to port %d succeeded." % (self.test_port))
self.game_running = False
self._finished(True)