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