comparison pytouhou/formats/score.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 5492472963b0
children d1f0bb0b7a17
comparison
equal deleted inserted replaced
376:69ec72b990a4 377:70e2ed71b09c
14 14
15 15
16 from struct import pack, unpack, Struct 16 from struct import pack, unpack, Struct
17 from collections import namedtuple 17 from collections import namedtuple
18 from io import BytesIO 18 from io import BytesIO
19
20 from pytouhou.formats import ChecksumError
19 21
20 22
21 class TH6Score(object): 23 class TH6Score(object):
22 entry_types = { 24 entry_types = {
23 b'TH6K': (Struct('<I'), 25 b'TH6K': (Struct('<I'),
76 self.unknown1, self.key1, checksum = unpack('<BBH', file.read(4)) 78 self.unknown1, self.key1, checksum = unpack('<BBH', file.read(4))
77 79
78 # Verify checksum 80 # Verify checksum
79 if verify: 81 if verify:
80 #TODO: is there more to it? 82 #TODO: is there more to it?
81 if checksum != sum(ord(c) for c in file.read()) & 0xFFFF: 83 real_sum = sum(ord(c) for c in file.read()) & 0xFFFF
82 raise Exception 84 if checksum != real_sum:
85 raise ChecksumError(checksum, real_sum)
83 file.seek(4) 86 file.seek(4)
84 87
85 # Read second-part header 88 # Read second-part header
86 data = unpack('<HBBIII', file.read(16)) 89 data = unpack('<HBBIII', file.read(16))
87 self.unknown2, self.key2, self.unknown3, offset, self.unknown4, size = data 90 self.unknown2, self.key2, self.unknown3, offset, self.unknown4, size = data