comparison gamelib/savegame.py @ 491:75ef6ea3b7a6

Start of support for save game snapshots.
author Simon Cross <hodgestar@gmail.com>
date Wed, 25 Nov 2009 22:12:08 +0000
parents 8897a436a8cb
children bf90a2948e34
comparison
equal deleted inserted replaced
490:8308bef91864 491:75ef6ea3b7a6
5 5
6 from pgu import gui 6 from pgu import gui
7 7
8 import config 8 import config
9 import version 9 import version
10
11 def open_save_game(fullpath):
12 """Open a save game file."""
13 try:
14 xml = open(fullpath, "rb").read()
15 params, methodname = xmlrpclib.loads(xml)
16 if methodname != "foxassault":
17 raise SaveGameError("File does not appear to be a "
18 "Fox Assault save game.")
19 save_version = params[0]
20 if save_version != version.SAVE_GAME_VERSION:
21 raise SaveGameError("Incompatible save game version.")
22 data = params[1]
23 except Exception, e:
24 raise SaveGameError("Failed to load game: %s" % (e,))
25
26 return data, None
27
28
29 class SaveGameError(Exception):
30 pass
10 31
11 32
12 class BaseSaveRestoreDialog(gui.Dialog): 33 class BaseSaveRestoreDialog(gui.Dialog):
13 """Save game dialog.""" 34 """Save game dialog."""
14 35
37 for name in games: 58 for name in games:
38 self.save_list.add(name, value=name) 59 self.save_list.add(name, value=name)
39 self.save_list.set_vertical_scroll(0) 60 self.save_list.set_vertical_scroll(0)
40 self.save_list.connect(gui.CHANGE, self._save_list_change) 61 self.save_list.connect(gui.CHANGE, self._save_list_change)
41 62
63 self.image_container = gui.Container()
64
42 button_ok = gui.Button(button_txt) 65 button_ok = gui.Button(button_txt)
43 button_ok.connect(gui.CLICK, self._click_ok) 66 button_ok.connect(gui.CLICK, self._click_ok)
44 67
45 button_cancel = gui.Button("Cancel") 68 button_cancel = gui.Button("Cancel")
46 button_cancel.connect(gui.CLICK, self._click_cancel) 69 button_cancel.connect(gui.CLICK, self._click_cancel)
47 70
48 body = gui.Table() 71 body = gui.Table()
49 body.tr() 72 body.tr()
50 body.td(self.save_list, style=td_style, colspan=2) 73 body.td(self.save_list, style=td_style, colspan=2)
51 body.td(gui.Label("Image"), style=td_style, colspan=2) 74 body.td(self.image_container, style=td_style, colspan=2)
52 body.tr() 75 body.tr()
53 if self.name_input: 76 if self.name_input:
54 body.td(gui.Label("Save as:"), style=td_style, align=1) 77 body.td(gui.Label("Save as:"), style=td_style, align=1)
55 body.td(self.name_input, style=td_style) 78 body.td(self.name_input, style=td_style)
56 else: 79 else:
74 root, ext = os.path.splitext(filename) 97 root, ext = os.path.splitext(filename)
75 if not os.path.isfile(fullpath): 98 if not os.path.isfile(fullpath):
76 continue 99 continue
77 if ext != ".xml": 100 if ext != ".xml":
78 continue 101 continue
79 self.save_games[root] = None 102 self.save_games[root] = self._create_image_widget(fullpath)
103
104 def _create_image_widget(self, fullpath):
105 """Create an image showing the contents of a save game file."""
106 try:
107 data, screenshot = open_save_game(fullpath)
108 except SaveGameError:
109 return gui.Label("Bad Save Game")
110
111 if screenshot is None:
112 return gui.Label("No screenshot")
113
114 return gui.Image(screenshot)
80 115
81 def _save_list_change(self): 116 def _save_list_change(self):
82 if self.name_input: 117 if self.name_input:
83 self.name_input.value = self.save_list.value 118 self.name_input.value = self.save_list.value
119
120 for w in self.image_container.widgets:
121 self.image_container.remove(w)
122
123 image_widget = self.save_games[self.save_list.value]
124 self.image_container.add(image_widget, 0, 0)
84 125
85 def _click_ok(self): 126 def _click_ok(self):
86 if self.name_input: 127 if self.name_input:
87 self.value = self.name_input.value 128 self.value = self.name_input.value
88 else: 129 else:
126 167
127 def _restore(self, gameboard): 168 def _restore(self, gameboard):
128 filename = self.get_fullpath() 169 filename = self.get_fullpath()
129 if filename is None: 170 if filename is None:
130 return 171 return
172
131 try: 173 try:
132 xml = open(filename, "rb").read() 174 data, screenshot = open_save_game(filename)
133 params, methodname = xmlrpclib.loads(xml)
134 if methodname != "foxassault":
135 raise ValueError("File does not appear to be a "
136 "Fox Assault save game.")
137 save_version = params[0]
138 if save_version != version.SAVE_GAME_VERSION:
139 raise ValueError("Incompatible save game version.")
140 data = params[1]
141 except Exception, e: 175 except Exception, e:
142 "Failed to load game: %s" % (e,) 176 print "Failed to load game: %s" % (e,)
143 return 177 return
144 178
145 gameboard.restore_game(data) 179 gameboard.restore_game(data)