|
1 | 1 | import os |
2 | 2 |
|
| 3 | +from config import FULL, PARTIAL, CUSTOM, FILES, PROCESSORS, READERS |
3 | 4 | from mpyq import MPQArchive |
4 | | -from utils import ReplayBuffer, LITTLE_ENDIAN |
5 | 5 | from objects import Replay |
6 | | -from processors import * |
7 | | -from readers import * |
| 6 | +from utils import read_header |
8 | 7 |
|
9 | 8 | __version__ = "0.3.0" |
| 9 | +__author__ = "Graylin Kim <[email protected]>" |
10 | 10 |
|
11 | | -FULL = "FULL" |
12 | | -PARTIAL = "PARTIAL" |
13 | | -CUSTOM = "CUSTOM" |
14 | | - |
15 | | -FILES = { |
16 | | - "FULL": [ |
17 | | - 'replay.initData', |
18 | | - 'replay.details', |
19 | | - 'replay.attributes.events', |
20 | | - 'replay.message.events', |
21 | | - 'replay.game.events' |
22 | | - ], |
23 | | - |
24 | | - "PARTIAL": [ |
25 | | - 'replay.initData', |
26 | | - 'replay.details', |
27 | | - 'replay.attributes.events', |
28 | | - 'replay.message.events' |
29 | | - ], |
30 | | -} |
31 | | - |
32 | | -PROCESSORS = { |
33 | | - "FULL": [ |
34 | | - PeopleProcessor, |
35 | | - AttributeProcessor, |
36 | | - TeamsProcessor, |
37 | | - MessageProcessor, |
38 | | - RecorderProcessor, |
39 | | - EventProcessor, |
40 | | - ApmProcessor, |
41 | | - ResultsProcessor, |
42 | | - ], |
| 11 | +class SC2Reader(object): |
| 12 | + ''' Class level interface to sc2reader. |
43 | 13 | |
44 | | - "PARTIAL": [ |
45 | | - PeopleProcessor, |
46 | | - AttributeProcessor, |
47 | | - TeamsProcessor, |
48 | | - MessageProcessor, |
49 | | - RecorderProcessor, |
50 | | - ], |
51 | | -} |
52 | | - |
53 | | -class ReaderMap(object): |
54 | | - def __getitem__(self,key): |
55 | | - if int(key) in (16117,16195,16223,16291): |
56 | | - return { |
57 | | - 'replay.initData': InitDataReader(), |
58 | | - 'replay.details': DetailsReader(), |
59 | | - 'replay.attributes.events': AttributeEventsReader(), |
60 | | - 'replay.message.events': MessageEventsReader(), |
61 | | - 'replay.game.events': GameEventsReader(), |
62 | | - } |
63 | | - |
64 | | - elif int(key) in (16561,16605,16755,16939): |
65 | | - return { |
66 | | - 'replay.initData': InitDataReader(), |
67 | | - 'replay.details': DetailsReader(), |
68 | | - 'replay.attributes.events': AttributeEventsReader(), |
69 | | - 'replay.message.events': MessageEventsReader(), |
70 | | - 'replay.game.events': GameEventsReader_16561(), |
71 | | - } |
72 | | - |
73 | | - elif int(key) in (17326,17682,17811,18092,18221,18317): |
74 | | - return { |
75 | | - 'replay.initData': InitDataReader(), |
76 | | - 'replay.details': DetailsReader(), |
77 | | - 'replay.attributes.events': AttributeEventsReader_17326(), |
78 | | - 'replay.message.events': MessageEventsReader(), |
79 | | - 'replay.game.events': GameEventsReader_16561(), |
80 | | - } |
81 | | - |
82 | | - #This one is also a catch all. If the build isn't recognized, try to use |
83 | | - #the latest parsing code and hope that it works! |
84 | | - elif int(key) in (18574,) or True: |
85 | | - return { |
86 | | - 'replay.initData': InitDataReader(), |
87 | | - 'replay.details': DetailsReader(), |
88 | | - 'replay.attributes.events': AttributeEventsReader_17326(), |
89 | | - 'replay.message.events': MessageEventsReader(), |
90 | | - 'replay.game.events': GameEventsReader_18574(), |
91 | | - } |
92 | | - |
93 | | -READERS = ReaderMap() |
94 | | - |
95 | | -def read_header(file): |
96 | | - buffer = ReplayBuffer(file) |
97 | | - |
98 | | - #Check the file type for the MPQ header bytes |
99 | | - if buffer.read_hex(4).upper() != "4D50511B": |
100 | | - print "Header Hex was: %s" % buffer.read_hex(4).upper() |
101 | | - raise ValueError("File '%s' is not an MPQ file" % file.name) |
102 | | - |
103 | | - #Extract replay header data, we don't actually use this for anything |
104 | | - max_data_size = buffer.read_int(LITTLE_ENDIAN) #possibly data max size |
105 | | - header_offset = buffer.read_int(LITTLE_ENDIAN) #Offset of the second header |
106 | | - data_size = buffer.read_int(LITTLE_ENDIAN) #possibly data size |
107 | | - |
108 | | - #Extract replay attributes from the mpq |
109 | | - data = buffer.read_data_struct() |
110 | | - |
111 | | - #return the release and frames information |
112 | | - return data[1],data[3] |
| 14 | + <<usage documentation here>> |
| 15 | + ''' |
113 | 16 |
|
114 | | -class SC2Reader(object): |
115 | 17 | def __init__(self, parse="FULL", directory="", processors=[], debug=False, files=None, verbose=False): |
116 | | - #Sanitize the parse level |
117 | | - parse = parse.upper() |
118 | | - if parse not in ("FULL","PARTIAL","CUSTOM"): |
| 18 | + try: |
| 19 | + #Update and save the reader configuration |
| 20 | + parse = parse.upper() |
| 21 | + files = FILES.get(parse,files) |
| 22 | + processors = PROCESSORS.get(parse,processors) |
| 23 | + self.__dict__.update(locals()) |
| 24 | + except KeyError: |
119 | 25 | raise ValueError("Unrecognized parse argument `%s`" % parse) |
120 | | - |
121 | | - #get our defaults and save preferences |
122 | | - files = FILES.get(parse,files) |
123 | | - processors = PROCESSORS.get(parse,processors) |
124 | | - self.__dict__.update(locals()) |
125 | | - |
| 26 | + |
126 | 27 | def read(self, location): |
127 | | - #Sanitize the location provided (accounting for directory option) |
128 | 28 | if self.directory: |
129 | 29 | location = os.path.join(self.directory,location) |
130 | | - if not os.path.exists(location): |
131 | | - raise ValueError("Path `%s` cannot be found" % location) |
132 | | - |
| 30 | + |
133 | 31 | if self.verbose: print "Reading: %s" % location |
134 | | - |
135 | | - #If its a directory, read each subfile/directory and combine the lists |
| 32 | + |
136 | 33 | if os.path.isdir(location): |
| 34 | + #SC2Reader::read each subfile/directory and combine the lists |
137 | 35 | read = lambda file: self.read(os.path.join(location,file)) |
138 | 36 | tolist = lambda x: [x] if isinstance(x,Replay) else x |
139 | 37 | return sum(map(tolist,(read(x) for x in os.listdir(location))),[]) |
140 | | - |
141 | | - #The primary replay reading routine |
| 38 | + |
142 | 39 | else: |
143 | | - if(os.path.splitext(location)[1].lower() != '.sc2replay'): |
144 | | - raise TypeError("Target file must of the SC2Replay file extension") |
145 | | - |
146 | 40 | with open(location) as replay_file: |
147 | | - #Use the MPQ Header information to initialize the replay |
148 | 41 | release,frames = read_header(replay_file) |
149 | 42 | replay = Replay(location,release,frames) |
150 | 43 | archive = MPQArchive(location,listfile=False) |
151 | | - |
152 | | - #Extract and Parse the relevant files based on parse level |
| 44 | + |
153 | 45 | for file in self.files: |
154 | 46 | buffer = ReplayBuffer(archive.read_file(file)) |
155 | 47 | READERS[replay.build][file].read(buffer,replay) |
156 | | - |
157 | | - #Do cleanup and post processing |
| 48 | + |
158 | 49 | for process in self.processors: |
159 | 50 | replay = process(replay) |
160 | | - |
| 51 | + |
161 | 52 | return replay |
162 | 53 |
|
163 | 54 | def configure(self,**options): |
164 | 55 | self.__dict__.update(options) |
165 | | - |
166 | | - |
167 | | -#Prepare the lightweight interface |
| 56 | + |
| 57 | + |
| 58 | +''' The package level interface is just a lightweight wrapper around a default |
| 59 | + SC2Reader class. See the documentation above for usage details ''' |
| 60 | + |
168 | 61 | __defaultSC2Reader = SC2Reader() |
169 | 62 |
|
170 | 63 | def configure(**options): |
|
0 commit comments