Skip to content

Commit 281adba

Browse files
committed
Merge branch 'optimized'
2 parents af64dc0 + 2256bcc commit 281adba

File tree

4 files changed

+23
-10
lines changed

4 files changed

+23
-10
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@
33
dist
44
build
55
sc2reader.egg-info
6+
replay_profile
67
PKG-INFO.txt

profile.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import cProfile
2+
from pstats import Stats
3+
4+
import os,sys
5+
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
6+
from sc2reader import Replay
7+
8+
9+
cProfile.run("Replay('test_replays/build17811/1.sc2replay')","replay_profile")
10+
stats = Stats("replay_profile")
11+
stats.strip_dirs().sort_stats("time").print_stats(15)

sc2reader/parsers.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ def load(self, replay, filecontents):
143143
replay.messages = list()
144144
bytes, time = ByteStream(filecontents), 0
145145

146-
while(bytes.length!=0):
146+
while(bytes.remaining!=0):
147147
time += bytes.get_timestamp()
148148
player_id = bytes.get_big_int(1) & 0x0F
149149
flags = bytes.get_big_int(1)
@@ -221,7 +221,7 @@ def load(self, replay, filecontents):
221221
#set up an event list, start the timer, and process the file contents
222222
replay.events, elapsed_time, bytes = list(), 0, ByteStream(filecontents)
223223

224-
while bytes.length > 0:
224+
while bytes.remaining > 0:
225225
#First section is always a timestamp marking the elapsed time
226226
#since the last eventObjectlisted
227227
location = hex(bytes.cursor)

sc2reader/utils.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,21 @@ class ByteStream(object):
33
"""Most functions will return the byte_code as well when requested"""
44

55
def __init__(self, stream):
6+
self.__cbyte = 0
67
self.cursor = -1 #First element is 0
78
self.stream = stream.encode("hex").upper()
89

910
def get_big(self, number, byte_code=False):
1011
#Do a sanity check, if streams are parsed right this won't ever happen
11-
if len(self.stream) < number*2:
12-
msg = "Stream is only %s bytes long; %s bytes requested"
13-
raise ValueError(msg % (self.length, number) )
12+
if len(self.stream)-self.__cbyte < number*2:
13+
msg = "Stream only has %s bytes left; %s bytes requested"
14+
raise ValueError(msg % (self.remaining, number) )
1415

1516
#For big endian, the byte_string is the result
16-
result = self.stream[:number*2]
17+
result = self.stream[self.__cbyte:self.__cbyte+number*2]
1718

1819
#Move the ByteStream forward
19-
self.stream = self.stream[number*2:]
20+
self.__cbyte = self.__cbyte + number*2
2021
self.cursor = self.cursor + number
2122

2223
if byte_code:
@@ -31,7 +32,7 @@ def skip(self, number, byte_code=False):
3132
self.get_big(number)
3233

3334
def peek(self, number):
34-
return self.stream[:number*2]
35+
return self.stream[self.__cbyte:self.__cbyte + number*2]
3536

3637
def get_little(self, number, byte_code=False):
3738
#Get a list of the next 'number' of bytes from the stream
@@ -166,5 +167,5 @@ def parse_serialized_data(self, byte_code=False):
166167
return data
167168

168169
@property
169-
def length(self):
170-
return len(self.stream)
170+
def remaining(self):
171+
return len(self.stream)-self.__cbyte

0 commit comments

Comments
 (0)