Skip to content

Commit c660195

Browse files
author
Graylin Kim
committed
Move the header parsing into the Replay class.
The code fits better in the constructor. The constructor has also been changed to keep a reference to a copy of the replay's reader configuration.
1 parent 274c908 commit c660195

File tree

3 files changed

+20
-19
lines changed

3 files changed

+20
-19
lines changed

sc2reader/__init__.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import os
22

3+
import copy
4+
35
from config import FULL, PARTIAL, CUSTOM, FILES, PROCESSORS, READERS
46
from mpyq import MPQArchive
57
from objects import Replay
6-
from utils import ReplayBuffer, read_header
8+
from utils import ReplayBuffer
79

810
__version__ = "0.3.0"
911
__author__ = "Graylin Kim <[email protected]>"
@@ -14,7 +16,7 @@ class SC2Reader(object):
1416
<<usage documentation here>>
1517
'''
1618

17-
def __init__(self, parse_type="FULL", directory="", processors=[], debug=False, files=[], verbose=False):
19+
def __init__(self, parse_type="FULL", directory="", processors=[], debug=False, files=[], verbose=False, copy=copy.copy):
1820
try:
1921
#Update and save the reader configuration
2022
parse_type = parse_type.upper()
@@ -38,8 +40,7 @@ def read(self, location):
3840

3941
else:
4042
with open(location) as replay_file:
41-
release,frames = read_header(replay_file)
42-
replay = Replay(location,release,frames)
43+
replay = Replay(self.copy(self),replay_file)
4344
archive = MPQArchive(location,listfile=False)
4445

4546
for file in self.files:

sc2reader/objects.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,25 @@
33

44
from sc2reader.constants import *
55
from sc2reader.data import GameObject, ABILITIES
6-
from sc2reader.utils import PersonDict,Selection
7-
6+
from sc2reader.utils import PersonDict, Selection, read_header
87

98
class Replay(object):
109

11-
def __init__(self, filename, release, frames=0):
12-
#Split and offset for matching indexes if they pass in the release string
13-
if isinstance(release,basestring): release = [None]+release.split('.')
14-
15-
#Assign all the relevant information to the replay object
16-
self.build = release[4]
17-
self.versions = (release[1], release[2], release[3], release[4])
18-
self.release_string = "%s.%s.%s.%s" % self.versions
19-
self.frames, self.seconds = (frames, frames/16)
10+
def __init__(self, reader, replay_file):
11+
#Useful references
12+
self.reader = reader
13+
self.filename = replay_file.name
14+
15+
#header information
16+
self.versions,self.frames = read_header(replay_file)
17+
self.build = self.versions[4]
18+
self.release_string = "%s.%s.%s.%s" % tuple(self.versions[1:5])
19+
self.seconds = self.frames/16
2020
self.length = (self.seconds/60, self.seconds%60)
2121

22+
#default values, filled in during file read
2223
self.player_names = list()
2324
self.other_people = set()
24-
self.filename = filename
2525
self.speed = ""
2626
self.type = ""
2727
self.category = ""
@@ -43,7 +43,7 @@ def __init__(self, filename, release, frames=0):
4343
self.winner_known = False
4444

4545
# Set in parsers.DetailParser.load, should we hide this?
46-
self.file_time = None # Probably number milliseconds since EPOCH
46+
self.file_time = None
4747

4848
# TODO: Test EPOCH differences between MacOsX and Windows
4949
# http://en.wikipedia.org/wiki/Epoch_(reference_date)

sc2reader/utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -507,5 +507,5 @@ def read_header(file):
507507
data_size = buffer.read_int(LITTLE_ENDIAN)
508508
header_data = buffer.read_data_struct()
509509

510-
#return the release array (version,major,minor,build) and frame count
511-
return header_data[1],header_data[3]
510+
#array [unknown,version,major,minor,build,unknown] and frame count
511+
return header_data[1].values(),header_data[3]

0 commit comments

Comments
 (0)