Mercurial > rinkhals
comparison gamelib/savegame.py @ 499:c2a4e3a24e9a
Factor out write_savegame method. Zlib compress save games to reduce space from ~600k to ~15k.
author | Simon Cross <hodgestar@gmail.com> |
---|---|
date | Thu, 26 Nov 2009 00:06:59 +0000 |
parents | bf90a2948e34 |
children | 77a3f7f1c856 |
comparison
equal
deleted
inserted
replaced
498:62b9a4e21f1a | 499:c2a4e3a24e9a |
---|---|
2 | 2 |
3 import xmlrpclib | 3 import xmlrpclib |
4 import os | 4 import os |
5 import StringIO | 5 import StringIO |
6 import base64 | 6 import base64 |
7 import zlib | |
7 | 8 |
8 from pgu import gui | 9 from pgu import gui |
9 import pygame | 10 import pygame |
10 | 11 |
11 import config | 12 import config |
12 import version | 13 import version |
13 | 14 |
14 def open_save_game(fullpath): | 15 def read_savegame(fullpath): |
15 """Open a save game file.""" | 16 """Open a save game file.""" |
16 try: | 17 try: |
17 xml = open(fullpath, "rb").read() | 18 xml = zlib.decompress(open(fullpath, "rb").read()) |
18 params, methodname = xmlrpclib.loads(xml) | 19 params, methodname = xmlrpclib.loads(xml) |
19 if methodname != "foxassault": | 20 if methodname != "foxassault": |
20 raise SaveGameError("File does not appear to be a " | 21 raise SaveGameError("File does not appear to be a " |
21 "Fox Assault save game.") | 22 "Fox Assault save game.") |
22 save_version = params[0] | 23 save_version = params[0] |
32 | 33 |
33 except Exception, e: | 34 except Exception, e: |
34 raise SaveGameError("Failed to load game: %s" % (e,)) | 35 raise SaveGameError("Failed to load game: %s" % (e,)) |
35 | 36 |
36 return data, snapshot | 37 return data, snapshot |
38 | |
39 def write_savegame(fullpath, data, snapshot): | |
40 """Write a save game file.""" | |
41 try: | |
42 snapshot_data = encode_snapshot(snapshot) | |
43 params = (version.SAVE_GAME_VERSION, data, snapshot_data) | |
44 xml = xmlrpclib.dumps(params, "foxassault") | |
45 open(fullpath, "wb").write(zlib.compress(xml)) | |
46 except Exception, e: | |
47 raise SaveGameError("Failed to save game: %s" % (e,)) | |
37 | 48 |
38 def encode_snapshot(snapshot): | 49 def encode_snapshot(snapshot): |
39 """Encode a snapshot.""" | 50 """Encode a snapshot.""" |
40 snapshot_file = StringIO.StringIO() | 51 snapshot_file = StringIO.StringIO() |
41 pygame.image.save(snapshot, snapshot_file) | 52 pygame.image.save(snapshot, snapshot_file) |
127 self.save_games[root] = self._create_image_widget(fullpath) | 138 self.save_games[root] = self._create_image_widget(fullpath) |
128 | 139 |
129 def _create_image_widget(self, fullpath): | 140 def _create_image_widget(self, fullpath): |
130 """Create an image showing the contents of a save game file.""" | 141 """Create an image showing the contents of a save game file.""" |
131 try: | 142 try: |
132 data, screenshot = open_save_game(fullpath) | 143 data, screenshot = read_savegame(fullpath) |
133 except SaveGameError: | 144 except SaveGameError: |
134 return gui.Label("Bad Save Game") | 145 return gui.Label("Bad Save Game") |
135 | 146 |
136 if screenshot is None: | 147 if screenshot is None: |
137 return gui.Label("No screenshot") | 148 return gui.Label("No screenshot") |
174 filename = self.get_fullpath() | 185 filename = self.get_fullpath() |
175 if filename is None: | 186 if filename is None: |
176 return | 187 return |
177 | 188 |
178 data = gameboard.save_game() | 189 data = gameboard.save_game() |
179 | |
180 snapshot = gameboard.snapshot() | 190 snapshot = gameboard.snapshot() |
181 snapshot_data = encode_snapshot(snapshot) | 191 |
182 | 192 try: |
183 params = (version.SAVE_GAME_VERSION, data, snapshot_data) | 193 write_savegame(filename, data, snapshot) |
184 xml = xmlrpclib.dumps(params, "foxassault") | |
185 try: | |
186 open(filename, "wb").write(xml) | |
187 except Exception, e: | 194 except Exception, e: |
188 print "Failed to save game: %s" % (e,) | 195 print "Failed to save game: %s" % (e,) |
189 | 196 |
190 | 197 |
191 class RestoreDialog(BaseSaveRestoreDialog): | 198 class RestoreDialog(BaseSaveRestoreDialog): |
199 filename = self.get_fullpath() | 206 filename = self.get_fullpath() |
200 if filename is None: | 207 if filename is None: |
201 return | 208 return |
202 | 209 |
203 try: | 210 try: |
204 data, screenshot = open_save_game(filename) | 211 data, screenshot = read_savegame(filename) |
205 except Exception, e: | 212 except Exception, e: |
206 print "Failed to load game: %s" % (e,) | 213 print "Failed to load game: %s" % (e,) |
207 return | 214 return |
208 | 215 |
209 gameboard.restore_game(data) | 216 gameboard.restore_game(data) |