Mercurial > rinkhals
comparison gamelib/toolbar.py @ 450:5d74d0e4a4cc
Switch to XML (RPC) for save game encoding.
author | Simon Cross <hodgestar@gmail.com> |
---|---|
date | Sat, 21 Nov 2009 20:11:40 +0000 |
parents | 92e7a641b4a6 |
children | 30e6d6097b12 |
comparison
equal
deleted
inserted
replaced
449:938498b8cd03 | 450:5d74d0e4a4cc |
---|---|
1 import pygame | 1 import pygame |
2 import xmlrpclib | |
2 from pgu import gui | 3 from pgu import gui |
3 | 4 |
4 import icons | 5 import icons |
5 import constants | 6 import constants |
6 import buildings | 7 import buildings |
7 import equipment | 8 import equipment |
8 import cursors | 9 import cursors |
9 import engine | 10 import engine |
10 try: | |
11 import json | |
12 except ImportError: | |
13 import simplejson as json | |
14 | 11 |
15 class OpaqueLabel(gui.Label): | 12 class OpaqueLabel(gui.Label): |
16 def __init__(self, value, **params): | 13 def __init__(self, value, **params): |
17 gui.Label.__init__(self, value, **params) | 14 gui.Label.__init__(self, value, **params) |
18 if 'width' in params: | 15 if 'width' in params: |
164 | 161 |
165 def save(): | 162 def save(): |
166 if dialog.value is None: | 163 if dialog.value is None: |
167 return | 164 return |
168 data = self.gameboard.save_game() | 165 data = self.gameboard.save_game() |
169 open(dialog.value, "wb").write(json.dumps(data)) | 166 xml = xmlrpclib.dumps((data,), "foxassault") |
167 try: | |
168 open(dialog.value, "wb").write(xml) | |
169 except Exception, e: | |
170 print "Failed to save game: %s" % (e,) | |
170 | 171 |
171 dialog.connect(gui.CHANGE, save) | 172 dialog.connect(gui.CHANGE, save) |
172 dialog.open() | 173 dialog.open() |
173 | 174 |
174 def load_game(self): | 175 def load_game(self): |
176 dialog = gui.FileDialog("Load game ...", button_txt="Load") | 177 dialog = gui.FileDialog("Load game ...", button_txt="Load") |
177 | 178 |
178 def restore(): | 179 def restore(): |
179 if dialog.value is None: | 180 if dialog.value is None: |
180 return | 181 return |
181 data = json.loads(open(dialog.value, "rb").read()) | 182 try: |
183 xml = open(dialog.value, "rb").read() | |
184 params, methodname = xmlrpclib.loads(xml) | |
185 if methodname != "foxassault": | |
186 raise ValueError("Bad XML save game.") | |
187 data = params[0] | |
188 except Exception, e: | |
189 "Failed to load game: %s" % (e,) | |
190 return | |
191 | |
182 self.gameboard.restore_game(data) | 192 self.gameboard.restore_game(data) |
183 | 193 |
184 dialog.connect(gui.CHANGE, restore) | 194 dialog.connect(gui.CHANGE, restore) |
185 dialog.open() | 195 dialog.open() |
186 | 196 |