annotate pytouhou/formats/t6rp.py @ 512:b39ad30c6620

Add a pure SDL backend.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Thu, 05 Dec 2013 01:55:39 +0100
parents d8aab27a2ab2
children d1f0bb0b7a17
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
187
46793ccfedca Implement replays.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
1 # -*- encoding: utf-8 -*-
46793ccfedca Implement replays.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
2 ##
46793ccfedca Implement replays.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
3 ## Copyright (C) 2011 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
46793ccfedca Implement replays.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
4 ##
46793ccfedca Implement replays.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
5 ## This program is free software; you can redistribute it and/or modify
46793ccfedca Implement replays.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
6 ## it under the terms of the GNU General Public License as published
46793ccfedca Implement replays.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
7 ## by the Free Software Foundation; version 3 only.
46793ccfedca Implement replays.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
8 ##
46793ccfedca Implement replays.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
9 ## This program is distributed in the hope that it will be useful,
46793ccfedca Implement replays.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
10 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
46793ccfedca Implement replays.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
11 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
46793ccfedca Implement replays.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
12 ## GNU General Public License for more details.
46793ccfedca Implement replays.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
13 ##
46793ccfedca Implement replays.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
14
204
88361534c77e Add some documentation (argh, so much left to document!)
Thibaut Girka <thib@sitedethib.com>
parents: 193
diff changeset
15 """Touhou 6 Replay (T6RP) files handling.
88361534c77e Add some documentation (argh, so much left to document!)
Thibaut Girka <thib@sitedethib.com>
parents: 193
diff changeset
16
88361534c77e Add some documentation (argh, so much left to document!)
Thibaut Girka <thib@sitedethib.com>
parents: 193
diff changeset
17 This module provides classes for handling the Touhou 6 Replay file format.
88361534c77e Add some documentation (argh, so much left to document!)
Thibaut Girka <thib@sitedethib.com>
parents: 193
diff changeset
18 The T6RP file format is an encrypted format describing different aspects of
88361534c77e Add some documentation (argh, so much left to document!)
Thibaut Girka <thib@sitedethib.com>
parents: 193
diff changeset
19 a game of EoSD. Since the EoSD engine is entirely deterministic, a small
88361534c77e Add some documentation (argh, so much left to document!)
Thibaut Girka <thib@sitedethib.com>
parents: 193
diff changeset
20 replay file is sufficient to unfold a full game.
88361534c77e Add some documentation (argh, so much left to document!)
Thibaut Girka <thib@sitedethib.com>
parents: 193
diff changeset
21 """
88361534c77e Add some documentation (argh, so much left to document!)
Thibaut Girka <thib@sitedethib.com>
parents: 193
diff changeset
22
373
6deab6ad8be8 Add the ability to save a replay.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 328
diff changeset
23 from struct import unpack, pack
188
008f90ebfdc0 Fix replay handling and add support for encrypted replays
Thibaut Girka <thib@sitedethib.com>
parents: 187
diff changeset
24 from io import BytesIO
373
6deab6ad8be8 Add the ability to save a replay.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 328
diff changeset
25 from time import strftime
188
008f90ebfdc0 Fix replay handling and add support for encrypted replays
Thibaut Girka <thib@sitedethib.com>
parents: 187
diff changeset
26
305
5492472963b0 Minor cleanup
Thibaut Girka <thib@sitedethib.com>
parents: 204
diff changeset
27 from pytouhou.utils.helpers import read_string, get_logger
385
d8aab27a2ab2 Add missing imports, and remove side-effects in asserts.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 377
diff changeset
28 from pytouhou.formats import ChecksumError
187
46793ccfedca Implement replays.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
29
46793ccfedca Implement replays.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
30 logger = get_logger(__name__)
46793ccfedca Implement replays.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
31
46793ccfedca Implement replays.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
32
46793ccfedca Implement replays.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
33 class Level(object):
46793ccfedca Implement replays.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
34 def __init__(self):
188
008f90ebfdc0 Fix replay handling and add support for encrypted replays
Thibaut Girka <thib@sitedethib.com>
parents: 187
diff changeset
35 self.score = 0
008f90ebfdc0 Fix replay handling and add support for encrypted replays
Thibaut Girka <thib@sitedethib.com>
parents: 187
diff changeset
36 self.random_seed = 0
328
56523a16db1d Fix some replay synchronization issues and update the TODO.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 305
diff changeset
37 self.point_items = 0
188
008f90ebfdc0 Fix replay handling and add support for encrypted replays
Thibaut Girka <thib@sitedethib.com>
parents: 187
diff changeset
38
008f90ebfdc0 Fix replay handling and add support for encrypted replays
Thibaut Girka <thib@sitedethib.com>
parents: 187
diff changeset
39 self.power = 0
008f90ebfdc0 Fix replay handling and add support for encrypted replays
Thibaut Girka <thib@sitedethib.com>
parents: 187
diff changeset
40 self.lives = 2
008f90ebfdc0 Fix replay handling and add support for encrypted replays
Thibaut Girka <thib@sitedethib.com>
parents: 187
diff changeset
41 self.bombs = 3
008f90ebfdc0 Fix replay handling and add support for encrypted replays
Thibaut Girka <thib@sitedethib.com>
parents: 187
diff changeset
42 self.difficulty = 16
328
56523a16db1d Fix some replay synchronization issues and update the TODO.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 305
diff changeset
43 self.unknown = 0
187
46793ccfedca Implement replays.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
44 self.keys = []
46793ccfedca Implement replays.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
45
46793ccfedca Implement replays.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
46
374
6a63fd3deb76 Use an iterator to get the keystates from a replay.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 373
diff changeset
47 def iter_keystates(self):
6a63fd3deb76 Use an iterator to get the keystates from a replay.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 373
diff changeset
48 counter = 0
6a63fd3deb76 Use an iterator to get the keystates from a replay.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 373
diff changeset
49 previous = 0
6a63fd3deb76 Use an iterator to get the keystates from a replay.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 373
diff changeset
50 for frame, keystate, unknown in self.keys:
6a63fd3deb76 Use an iterator to get the keystates from a replay.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 373
diff changeset
51 while frame >= counter:
6a63fd3deb76 Use an iterator to get the keystates from a replay.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 373
diff changeset
52 yield previous
6a63fd3deb76 Use an iterator to get the keystates from a replay.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 373
diff changeset
53 counter += 1
6a63fd3deb76 Use an iterator to get the keystates from a replay.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 373
diff changeset
54 previous = keystate
6a63fd3deb76 Use an iterator to get the keystates from a replay.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 373
diff changeset
55
6a63fd3deb76 Use an iterator to get the keystates from a replay.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 373
diff changeset
56
6a63fd3deb76 Use an iterator to get the keystates from a replay.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 373
diff changeset
57
187
46793ccfedca Implement replays.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
58 class T6RP(object):
46793ccfedca Implement replays.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
59 def __init__(self):
188
008f90ebfdc0 Fix replay handling and add support for encrypted replays
Thibaut Girka <thib@sitedethib.com>
parents: 187
diff changeset
60 self.version = 0x102
008f90ebfdc0 Fix replay handling and add support for encrypted replays
Thibaut Girka <thib@sitedethib.com>
parents: 187
diff changeset
61 self.character = 0
008f90ebfdc0 Fix replay handling and add support for encrypted replays
Thibaut Girka <thib@sitedethib.com>
parents: 187
diff changeset
62 self.rank = 0
373
6deab6ad8be8 Add the ability to save a replay.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 328
diff changeset
63 self.unknown1 = 0
6deab6ad8be8 Add the ability to save a replay.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 328
diff changeset
64 self.unknown2 = 0
188
008f90ebfdc0 Fix replay handling and add support for encrypted replays
Thibaut Girka <thib@sitedethib.com>
parents: 187
diff changeset
65 self.key = 0
373
6deab6ad8be8 Add the ability to save a replay.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 328
diff changeset
66 self.unknown3 = 0
6deab6ad8be8 Add the ability to save a replay.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 328
diff changeset
67 self.date = strftime('%d/%m/%y')
6deab6ad8be8 Add the ability to save a replay.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 328
diff changeset
68 self.name = 'PyTouhou'
6deab6ad8be8 Add the ability to save a replay.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 328
diff changeset
69 self.unknown4 = 0
6deab6ad8be8 Add the ability to save a replay.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 328
diff changeset
70 self.score = 0
6deab6ad8be8 Add the ability to save a replay.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 328
diff changeset
71 self.unknown5 = 0
6deab6ad8be8 Add the ability to save a replay.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 328
diff changeset
72 self.slowdown = 0.
6deab6ad8be8 Add the ability to save a replay.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 328
diff changeset
73 self.unknown6 = 0
189
ba3297ab3bde Fix handling for replay files not beginning with stage 1
Thibaut Girka <thib@sitedethib.com>
parents: 188
diff changeset
74 self.levels = [None] * 7
187
46793ccfedca Implement replays.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
75
46793ccfedca Implement replays.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
76
46793ccfedca Implement replays.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
77 @classmethod
188
008f90ebfdc0 Fix replay handling and add support for encrypted replays
Thibaut Girka <thib@sitedethib.com>
parents: 187
diff changeset
78 def read(cls, file, decrypt=True, verify=True):
204
88361534c77e Add some documentation (argh, so much left to document!)
Thibaut Girka <thib@sitedethib.com>
parents: 193
diff changeset
79 """Read a T6RP file.
88361534c77e Add some documentation (argh, so much left to document!)
Thibaut Girka <thib@sitedethib.com>
parents: 193
diff changeset
80
88361534c77e Add some documentation (argh, so much left to document!)
Thibaut Girka <thib@sitedethib.com>
parents: 193
diff changeset
81 Raise an exception if the file is invalid.
88361534c77e Add some documentation (argh, so much left to document!)
Thibaut Girka <thib@sitedethib.com>
parents: 193
diff changeset
82 Return a T6RP instance otherwise.
88361534c77e Add some documentation (argh, so much left to document!)
Thibaut Girka <thib@sitedethib.com>
parents: 193
diff changeset
83
88361534c77e Add some documentation (argh, so much left to document!)
Thibaut Girka <thib@sitedethib.com>
parents: 193
diff changeset
84 Keyword arguments:
88361534c77e Add some documentation (argh, so much left to document!)
Thibaut Girka <thib@sitedethib.com>
parents: 193
diff changeset
85 decrypt -- whether or not to decrypt the file (default True)
88361534c77e Add some documentation (argh, so much left to document!)
Thibaut Girka <thib@sitedethib.com>
parents: 193
diff changeset
86 verify -- whether or not to verify the file's checksum (default True)
88361534c77e Add some documentation (argh, so much left to document!)
Thibaut Girka <thib@sitedethib.com>
parents: 193
diff changeset
87 """
88361534c77e Add some documentation (argh, so much left to document!)
Thibaut Girka <thib@sitedethib.com>
parents: 193
diff changeset
88
377
70e2ed71b09c Add meaningful exceptions in format parsing.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 374
diff changeset
89 magic = file.read(4)
70e2ed71b09c Add meaningful exceptions in format parsing.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 374
diff changeset
90 assert magic == b'T6RP'
187
46793ccfedca Implement replays.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
91
46793ccfedca Implement replays.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
92 replay = cls()
46793ccfedca Implement replays.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
93
188
008f90ebfdc0 Fix replay handling and add support for encrypted replays
Thibaut Girka <thib@sitedethib.com>
parents: 187
diff changeset
94 replay.version, replay.character, replay.rank = unpack('<HBB', file.read(4))
008f90ebfdc0 Fix replay handling and add support for encrypted replays
Thibaut Girka <thib@sitedethib.com>
parents: 187
diff changeset
95 checksum, replay.unknown1, replay.unknown2, replay.key = unpack('<IBBB', file.read(7))
008f90ebfdc0 Fix replay handling and add support for encrypted replays
Thibaut Girka <thib@sitedethib.com>
parents: 187
diff changeset
96
008f90ebfdc0 Fix replay handling and add support for encrypted replays
Thibaut Girka <thib@sitedethib.com>
parents: 187
diff changeset
97 # Decrypt data
008f90ebfdc0 Fix replay handling and add support for encrypted replays
Thibaut Girka <thib@sitedethib.com>
parents: 187
diff changeset
98 if decrypt:
008f90ebfdc0 Fix replay handling and add support for encrypted replays
Thibaut Girka <thib@sitedethib.com>
parents: 187
diff changeset
99 decrypted_file = BytesIO()
008f90ebfdc0 Fix replay handling and add support for encrypted replays
Thibaut Girka <thib@sitedethib.com>
parents: 187
diff changeset
100 file.seek(0)
008f90ebfdc0 Fix replay handling and add support for encrypted replays
Thibaut Girka <thib@sitedethib.com>
parents: 187
diff changeset
101 decrypted_file.write(file.read(15))
008f90ebfdc0 Fix replay handling and add support for encrypted replays
Thibaut Girka <thib@sitedethib.com>
parents: 187
diff changeset
102 decrypted_file.write(b''.join(chr((ord(c) - replay.key - 7*i) & 0xff) for i, c in enumerate(file.read())))
008f90ebfdc0 Fix replay handling and add support for encrypted replays
Thibaut Girka <thib@sitedethib.com>
parents: 187
diff changeset
103 file = decrypted_file
008f90ebfdc0 Fix replay handling and add support for encrypted replays
Thibaut Girka <thib@sitedethib.com>
parents: 187
diff changeset
104 file.seek(15)
008f90ebfdc0 Fix replay handling and add support for encrypted replays
Thibaut Girka <thib@sitedethib.com>
parents: 187
diff changeset
105
008f90ebfdc0 Fix replay handling and add support for encrypted replays
Thibaut Girka <thib@sitedethib.com>
parents: 187
diff changeset
106 # Verify checksum
008f90ebfdc0 Fix replay handling and add support for encrypted replays
Thibaut Girka <thib@sitedethib.com>
parents: 187
diff changeset
107 if verify:
008f90ebfdc0 Fix replay handling and add support for encrypted replays
Thibaut Girka <thib@sitedethib.com>
parents: 187
diff changeset
108 data = file.read()
008f90ebfdc0 Fix replay handling and add support for encrypted replays
Thibaut Girka <thib@sitedethib.com>
parents: 187
diff changeset
109 file.seek(15)
373
6deab6ad8be8 Add the ability to save a replay.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 328
diff changeset
110 real_sum = (sum(ord(c) for c in data) + 0x3f000318 + replay.key) & 0xffffffff
6deab6ad8be8 Add the ability to save a replay.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 328
diff changeset
111 if checksum != real_sum:
377
70e2ed71b09c Add meaningful exceptions in format parsing.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 374
diff changeset
112 raise ChecksumError(checksum, real_sum)
188
008f90ebfdc0 Fix replay handling and add support for encrypted replays
Thibaut Girka <thib@sitedethib.com>
parents: 187
diff changeset
113
328
56523a16db1d Fix some replay synchronization issues and update the TODO.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 305
diff changeset
114 replay.unknown3, = unpack('<B', file.read(1))
193
9f58e2a6e950 Fix particles, fix "random" item popping, change update order to match the original game's more closely.
Thibaut Girka <thib@sitedethib.com>
parents: 189
diff changeset
115 replay.date = file.read(9) #read_string(file, 9, 'ascii')
9f58e2a6e950 Fix particles, fix "random" item popping, change update order to match the original game's more closely.
Thibaut Girka <thib@sitedethib.com>
parents: 189
diff changeset
116 replay.name = file.read(9) #read_string(file, 9, 'ascii').rstrip()
188
008f90ebfdc0 Fix replay handling and add support for encrypted replays
Thibaut Girka <thib@sitedethib.com>
parents: 187
diff changeset
117 replay.unknown4, replay.score, replay.unknown5, replay.slowdown, replay.unknown6 = unpack('<HIIfI', file.read(18))
187
46793ccfedca Implement replays.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
118
46793ccfedca Implement replays.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
119 stages_offsets = unpack('<7I', file.read(28))
46793ccfedca Implement replays.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
120
189
ba3297ab3bde Fix handling for replay files not beginning with stage 1
Thibaut Girka <thib@sitedethib.com>
parents: 188
diff changeset
121 for i, offset in enumerate(stages_offsets):
187
46793ccfedca Implement replays.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
122 if offset == 0:
46793ccfedca Implement replays.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
123 continue
46793ccfedca Implement replays.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
124
46793ccfedca Implement replays.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
125 level = Level()
189
ba3297ab3bde Fix handling for replay files not beginning with stage 1
Thibaut Girka <thib@sitedethib.com>
parents: 188
diff changeset
126 replay.levels[i] = level
187
46793ccfedca Implement replays.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
127
46793ccfedca Implement replays.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
128 file.seek(offset)
328
56523a16db1d Fix some replay synchronization issues and update the TODO.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 305
diff changeset
129 (level.score, level.random_seed, level.point_items, level.power,
56523a16db1d Fix some replay synchronization issues and update the TODO.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 305
diff changeset
130 level.lives, level.bombs, level.difficulty, level.unknown) = unpack('<IHHBbbBI', file.read(16))
187
46793ccfedca Implement replays.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
131
46793ccfedca Implement replays.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
132 while True:
46793ccfedca Implement replays.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
133 time, keys, unknown = unpack('<IHH', file.read(8))
46793ccfedca Implement replays.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
134
46793ccfedca Implement replays.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
135 if time == 9999999:
46793ccfedca Implement replays.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
136 break
46793ccfedca Implement replays.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
137
188
008f90ebfdc0 Fix replay handling and add support for encrypted replays
Thibaut Girka <thib@sitedethib.com>
parents: 187
diff changeset
138 level.keys.append((time, keys, unknown))
187
46793ccfedca Implement replays.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
139
188
008f90ebfdc0 Fix replay handling and add support for encrypted replays
Thibaut Girka <thib@sitedethib.com>
parents: 187
diff changeset
140 return replay
373
6deab6ad8be8 Add the ability to save a replay.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 328
diff changeset
141
6deab6ad8be8 Add the ability to save a replay.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 328
diff changeset
142
6deab6ad8be8 Add the ability to save a replay.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 328
diff changeset
143 def write(self, file, encrypt=True):
6deab6ad8be8 Add the ability to save a replay.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 328
diff changeset
144 if encrypt:
6deab6ad8be8 Add the ability to save a replay.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 328
diff changeset
145 encrypted_file = file
6deab6ad8be8 Add the ability to save a replay.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 328
diff changeset
146 file = BytesIO()
6deab6ad8be8 Add the ability to save a replay.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 328
diff changeset
147
6deab6ad8be8 Add the ability to save a replay.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 328
diff changeset
148 file.write(b'T6RP')
6deab6ad8be8 Add the ability to save a replay.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 328
diff changeset
149 file.write(pack('<HBB', self.version, self.character, self.rank))
6deab6ad8be8 Add the ability to save a replay.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 328
diff changeset
150
6deab6ad8be8 Add the ability to save a replay.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 328
diff changeset
151 checksum_offset = file.tell()
6deab6ad8be8 Add the ability to save a replay.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 328
diff changeset
152 file.seek(4, 1) # For checksum
6deab6ad8be8 Add the ability to save a replay.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 328
diff changeset
153 file.write(pack('<BBB', self.unknown1, self.unknown2, self.key))
6deab6ad8be8 Add the ability to save a replay.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 328
diff changeset
154
6deab6ad8be8 Add the ability to save a replay.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 328
diff changeset
155 file.write(pack('<B', self.unknown3))
6deab6ad8be8 Add the ability to save a replay.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 328
diff changeset
156
6deab6ad8be8 Add the ability to save a replay.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 328
diff changeset
157 #TODO: find a more elegant method.
6deab6ad8be8 Add the ability to save a replay.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 328
diff changeset
158 n = 9 - len(self.date)
6deab6ad8be8 Add the ability to save a replay.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 328
diff changeset
159 file.write(self.date)
6deab6ad8be8 Add the ability to save a replay.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 328
diff changeset
160 file.write('\0' * n)
6deab6ad8be8 Add the ability to save a replay.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 328
diff changeset
161 n = 9 - len(self.name)
6deab6ad8be8 Add the ability to save a replay.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 328
diff changeset
162 file.write(self.name)
6deab6ad8be8 Add the ability to save a replay.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 328
diff changeset
163 file.write('\0' * n)
6deab6ad8be8 Add the ability to save a replay.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 328
diff changeset
164
6deab6ad8be8 Add the ability to save a replay.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 328
diff changeset
165 file.write(pack('<HIIfI', self.unknown4, self.score, self.unknown5, self.slowdown, self.unknown6))
6deab6ad8be8 Add the ability to save a replay.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 328
diff changeset
166
6deab6ad8be8 Add the ability to save a replay.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 328
diff changeset
167 stages_offsets_offset = file.tell()
6deab6ad8be8 Add the ability to save a replay.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 328
diff changeset
168 file.seek(7*4, 1) # Skip the stages offsets.
6deab6ad8be8 Add the ability to save a replay.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 328
diff changeset
169
6deab6ad8be8 Add the ability to save a replay.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 328
diff changeset
170 stages_offsets = []
6deab6ad8be8 Add the ability to save a replay.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 328
diff changeset
171 for level in self.levels:
6deab6ad8be8 Add the ability to save a replay.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 328
diff changeset
172 if not level:
6deab6ad8be8 Add the ability to save a replay.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 328
diff changeset
173 stages_offsets.append(0)
6deab6ad8be8 Add the ability to save a replay.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 328
diff changeset
174 continue
6deab6ad8be8 Add the ability to save a replay.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 328
diff changeset
175
6deab6ad8be8 Add the ability to save a replay.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 328
diff changeset
176 stages_offsets.append(file.tell())
6deab6ad8be8 Add the ability to save a replay.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 328
diff changeset
177 file.write(pack('<IHHBbbBI', level.score, level.random_seed,
6deab6ad8be8 Add the ability to save a replay.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 328
diff changeset
178 level.point_items, level.power, level.lives,
6deab6ad8be8 Add the ability to save a replay.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 328
diff changeset
179 level.bombs, level.difficulty, level.unknown))
6deab6ad8be8 Add the ability to save a replay.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 328
diff changeset
180
6deab6ad8be8 Add the ability to save a replay.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 328
diff changeset
181 for time, keys, unknown in level.keys:
6deab6ad8be8 Add the ability to save a replay.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 328
diff changeset
182 file.write(pack('<IHH', time, keys, unknown))
6deab6ad8be8 Add the ability to save a replay.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 328
diff changeset
183
6deab6ad8be8 Add the ability to save a replay.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 328
diff changeset
184 file.write(pack('<IHH', 9999999, 0, 0))
6deab6ad8be8 Add the ability to save a replay.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 328
diff changeset
185
6deab6ad8be8 Add the ability to save a replay.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 328
diff changeset
186 file.seek(stages_offsets_offset)
6deab6ad8be8 Add the ability to save a replay.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 328
diff changeset
187 file.write(pack('<7I', *stages_offsets))
6deab6ad8be8 Add the ability to save a replay.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 328
diff changeset
188
6deab6ad8be8 Add the ability to save a replay.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 328
diff changeset
189 # Write checksum
6deab6ad8be8 Add the ability to save a replay.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 328
diff changeset
190 file.seek(15)
6deab6ad8be8 Add the ability to save a replay.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 328
diff changeset
191 data = file.read()
6deab6ad8be8 Add the ability to save a replay.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 328
diff changeset
192 checksum = (sum(ord(c) for c in data) + 0x3f000318 + self.key) & 0xffffffff
6deab6ad8be8 Add the ability to save a replay.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 328
diff changeset
193 file.seek(checksum_offset)
6deab6ad8be8 Add the ability to save a replay.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 328
diff changeset
194 file.write(pack('<I', checksum))
6deab6ad8be8 Add the ability to save a replay.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 328
diff changeset
195
6deab6ad8be8 Add the ability to save a replay.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 328
diff changeset
196 # Encrypt
6deab6ad8be8 Add the ability to save a replay.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 328
diff changeset
197 if encrypt:
6deab6ad8be8 Add the ability to save a replay.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 328
diff changeset
198 file.seek(0)
6deab6ad8be8 Add the ability to save a replay.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 328
diff changeset
199 encrypted_file.write(file.read(15))
6deab6ad8be8 Add the ability to save a replay.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 328
diff changeset
200 encrypted_file.write(b''.join(chr((ord(c) + self.key + 7*i) & 0xff) for i, c in enumerate(file.read())))
6deab6ad8be8 Add the ability to save a replay.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 328
diff changeset
201