Skip to content

Commit 6b540c2

Browse files
committed
Improvements to the serialized structure parsing.
1 parent d2af620 commit 6b540c2

File tree

1 file changed

+24
-11
lines changed

1 file changed

+24
-11
lines changed

sc2reader/utils.py

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -268,18 +268,32 @@ def read_data_struct(self):
268268
"""
269269
#The first byte serves as a flag for the type of data to follow
270270
datatype = self.read_byte()
271+
272+
if datatype == 0x00:
273+
#0x00 is an array where the first X bytes mark the number of entries in
274+
#the array. See variable int documentation for details.
275+
entries = self.read_variable_int()
276+
return [self.read_data_struct() for i in range(entries)]
277+
271278
if datatype == 0x02:
272279
#0x02 is a byte string with the first byte indicating
273280
#the length of the byte string to follow
274-
count = self.read_count()
275-
return self.read_string(count)
281+
return self.read_string(self.read_count())
282+
283+
elif datatype == 0x03:
284+
#0x03 is an unknown data type where the first byte appears
285+
#to have no effect and kicks back the next instruction
286+
flag = self.read_byte()
287+
return self.read_data_struct()
276288

277289
elif datatype == 0x04:
278-
#0x04 is an serialized data list with first two bytes always 01 00
279-
#and the next byte indicating the number of elements in the list
280-
#each element is a serialized data structure
281-
self.skip(2) #01 00
282-
return [self.read_data_struct() for i in range(self.read_count())]
290+
#0x04 is an unknown data type where the first byte of information
291+
#is a switch (1 or 0) that can trigger another structure to be
292+
#read.
293+
if self.read_byte():
294+
return self.read_data_struct()
295+
else:
296+
return 0
283297

284298
elif datatype == 0x05:
285299
#0x05 is a serialized key,value structure with the first byte
@@ -288,15 +302,14 @@ def read_data_struct(self):
288302
#followed by the serialized data object value
289303
data = dict()
290304
for i in range(self.read_count()):
291-
count = self.read_count()
292-
key,value = count, self.read_data_struct()
293-
data[key] = value #Done like this to keep correct parse order
305+
key = self.read_count()
306+
data[key] = self.read_data_struct() #Done like this to keep correct parse order
294307
return data
295308

296309
elif datatype == 0x06:
297310
return self.read_byte()
298311
elif datatype == 0x07:
299-
return self.read_int()
312+
return self.read_chars(4)
300313
elif datatype == 0x09:
301314
return self.read_variable_int()
302315

0 commit comments

Comments
 (0)