| 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 | #ifndef PROJECT_SETTINGS_H |
| 32 | #define PROJECT_SETTINGS_H |
| 33 | |
| 34 | #include "core/object/class_db.h" |
| 35 | #include "core/os/thread_safe.h" |
| 36 | #include "core/templates/hash_map.h" |
| 37 | #include "core/templates/local_vector.h" |
| 38 | #include "core/templates/rb_set.h" |
| 39 | |
| 40 | template <typename T> |
| 41 | class TypedArray; |
| 42 | |
| 43 | class ProjectSettings : public Object { |
| 44 | GDCLASS(ProjectSettings, Object); |
| 45 | _THREAD_SAFE_CLASS_ |
| 46 | friend class TestProjectSettingsInternalsAccessor; |
| 47 | |
| 48 | bool is_changed = false; |
| 49 | |
| 50 | public: |
| 51 | typedef HashMap<String, Variant> CustomMap; |
| 52 | static const String PROJECT_DATA_DIR_NAME_SUFFIX; |
| 53 | |
| 54 | enum { |
| 55 | // Properties that are not for built in values begin from this value, so builtin ones are displayed first. |
| 56 | NO_BUILTIN_ORDER_BASE = 1 << 16 |
| 57 | }; |
| 58 | |
| 59 | #ifdef TOOLS_ENABLED |
| 60 | const static PackedStringArray get_required_features(); |
| 61 | const static PackedStringArray get_unsupported_features(const PackedStringArray &p_project_features); |
| 62 | #endif // TOOLS_ENABLED |
| 63 | |
| 64 | struct AutoloadInfo { |
| 65 | StringName name; |
| 66 | String path; |
| 67 | bool is_singleton = false; |
| 68 | }; |
| 69 | |
| 70 | protected: |
| 71 | struct VariantContainer { |
| 72 | int order = 0; |
| 73 | bool persist = false; |
| 74 | bool basic = false; |
| 75 | bool internal = false; |
| 76 | Variant variant; |
| 77 | Variant initial; |
| 78 | bool hide_from_editor = false; |
| 79 | bool restart_if_changed = false; |
| 80 | #ifdef DEBUG_METHODS_ENABLED |
| 81 | bool ignore_value_in_docs = false; |
| 82 | #endif |
| 83 | |
| 84 | VariantContainer() {} |
| 85 | |
| 86 | VariantContainer(const Variant &p_variant, int p_order, bool p_persist = false) : |
| 87 | order(p_order), |
| 88 | persist(p_persist), |
| 89 | variant(p_variant) { |
| 90 | } |
| 91 | }; |
| 92 | |
| 93 | int last_order = NO_BUILTIN_ORDER_BASE; |
| 94 | int last_builtin_order = 0; |
| 95 | uint64_t last_save_time = 0; |
| 96 | |
| 97 | RBMap<StringName, VariantContainer> props; // NOTE: Key order is used e.g. in the save_custom method. |
| 98 | String resource_path; |
| 99 | HashMap<StringName, PropertyInfo> custom_prop_info; |
| 100 | bool using_datapack = false; |
| 101 | bool project_loaded = false; |
| 102 | List<String> input_presets; |
| 103 | |
| 104 | HashSet<String> custom_features; |
| 105 | HashMap<StringName, LocalVector<Pair<StringName, StringName>>> feature_overrides; |
| 106 | |
| 107 | LocalVector<String> hidden_prefixes; |
| 108 | HashMap<StringName, AutoloadInfo> autoloads; |
| 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 ProjectSettings *singleton; |
| 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 = true, int p_offset = 0); |
| 143 | |
| 144 | void _add_property_info_bind(const Dictionary &p_info); |
| 145 | |
| 146 | Error _setup(const String &p_path, const String &p_main_pack, bool p_upwards = false, bool p_ignore_override = false); |
| 147 | |
| 148 | void _add_builtin_input_map(); |
| 149 | |
| 150 | protected: |
| 151 | static void _bind_methods(); |
| 152 | |
| 153 | public: |
| 154 | static const int CONFIG_VERSION = 5; |
| 155 | |
| 156 | void set_setting(const String &p_setting, const Variant &p_value); |
| 157 | Variant get_setting(const String &p_setting, const Variant &p_default_value = Variant()) const; |
| 158 | TypedArray<Dictionary> get_global_class_list(); |
| 159 | void store_global_class_list(const Array &p_classes); |
| 160 | String get_global_class_list_path() const; |
| 161 | |
| 162 | bool has_setting(String p_var) const; |
| 163 | String localize_path(const String &p_path) const; |
| 164 | String globalize_path(const String &p_path) const; |
| 165 | |
| 166 | void set_initial_value(const String &p_name, const Variant &p_value); |
| 167 | void set_as_basic(const String &p_name, bool p_basic); |
| 168 | void set_as_internal(const String &p_name, bool p_internal); |
| 169 | void set_restart_if_changed(const String &p_name, bool p_restart); |
| 170 | void set_ignore_value_in_docs(const String &p_name, bool p_ignore); |
| 171 | bool get_ignore_value_in_docs(const String &p_name) const; |
| 172 | void add_hidden_prefix(const String &p_prefix); |
| 173 | |
| 174 | String get_project_data_dir_name() const; |
| 175 | String get_project_data_path() const; |
| 176 | String get_resource_path() const; |
| 177 | String get_imported_files_path() const; |
| 178 | |
| 179 | static ProjectSettings *get_singleton(); |
| 180 | |
| 181 | void clear(const String &p_name); |
| 182 | int get_order(const String &p_name) const; |
| 183 | void set_order(const String &p_name, int p_order); |
| 184 | void set_builtin_order(const String &p_name); |
| 185 | bool is_builtin_setting(const String &p_name) const; |
| 186 | |
| 187 | Error setup(const String &p_path, const String &p_main_pack, bool p_upwards = false, bool p_ignore_override = false); |
| 188 | |
| 189 | Error load_custom(const String &p_path); |
| 190 | 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); |
| 191 | Error save(); |
| 192 | void set_custom_property_info(const PropertyInfo &p_info); |
| 193 | const HashMap<StringName, PropertyInfo> &get_custom_property_info() const; |
| 194 | uint64_t get_last_saved_time() { return last_save_time; } |
| 195 | |
| 196 | List<String> get_input_presets() const { return input_presets; } |
| 197 | |
| 198 | Variant get_setting_with_override(const StringName &p_name) const; |
| 199 | |
| 200 | bool is_using_datapack() const; |
| 201 | bool is_project_loaded() const; |
| 202 | |
| 203 | bool has_custom_feature(const String &p_feature) const; |
| 204 | |
| 205 | const HashMap<StringName, AutoloadInfo> &get_autoload_list() const; |
| 206 | void add_autoload(const AutoloadInfo &p_autoload); |
| 207 | void remove_autoload(const StringName &p_autoload); |
| 208 | bool has_autoload(const StringName &p_autoload) const; |
| 209 | AutoloadInfo get_autoload(const StringName &p_name) const; |
| 210 | |
| 211 | ProjectSettings(); |
| 212 | ~ProjectSettings(); |
| 213 | }; |
| 214 | |
| 215 | // Not a macro any longer. |
| 216 | 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); |
| 217 | 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); |
| 218 | |
| 219 | #define GLOBAL_DEF(m_var, m_value) _GLOBAL_DEF(m_var, m_value) |
| 220 | #define GLOBAL_DEF_RST(m_var, m_value) _GLOBAL_DEF(m_var, m_value, true) |
| 221 | #define GLOBAL_DEF_NOVAL(m_var, m_value) _GLOBAL_DEF(m_var, m_value, false, true) |
| 222 | #define GLOBAL_DEF_RST_NOVAL(m_var, m_value) _GLOBAL_DEF(m_var, m_value, true, true) |
| 223 | #define GLOBAL_GET(m_var) ProjectSettings::get_singleton()->get_setting_with_override(m_var) |
| 224 | |
| 225 | #define GLOBAL_DEF_BASIC(m_var, m_value) _GLOBAL_DEF(m_var, m_value, false, false, true) |
| 226 | #define GLOBAL_DEF_RST_BASIC(m_var, m_value) _GLOBAL_DEF(m_var, m_value, true, false, true) |
| 227 | #define GLOBAL_DEF_NOVAL_BASIC(m_var, m_value) _GLOBAL_DEF(m_var, m_value, false, true, true) |
| 228 | #define GLOBAL_DEF_RST_NOVAL_BASIC(m_var, m_value) _GLOBAL_DEF(m_var, m_value, true, true, true) |
| 229 | |
| 230 | #define GLOBAL_DEF_INTERNAL(m_var, m_value) _GLOBAL_DEF(m_var, m_value, false, false, false, true) |
| 231 | |
| 232 | #endif // PROJECT_SETTINGS_H |
| 233 | |