globals.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*************************************************************************/
  2. /* globals.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
  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 GLOBALS_H
  31. #define GLOBALS_H
  32. #include "object.h"
  33. #include "os/thread_safe.h"
  34. #include "set.h"
  35. /**
  36. @author Juan Linietsky <reduzio@gmail.com>
  37. */
  38. class Globals : public Object {
  39. OBJ_TYPE(Globals, Object);
  40. _THREAD_SAFE_CLASS_
  41. public:
  42. typedef Map<String, Variant> CustomMap;
  43. struct Singleton {
  44. StringName name;
  45. Object *ptr;
  46. Singleton(const StringName &p_name = StringName(), Object *p_ptr = NULL) {
  47. name = p_name;
  48. ptr = p_ptr;
  49. }
  50. };
  51. protected:
  52. enum {
  53. NO_ORDER_BASE = 1 << 18
  54. };
  55. struct VariantContainer {
  56. int order;
  57. bool persist;
  58. Variant variant;
  59. bool hide_from_editor;
  60. bool overrided;
  61. VariantContainer() {
  62. order = 0;
  63. hide_from_editor = false;
  64. persist = false;
  65. overrided = false;
  66. }
  67. VariantContainer(const Variant &p_variant, int p_order, bool p_persist = false) {
  68. variant = p_variant;
  69. order = p_order;
  70. hide_from_editor = false;
  71. persist = p_persist;
  72. overrided = false;
  73. }
  74. };
  75. bool registering_order;
  76. int last_order;
  77. Map<StringName, VariantContainer> props;
  78. String resource_path;
  79. Map<StringName, PropertyInfo> custom_prop_info;
  80. bool disable_platform_override;
  81. bool using_datapack;
  82. List<String> input_presets;
  83. bool _set(const StringName &p_name, const Variant &p_value);
  84. bool _get(const StringName &p_name, Variant &r_ret) const;
  85. void _get_property_list(List<PropertyInfo> *p_list) const;
  86. static Globals *singleton;
  87. Error _load_settings(const String p_path);
  88. Error _load_settings_binary(const String p_path);
  89. Error _save_settings_text(const String &p_file, const Map<String, List<String> > &props, const CustomMap &p_custom = CustomMap());
  90. Error _save_settings_binary(const String &p_file, const Map<String, List<String> > &props, const CustomMap &p_custom = CustomMap());
  91. List<Singleton> singletons;
  92. Error _save_custom_bnd(const String &p_file);
  93. bool _load_resource_pack(const String &p_pack);
  94. void _add_property_info_bind(const Dictionary &p_info);
  95. protected:
  96. static void _bind_methods();
  97. public:
  98. bool has(String p_var) const;
  99. String localize_path(const String &p_path) const;
  100. String globalize_path(const String &p_path) const;
  101. void set_persisting(const String &p_name, bool p_persist);
  102. bool is_persisting(const String &p_name) const;
  103. String get_resource_path() const;
  104. static Globals *get_singleton();
  105. void clear(const String &p_name);
  106. int get_order(const String &p_name) const;
  107. void set_order(const String &p_name, int p_order);
  108. Error setup(const String &p_path, const String &p_main_pack);
  109. Error save_custom(const String &p_path = "", const CustomMap &p_custom = CustomMap(), const Set<String> &p_ignore_masks = Set<String>());
  110. Error save();
  111. void set_custom_property_info(const String &p_prop, const PropertyInfo &p_info);
  112. void add_singleton(const Singleton &p_singleton);
  113. void get_singletons(List<Singleton> *p_singletons);
  114. bool has_singleton(const String &p_name) const;
  115. Vector<String> get_optimizer_presets() const;
  116. List<String> get_input_presets() const { return input_presets; }
  117. void set_disable_platform_override(bool p_disable);
  118. Object *get_singleton_object(const String &p_name) const;
  119. void register_global_defaults();
  120. bool is_using_datapack() const;
  121. void set_registering_order(bool p_registering);
  122. Globals();
  123. ~Globals();
  124. };
  125. //not a macro any longer
  126. Variant _GLOBAL_DEF(const String &p_var, const Variant &p_default);
  127. #define GLOBAL_DEF(m_var, m_value) _GLOBAL_DEF(m_var, m_value)
  128. #endif