Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/core/config/project_settings.h
9973 views
1
/**************************************************************************/
2
/* project_settings.h */
3
/**************************************************************************/
4
/* This file is part of: */
5
/* GODOT ENGINE */
6
/* https://godotengine.org */
7
/**************************************************************************/
8
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10
/* */
11
/* Permission is hereby granted, free of charge, to any person obtaining */
12
/* a copy of this software and associated documentation files (the */
13
/* "Software"), to deal in the Software without restriction, including */
14
/* without limitation the rights to use, copy, modify, merge, publish, */
15
/* distribute, sublicense, and/or sell copies of the Software, and to */
16
/* permit persons to whom the Software is furnished to do so, subject to */
17
/* the following conditions: */
18
/* */
19
/* The above copyright notice and this permission notice shall be */
20
/* included in all copies or substantial portions of the Software. */
21
/* */
22
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29
/**************************************************************************/
30
31
#pragma once
32
33
#include "core/object/class_db.h"
34
35
template <typename T>
36
class TypedArray;
37
38
class ProjectSettings : public Object {
39
GDCLASS(ProjectSettings, Object);
40
_THREAD_SAFE_CLASS_
41
friend class TestProjectSettingsInternalsAccessor;
42
43
bool is_changed = false;
44
45
// Starting version from 1 ensures that all callers can reset their tested version to 0,
46
// and will always detect the initial project settings as a "change".
47
uint32_t _version = 1;
48
49
public:
50
typedef HashMap<String, Variant> CustomMap;
51
static inline const String PROJECT_DATA_DIR_NAME_SUFFIX = "godot";
52
static inline const String EDITOR_SETTING_OVERRIDE_PREFIX = "editor_overrides/";
53
54
// Properties that are not for built in values begin from this value, so builtin ones are displayed first.
55
constexpr static const int32_t NO_BUILTIN_ORDER_BASE = 1 << 16;
56
57
#ifdef TOOLS_ENABLED
58
const static PackedStringArray get_required_features();
59
const static PackedStringArray get_unsupported_features(const PackedStringArray &p_project_features);
60
#endif // TOOLS_ENABLED
61
62
struct AutoloadInfo {
63
StringName name;
64
String path;
65
bool is_singleton = false;
66
};
67
68
protected:
69
struct VariantContainer {
70
int order = 0;
71
bool persist = false;
72
bool basic = false;
73
bool internal = false;
74
Variant variant;
75
Variant initial;
76
bool hide_from_editor = false;
77
bool restart_if_changed = false;
78
#ifdef DEBUG_ENABLED
79
bool ignore_value_in_docs = false;
80
#endif // DEBUG_ENABLED
81
82
VariantContainer() {}
83
84
VariantContainer(const Variant &p_variant, int p_order, bool p_persist = false) :
85
order(p_order),
86
persist(p_persist),
87
variant(p_variant) {
88
}
89
};
90
91
int last_order = NO_BUILTIN_ORDER_BASE;
92
int last_builtin_order = 0;
93
uint64_t last_save_time = 0;
94
95
RBMap<StringName, VariantContainer> props; // NOTE: Key order is used e.g. in the save_custom method.
96
String resource_path;
97
HashMap<StringName, PropertyInfo> custom_prop_info;
98
bool using_datapack = false;
99
bool project_loaded = false;
100
List<String> input_presets;
101
102
HashSet<String> custom_features;
103
HashMap<StringName, LocalVector<Pair<StringName, StringName>>> feature_overrides;
104
105
LocalVector<String> hidden_prefixes;
106
HashMap<StringName, AutoloadInfo> autoloads;
107
HashMap<StringName, String> global_groups;
108
HashMap<StringName, HashSet<StringName>> scene_groups_cache;
109
110
Array global_class_list;
111
bool is_global_class_list_loaded = false;
112
113
String project_data_dir_name;
114
115
bool _set(const StringName &p_name, const Variant &p_value);
116
bool _get(const StringName &p_name, Variant &r_ret) const;
117
void _get_property_list(List<PropertyInfo> *p_list) const;
118
bool _property_can_revert(const StringName &p_name) const;
119
bool _property_get_revert(const StringName &p_name, Variant &r_property) const;
120
121
void _queue_changed();
122
void _emit_changed();
123
124
static inline ProjectSettings *singleton = nullptr;
125
126
Error _load_settings_text(const String &p_path);
127
Error _load_settings_binary(const String &p_path);
128
Error _load_settings_text_or_binary(const String &p_text_path, const String &p_bin_path);
129
130
Error _save_settings_text(const String &p_file, const RBMap<String, List<String>> &props, const CustomMap &p_custom = CustomMap(), const String &p_custom_features = String());
131
Error _save_settings_binary(const String &p_file, const RBMap<String, List<String>> &props, const CustomMap &p_custom = CustomMap(), const String &p_custom_features = String());
132
133
Error _save_custom_bnd(const String &p_file);
134
135
#ifdef TOOLS_ENABLED
136
const static PackedStringArray _get_supported_features();
137
const static PackedStringArray _trim_to_supported_features(const PackedStringArray &p_project_features);
138
#endif // TOOLS_ENABLED
139
140
void _convert_to_last_version(int p_from_version);
141
142
bool load_resource_pack(const String &p_pack, bool p_replace_files, int p_offset);
143
bool _load_resource_pack(const String &p_pack, bool p_replace_files = true, int p_offset = 0, bool p_main_pack = false);
144
145
void _add_property_info_bind(const Dictionary &p_info);
146
147
Error _setup(const String &p_path, const String &p_main_pack, bool p_upwards = false, bool p_ignore_override = false);
148
149
void _add_builtin_input_map();
150
151
protected:
152
static void _bind_methods();
153
154
public:
155
static const int CONFIG_VERSION = 5;
156
157
#ifdef TOOLS_ENABLED
158
HashMap<String, PropertyInfo> editor_settings_info;
159
#endif
160
161
void set_setting(const String &p_setting, const Variant &p_value);
162
Variant get_setting(const String &p_setting, const Variant &p_default_value = Variant()) const;
163
TypedArray<Dictionary> get_global_class_list();
164
void refresh_global_class_list();
165
void store_global_class_list(const Array &p_classes);
166
String get_global_class_list_path() const;
167
168
bool has_setting(const String &p_var) const;
169
String localize_path(const String &p_path) const;
170
String globalize_path(const String &p_path) const;
171
172
void set_initial_value(const String &p_name, const Variant &p_value);
173
void set_as_basic(const String &p_name, bool p_basic);
174
void set_as_internal(const String &p_name, bool p_internal);
175
void set_restart_if_changed(const String &p_name, bool p_restart);
176
void set_ignore_value_in_docs(const String &p_name, bool p_ignore);
177
bool get_ignore_value_in_docs(const String &p_name) const;
178
void add_hidden_prefix(const String &p_prefix);
179
180
String get_project_data_dir_name() const;
181
String get_project_data_path() const;
182
String get_resource_path() const;
183
String get_imported_files_path() const;
184
185
static ProjectSettings *get_singleton();
186
187
void clear(const String &p_name);
188
int get_order(const String &p_name) const;
189
void set_order(const String &p_name, int p_order);
190
void set_builtin_order(const String &p_name);
191
bool is_builtin_setting(const String &p_name) const;
192
193
Error setup(const String &p_path, const String &p_main_pack, bool p_upwards = false, bool p_ignore_override = false);
194
195
Error load_custom(const String &p_path);
196
Error save_custom(const String &p_path = "", const CustomMap &p_custom = CustomMap(), const Vector<String> &p_custom_features = Vector<String>(), bool p_merge_with_current = true);
197
Error save();
198
void set_custom_property_info(const PropertyInfo &p_info);
199
const HashMap<StringName, PropertyInfo> &get_custom_property_info() const;
200
uint64_t get_last_saved_time() { return last_save_time; }
201
202
List<String> get_input_presets() const { return input_presets; }
203
204
Variant get_setting_with_override(const StringName &p_name) const;
205
Variant get_setting_with_override_and_custom_features(const StringName &p_name, const Vector<String> &p_features) const;
206
207
bool is_using_datapack() const;
208
bool is_project_loaded() const;
209
210
bool has_custom_feature(const String &p_feature) const;
211
212
const HashMap<StringName, AutoloadInfo> &get_autoload_list() const;
213
void add_autoload(const AutoloadInfo &p_autoload);
214
void remove_autoload(const StringName &p_autoload);
215
bool has_autoload(const StringName &p_autoload) const;
216
AutoloadInfo get_autoload(const StringName &p_name) const;
217
218
const HashMap<StringName, String> &get_global_groups_list() const;
219
void add_global_group(const StringName &p_name, const String &p_description);
220
void remove_global_group(const StringName &p_name);
221
bool has_global_group(const StringName &p_name) const;
222
223
const HashMap<StringName, HashSet<StringName>> &get_scene_groups_cache() const;
224
void add_scene_groups_cache(const StringName &p_path, const HashSet<StringName> &p_cache);
225
void remove_scene_groups_cache(const StringName &p_path);
226
void save_scene_groups_cache();
227
String get_scene_groups_cache_path() const;
228
void load_scene_groups_cache();
229
230
// Testing a version allows fast cached GET_GLOBAL macros.
231
uint32_t get_version() const { return _version; }
232
233
#ifdef TOOLS_ENABLED
234
virtual void get_argument_options(const StringName &p_function, int p_idx, List<String> *r_options) const override;
235
#endif
236
237
void set_editor_setting_override(const String &p_setting, const Variant &p_value);
238
bool has_editor_setting_override(const String &p_setting) const;
239
Variant get_editor_setting_override(const String &p_setting) const;
240
241
ProjectSettings();
242
ProjectSettings(const String &p_path);
243
~ProjectSettings();
244
};
245
246
// Not a macro any longer.
247
Variant _GLOBAL_DEF(const String &p_var, const Variant &p_default, bool p_restart_if_changed = false, bool p_ignore_value_in_docs = false, bool p_basic = false, bool p_internal = false);
248
Variant _GLOBAL_DEF(const PropertyInfo &p_info, const Variant &p_default, bool p_restart_if_changed = false, bool p_ignore_value_in_docs = false, bool p_basic = false, bool p_internal = false);
249
250
#define GLOBAL_DEF(m_var, m_value) _GLOBAL_DEF(m_var, m_value)
251
#define GLOBAL_DEF_RST(m_var, m_value) _GLOBAL_DEF(m_var, m_value, true)
252
#define GLOBAL_DEF_NOVAL(m_var, m_value) _GLOBAL_DEF(m_var, m_value, false, true)
253
#define GLOBAL_DEF_RST_NOVAL(m_var, m_value) _GLOBAL_DEF(m_var, m_value, true, true)
254
#define GLOBAL_GET(m_var) ProjectSettings::get_singleton()->get_setting_with_override(m_var)
255
256
#define GLOBAL_DEF_BASIC(m_var, m_value) _GLOBAL_DEF(m_var, m_value, false, false, true)
257
#define GLOBAL_DEF_RST_BASIC(m_var, m_value) _GLOBAL_DEF(m_var, m_value, true, false, true)
258
#define GLOBAL_DEF_NOVAL_BASIC(m_var, m_value) _GLOBAL_DEF(m_var, m_value, false, true, true)
259
#define GLOBAL_DEF_RST_NOVAL_BASIC(m_var, m_value) _GLOBAL_DEF(m_var, m_value, true, true, true)
260
261
#define GLOBAL_DEF_INTERNAL(m_var, m_value) _GLOBAL_DEF(m_var, m_value, false, false, false, true)
262
263
/////////////////////////////////////////////////////////////////////////////////////////
264
// Cached versions of GLOBAL_GET.
265
// Cached but uses a typed variable for storage, this can be more efficient.
266
// Variables prefixed with _ggc_ to avoid shadowing warnings.
267
#define GLOBAL_GET_CACHED(m_type, m_setting_name) ([](const char *p_name) -> m_type {\
268
static_assert(std::is_trivially_destructible<m_type>::value, "GLOBAL_GET_CACHED must use a trivial type that allows static lifetime.");\
269
static m_type _ggc_local_var;\
270
static uint32_t _ggc_local_version = 0;\
271
static SpinLock _ggc_spin;\
272
uint32_t _ggc_new_version = ProjectSettings::get_singleton()->get_version();\
273
if (_ggc_local_version != _ggc_new_version) {\
274
_ggc_spin.lock();\
275
_ggc_local_version = _ggc_new_version;\
276
_ggc_local_var = ProjectSettings::get_singleton()->get_setting_with_override(p_name);\
277
m_type _ggc_temp = _ggc_local_var;\
278
_ggc_spin.unlock();\
279
return _ggc_temp;\
280
}\
281
_ggc_spin.lock();\
282
m_type _ggc_temp2 = _ggc_local_var;\
283
_ggc_spin.unlock();\
284
return _ggc_temp2; })(m_setting_name)
285
286