Luanti 5.10.0-dev
 
Loading...
Searching...
No Matches
settings.h
Go to the documentation of this file.
1// Luanti
2// SPDX-License-Identifier: LGPL-2.1-or-later
3// Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5#pragma once
6
8#include "util/string.h"
9#include "util/basic_macros.h"
10#include <string>
11#include <set>
12#include <map>
13#include <mutex>
14
15class Settings;
16struct NoiseParams;
17
18// Global objects
19extern Settings *g_settings; // Same as Settings::getLayer(SL_GLOBAL);
20extern std::string g_settings_path;
21
22// Type for a settings changed callback function
23typedef void (*SettingsChangedCallback)(const std::string &name, void *data);
24
25typedef std::vector<
26 std::pair<
28 void *
29 >
31
32typedef std::unordered_map<std::string, SettingsCallbackList> SettingsCallbackMap;
33
36 VALUETYPE_FLAG // Doesn't take any arguments
37};
38
48
49// Describes the global setting layers, SL_GLOBAL is where settings are read from
56
57// Implements the hierarchy a settings object may be part of
59public:
60 /*
61 * A settings object that may be part of another hierarchy can
62 * occupy the index 0 as a fallback. If not set you can use 0 on your own.
63 */
64 SettingsHierarchy(Settings *fallback = nullptr);
65
67
68 Settings *getLayer(int layer) const;
69
70private:
71 friend class Settings;
72 Settings *getParent(int layer) const;
73 void onLayerCreated(int layer, Settings *obj);
74 void onLayerRemoved(int layer);
75
76 std::vector<Settings*> layers;
77};
78
79struct ValueSpec {
80 ValueSpec(ValueType a_type, const char *a_help=NULL)
81 {
82 type = a_type;
83 help = a_help;
84 }
85
87 const char *help;
88};
89
91 SettingsEntry() = default;
92
93 SettingsEntry(const std::string &value_) :
94 value(value_)
95 {}
96
98 group(group_),
99 is_group(true)
100 {}
101
102 std::string value = "";
103 Settings *group = nullptr;
104 bool is_group = false;
105};
106
107typedef std::unordered_map<std::string, SettingsEntry> SettingEntries;
108
109class Settings {
110public:
111 /* These functions operate on the global hierarchy! */
112 static Settings *createLayer(SettingsLayer sl, std::string_view end_tag = "");
113 static Settings *getLayer(SettingsLayer sl);
114
115
116 Settings(std::string_view end_tag = "") :
117 m_end_tag(end_tag)
118 {}
119 Settings(std::string_view end_tag, SettingsHierarchy *h, int settings_layer);
120 ~Settings();
121
122 Settings & operator=(const Settings &other);
123
124 /***********************
125 * Reading and writing *
126 ***********************/
127
128 // Read configuration file. Returns success.
129 bool readConfigFile(const char *filename);
130 //Updates configuration file. Returns success.
131 bool updateConfigFile(const char *filename);
132 // NOTE: Types of allowed_options are ignored. Returns success.
133 bool parseCommandLine(int argc, char *argv[],
134 const std::map<std::string, ValueSpec> &allowed_options);
135 bool parseConfigLines(std::istream &is);
136 void writeLines(std::ostream &os, u32 tab_depth=0) const;
137
138 /***********
139 * Getters *
140 ***********/
141
142 Settings *getGroup(const std::string &name) const;
143 const std::string &get(const std::string &name) const;
144 bool getBool(const std::string &name) const;
145 u16 getU16(const std::string &name) const;
146 s16 getS16(const std::string &name) const;
147 u32 getU32(const std::string &name) const;
148 s32 getS32(const std::string &name) const;
149 u64 getU64(const std::string &name) const;
150 float getFloat(const std::string &name) const;
151 float getFloat(const std::string &name, float min, float max) const;
152 v2f getV2F(const std::string &name) const;
153 v3f getV3F(const std::string &name) const;
154 u32 getFlagStr(const std::string &name, const FlagDesc *flagdesc,
155 u32 *flagmask) const;
156 bool getNoiseParams(const std::string &name, NoiseParams &np) const;
157 bool getNoiseParamsFromValue(const std::string &name, NoiseParams &np) const;
158 bool getNoiseParamsFromGroup(const std::string &name, NoiseParams &np) const;
159
160 // return all keys used in this object
161 std::vector<std::string> getNames() const;
162 // check if setting exists anywhere in the hierarchy
163 bool exists(const std::string &name) const;
164 // check if setting exists in this object ("locally")
165 bool existsLocal(const std::string &name) const;
166
167
168 /***************************************
169 * Getters that don't throw exceptions *
170 ***************************************/
171
172 bool getGroupNoEx(const std::string &name, Settings *&val) const;
173 bool getNoEx(const std::string &name, std::string &val) const;
174 bool getFlag(const std::string &name) const;
175 bool getU16NoEx(const std::string &name, u16 &val) const;
176 bool getS16NoEx(const std::string &name, s16 &val) const;
177 bool getU32NoEx(const std::string &name, u32 &val) const;
178 bool getS32NoEx(const std::string &name, s32 &val) const;
179 bool getU64NoEx(const std::string &name, u64 &val) const;
180 bool getFloatNoEx(const std::string &name, float &val) const;
181 bool getV2FNoEx(const std::string &name, v2f &val) const;
182 bool getV3FNoEx(const std::string &name, v3f &val) const;
183
184 // Like other getters, but handling each flag individualy:
185 // 1) Read default flags (or 0)
186 // 2) Override using user-defined flags
187 bool getFlagStrNoEx(const std::string &name, u32 &val,
188 const FlagDesc *flagdesc) const;
189
190
191 /***********
192 * Setters *
193 ***********/
194
195 // N.B. Groups not allocated with new must be set to NULL in the settings
196 // tree before object destruction.
197 bool setEntry(const std::string &name, const void *entry,
198 bool set_group);
199 bool set(const std::string &name, const std::string &value);
200 bool setDefault(const std::string &name, const std::string &value);
201 bool setGroup(const std::string &name, const Settings &group);
202 bool setBool(const std::string &name, bool value);
203 bool setS16(const std::string &name, s16 value);
204 bool setU16(const std::string &name, u16 value);
205 bool setS32(const std::string &name, s32 value);
206 bool setU64(const std::string &name, u64 value);
207 bool setFloat(const std::string &name, float value);
208 bool setV2F(const std::string &name, v2f value);
209 bool setV3F(const std::string &name, v3f value);
210 bool setFlagStr(const std::string &name, u32 flags,
211 const FlagDesc *flagdesc = nullptr, u32 flagmask = U32_MAX);
212 bool setNoiseParams(const std::string &name, const NoiseParams &np);
213
214 // remove a setting
215 bool remove(const std::string &name);
216
217 /*****************
218 * Miscellaneous *
219 *****************/
220
221 void setDefault(const std::string &name, const FlagDesc *flagdesc, u32 flags);
222 const FlagDesc *getFlagDescFallback(const std::string &name) const;
223
224 void registerChangedCallback(const std::string &name,
225 SettingsChangedCallback cbf, void *userdata = NULL);
226 size_t deregisterAllChangedCallbacks(const void *userdata);
227
229
230 // Returns the settings layer this object is.
231 // If within the global hierarchy you can cast this to enum SettingsLayer
232 inline int getLayer() const { return m_settingslayer; }
233
234private:
235 /***********************
236 * Reading and writing *
237 ***********************/
238
239 SettingsParseEvent parseConfigObject(const std::string &line,
240 std::string &name, std::string &value);
241 bool updateConfigObject(std::istream &is, std::ostream &os,
242 u32 tab_depth=0);
243
244 static bool checkNameValid(std::string_view name);
245 static bool checkValueValid(std::string_view value);
246 static std::string getMultiline(std::istream &is, size_t *num_lines=NULL);
247 static void printEntry(std::ostream &os, const std::string &name,
248 const SettingsEntry &entry, u32 tab_depth=0);
249
250 /***********
251 * Getters *
252 ***********/
253 Settings *getParent() const;
254
255 const SettingsEntry &getEntry(const std::string &name) const;
256
257 // Allow TestSettings to run sanity checks using private functions.
258 friend class TestSettings;
259 // For sane mutex locking when iterating
260 friend class LuaSettings;
261
262 void clearNoLock();
263
264 void doCallbacks(const std::string &name) const;
265
268 std::string m_end_tag;
269
270 mutable std::mutex m_callback_mutex;
271
272 // All methods that access m_settings/m_defaults directly should lock this.
273 mutable std::mutex m_mutex;
274
277
278 static std::unordered_map<std::string, const FlagDesc *> s_flags;
279};
#define DISABLE_CLASS_COPY(C)
Definition basic_macros.h:26
Definition l_settings.h:13
Definition settings.h:58
Settings * getParent(int layer) const
Definition settings.cpp:44
SettingsHierarchy(Settings *fallback=nullptr)
Definition settings.cpp:30
std::vector< Settings * > layers
Definition settings.h:76
void onLayerCreated(int layer, Settings *obj)
Definition settings.cpp:57
Settings * getLayer(int layer) const
Definition settings.cpp:36
void onLayerRemoved(int layer)
Definition settings.cpp:75
Definition settings.h:109
v2f getV2F(const std::string &name) const
Definition settings.cpp:533
static bool checkValueValid(std::string_view value)
Definition settings.cpp:154
bool setGroup(const std::string &name, const Settings &group)
Definition settings.cpp:866
v3f getV3F(const std::string &name) const
Definition settings.cpp:544
SettingsCallbackMap m_callbacks
Definition settings.h:267
std::mutex m_callback_mutex
Definition settings.h:270
bool getFloatNoEx(const std::string &name, float &val) const
Definition settings.cpp:711
bool getFlag(const std::string &name) const
Definition settings.cpp:701
bool getU32NoEx(const std::string &name, u32 &val) const
Definition settings.cpp:743
bool getV3FNoEx(const std::string &name, v3f &val) const
Definition settings.cpp:786
void doCallbacks(const std::string &name) const
Definition settings.cpp:1067
bool setS32(const std::string &name, s32 value)
Definition settings.cpp:894
u64 getU64(const std::string &name) const
Definition settings.cpp:526
static std::unordered_map< std::string, const FlagDesc * > s_flags
Definition settings.h:278
bool getU64NoEx(const std::string &name, u64 &val) const
Definition settings.cpp:764
~Settings()
Definition settings.cpp:108
bool readConfigFile(const char *filename)
Definition settings.cpp:191
bool getS32NoEx(const std::string &name, s32 &val) const
Definition settings.cpp:753
bool setU16(const std::string &name, u16 value)
Definition settings.cpp:888
bool remove(const std::string &name)
Definition settings.cpp:959
int m_settingslayer
Definition settings.h:276
Settings * getGroup(const std::string &name) const
Definition settings.cpp:466
bool getS16NoEx(const std::string &name, s16 &val) const
Definition settings.cpp:733
bool getNoiseParamsFromValue(const std::string &name, NoiseParams &np) const
Definition settings.cpp:589
bool getGroupNoEx(const std::string &name, Settings *&val) const
Definition settings.cpp:679
int getLayer() const
Definition settings.h:232
u32 getU32(const std::string &name) const
Definition settings.cpp:502
s32 getS32(const std::string &name) const
Definition settings.cpp:507
bool getNoEx(const std::string &name, std::string &val) const
Definition settings.cpp:690
void writeLines(std::ostream &os, u32 tab_depth=0) const
Definition settings.cpp:241
void registerChangedCallback(const std::string &name, SettingsChangedCallback cbf, void *userdata=NULL)
Definition settings.cpp:1029
bool setDefault(const std::string &name, const std::string &value)
Definition settings.cpp:858
std::vector< std::string > getNames() const
Definition settings.cpp:661
const SettingsEntry & getEntry(const std::string &name) const
Definition settings.cpp:449
size_t deregisterAllChangedCallbacks(const void *userdata)
Definition settings.cpp:1036
bool getV2FNoEx(const std::string &name, v2f &val) const
Definition settings.cpp:775
bool updateConfigFile(const char *filename)
Definition settings.cpp:367
const std::string & get(const std::string &name) const
Definition settings.cpp:475
SettingsParseEvent parseConfigObject(const std::string &line, std::string &name, std::string &value)
Definition settings.cpp:979
bool getFlagStrNoEx(const std::string &name, u32 &val, const FlagDesc *flagdesc) const
Definition settings.cpp:797
bool setBool(const std::string &name, bool value)
Definition settings.cpp:876
float getFloat(const std::string &name) const
Definition settings.cpp:513
Settings * getParent() const
Definition settings.cpp:443
bool set(const std::string &name, const std::string &value)
Definition settings.cpp:847
static std::string getMultiline(std::istream &is, size_t *num_lines=NULL)
Definition settings.cpp:165
bool getBool(const std::string &name) const
Definition settings.cpp:484
bool setS16(const std::string &name, s16 value)
Definition settings.cpp:882
Settings(std::string_view end_tag="")
Definition settings.h:116
bool parseConfigLines(std::istream &is)
Definition settings.cpp:201
std::string m_end_tag
Definition settings.h:268
void clearNoLock()
Definition settings.cpp:1007
bool setFloat(const std::string &name, float value)
Definition settings.cpp:908
bool setEntry(const std::string &name, const void *entry, bool set_group)
Definition settings.cpp:819
std::mutex m_mutex
Definition settings.h:273
bool setNoiseParams(const std::string &name, const NoiseParams &np)
Definition settings.cpp:942
static void printEntry(std::ostream &os, const std::string &name, const SettingsEntry &entry, u32 tab_depth=0)
Definition settings.cpp:258
bool exists(const std::string &name) const
Definition settings.cpp:643
Settings & operator=(const Settings &other)
Definition settings.cpp:119
void removeSecureSettings()
Definition settings.cpp:1054
bool getNoiseParams(const std::string &name, NoiseParams &np) const
Definition settings.cpp:578
bool setV3F(const std::string &name, v3f value)
Definition settings.cpp:922
bool existsLocal(const std::string &name) const
Definition settings.cpp:653
u16 getU16(const std::string &name) const
Definition settings.cpp:490
static bool checkNameValid(std::string_view name)
Definition settings.cpp:139
bool setFlagStr(const std::string &name, u32 flags, const FlagDesc *flagdesc=nullptr, u32 flagmask=U32_MAX)
Definition settings.cpp:930
bool setV2F(const std::string &name, v2f value)
Definition settings.cpp:914
bool getU16NoEx(const std::string &name, u16 &val) const
Definition settings.cpp:722
SettingEntries m_settings
Definition settings.h:266
u32 getFlagStr(const std::string &name, const FlagDesc *flagdesc, u32 *flagmask) const
Definition settings.cpp:550
s16 getS16(const std::string &name) const
Definition settings.cpp:496
bool updateConfigObject(std::istream &is, std::ostream &os, u32 tab_depth=0)
Definition settings.cpp:281
SettingsHierarchy * m_hierarchy
Definition settings.h:275
static Settings * createLayer(SettingsLayer sl, std::string_view end_tag="")
Definition settings.cpp:85
bool getNoiseParamsFromGroup(const std::string &name, NoiseParams &np) const
Definition settings.cpp:619
bool setU64(const std::string &name, u64 value)
Definition settings.cpp:900
const FlagDesc * getFlagDescFallback(const std::string &name) const
Definition settings.cpp:1023
bool parseCommandLine(int argc, char *argv[], const std::map< std::string, ValueSpec > &allowed_options)
Definition settings.cpp:387
Definition test_settings.cpp:12
core::vector2d< f32 > v2f
Definition irr_v2d.h:11
core::vector3df v3f
Definition irr_v3d.h:11
#define U32_MAX
Definition irrlichttypes.h:29
static OptionList allowed_options
Definition main.cpp:121
Settings * g_settings
Definition settings.cpp:22
std::vector< std::pair< SettingsChangedCallback, void * > > SettingsCallbackList
Definition settings.h:30
std::unordered_map< std::string, SettingsEntry > SettingEntries
Definition settings.h:107
SettingsLayer
Definition settings.h:50
@ SL_GLOBAL
Definition settings.h:53
@ SL_GAME
Definition settings.h:52
@ SL_DEFAULTS
Definition settings.h:51
@ SL_TOTAL_COUNT
Definition settings.h:54
void(* SettingsChangedCallback)(const std::string &name, void *data)
Definition settings.h:23
std::string g_settings_path
Definition settings.cpp:24
SettingsParseEvent
Definition settings.h:39
@ SPE_KVPAIR
Definition settings.h:43
@ SPE_MULTILINE
Definition settings.h:46
@ SPE_GROUP
Definition settings.h:45
@ SPE_INVALID
Definition settings.h:41
@ SPE_COMMENT
Definition settings.h:42
@ SPE_END
Definition settings.h:44
@ SPE_NONE
Definition settings.h:40
std::unordered_map< std::string, SettingsCallbackList > SettingsCallbackMap
Definition settings.h:32
ValueType
Definition settings.h:34
@ VALUETYPE_FLAG
Definition settings.h:36
@ VALUETYPE_STRING
Definition settings.h:35
Definition string.h:67
Definition noise.h:119
Definition settings.h:90
std::string value
Definition settings.h:102
SettingsEntry(const std::string &value_)
Definition settings.h:93
bool is_group
Definition settings.h:104
Settings * group
Definition settings.h:103
SettingsEntry(Settings *group_)
Definition settings.h:97
SettingsEntry()=default
Definition settings.h:79
const char * help
Definition settings.h:87
ValueSpec(ValueType a_type, const char *a_help=NULL)
Definition settings.h:80
ValueType type
Definition settings.h:86