|
| 1 | +#!/usr/bin/env python |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | + |
| 4 | +import sys, os, re |
| 5 | +import termios |
| 6 | +import fcntl |
| 7 | + |
| 8 | +import sc2reader |
| 9 | +from sc2reader.objects import * |
| 10 | + |
| 11 | +def myGetch(): |
| 12 | + fd = sys.stdin.fileno() |
| 13 | + oldterm = termios.tcgetattr(fd) |
| 14 | + newattr = termios.tcgetattr(fd) |
| 15 | + newattr[3] = newattr[3] & ~termios.ICANON & ~termios.ECHO |
| 16 | + termios.tcsetattr(fd, termios.TCSANOW, newattr) |
| 17 | + oldflags = fcntl.fcntl(fd, fcntl.F_GETFL) |
| 18 | + fcntl.fcntl(fd, fcntl.F_SETFL, oldflags | os.O_NONBLOCK) |
| 19 | + try: |
| 20 | + while 1: |
| 21 | + try: |
| 22 | + c = sys.stdin.read(1) |
| 23 | + break |
| 24 | + except IOError: pass |
| 25 | + finally: |
| 26 | + termios.tcsetattr(fd, termios.TCSAFLUSH, oldterm) |
| 27 | + fcntl.fcntl(fd, fcntl.F_SETFL, oldflags) |
| 28 | + |
| 29 | +def get_args(): |
| 30 | + import argparse |
| 31 | + |
| 32 | + parser = argparse.ArgumentParser( |
| 33 | + description="""Step by step replay of game events; shows only the |
| 34 | + Initialization, Ability, and Selection events by default. Press any |
| 35 | + key to advance through the events in sequential order.""", |
| 36 | + epilog="And that's all folks") |
| 37 | + |
| 38 | + parser.add_argument('FILE',type=str, |
| 39 | + help="The file you would like to replay") |
| 40 | + parser.add_argument('--player',default=0, type=int, |
| 41 | + help="The number of the player you would like to watch. Defaults to 0 (All).") |
| 42 | + parser.add_argument('--bytes',default=False,action="store_true", |
| 43 | + help="Displays the byte code of the event in hex after each event.") |
| 44 | + parser.add_argument('--hotkeys',default=False,action="store_true", |
| 45 | + help="Shows the hotkey events in the event stream.") |
| 46 | + parser.add_argument('--cameras',default=False,action="store_true", |
| 47 | + help="Shows the camera events in the event stream.") |
| 48 | + |
| 49 | + return parser.parse_args() |
| 50 | + |
| 51 | +def main(): |
| 52 | + args = get_args() |
| 53 | + replay = sc2reader.read_file(args.FILE,debug=True) |
| 54 | + print "Release {0}".format(replay.release_string) |
| 55 | + print "{0} on {1}".format(replay.type,replay.map) |
| 56 | + for player in replay.players: |
| 57 | + print player |
| 58 | + print "\n--------------------------\n\n" |
| 59 | + |
| 60 | + # Allow picking of the player to 'watch' |
| 61 | + if args.player: |
| 62 | + events = replay.player[args.player].events |
| 63 | + else: |
| 64 | + events = replay.events |
| 65 | + |
| 66 | + # Loop through the events |
| 67 | + data = sc2reader.config.build_data[replay.build] |
| 68 | + for event in events: |
| 69 | + # Use their options to filter the event stream |
| 70 | + if isinstance(event,AbilityEvent) or\ |
| 71 | + isinstance(event,SelectionEvent) or\ |
| 72 | + isinstance(event,PlayerJoinEvent) or\ |
| 73 | + isinstance(event, PlayerLeaveEvent) or\ |
| 74 | + isinstance(event,GameStartEvent) or\ |
| 75 | + (args.hotkeys and isinstance(event,HotkeyEvent)) or\ |
| 76 | + (args.cameras and isinstance(event,CameraEvent)): |
| 77 | + |
| 78 | + event.apply(data) |
| 79 | + print event |
| 80 | + if args.bytes: |
| 81 | + print "\t"+event.bytes.encode('hex') |
| 82 | + |
| 83 | + if re.search('UNKNOWN|ERROR', str(event)): |
| 84 | + myGetch() |
| 85 | + |
| 86 | + |
| 87 | +if __name__ == '__main__': |
| 88 | + main() |
0 commit comments