Starsector API
Loading...
Searching...
No Matches
SharedSettings.java
Go to the documentation of this file.
1package com.fs.starfarer.api.impl;
2
3import java.io.IOException;
4
5import org.json.JSONException;
6import org.json.JSONObject;
7
8import com.fs.starfarer.api.Global;
9
24public class SharedSettings {
25 public static String SETTINGS_DATA_FILE = "core_shared_settings.json";
26
27 protected static JSONObject json = new JSONObject();
28
29 static {
31
32 // not needed if saveIfNeeded() is called after making changes
33// Runtime.getRuntime().addShutdownHook(new Thread() {
34// public void run() {
35// // remotely possible for this to fail (if it takes >15s, somehow?)
36// // in which case it'll just start fresh on next load
37// saveIfNeeded();
38// }
39// });
40 }
41
42 public static void loadIfNeeded() {
43 try {
46 }
47 } catch (IOException e) {
48 Global.getLogger(SharedSettings.class).warn(e.getMessage(), e);
49 } catch (JSONException e) {
50 Global.getLogger(SharedSettings.class).warn(e.getMessage(), e);
51 }
52 }
53
54 public static void saveIfNeeded() {
55 try {
57 } catch (JSONException e) {
58 Global.getLogger(SharedSettings.class).warn(e.getMessage(), e);
59 } catch (IOException e) {
60 Global.getLogger(SharedSettings.class).warn(e.getMessage(), e);
61 }
62 }
63
64 public static JSONObject get() {
65 return json;
66 }
67
68
69 public static boolean optBoolean(String key, boolean defaultValue) {
70 return json.optBoolean(key, defaultValue);
71 }
72 public static void setBoolean(String key, boolean value) {
73 try {
74 json.put(key, value);
75 } catch (JSONException e) {
76 Global.getLogger(SharedSettings.class).warn(e.getMessage(), e);
77 }
78 }
79
80 public static float optFloat(String key, float defaultValue) {
81 return (float) json.optDouble(key, defaultValue);
82 }
83 public static void setFloat(String key, float value) {
84 try {
85 json.put(key, value);
86 } catch (JSONException e) {
87 Global.getLogger(SharedSettings.class).warn(e.getMessage(), e);
88 }
89 }
90
91 public static int optInt(String key, int defaultValue) {
92 return json.optInt(key, defaultValue);
93 }
94 public static void setInt(String key, int value) {
95 try {
96 json.put(key, value);
97 } catch (JSONException e) {
98 Global.getLogger(SharedSettings.class).warn(e.getMessage(), e);
99 }
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
static SettingsAPI getSettings()
Definition Global.java:57
static Logger getLogger(Class c)
Definition Global.java:32
static void setString(String key, String value)
static String optString(String key, String defaultValue)
static void setBoolean(String key, boolean value)
static int optInt(String key, int defaultValue)
static void setInt(String key, int value)
static void setFloat(String key, float value)
static float optFloat(String key, float defaultValue)
static boolean optBoolean(String key, boolean defaultValue)
boolean fileExistsInCommon(String filename)
void writeJSONToCommon(String filename, JSONObject json, boolean onlyIfChanged)
JSONObject readJSONFromCommon(String filename, boolean putInWriteCache)