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