1
package games.strategy.engine.framework;
6
import games.strategy.engine.data.*;
7
import games.strategy.engine.delegate.*;
8
import games.strategy.util.*;
9
import java.util.zip.*;
10
import games.strategy.engine.*;
17
* Description: Respsible for loading saved games, new games from xml, and
21
* @author Sean Bridges
24
public class GameDataManager
27
private final static String DELEGATE_START = "<DelegateStart>";
28
private final static String DELEGATE_DATA_NEXT = "<DelegateData>";
29
private final static String DELEGATE_LIST_END = "<EndDelegateList>";
31
public GameDataManager()
35
public GameData loadGame(File savedGameFile) throws IOException
37
InputStream input = null;
40
input = new BufferedInputStream(new FileInputStream(savedGameFile));
41
return loadGame(input);
55
public GameData loadGame(InputStream input) throws IOException
57
return loadGame(new ObjectInputStream(new GZIPInputStream(input)));
60
public GameData loadGame(ObjectInputStream input) throws IOException
64
//TODO we should check the game version as well
65
Version readVersion = (Version) input.readObject();
66
if (!readVersion.equals(EngineVersion.VERSION))
67
throw new IOException("Incompatible engine versions. We are: " + EngineVersion.VERSION + " . Trying to load game created with: " + readVersion);
69
GameData data = (GameData) input.readObject();
71
loadDelegates(input, data);
72
data.postDeSerialize();
76
} catch (ClassNotFoundException cnfe)
78
throw new IOException(cnfe.getMessage());
83
private void loadDelegates(ObjectInputStream input, GameData data) throws ClassNotFoundException, IOException
85
for (Object endMarker = input.readObject(); !endMarker.equals(DELEGATE_LIST_END); endMarker = input.readObject())
87
String name = (String) input.readObject();
88
String displayName = (String) input.readObject();
89
String className = (String) input.readObject();
94
instance = (IDelegate) Class.forName(className).newInstance();
95
instance.initialize(name, displayName);
96
data.getDelegateList().addDelegate(instance);
100
throw new IOException(e.getMessage());
103
String next = (String) input.readObject();
104
if (next.equals(DELEGATE_DATA_NEXT))
106
instance.loadState((Serializable) input.readObject());
111
public void saveGame(File destination, GameData data) throws IOException
114
BufferedOutputStream out = null;
117
OutputStream fileStream = new FileOutputStream(destination);
118
out = new BufferedOutputStream(fileStream);
120
saveGame(fileStream, data);
127
} catch (Exception e)
134
public void saveGame(OutputStream sink, GameData data) throws IOException
136
saveGame(sink, data, true);
139
public void saveGame(OutputStream sink, GameData data, boolean saveDelegateInfo) throws IOException
141
//write internally first in case of error
142
ByteArrayOutputStream bytes = new ByteArrayOutputStream(25000);
143
ObjectOutputStream outStream = new ObjectOutputStream(bytes);
145
outStream.writeObject(games.strategy.engine.EngineVersion.VERSION);
146
data.acquireReadLock();
149
outStream.writeObject(data);
151
if (saveDelegateInfo)
152
writeDelegates(data, outStream);
154
outStream.writeObject(DELEGATE_LIST_END);
158
data.releaseReadLock();
161
GZIPOutputStream zippedOut = new GZIPOutputStream(sink);
163
zippedOut.write(bytes.toByteArray());
168
private void writeDelegates(GameData data, ObjectOutputStream out) throws IOException
171
Iterator iter = data.getDelegateList().iterator();
172
while (iter.hasNext())
174
out.writeObject(DELEGATE_START);
176
IDelegate delegate = (IDelegate) iter.next();
178
//write out the delegate info
179
out.writeObject(delegate.getName());
180
out.writeObject(delegate.getDisplayName());
181
out.writeObject(delegate.getClass().getName());
184
out.writeObject(DELEGATE_DATA_NEXT);
185
out.writeObject(delegate.saveState());
187
//mark end of delegate section
188
out.writeObject(DELEGATE_LIST_END);