Mercurial > touhou
comparison pytouhou/formats/t6rp.py @ 187:46793ccfedca
Implement replays.
author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
---|---|
date | Wed, 26 Oct 2011 17:54:03 -0700 |
parents | |
children | 008f90ebfdc0 |
comparison
equal
deleted
inserted
replaced
186:84da28ae7ee4 | 187:46793ccfedca |
---|---|
1 # -*- encoding: utf-8 -*- | |
2 ## | |
3 ## Copyright (C) 2011 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> | |
4 ## | |
5 ## This program is free software; you can redistribute it and/or modify | |
6 ## it under the terms of the GNU General Public License as published | |
7 ## by the Free Software Foundation; version 3 only. | |
8 ## | |
9 ## This program is distributed in the hope that it will be useful, | |
10 ## but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 ## GNU General Public License for more details. | |
13 ## | |
14 | |
15 from struct import unpack | |
16 from pytouhou.utils.helpers import read_string | |
17 | |
18 from pytouhou.utils.helpers import get_logger | |
19 | |
20 logger = get_logger(__name__) | |
21 | |
22 | |
23 class Level(object): | |
24 def __init__(self): | |
25 self.keys = [] | |
26 | |
27 | |
28 class T6RP(object): | |
29 def __init__(self): | |
30 self.levels = [] | |
31 | |
32 | |
33 @classmethod | |
34 def read(cls, file): | |
35 if file.read(4) != b'T6RP': | |
36 raise Exception | |
37 if file.read(2) != b'\x02\x01': | |
38 raise Exception | |
39 | |
40 replay = cls() | |
41 | |
42 replay.character, replay.rank, checksum, unknown, key, unknown = unpack('<BBHIBB', file.read(10)) | |
43 replay.date = read_string(file, 9, 'ascii') | |
44 replay.name = read_string(file, 9, 'ascii').rstrip() | |
45 unknown, replay.score, unknown, replay.slowdown, unknown = unpack('<HIIfI', file.read(18)) | |
46 | |
47 stages_offsets = unpack('<7I', file.read(28)) | |
48 | |
49 replay.levels = [] | |
50 | |
51 for offset in stages_offsets: | |
52 replay.levels.append(None) | |
53 | |
54 if offset == 0: | |
55 continue | |
56 | |
57 level = Level() | |
58 replay.levels[-1] = level | |
59 | |
60 file.seek(offset) | |
61 level.score, level.random_seed, unknown, level.power, level.lives, level.bombs, level.difficulty, unknown = unpack('<IHHBbbBI', file.read(16)) | |
62 | |
63 while True: | |
64 time, keys, unknown = unpack('<IHH', file.read(8)) | |
65 | |
66 if time == 9999999: | |
67 break | |
68 | |
69 level.keys.append((time, keys)) | |
70 |