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

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

AEI2: .NET detection

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