project_settings.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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. #ifndef PROJECT_SETTINGS_H
  31. #define PROJECT_SETTINGS_H
  32. #include "core/object/class_db.h"
  33. template <typename T>
  34. class TypedArray;
  35. class ProjectSettings : public Object {
  36. GDCLASS(ProjectSettings, Object);
  37. _THREAD_SAFE_CLASS_
  38. friend class TestProjectSettingsInternalsAccessor;
  39. bool is_changed = false;
  40. public:
  41. typedef HashMap<String, Variant> CustomMap;
  42. static const String PROJECT_DATA_DIR_NAME_SUFFIX;
  43. // Properties that are not for built in values begin from this value, so builtin ones are displayed first.
  44. constexpr static const int32_t NO_BUILTIN_ORDER_BASE = 1 << 16;
  45. #ifdef TOOLS_ENABLED
  46. const static PackedStringArray get_required_features();
  47. const static PackedStringArray get_unsupported_features(const PackedStringArray &p_project_features);
  48. #endif // TOOLS_ENABLED
  49. struct AutoloadInfo {
  50. StringName name;
  51. String path;
  52. bool is_singleton = false;
  53. };
  54. protected:
  55. struct VariantContainer {
  56. int order = 0;
  57. bool persist = false;
  58. bool basic = false;
  59. bool internal = false;
  60. Variant variant;
  61. Variant initial;
  62. bool hide_from_editor = false;
  63. bool restart_if_changed = false;
  64. #ifdef DEBUG_METHODS_ENABLED
  65. bool ignore_value_in_docs = false;
  66. #endif
  67. VariantContainer() {}
  68. VariantContainer(const Variant &p_variant, int p_order, bool p_persist = false) :
  69. order(p_order),
  70. persist(p_persist),
  71. variant(p_variant) {
  72. }
  73. };
  74. int last_order = NO_BUILTIN_ORDER_BASE;
  75. int last_builtin_order = 0;
  76. uint64_t last_save_time = 0;
  77. RBMap<StringName, VariantContainer> props; // NOTE: Key order is used e.g. in the save_custom method.
  78. String resource_path;
  79. HashMap<StringName, PropertyInfo> custom_prop_info;
  80. bool using_datapack = false;
  81. bool project_loaded = false;
  82. List<String> input_presets;
  83. HashSet<String> custom_features;
  84. HashMap<StringName, LocalVector<Pair<StringName, StringName>>> feature_overrides;
  85. LocalVector<String> hidden_prefixes;
  86. HashMap<StringName, AutoloadInfo> autoloads;
  87. HashMap<StringName, String> global_groups;
  88. HashMap<StringName, HashSet<StringName>> scene_groups_cache;
  89. Array global_class_list;
  90. bool is_global_class_list_loaded = false;
  91. String project_data_dir_name;
  92. bool _set(const StringName &p_name, const Variant &p_value);
  93. bool _get(const StringName &p_name, Variant &r_ret) const;
  94. void _get_property_list(List<PropertyInfo> *p_list) const;
  95. bool _property_can_revert(const StringName &p_name) const;
  96. bool _property_get_revert(const StringName &p_name, Variant &r_property) const;
  97. void _queue_changed();
  98. void _emit_changed();
  99. static ProjectSettings *singleton;
  100. Error _load_settings_text(const String &p_path);
  101. Error _load_settings_binary(const String &p_path);
  102. Error _load_settings_text_or_binary(const String &p_text_path, const String &p_bin_path);
  103. 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());
  104. 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());
  105. Error _save_custom_bnd(const String &p_file);
  106. #ifdef TOOLS_ENABLED
  107. const static PackedStringArray _get_supported_features();
  108. const static PackedStringArray _trim_to_supported_features(const PackedStringArray &p_project_features);
  109. #endif // TOOLS_ENABLED
  110. void _convert_to_last_version(int p_from_version);
  111. bool load_resource_pack(const String &p_pack, bool p_replace_files, int p_offset);
  112. bool _load_resource_pack(const String &p_pack, bool p_replace_files = true, int p_offset = 0, bool p_main_pack = false);
  113. void _add_property_info_bind(const Dictionary &p_info);
  114. Error _setup(const String &p_path, const String &p_main_pack, bool p_upwards = false, bool p_ignore_override = false);
  115. void _add_builtin_input_map();
  116. protected:
  117. static void _bind_methods();
  118. public:
  119. static const int CONFIG_VERSION = 5;
  120. void set_setting(const String &p_setting, const Variant &p_value);
  121. Variant get_setting(const String &p_setting, const Variant &p_default_value = Variant()) const;
  122. TypedArray<Dictionary> get_global_class_list();
  123. void refresh_global_class_list();
  124. void store_global_class_list(const Array &p_classes);
  125. String get_global_class_list_path() const;
  126. bool has_setting(const String &p_var) const;
  127. String localize_path(const String &p_path) const;
  128. String globalize_path(const String &p_path) const;
  129. void set_initial_value(const String &p_name, const Variant &p_value);
  130. void set_as_basic(const String &p_name, bool p_basic);
  131. void set_as_internal(const String &p_name, bool p_internal);
  132. void set_restart_if_changed(const String &p_name, bool p_restart);
  133. void set_ignore_value_in_docs(const String &p_name, bool p_ignore);
  134. bool get_ignore_value_in_docs(const String &p_name) const;
  135. void add_hidden_prefix(const String &p_prefix);
  136. String get_project_data_dir_name() const;
  137. String get_project_data_path() const;
  138. String get_resource_path() const;
  139. String get_imported_files_path() const;
  140. static ProjectSettings *get_singleton();
  141. void clear(const String &p_name);
  142. int get_order(const String &p_name) const;
  143. void set_order(const String &p_name, int p_order);
  144. void set_builtin_order(const String &p_name);
  145. bool is_builtin_setting(const String &p_name) const;
  146. Error setup(const String &p_path, const String &p_main_pack, bool p_upwards = false, bool p_ignore_override = false);
  147. Error load_custom(const String &p_path);
  148. 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);
  149. Error save();
  150. void set_custom_property_info(const PropertyInfo &p_info);
  151. const HashMap<StringName, PropertyInfo> &get_custom_property_info() const;
  152. uint64_t get_last_saved_time() { return last_save_time; }
  153. List<String> get_input_presets() const { return input_presets; }
  154. Variant get_setting_with_override(const StringName &p_name) const;
  155. bool is_using_datapack() const;
  156. bool is_project_loaded() const;
  157. bool has_custom_feature(const String &p_feature) const;
  158. const HashMap<StringName, AutoloadInfo> &get_autoload_list() const;
  159. void add_autoload(const AutoloadInfo &p_autoload);
  160. void remove_autoload(const StringName &p_autoload);
  161. bool has_autoload(const StringName &p_autoload) const;
  162. AutoloadInfo get_autoload(const StringName &p_name) const;
  163. const HashMap<StringName, String> &get_global_groups_list() const;
  164. void add_global_group(const StringName &p_name, const String &p_description);
  165. void remove_global_group(const StringName &p_name);
  166. bool has_global_group(const StringName &p_name) const;
  167. const HashMap<StringName, HashSet<StringName>> &get_scene_groups_cache() const;
  168. void add_scene_groups_cache(const StringName &p_path, const HashSet<StringName> &p_cache);
  169. void remove_scene_groups_cache(const StringName &p_path);
  170. void save_scene_groups_cache();
  171. String get_scene_groups_cache_path() const;
  172. void load_scene_groups_cache();
  173. #ifdef TOOLS_ENABLED
  174. virtual void get_argument_options(const StringName &p_function, int p_idx, List<String> *r_options) const override;
  175. #endif
  176. ProjectSettings();
  177. ProjectSettings(const String &p_path);
  178. ~ProjectSettings();
  179. };
  180. // Not a macro any longer.
  181. 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);
  182. 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);
  183. #define GLOBAL_DEF(m_var, m_value) _GLOBAL_DEF(m_var, m_value)
  184. #define GLOBAL_DEF_RST(m_var, m_value) _GLOBAL_DEF(m_var, m_value, true)
  185. #define GLOBAL_DEF_NOVAL(m_var, m_value) _GLOBAL_DEF(m_var, m_value, false, true)
  186. #define GLOBAL_DEF_RST_NOVAL(m_var, m_value) _GLOBAL_DEF(m_var, m_value, true, true)
  187. #define GLOBAL_GET(m_var) ProjectSettings::get_singleton()->get_setting_with_override(m_var)
  188. #define GLOBAL_DEF_BASIC(m_var, m_value) _GLOBAL_DEF(m_var, m_value, false, false, true)
  189. #define GLOBAL_DEF_RST_BASIC(m_var, m_value) _GLOBAL_DEF(m_var, m_value, true, false, true)
  190. #define GLOBAL_DEF_NOVAL_BASIC(m_var, m_value) _GLOBAL_DEF(m_var, m_value, false, true, true)
  191. #define GLOBAL_DEF_RST_NOVAL_BASIC(m_var, m_value) _GLOBAL_DEF(m_var, m_value, true, true, true)
  192. #define GLOBAL_DEF_INTERNAL(m_var, m_value) _GLOBAL_DEF(m_var, m_value, false, false, false, true)
  193. #endif // PROJECT_SETTINGS_H