/*************************************************************************/ /* object.cpp */ /*************************************************************************/ /* This file is part of: */ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */ /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ /* "Software"), to deal in the Software without restriction, including */ /* without limitation the rights to use, copy, modify, merge, publish, */ /* distribute, sublicense, and/or sell copies of the Software, and to */ /* permit persons to whom the Software is furnished to do so, subject to */ /* the following conditions: */ /* */ /* The above copyright notice and this permission notice shall be */ /* included in all copies or substantial portions of the Software. */ /* */ /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************************/ #include "object.h" #include "core/core_string_names.h" #include "core/io/resource.h" #include "core/object/class_db.h" #include "core/object/message_queue.h" #include "core/object/script_language.h" #include "core/os/os.h" #include "core/string/print_string.h" #include "core/string/translation.h" #ifdef DEBUG_ENABLED struct _ObjectDebugLock { Object *obj; _ObjectDebugLock(Object *p_obj) { obj = p_obj; obj->_lock_index.ref(); } ~_ObjectDebugLock() { obj->_lock_index.unref(); } }; #define OBJ_DEBUG_LOCK _ObjectDebugLock _debug_lock(this); #else #define OBJ_DEBUG_LOCK #endif PropertyInfo::operator Dictionary() const { Dictionary d; d["name"] = name; d["class_name"] = class_name; d["type"] = type; d["hint"] = hint; d["hint_string"] = hint_string; d["usage"] = usage; return d; } PropertyInfo PropertyInfo::from_dict(const Dictionary &p_dict) { PropertyInfo pi; if (p_dict.has("type")) { pi.type = Variant::Type(int(p_dict["type"])); } if (p_dict.has("name")) { pi.name = p_dict["name"]; } if (p_dict.has("class_name")) { pi.class_name = p_dict["class_name"]; } if (p_dict.has("hint")) { pi.hint = PropertyHint(int(p_dict["hint"])); } if (p_dict.has("hint_string")) { pi.hint_string = p_dict["hint_string"]; } if (p_dict.has("usage")) { pi.usage = p_dict["usage"]; } return pi; } Array convert_property_list(const List *p_list) { Array va; for (const List::Element *E = p_list->front(); E; E = E->next()) { va.push_back(Dictionary(E->get())); } return va; } MethodInfo::operator Dictionary() const { Dictionary d; d["name"] = name; d["args"] = convert_property_list(&arguments); Array da; for (int i = 0; i < default_arguments.size(); i++) { da.push_back(default_arguments[i]); } d["default_args"] = da; d["flags"] = flags; d["id"] = id; Dictionary r = return_val; d["return"] = r; return d; } MethodInfo MethodInfo::from_dict(const Dictionary &p_dict) { MethodInfo mi; if (p_dict.has("name")) { mi.name = p_dict["name"]; } Array args; if (p_dict.has("args")) { args = p_dict["args"]; } for (int i = 0; i < args.size(); i++) { Dictionary d = args[i]; mi.arguments.push_back(PropertyInfo::from_dict(d)); } Array defargs; if (p_dict.has("default_args")) { defargs = p_dict["default_args"]; } for (int i = 0; i < defargs.size(); i++) { mi.default_arguments.push_back(defargs[i]); } if (p_dict.has("return")) { mi.return_val = PropertyInfo::from_dict(p_dict["return"]); } if (p_dict.has("flags")) { mi.flags = p_dict["flags"]; } return mi; } Object::Connection::operator Variant() const { Dictionary d; d["signal"] = signal; d["callable"] = callable; d["flags"] = flags; d["binds"] = binds; return d; } bool Object::Connection::operator<(const Connection &p_conn) const { if (signal == p_conn.signal) { return callable < p_conn.callable; } else { return signal < p_conn.signal; } } Object::Connection::Connection(const Variant &p_variant) { Dictionary d = p_variant; if (d.has("signal")) { signal = d["signal"]; } if (d.has("callable")) { callable = d["callable"]; } if (d.has("flags")) { flags = d["flags"]; } if (d.has("binds")) { binds = d["binds"]; } } bool Object::_predelete() { _predelete_ok = 1; notification(NOTIFICATION_PREDELETE, true); if (_predelete_ok) { _class_ptr = nullptr; //must restore so destructors can access class ptr correctly } return _predelete_ok; } void Object::_postinitialize() { _class_ptr = _get_class_namev(); _initialize_classv(); notification(NOTIFICATION_POSTINITIALIZE); } void Object::get_valid_parents_static(List *p_parents) { } void Object::_get_valid_parents_static(List *p_parents) { } void Object::set(const StringName &p_name, const Variant &p_value, bool *r_valid) { #ifdef TOOLS_ENABLED _edited = true; #endif if (script_instance) { if (script_instance->set(p_name, p_value)) { if (r_valid) { *r_valid = true; } return; } } if (_extension && _extension->set) { // C style pointer casts should never trigger a compiler warning because the risk is assumed by the user, so GCC should keep quiet about it. #if defined(__GNUC__) && !defined(__clang__) #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wignored-qualifiers" #endif if (_extension->set(_extension_instance, (const GDNativeStringNamePtr)&p_name, (const GDNativeVariantPtr)&p_value)) { if (r_valid) { *r_valid = true; } return; } #if defined(__GNUC__) && !defined(__clang__) #pragma GCC diagnostic pop #endif } // Try built-in setter. { if (ClassDB::set_property(this, p_name, p_value, r_valid)) { return; } } if (p_name == CoreStringNames::get_singleton()->_script) { set_script(p_value); if (r_valid) { *r_valid = true; } return; } else { Variant **V = metadata_properties.getptr(p_name); if (V) { **V = p_value; if (r_valid) { *r_valid = true; } return; } else if (p_name.operator String().begins_with("metadata/")) { // Must exist, otherwise duplicate() will not work. set_meta(p_name.operator String().replace_first("metadata/", ""), p_value); if (r_valid) { *r_valid = true; } return; } } #ifdef TOOLS_ENABLED if (script_instance) { bool valid; script_instance->property_set_fallback(p_name, p_value, &valid); if (valid) { if (r_valid) { *r_valid = true; } return; } } #endif // Something inside the object... :| bool success = _setv(p_name, p_value); if (success) { if (r_valid) { *r_valid = true; } return; } if (r_valid) { *r_valid = false; } } Variant Object::get(const StringName &p_name, bool *r_valid) const { Variant ret; if (script_instance) { if (script_instance->get(p_name, ret)) { if (r_valid) { *r_valid = true; } return ret; } } if (_extension && _extension->get) { // C style pointer casts should never trigger a compiler warning because the risk is assumed by the user, so GCC should keep quiet about it. #if defined(__GNUC__) && !defined(__clang__) #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wignored-qualifiers" #endif if (_extension->get(_extension_instance, (const GDNativeStringNamePtr)&p_name, (GDNativeVariantPtr)&ret)) { if (r_valid) { *r_valid = true; } return ret; } #if defined(__GNUC__) && !defined(__clang__) #pragma GCC diagnostic pop #endif } // Try built-in getter. { if (ClassDB::get_property(const_cast(this), p_name, ret)) { if (r_valid) { *r_valid = true; } return ret; } } if (p_name == CoreStringNames::get_singleton()->_script) { ret = get_script(); if (r_valid) { *r_valid = true; } return ret; } const Variant *const *V = metadata_properties.getptr(p_name); if (V) { ret = **V; if (r_valid) { *r_valid = true; } return ret; } else { #ifdef TOOLS_ENABLED if (script_instance) { bool valid; ret = script_instance->property_get_fallback(p_name, &valid); if (valid) { if (r_valid) { *r_valid = true; } return ret; } } #endif // Something inside the object... :| bool success = _getv(p_name, ret); if (success) { if (r_valid) { *r_valid = true; } return ret; } if (r_valid) { *r_valid = false; } return Variant(); } } void Object::set_indexed(const Vector &p_names, const Variant &p_value, bool *r_valid) { if (p_names.is_empty()) { if (r_valid) { *r_valid = false; } return; } if (p_names.size() == 1) { set(p_names[0], p_value, r_valid); return; } bool valid = false; if (!r_valid) { r_valid = &valid; } List value_stack; value_stack.push_back(get(p_names[0], r_valid)); if (!*r_valid) { value_stack.clear(); return; } for (int i = 1; i < p_names.size() - 1; i++) { value_stack.push_back(value_stack.back()->get().get_named(p_names[i], valid)); if (r_valid) { *r_valid = valid; } if (!valid) { value_stack.clear(); return; } } value_stack.push_back(p_value); // p_names[p_names.size() - 1] for (int i = p_names.size() - 1; i > 0; i--) { value_stack.back()->prev()->get().set_named(p_names[i], value_stack.back()->get(), valid); value_stack.pop_back(); if (r_valid) { *r_valid = valid; } if (!valid) { value_stack.clear(); return; } } set(p_names[0], value_stack.back()->get(), r_valid); value_stack.pop_back(); ERR_FAIL_COND(!value_stack.is_empty()); } Variant Object::get_indexed(const Vector &p_names, bool *r_valid) const { if (p_names.is_empty()) { if (r_valid) { *r_valid = false; } return Variant(); } bool valid = false; Variant current_value = get(p_names[0], &valid); for (int i = 1; i < p_names.size(); i++) { current_value = current_value.get_named(p_names[i], valid); if (!valid) { break; } } if (r_valid) { *r_valid = valid; } return current_value; } void Object::get_property_list(List *p_list, bool p_reversed) const { if (script_instance && p_reversed) { p_list->push_back(PropertyInfo(Variant::NIL, "Script Variables", PROPERTY_HINT_NONE, String(), PROPERTY_USAGE_CATEGORY)); script_instance->get_property_list(p_list); } if (_extension) { const ObjectNativeExtension *current_extension = _extension; while (current_extension) { p_list->push_back(PropertyInfo(Variant::NIL, current_extension->class_name, PROPERTY_HINT_NONE, String(), PROPERTY_USAGE_CATEGORY)); ClassDB::get_property_list(current_extension->class_name, p_list, true, this); current_extension = current_extension->parent; } } if (_extension && _extension->get_property_list) { uint32_t pcount; const GDNativePropertyInfo *pinfo = _extension->get_property_list(_extension_instance, &pcount); for (uint32_t i = 0; i < pcount; i++) { p_list->push_back(PropertyInfo(Variant::Type(pinfo[i].type), pinfo[i].class_name, PropertyHint(pinfo[i].hint), pinfo[i].hint_string, pinfo[i].usage, pinfo[i].class_name)); } if (_extension->free_property_list) { _extension->free_property_list(_extension_instance, pinfo); } } _get_property_listv(p_list, p_reversed); if (!is_class("Script")) { // can still be set, but this is for user-friendliness p_list->push_back(PropertyInfo(Variant::OBJECT, "script", PROPERTY_HINT_RESOURCE_TYPE, "Script", PROPERTY_USAGE_DEFAULT)); } if (script_instance && !p_reversed) { p_list->push_back(PropertyInfo(Variant::NIL, "Script Variables", PROPERTY_HINT_NONE, String(), PROPERTY_USAGE_CATEGORY)); script_instance->get_property_list(p_list); } for (const KeyValue &K : metadata) { PropertyInfo pi = PropertyInfo(K.value.get_type(), "metadata/" + K.key.operator String()); if (K.value.get_type() == Variant::OBJECT) { pi.hint = PROPERTY_HINT_RESOURCE_TYPE; pi.hint_string = "Resource"; } p_list->push_back(pi); } } void Object::_validate_property(PropertyInfo &property) const { } void Object::get_method_list(List *p_list) const { ClassDB::get_method_list(get_class_name(), p_list); if (script_instance) { script_instance->get_method_list(p_list); } } Variant Object::_call_bind(const Variant **p_args, int p_argcount, Callable::CallError &r_error) { if (p_argcount < 1) { r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS; r_error.argument = 0; return Variant(); } if (p_args[0]->get_type() != Variant::STRING_NAME && p_args[0]->get_type() != Variant::STRING) { r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT; r_error.argument = 0; r_error.expected = Variant::STRING_NAME; return Variant(); } StringName method = *p_args[0]; return callp(method, &p_args[1], p_argcount - 1, r_error); } Variant Object::_call_deferred_bind(const Variant **p_args, int p_argcount, Callable::CallError &r_error) { if (p_argcount < 1) { r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS; r_error.argument = 0; return Variant(); } if (p_args[0]->get_type() != Variant::STRING_NAME && p_args[0]->get_type() != Variant::STRING) { r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT; r_error.argument = 0; r_error.expected = Variant::STRING_NAME; return Variant(); } r_error.error = Callable::CallError::CALL_OK; StringName method = *p_args[0]; MessageQueue::get_singleton()->push_callp(get_instance_id(), method, &p_args[1], p_argcount - 1, true); return Variant(); } bool Object::has_method(const StringName &p_method) const { if (p_method == CoreStringNames::get_singleton()->_free) { return true; } if (script_instance && script_instance->has_method(p_method)) { return true; } MethodBind *method = ClassDB::get_method(get_class_name(), p_method); return method != nullptr; } Variant Object::getvar(const Variant &p_key, bool *r_valid) const { if (r_valid) { *r_valid = false; } if (p_key.get_type() == Variant::STRING_NAME || p_key.get_type() == Variant::STRING) { return get(p_key, r_valid); } return Variant(); } void Object::setvar(const Variant &p_key, const Variant &p_value, bool *r_valid) { if (r_valid) { *r_valid = false; } if (p_key.get_type() == Variant::STRING_NAME || p_key.get_type() == Variant::STRING) { return set(p_key, p_value, r_valid); } } Variant Object::callv(const StringName &p_method, const Array &p_args) { const Variant **argptrs = nullptr; if (p_args.size() > 0) { argptrs = (const Variant **)alloca(sizeof(Variant *) * p_args.size()); for (int i = 0; i < p_args.size(); i++) { argptrs[i] = &p_args[i]; } } Callable::CallError ce; Variant ret = callp(p_method, argptrs, p_args.size(), ce); if (ce.error != Callable::CallError::CALL_OK) { ERR_FAIL_V_MSG(Variant(), "Error calling method from 'callv': " + Variant::get_call_error_text(this, p_method, argptrs, p_args.size(), ce) + "."); } return ret; } Variant Object::callp(const StringName &p_method, const Variant **p_args, int p_argcount, Callable::CallError &r_error) { r_error.error = Callable::CallError::CALL_OK; if (p_method == CoreStringNames::get_singleton()->_free) { //free must be here, before anything, always ready #ifdef DEBUG_ENABLED if (p_argcount != 0) { r_error.argument = 0; r_error.error = Callable::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS; return Variant(); } if (Object::cast_to(this)) { r_error.argument = 0; r_error.error = Callable::CallError::CALL_ERROR_INVALID_METHOD; ERR_FAIL_V_MSG(Variant(), "Can't 'free' a reference."); } if (_lock_index.get() > 1) { r_error.argument = 0; r_error.error = Callable::CallError::CALL_ERROR_INVALID_METHOD; ERR_FAIL_V_MSG(Variant(), "Object is locked and can't be freed."); } #endif //must be here, must be before everything, memdelete(this); r_error.error = Callable::CallError::CALL_OK; return Variant(); } Variant ret; OBJ_DEBUG_LOCK if (script_instance) { ret = script_instance->callp(p_method, p_args, p_argcount, r_error); //force jumptable switch (r_error.error) { case Callable::CallError::CALL_OK: return ret; case Callable::CallError::CALL_ERROR_INVALID_METHOD: break; case Callable::CallError::CALL_ERROR_INVALID_ARGUMENT: case Callable::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS: case Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS: return ret; case Callable::CallError::CALL_ERROR_INSTANCE_IS_NULL: { } } } //extension does not need this, because all methods are registered in MethodBind MethodBind *method = ClassDB::get_method(get_class_name(), p_method); if (method) { ret = method->call(this, p_args, p_argcount, r_error); } else { r_error.error = Callable::CallError::CALL_ERROR_INVALID_METHOD; } return ret; } void Object::notification(int p_notification, bool p_reversed) { _notificationv(p_notification, p_reversed); if (script_instance) { script_instance->notification(p_notification); } if (_extension && _extension->notification) { _extension->notification(_extension_instance, p_notification); } } String Object::to_string() { if (script_instance) { bool valid; String ret = script_instance->to_string(&valid); if (valid) { return ret; } } if (_extension && _extension->to_string) { String ret; _extension->to_string(_extension_instance, &ret); return ret; } return "[" + get_class() + ":" + itos(get_instance_id()) + "]"; } void Object::set_script_and_instance(const Variant &p_script, ScriptInstance *p_instance) { //this function is not meant to be used in any of these ways ERR_FAIL_COND(p_script.is_null()); ERR_FAIL_COND(!p_instance); ERR_FAIL_COND(script_instance != nullptr || !script.is_null()); script = p_script; script_instance = p_instance; } void Object::set_script(const Variant &p_script) { if (script == p_script) { return; } if (script_instance) { memdelete(script_instance); script_instance = nullptr; } script = p_script; Ref