3 Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include "irrlichttypes_bloated.h"
23 #include "util/string.h"
33 extern Settings *g_settings;
34 extern std::string g_settings_path;
36 // Type for a settings changed callback function
37 typedef void (*SettingsChangedCallback)(const std::string &name, void *data);
41 SettingsChangedCallback,
44 > SettingsCallbackList;
46 typedef std::unordered_map<std::string, SettingsCallbackList> SettingsCallbackMap;
50 VALUETYPE_FLAG // Doesn't take any arguments
53 enum SettingsParseEvent {
64 ValueSpec(ValueType a_type, const char *a_help=NULL)
74 struct SettingsEntry {
75 SettingsEntry() = default;
77 SettingsEntry(const std::string &value_) :
81 SettingsEntry(Settings *group_) :
86 std::string value = "";
87 Settings *group = nullptr;
88 bool is_group = false;
91 typedef std::unordered_map<std::string, SettingsEntry> SettingEntries;
99 Settings & operator += (const Settings &other);
100 Settings & operator = (const Settings &other);
102 /***********************
103 * Reading and writing *
104 ***********************/
106 // Read configuration file. Returns success.
107 bool readConfigFile(const char *filename);
108 //Updates configuration file. Returns success.
109 bool updateConfigFile(const char *filename);
110 // NOTE: Types of allowed_options are ignored. Returns success.
111 bool parseCommandLine(int argc, char *argv[],
112 std::map<std::string, ValueSpec> &allowed_options);
113 bool parseConfigLines(std::istream &is, const std::string &end = "");
114 void writeLines(std::ostream &os, u32 tab_depth=0) const;
120 Settings *getGroup(const std::string &name) const;
121 const std::string &get(const std::string &name) const;
122 const std::string &getDefault(const std::string &name) const;
123 bool getBool(const std::string &name) const;
124 u16 getU16(const std::string &name) const;
125 s16 getS16(const std::string &name) const;
126 u32 getU32(const std::string &name) const;
127 s32 getS32(const std::string &name) const;
128 u64 getU64(const std::string &name) const;
129 float getFloat(const std::string &name) const;
130 v2f getV2F(const std::string &name) const;
131 v3f getV3F(const std::string &name) const;
132 u32 getFlagStr(const std::string &name, const FlagDesc *flagdesc,
133 u32 *flagmask) const;
134 bool getNoiseParams(const std::string &name, NoiseParams &np) const;
135 bool getNoiseParamsFromValue(const std::string &name, NoiseParams &np) const;
136 bool getNoiseParamsFromGroup(const std::string &name, NoiseParams &np) const;
138 // return all keys used
139 std::vector<std::string> getNames() const;
140 bool exists(const std::string &name) const;
143 /***************************************
144 * Getters that don't throw exceptions *
145 ***************************************/
147 bool getGroupNoEx(const std::string &name, Settings *&val) const;
148 bool getNoEx(const std::string &name, std::string &val) const;
149 bool getDefaultNoEx(const std::string &name, std::string &val) const;
150 bool getFlag(const std::string &name) const;
151 bool getU16NoEx(const std::string &name, u16 &val) const;
152 bool getS16NoEx(const std::string &name, s16 &val) const;
153 bool getS32NoEx(const std::string &name, s32 &val) const;
154 bool getU64NoEx(const std::string &name, u64 &val) const;
155 bool getFloatNoEx(const std::string &name, float &val) const;
156 bool getV2FNoEx(const std::string &name, v2f &val) const;
157 bool getV3FNoEx(const std::string &name, v3f &val) const;
159 // Like other getters, but handling each flag individualy:
160 // 1) Read default flags (or 0)
161 // 2) Override using user-defined flags
162 bool getFlagStrNoEx(const std::string &name, u32 &val,
163 const FlagDesc *flagdesc) const;
170 // N.B. Groups not allocated with new must be set to NULL in the settings
171 // tree before object destruction.
172 bool setEntry(const std::string &name, const void *entry,
173 bool set_group, bool set_default);
174 bool set(const std::string &name, const std::string &value);
175 bool setDefault(const std::string &name, const std::string &value);
176 bool setGroup(const std::string &name, const Settings &group);
177 bool setGroupDefault(const std::string &name, const Settings &group);
178 bool setBool(const std::string &name, bool value);
179 bool setS16(const std::string &name, s16 value);
180 bool setU16(const std::string &name, u16 value);
181 bool setS32(const std::string &name, s32 value);
182 bool setU64(const std::string &name, u64 value);
183 bool setFloat(const std::string &name, float value);
184 bool setV2F(const std::string &name, v2f value);
185 bool setV3F(const std::string &name, v3f value);
186 bool setFlagStr(const std::string &name, u32 flags,
187 const FlagDesc *flagdesc = nullptr, u32 flagmask = U32_MAX);
188 bool setNoiseParams(const std::string &name, const NoiseParams &np,
189 bool set_default=false);
192 bool remove(const std::string &name);
194 void clearDefaults();
200 void setDefault(const std::string &name, const FlagDesc *flagdesc, u32 flags);
201 // Takes the provided setting values and uses them as new defaults
202 void overrideDefaults(Settings *other);
203 const FlagDesc *getFlagDescFallback(const std::string &name) const;
205 void registerChangedCallback(const std::string &name,
206 SettingsChangedCallback cbf, void *userdata = NULL);
207 void deregisterChangedCallback(const std::string &name,
208 SettingsChangedCallback cbf, void *userdata = NULL);
210 void removeSecureSettings();
213 /***********************
214 * Reading and writing *
215 ***********************/
217 SettingsParseEvent parseConfigObject(const std::string &line,
218 const std::string &end, std::string &name, std::string &value);
219 bool updateConfigObject(std::istream &is, std::ostream &os,
220 const std::string &end, u32 tab_depth=0);
222 static bool checkNameValid(const std::string &name);
223 static bool checkValueValid(const std::string &value);
224 static std::string getMultiline(std::istream &is, size_t *num_lines=NULL);
225 static void printEntry(std::ostream &os, const std::string &name,
226 const SettingsEntry &entry, u32 tab_depth=0);
232 const SettingsEntry &getEntry(const std::string &name) const;
233 const SettingsEntry &getEntryDefault(const std::string &name) const;
235 // Allow TestSettings to run sanity checks using private functions.
236 friend class TestSettings;
238 void updateNoLock(const Settings &other);
240 void clearDefaultsNoLock();
242 void doCallbacks(const std::string &name) const;
244 SettingEntries m_settings;
245 SettingEntries m_defaults;
246 std::unordered_map<std::string, const FlagDesc *> m_flags;
248 SettingsCallbackMap m_callbacks;
250 mutable std::mutex m_callback_mutex;
252 // All methods that access m_settings/m_defaults directly should lock this.
253 mutable std::mutex m_mutex;