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