001package com.fs.starfarer.api.impl;
002
003import java.io.IOException;
004
005import org.json.JSONException;
006import org.json.JSONObject;
007
008import com.fs.starfarer.api.Global;
009
010/**
011 * Generally meant for UI settings that are shared across multiple campaigns.
012 * 
013 * "Was this checkbox checked last time this dialog was closed", etc. In some cases that should be tracked
014 * per-campaign, but in some cases it makes sense for this to be shared.
015 * 
016 * The amount of data stored here should be small and bounded (i.e. not growing indefinitely).
017 * 
018 * Code using this should assume the data stored here may be lost (e.g. due to the common folder being deleted)
019 * and handle this/recover gracefully.
020 * 
021 * @author Alex
022 *
023 */
024public class SharedSettings {
025        public static String SETTINGS_DATA_FILE = "core_shared_settings.json";
026        
027        protected static JSONObject json = new JSONObject();
028        
029        static {
030                loadIfNeeded();
031                
032                // not needed if saveIfNeeded() is called after making changes
033//              Runtime.getRuntime().addShutdownHook(new Thread() {
034//                      public void run() {
035//                              // remotely possible for this to fail (if it takes >15s, somehow?)
036//                              // in which case it'll just start fresh on next load
037//                              saveIfNeeded();
038//                      }
039//              });
040        }
041        
042        public static void loadIfNeeded() {
043                try {
044                        if (Global.getSettings().fileExistsInCommon(SETTINGS_DATA_FILE)) {
045                                json = Global.getSettings().readJSONFromCommon(SETTINGS_DATA_FILE, true);
046                        }
047                } catch (IOException e) {
048                        Global.getLogger(SharedSettings.class).warn(e.getMessage(), e);
049                } catch (JSONException e) {
050                        Global.getLogger(SharedSettings.class).warn(e.getMessage(), e);
051                }
052        }
053        
054        public static void saveIfNeeded() {
055                try {
056                        Global.getSettings().writeJSONToCommon(SETTINGS_DATA_FILE, json, true);
057                } catch (JSONException e) {
058                        Global.getLogger(SharedSettings.class).warn(e.getMessage(), e);
059                } catch (IOException e) {
060                        Global.getLogger(SharedSettings.class).warn(e.getMessage(), e);
061                }
062        }
063
064        public static JSONObject get() {
065                return json;
066        }
067        
068        
069        public static boolean optBoolean(String key, boolean defaultValue) {
070                return json.optBoolean(key, defaultValue);
071        }
072        public static void setBoolean(String key, boolean value) {
073                try {
074                        json.put(key, value);
075                } catch (JSONException e) {
076                        Global.getLogger(SharedSettings.class).warn(e.getMessage(), e);
077                }
078        }
079
080        public static float optFloat(String key, float defaultValue) {
081                return (float) json.optDouble(key, defaultValue);
082        }
083        public static void setFloat(String key, float value) {
084                try {
085                        json.put(key, value);
086                } catch (JSONException e) {
087                        Global.getLogger(SharedSettings.class).warn(e.getMessage(), e);
088                }
089        }
090        
091        public static int optInt(String key, int defaultValue) {
092                return json.optInt(key, defaultValue);
093        }
094        public static void setInt(String key, int value) {
095                try {
096                        json.put(key, value);
097                } catch (JSONException e) {
098                        Global.getLogger(SharedSettings.class).warn(e.getMessage(), e);
099                }
100        }
101        
102        public static String optString(String key, String defaultValue) {
103                return json.optString(key, defaultValue);
104        }
105        public static void setString(String key, String value) {
106                try {
107                        json.put(key, value);
108                } catch (JSONException e) {
109                        Global.getLogger(SharedSettings.class).warn(e.getMessage(), e);
110                }
111        }
112        
113        public static void unset(String key) {
114                json.remove(key);
115        }
116}
117
118
119
120
121
122
123