Starsector API
Loading...
Searching...
No Matches
BaseSharedJSONFile.java
Go to the documentation of this file.
1package com.fs.starfarer.api.impl;
2
3import java.io.IOException;
4import java.util.ArrayList;
5import java.util.Collections;
6import java.util.HashMap;
7import java.util.LinkedHashSet;
8import java.util.List;
9import java.util.Map;
10import java.util.Set;
11
12import org.json.JSONArray;
13import org.json.JSONException;
14import org.json.JSONObject;
15
16import com.fs.starfarer.api.Global;
17
30public abstract class BaseSharedJSONFile {
31 protected abstract String getFilename();
32
33 protected JSONObject json = new JSONObject();
34
35 public void loadIfNeeded() {
36 try {
39 }
40 } catch (IOException e) {
41 Global.getLogger(getClass()).warn(e.getMessage(), e);
42 } catch (JSONException e) {
43 Global.getLogger(getClass()).warn(e.getMessage(), e);
44 }
45 }
46
47 public void saveIfNeeded() {
48// if (true) {
49// return;
50// }
51 try {
53 } catch (JSONException e) {
54 Global.getLogger(getClass()).warn(e.getMessage(), e);
55 } catch (IOException e) {
56 Global.getLogger(getClass()).warn(e.getMessage(), e);
57 }
58 }
59
60 protected Map<String, Set<String>> setCache = new HashMap<>();
61
62 public Set<String> getSet(String key) {
63 if (setCache.containsKey(key)) return setCache.get(key);
64
65 JSONArray arr = json.optJSONArray(key);
66 Set<String> set = new LinkedHashSet<>();
67 if (arr != null) {
68 for (int i = 0; i < arr.length(); i++) {
69 String curr = arr.optString(i);
70 if (curr != null) {
71 set.add(curr);
72 }
73 }
74 }
75 setCache.put(key, set);
76 return set;
77 }
78
79 public boolean doesSetContain(String key, String value) {
80 Set<String> set = getSet(key);
81 return set.contains(value);
82 }
83
84 public boolean addToSet(String key, String value) {
85 Set<String> set = getSet(key);
86 if (set.contains(value)) {
87 return false;
88 }
89
90 set.add(value);
91
92 setCache.put(key, set);
93
94 JSONArray arr = new JSONArray();
95 List<String> list = new ArrayList<>();
96 list.addAll(set);
97 Collections.sort(list);
98
99 for (String curr : list) {
100 arr.put(curr);
101 }
102
103 try {
104 json.put(key, arr);
105 } catch (JSONException e) {
106 Global.getLogger(getClass()).warn(e.getMessage(), e);
107 }
108
109 return true;
110 }
111
112 public boolean optBoolean(String key, boolean defaultValue) {
113 return json.optBoolean(key, defaultValue);
114 }
115 public void setBoolean(String key, boolean value) {
116 try {
117 json.put(key, value);
118 } catch (JSONException e) {
119 Global.getLogger(getClass()).warn(e.getMessage(), e);
120 }
121 }
122
123 public float optFloat(String key, float defaultValue) {
124 return (float) json.optDouble(key, defaultValue);
125 }
126 public void setFloat(String key, float value) {
127 try {
128 json.put(key, value);
129 } catch (JSONException e) {
130 Global.getLogger(getClass()).warn(e.getMessage(), e);
131 }
132 }
133
134 public int optInt(String key, int defaultValue) {
135 return json.optInt(key, defaultValue);
136 }
137 public void setInt(String key, int value) {
138 try {
139 json.put(key, value);
140 } catch (JSONException e) {
141 Global.getLogger(getClass()).warn(e.getMessage(), e);
142 }
143 }
144
145 public String optString(String key, String defaultValue) {
146 return json.optString(key, defaultValue);
147 }
148 public void setString(String key, String value) {
149 try {
150 json.put(key, value);
151 } catch (JSONException e) {
152 Global.getLogger(getClass()).warn(e.getMessage(), e);
153 }
154 }
155
156 public void unset(String key) {
157 json.remove(key);
158 }
159}
160
161
162
163
164
165
166
static SettingsAPI getSettings()
Definition Global.java:57
static Logger getLogger(Class c)
Definition Global.java:32
float optFloat(String key, float defaultValue)
String optString(String key, String defaultValue)
boolean optBoolean(String key, boolean defaultValue)
boolean addToSet(String key, String value)
boolean doesSetContain(String key, String value)
boolean fileExistsInCommon(String filename)
void writeJSONToCommon(String filename, JSONObject json, boolean onlyIfChanged)
JSONObject readJSONFromCommon(String filename, boolean putInWriteCache)