Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
epoxy
GitHub Repository: epoxy/proj11
Path: blob/master/SLICK_HOME/src/org/newdawn/slick/SavedState.java
1456 views
1
package org.newdawn.slick;
2
3
import java.io.IOException;
4
import java.util.HashMap;
5
6
import javax.jnlp.ServiceManager;
7
8
import org.newdawn.slick.muffin.FileMuffin;
9
import org.newdawn.slick.muffin.Muffin;
10
import org.newdawn.slick.muffin.WebstartMuffin;
11
import org.newdawn.slick.util.Log;
12
13
/**
14
* A utility to allow game setup/state to be stored locally. This utility will adapt to the
15
* current enviornment (webstart or file based). Note that this will not currently
16
* work in an applet.
17
*
18
* @author kappaOne
19
*/
20
public class SavedState {
21
/** file name of where the scores will be saved */
22
private String fileName;
23
/** Type of Muffin to use */
24
private Muffin muffin;
25
/** hash map where int data will be stored */
26
private HashMap numericData = new HashMap();
27
/** hash map where string data will be stored */
28
private HashMap stringData = new HashMap();
29
30
/**
31
* Create and Test to see if the app is running
32
* as webstart or local app and select the appropriate
33
* muffin type
34
*
35
* @param fileName name of muffin where data will be saved
36
* @throws SlickException Indicates a failure to load the stored state
37
*/
38
public SavedState(String fileName) throws SlickException {
39
this.fileName = fileName;
40
41
if (isWebstartAvailable()) {
42
muffin = new WebstartMuffin();
43
}
44
else {
45
muffin = new FileMuffin();
46
}
47
48
try {
49
load();
50
} catch (IOException e) {
51
throw new SlickException("Failed to load state on startup",e);
52
}
53
}
54
55
/**
56
* Get number stored at given location
57
*
58
* @param nameOfField The name of the number to retrieve
59
* @return The number saved at this location
60
*/
61
public double getNumber(String nameOfField) {
62
return getNumber(nameOfField, 0);
63
}
64
65
/**
66
* Get number stored at given location
67
*
68
* @param nameOfField The name of the number to retrieve
69
* @param defaultValue The value to return if the specified value hasn't been set
70
* @return The number saved at this location
71
*/
72
public double getNumber(String nameOfField, double defaultValue) {
73
Double value = ((Double)numericData.get(nameOfField));
74
75
if (value == null) {
76
return defaultValue;
77
}
78
79
return value.doubleValue();
80
}
81
82
/**
83
* Save the given value at the given location
84
* will overwrite any previous value at this location
85
*
86
* @param nameOfField The name to store the value against
87
* @param value The value to store
88
*/
89
public void setNumber(String nameOfField, double value){
90
numericData.put(nameOfField, new Double(value));
91
}
92
93
/**
94
* Get the String at the given location
95
*
96
* @param nameOfField location of string
97
* @return String stored at the location given
98
*/
99
public String getString(String nameOfField) {
100
return getString(nameOfField, null);
101
}
102
103
/**
104
* Get the String at the given location
105
*
106
* @param nameOfField location of string
107
* @param defaultValue The value to return if the specified value hasn't been set
108
* @return String stored at the location given
109
*/
110
public String getString(String nameOfField, String defaultValue) {
111
String value = (String) stringData.get(nameOfField);
112
113
if (value == null) {
114
return defaultValue;
115
}
116
117
return value;
118
}
119
120
/**
121
* Save the given value at the given location
122
* will overwrite any previous value at this location
123
*
124
* @param nameOfField location to store int
125
* @param value The value to store
126
*/
127
public void setString(String nameOfField, String value){
128
stringData.put(nameOfField, value);
129
}
130
131
/**
132
* Save the stored data to file/muffin
133
*
134
* @throws IOException Indicates it wasn't possible to store the state
135
*/
136
public void save() throws IOException {
137
muffin.saveFile(numericData, fileName + "_Number");
138
muffin.saveFile(stringData, fileName + "_String");
139
}
140
141
/**
142
* Load the data from file/muffin
143
*
144
* @throws IOException Indicates it wasn't possible to load the state
145
*/
146
public void load() throws IOException {
147
numericData = muffin.loadFile(fileName + "_Number");
148
stringData = muffin.loadFile(fileName + "_String");
149
}
150
151
/**
152
* Will delete all current data held in Score
153
*/
154
public void clear() {
155
numericData.clear();
156
stringData.clear();
157
}
158
159
/**
160
* Quick test to see if running through Java webstart
161
*
162
* @return True if jws running
163
*/
164
private boolean isWebstartAvailable() {
165
try {
166
Class.forName("javax.jnlp.ServiceManager");
167
// this causes to go and see if the service is available
168
ServiceManager.lookup("javax.jnlp.PersistenceService");
169
Log.info("Webstart detected using Muffins");
170
} catch (Exception e) {
171
Log.info("Using Local File System");
172
return false;
173
}
174
return true;
175
}
176
}
177