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