comparison 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
comparison
equal deleted inserted replaced
135:c53d91300c1c 136:d3005ebe797a
48 entry_offsets = unpack('<%dI' % entry_count, file.read(4 * entry_count)) 48 entry_offsets = unpack('<%dI' % entry_count, file.read(4 * entry_count))
49 49
50 msg = cls() 50 msg = cls()
51 msg.msgs = [] 51 msg.msgs = []
52 52
53 new_entry = True 53 for offset in entry_offsets:
54 while True: 54 if msg.msgs and offset == entry_offsets[0]: # In EoSD, Reimu’s scripts start at 0, and Marisa’s ones at 10.
55 offset = file.tell() 55 continue # If Reimu has less than 10 scripts, the remaining offsets are equal to her first.
56 56
57 try: 57 msg.msgs.append([])
58 file.seek(offset)
59
60 while True:
58 time, opcode, size = unpack('<HBB', file.read(4)) 61 time, opcode, size = unpack('<HBB', file.read(4))
59 except: 62 if time == 0 and opcode == 0:
60 return 63 break
64 data = file.read(size)
65 if opcode in cls._instructions:
66 fmt = '<%s' % cls._instructions[opcode][0]
67 if fmt.endswith('s'):
68 fmt = fmt[:-1]
69 fmt = '%s%ds' % (fmt, size - calcsize(fmt))
70 args = unpack(fmt, data)
71 if fmt.endswith('s'):
72 args = args[:-1] + (args[-1].decode('shift_jis'),)
73 else:
74 args = (data, )
75 logger.warn('unknown msg opcode %d', opcode)
61 76
62 if time == 0 and opcode == 0: 77 msg.msgs[-1].append((time, opcode, args))
63 new_entry = True
64
65 if new_entry and opcode != 0:
66 new_entry = False
67 time = -1
68 if offset in entry_offsets:
69 #print(entry_offsets.index(offset))
70 msg.msgs.append([])
71
72 data = file.read(size)
73
74 if opcode in cls._instructions:
75 fmt = '<%s' % cls._instructions[opcode][0]
76 if fmt.endswith('s'):
77 fmt = fmt[:-1]
78 fmt = '%s%ds' % (fmt, size - calcsize(fmt))
79 args = unpack(fmt, data)
80 if fmt.endswith('s'):
81 args = args[:-1] + (args[-1].decode('shift_jis'),)
82 else:
83 args = (data, )
84 logger.warn('unknown msg opcode %d', opcode)
85
86 msg.msgs[-1].append((time, opcode, args))
87 78
88 79
89 return msg 80 return msg
90 81