-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathSpringDemoFile.py
More file actions
1677 lines (1599 loc) · 69.9 KB
/
SpringDemoFile.py
File metadata and controls
1677 lines (1599 loc) · 69.9 KB
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
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/python
#
# SpringDemoFile - Class library for parsing Spring Demo Files
#
# The module should be placed in a directory in your Python class path or in the
# directory of any module that is using it.
#
# Tested on Python 2.7.2, Windows 7 on ZK Games only, YMMV on other platforms and other games based on Spring
#
# (C) 2011, Rene van 't Veen
#
# This program 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.
#
# This program 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.
#
# For a copy of the GNU General Public License see <http://www.gnu.org/licenses/>.
#
"""Python script to collect statistics from one or more spring demo files"""
import re
import struct
import os.path
import gzip
import magic
try:
from typing import Dict, List, Tuple, Union
except ImportError:
pass
__author__ = 'rene'
__version__ = '0.2.2'
class PlayerStatistics:
"""
Just a struct of player statistics
"""
def __init__(self):
"""
Initialize all member variables to zero
Note that this will also be the value for any player that leaves the game before the game is over
"""
# total mouse movement during game
self.mousePixels = 0
# number of mouse clicks during game
self.mouseClicks = 0
# number of keypresses during game
self.keyPresses = 0
# number of commands given during game
self.numCommands = 0
# number of unit commands resulting from key presses and/or mouse clicks
self.unitCommands = 0
def __repr__(self):
"""
Pretty printing the structure
"""
return (
'KP: ' + str(self.keyPresses) +
', MP: ' + str(self.mousePixels) +
', MC: ' + str(self.mouseClicks) +
', NC: ' + str(self.numCommands) +
', UC: ' + str(self.unitCommands)
)
class TeamStatistics:
"""
Just a struct of team statistics
"""
def __init__(self):
"""
Initialize all member variables to zero
"""
# the game frame ID (use as a measure of time)
self.frame = 0
# metal used (to build units or morph)
self.metalUsed = 0.0
# energy used (to build units, repair, resurrect, overdrive)
self.energyUsed = 0.0
# metal produced
self.metalProduced = 0.0
# energy produced
self.energyProduced = 0.0
# excess metal (that is, metal for which player has no storage)
self.metalExcess = 0.0
# excess energy (in zero-k this is almost always 0 as it is used to overdrive mexes)
self.energyExcess = 0.0
# metal received from sharing (either explicit or implicit through metal communism)
self.metalReceived = 0.0
# in zero-k energy received is most always zero unless someone sent you some E
self.energyReceived = 0.0
# metal sent by sharing (explicit and implicit)
self.metalSent = 0.0
# in zero-k energy sent is most always zero unless you explicitly sent some to a teammate
self.energySent = 0.0
# damage you dealt (in HP)
self.damageDealt = 0.0
# damage you received (in HP)
self.damageReceived = 0.0
# number of units produced
self.unitsProduced = 0
self.unitsDied = 0
self.unitsReceived = 0
self.unitsSent = 0
self.unitsCaptured = 0
self.unitsOutCaptured = 0
self.unitsKilled = 0
def __repr__(self):
"""
Pretty printing
"""
s = (
str(self.frame) + ' M(Use: ' + str(self.metalUsed) +
' Prod: ' + str(self.metalProduced) +
' Excess: ' + str(self.metalExcess) +
' Rcv: ' + str(self.metalReceived) +
' Sent: ' + str(self.metalSent) +
') E(Use: ' + str(self.energyUsed) +
' Prod: ' + str(self.energyProduced) +
' Excess: ' + str(self.energyExcess) +
' Rcv: ' + str(self.energyReceived) +
' Sent: ' + str(self.energySent) +
') Damage(Done: ' + str(self.damageDealt) +
' Received: ' + str(self.damageReceived) +
') Units(Prod: ' + str(self.unitsProduced) +
' Dead: ' + str(self.unitsDied) +
' Rcv: ' + str(self.unitsReceived) +
' Sent: ' + str(self.unitsSent) +
' Cap: ' + str(self.unitsCaptured) +
' Steal: ' + str(self.unitsOutCaptured)
)
if hasattr(self, 'unitsKilled'):
s += ' Killed:' + str(self.unitsKilled)
s += ')'
return s
class DemoRecord:
"""
Class that represents a single record in the demo stream
"""
# record type, this is data[0] (see BaseNetProtocol.h)
KEYFRAME = 1
NEWFRAME = 2
QUIT = 3
STARTPLAYING = 4
SETPLAYERNUM = 5
PLAYERNAME = 6
CHAT = 7
RANDSEED = 8
GAMEID = 9
PATH_CHECKSUM = 10
COMMAND = 11
SELECT = 12
PAUSE = 13
AICOMMAND = 14
AICOMMANDS = 15
AISHARE = 16
USER_SPEED = 19
INTERNAL_SPEED = 20
CPU_USAGE = 21
DIRECT_CONTROL = 22
DC_UPDATE = 23
SHARE = 26
SETSHARE = 27
SENDPLAYERSTAT = 28
PLAYERSTAT = 29
GAMEOVER = 30
MAPDRAW = 31
SYNCRESPONSE = 33
SYSTEMMSG = 35
STARTPOS = 36
PLAYERINFO = 38
PLAYERLEFT = 39
LUAMSG = 50
TEAM = 51
GAMEDATA = 52
ALLIANCE = 53
CCOMMAND = 54
CUSTOM_DATA = 55
TEAMSTAT = 60
ATTEMPT_CONNECT = 65
AI_CREATED = 70
AI_STATE_CHANGED = 71
REQUEST_TEAMSTAT = 72
CREATE_NEWPLAYER = 75
# Forged records have negative numbers
# ZK SPRINGIE records are really chat messages
ZK_DAMAGE = -1
ZK_UNIT = -2
ZK_AWARD = -3
# for ZK springie records we dont know about
ZK_OTHER = -4
# Chat destinations (from ChatMessage.h)
CHAT_ALLIES = 252
CHAT_SPECTATORS = 253
CHAT_EVERYONE = 254
# not defined in ChatMessage.h but present in demo files
CHAT_HOST = 255
# helper constants
__SPRINGIE = 4 + len('SPRINGIE:')
__MODSTATSDMG = __SPRINGIE + len('stats,dmg,')
__MODSTATSUNIT = __SPRINGIE + len('stats,unit,')
__AWARD = __SPRINGIE + len('award,')
def __init__(self):
"""
Constructor, initializes all members to default values
"""
self.gametime = 0.0
self.data = bytearray()
def type(self): # type: () -> Union[None, int]
"""
Returns the type of record or None if the DemoRecord is not mapped to data
"""
if len(self.data) > 0:
v = ord(self.data[0])
if v == self.CHAT:
if ord(self.data[3]) == self.CHAT_HOST:
if self.data[4:self.__SPRINGIE] == 'SPRINGIE:':
if self.data[self.__SPRINGIE:self.__MODSTATSDMG] == 'stats,dmg,':
return self.ZK_DAMAGE
elif self.data[self.__SPRINGIE:self.__MODSTATSUNIT] == 'stats,unit,':
return self.ZK_UNIT
elif self.data[self.__SPRINGIE:self.__AWARD] == 'award,':
return self.ZK_AWARD
else:
# examples include teams and plist
return self.ZK_OTHER
return v
return None
def player(self): # type () -> Union[None, int]
"""
Returns the initiating player number for messages that have an initiating player or None for those messages that do not
have it, or if the record cannot be parsed
"""
t = self.type()
if t is None:
return None
try:
d1 = ord(self.data[1])
d2 = ord(self.data[2])
d3 = ord(self.data[3])
except IndexError:
# self.data only 2 long in case of PLAYERLEFT
return None
record_type_to_player_number = {
self.SETPLAYERNUM: d1,
self.PLAYERNAME: d2,
self.CHAT: d2,
self.ZK_DAMAGE: d2,
self.ZK_UNIT: d2,
self.ZK_AWARD: d2,
self.ZK_OTHER: d2,
self.PATH_CHECKSUM: d1,
self.COMMAND: d2,
self.SELECT: d2,
self.PAUSE: d1,
self.AICOMMAND: d2,
self.AICOMMANDS: d2,
self.AISHARE: d2,
self.USER_SPEED: d1,
self.DIRECT_CONTROL: d1,
self.DC_UPDATE: d1,
self.SHARE: d1,
self.SETSHARE: d1,
self.PLAYERSTAT: d1,
self.MAPDRAW: d2,
self.SYNCRESPONSE: d1,
self.SYSTEMMSG: d2, # appears meaningless, it is always 0
self.STARTPOS: d1,
self.PLAYERINFO: d1,
self.PLAYERLEFT: d1,
self.LUAMSG: d3,
self.TEAM: d1,
self.ALLIANCE: d1,
self.CUSTOM_DATA: d1,
self.AI_CREATED: d2,
self.AI_STATE_CHANGED: d1,
self.CREATE_NEWPLAYER: d2, # appears meaningless, it is always 0, the player number is assigned by SETPLAYERNUM
}
return record_type_to_player_number.get(t)
def destination(self): # type () -> Union[None, int]
"""
Returns the destination player number for messages that have an destination player or None for those messages that do not
have it, or if the record cannot be parsed
"""
t = self.type()
if t is None:
return None
elif t == self.CHAT:
return ord(self.data[3])
else:
return None
def text(self): # type () -> Union[None, str]
"""
Returns the text of messages that have text or None if the message has no text
"""
t = self.type()
if t is None:
return None
elif t == self.MAPDRAW and len(self.data) > 9 and ord(self.data[3]) == 0:
# apparently there is a 0 byte in front of the label
index = 9
else:
record_type_to_text_index = {
self.QUIT: 3, # apparently there is a 0 byte in front of the label
self.PLAYERNAME: 3,
self.CHAT: 3,
self.SYSTEMMSG: 4, # apparently, there is a 255 byte in front of the label
self.AI_CREATED: 8, # untested
self.CREATE_NEWPLAYER: 6,
self.ZK_DAMAGE: self.__MODSTATSDMG,
self.ZK_UNIT: self.__MODSTATSUNIT,
self.ZK_AWARD: self.__AWARD,
self.ZK_OTHER: self.__SPRINGIE,
}
try:
index = record_type_to_text_index[t]
except KeyError:
return None
s = self.data[index:]
# get rid of any terminating null characters
s = s.strip('\0')
return None if s == '' else s
def spectator(self): # type () -> Union[None, int]
"""
Returns the value of the spectator field in the createnewplayer record only or None.
"""
t = self.type()
if t is None:
return None
elif t == self.CREATE_NEWPLAYER:
return ord(self.data[3])
else:
return None
def reason(self): # type () -> Union[None, int]
"""
Returns the value of the reason or type field in the PAUSED and PLAYERLEFT record only or None.
"""
t = self.type()
if t is None:
return None
elif t == self.PAUSE:
return ord(self.data[2])
elif t == self.PLAYERLEFT:
return ord(self.data[2])
else:
return None
def team(self): # type () -> Union[None, int]
"""
Returns the value of the team field in the records that have such a field or None.
"""
t = self.type()
if t is None:
return None
elif t == self.CREATE_NEWPLAYER:
return ord(self.data[4])
else:
return None
def __repr__(self):
"""
Pretty printing
"""
v = divmod(self.gametime, 60.0)
w = divmod(v[0], 60.0)
if w[0] == 0.0:
s = '%.0fm%04.1fs' % (w[1], v[1])
else:
s = '%.0fh%02.0fm%04.1fs' % (w[0], w[1], v[1])
s += ':' + repr(bytearray(self.data))
return s
class DemoFileReader:
"""
Class whose instance reads Spring demo files
"""
zkunitnames = {
'amgeo': 'Moho Geothermal Powerplant', # 1 amgeo.lua
'amphaa': 'Angler', # 2 amphaa.lua
'amphassault': 'Grizzly', # 3 amphassault.lua
'amphcon': 'Clam', # 4 amphcon.lua
'amphfloater': 'Buoy', # 5 amphfloater.lua
'amphraider': 'Grebe', # 6 amphraider.lua
'amphraider2': 'Archer', # 7 amphraider2.lua
'amphraider3': 'Duck', # 8 amphraider3.lua
'amphriot': 'Scallop', # 9 amphriot.lua
'amphtele': 'Djinn', # 10 amphtele.lua
'arm_spider': 'Weaver', # 11 arm_spider.lua
'arm_venom': 'Venom', # 12 arm_venom.lua
'armaak': 'Archangel', # 13 armaak.lua
'armamd': 'Protector', # 14 armamd.lua
'armanni': 'Annihilator', # 15 armanni.lua
'armarad': 'Advanced Radar Tower', # 16 armarad.lua
'armartic': 'Faraday', # 17 armartic.lua
'armasp': 'Air Repair/Rearm Pad', # 18 armasp.lua
'armbanth': 'Bantha', # 19 armbanth.lua
'armbrawl': 'Brawler', # 20 armbrawl.lua
'armbrtha': 'Big Bertha', # 21 armbrtha.lua
'armca': 'Crane', # 22 armca.lua
'armcarry': 'Reef', # 23 armcarry.lua
'armcir': 'Chainsaw', # 24 armcir.lua
'armcom1': 'Strike Commander', # 25 armcom1.lua
'armcomdgun': 'Ultimatum', # 26 armcomdgun.lua
'armcrabe': 'Crabe', # 27 armcrabe.lua
'armcsa': 'Athena', # 28 armcsa.lua
'armcybr': 'Licho', # 29 armcybr.lua
'armdeva': 'Stardust', # 30 armdeva.lua
'armestor': 'Energy Transmission Pylon', # 31 armestor.lua
'armflea': 'Flea', # 32 armflea.lua
'armfus': 'Fusion Reactor', # 33 armfus.lua
'armham': 'Hammer', # 34 armham.lua
'armjamt': 'Sneaky Pete', # 35 armjamt.lua
'armjeth': 'Jethro', # 36 armjeth.lua
'armkam': 'Banshee', # 37 armkam.lua
'armmanni': 'Penetrator', # 38 armmanni.lua
'armmerl': 'Merl', # 39 armmerl.lua
'armmstor': 'Storage', # 40 armmstor.lua
'armnanotc': 'Caretaker', # 41 armnanotc.lua
'armorco': 'Detriment', # 42 armorco.lua
'armpb': 'Gauss', # 43 armpb.lua
'armpt': 'Skeeter', # 44 armpt.lua
'armpw': 'Glaive', # 45 armpw.lua
'armraven': 'Catapult', # 46 armraven.lua
'armraz': 'Razorback', # 47 armraz.lua
'armrectr': 'Rector', # 48 armrectr.lua
'armrock': 'Rocko', # 49 armrock.lua
'armroy': 'Crusader', # 50 armroy.lua
'armsnipe': 'Sharpshooter', # 51 armsnipe.lua
'armsolar': 'Solar Collector', # 52 armsolar.lua
'armsonar': 'Sonar Station', # 53 armsonar.lua
'armsptk': 'Recluse', # 54 armsptk.lua
'armspy': 'Infiltrator', # 55 armspy.lua
'armstiletto_laser': 'Stiletto', # 56 armstiletto_laser.lua
'armtboat': 'Surfboard', # 57 armtboat.lua
'armtick': 'Tick', # 58 armtick.lua
'armwar': 'Warrior', # 59 armwar.lua
'armwin': 'Wind/Tidal Generator', # 60 armwin.lua
'armzeus': 'Zeus', # 61 armzeus.lua
'assaultcruiser': 'Vanquisher', # 62 assaultcruiser.lua
'asteroid': 'Asteroid', # 63 asteroid.lua
'attackdrone': 'Firefly', # 64 attackdrone.lua
'blackdawn': 'Black Dawn', # 65 blackdawn.lua
'bladew': 'Gnat', # 66 bladew.lua
'blastwing': 'Blastwing', # 67 blastwing.lua
'cafus': 'Singularity Reactor', # 68 cafus.lua
'capturecar': 'Dominatrix', # 69 capturecar.lua
'carrydrone': 'Gull', # 70 carrydrone.lua
'chicken': 'Chicken', # 71 chicken.lua
'chicken_blimpy': 'Blimpy', # 72 chicken_blimpy.lua
'chicken_digger': 'Digger', # 73 chicken_digger.lua
'chicken_digger_b': 'Digger (burrowed)', # 74 chicken_digger_b.lua
'chicken_dodo': 'Dodo', # 75 chicken_dodo.lua
'chicken_dragon': 'White Dragon', # 76 chicken_dragon.lua
'chicken_drone': 'Drone', # 77 chicken_drone.lua
'chicken_drone_starter': 'Drone', # 78 chicken_drone_starter.lua
'chicken_leaper': 'Leaper', # 79 chicken_leaper.lua
'chicken_listener': 'Listener', # 80 chicken_listener.lua
'chicken_listener_b': 'Listener (burrowed)', # 81 chicken_listener_b.lua
'chicken_pigeon': 'Pigeon', # 82 chicken_pigeon.lua
'chicken_roc': 'Roc', # 83 chicken_roc.lua
'chicken_shield': 'Toad', # 84 chicken_shield.lua
'chicken_spidermonkey': 'Spidermonkey', # 85 chicken_spidermonkey.lua
'chicken_sporeshooter': 'Sporeshooter', # 86 chicken_sporeshooter.lua
'chicken_tiamat': 'Tiamat', # 87 chicken_tiamat.lua
'chickena': 'Cockatrice', # 88 chickena.lua
'chickenblobber': 'Blobber', # 89 chickenblobber.lua
'chickenbroodqueen': 'Chicken Brood Queen', # 90 chickenbroodqueen.lua
'chickenc': 'Basilisk', # 91 chickenc.lua
'chickend': 'Chicken Tube', # 92 chickend.lua
'chickenf': 'Talon', # 93 chickenf.lua
'chickenflyerqueen': 'Chicken Flyer Queen', # 94 chickenflyerqueen.lua
'chickenlandqueen': 'Chicken Queen', # 95 chickenlandqueen.lua
'chickenq': 'Chicken Queen', # 96 chickenq.lua
'chickenr': 'Lobber', # 97 chickenr.lua
'chickens': 'Spiker', # 98 chickens.lua
'chickenspire': 'Chicken Spire', # 99 chickenspire.lua
'chickenwurm': 'Wurm', # 100 chickenwurm.lua
'commrecon1': 'Recon Commander', # 101 commrecon1.lua
'commsupport1': 'Support Commander', # 102 commsupport1.lua
'coracv': 'Welder', # 103 coracv.lua
'corak': 'Bandit', # 104 corak.lua
'corape': 'Rapier', # 105 corape.lua
'corarch': 'Shredder', # 106 corarch.lua
'corawac': 'Vulture', # 107 corawac.lua
'corbats': 'Warlord', # 108 corbats.lua
'corbhmth': 'Behemoth', # 109 corbhmth.lua
'corbtrans': 'Vindicator', # 110 corbtrans.lua
'corcan': 'Jack', # 111 corcan.lua
'corch': 'Quill', # 112 corch.lua
'corclog': 'Dirtbag', # 113 corclog.lua
'corcom1': 'Battle Commander', # 114 corcom1.lua
'corcrash': 'Vandal', # 115 corcrash.lua
'corcrw': 'Krow', # 116 corcrw.lua
'corcs': 'Mariner', # 117 corcs.lua
'cordoom': 'Doomsday Machine', # 118 cordoom.lua
'core_spectre': 'Aspis', # 119 core_spectre.lua
'coresupp': 'Typhoon', # 120 coresupp.lua
'corfast': 'Freaker', # 121 corfast.lua
'corfav': 'Dart', # 122 corfav.lua
'corflak': 'Cobra', # 123 corflak.lua
'corgarp': 'Wolverine', # 124 corgarp.lua
'corgator': 'Scorcher', # 125 corgator.lua
'corgol': 'Goliath', # 126 corgol.lua
'corgrav': 'Newton', # 127 corgrav.lua
'corhlt': 'Stinger', # 128 corhlt.lua
'corhurc2': 'Phoenix', # 129 corhurc2.lua
'corjamt': 'Aegis', # 130 corjamt.lua
'corlevlr': 'Leveler', # 131 corlevlr.lua
'corllt': 'Lotus', # 132 corllt.lua
'cormak': 'Outlaw', # 133 cormak.lua
'cormart': 'Pillager', # 134 cormart.lua
'cormex': 'Metal Extractor', # 135 cormex.lua
'cormist': 'Slasher', # 136 cormist.lua
'cornecro': 'Convict', # 137 cornecro.lua
'corned': 'Mason', # 138 corned.lua
'cornukesub': 'Leviathan', # 139 cornukesub.lua
'corpre': 'Scorcher', # 140 corpre.lua
'corpyro': 'Pyro', # 141 corpyro.lua
'corpyro2': 'Pyro', # 142 corpyro2.lua
'corrad': 'Radar Tower', # 143 corrad.lua
'corraid': 'Ravager', # 144 corraid.lua
'corrazor': 'Razor\'s Kiss', # 145 corrazor.lua
'correap': 'Reaper', # 146 correap.lua
'corrl': 'Defender', # 147 corrl.lua
'corroach': 'Roach', # 148 corroach.lua
'corroy': 'Enforcer', # 149 corroy.lua
'corsent': 'Copperhead', # 150 corsent.lua
'corsh': 'Scrubber', # 151 corsh.lua
'corshad': 'Shadow', # 152 corshad.lua
'corsilo': 'Silencer', # 153 corsilo.lua
'corsktl': 'Skuttle', # 154 corsktl.lua
'corstorm': 'Rogue', # 155 corstorm.lua
'corsub': 'Snake', # 156 corsub.lua
'corsumo': 'Sumo', # 157 corsumo.lua
'corsy': 'Shipyard', # 158 corsy.lua
'corthud': 'Thug', # 159 corthud.lua
'cortl': 'Urchin', # 160 cortl.lua
'corvalk': 'Valkyrie', # 161 corvalk.lua
'corvamp': 'Vamp', # 162 corvamp.lua
'corvrad': 'Informant', # 163 corvrad.lua
'cremcom1': 'Strike Commander', # 164 cremcom1.lua
'dante': 'Dante', # 165 dante.lua
'dclship': 'Hunter', # 166 dclship.lua
'destroyer': 'Daimyo', # 167 destroyer.lua
'empmissile': 'Shockley', # 168 empmissile.lua
'factoryamph': 'Amphibious Operations Plant', # 169 factoryamph.lua
'factorycloak': 'Cloaky Bot Factory', # 170 factorycloak.lua
'factorygunship': 'Gunship Plant', # 171 factorygunship.lua
'factoryhover': 'Hovercraft Platform', # 172 factoryhover.lua
'factoryjump': 'Jumpjet/Specialist Plant', # 173 factoryjump.lua
'factoryplane': 'Airplane Plant', # 174 factoryplane.lua
'factoryshield': 'Shield Bot Factory', # 175 factoryshield.lua
'factoryspider': 'Spider Factory', # 176 factoryspider.lua
'factorytank': 'Heavy Tank Factory', # 177 factorytank.lua
'factoryveh': 'Light Vehicle Factory', # 178 factoryveh.lua
'fakeunit': 'Fake radar signal', # 179 fakeunit.lua
'fakeunit_aatarget': 'Fake AA target', # 180 fakeunit_aatarget.lua
'fighter': 'Avenger', # 181 fighter.lua
'firebug': 'firebug', # 182 firebug.lua
'firewalker': 'Firewalker', # 183 firewalker.lua
'funnelweb': 'Funnelweb', # 184 funnelweb.lua
'geo': 'Geothermal Powerplant', # 185 geo.lua
'gorg': 'Jugglenaut', # 186 gorg.lua
'hoveraa': 'Flail', # 187 hoveraa.lua
'hoverassault': 'Halberd', # 188 hoverassault.lua
'hoverminer': 'Dampener', # 189 hoverminer.lua
'hoverriot': 'Mace', # 190 hoverriot.lua
'hovershotgun': 'Punisher', # 191 hovershotgun.lua
'hoverskirm': 'Blischpt', # 192 hoverskirm.lua
'logkoda': 'Kodachi', # 193 logkoda.lua
'mahlazer': 'Starlight', # 194 mahlazer.lua
'missilesilo': 'Missile Silo', # 195 missilesilo.lua
'missiletower': 'Hacksaw', # 196 missiletower.lua
'napalmmissile': 'Inferno', # 197 napalmmissile.lua
'neebcomm': 'Neeb Comm', # 198 neebcomm.lua
'nest': 'Nest', # 199 nest.lua
'nsaclash': 'Scalpel', # 200 nsaclash.lua
'panther': 'Panther', # 201 panther.lua
'puppy': 'Puppy', # 202 puppy.lua
'pw_generic': 'Generic Neutral Structure', # 203 pw_generic.lua
'railgunturret': 'Splinter', # 204 railgunturret.lua
'raveparty': 'Disco Rave Party', # 205 raveparty.lua
'roost': 'Roost', # 206 roost.lua
'roostfac': 'Roost', # 207 roostfac.lua
'scorpion': 'Scorpion', # 208 scorpion.lua
'screamer': 'Screamer', # 209 screamer.lua
'seismic': 'Quake', # 210 seismic.lua
'serpent': 'Serpent', # 211 serpent.lua
'shieldarty': 'Racketeer', # 212 shieldarty.lua
'shieldfelon': 'Felon', # 213 shieldfelon.lua
'slowmort': 'Moderator', # 214 slowmort.lua
'spherecloaker': 'Eraser', # 215 spherecloaker.lua
'spherepole': 'Scythe', # 216 spherepole.lua
'spideraa': 'Tarantula', # 217 spideraa.lua
'spiderassault': 'Hermit', # 218 spiderassault.lua
'striderhub': 'Strider Hub', # 219 striderhub.lua
'subscout': 'Lancelet', # 220 subscout.lua
'tacnuke': 'Eos', # 221 tacnuke.lua
'tawf114': 'Banisher', # 222 tawf114.lua
'tele_beacon': 'Lighthouse', # 223 tele_beacon.lua
'terraunit': 'Terraform', # 224 terraunit.lua
'thicket': 'Thicket', # 225 thicket.lua
'tiptest': 'TIP test unit', # 226 tiptest.lua
'trem': 'Tremor', # 227 trem.lua
'wolverine_mine': 'Claw', # 228 wolverine_mine.lua
'zenith': 'Zenith' # 229 zenith.lua
}
def __init__(self, fn, dirname='My Games/Spring/demos/'):
"""
Initializer for the class instance. Opens the file named fn (with dirname prepended to it)
"""
if dirname:
self.filename = os.path.join(dirname, fn)
else:
self.filename = fn
# initialize some data members
self.file = None
# see .error(), this member gets set to the last error or warning message
self._lasterror = None
# major file format
self.file_version = -1
# minor file format or header size
self.headersize = 0
# engine version
self.engine_version = None
# stuff parsed from the demo file header
self.gameid = None
self.timestamp = None
self.scriptsize = 0
self.demostreamsize = 0
self.totalgametime = 0
self.elapsedrealtime = 0
# the number of players, including spectators
self.numplayers = 0
self.playerstatchunksize = 0
self.playerstatelemsize = 0
# the number of teams, each spectator is his own team
self.numteams = 0
self.teamstatchunksize = 0
self.teamstatelemsize = 0
self.teamstatperiod = 0
# file version 5 changed the definition, it is now a list
self.winningteamchunksize = 0
self.winningteam = list()
# things we can infer from the header
self.incomplete = True
self.exited = False
self.crashed = False
# the raw startscript
self.startscript = None
# real player details inferred from the start script
# the data structure is a list of tuples, with each tuple:
# 1. the player name
# 2. the actual team (allyteam) the player belongs to or -1 for spectators
# 3. the team to which the player is mapped or -1 for spectators
# 4. the key in the start script dictionary mapping to the player
self.players = None # type: List[Tuple[str, int, int, str, int, bool]]
# dictionary mapping player numbers to player names
self.playernames = None
# real team details inferred from the start script
self.teams = None # type: List[Tuple[str, int, str, bool]]
# Game type and map inferred from the start script
self.gametype = None
self.map = None
# player statistics, call method playerstats()
self.playerstatistics = None
# team statistics, call method teamstats()
self.teamstatistics = None
# open the file, if it fails self.file will remain at None
if self.get_mime_type(self.filename).endswith('gzip'):
self.file = gzip.open(self.filename, 'rb')
else:
self.file = open(self.filename, 'rb')
@staticmethod
def get_mime_type(filename): # type (str) -> str
if hasattr(magic, 'from_file'):
return magic.from_file(filename, mime=True)
elif hasattr(magic, 'detect_from_filename'):
return magic.detect_from_filename(filename).mime_type
else:
raise RuntimeError('Unknown version or type of "magic" library.')
def header(self): # type () -> bool
"""
Reads the file header and stuffs whatever it read into data members. Do this only once.
Returns False if the header was not parsed successfully, True if it did
"""
if self.file is None:
self._lasterror = 'File ' + self.filename + ' not open.'
return False
self.file.seek(0, 0)
# read the 'fixed' header first
size = struct.calcsize('=16s2i')
buffer_ = self.file.read(size)
if len(buffer_) != size:
self._lasterror = 'Unable to read fixed header from ' + self.filename
return False
values = struct.unpack('=16s2i', buffer_)
# check magic
self.headervalues = values
if values[0] != 'spring demofile\0':
self._lasterror = 'File ' + self.filename + ' is probably not a spring demofile'
return False
if values[1] != 4 and values[1] != 5:
self._lasterror = 'File ' + self.filename + ' contains a version ' + str(values[1]) + ' other than 4'
return False
self.version = values[1]
# size of the header or minor file version
self.headersize = values[2]
if self.version == 5:
size = struct.calcsize('256s')
else:
# version 4
size = struct.calcsize('16s')
nsize = len(buffer_)
buffer_ = self.file.read(size)
if len(buffer_) != size:
self._lasterror = 'Unable to read engine version from ' + self.filename
return False
if self.version == 5:
values = struct.unpack('256s', buffer_)
else:
# version 4
values = struct.unpack('16s', buffer_)
# store the engine version in self.engine_version
position = values[0].find('\0')
if position == -1:
self.engine_version = values[0]
elif position == 0:
self.engine_version = ''
else:
self.engine_version = values[0][0:position]
nsize += len(buffer_)
# the next part concerns game ID and various chunk sizes
size = struct.calcsize('=16sQ12i')
if size + nsize != self.headersize:
self._lasterror = 'File ' + self.filename + ' has header length ' + str(self.headersize) + ', expected ' + str(size + len(buffer_))
return False
buffer_ = self.file.read(size)
if len(buffer_) != size:
self._lasterror = 'Unable to read variable header from ' + self.filename
return False
values = struct.unpack('=16sQ12i', buffer_)
self.gameid = values[0]
self.timestamp = values[1]
self.scriptsize = values[2]
self.demostreamsize = values[3]
self.totalgametime = values[4]
self.elapsedrealtime = values[5]
self.numplayers = values[6]
self.playerstatchunksize = values[7]
self.playerstatelemsize = values[8]
self.numteams = values[9]
self.teamstatchunksize = values[10]
self.teamstatelemsize = values[11]
self.teamstatperiod = values[12]
if self.version == 4:
if values[13] != -1:
self.winningteam.append(values[13])
else:
self.winningteamchunksize = values[13]
if self.demostreamsize == 0:
self._lasterror = 'Spring crashed, recording incomplete'
self.crashed = True
else:
if self.playerstatchunksize == 0 and self.teamstatchunksize == 0:
# demo recording was stopped before the game ended; player quit spring before the game ended
self.incomplete = True
self._lasterror = self.filename + ' is not a complete demo'
elif self.winningteam == -1:
# self.filename + ': ' + str(self.numplayers) + ' players in ' + str(self.numteams) + ' teams, nobody won, the game was exited before it could end.'
self.exited = True
self.incomplete = False
else:
self.incomplete = False
# print(self.filename + ': ' + str(self.numplayers) + ' players in ' + str(self.numteams) + ' teams, team #' + str(self.winningteam) + ' won.')
return True
def script(self): # type () -> Union[None, str]
"""
Read the startscript and parse it.
On success, the raw startscript is returned, on failure None is returned
"""
self.startscript = None
if self.file is None:
self._lasterror = 'File ' + self.filename + ' not open.'
return None
if self.headersize == 0:
self._lasterror = 'Cannot read start script, read the header first'
return None
if self.scriptsize == 0:
self._lasterror = 'Cannot read start script, none is recorded'
return None
self.file.seek(self.headersize, 0)
buffer = self.file.read(self.scriptsize)
if len(buffer) != self.scriptsize:
self._lasterror = 'File ' + self.filename + ' contains an incomplete (broken) start script'
return None
self.startscript = buffer
# now parse the start script, begin by initializing a root dictionary
self.settings = dict()
level = 0
dictstack = list()
currentdict = self.settings
dictstack.append(currentdict)
# precompile some regular expressions
hash_ = re.compile(r'^\s*\[\s*(\w+)\s*\]\s*$')
opening = re.compile(r'^\s*\{\s*$')
closing = re.compile(r'^\s*\}\s*$')
simple = re.compile(r'^\s*(\w+)\s*=(.*?);\s*$')
empty = re.compile(r'^\s*$')
position = 0
expectedopen = False
errors = 0
currentline = 0
self._lasterror = ''
while position < len(self.startscript):
nextposition = self.startscript.find('\n', position)
if nextposition == -1:
position = len(self.startscript)
continue
line = self.startscript[position:nextposition]
position = nextposition + 1
currentline = currentline + 1
test = hash_.match(line)
if test is not None:
name = test.group(1)
if name in currentdict:
self._lasterror += 'Duplicate dictionary entry ' + name + ' in start script at line ' + str(currentline) + '\n'
errors = errors + 1
else:
newdict = dict()
currentdict[name] = newdict
expectedopen = True
lastdict = name
continue
test = opening.match(line)
if test is not None:
if not expectedopen:
self._lasterror += 'No { expected in start script at line ' + str(currentline) + '\n'
errors = errors + 1
continue
expectedopen = False
level = level + 1
dictstack.append(currentdict[lastdict])
currentdict = currentdict[lastdict]
# print('Pushed ' + lastdict + ' at level ' + str(level) + '[' + str(len(dictstack)) + ']')
continue
test = empty.match(line)
if test is not None:
continue
test = closing.match(line)
if test is not None:
if expectedopen:
self._lasterror += 'Expected opening { in start script at line ' + str(currentline) + '\n'
errors = errors + 1
continue
level = level - 1
if level < 0:
self._lasterror += 'Unmatched } in start script at line ' + str(currentline) + '\n'
errors = errors + 1
continue
dictstack.pop()
currentdict = dictstack[-1]
# print('Popped to level ' + str(level) + '[' + str(len(dictstack)) + ']')
continue
test = simple.match(line)
if test is not None:
if expectedopen:
self._lasterror += 'Expected opening { in start script at line ' + str(currentline) + '\n'
errors = errors + 1
continue
name = test.group(1)
if name in currentdict:
self._lasterror += 'Duplicate dictionary entry ' + name + ' in start script at line ' + str(currentline) + '\n'
errors = errors + 1
currentdict[name] = test.group(2).strip()
continue
if len(line) < 70:
self._lasterror += 'No match: ' + line + '\n'
else:
self._lasterror += 'No match: ' + line[0:60] + ' ... ' + '\n'
errors = errors + 1
if level != 0:
self._lasterror = 'Unmatched { at EOF in start script' + '\n'
errors = errors + 1
if errors != 0:
self._lasterror += str(errors) + ' error(s) while parsing start script in file ' + self.filename
return None
self._lasterror = None
if 'game' not in self.settings or not isinstance(self.settings['game'], dict):
self._lasterror = 'Game settings not present or not a dictionary'
return None
if 'gametype' in self.settings['game'] and isinstance(self.settings['game']['gametype'], basestring):
self.gametype = self.settings['game']['gametype']
else:
self.gametype = 'Unknown'
if 'mapname' in self.settings['game'] and isinstance(self.settings['game']['mapname'], basestring):
self.map = self.settings['game']['mapname']
else:
self.map = 'Unknown map'
# find out who the players are
playerseq = 0
self.players = list()
self.playernames = dict()
while playerseq < 128:
if 'player' + str(playerseq) not in self.settings['game']:
playerseq = playerseq + 1
continue
dictname = 'player' + str(playerseq)
playerdict = self.settings['game'][dictname]
if not isinstance(playerdict, dict):
self._lasterror = 'Entry for ' + dictname + ' is not a dictionary'
return None
if 'name' not in playerdict or 'spectator' not in playerdict:
self._lasterror = 'Game settings incomplete for ' + dictname
return None
playername = playerdict['name']
spectator = playerdict['spectator']
if spectator == '0':
# this person is playing
if 'team' not in playerdict:
self._lasterror = 'Game settings incomplete for ' + dictname + ', active player ' + playername
return None
team = int(playerdict['team'])
if 'team' + str(team) not in self.settings['game']:
self._lasterror = 'Unable to find team for ' + dictname + ', active player ' + playername
return None
teamdict = self.settings['game']['team' + str(team)]
if not isinstance(teamdict, dict):
self._lasterror = 'Team entry for active player ' + dictname + ' is not a dictionary'
return None
if 'allyteam' not in teamdict:
self._lasterror = 'Unable to find the real team for ' + dictname + ', active player ' + playername
return None
realteam = int(teamdict['allyteam'])
# note that the value of the teamleader entry in the teamdict should be the same as the player sequence number
self.players.append((playername, realteam, team, dictname, playerseq, False))
else:
# this person is *not* playing, so we make a default entry in the list
self.players.append((playername, -1, -1, dictname, playerseq, False))
self.playernames[playerseq] = playername
playerseq = playerseq + 1
# find out if there are AI's
aiseq = 0
ais = list()
while aiseq < 128:
if 'ai' + str(aiseq) not in self.settings['game']:
aiseq = aiseq + 1
continue
dictname = 'ai' + str(aiseq)
playerdict = self.settings['game'][dictname]
if not isinstance(playerdict, dict):
self._lasterror = 'Entry for ' + dictname + ' is not a dictionary'
return None
if ('shortname' not in playerdict and 'name' not in playerdict) or 'team' not in playerdict:
self._lasterror = 'Game settings incomplete for ' + dictname
return None