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