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