project_settings.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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.h"
  33. #include "core/os/thread_safe.h"
  34. #include "core/set.h"
  35. // Querying ProjectSettings is usually done at startup.
  36. // Additionally, in order to keep track of changes to ProjectSettings,
  37. // instead of Querying all the strings every frame just in case of changes,
  38. // there is a signal "project_settings_changed" which objects can subscribe to.
  39. // E.g. (from another Godot object)
  40. // // Call your user written object function to Query the project settings once at creation,
  41. // perhaps in an ENTER_TREE notification:
  42. // _project_settings_changed()
  43. // // Then connect your function to the signal so it is called every time something changes in future:
  44. // ProjectSettings::get_singleton()->connect("project_settings_changed", this, "_project_settings_changed");
  45. // Where for example your function may take the form:
  46. // void _project_settings_changed() {
  47. // _shadowmap_size = GLOBAL_GET("rendering/quality/shadow_atlas/size");
  48. // }
  49. // You may want to also disconnect from the signal in EXIT_TREE notification, if your object may be deleted
  50. // before ProjectSettings:
  51. // ProjectSettings::get_singleton()->disconnect("project_settings_changed", this, "_project_settings_changed");
  52. // Additionally, for objects that are not regular Godot objects capable of subscribing to signals (e.g. Rasterizers),
  53. // you can also query the function "has_changes()" each frame,
  54. // and update your local settings whenever this is set.
  55. class ProjectSettings : public Object {
  56. GDCLASS(ProjectSettings, Object);
  57. _THREAD_SAFE_CLASS_
  58. int _dirty_this_frame = 2;
  59. public:
  60. typedef Map<String, Variant> CustomMap;
  61. static const String PROJECT_DATA_DIR_NAME_SUFFIX;
  62. enum {
  63. //properties that are not for built in values begin from this value, so builtin ones are displayed first
  64. NO_BUILTIN_ORDER_BASE = 1 << 16
  65. };
  66. protected:
  67. struct VariantContainer {
  68. int order;
  69. bool persist;
  70. Variant variant;
  71. Variant initial;
  72. bool hide_from_editor;
  73. bool overridden;
  74. bool restart_if_changed;
  75. #ifdef DEBUG_METHODS_ENABLED
  76. bool ignore_value_in_docs = false;
  77. #endif
  78. VariantContainer() :
  79. order(0),
  80. persist(false),
  81. hide_from_editor(false),
  82. overridden(false),
  83. restart_if_changed(false) {
  84. }
  85. VariantContainer(const Variant &p_variant, int p_order, bool p_persist = false) :
  86. order(p_order),
  87. persist(p_persist),
  88. variant(p_variant),
  89. hide_from_editor(false),
  90. overridden(false),
  91. restart_if_changed(false) {
  92. }
  93. };
  94. bool registering_order;
  95. int last_order;
  96. int last_builtin_order;
  97. uint64_t last_save_time = 0;
  98. Map<StringName, VariantContainer> props;
  99. String resource_path;
  100. Map<StringName, PropertyInfo> custom_prop_info;
  101. bool disable_feature_overrides;
  102. bool using_datapack;
  103. List<String> input_presets;
  104. Set<String> custom_features;
  105. Map<StringName, StringName> feature_overrides;
  106. String project_data_dir_name;
  107. bool _set(const StringName &p_name, const Variant &p_value);
  108. bool _get(const StringName &p_name, Variant &r_ret) const;
  109. void _get_property_list(List<PropertyInfo> *p_list) const;
  110. static ProjectSettings *singleton;
  111. Error _load_settings_text(const String &p_path);
  112. Error _load_settings_binary(const String &p_path);
  113. Error _load_settings_text_or_binary(const String &p_text_path, const String &p_bin_path);
  114. Error _save_settings_text(const String &p_file, const Map<String, List<String>> &props, const CustomMap &p_custom = CustomMap(), const String &p_custom_features = String());
  115. Error _save_settings_binary(const String &p_file, const Map<String, List<String>> &props, const CustomMap &p_custom = CustomMap(), const String &p_custom_features = String());
  116. Error _save_custom_bnd(const String &p_file);
  117. void _convert_to_last_version(int p_from_version);
  118. bool _load_resource_pack(const String &p_pack, bool p_replace_files = true, int p_offset = 0);
  119. void _add_property_info_bind(const Dictionary &p_info);
  120. Error _setup(const String &p_path, const String &p_main_pack, bool p_upwards = false, bool p_ignore_override = false);
  121. static void _bind_methods();
  122. public:
  123. static const int CONFIG_VERSION = 4;
  124. void set_setting(const String &p_setting, const Variant &p_value);
  125. Variant get_setting(const String &p_setting) const;
  126. bool has_setting(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_restart_if_changed(const String &p_name, bool p_restart);
  131. void set_hide_from_editor(const String &p_name, bool p_hide_from_editor);
  132. void set_ignore_value_in_docs(const String &p_name, bool p_ignore);
  133. bool get_ignore_value_in_docs(const String &p_name) const;
  134. bool property_can_revert(const String &p_name);
  135. Variant property_get_revert(const String &p_name);
  136. String get_project_data_dir_name() const;
  137. String get_project_data_path() const;
  138. String get_resource_path() const;
  139. static ProjectSettings *get_singleton();
  140. void clear(const String &p_name);
  141. int get_order(const String &p_name) const;
  142. void set_order(const String &p_name, int p_order);
  143. void set_builtin_order(const String &p_name);
  144. Error setup(const String &p_path, const String &p_main_pack, bool p_upwards = false, bool p_ignore_override = false);
  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 String &p_prop, const PropertyInfo &p_info);
  148. const Map<StringName, PropertyInfo> &get_custom_property_info() const;
  149. uint64_t get_last_saved_time() { return last_save_time; }
  150. Vector<String> get_optimizer_presets() const;
  151. List<String> get_input_presets() const { return input_presets; }
  152. void set_disable_feature_overrides(bool p_disable);
  153. bool is_using_datapack() const;
  154. void set_registering_order(bool p_enable);
  155. bool has_custom_feature(const String &p_feature) const;
  156. // Either use the signal `project_settings_changed` or query this function.
  157. // N.B. _dirty_this_frame is set initially to 2.
  158. // This is to cope with the situation where a project setting is changed in the iteration AFTER it is read.
  159. // There is therefore the potential for a change to be missed. Persisting the counter
  160. // for two frames avoids this, at the cost of a frame delay.
  161. bool has_changes() const { return _dirty_this_frame == 1; }
  162. void update();
  163. ProjectSettings();
  164. ~ProjectSettings();
  165. };
  166. //not a macro any longer
  167. Variant _GLOBAL_DEF(const String &p_var, const Variant &p_default, bool p_restart_if_changed = false, bool p_ignore_value_in_docs = false);
  168. Variant _GLOBAL_DEF_ALIAS(const String &p_var, const String &p_old_name, const Variant &p_default, bool p_restart_if_changed = false);
  169. #define GLOBAL_DEF(m_var, m_value) _GLOBAL_DEF(m_var, m_value)
  170. #define GLOBAL_DEF_RST(m_var, m_value) _GLOBAL_DEF(m_var, m_value, true)
  171. #define GLOBAL_DEF_NOVAL(m_var, m_value) _GLOBAL_DEF(m_var, m_value, false, true)
  172. #define GLOBAL_DEF_RST_NOVAL(m_var, m_value) _GLOBAL_DEF(m_var, m_value, true, true)
  173. #define GLOBAL_DEF_ALIAS(m_var, m_old_name, m_value) _GLOBAL_DEF_ALIAS(m_var, m_old_name, m_value)
  174. #define GLOBAL_DEF_ALIAS_RST(m_var, m_old_name, m_value) _GLOBAL_DEF(m_var, m_old_name, m_value, true)
  175. #define GLOBAL_GET(m_var) ProjectSettings::get_singleton()->get(m_var)
  176. #endif // PROJECT_SETTINGS_H