This repository has been archived by the owner on Jan 7, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RemoteProcessClient.py
729 lines (592 loc) · 27.8 KB
/
RemoteProcessClient.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
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
import _socket
import struct
from model.Facility import Facility
from model.FacilityType import FacilityType
from model.Game import Game
from model.Player import Player
from model.PlayerContext import PlayerContext
from model.TerrainType import TerrainType
from model.Vehicle import Vehicle
from model.VehicleType import VehicleType
from model.VehicleUpdate import VehicleUpdate
from model.WeatherType import WeatherType
from model.World import World
class RemoteProcessClient:
BUFFER_SIZE_BYTES = 1 << 20
LITTLE_ENDIAN_BYTE_ORDER = True
BYTE_ORDER_FORMAT_STRING = "<" if LITTLE_ENDIAN_BYTE_ORDER else ">"
BYTE_FORMAT_STRING = BYTE_ORDER_FORMAT_STRING + "b"
INT_FORMAT_STRING = BYTE_ORDER_FORMAT_STRING + "i"
LONG_FORMAT_STRING = BYTE_ORDER_FORMAT_STRING + "q"
DOUBLE_FORMAT_STRING = BYTE_ORDER_FORMAT_STRING + "d"
SIGNED_BYTE_SIZE_BYTES = 1
INTEGER_SIZE_BYTES = 4
LONG_SIZE_BYTES = 8
DOUBLE_SIZE_BYTES = 8
def __init__(self, host, port):
self.socket = _socket.socket()
self.socket.setsockopt(_socket.IPPROTO_TCP, _socket.TCP_NODELAY, True)
self.socket.connect((host, port))
self.read_buffer = bytes()
self.read_index = 0
self.previous_players = None
self.previous_player_by_id = {}
self.previous_facilities = None
self.previous_facility_by_id = {}
self.terrain_by_cell_x_y = None
self.weather_by_cell_x_y = None
def write_token_message(self, token):
self.write_enum(RemoteProcessClient.MessageType.AUTHENTICATION_TOKEN)
self.write_string(token)
def write_protocol_version_message(self):
self.write_enum(RemoteProcessClient.MessageType.PROTOCOL_VERSION)
self.write_int(3)
def read_team_size_message(self):
message_type = self.read_enum(RemoteProcessClient.MessageType)
self.ensure_message_type(message_type, RemoteProcessClient.MessageType.TEAM_SIZE)
self.read_int()
def read_game_context_message(self):
message_type = self.read_enum(RemoteProcessClient.MessageType)
self.ensure_message_type(message_type, RemoteProcessClient.MessageType.GAME_CONTEXT)
return self.read_game()
def read_player_context_message(self):
message_type = self.read_enum(RemoteProcessClient.MessageType)
if message_type == RemoteProcessClient.MessageType.GAME_OVER:
return None
self.ensure_message_type(message_type, RemoteProcessClient.MessageType.PLAYER_CONTEXT)
return self.read_player_context()
def write_move_message(self, move):
self.write_enum(RemoteProcessClient.MessageType.MOVE)
self.write_move(move)
def close(self):
self.socket.close()
def read_facility(self):
flag = self.read_signed_byte()
if flag == 0:
return None
if flag == 127:
return self.previous_facility_by_id[self.read_long()]
facility = Facility(
self.read_long(), self.read_enum(FacilityType), self.read_long(), self.read_double(), self.read_double(),
self.read_double(), self.read_enum(VehicleType), self.read_int()
)
self.previous_facility_by_id[facility.id] = facility
return facility
def write_facility(self, facility):
if facility is None:
self.write_boolean(False)
else:
self.write_boolean(True)
self.write_long(facility.id)
self.write_enum(facility.type)
self.write_long(facility.owner_player_id)
self.write_double(facility.left)
self.write_double(facility.top)
self.write_double(facility.capture_points)
self.write_enum(facility.vehicle_type)
self.write_int(facility.production_progress)
def read_facilities(self):
facility_count = self.read_int()
if facility_count < 0:
return self.previous_facilities
facilities = [self.read_facility() for _ in range(facility_count)]
self.previous_facilities = facilities
return facilities
def write_facilities(self, facilities):
if facilities is None:
self.write_int(-1)
else:
self.write_int(facilities.__len__())
for facility in facilities:
self.write_facility(facility)
def read_game(self):
if not self.read_boolean():
return None
byte_array = self.read_bytes(565)
game = struct.unpack(
RemoteProcessClient.BYTE_ORDER_FORMAT_STRING + "qi2db9i19di4d7i4d7i2d3i2di4d7i4d6i4d2i2di", byte_array)
return Game(
game[0], game[1], game[2], game[3], game[4] != 0, game[5], game[6], game[7], game[8], game[9], game[10],
game[11], game[12], game[13], game[14], game[15], game[16], game[17], game[18], game[19], game[20],
game[21], game[22], game[23], game[24], game[25], game[26], game[27], game[28], game[29], game[30],
game[31], game[32], game[33], game[34], game[35], game[36], game[37], game[38], game[39], game[40],
game[41], game[42], game[43], game[44], game[45], game[46], game[47], game[48], game[49], game[50],
game[51], game[52], game[53], game[54], game[55], game[56], game[57], game[58], game[59], game[60],
game[61], game[62], game[63], game[64], game[65], game[66], game[67], game[68], game[69], game[70],
game[71], game[72], game[73], game[74], game[75], game[76], game[77], game[78], game[79], game[80],
game[81], game[82], game[83], game[84], game[85], game[86], game[87], game[88], game[89], game[90],
game[91], game[92], game[93]
)
def write_game(self, game):
if game is None:
self.write_boolean(False)
else:
self.write_boolean(True)
self.write_long(game.random_seed)
self.write_int(game.tick_count)
self.write_double(game.world_width)
self.write_double(game.world_height)
self.write_boolean(game.fog_of_war_enabled)
self.write_int(game.victory_score)
self.write_int(game.facility_capture_score)
self.write_int(game.vehicle_elimination_score)
self.write_int(game.action_detection_interval)
self.write_int(game.base_action_count)
self.write_int(game.additional_action_count_per_control_center)
self.write_int(game.max_unit_group)
self.write_int(game.terrain_weather_map_column_count)
self.write_int(game.terrain_weather_map_row_count)
self.write_double(game.plain_terrain_vision_factor)
self.write_double(game.plain_terrain_stealth_factor)
self.write_double(game.plain_terrain_speed_factor)
self.write_double(game.swamp_terrain_vision_factor)
self.write_double(game.swamp_terrain_stealth_factor)
self.write_double(game.swamp_terrain_speed_factor)
self.write_double(game.forest_terrain_vision_factor)
self.write_double(game.forest_terrain_stealth_factor)
self.write_double(game.forest_terrain_speed_factor)
self.write_double(game.clear_weather_vision_factor)
self.write_double(game.clear_weather_stealth_factor)
self.write_double(game.clear_weather_speed_factor)
self.write_double(game.cloud_weather_vision_factor)
self.write_double(game.cloud_weather_stealth_factor)
self.write_double(game.cloud_weather_speed_factor)
self.write_double(game.rain_weather_vision_factor)
self.write_double(game.rain_weather_stealth_factor)
self.write_double(game.rain_weather_speed_factor)
self.write_double(game.vehicle_radius)
self.write_int(game.tank_durability)
self.write_double(game.tank_speed)
self.write_double(game.tank_vision_range)
self.write_double(game.tank_ground_attack_range)
self.write_double(game.tank_aerial_attack_range)
self.write_int(game.tank_ground_damage)
self.write_int(game.tank_aerial_damage)
self.write_int(game.tank_ground_defence)
self.write_int(game.tank_aerial_defence)
self.write_int(game.tank_attack_cooldown_ticks)
self.write_int(game.tank_production_cost)
self.write_int(game.ifv_durability)
self.write_double(game.ifv_speed)
self.write_double(game.ifv_vision_range)
self.write_double(game.ifv_ground_attack_range)
self.write_double(game.ifv_aerial_attack_range)
self.write_int(game.ifv_ground_damage)
self.write_int(game.ifv_aerial_damage)
self.write_int(game.ifv_ground_defence)
self.write_int(game.ifv_aerial_defence)
self.write_int(game.ifv_attack_cooldown_ticks)
self.write_int(game.ifv_production_cost)
self.write_int(game.arrv_durability)
self.write_double(game.arrv_speed)
self.write_double(game.arrv_vision_range)
self.write_int(game.arrv_ground_defence)
self.write_int(game.arrv_aerial_defence)
self.write_int(game.arrv_production_cost)
self.write_double(game.arrv_repair_range)
self.write_double(game.arrv_repair_speed)
self.write_int(game.helicopter_durability)
self.write_double(game.helicopter_speed)
self.write_double(game.helicopter_vision_range)
self.write_double(game.helicopter_ground_attack_range)
self.write_double(game.helicopter_aerial_attack_range)
self.write_int(game.helicopter_ground_damage)
self.write_int(game.helicopter_aerial_damage)
self.write_int(game.helicopter_ground_defence)
self.write_int(game.helicopter_aerial_defence)
self.write_int(game.helicopter_attack_cooldown_ticks)
self.write_int(game.helicopter_production_cost)
self.write_int(game.fighter_durability)
self.write_double(game.fighter_speed)
self.write_double(game.fighter_vision_range)
self.write_double(game.fighter_ground_attack_range)
self.write_double(game.fighter_aerial_attack_range)
self.write_int(game.fighter_ground_damage)
self.write_int(game.fighter_aerial_damage)
self.write_int(game.fighter_ground_defence)
self.write_int(game.fighter_aerial_defence)
self.write_int(game.fighter_attack_cooldown_ticks)
self.write_int(game.fighter_production_cost)
self.write_double(game.max_facility_capture_points)
self.write_double(game.facility_capture_points_per_vehicle_per_tick)
self.write_double(game.facility_width)
self.write_double(game.facility_height)
self.write_int(game.base_tactical_nuclear_strike_cooldown)
self.write_int(game.tactical_nuclear_strike_cooldown_decrease_per_control_center)
self.write_double(game.max_tactical_nuclear_strike_damage)
self.write_double(game.tactical_nuclear_strike_radius)
self.write_int(game.tactical_nuclear_strike_delay)
def read_games(self):
game_count = self.read_int()
if game_count < 0:
return None
return [self.read_game() for _ in range(game_count)]
def write_games(self, games):
if games is None:
self.write_int(-1)
else:
self.write_int(games.__len__())
for game in games:
self.write_game(game)
def write_move(self, move):
if move is None:
self.write_boolean(False)
else:
self.write_boolean(True)
self.write_enum(move.action)
self.write_int(move.group)
self.write_double(move.left)
self.write_double(move.top)
self.write_double(move.right)
self.write_double(move.bottom)
self.write_double(move.x)
self.write_double(move.y)
self.write_double(move.angle)
self.write_double(move.factor)
self.write_double(move.max_speed)
self.write_double(move.max_angular_speed)
self.write_enum(move.vehicle_type)
self.write_long(move.facility_id)
self.write_long(move.vehicle_id)
def write_moves(self, moves):
if moves is None:
self.write_int(-1)
else:
self.write_int(moves.__len__())
for move in moves:
self.write_move(move)
def read_player(self):
flag = self.read_signed_byte()
if flag == 0:
return None
if flag == 127:
return self.previous_player_by_id[self.read_long()]
byte_array = self.read_bytes(50)
player = struct.unpack(RemoteProcessClient.BYTE_ORDER_FORMAT_STRING + "q2b3iqi2d", byte_array)
player = Player(
player[0], player[1] != 0, player[2] != 0, player[3], player[4], player[5], player[6], player[7], player[8],
player[9]
)
self.previous_player_by_id[player.id] = player
return player
def write_player(self, player):
if player is None:
self.write_boolean(False)
else:
self.write_boolean(True)
self.write_long(player.id)
self.write_boolean(player.me)
self.write_boolean(player.strategy_crashed)
self.write_int(player.score)
self.write_int(player.remaining_action_cooldown_ticks)
self.write_int(player.remaining_nuclear_strike_cooldown_ticks)
self.write_long(player.next_nuclear_strike_vehicle_id)
self.write_int(player.next_nuclear_strike_tick_index)
self.write_double(player.next_nuclear_strike_x)
self.write_double(player.next_nuclear_strike_y)
def read_players(self):
player_count = self.read_int()
if player_count < 0:
return self.previous_players
players = [self.read_player() for _ in range(player_count)]
self.previous_players = players
return players
def write_players(self, players):
if players is None:
self.write_int(-1)
else:
self.write_int(players.__len__())
for player in players:
self.write_player(player)
def read_player_context(self):
if not self.read_boolean():
return None
return PlayerContext(self.read_player(), self.read_world())
def write_player_context(self, player_context):
if player_context is None:
self.write_boolean(False)
else:
self.write_boolean(True)
self.write_player(player_context.player)
self.write_world(player_context.world)
def read_player_contexts(self):
player_context_count = self.read_int()
if player_context_count < 0:
return None
return [self.read_player_context() for _ in range(player_context_count)]
def write_player_contexts(self, player_contexts):
if player_contexts is None:
self.write_int(-1)
else:
self.write_int(player_contexts.__len__())
for player_context in player_contexts:
self.write_player_context(player_context)
def read_vehicle(self):
if not self.read_boolean():
return None
byte_array = self.read_bytes(128)
vehicle = struct.unpack(RemoteProcessClient.BYTE_ORDER_FORMAT_STRING + "q3dq2i7d6i", byte_array)
return Vehicle(
vehicle[0], vehicle[1], vehicle[2], vehicle[3], vehicle[4], vehicle[5], vehicle[6], vehicle[7], vehicle[8],
vehicle[9], vehicle[10], vehicle[11], vehicle[12], vehicle[13], vehicle[14], vehicle[15], vehicle[16],
vehicle[17], vehicle[18], vehicle[19], self.read_enum(VehicleType), self.read_boolean(),
self.read_boolean(), self.read_ints()
)
def write_vehicle(self, vehicle):
if vehicle is None:
self.write_boolean(False)
else:
self.write_boolean(True)
self.write_long(vehicle.id)
self.write_double(vehicle.x)
self.write_double(vehicle.y)
self.write_double(vehicle.radius)
self.write_long(vehicle.player_id)
self.write_int(vehicle.durability)
self.write_int(vehicle.max_durability)
self.write_double(vehicle.max_speed)
self.write_double(vehicle.vision_range)
self.write_double(vehicle.squared_vision_range)
self.write_double(vehicle.ground_attack_range)
self.write_double(vehicle.squared_ground_attack_range)
self.write_double(vehicle.aerial_attack_range)
self.write_double(vehicle.squared_aerial_attack_range)
self.write_int(vehicle.ground_damage)
self.write_int(vehicle.aerial_damage)
self.write_int(vehicle.ground_defence)
self.write_int(vehicle.aerial_defence)
self.write_int(vehicle.attack_cooldown_ticks)
self.write_int(vehicle.remaining_attack_cooldown_ticks)
self.write_enum(vehicle.type)
self.write_boolean(vehicle.aerial)
self.write_boolean(vehicle.selected)
self.write_ints(vehicle.groups)
def read_vehicles(self):
vehicle_count = self.read_int()
if vehicle_count < 0:
return None
return [self.read_vehicle() for _ in range(vehicle_count)]
def write_vehicles(self, vehicles):
if vehicles is None:
self.write_int(-1)
else:
self.write_int(vehicles.__len__())
for vehicle in vehicles:
self.write_vehicle(vehicle)
def read_vehicle_update(self):
if not self.read_boolean():
return None
byte_array = self.read_bytes(33)
vehicle_update = struct.unpack(RemoteProcessClient.BYTE_ORDER_FORMAT_STRING + "q2d2ib", byte_array)
return VehicleUpdate(
vehicle_update[0], vehicle_update[1], vehicle_update[2], vehicle_update[3], vehicle_update[4],
vehicle_update[5] != 0, self.read_ints()
)
def write_vehicle_update(self, vehicle_update):
if vehicle_update is None:
self.write_boolean(False)
else:
self.write_boolean(True)
self.write_long(vehicle_update.id)
self.write_double(vehicle_update.x)
self.write_double(vehicle_update.y)
self.write_int(vehicle_update.durability)
self.write_int(vehicle_update.remaining_attack_cooldown_ticks)
self.write_boolean(vehicle_update.selected)
self.write_ints(vehicle_update.groups)
def read_vehicle_updates(self):
vehicle_update_count = self.read_int()
if vehicle_update_count < 0:
return None
return [self.read_vehicle_update() for _ in range(vehicle_update_count)]
def write_vehicle_updates(self, vehicle_updates):
if vehicle_updates is None:
self.write_int(-1)
else:
self.write_int(vehicle_updates.__len__())
for vehicle_update in vehicle_updates:
self.write_vehicle_update(vehicle_update)
def read_world(self):
if not self.read_boolean():
return None
byte_array = self.read_bytes(24)
world = struct.unpack(RemoteProcessClient.BYTE_ORDER_FORMAT_STRING + "2i2d", byte_array)
return World(
world[0], world[1], world[2], world[3], self.read_players(), self.read_vehicles(),
self.read_vehicle_updates(), self.read_terrain_by_cell_x_y(), self.read_weather_by_cell_x_y(),
self.read_facilities()
)
def write_world(self, world):
if world is None:
self.write_boolean(False)
else:
self.write_boolean(True)
self.write_int(world.tick_index)
self.write_int(world.tick_count)
self.write_double(world.width)
self.write_double(world.height)
self.write_players(world.players)
self.write_vehicles(world.new_vehicles)
self.write_vehicle_updates(world.vehicle_updates)
self.write_enums_2d(world.terrain_by_cell_x_y)
self.write_enums_2d(world.weather_by_cell_x_y)
self.write_facilities(world.facilities)
def read_worlds(self):
world_count = self.read_int()
if world_count < 0:
return None
return [self.read_world() for _ in range(world_count)]
def write_worlds(self, worlds):
if worlds is None:
self.write_int(-1)
else:
self.write_int(worlds.__len__())
for world in worlds:
self.write_world(world)
def read_terrain_by_cell_x_y(self):
if self.terrain_by_cell_x_y is None:
self.terrain_by_cell_x_y = self.read_enums_2d(TerrainType)
return self.terrain_by_cell_x_y
def read_weather_by_cell_x_y(self):
if self.weather_by_cell_x_y is None:
self.weather_by_cell_x_y = self.read_enums_2d(WeatherType)
return self.weather_by_cell_x_y
@staticmethod
def ensure_message_type(actual_type, expected_type):
if actual_type != expected_type:
raise ValueError("Received wrong message [actual=%s, expected=%s]." % (actual_type, expected_type))
def read_enum(self, enum_class):
byte_array = self.read_bytes(RemoteProcessClient.SIGNED_BYTE_SIZE_BYTES)
value = struct.unpack(RemoteProcessClient.BYTE_FORMAT_STRING, byte_array)[0]
for enum_key, enum_value in enum_class.__dict__.items():
if not str(enum_key).startswith("__") and value == enum_value:
return enum_value
return None
def read_byte_array(self, nullable):
count = self.read_int()
if count <= 0:
return None if nullable and count < 0 else bytes()
return self.read_bytes(count)
def write_byte_array(self, array):
if array is None:
self.write_int(-1)
else:
self.write_int(array.__len__())
self.write_bytes(array)
def read_enums(self, enum_class):
count = self.read_int()
return None if count < 0 else [self.read_enum(enum_class) for _ in range(count)]
def read_enums_2d(self, enum_class):
count = self.read_int()
return None if count < 0 else [self.read_enums(enum_class) for _ in range(count)]
def write_enum(self, value):
self.write_bytes(struct.pack(
RemoteProcessClient.BYTE_FORMAT_STRING, -1 if value is None else value
))
def write_enums(self, enums):
if enums is None:
self.write_int(-1)
else:
self.write_int(enums.__len__())
for value in enums:
self.write_enum(value)
def write_enums_2d(self, enums_2d):
if enums_2d is None:
self.write_int(-1)
else:
self.write_int(enums_2d.__len__())
for enums in enums_2d:
self.write_enums(enums)
def read_string(self):
length = self.read_int()
if length == -1:
return None
byte_array = self.read_bytes(length)
return byte_array.decode()
def write_string(self, value):
if value is None:
self.write_int(-1)
return
byte_array = value.encode()
self.write_int(len(byte_array))
self.write_bytes(byte_array)
def read_signed_byte(self):
byte_array = self.read_bytes(RemoteProcessClient.SIGNED_BYTE_SIZE_BYTES)
return struct.unpack(RemoteProcessClient.BYTE_FORMAT_STRING, byte_array)[0]
def read_boolean(self):
return self.read_signed_byte() != 0
def read_boolean_array(self, count):
byte_array = self.read_bytes(count * RemoteProcessClient.SIGNED_BYTE_SIZE_BYTES)
unpacked_bytes = struct.unpack(RemoteProcessClient.BYTE_ORDER_FORMAT_STRING + str(count) + "b", byte_array)
return [unpacked_bytes[i] != 0 for i in range(count)]
def write_boolean(self, value):
self.write_bytes(struct.pack(RemoteProcessClient.BYTE_FORMAT_STRING, 1 if value else 0))
def read_int(self):
byte_array = self.read_bytes(RemoteProcessClient.INTEGER_SIZE_BYTES)
return struct.unpack(RemoteProcessClient.INT_FORMAT_STRING, byte_array)[0]
def read_ints(self):
count = self.read_int()
if count < 0:
return None
ints = []
if count > 0:
byte_array = self.read_bytes(RemoteProcessClient.INTEGER_SIZE_BYTES * count)
ints.extend(struct.unpack(RemoteProcessClient.BYTE_ORDER_FORMAT_STRING + str(count) + "i", byte_array))
return ints
def read_ints_2d(self):
count = self.read_int()
return None if count < 0 else [self.read_ints() for _ in range(count)]
def write_int(self, value):
self.write_bytes(struct.pack(RemoteProcessClient.INT_FORMAT_STRING, value))
def write_ints(self, ints):
if ints is None:
self.write_int(-1)
else:
self.write_int(ints.__len__())
for value in ints:
self.write_int(value)
def write_ints_2d(self, ints_2d):
if ints_2d is None:
self.write_int(-1)
else:
self.write_int(ints_2d.__len__())
for ints in ints_2d:
self.write_ints(ints)
def read_long(self):
byte_array = self.read_bytes(RemoteProcessClient.LONG_SIZE_BYTES)
return struct.unpack(RemoteProcessClient.LONG_FORMAT_STRING, byte_array)[0]
def write_long(self, value):
self.write_bytes(struct.pack(RemoteProcessClient.LONG_FORMAT_STRING, value))
def read_double(self):
byte_array = self.read_bytes(RemoteProcessClient.DOUBLE_SIZE_BYTES)
return struct.unpack(RemoteProcessClient.DOUBLE_FORMAT_STRING, byte_array)[0]
def write_double(self, value):
self.write_bytes(struct.pack(RemoteProcessClient.DOUBLE_FORMAT_STRING, value))
def read_bytes(self, byte_count):
if len(self.read_buffer) - self.read_index < byte_count:
self.read_buffer = self.read_buffer[self.read_index:]
self.read_index = 0
while len(self.read_buffer) - self.read_index < byte_count:
chunk = self.socket.recv(max(
RemoteProcessClient.BUFFER_SIZE_BYTES - len(self.read_buffer),
byte_count - len(self.read_buffer) + self.read_index
))
if not len(chunk):
break
self.read_buffer += chunk
if len(self.read_buffer) - self.read_index < byte_count:
raise IOError("Can't read %s bytes from input stream." % str(byte_count))
byte_array = self.read_buffer[self.read_index:self.read_index + byte_count]
self.read_index += byte_count
return byte_array
def write_bytes(self, byte_array):
self.socket.sendall(byte_array)
class MessageType:
UNKNOWN = 0
GAME_OVER = 1
AUTHENTICATION_TOKEN = 2
TEAM_SIZE = 3
PROTOCOL_VERSION = 4
GAME_CONTEXT = 5
PLAYER_CONTEXT = 6
MOVE = 7