|
1 | 1 | #!/usr/bin/env python |
2 | 2 | # -*- coding: utf-8 -*- |
3 | 3 |
|
4 | | -import os, sys |
5 | | -sys.path.append(r'C:\Users\graylinkim\sc2reader') |
6 | | -from sc2reader import Replay |
7 | | -from sc2reader.exceptions import ParseError |
| 4 | +import os,sys |
8 | 5 |
|
9 | | -def do_file(filename): |
10 | | - |
| 6 | +import argparse |
| 7 | + |
| 8 | +import sc2reader |
| 9 | +from sc2reader.utils import get_files |
| 10 | +from sc2reader.exceptions import ReadError |
| 11 | + |
| 12 | +def doFile(filename, arguments): |
| 13 | + '''Prints summary information about SC2 replay file''' |
11 | 14 | try: |
12 | | - replay = Replay(filename) |
13 | | - print "\nStarcraft II Version %s" % replay.release_string |
14 | | - print "%s on %s played on %s" % (replay.type, replay.map, replay.date) |
15 | | - |
16 | | - #Player[0] is None so that players can be indexed by ID |
17 | | - for team,players in replay.teams.iteritems(): |
18 | | - print "\n\tTeam %s: %s" % (team, replay.results[team]) |
19 | | - for player in players: |
20 | | - print "\t\t%s" % player |
21 | | - |
22 | | - except ParseError as e: |
23 | | - prev = e.replay.events[-1] |
24 | | - event = e.event |
25 | | - bytes = e.event.bytes+e.bytes.peek(30) |
26 | | - print "\nVersion %s replay:\n\t%s" % (e.replay.release_string, e.replay.file) |
27 | | - print "\tError parsing event Type=%s, Code=%s" % (hex(e.event.type), hex(e.event.code)) |
28 | | - print "\tPrevious Event: %s" % prev.name |
29 | | - print "\t\t"+prev.bytes |
| 15 | + replay = sc2reader.read_file(filename, debug=True) |
| 16 | + except ReadError as e: |
| 17 | + prev = e.game_events[-1] |
| 18 | + print "\nVersion {0} replay:\n\t{1}".format(e.replay.release_string, e.replay.filename) |
| 19 | + print "\t{0}, Type={1:X}, Code={2:X}".format(e.msg, e.type,e.code) |
| 20 | + print "\tPrevious Event: {0}".format(prev.name) |
| 21 | + print "\t\t"+prev.bytes.encode('hex') |
30 | 22 | print "\tFollowing Bytes:" |
31 | | - print "\t\t"+bytes |
32 | | - |
33 | | - |
34 | | -def do_dir(dirname): |
35 | | - for name in os.listdir(dirname): |
36 | | - replaypath = os.path.join(dirname, name) |
37 | | - |
38 | | - if os.path.isfile(replaypath): |
39 | | - do_file(replaypath) |
40 | | - elif os.path.isdir(replaypath): |
41 | | - do_dir(replaypath) |
42 | | - |
43 | | -assert len(sys.argv)>1, "A path to at least 1 sc2replay file is required" |
44 | | -for filename in sys.argv[1:]: |
45 | | - if os.path.isfile(filename): |
46 | | - do_file(filename) |
47 | | - elif os.path.isdir(filename): |
48 | | - do_dir(filename) |
| 23 | + print "\t\t"+e.buffer.read_range(e.location,e.location+30).encode('hex') |
| 24 | + print "Error with '{0}': ".format(filename) |
| 25 | + print e |
| 26 | + return |
| 27 | + except TypeError as e: |
| 28 | + print "Error with '%s': " % filename, |
| 29 | + print e |
| 30 | + return |
| 31 | + except ValueError as e: |
| 32 | + print "Error with '%s': " % filename, |
| 33 | + print e |
| 34 | + return |
| 35 | + |
| 36 | + if arguments.map: |
| 37 | + print " Map: {0}".format(replay.map) |
| 38 | + if arguments.length: |
| 39 | + print " Length: {0}:{1}".format(*replay.length) |
| 40 | + if arguments.date: |
| 41 | + print " Date: {0}".format(replay.date) |
| 42 | + if arguments.teams: |
| 43 | + races = list() |
| 44 | + for team in replay.teams: |
| 45 | + races.append(''.join([player.chosen_race[0] for player in team.players])) |
| 46 | + print " Teams: {0}".format("v".join(races)) |
| 47 | + |
| 48 | + for team in replay.teams: |
| 49 | + print " Team {0}\t{1} ({2})".format(team.number,team.players[0].name,team.players[0].chosen_race[0]) |
| 50 | + for player in team.players[1:]: |
| 51 | + print " \t{0} ({1})".format(player.name,player.chosen_race[0]) |
| 52 | + if arguments.messages: |
| 53 | + print " Messages:" |
| 54 | + for message in replay.messages: |
| 55 | + minutes, seconds = ((message.time/16)/60,(message.time/16)%60) |
| 56 | + player_name = message.sender.name |
| 57 | + print " %02i:%02i %s:\t%s" % (minutes, seconds, player_name, message.text) |
| 58 | + if arguments.version: |
| 59 | + print " Version: {0}".format(replay.release_string) |
| 60 | + |
| 61 | + print |
| 62 | + |
| 63 | +if __name__ == '__main__': |
| 64 | + parser = argparse.ArgumentParser(description='Prints basic information from SC2 replay files or directories.') |
| 65 | + parser.add_argument('paths', metavar='filename', type=str, nargs='+', |
| 66 | + help="Paths to one or more SC2Replay files or directories") |
| 67 | + parser.add_argument('--date', action="store_true", default=True, |
| 68 | + help="Print game date") |
| 69 | + parser.add_argument('--length', action="store_true", default=False, |
| 70 | + help="Print game duration mm:ss in game time (not real time)") |
| 71 | + parser.add_argument('--map', action="store_true", default=True, |
| 72 | + help="Print map name") |
| 73 | + parser.add_argument('--messages', action="store_true", default=False, |
| 74 | + help="Print in-game player chat messages") |
| 75 | + parser.add_argument('--teams', action="store_true", default=True, |
| 76 | + help="Print teams, their players, and the race matchup") |
| 77 | + parser.add_argument('--version', action="store_true", default=True, |
| 78 | + help="Print the release string as seen in game") |
| 79 | + parser.add_argument('--recursive', action="store_true", default=True, |
| 80 | + help="Recursively read through directories of replays") |
| 81 | + |
| 82 | + arguments = parser.parse_args() |
| 83 | + |
| 84 | + for path in arguments.paths: |
| 85 | + if arguments.recursive: |
| 86 | + files = get_files(path) |
| 87 | + else: |
| 88 | + files = get_files(path, depth=0) |
| 89 | + |
| 90 | + for file in files: |
| 91 | + print "\n--------------------------------------\n{0}\n".format(file) |
| 92 | + doFile(file, arguments) |
0 commit comments