~ubuntu-branches/ubuntu/precise/triplea/precise

« back to all changes in this revision

Viewing changes to src/games/strategy/engine/framework/GameDataManager.java

  • Committer: Package Import Robot
  • Author(s): Scott Howard
  • Date: 2011-11-11 21:40:11 UTC
  • Revision ID: package-import@ubuntu.com-20111111214011-sehf2rwat36o2xqf
Tags: upstream-1.3.2.2
ImportĀ upstreamĀ versionĀ 1.3.2.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package games.strategy.engine.framework;
 
2
 
 
3
import java.io.*;
 
4
import java.util.*;
 
5
 
 
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.*;
 
11
 
 
12
/**
 
13
 * <p>
 
14
 * Title: TripleA
 
15
 * </p>
 
16
 * <p>
 
17
 * Description: Respsible for loading saved games, new games from xml, and
 
18
 * saving games
 
19
 * </p>
 
20
 * 
 
21
 * @author Sean Bridges
 
22
 */
 
23
 
 
24
public class GameDataManager
 
25
{
 
26
 
 
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>";
 
30
 
 
31
    public GameDataManager()
 
32
    {
 
33
    }
 
34
 
 
35
    public GameData loadGame(File savedGameFile) throws IOException
 
36
    {
 
37
        InputStream input = null;
 
38
        try
 
39
        {
 
40
            input = new BufferedInputStream(new FileInputStream(savedGameFile));
 
41
            return loadGame(input);
 
42
        } finally
 
43
        {
 
44
            try
 
45
            {
 
46
                if(input != null)
 
47
                    input.close();
 
48
            } catch (Exception e)
 
49
            {
 
50
            }
 
51
        }
 
52
 
 
53
    }
 
54
 
 
55
    public GameData loadGame(InputStream input) throws IOException
 
56
    {
 
57
        return loadGame(new ObjectInputStream(new GZIPInputStream(input)));
 
58
    }
 
59
 
 
60
    public GameData loadGame(ObjectInputStream input) throws IOException
 
61
    {
 
62
        try
 
63
        {
 
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);
 
68
 
 
69
            GameData data = (GameData) input.readObject();
 
70
 
 
71
            loadDelegates(input, data);
 
72
            data.postDeSerialize();
 
73
 
 
74
            return data;
 
75
 
 
76
        } catch (ClassNotFoundException cnfe)
 
77
        {
 
78
            throw new IOException(cnfe.getMessage());
 
79
        }
 
80
 
 
81
    }
 
82
 
 
83
    private void loadDelegates(ObjectInputStream input, GameData data) throws ClassNotFoundException, IOException
 
84
    {
 
85
        for (Object endMarker = input.readObject(); !endMarker.equals(DELEGATE_LIST_END); endMarker = input.readObject())
 
86
        {
 
87
            String name = (String) input.readObject();
 
88
            String displayName = (String) input.readObject();
 
89
            String className = (String) input.readObject();
 
90
 
 
91
            IDelegate instance;
 
92
            try
 
93
            {
 
94
                instance = (IDelegate) Class.forName(className).newInstance();
 
95
                instance.initialize(name, displayName);
 
96
                data.getDelegateList().addDelegate(instance);
 
97
            } catch (Exception e)
 
98
            {
 
99
                e.printStackTrace();
 
100
                throw new IOException(e.getMessage());
 
101
            }
 
102
 
 
103
            String next = (String) input.readObject();
 
104
            if (next.equals(DELEGATE_DATA_NEXT))
 
105
            {
 
106
                instance.loadState((Serializable) input.readObject());
 
107
            }
 
108
        }
 
109
    }
 
110
 
 
111
    public void saveGame(File destination, GameData data) throws IOException
 
112
    {
 
113
        
 
114
        BufferedOutputStream out = null;
 
115
        try
 
116
        {
 
117
            OutputStream fileStream = new FileOutputStream(destination);
 
118
            out = new BufferedOutputStream(fileStream);
 
119
            
 
120
            saveGame(fileStream, data);
 
121
        } finally
 
122
        {
 
123
            try
 
124
            {
 
125
                if(out != null)
 
126
                    out.close();
 
127
            } catch (Exception e)
 
128
            {
 
129
                e.printStackTrace();
 
130
            }
 
131
        }
 
132
    }
 
133
 
 
134
    public void saveGame(OutputStream sink, GameData data) throws IOException
 
135
    {
 
136
        saveGame(sink, data, true);
 
137
    }
 
138
 
 
139
    public void saveGame(OutputStream sink, GameData data, boolean saveDelegateInfo) throws IOException
 
140
    {        
 
141
        //write internally first in case of error
 
142
        ByteArrayOutputStream bytes = new ByteArrayOutputStream(25000);
 
143
        ObjectOutputStream outStream = new ObjectOutputStream(bytes);
 
144
 
 
145
        outStream.writeObject(games.strategy.engine.EngineVersion.VERSION);
 
146
        data.acquireReadLock();
 
147
        try
 
148
        {
 
149
            outStream.writeObject(data);
 
150
                
 
151
                if (saveDelegateInfo)
 
152
                    writeDelegates(data, outStream);
 
153
                else
 
154
                    outStream.writeObject(DELEGATE_LIST_END);
 
155
        }
 
156
        finally
 
157
        {
 
158
            data.releaseReadLock();
 
159
        }
 
160
        
 
161
        GZIPOutputStream zippedOut = new GZIPOutputStream(sink);
 
162
        //now write to file
 
163
        zippedOut.write(bytes.toByteArray());
 
164
        zippedOut.flush();
 
165
        zippedOut.close();
 
166
    }
 
167
 
 
168
    private void writeDelegates(GameData data, ObjectOutputStream out) throws IOException
 
169
    {
 
170
 
 
171
        Iterator iter = data.getDelegateList().iterator();
 
172
        while (iter.hasNext())
 
173
        {
 
174
            out.writeObject(DELEGATE_START);
 
175
 
 
176
            IDelegate delegate = (IDelegate) iter.next();
 
177
 
 
178
            //write out the delegate info
 
179
            out.writeObject(delegate.getName());
 
180
            out.writeObject(delegate.getDisplayName());
 
181
            out.writeObject(delegate.getClass().getName());
 
182
 
 
183
 
 
184
            out.writeObject(DELEGATE_DATA_NEXT);
 
185
            out.writeObject(delegate.saveState());
 
186
        }
 
187
        //mark end of delegate section
 
188
        out.writeObject(DELEGATE_LIST_END);
 
189
    }
 
190
 
 
191
}
 
192