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

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

AEI: OniSplit / globalization

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