package net.oni2.aeinstaller.backend; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.Serializable; import java.util.HashMap; import java.util.Vector; import com.thoughtworks.xstream.XStream; import com.thoughtworks.xstream.io.xml.StaxDriver; /** * Manages and stores programm settings * * @author Christian Illy */ public class Settings implements Serializable { private static final long serialVersionUID = 8067725289301601179L; /** * @author Christian Illy */ public enum Architecture { /** * 32 bit */ X86, /** * 64 bit */ AMD64 }; /** * @author Christian Illy */ public enum Platform { /** * Running Windows */ WIN, /** * Running MacOS */ MACOS, /** * Running a Linux */ LINUX, /** * Unknown OS */ UNKNOWN } private static Settings instance = new Settings(); private static boolean debugRun = false; private HashMap prefs = new HashMap(); private boolean printNamesNotInMap = false; /** * @return path to wine */ public static String getWinePath() { if (getPlatform() != Platform.LINUX) return null; Vector cmd = new Vector(); cmd.add("which"); cmd.add("wine"); Vector res = null; try { res = QuickAppExecution.execute(cmd); } catch (IOException e) { e.printStackTrace(); } if (res != null) { if (res.get(0).startsWith("/") && res.get(0).endsWith("wine")) { return res.get(0); } } return null; } /** * Get the singleton instance * * @return Singleton instance */ public static Settings getInstance() { return instance; } /** * @param debug * Debug mode */ public static void setDebug(boolean debug) { debugRun = debug; } /** * @return Is debug run */ public static boolean getDebug() { return debugRun; } /** * @return Processor architecture */ public static Architecture getArchitecture() { switch (getPlatform()) { case WIN: String arch = System.getenv("PROCESSOR_ARCHITECTURE") .toLowerCase(); if (arch.startsWith("x86")) return Architecture.X86; return Architecture.AMD64; case MACOS: case LINUX: Vector cmd = new Vector(); cmd.add("getconf"); cmd.add("LONG_BIT"); Vector res = null; try { res = QuickAppExecution.execute(cmd); } catch (IOException e) { e.printStackTrace(); } if (res != null) { if (res.get(0).equals("64")) return Architecture.AMD64; } return Architecture.X86; default: return null; } } /** * @return The operating system running on */ public static Platform getPlatform() { String os = System.getProperty("os.name").toLowerCase(); if (os.startsWith("win")) return Platform.WIN; if (os.startsWith("linux")) return Platform.LINUX; if (os.startsWith("mac")) return Platform.MACOS; return Platform.UNKNOWN; } /** * @return Mod Depot cache filename */ public static File getDepotCacheFilename() { return new File(Paths.getPrefsPath(), "ModDepotCache.xml"); } private static File getSettingsFilename() { return new File(Paths.getPrefsPath(), "AEI-Settings.xml"); } private static XStream getXStream() { XStream xs = new XStream(new StaxDriver()); xs.alias("Settings", Settings.class); return xs; } /** * Serializes the settings to disk */ public void serializeToFile() { try { FileOutputStream fos = new FileOutputStream(getSettingsFilename()); XStream xs = getXStream(); xs.toXML(this, fos); fos.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } /** * Deserializes the settings from disk */ public static void deserializeFromFile() { try { FileInputStream fis = new FileInputStream(getSettingsFilename()); XStream xs = getXStream(); Object obj = xs.fromXML(fis); if (obj instanceof Settings) instance = (Settings) obj; fis.close(); } catch (FileNotFoundException e) { } catch (IOException e) { } } /** * Put a string value * * @param key * Key for value * @param value * Value */ public void put(String key, String value) { prefs.put(key, value); } /** * Put a boolean value * * @param key * Key for value * @param value * Value */ public void put(String key, boolean value) { prefs.put(key, value); } /** * Put a int value * * @param key * Key for value * @param value * Value */ public void put(String key, int value) { prefs.put(key, value); } /** * Get a string value * * @param key * Key for value * @param def * Default return value if key does not exist * @return Value */ public String get(String key, String def) { if (prefs.containsKey(key)) { if (prefs.get(key) instanceof String) return (String) (prefs.get(key)); } if (printNamesNotInMap) System.out.println("Settings: Key \"" + key + "\" not in Map, defaulting to \"" + def + "\"."); return def; } /** * Get a boolean value * * @param key * Key for value * @param def * Default return value if key does not exist * @return Value */ public Boolean get(String key, Boolean def) { if (prefs.containsKey(key)) { if (prefs.get(key) instanceof Boolean) return (Boolean) (prefs.get(key)); } if (printNamesNotInMap) System.out.println("Settings: Key \"" + key + "\" not in Map, defaulting to \"" + def + "\"."); return def; } /** * Get a int value * * @param key * Key for value * @param def * Default return value if key does not exist * @return Value */ public int get(String key, int def) { if (prefs.containsKey(key)) { if (prefs.get(key) instanceof Integer) return (Integer) (prefs.get(key)); } if (printNamesNotInMap) System.out.println("Settings: Key \"" + key + "\" not in Map, defaulting to \"" + def + "\"."); return def; } /** * Remove a value * * @param key * Key to value to remove */ public void removeValue(String key) { prefs.remove(key); } }