#include "property_utils.h"
#include "core/config/engine.h"
#include "core/object/script_language.h"
#include "core/templates/local_vector.h"
#include "scene/resources/packed_scene.h"
#ifdef TOOLS_ENABLED
#include "editor/editor_node.h"
#endif
bool PropertyUtils::is_property_value_different(const Object *p_object, const Variant &p_a, const Variant &p_b) {
if (p_a.get_type() == Variant::FLOAT && p_b.get_type() == Variant::FLOAT) {
return !Math::is_equal_approx((float)p_a, (float)p_b);
} else if (p_a.get_type() == Variant::NODE_PATH && p_b.get_type() == Variant::OBJECT) {
const Node *base_node = Object::cast_to<Node>(p_object);
const Node *target_node = Object::cast_to<Node>(p_b);
if (base_node && target_node) {
return p_a != base_node->get_path_to(target_node);
}
}
if (p_a.get_type() == Variant::ARRAY && p_b.get_type() == Variant::ARRAY) {
const Node *base_node = Object::cast_to<Node>(p_object);
Array array1 = p_a;
Array array2 = p_b;
if (base_node && !array1.is_empty() && array2.size() == array1.size() && array1[0].get_type() == Variant::NODE_PATH && array2[0].get_type() == Variant::OBJECT) {
for (int i = 0; i < array1.size(); i++) {
const Node *target_node = Object::cast_to<Node>(array2[i]);
if (!target_node || array1[i] != base_node->get_path_to(target_node)) {
return true;
}
}
return false;
}
}
const Variant &a = p_a.get_type() == Variant::OBJECT && (Object *)p_a == nullptr ? Variant() : p_a;
const Variant &b = p_b.get_type() == Variant::OBJECT && (Object *)p_b == nullptr ? Variant() : p_b;
return a != b;
}
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) {
if (r_is_class_default) {
*r_is_class_default = false;
}
if (r_is_valid) {
*r_is_valid = false;
}
if (!p_states_stack_cache && p_property == CoreStringName(script) && p_object->has_meta(SceneStringName(_custom_type_script))) {
Ref<Script> ct_scr = get_custom_type_script(p_object);
if (r_is_valid) {
*r_is_valid = true;
}
return ct_scr;
}
Ref<Script> topmost_script;
if (const Node *node = Object::cast_to<Node>(p_object)) {
const Vector<SceneState::PackState> &states_stack = p_states_stack_cache ? *p_states_stack_cache : PropertyUtils::get_node_states_stack(node, p_owner);
for (int i = 0; i < states_stack.size(); ++i) {
const SceneState::PackState &ia = states_stack[i];
bool found = false;
bool node_deferred = false;
Variant value_in_ancestor = ia.state->get_property_value(ia.node, p_property, found, node_deferred);
if (found) {
if (r_is_valid) {
*r_is_valid = true;
}
if (node_deferred) {
if (value_in_ancestor.get_type() == Variant::ARRAY) {
Array paths = value_in_ancestor;
bool valid = false;
Array array = node->get(p_property, &valid);
ERR_CONTINUE(!valid);
array = array.duplicate();
array.resize(paths.size());
for (int j = 0; j < array.size(); j++) {
array.set(j, node->get_node_or_null(paths[j]));
}
value_in_ancestor = array;
} else {
value_in_ancestor = node->get_node_or_null(value_in_ancestor);
}
}
return value_in_ancestor;
}
bool has_script = false;
Variant script = ia.state->get_property_value(ia.node, SNAME("script"), has_script, node_deferred);
if (has_script) {
Ref<Script> scr = script;
if (scr.is_valid()) {
topmost_script = scr;
}
}
}
}
if (topmost_script.is_null()) {
topmost_script = p_object->get_script();
}
if (topmost_script.is_valid()) {
if (p_update_exports && Engine::get_singleton()->is_editor_hint()) {
topmost_script->update_exports();
}
Variant default_value;
if (topmost_script->get_property_default_value(p_property, default_value)) {
if (r_is_valid) {
*r_is_valid = true;
}
return default_value;
}
}
{
if (r_is_class_default) {
*r_is_class_default = true;
}
bool valid = false;
Variant value = ClassDB::class_get_default_property_value(p_object->get_class_name(), p_property, &valid);
if (valid) {
if (r_is_valid) {
*r_is_valid = true;
}
return value;
} else {
String prop_str = String(p_property);
int p = prop_str.rfind_char('/');
if (p != -1 && p < prop_str.length() - 1) {
bool all_digits = true;
for (int i = p + 1; i < prop_str.length(); i++) {
if (!is_digit(prop_str[i])) {
all_digits = false;
break;
}
}
if (r_is_valid) {
*r_is_valid = all_digits;
}
}
return Variant();
}
}
}
namespace {
struct _FastPackState {
SceneState *state = nullptr;
int node = -1;
};
}
static bool _collect_inheritance_chain(const Ref<SceneState> &p_state, const NodePath &p_path, LocalVector<_FastPackState> &r_states_stack) {
bool found = false;
LocalVector<_FastPackState> inheritance_states;
Ref<SceneState> state = p_state;
while (state.is_valid()) {
int node = state->find_node_by_path(p_path);
if (node >= 0) {
inheritance_states.push_back({ state.ptr(), node });
found = true;
}
state = state->get_base_scene_state();
}
if (inheritance_states.size() > 0) {
for (int i = inheritance_states.size() - 1; i >= 0; --i) {
r_states_stack.push_back(inheritance_states[i]);
}
}
return found;
}
Vector<SceneState::PackState> PropertyUtils::get_node_states_stack(const Node *p_node, const Node *p_owner, bool *r_instantiated_by_owner) {
if (r_instantiated_by_owner) {
*r_instantiated_by_owner = true;
}
LocalVector<_FastPackState> states_stack;
{
const Node *owner = p_owner;
#ifdef TOOLS_ENABLED
if (!p_owner && Engine::get_singleton()->is_editor_hint()) {
owner = EditorNode::get_singleton()->get_edited_scene();
}
#endif
const Node *n = p_node;
while (n) {
if (n == owner) {
const Ref<SceneState> &state = n->get_scene_inherited_state();
if (_collect_inheritance_chain(state, n->get_path_to(p_node), states_stack)) {
if (r_instantiated_by_owner) {
*r_instantiated_by_owner = false;
}
}
break;
} else if (!n->get_scene_file_path().is_empty()) {
const Ref<SceneState> &state = n->get_scene_instance_state();
_collect_inheritance_chain(state, n->get_path_to(p_node), states_stack);
}
n = n->get_owner();
}
}
Vector<SceneState::PackState> states_stack_ret;
{
states_stack_ret.resize(states_stack.size());
_FastPackState *ps = states_stack.ptr();
if (states_stack.size() > 0) {
for (int i = states_stack.size() - 1; i >= 0; --i) {
states_stack_ret.write[i].state.reference_ptr(ps->state);
states_stack_ret.write[i].node = ps->node;
++ps;
}
}
}
return states_stack_ret;
}
void PropertyUtils::assign_custom_type_script(Object *p_object, const Ref<Script> &p_script) {
ERR_FAIL_NULL(p_object);
ERR_FAIL_COND(p_script.is_null());
const String &path = p_script->get_path();
ERR_FAIL_COND(!path.is_resource_file());
ResourceUID::ID script_uid = ResourceLoader::get_resource_uid(path);
if (script_uid != ResourceUID::INVALID_ID) {
p_object->set_meta(SceneStringName(_custom_type_script), ResourceUID::get_singleton()->id_to_text(script_uid));
}
}
Ref<Script> PropertyUtils::get_custom_type_script(const Object *p_object) {
Variant custom_script = p_object->get_meta(SceneStringName(_custom_type_script));
#ifndef DISABLE_DEPRECATED
if (custom_script.get_type() == Variant::OBJECT) {
Ref<Script> script_object(custom_script);
assign_custom_type_script(const_cast<Object *>(p_object), script_object);
return script_object;
}
#endif
ResourceUID::ID id = ResourceUID::get_singleton()->text_to_id(custom_script);
if (unlikely(id == ResourceUID::INVALID_ID || !ResourceUID::get_singleton()->has_id(id))) {
const_cast<Object *>(p_object)->remove_meta(SceneStringName(_custom_type_script));
ERR_FAIL_V_MSG(Ref<Script>(), vformat("Invalid custom type script UID: %s. Removing.", custom_script.operator String()));
} else {
custom_script = ResourceUID::get_singleton()->get_id_path(id);
}
return ResourceLoader::load(custom_script);
}