comparison pytouhou/formats/t6rp.py @ 377:70e2ed71b09c

Add meaningful exceptions in format parsing.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Wed, 29 Aug 2012 18:34:28 +0200
parents 6a63fd3deb76
children d8aab27a2ab2
comparison
equal deleted inserted replaced
376:69ec72b990a4 377:70e2ed71b09c
83 Keyword arguments: 83 Keyword arguments:
84 decrypt -- whether or not to decrypt the file (default True) 84 decrypt -- whether or not to decrypt the file (default True)
85 verify -- whether or not to verify the file's checksum (default True) 85 verify -- whether or not to verify the file's checksum (default True)
86 """ 86 """
87 87
88 if file.read(4) != b'T6RP': 88 magic = file.read(4)
89 raise Exception 89 assert magic == b'T6RP'
90 90
91 replay = cls() 91 replay = cls()
92 92
93 replay.version, replay.character, replay.rank = unpack('<HBB', file.read(4)) 93 replay.version, replay.character, replay.rank = unpack('<HBB', file.read(4))
94 checksum, replay.unknown1, replay.unknown2, replay.key = unpack('<IBBB', file.read(7)) 94 checksum, replay.unknown1, replay.unknown2, replay.key = unpack('<IBBB', file.read(7))
106 if verify: 106 if verify:
107 data = file.read() 107 data = file.read()
108 file.seek(15) 108 file.seek(15)
109 real_sum = (sum(ord(c) for c in data) + 0x3f000318 + replay.key) & 0xffffffff 109 real_sum = (sum(ord(c) for c in data) + 0x3f000318 + replay.key) & 0xffffffff
110 if checksum != real_sum: 110 if checksum != real_sum:
111 raise Exception('Checksum mismatch: %d ≠ %d.' % (checksum, real_sum)) 111 raise ChecksumError(checksum, real_sum)
112 112
113 replay.unknown3, = unpack('<B', file.read(1)) 113 replay.unknown3, = unpack('<B', file.read(1))
114 replay.date = file.read(9) #read_string(file, 9, 'ascii') 114 replay.date = file.read(9) #read_string(file, 9, 'ascii')
115 replay.name = file.read(9) #read_string(file, 9, 'ascii').rstrip() 115 replay.name = file.read(9) #read_string(file, 9, 'ascii').rstrip()
116 replay.unknown4, replay.score, replay.unknown5, replay.slowdown, replay.unknown6 = unpack('<HIIfI', file.read(18)) 116 replay.unknown4, replay.score, replay.unknown5, replay.slowdown, replay.unknown6 = unpack('<HIIfI', file.read(18))