001package com.fs.starfarer.api.impl;
002
003import java.io.IOException;
004import java.util.ArrayList;
005import java.util.Collections;
006import java.util.HashMap;
007import java.util.LinkedHashSet;
008import java.util.List;
009import java.util.Map;
010import java.util.Set;
011
012import org.json.JSONArray;
013import org.json.JSONException;
014import org.json.JSONObject;
015
016import com.fs.starfarer.api.Global;
017
018/**
019 * Base class. Extending classes are for relatively small amounts of data meant to be shared across
020 * different saves.
021 * 
022 * The amount of data stored here should be small and bounded (i.e. not growing indefinitely).
023 * 
024 * Code using this should assume the data stored here may be lost (e.g. due to the common folder being deleted)
025 * and handle this/recover gracefully.
026 * 
027 * @author Alex
028 *
029 */
030public abstract class BaseSharedJSONFile {
031        protected abstract String getFilename();
032        
033        protected JSONObject json = new JSONObject();
034        
035        public void loadIfNeeded() {
036                try {
037                        if (Global.getSettings().fileExistsInCommon(getFilename())) {
038                                json = Global.getSettings().readJSONFromCommon(getFilename(), true);
039                        }
040                } catch (IOException e) {
041                        Global.getLogger(getClass()).warn(e.getMessage(), e);
042                } catch (JSONException e) {
043                        Global.getLogger(getClass()).warn(e.getMessage(), e);
044                }
045        }
046        
047        public void saveIfNeeded() {
048//              if (true) {
049//                      return;
050//              }
051                try {
052                        Global.getSettings().writeJSONToCommon(getFilename(), json, true);
053                } catch (JSONException e) {
054                        Global.getLogger(getClass()).warn(e.getMessage(), e);
055                } catch (IOException e) {
056                        Global.getLogger(getClass()).warn(e.getMessage(), e);
057                }
058        }
059        
060        protected Map<String, Set<String>> setCache = new HashMap<>();
061        
062        public Set<String> getSet(String key) {
063                if (setCache.containsKey(key)) return setCache.get(key);
064                
065                JSONArray arr = json.optJSONArray(key);
066                Set<String> set = new LinkedHashSet<>();
067                if (arr != null) {
068                        for (int i = 0; i < arr.length(); i++) {
069                                String curr = arr.optString(i);
070                                if (curr != null) {
071                                        set.add(curr);
072                                }
073                        }
074                }
075                setCache.put(key, set);
076                return set;
077        }
078
079        public boolean doesSetContain(String key, String value) {
080                Set<String> set = getSet(key);
081                return set.contains(value);
082        }
083        
084        public boolean addToSet(String key, String value) {
085                Set<String> set = getSet(key);
086                if (set.contains(value)) {
087                        return false;
088                }
089                
090                set.add(value);
091                
092                setCache.put(key, set);
093                
094                JSONArray arr = new JSONArray();
095                List<String> list = new ArrayList<>();
096                list.addAll(set);
097                Collections.sort(list);
098                
099                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