]> git.lizzy.rs Git - dragonfireclient.git/blob - src/settings.h
Fix #5617 - respect message and reconnect parameters when shutting down immediately...
[dragonfireclient.git] / src / settings.h
1 /*
2 Minetest
3 Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
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.
9
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.
14
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.
18 */
19
20 #ifndef SETTINGS_HEADER
21 #define SETTINGS_HEADER
22
23 #include "irrlichttypes_bloated.h"
24 #include "util/string.h"
25 #include "threading/mutex.h"
26 #include <string>
27 #include "util/cpp11_container.h"
28 #include <list>
29 #include <set>
30
31 class Settings;
32 struct NoiseParams;
33
34 // Global objects
35 extern Settings *g_settings;
36 extern std::string g_settings_path;
37
38 // Type for a settings changed callback function
39 typedef void (*SettingsChangedCallback)(const std::string &name, void *data);
40
41 typedef std::vector<
42         std::pair<
43                 SettingsChangedCallback,
44                 void *
45         >
46 > SettingsCallbackList;
47
48 typedef UNORDERED_MAP<std::string, SettingsCallbackList> SettingsCallbackMap;
49
50 enum ValueType {
51         VALUETYPE_STRING,
52         VALUETYPE_FLAG // Doesn't take any arguments
53 };
54
55 enum SettingsParseEvent {
56         SPE_NONE,
57         SPE_INVALID,
58         SPE_COMMENT,
59         SPE_KVPAIR,
60         SPE_END,
61         SPE_GROUP,
62         SPE_MULTILINE,
63 };
64
65 struct ValueSpec {
66         ValueSpec(ValueType a_type, const char *a_help=NULL)
67         {
68                 type = a_type;
69                 help = a_help;
70         }
71
72         ValueType type;
73         const char *help;
74 };
75
76 struct SettingsEntry {
77         SettingsEntry() :
78                 group(NULL),
79                 is_group(false)
80         {}
81
82         SettingsEntry(const std::string &value_) :
83                 value(value_),
84                 group(NULL),
85                 is_group(false)
86         {}
87
88         SettingsEntry(Settings *group_) :
89                 group(group_),
90                 is_group(true)
91         {}
92
93         std::string value;
94         Settings *group;
95         bool is_group;
96 };
97
98 typedef UNORDERED_MAP<std::string, SettingsEntry> SettingEntries;
99
100 class Settings {
101 public:
102         Settings() {}
103         ~Settings();
104
105         Settings & operator += (const Settings &other);
106         Settings & operator = (const Settings &other);
107
108         /***********************
109          * Reading and writing *
110          ***********************/
111
112         // Read configuration file.  Returns success.
113         bool readConfigFile(const char *filename);
114         //Updates configuration file.  Returns success.
115         bool updateConfigFile(const char *filename);
116         // NOTE: Types of allowed_options are ignored.  Returns success.
117         bool parseCommandLine(int argc, char *argv[],
118                         std::map<std::string, ValueSpec> &allowed_options);
119         bool parseConfigLines(std::istream &is, const std::string &end = "");
120         void writeLines(std::ostream &os, u32 tab_depth=0) const;
121
122         SettingsParseEvent parseConfigObject(const std::string &line,
123                 const std::string &end, std::string &name, std::string &value);
124         bool updateConfigObject(std::istream &is, std::ostream &os,
125                 const std::string &end, u32 tab_depth=0);
126
127         static bool checkNameValid(const std::string &name);
128         static bool checkValueValid(const std::string &value);
129         static std::string getMultiline(std::istream &is, size_t *num_lines=NULL);
130         static void printEntry(std::ostream &os, const std::string &name,
131                 const SettingsEntry &entry, u32 tab_depth=0);
132
133         /***********
134          * Getters *
135          ***********/
136
137         const SettingsEntry &getEntry(const std::string &name) const;
138         Settings *getGroup(const std::string &name) const;
139         const std::string &get(const std::string &name) const;
140         bool getBool(const std::string &name) const;
141         u16 getU16(const std::string &name) const;
142         s16 getS16(const std::string &name) const;
143         s32 getS32(const std::string &name) const;
144         u64 getU64(const std::string &name) const;
145         float getFloat(const std::string &name) const;
146         v2f getV2F(const std::string &name) const;
147         v3f getV3F(const std::string &name) const;
148         u32 getFlagStr(const std::string &name, const FlagDesc *flagdesc,
149                         u32 *flagmask) const;
150         // N.B. if getStruct() is used to read a non-POD aggregate type,
151         // the behavior is undefined.
152         bool getStruct(const std::string &name, const std::string &format,
153                         void *out, size_t olen) const;
154         bool getNoiseParams(const std::string &name, NoiseParams &np) const;
155         bool getNoiseParamsFromValue(const std::string &name, NoiseParams &np) const;
156         bool getNoiseParamsFromGroup(const std::string &name, NoiseParams &np) const;
157
158         // return all keys used
159         std::vector<std::string> getNames() const;
160         bool exists(const std::string &name) const;
161
162
163         /***************************************
164          * Getters that don't throw exceptions *
165          ***************************************/
166
167         bool getEntryNoEx(const std::string &name, SettingsEntry &val) const;
168         bool getGroupNoEx(const std::string &name, Settings *&val) const;
169         bool getNoEx(const std::string &name, std::string &val) const;
170         bool getFlag(const std::string &name) const;
171         bool getU16NoEx(const std::string &name, u16 &val) const;
172         bool getS16NoEx(const std::string &name, s16 &val) const;
173         bool getS32NoEx(const std::string &name, s32 &val) const;
174         bool getU64NoEx(const std::string &name, u64 &val) const;
175         bool getFloatNoEx(const std::string &name, float &val) const;
176         bool getV2FNoEx(const std::string &name, v2f &val) const;
177         bool getV3FNoEx(const std::string &name, v3f &val) const;
178         // N.B. getFlagStrNoEx() does not set val, but merely modifies it.  Thus,
179         // val must be initialized before using getFlagStrNoEx().  The intention of
180         // this is to simplify modifying a flags field from a default value.
181         bool getFlagStrNoEx(const std::string &name, u32 &val, FlagDesc *flagdesc) const;
182
183
184         /***********
185          * Setters *
186          ***********/
187
188         // N.B. Groups not allocated with new must be set to NULL in the settings
189         // tree before object destruction.
190         bool setEntry(const std::string &name, const void *entry,
191                 bool set_group, bool set_default);
192         bool set(const std::string &name, const std::string &value);
193         bool setDefault(const std::string &name, const std::string &value);
194         bool setGroup(const std::string &name, Settings *group);
195         bool setGroupDefault(const std::string &name, Settings *group);
196         bool setBool(const std::string &name, bool value);
197         bool setS16(const std::string &name, s16 value);
198         bool setU16(const std::string &name, u16 value);
199         bool setS32(const std::string &name, s32 value);
200         bool setU64(const std::string &name, u64 value);
201         bool setFloat(const std::string &name, float value);
202         bool setV2F(const std::string &name, v2f value);
203         bool setV3F(const std::string &name, v3f value);
204         bool setFlagStr(const std::string &name, u32 flags,
205                 const FlagDesc *flagdesc, u32 flagmask);
206         bool setNoiseParams(const std::string &name, const NoiseParams &np,
207                 bool set_default=false);
208         // N.B. if setStruct() is used to write a non-POD aggregate type,
209         // the behavior is undefined.
210         bool setStruct(const std::string &name, const std::string &format, void *value);
211
212         // remove a setting
213         bool remove(const std::string &name);
214         void clear();
215         void clearDefaults();
216         void updateValue(const Settings &other, const std::string &name);
217         void update(const Settings &other);
218
219         void registerChangedCallback(const std::string &name,
220                 SettingsChangedCallback cbf, void *userdata = NULL);
221         void deregisterChangedCallback(const std::string &name,
222                 SettingsChangedCallback cbf, void *userdata = NULL);
223
224 private:
225         void updateNoLock(const Settings &other);
226         void clearNoLock();
227         void clearDefaultsNoLock();
228
229         void doCallbacks(const std::string &name) const;
230
231         SettingEntries m_settings;
232         SettingEntries m_defaults;
233
234         SettingsCallbackMap m_callbacks;
235
236         mutable Mutex m_callback_mutex;
237
238         // All methods that access m_settings/m_defaults directly should lock this.
239         mutable Mutex m_mutex;
240
241 };
242
243 #endif
244