project_settings.h 12 KB

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