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

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

AEI2 0.99e:

  • Added "-nocacheupdate" argument to prevent from updating local Depot cache
File size: 7.2 KB
RevLine 
[591]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;
[594]10import java.util.Vector;
[591]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 */
[594]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 */
[591]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;
[605]67
[621]68 private boolean offlineMode = false;
[649]69 private boolean noCacheUpdate = false;
[621]70
[591]71 /**
[605]72 * @return path to wine
73 */
74 public static String getWinePath() {
75 if (getPlatform() != Platform.LINUX)
76 return null;
77
78 Vector<String> cmd = new Vector<String>();
79 cmd.add("which");
80 cmd.add("wine");
81 Vector<String> res = null;
82 try {
[623]83 res = AppExecution.executeAndWait(cmd);
[605]84 } catch (IOException e) {
85 e.printStackTrace();
86 }
87 if (res != null) {
88 if (res.get(0).startsWith("/") && res.get(0).endsWith("wine")) {
89 return res.get(0);
90 }
91 }
92 return null;
93 }
94
95 /**
[591]96 * Get the singleton instance
97 *
98 * @return Singleton instance
99 */
100 public static Settings getInstance() {
101 return instance;
102 }
103
104 /**
105 * @param debug
106 * Debug mode
107 */
108 public static void setDebug(boolean debug) {
109 debugRun = debug;
110 }
111
112 /**
113 * @return Is debug run
114 */
[625]115 public static boolean isDebug() {
[591]116 return debugRun;
117 }
118
119 /**
[594]120 * @return Processor architecture
121 */
122 public static Architecture getArchitecture() {
123 switch (getPlatform()) {
124 case WIN:
125 String arch = System.getenv("PROCESSOR_ARCHITECTURE")
126 .toLowerCase();
127 if (arch.startsWith("x86"))
128 return Architecture.X86;
129 return Architecture.AMD64;
130 case MACOS:
131 case LINUX:
132 Vector<String> cmd = new Vector<String>();
133 cmd.add("getconf");
134 cmd.add("LONG_BIT");
135 Vector<String> res = null;
136 try {
[623]137 res = AppExecution.executeAndWait(cmd);
[594]138 } catch (IOException e) {
139 e.printStackTrace();
140 }
141 if (res != null) {
142 if (res.get(0).equals("64"))
143 return Architecture.AMD64;
144 }
145 return Architecture.X86;
146 default:
147 return null;
148 }
149 }
150
151 /**
[591]152 * @return The operating system running on
153 */
154 public static Platform getPlatform() {
155 String os = System.getProperty("os.name").toLowerCase();
156 if (os.startsWith("win"))
157 return Platform.WIN;
158 if (os.startsWith("linux"))
159 return Platform.LINUX;
160 if (os.startsWith("mac"))
161 return Platform.MACOS;
162 return Platform.UNKNOWN;
163 }
164
165 /**
[621]166 * @return Is offline?
167 */
168 public boolean isOfflineMode() {
169 return offlineMode;
170 }
171
172 /**
173 * @param offline
174 * Is offline?
175 */
176 public void setOfflineMode(boolean offline) {
177 this.offlineMode = offline;
178 }
179
180 /**
[649]181 * @return Is in noCacheUpdate mode?
182 */
183 public boolean isNoCacheUpdateMode() {
184 return noCacheUpdate;
185 }
186
187 /**
188 * @param noCacheUpdate
189 * Is in noCacheUpdate mode?
190 */
191 public void setNoCacheUpdateMode(boolean noCacheUpdate) {
192 this.noCacheUpdate = noCacheUpdate;
193 }
194
195 /**
[591]196 * @return Mod Depot cache filename
197 */
[596]198 public static File getDepotCacheFilename() {
199 return new File(Paths.getPrefsPath(), "ModDepotCache.xml");
[591]200 }
201
[596]202 private static File getSettingsFilename() {
203 return new File(Paths.getPrefsPath(), "AEI-Settings.xml");
[591]204 }
205
206 private static XStream getXStream() {
207 XStream xs = new XStream(new StaxDriver());
208 xs.alias("Settings", Settings.class);
209 return xs;
210 }
211
212 /**
213 * Serializes the settings to disk
214 */
215 public void serializeToFile() {
216 try {
217 FileOutputStream fos = new FileOutputStream(getSettingsFilename());
218 XStream xs = getXStream();
219 xs.toXML(this, fos);
220 fos.close();
221 } catch (FileNotFoundException e) {
222 e.printStackTrace();
223 } catch (IOException e) {
224 e.printStackTrace();
225 }
226 }
227
228 /**
229 * Deserializes the settings from disk
230 */
231 public static void deserializeFromFile() {
232 try {
233 FileInputStream fis = new FileInputStream(getSettingsFilename());
234 XStream xs = getXStream();
235 Object obj = xs.fromXML(fis);
236 if (obj instanceof Settings)
237 instance = (Settings) obj;
238 fis.close();
239 } catch (FileNotFoundException e) {
240 } catch (IOException e) {
241 }
242 }
243
244 /**
245 * Put a string value
246 *
247 * @param key
248 * Key for value
249 * @param value
250 * Value
251 */
252 public void put(String key, String value) {
253 prefs.put(key, value);
254 }
255
256 /**
257 * Put a boolean value
258 *
259 * @param key
260 * Key for value
261 * @param value
262 * Value
263 */
264 public void put(String key, boolean value) {
265 prefs.put(key, value);
266 }
267
268 /**
269 * Put a int value
270 *
271 * @param key
272 * Key for value
273 * @param value
274 * Value
275 */
276 public void put(String key, int value) {
277 prefs.put(key, value);
278 }
279
280 /**
281 * Get a string value
282 *
283 * @param key
284 * Key for value
285 * @param def
286 * Default return value if key does not exist
287 * @return Value
288 */
289 public String get(String key, String def) {
290 if (prefs.containsKey(key)) {
291 if (prefs.get(key) instanceof String)
292 return (String) (prefs.get(key));
293 }
294 if (printNamesNotInMap)
295 System.out.println("Settings: Key \"" + key
296 + "\" not in Map, defaulting to \"" + def + "\".");
297 return def;
298 }
299
300 /**
301 * Get a boolean value
302 *
303 * @param key
304 * Key for value
305 * @param def
306 * Default return value if key does not exist
307 * @return Value
308 */
309 public Boolean get(String key, Boolean def) {
310 if (prefs.containsKey(key)) {
311 if (prefs.get(key) instanceof Boolean)
312 return (Boolean) (prefs.get(key));
313 }
314 if (printNamesNotInMap)
315 System.out.println("Settings: Key \"" + key
316 + "\" not in Map, defaulting to \"" + def + "\".");
317 return def;
318 }
319
320 /**
321 * Get a int value
322 *
323 * @param key
324 * Key for value
325 * @param def
326 * Default return value if key does not exist
327 * @return Value
328 */
329 public int get(String key, int def) {
330 if (prefs.containsKey(key)) {
331 if (prefs.get(key) instanceof Integer)
332 return (Integer) (prefs.get(key));
333 }
334 if (printNamesNotInMap)
335 System.out.println("Settings: Key \"" + key
336 + "\" not in Map, defaulting to \"" + def + "\".");
337 return def;
338 }
339
340 /**
341 * Remove a value
342 *
343 * @param key
344 * Key to value to remove
345 */
346 public void removeValue(String key) {
347 prefs.remove(key);
348 }
349
350}
Note: See TracBrowser for help on using the repository browser.