source: AE/installer2/src/net/oni2/aeinstaller/backend/Settings.java@ 619

Last change on this file since 619 was 619, checked in by alloc, 12 years ago

AEI2:

  • Sanitizing level file names for MacOS (level*_Final.#)
File size: 6.5 KB
Line 
1package net.oni2.aeinstaller.backend;
2
3import java.io.File;
4import java.io.FileInputStream;
5import java.io.FileNotFoundException;
6import java.io.FileOutputStream;
7import java.io.IOException;
8import java.io.Serializable;
9import java.util.HashMap;
10import java.util.Vector;
11
12import com.thoughtworks.xstream.XStream;
13import com.thoughtworks.xstream.io.xml.StaxDriver;
14
15/**
16 * Manages and stores programm settings
17 *
18 * @author Christian Illy
19 */
20public class Settings implements Serializable {
21
22 private static final long serialVersionUID = 8067725289301601179L;
23
24 /**
25 * @author Christian Illy
26 */
27 public enum Architecture {
28 /**
29 * 32 bit
30 */
31 X86,
32 /**
33 * 64 bit
34 */
35 AMD64
36 };
37
38 /**
39 * @author Christian Illy
40 */
41 public enum Platform {
42 /**
43 * Running Windows
44 */
45 WIN,
46 /**
47 * Running MacOS
48 */
49 MACOS,
50 /**
51 * Running a Linux
52 */
53 LINUX,
54 /**
55 * Unknown OS
56 */
57 UNKNOWN
58 }
59
60 private static Settings instance = new Settings();
61
62 private static boolean debugRun = false;
63
64 private HashMap<String, Object> prefs = new HashMap<String, Object>();
65
66 private boolean printNamesNotInMap = false;
67
68 /**
69 * @return path to wine
70 */
71 public static String getWinePath() {
72 if (getPlatform() != Platform.LINUX)
73 return null;
74
75 Vector<String> cmd = new Vector<String>();
76 cmd.add("which");
77 cmd.add("wine");
78 Vector<String> res = null;
79 try {
80 res = QuickAppExecution.execute(cmd);
81 } catch (IOException e) {
82 e.printStackTrace();
83 }
84 if (res != null) {
85 if (res.get(0).startsWith("/") && res.get(0).endsWith("wine")) {
86 return res.get(0);
87 }
88 }
89 return null;
90 }
91
92 /**
93 * Get the singleton instance
94 *
95 * @return Singleton instance
96 */
97 public static Settings getInstance() {
98 return instance;
99 }
100
101 /**
102 * @param debug
103 * Debug mode
104 */
105 public static void setDebug(boolean debug) {
106 debugRun = debug;
107 }
108
109 /**
110 * @return Is debug run
111 */
112 public static boolean getDebug() {
113 return debugRun;
114 }
115
116 /**
117 * @return Processor architecture
118 */
119 public static Architecture getArchitecture() {
120 switch (getPlatform()) {
121 case WIN:
122 String arch = System.getenv("PROCESSOR_ARCHITECTURE")
123 .toLowerCase();
124 if (arch.startsWith("x86"))
125 return Architecture.X86;
126 return Architecture.AMD64;
127 case MACOS:
128 case LINUX:
129 Vector<String> cmd = new Vector<String>();
130 cmd.add("getconf");
131 cmd.add("LONG_BIT");
132 Vector<String> res = null;
133 try {
134 res = QuickAppExecution.execute(cmd);
135 } catch (IOException e) {
136 e.printStackTrace();
137 }
138 if (res != null) {
139 if (res.get(0).equals("64"))
140 return Architecture.AMD64;
141 }
142 return Architecture.X86;
143 default:
144 return null;
145 }
146 }
147
148 /**
149 * @return The operating system running on
150 */
151 public static Platform getPlatform() {
152 String os = System.getProperty("os.name").toLowerCase();
153 if (os.startsWith("win"))
154 return Platform.WIN;
155 if (os.startsWith("linux"))
156 return Platform.LINUX;
157 if (os.startsWith("mac"))
158 return Platform.MACOS;
159 return Platform.UNKNOWN;
160 }
161
162 /**
163 * @return Mod Depot cache filename
164 */
165 public static File getDepotCacheFilename() {
166 return new File(Paths.getPrefsPath(), "ModDepotCache.xml");
167 }
168
169 private static File getSettingsFilename() {
170 return new File(Paths.getPrefsPath(), "AEI-Settings.xml");
171 }
172
173 private static XStream getXStream() {
174 XStream xs = new XStream(new StaxDriver());
175 xs.alias("Settings", Settings.class);
176 return xs;
177 }
178
179 /**
180 * Serializes the settings to disk
181 */
182 public void serializeToFile() {
183 try {
184 FileOutputStream fos = new FileOutputStream(getSettingsFilename());
185 XStream xs = getXStream();
186 xs.toXML(this, fos);
187 fos.close();
188 } catch (FileNotFoundException e) {
189 e.printStackTrace();
190 } catch (IOException e) {
191 e.printStackTrace();
192 }
193 }
194
195 /**
196 * Deserializes the settings from disk
197 */
198 public static void deserializeFromFile() {
199 try {
200 FileInputStream fis = new FileInputStream(getSettingsFilename());
201 XStream xs = getXStream();
202 Object obj = xs.fromXML(fis);
203 if (obj instanceof Settings)
204 instance = (Settings) obj;
205 fis.close();
206 } catch (FileNotFoundException e) {
207 } catch (IOException e) {
208 }
209 }
210
211 /**
212 * Put a string value
213 *
214 * @param key
215 * Key for value
216 * @param value
217 * Value
218 */
219 public void put(String key, String value) {
220 prefs.put(key, value);
221 }
222
223 /**
224 * Put a boolean value
225 *
226 * @param key
227 * Key for value
228 * @param value
229 * Value
230 */
231 public void put(String key, boolean value) {
232 prefs.put(key, value);
233 }
234
235 /**
236 * Put a int value
237 *
238 * @param key
239 * Key for value
240 * @param value
241 * Value
242 */
243 public void put(String key, int value) {
244 prefs.put(key, value);
245 }
246
247 /**
248 * Get a string value
249 *
250 * @param key
251 * Key for value
252 * @param def
253 * Default return value if key does not exist
254 * @return Value
255 */
256 public String get(String key, String def) {
257 if (prefs.containsKey(key)) {
258 if (prefs.get(key) instanceof String)
259 return (String) (prefs.get(key));
260 }
261 if (printNamesNotInMap)
262 System.out.println("Settings: Key \"" + key
263 + "\" not in Map, defaulting to \"" + def + "\".");
264 return def;
265 }
266
267 /**
268 * Get a boolean value
269 *
270 * @param key
271 * Key for value
272 * @param def
273 * Default return value if key does not exist
274 * @return Value
275 */
276 public Boolean get(String key, Boolean def) {
277 if (prefs.containsKey(key)) {
278 if (prefs.get(key) instanceof Boolean)
279 return (Boolean) (prefs.get(key));
280 }
281 if (printNamesNotInMap)
282 System.out.println("Settings: Key \"" + key
283 + "\" not in Map, defaulting to \"" + def + "\".");
284 return def;
285 }
286
287 /**
288 * Get a int value
289 *
290 * @param key
291 * Key for value
292 * @param def
293 * Default return value if key does not exist
294 * @return Value
295 */
296 public int get(String key, int def) {
297 if (prefs.containsKey(key)) {
298 if (prefs.get(key) instanceof Integer)
299 return (Integer) (prefs.get(key));
300 }
301 if (printNamesNotInMap)
302 System.out.println("Settings: Key \"" + key
303 + "\" not in Map, defaulting to \"" + def + "\".");
304 return def;
305 }
306
307 /**
308 * Remove a value
309 *
310 * @param key
311 * Key to value to remove
312 */
313 public void removeValue(String key) {
314 prefs.remove(key);
315 }
316
317}
Note: See TracBrowser for help on using the repository browser.