[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.io.UnsupportedEncodingException;
|
---|
| 10 | import java.net.URLDecoder;
|
---|
| 11 | import java.util.HashMap;
|
---|
| 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 Platform {
|
---|
| 29 | /**
|
---|
| 30 | * Running Windows
|
---|
| 31 | */
|
---|
| 32 | WIN,
|
---|
| 33 | /**
|
---|
| 34 | * Running MacOS
|
---|
| 35 | */
|
---|
| 36 | MACOS,
|
---|
| 37 | /**
|
---|
| 38 | * Running a Linux
|
---|
| 39 | */
|
---|
| 40 | LINUX,
|
---|
| 41 | /**
|
---|
| 42 | * Unknown OS
|
---|
| 43 | */
|
---|
| 44 | UNKNOWN
|
---|
| 45 | }
|
---|
| 46 |
|
---|
| 47 | private static Settings instance = new Settings();
|
---|
| 48 |
|
---|
| 49 | private static boolean debugRun = false;
|
---|
| 50 |
|
---|
| 51 | private HashMap<String, Object> prefs = new HashMap<String, Object>();
|
---|
| 52 |
|
---|
| 53 | private boolean printNamesNotInMap = false;
|
---|
| 54 |
|
---|
| 55 | /**
|
---|
| 56 | * Get the singleton instance
|
---|
| 57 | *
|
---|
| 58 | * @return Singleton instance
|
---|
| 59 | */
|
---|
| 60 | public static Settings getInstance() {
|
---|
| 61 | return instance;
|
---|
| 62 | }
|
---|
| 63 |
|
---|
| 64 | /**
|
---|
| 65 | * @param debug
|
---|
| 66 | * Debug mode
|
---|
| 67 | */
|
---|
| 68 | public static void setDebug(boolean debug) {
|
---|
| 69 | debugRun = debug;
|
---|
| 70 | }
|
---|
| 71 |
|
---|
| 72 | /**
|
---|
| 73 | * @return Is debug run
|
---|
| 74 | */
|
---|
| 75 | public static boolean getDebug() {
|
---|
| 76 | return debugRun;
|
---|
| 77 | }
|
---|
| 78 |
|
---|
| 79 | /**
|
---|
| 80 | * @return The operating system running on
|
---|
| 81 | */
|
---|
| 82 | public static Platform getPlatform() {
|
---|
| 83 | String os = System.getProperty("os.name").toLowerCase();
|
---|
| 84 | if (os.startsWith("win"))
|
---|
| 85 | return Platform.WIN;
|
---|
| 86 | if (os.startsWith("linux"))
|
---|
| 87 | return Platform.LINUX;
|
---|
| 88 | if (os.startsWith("mac"))
|
---|
| 89 | return Platform.MACOS;
|
---|
| 90 | return Platform.UNKNOWN;
|
---|
| 91 | }
|
---|
| 92 |
|
---|
| 93 | /**
|
---|
| 94 | * Get the Jar path
|
---|
| 95 | *
|
---|
| 96 | * @return Path
|
---|
| 97 | */
|
---|
| 98 | public static String getJarPath() {
|
---|
| 99 | String jarPath = Settings.class.getProtectionDomain().getCodeSource()
|
---|
| 100 | .getLocation().getPath();
|
---|
| 101 | String decodedPath = null;
|
---|
| 102 | try {
|
---|
| 103 | decodedPath = URLDecoder.decode(jarPath, "UTF-8");
|
---|
| 104 | } catch (UnsupportedEncodingException e) {
|
---|
| 105 | e.printStackTrace();
|
---|
| 106 | }
|
---|
| 107 | return new File(decodedPath).getParentFile().getPath() + "/";
|
---|
| 108 | }
|
---|
| 109 |
|
---|
| 110 | /**
|
---|
| 111 | * Get the preferences path
|
---|
| 112 | *
|
---|
| 113 | * @return Path
|
---|
| 114 | */
|
---|
| 115 | public static String getPrefsPath() {
|
---|
| 116 | return getJarPath();
|
---|
| 117 | }
|
---|
| 118 |
|
---|
| 119 | /**
|
---|
| 120 | * Get the path to store downloaded files
|
---|
| 121 | *
|
---|
| 122 | * @return Download path
|
---|
| 123 | */
|
---|
| 124 | public static String getDownloadPath() {
|
---|
| 125 | return getTempPath() + "oni_aei/";
|
---|
| 126 | }
|
---|
| 127 |
|
---|
| 128 | /**
|
---|
| 129 | * Get the path to store game information data
|
---|
| 130 | *
|
---|
| 131 | * @return Data path
|
---|
| 132 | */
|
---|
| 133 | public static String getDataPath() {
|
---|
| 134 | return getJarPath() + "mods/";
|
---|
| 135 | }
|
---|
| 136 |
|
---|
| 137 | /**
|
---|
| 138 | * Get the systems temp-path
|
---|
| 139 | *
|
---|
| 140 | * @return Path
|
---|
| 141 | */
|
---|
| 142 | public static String getTempPath() {
|
---|
| 143 | return new File(System.getProperty("java.io.tmpdir")).getPath() + "/";
|
---|
| 144 | }
|
---|
| 145 |
|
---|
| 146 | /**
|
---|
| 147 | * @return Mod Depot cache filename
|
---|
| 148 | */
|
---|
| 149 | public static String getDepotCacheFilename() {
|
---|
| 150 | return Settings.getPrefsPath() + "ModDepotCache.xml";
|
---|
| 151 | }
|
---|
| 152 |
|
---|
| 153 | private static String getSettingsFilename() {
|
---|
| 154 | return Settings.getPrefsPath() + "AEI-Settings.xml";
|
---|
| 155 | }
|
---|
| 156 |
|
---|
| 157 | private static XStream getXStream() {
|
---|
| 158 | XStream xs = new XStream(new StaxDriver());
|
---|
| 159 | xs.alias("Settings", Settings.class);
|
---|
| 160 | return xs;
|
---|
| 161 | }
|
---|
| 162 |
|
---|
| 163 | /**
|
---|
| 164 | * Serializes the settings to disk
|
---|
| 165 | */
|
---|
| 166 | public void serializeToFile() {
|
---|
| 167 | try {
|
---|
| 168 | FileOutputStream fos = new FileOutputStream(getSettingsFilename());
|
---|
| 169 | XStream xs = getXStream();
|
---|
| 170 | xs.toXML(this, fos);
|
---|
| 171 | fos.close();
|
---|
| 172 | } catch (FileNotFoundException e) {
|
---|
| 173 | e.printStackTrace();
|
---|
| 174 | } catch (IOException e) {
|
---|
| 175 | e.printStackTrace();
|
---|
| 176 | }
|
---|
| 177 | }
|
---|
| 178 |
|
---|
| 179 | /**
|
---|
| 180 | * Deserializes the settings from disk
|
---|
| 181 | */
|
---|
| 182 | public static void deserializeFromFile() {
|
---|
| 183 | try {
|
---|
| 184 | FileInputStream fis = new FileInputStream(getSettingsFilename());
|
---|
| 185 | XStream xs = getXStream();
|
---|
| 186 | Object obj = xs.fromXML(fis);
|
---|
| 187 | if (obj instanceof Settings)
|
---|
| 188 | instance = (Settings) obj;
|
---|
| 189 | fis.close();
|
---|
| 190 | } catch (FileNotFoundException e) {
|
---|
| 191 | } catch (IOException e) {
|
---|
| 192 | }
|
---|
| 193 | }
|
---|
| 194 |
|
---|
| 195 | /**
|
---|
| 196 | * Put a string value
|
---|
| 197 | *
|
---|
| 198 | * @param key
|
---|
| 199 | * Key for value
|
---|
| 200 | * @param value
|
---|
| 201 | * Value
|
---|
| 202 | */
|
---|
| 203 | public void put(String key, String value) {
|
---|
| 204 | prefs.put(key, value);
|
---|
| 205 | }
|
---|
| 206 |
|
---|
| 207 | /**
|
---|
| 208 | * Put a boolean value
|
---|
| 209 | *
|
---|
| 210 | * @param key
|
---|
| 211 | * Key for value
|
---|
| 212 | * @param value
|
---|
| 213 | * Value
|
---|
| 214 | */
|
---|
| 215 | public void put(String key, boolean value) {
|
---|
| 216 | prefs.put(key, value);
|
---|
| 217 | }
|
---|
| 218 |
|
---|
| 219 | /**
|
---|
| 220 | * Put a int value
|
---|
| 221 | *
|
---|
| 222 | * @param key
|
---|
| 223 | * Key for value
|
---|
| 224 | * @param value
|
---|
| 225 | * Value
|
---|
| 226 | */
|
---|
| 227 | public void put(String key, int value) {
|
---|
| 228 | prefs.put(key, value);
|
---|
| 229 | }
|
---|
| 230 |
|
---|
| 231 | /**
|
---|
| 232 | * Get a string value
|
---|
| 233 | *
|
---|
| 234 | * @param key
|
---|
| 235 | * Key for value
|
---|
| 236 | * @param def
|
---|
| 237 | * Default return value if key does not exist
|
---|
| 238 | * @return Value
|
---|
| 239 | */
|
---|
| 240 | public String get(String key, String def) {
|
---|
| 241 | if (prefs.containsKey(key)) {
|
---|
| 242 | if (prefs.get(key) instanceof String)
|
---|
| 243 | return (String) (prefs.get(key));
|
---|
| 244 | }
|
---|
| 245 | if (printNamesNotInMap)
|
---|
| 246 | System.out.println("Settings: Key \"" + key
|
---|
| 247 | + "\" not in Map, defaulting to \"" + def + "\".");
|
---|
| 248 | return def;
|
---|
| 249 | }
|
---|
| 250 |
|
---|
| 251 | /**
|
---|
| 252 | * Get a boolean value
|
---|
| 253 | *
|
---|
| 254 | * @param key
|
---|
| 255 | * Key for value
|
---|
| 256 | * @param def
|
---|
| 257 | * Default return value if key does not exist
|
---|
| 258 | * @return Value
|
---|
| 259 | */
|
---|
| 260 | public Boolean get(String key, Boolean def) {
|
---|
| 261 | if (prefs.containsKey(key)) {
|
---|
| 262 | if (prefs.get(key) instanceof Boolean)
|
---|
| 263 | return (Boolean) (prefs.get(key));
|
---|
| 264 | }
|
---|
| 265 | if (printNamesNotInMap)
|
---|
| 266 | System.out.println("Settings: Key \"" + key
|
---|
| 267 | + "\" not in Map, defaulting to \"" + def + "\".");
|
---|
| 268 | return def;
|
---|
| 269 | }
|
---|
| 270 |
|
---|
| 271 | /**
|
---|
| 272 | * Get a int value
|
---|
| 273 | *
|
---|
| 274 | * @param key
|
---|
| 275 | * Key for value
|
---|
| 276 | * @param def
|
---|
| 277 | * Default return value if key does not exist
|
---|
| 278 | * @return Value
|
---|
| 279 | */
|
---|
| 280 | public int get(String key, int def) {
|
---|
| 281 | if (prefs.containsKey(key)) {
|
---|
| 282 | if (prefs.get(key) instanceof Integer)
|
---|
| 283 | return (Integer) (prefs.get(key));
|
---|
| 284 | }
|
---|
| 285 | if (printNamesNotInMap)
|
---|
| 286 | System.out.println("Settings: Key \"" + key
|
---|
| 287 | + "\" not in Map, defaulting to \"" + def + "\".");
|
---|
| 288 | return def;
|
---|
| 289 | }
|
---|
| 290 |
|
---|
| 291 | /**
|
---|
| 292 | * Remove a value
|
---|
| 293 | *
|
---|
| 294 | * @param key
|
---|
| 295 | * Key to value to remove
|
---|
| 296 | */
|
---|
| 297 | public void removeValue(String key) {
|
---|
| 298 | prefs.remove(key);
|
---|
| 299 | }
|
---|
| 300 |
|
---|
| 301 | }
|
---|