diff pytouhou/formats/msg.py @ 136:d3005ebe797a

Fix MSG parsing, use the offsets instead of trying to relate them to the actual data.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Fri, 23 Sep 2011 10:05:20 -0700
parents 2cad2e84a49e
children df8b2ab54639
line wrap: on
line diff
--- a/pytouhou/formats/msg.py
+++ b/pytouhou/formats/msg.py
@@ -50,40 +50,31 @@ class MSG(object):
         msg = cls()
         msg.msgs = []
 
-        new_entry = True
-        while True:
-            offset = file.tell()
+        for offset in entry_offsets:
+            if msg.msgs and offset == entry_offsets[0]: # In EoSD, Reimu’s scripts start at 0, and Marisa’s ones at 10.
+                continue                                # If Reimu has less than 10 scripts, the remaining offsets are equal to her first.
 
-            try:
-                time, opcode, size = unpack('<HBB', file.read(4))
-            except:
-                return
-
-            if time == 0 and opcode == 0:
-                new_entry = True
+            msg.msgs.append([])
+            file.seek(offset)
 
-            if new_entry and opcode != 0:
-                new_entry = False
-                time = -1
-                if offset in entry_offsets:
-                    #print(entry_offsets.index(offset))
-                    msg.msgs.append([])
-
-            data = file.read(size)
+            while True:
+                time, opcode, size = unpack('<HBB', file.read(4))
+                if time == 0 and opcode == 0:
+                    break
+                data = file.read(size)
+                if opcode in cls._instructions:
+                    fmt = '<%s' % cls._instructions[opcode][0]
+                    if fmt.endswith('s'):
+                        fmt = fmt[:-1]
+                        fmt = '%s%ds' % (fmt, size - calcsize(fmt))
+                    args = unpack(fmt, data)
+                    if fmt.endswith('s'):
+                        args = args[:-1] + (args[-1].decode('shift_jis'),)
+                else:
+                    args = (data, )
+                    logger.warn('unknown msg opcode %d', opcode)
 
-            if opcode in cls._instructions:
-                fmt = '<%s' % cls._instructions[opcode][0]
-                if fmt.endswith('s'):
-                    fmt = fmt[:-1]
-                    fmt = '%s%ds' % (fmt, size - calcsize(fmt))
-                args = unpack(fmt, data)
-                if fmt.endswith('s'):
-                    args = args[:-1] + (args[-1].decode('shift_jis'),)
-            else:
-                args = (data, )
-                logger.warn('unknown msg opcode %d', opcode)
-
-            msg.msgs[-1].append((time, opcode, args))
+                msg.msgs[-1].append((time, opcode, args))
 
 
         return msg