Pyrogenesis HEAD
Pyrogenesis, a RTS Engine
ConfigDB.h
Go to the documentation of this file.
1/* Copyright (C) 2026 Wildfire Games.
2 * This file is part of 0 A.D.
3 *
4 * 0 A.D. is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * 0 A.D. is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18/*
19 CConfigDB - Load, access and store configuration variables
20
21 TDD : http://www.wildfiregames.com/forum/index.php?showtopic=1125
22 OVERVIEW:
23
24 JavaScript: Check this documentation: https://gitea.wildfiregames.com/0ad/0ad/wiki/Exposed_ConfigDB_Functions
25*/
26
27#ifndef INCLUDED_CONFIGDB
28#define INCLUDED_CONFIGDB
29
31#include "ps/CStr.h"
32
33#include <array>
34#include <functional>
35#include <map>
36#include <memory>
37#include <mutex>
38#include <string>
39#include <string_view>
40#include <utility>
41#include <vector>
42
43/**
44 * Namespace priorities:
45 * - Command line args override everything
46 * - User supersedes HWDetect (let the user try crashing his system).
47 * - HWDetect supersedes mods & default -> mods can mod hwdetect itself.
48 * - SYSTEM is used for local.cfg and is basically for setting custom defaults.
49 */
51{
59};
60
61using CConfigValueSet = std::vector<CStr>;
62
63// Opaque data type so that callers that hook into ConfigDB can delete their hooks.
64// Would be defined in CConfigDB but then it couldn't be forward-declared, which is rather annoying.
65// Actually defined below - requires access to CConfigDB.
66class CConfigDBHook;
67
69{
71public:
72 CConfigDB();
73 ~CConfigDB();
74 CConfigDB(const CConfigDB&) = delete;
75 CConfigDB(CConfigDB&&) = delete;
76
77 static void Initialise();
78 static void Shutdown();
79 static bool IsInitialised();
80 static CConfigDB* Instance();
81
82 /**
83 * Attempt to retrieve the value of a config variable with the given name;
84 * will search CFG_COMMAND first, and then all namespaces from the specified
85 * namespace down.
86 */
87 void GetValue(EConfigNamespace ns, const std::string_view name, bool& value);
88 ///@copydoc CConfigDB::GetValue
89 void GetValue(EConfigNamespace ns, const std::string_view name, int& value);
90 ///@copydoc CConfigDB::GetValue
91 void GetValue(EConfigNamespace ns, const std::string_view name, std::uint32_t& value);
92 ///@copydoc CConfigDB::GetValue
93 void GetValue(EConfigNamespace ns, const std::string_view name, float& value);
94 ///@copydoc CConfigDB::GetValue
95 void GetValue(EConfigNamespace ns, const std::string_view name, double& value);
96 ///@copydoc CConfigDB::GetValue
97 void GetValue(EConfigNamespace ns, const std::string_view name, std::string& value);
98
99 template<typename T>
100 [[nodiscard]] T Get(const std::string_view name, T value, const EConfigNamespace ns = CFG_USER)
101 {
102 GetValue(ns, name, value);
103 return value;
104 }
105
106 template<typename T>
107 [[nodiscard]] static T GetIfInitialised(const std::string_view name, T defaultValue,
108 const EConfigNamespace ns = CFG_USER)
109 {
110 return IsInitialised() ? Instance()->Get(name, std::move(defaultValue), ns) : defaultValue;
111 }
112
113 /**
114 * Returns true if changed with respect to last write on file
115 */
116 bool HasChanges(EConfigNamespace ns) const;
117
118 void SetChanges(EConfigNamespace ns, bool value);
119
120 /**
121 * Attempt to retrieve a vector of values corresponding to the given setting;
122 * will search CFG_COMMAND first, and then all namespaces from the specified
123 * namespace down.
124 */
125 void GetValues(EConfigNamespace ns, const CStr& name, CConfigValueSet& values) const;
126
127 /**
128 * Returns the namespace that the value returned by GetValues was defined in,
129 * or CFG_LAST if it wasn't defined at all.
130 */
131 EConfigNamespace GetValueNamespace(EConfigNamespace ns, const CStr& name) const;
132
133 /**
134 * Retrieve a map of values corresponding to settings whose names begin
135 * with the given prefix;
136 * will search all namespaces from default up to the specified namespace.
137 */
138 std::map<CStr, CConfigValueSet> GetValuesWithPrefix(EConfigNamespace ns, const CStr& prefix) const;
139
140 /**
141 * Save a config value in the specified namespace. If the config variable
142 * existed the value is replaced.
143 */
144 void SetValueString(EConfigNamespace ns, const CStr& name, const CStr& value);
145
146 void SetValueBool(EConfigNamespace ns, const CStr& name, const bool value);
147
148 void SetValueList(EConfigNamespace ns, const CStr& name, std::vector<CStr> values);
149
150 /**
151 * Remove a config value in the specified namespace.
152 */
153 bool RemoveValue(EConfigNamespace ns, const CStr& name);
154
155 /**
156 * Set the path to the config file used to populate the specified namespace
157 * Note that this function does not actually load the config file. Use
158 * the Reload() method if you want to read the config file at the same time.
159 *
160 * 'path': The path to the config file.
161 */
162 void SetConfigFile(EConfigNamespace ns, const VfsPath& path);
163
164 /**
165 * Reload the config file associated with the specified config namespace
166 * (the last config file path set with SetConfigFile)
167 *
168 * Returns:
169 * true: if the reload succeeded,
170 * false: if the reload failed
171 */
173
174 /**
175 * Write the current state of the specified config namespace to the file
176 * specified by 'path'
177 *
178 * Returns:
179 * true: if the config namespace was successfully written to the file
180 * false: if an error occurred
181 */
182 bool WriteFile(EConfigNamespace ns, const VfsPath& path) const;
183
184 /**
185 * Write the current state of the specified config namespace to the file
186 * it was originally loaded from.
187 *
188 * Returns:
189 * true: if the config namespace was successfully written to the file
190 * false: if an error occurred
191 */
192 bool WriteFile(EConfigNamespace ns) const;
193
194 /**
195 * Write a config value to the file specified by 'path'
196 *
197 * Returns:
198 * true: if the config value was successfully saved and written to the file
199 * false: if an error occurred
200 */
201 bool WriteValueToFile(EConfigNamespace ns, const CStr& name, const CStr& value, const VfsPath& path);
202
203 bool WriteValueToFile(EConfigNamespace ns, const CStr& name, const CStr& value);
204
205 /**
206 * Register a simple lambda that will be called anytime the value changes in any namespace
207 * This is simple on purpose, the hook is responsible for checking if it should do something.
208 * When RegisterHookAndCall is called, the hook is immediately triggered.
209 * NB: CConfigDBHook will auto-unregister the hook when destroyed,
210 * so you can use it to tie the lifetime of the hook to your object.
211 * The hook will be deleted alongside ConfigDB anyways.
212 */
213 [[nodiscard]] CConfigDBHook RegisterHookAndCall(const CStr& name, std::function<void()> hook);
214
215 void UnregisterHook(CConfigDBHook&& hook);
216 void UnregisterHook(std::unique_ptr<CConfigDBHook> hook);
217
218private:
219 using TConfigMap = std::map<CStr, CConfigValueSet, std::less<>>;
220 std::array<TConfigMap, CFG_LAST> m_Map;
221 std::multimap<CStr, std::function<void()>> m_Hooks;
222 std::array<VfsPath, CFG_LAST> m_ConfigFile;
223 std::array<bool, CFG_LAST> m_HasChanges;
224
225 mutable std::recursive_mutex m_Mutex;
226};
227
229{
230 friend class CConfigDB;
231public:
232 CConfigDBHook() = delete;
233 CConfigDBHook(const CConfigDBHook&) = delete;
234 // Point the moved-from hook to end, which is checked for in UnregisterHook,
235 // to avoid a double-erase error.
237 {
238 m_Ptr = std::move(h.m_Ptr);
239 h.m_Ptr = m_ConfigDB.m_Hooks.end();
240 }
241 // Unregisters the hook. Must be called before the original ConfigDB gets deleted.
243 {
244 m_ConfigDB.UnregisterHook(std::move(*this));
245 }
246private:
247 CConfigDBHook(CConfigDB& cdb, std::multimap<CStr, std::function<void()>>::iterator p)
248 : m_ConfigDB(cdb), m_Ptr(p)
249 {};
250
251 std::multimap<CStr, std::function<void()>>::iterator m_Ptr;
253};
254
255#define g_ConfigDB (*CConfigDB::Instance())
256
257#endif // INCLUDED_CONFIGDB
std::vector< CStr > CConfigValueSet
Definition: ConfigDB.h:61
EConfigNamespace
Namespace priorities:
Definition: ConfigDB.h:51
@ CFG_HWDETECT
Definition: ConfigDB.h:55
@ CFG_COMMAND
Definition: ConfigDB.h:57
@ CFG_SYSTEM
Definition: ConfigDB.h:54
@ CFG_DEFAULT
Definition: ConfigDB.h:52
@ CFG_LAST
Definition: ConfigDB.h:58
@ CFG_MOD
Definition: ConfigDB.h:53
@ CFG_USER
Definition: ConfigDB.h:56
Definition: ConfigDB.h:229
CConfigDBHook()=delete
CConfigDBHook(const CConfigDBHook &)=delete
~CConfigDBHook()
Definition: ConfigDB.h:242
CConfigDBHook(CConfigDBHook &&h)
Definition: ConfigDB.h:236
CConfigDBHook(CConfigDB &cdb, std::multimap< CStr, std::function< void()> >::iterator p)
Definition: ConfigDB.h:247
std::multimap< CStr, std::function< void()> >::iterator m_Ptr
Definition: ConfigDB.h:251
CConfigDB & m_ConfigDB
Definition: ConfigDB.h:252
Definition: ConfigDB.h:69
void GetValue(EConfigNamespace ns, const std::string_view name, std::string &value)
Attempt to retrieve the value of a config variable with the given name; will search CFG_COMMAND first...
~CConfigDB()
Definition: ConfigDB.cpp:168
bool Reload(EConfigNamespace)
Reload the config file associated with the specified config namespace (the last config file path set ...
Definition: ConfigDB.cpp:308
CConfigDB(const CConfigDB &)=delete
static T GetIfInitialised(const std::string_view name, T defaultValue, const EConfigNamespace ns=CFG_USER)
Definition: ConfigDB.h:107
std::map< CStr, CConfigValueSet > GetValuesWithPrefix(EConfigNamespace ns, const CStr &prefix) const
Retrieve a map of values corresponding to settings whose names begin with the given prefix; will sear...
Definition: ConfigDB.cpp:230
void GetValue(EConfigNamespace ns, const std::string_view name, double &value)
Attempt to retrieve the value of a config variable with the given name; will search CFG_COMMAND first...
bool RemoveValue(EConfigNamespace ns, const CStr &name)
Remove a config value in the specified namespace.
Definition: ConfigDB.cpp:286
void GetValue(EConfigNamespace ns, const std::string_view name, float &value)
Attempt to retrieve the value of a config variable with the given name; will search CFG_COMMAND first...
T Get(const std::string_view name, T value, const EConfigNamespace ns=CFG_USER)
Definition: ConfigDB.h:100
void UnregisterHook(CConfigDBHook &&hook)
Definition: ConfigDB.cpp:558
void SetValueString(EConfigNamespace ns, const CStr &name, const CStr &value)
Save a config value in the specified namespace.
Definition: ConfigDB.cpp:251
void SetValueBool(EConfigNamespace ns, const CStr &name, const bool value)
Definition: ConfigDB.cpp:268
void SetChanges(EConfigNamespace ns, bool value)
Definition: ConfigDB.cpp:180
bool WriteValueToFile(EConfigNamespace ns, const CStr &name, const CStr &value, const VfsPath &path)
Write a config value to the file specified by 'path'.
Definition: ConfigDB.cpp:535
std::recursive_mutex m_Mutex
Definition: ConfigDB.h:225
CConfigDBHook RegisterHookAndCall(const CStr &name, std::function< void()> hook)
Register a simple lambda that will be called anytime the value changes in any namespace This is simpl...
Definition: ConfigDB.cpp:551
void SetConfigFile(EConfigNamespace ns, const VfsPath &path)
Set the path to the config file used to populate the specified namespace Note that this function does...
Definition: ConfigDB.cpp:300
std::multimap< CStr, std::function< void()> > m_Hooks
Definition: ConfigDB.h:221
CConfigDB()
Definition: ConfigDB.cpp:163
std::array< TConfigMap, CFG_LAST > m_Map
Definition: ConfigDB.h:220
void GetValue(EConfigNamespace ns, const std::string_view name, bool &value)
Attempt to retrieve the value of a config variable with the given name; will search CFG_COMMAND first...
static void Shutdown()
Definition: ConfigDB.cpp:147
void GetValues(EConfigNamespace ns, const CStr &name, CConfigValueSet &values) const
Attempt to retrieve a vector of values corresponding to the given setting; will search CFG_COMMAND fi...
Definition: ConfigDB.cpp:188
void GetValue(EConfigNamespace ns, const std::string_view name, std::uint32_t &value)
Attempt to retrieve the value of a config variable with the given name; will search CFG_COMMAND first...
static void Initialise()
Definition: ConfigDB.cpp:142
EConfigNamespace GetValueNamespace(EConfigNamespace ns, const CStr &name) const
Returns the namespace that the value returned by GetValues was defined in, or CFG_LAST if it wasn't d...
Definition: ConfigDB.cpp:211
void SetValueList(EConfigNamespace ns, const CStr &name, std::vector< CStr > values)
Definition: ConfigDB.cpp:274
void GetValue(EConfigNamespace ns, const std::string_view name, int &value)
Attempt to retrieve the value of a config variable with the given name; will search CFG_COMMAND first...
static CConfigDB * Instance()
Definition: ConfigDB.cpp:158
std::map< CStr, CConfigValueSet, std::less<> > TConfigMap
Definition: ConfigDB.h:219
static bool IsInitialised()
Definition: ConfigDB.cpp:153
bool HasChanges(EConfigNamespace ns) const
Returns true if changed with respect to last write on file.
Definition: ConfigDB.cpp:172
friend CConfigDBHook
Definition: ConfigDB.h:70
bool WriteFile(EConfigNamespace ns, const VfsPath &path) const
Write the current state of the specified config namespace to the file specified by 'path'.
Definition: ConfigDB.cpp:490
CConfigDB(CConfigDB &&)=delete
std::array< VfsPath, CFG_LAST > m_ConfigFile
Definition: ConfigDB.h:222
std::array< bool, CFG_LAST > m_HasChanges
Definition: ConfigDB.h:223
Definition: path.h:84
#define T(string_literal)
Definition: secure_crt.cpp:81