Skip to content

Commit a047164

Browse files
committed
Partway through ByteStream rewrite
1 parent 2e4ba57 commit a047164

File tree

4 files changed

+17
-23
lines changed

4 files changed

+17
-23
lines changed

sc2reader/eventparsers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ def load_get_hotkey_changed(self, event, bytes, first):
283283

284284
extras = first >> 3
285285
event.bytes += bytes.peek(extras+1)
286-
second = bytes.get_big(1)
286+
second = bytes.get_big_int(1)
287287
bytes.skip(extras)
288288

289289
if first & 0x04:

sc2reader/parsers.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,9 @@ def load_header(self, replay, bytes):
7070
def load_attribute(self, replay, bytes):
7171
#Get the attribute data elements
7272
attr_data = [
73-
bytes.get_big(4), #Header
73+
bytes.get_little_int(4), #Header
7474
bytes.get_little_int(4), #Attr Id
75-
bytes.get_big_int(1), #Player
75+
bytes.get_little_int(1), #Player
7676
bytes.get_little(4) #Value
7777
]
7878

sc2reader/replay.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,8 @@ def _parse_header(self):
8686
source = ByteStream(open(self.filename).read())
8787

8888
#Check the file type for the MPQ header bytes
89-
if source.get_big(4) != "4D50511B":
90-
raise TypeError("File '%s' is not an MPQ file" % self.filename)
89+
#if source.get_big(4) != "4D50511B":
90+
# raise TypeError("File '%s' is not an MPQ file" % self.filename)
9191

9292
#Extract replay header data
9393
max_data_size = source.get_little_int(4) #possibly data max size

sc2reader/utils.py

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -29,42 +29,35 @@ def __setitem__(self,key,value):
2929
super(PlayerDict,self).__setitem__(value.pid,value)
3030

3131

32-
32+
from StringIO import StringIO
33+
from os import SEEK_CUR,SEEK_END
34+
3335
class ByteStream(object):
3436
"""Track and return the bytes for investigative and debugging purposes"""
3537
"""Most functions will return the byte_code as well when requested"""
3638

3739
def __init__(self, stream):
3840
self.__cbyte = 0
3941
self.cursor = -1 #First element is 0
40-
self.stream = stream.encode("hex").upper()
42+
self.stream = StringIO(stream)
4143

4244
def get_big(self, number, byte_code=False):
43-
#Do a sanity check, if streams are parsed right this won't ever happen
44-
if len(self.stream)-self.__cbyte < number*2:
45-
msg = "Stream only has %s bytes left; %s bytes requested"
46-
raise ValueError(msg % (self.remaining, number) )
47-
48-
#For big endian, the byte_string is the result
49-
result = self.stream[self.__cbyte:self.__cbyte+number*2]
50-
51-
#Move the ByteStream forward
52-
self.__cbyte = self.__cbyte + number*2
53-
self.cursor = self.cursor + number
54-
45+
result = self.stream.read(number)
5546
if byte_code:
5647
return result, result
5748
return result
5849

5950
def skip(self, number, byte_code=False):
60-
#This is just an alias for get_big really, still return the byte_string
51+
#This is just an alias for get_big, return the byte_string if asked
6152
#so that it can be recorded for event analysis
6253
if byte_code:
6354
return self.get_big(number)
6455
self.get_big(number)
6556

6657
def peek(self, number):
67-
return self.stream[self.__cbyte:self.__cbyte + number*2]
58+
ret = self.stream.read(number)
59+
self.stream.seek(-number,SEEK_CUR)
60+
return ret
6861

6962
def get_little(self, number, byte_code=False):
7063
#Get a list of the next 'number' of bytes from the stream
@@ -83,8 +76,9 @@ def get_little(self, number, byte_code=False):
8376
def get_string(self, length, byte_code=False):
8477
string, bytes = self.get_big(length, byte_code=True)
8578
if byte_code:
86-
return string.decode("hex"), bytes
87-
return string.decode("hex")
79+
#This would be easier if I knew what the original encoding was
80+
return string.endcode("hex").decode("hex"), bytes
81+
return string.encode("hex").decode("hex")
8882

8983
def get_big_int(self, number, byte_code=False):
9084
result, byte_string = self.get_big(number, byte_code=True)

0 commit comments

Comments
 (0)