Debian Codegraph @

scene/property_utils.cpp

raw symbols SCIP
1 /**************************************************************************/
2 /* property_utils.cpp */
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
31 #include "property_utils.h"
32
33 #include "core/config/engine.h"
34 #include "core/io/resource_loader.h"
35 #include "core/object/script_language.h"
36 #include "core/templates/local_vector.h"
37 #include "scene/resources/packed_scene.h"
38
39 #ifdef TOOLS_ENABLED
40 #include "editor/editor_node.h"
41 #endif // TOOLS_ENABLED
42
43 bool PropertyUtils::is_property_value_different(const Object *p_object, const Variant &p_a, const Variant &p_b) {
44 if (p_a.get_type() == Variant::FLOAT && p_b.get_type() == Variant::FLOAT) {
45 // This must be done because, as some scenes save as text, there might be a tiny difference in floats due to numerical error.
46 return !Math::is_equal_approx((float)p_a, (float)p_b);
47 } else if (p_a.get_type() == Variant::NODE_PATH && p_b.get_type() == Variant::OBJECT) {
48 // With properties of type Node, left side is NodePath, while right side is Node.
49 const Node *base_node = Object::cast_to<Node>(p_object);
50 const Node *target_node = Object::cast_to<Node>(p_b);
51 if (base_node && target_node) {
52 return p_a != base_node->get_path_to(target_node);
53 }
54 }
55
56 if (p_a.get_type() == Variant::ARRAY && p_b.get_type() == Variant::ARRAY) {
57 const Node *base_node = Object::cast_to<Node>(p_object);
58 Array array1 = p_a;
59 Array array2 = p_b;
60
61 if (base_node && !array1.is_empty() && array2.size() == array1.size() && array1[0].get_type() == Variant::NODE_PATH && array2[0].get_type() == Variant::OBJECT) {
62 // Like above, but NodePaths/Nodes are inside arrays.
63 for (int i = 0; i < array1.size(); i++) {
64 const Node *target_node = Object::cast_to<Node>(array2[i]);
65 if (!target_node || array1[i] != base_node->get_path_to(target_node)) {
66 return true;
67 }
68 }
69 return false;
70 }
71 }
72
73 // For our purposes, treating null object as NIL is the right thing to do
74 const Variant &a = p_a.get_type() == Variant::OBJECT && (Object *)p_a == nullptr ? Variant() : p_a;
75 const Variant &b = p_b.get_type() == Variant::OBJECT && (Object *)p_b == nullptr ? Variant() : p_b;
76 return a != b;
77 }
78
79 Variant PropertyUtils::get_property_default_value(const Object *p_object, const StringName &p_property, bool *r_is_valid, const Vector<SceneState::PackState> *p_states_stack_cache, bool p_update_exports, const Node *p_owner, bool *r_is_class_default) {
80 // This function obeys the way property values are set when an object is instantiated,
81 // which is the following (the latter wins):
82 // 1. Default value from builtin class
83 // 2. Default value from script exported variable (from the topmost script)
84 // 3. Value overrides from the instantiation/inheritance stack
85
86 if (r_is_class_default) {
87 *r_is_class_default = false;
88 }
89 if (r_is_valid) {
90 *r_is_valid = false;
91 }
92
93 // Handle special case "script" property, where the default value is either null or the custom type script.
94 // Do this only if there's no states stack cache to trace for default values.
95 if (!p_states_stack_cache && p_property == CoreStringName(script) && p_object->has_meta(SceneStringName(_custom_type_script))) {
96 Ref<Script> ct_scr = get_custom_type_script(p_object);
97 if (r_is_valid) {
98 *r_is_valid = true;
99 }
100 return ct_scr;
101 }
102
103 Ref<Script> topmost_script;
104
105 if (const Node *node = Object::cast_to<Node>(p_object)) {
106 // Check inheritance/instantiation ancestors
107 const Vector<SceneState::PackState> &states_stack = p_states_stack_cache ? *p_states_stack_cache : PropertyUtils::get_node_states_stack(node, p_owner);
108 for (int i = 0; i < states_stack.size(); ++i) {
109 const SceneState::PackState &ia = states_stack[i];
110 bool found = false;
111 bool node_deferred = false;
112 Variant value_in_ancestor = ia.state->get_property_value(ia.node, p_property, found, node_deferred);
113 if (found) {
114 if (r_is_valid) {
115 *r_is_valid = true;
116 }
117 // Replace properties stored as NodePaths with actual Nodes.
118 // Otherwise, the property value would be considered as overridden.
119 if (node_deferred) {
120 if (value_in_ancestor.get_type() == Variant::ARRAY) {
121 Array paths = value_in_ancestor;
122
123 bool valid = false;
124 Array array = node->get(p_property, &valid);
125 ERR_CONTINUE(!valid);
126 array = array.duplicate();
127
128 array.resize(paths.size());
129 for (int j = 0; j < array.size(); j++) {
130 array.set(j, node->get_node_or_null(paths[j]));
131 }
132 value_in_ancestor = array;
133 } else {
134 value_in_ancestor = node->get_node_or_null(value_in_ancestor);
135 }
136 }
137 return value_in_ancestor;
138 }
139 // Save script for later
140 bool has_script = false;
141 Variant script = ia.state->get_property_value(ia.node, SNAME("script"), has_script, node_deferred);
142 if (has_script) {
143 Ref<Script> scr = script;
144 if (scr.is_valid()) {
145 topmost_script = scr;
146 }
147 }
148 }
149 }
150
151 // Let's see what default is set by the topmost script having a default, if any
152 if (topmost_script.is_null()) {
153 topmost_script = p_object->get_script();
154 }
155 if (topmost_script.is_valid()) {
156 // Should be called in the editor only and not at runtime,
157 // otherwise it can cause problems because of missing instance state support
158 if (p_update_exports && Engine::get_singleton()->is_editor_hint()) {
159 topmost_script->update_exports();
160 }
161 Variant default_value;
162 if (topmost_script->get_property_default_value(p_property, default_value)) {
163 if (r_is_valid) {
164 *r_is_valid = true;
165 }
166 return default_value;
167 }
168 }
169
170 // Fall back to the default from the native class
171 {
172 if (r_is_class_default) {
173 *r_is_class_default = true;
174 }
175 bool valid = false;
176 Variant value = ClassDB::class_get_default_property_value(p_object->get_class_name(), p_property, &valid);
177 if (valid) {
178 if (r_is_valid) {
179 *r_is_valid = true;
180 }
181 return value;
182 } else {
183 // Heuristically check if this is a synthetic property (whatever/0, whatever/1, etc.)
184 // because they are not in the class DB yet must have a default (null).
185 String prop_str = String(p_property);
186 int p = prop_str.rfind_char('/');
187 if (p != -1 && p < prop_str.length() - 1) {
188 bool all_digits = true;
189 for (int i = p + 1; i < prop_str.length(); i++) {
190 if (!is_digit(prop_str[i])) {
191 all_digits = false;
192 break;
193 }
194 }
195 if (r_is_valid) {
196 *r_is_valid = all_digits;
197 }
198 }
199 return Variant();
200 }
201 }
202 }
203
204 // Like SceneState::PackState, but using a raw pointer to avoid the cost of
205 // updating the reference count during the internal work of the functions below
206 namespace {
207 struct _FastPackState {
208 SceneState *state = nullptr;
209 int node = -1;
210 };
211 } // namespace
212
213 static bool _collect_inheritance_chain(const Ref<SceneState> &p_state, const NodePath &p_path, LocalVector<_FastPackState> &r_states_stack) {
214 bool found = false;
215
216 LocalVector<_FastPackState> inheritance_states;
217
218 Ref<SceneState> state = p_state;
219 while (state.is_valid()) {
220 int node = state->find_node_by_path(p_path);
221 if (node >= 0) {
222 // This one has state for this node
223 inheritance_states.push_back({ state.ptr(), node });
224 found = true;
225 }
226 state = state->get_base_scene_state();
227 }
228
229 if (inheritance_states.size() > 0) {
230 for (int i = inheritance_states.size() - 1; i >= 0; --i) {
231 r_states_stack.push_back(inheritance_states[i]);
232 }
233 }
234
235 return found;
236 }
237
238 Vector<SceneState::PackState> PropertyUtils::get_node_states_stack(const Node *p_node, const Node *p_owner, bool *r_instantiated_by_owner) {
239 if (r_instantiated_by_owner) {
240 *r_instantiated_by_owner = true;
241 }
242
243 LocalVector<_FastPackState> states_stack;
244 {
245 const Node *owner = p_owner;
246 #ifdef TOOLS_ENABLED
247 if (!p_owner && Engine::get_singleton()->is_editor_hint()) {
248 owner = EditorNode::get_singleton()->get_edited_scene();
249 }
250 #endif
251
252 const Node *n = p_node;
253 while (n) {
254 if (n == owner) {
255 const Ref<SceneState> &state = n->get_scene_inherited_state();
256 if (_collect_inheritance_chain(state, n->get_path_to(p_node), states_stack)) {
257 if (r_instantiated_by_owner) {
258 *r_instantiated_by_owner = false;
259 }
260 }
261 break;
262 } else if (n->is_instance()) {
263 const Ref<SceneState> &state = n->get_scene_instance_state();
264 _collect_inheritance_chain(state, n->get_path_to(p_node), states_stack);
265 }
266 n = n->get_owner();
267 }
268 }
269
270 // Convert to the proper type for returning, inverting the vector on the go
271 // (it was more convenient to fill the vector in reverse order)
272 Vector<SceneState::PackState> states_stack_ret;
273 {
274 states_stack_ret.resize(states_stack.size());
275 _FastPackState *ps = states_stack.ptr();
276 if (states_stack.size() > 0) {
277 for (int i = states_stack.size() - 1; i >= 0; --i) {
278 states_stack_ret.write[i].state.reference_ptr(ps->state);
279 states_stack_ret.write[i].node = ps->node;
280 ++ps;
281 }
282 }
283 }
284 return states_stack_ret;
285 }
286
287 void PropertyUtils::assign_custom_type_script(Object *p_object, const Ref<Script> &p_script) {
288 ERR_FAIL_NULL(p_object);
289 ERR_FAIL_COND(p_script.is_null());
290
291 const String &path = p_script->get_path();
292 ERR_FAIL_COND(!path.is_resource_file());
293
294 ResourceUID::ID script_uid = ResourceLoader::get_resource_uid(path);
295 if (script_uid != ResourceUID::INVALID_ID) {
296 p_object->set_meta(SceneStringName(_custom_type_script), ResourceUID::get_singleton()->id_to_text(script_uid));
297 }
298 }
299
300 Ref<Script> PropertyUtils::get_custom_type_script(const Object *p_object) {
301 Variant custom_script = p_object->get_meta(SceneStringName(_custom_type_script));
302 #ifndef DISABLE_DEPRECATED
303 if (custom_script.get_type() == Variant::OBJECT) {
304 // Convert old script meta.
305 Ref<Script> script_object(custom_script);
306 assign_custom_type_script(const_cast<Object *>(p_object), script_object);
307 return script_object;
308 }
309 #endif
310 ResourceUID::ID id = ResourceUID::get_singleton()->text_to_id(custom_script);
311 if (unlikely(id == ResourceUID::INVALID_ID || !ResourceUID::get_singleton()->has_id(id))) {
312 const_cast<Object *>(p_object)->remove_meta(SceneStringName(_custom_type_script));
313 ERR_FAIL_V_MSG(Ref<Script>(), vformat("Invalid custom type script UID: %s. Removing.", custom_script.operator String()));
314 } else {
315 custom_script = ResourceUID::get_singleton()->get_id_path(id);
316 }
317 return ResourceLoader::load(custom_script);
318 }