project_settings.h 9.5 KB

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