property_utils.cpp 12 KB

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