Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/core/config/project_settings.h
21328 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/object.h"
34
#include "core/templates/rb_map.h"
35
36
template <typename T>
37
class TypedArray;
38
39
class ProjectSettings : public Object {
40
GDCLASS(ProjectSettings, Object);
41
_THREAD_SAFE_CLASS_
42
friend class TestProjectSettingsInternalsAccessor;
43
44
bool is_changed = false;
45
46
// Starting version from 1 ensures that all callers can reset their tested version to 0,
47
// and will always detect the initial project settings as a "change".
48
uint32_t _version = 1;
49
50
// Track changed settings for get_changed_settings functionality
51
HashSet<StringName> changed_settings;
52
53
public:
54
typedef HashMap<String, Variant> CustomMap;
55
// This constant is used to make the ".godot" folder and paths like "res://.godot/editor".
56
static inline const String PROJECT_DATA_DIR_NAME_SUFFIX = "godot";
57
static inline const String EDITOR_SETTING_OVERRIDE_PREFIX = PNAME("editor_overrides") + String("/");
58
59
// Properties that are not for built in values begin from this value, so builtin ones are displayed first.
60
constexpr static const int32_t NO_BUILTIN_ORDER_BASE = 1 << 16;
61
62
#ifdef TOOLS_ENABLED
63
const static PackedStringArray get_required_features();
64
const static PackedStringArray get_unsupported_features(const PackedStringArray &p_project_features);
65
#endif // TOOLS_ENABLED
66
67
struct AutoloadInfo {
68
StringName name;
69
String path;
70
bool is_singleton = false;
71
};
72
73
protected:
74
struct VariantContainer {
75
int order = 0;
76
bool persist = false;
77
bool basic = false;
78
bool internal = false;
79
Variant variant;
80
Variant initial;
81
bool hide_from_editor = false;
82
bool restart_if_changed = false;
83
#ifdef DEBUG_ENABLED
84
bool ignore_value_in_docs = false;
85
#endif // DEBUG_ENABLED
86
87
VariantContainer() {}
88
89
VariantContainer(const Variant &p_variant, int p_order, bool p_persist = false) :
90
order(p_order),
91
persist(p_persist),
92
variant(p_variant) {
93
}
94
};
95
96
int last_order = NO_BUILTIN_ORDER_BASE;
97
int last_builtin_order = 0;
98
uint64_t last_save_time = 0;
99
100
RBMap<StringName, VariantContainer> props; // NOTE: Key order is used e.g. in the save_custom method.
101
String resource_path;
102
HashMap<StringName, PropertyInfo> custom_prop_info;
103
bool using_datapack = false;
104
bool project_loaded = false;
105
List<String> input_presets;
106
107
HashSet<String> custom_features;
108
HashMap<StringName, LocalVector<Pair<StringName, StringName>>> feature_overrides;
109
110
LocalVector<String> hidden_prefixes;
111
HashMap<StringName, AutoloadInfo> autoloads;
112
HashMap<StringName, String> global_groups;
113
HashMap<StringName, HashSet<StringName>> scene_groups_cache;
114
115
Array global_class_list;
116
bool is_global_class_list_loaded = false;
117
118
String project_data_dir_name;
119
120
bool _set(const StringName &p_name, const Variant &p_value);
121
bool _get(const StringName &p_name, Variant &r_ret) const;
122
void _get_property_list(List<PropertyInfo> *p_list) const;
123
bool _property_can_revert(const StringName &p_name) const;
124
bool _property_get_revert(const StringName &p_name, Variant &r_property) const;
125
126
void _queue_changed(const StringName &p_name);
127
void _emit_changed();
128
129
static inline ProjectSettings *singleton = nullptr;
130
131
Error _load_settings_text(const String &p_path);
132
Error _load_settings_binary(const String &p_path);
133
Error _load_settings_text_or_binary(const String &p_text_path, const String &p_bin_path);
134
135
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());
136
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());
137
138
Error _save_custom_bnd(const String &p_file);
139
140
#ifdef TOOLS_ENABLED
141
const static PackedStringArray _get_supported_features();
142
const static PackedStringArray _trim_to_supported_features(const PackedStringArray &p_project_features);
143
#endif // TOOLS_ENABLED
144
145
void _convert_to_last_version(int p_from_version);
146
147
bool load_resource_pack(const String &p_pack, bool p_replace_files, int p_offset);
148
bool _load_resource_pack(const String &p_pack, bool p_replace_files = true, int p_offset = 0, bool p_main_pack = false);
149
150
void _add_property_info_bind(const Dictionary &p_info);
151
152
Error _setup(const String &p_path, const String &p_main_pack, bool p_upwards = false, bool p_ignore_override = false);
153
154
void _add_builtin_input_map();
155
156
protected:
157
static void _bind_methods();
158
159
public:
160
static const int CONFIG_VERSION = 5;
161
162
#ifdef TOOLS_ENABLED
163
HashMap<String, PropertyInfo> editor_settings_info;
164
#endif
165
166
void set_setting(const String &p_setting, const Variant &p_value);
167
Variant get_setting(const String &p_setting, const Variant &p_default_value = Variant()) const;
168
TypedArray<Dictionary> get_global_class_list();
169
void refresh_global_class_list();
170
void store_global_class_list(const Array &p_classes);
171
String get_global_class_list_path() const;
172
173
bool has_setting(const String &p_var) const;
174
String localize_path(const String &p_path) const;
175
String globalize_path(const String &p_path) const;
176
177
void set_initial_value(const String &p_name, const Variant &p_value);
178
void set_as_basic(const String &p_name, bool p_basic);
179
void set_as_internal(const String &p_name, bool p_internal);
180
void set_restart_if_changed(const String &p_name, bool p_restart);
181
void set_ignore_value_in_docs(const String &p_name, bool p_ignore);
182
bool get_ignore_value_in_docs(const String &p_name) const;
183
void add_hidden_prefix(const String &p_prefix);
184
185
String get_project_data_dir_name() const;
186
String get_project_data_path() const;
187
String get_resource_path() const;
188
String get_imported_files_path() const;
189
190
static ProjectSettings *get_singleton();
191
192
void clear(const String &p_name);
193
int get_order(const String &p_name) const;
194
void set_order(const String &p_name, int p_order);
195
void set_builtin_order(const String &p_name);
196
bool is_builtin_setting(const String &p_name) const;
197
198
Error setup(const String &p_path, const String &p_main_pack, bool p_upwards = false, bool p_ignore_override = false);
199
200
Error load_custom(const String &p_path);
201
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);
202
Error save();
203
void set_custom_property_info(const PropertyInfo &p_info);
204
const HashMap<StringName, PropertyInfo> &get_custom_property_info() const;
205
uint64_t get_last_saved_time() { return last_save_time; }
206
207
List<String> get_input_presets() const { return input_presets; }
208
209
Variant get_setting_with_override(const StringName &p_name) const;
210
Variant get_setting_with_override_and_custom_features(const StringName &p_name, const Vector<String> &p_features) const;
211
212
bool is_using_datapack() const;
213
bool is_project_loaded() const;
214
215
bool has_custom_feature(const String &p_feature) const;
216
217
// Change tracking methods
218
PackedStringArray get_changed_settings() const;
219
bool check_changed_settings_in_group(const String &p_setting_prefix) const;
220
221
const HashMap<StringName, AutoloadInfo> &get_autoload_list() const;
222
void add_autoload(const AutoloadInfo &p_autoload, bool p_front_insert = false);
223
void remove_autoload(const StringName &p_autoload);
224
bool has_autoload(const StringName &p_autoload) const;
225
AutoloadInfo get_autoload(const StringName &p_name) const;
226
void fix_autoload_paths();
227
228
const HashMap<StringName, String> &get_global_groups_list() const;
229
void add_global_group(const StringName &p_name, const String &p_description);
230
void remove_global_group(const StringName &p_name);
231
bool has_global_group(const StringName &p_name) const;
232
233
const HashMap<StringName, HashSet<StringName>> &get_scene_groups_cache() const;
234
void add_scene_groups_cache(const StringName &p_path, const HashSet<StringName> &p_cache);
235
void remove_scene_groups_cache(const StringName &p_path);
236
void save_scene_groups_cache();
237
String get_scene_groups_cache_path() const;
238
void load_scene_groups_cache();
239
240
// Testing a version allows fast cached GET_GLOBAL macros.
241
uint32_t get_version() const { return _version; }
242
243
#ifdef TOOLS_ENABLED
244
virtual void get_argument_options(const StringName &p_function, int p_idx, List<String> *r_options) const override;
245
#endif
246
247
void set_editor_setting_override(const String &p_setting, const Variant &p_value);
248
bool has_editor_setting_override(const String &p_setting) const;
249
Variant get_editor_setting_override(const String &p_setting) const;
250
251
ProjectSettings();
252
ProjectSettings(const String &p_path);
253
~ProjectSettings();
254
};
255
256
// Not a macro any longer.
257
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);
258
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);
259
260
#define GLOBAL_DEF(m_var, m_value) _GLOBAL_DEF(m_var, m_value)
261
#define GLOBAL_DEF_RST(m_var, m_value) _GLOBAL_DEF(m_var, m_value, true)
262
#define GLOBAL_DEF_NOVAL(m_var, m_value) _GLOBAL_DEF(m_var, m_value, false, true)
263
#define GLOBAL_DEF_RST_NOVAL(m_var, m_value) _GLOBAL_DEF(m_var, m_value, true, true)
264
#define GLOBAL_GET(m_var) ProjectSettings::get_singleton()->get_setting_with_override(m_var)
265
266
#define GLOBAL_DEF_BASIC(m_var, m_value) _GLOBAL_DEF(m_var, m_value, false, false, true)
267
#define GLOBAL_DEF_RST_BASIC(m_var, m_value) _GLOBAL_DEF(m_var, m_value, true, false, true)
268
#define GLOBAL_DEF_NOVAL_BASIC(m_var, m_value) _GLOBAL_DEF(m_var, m_value, false, true, true)
269
#define GLOBAL_DEF_RST_NOVAL_BASIC(m_var, m_value) _GLOBAL_DEF(m_var, m_value, true, true, true)
270
271
#define GLOBAL_DEF_INTERNAL(m_var, m_value) _GLOBAL_DEF(m_var, m_value, false, false, false, true)
272
273
/////////////////////////////////////////////////////////////////////////////////////////
274
// Cached versions of GLOBAL_GET.
275
// Cached but uses a typed variable for storage, this can be more efficient.
276
// Variables prefixed with _ggc_ to avoid shadowing warnings.
277
#define GLOBAL_GET_CACHED(m_type, m_setting_name) ([](const char *p_name) -> m_type {\
278
static_assert(std::is_trivially_destructible<m_type>::value, "GLOBAL_GET_CACHED must use a trivial type that allows static lifetime.");\
279
static m_type _ggc_local_var;\
280
static uint32_t _ggc_local_version = 0;\
281
static SpinLock _ggc_spin;\
282
uint32_t _ggc_new_version = ProjectSettings::get_singleton()->get_version();\
283
if (_ggc_local_version != _ggc_new_version) {\
284
_ggc_spin.lock();\
285
_ggc_local_version = _ggc_new_version;\
286
_ggc_local_var = ProjectSettings::get_singleton()->get_setting_with_override(p_name);\
287
m_type _ggc_temp = _ggc_local_var;\
288
_ggc_spin.unlock();\
289
return _ggc_temp;\
290
}\
291
_ggc_spin.lock();\
292
m_type _ggc_temp2 = _ggc_local_var;\
293
_ggc_spin.unlock();\
294
return _ggc_temp2; })(m_setting_name)
295
296