/**************************************************************************/ /* resource.cpp */ /**************************************************************************/ /* This file is part of: */ /* GODOT ENGINE */ /* https://godotengine.org */ /**************************************************************************/ /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ /* */ /* 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 "resource.h" #include "core/io/resource_loader.h" #include "core/math/math_funcs.h" #include "core/math/random_pcg.h" #include "core/os/os.h" #include "core/variant/container_type_validate.h" #include "scene/main/node.h" //only so casting works void Resource::emit_changed() { if (emit_changed_state != EMIT_CHANGED_UNBLOCKED) { emit_changed_state = EMIT_CHANGED_BLOCKED_PENDING_EMIT; return; } if (ResourceLoader::is_within_load() && !Thread::is_main_thread()) { ResourceLoader::resource_changed_emit(this); return; } emit_signal(CoreStringName(changed)); } void Resource::_block_emit_changed() { if (emit_changed_state == EMIT_CHANGED_UNBLOCKED) { emit_changed_state = EMIT_CHANGED_BLOCKED; } } void Resource::_unblock_emit_changed() { bool emit = (emit_changed_state == EMIT_CHANGED_BLOCKED_PENDING_EMIT); emit_changed_state = EMIT_CHANGED_UNBLOCKED; if (emit) { emit_changed(); } } void Resource::_resource_path_changed() { } void Resource::set_path(const String &p_path, bool p_take_over) { if (path_cache == p_path) { return; } if (p_path.is_empty()) { p_take_over = false; // Can't take over an empty path } { MutexLock lock(ResourceCache::lock); if (!path_cache.is_empty()) { ResourceCache::resources.erase(path_cache); } path_cache = ""; Ref existing = ResourceCache::get_ref(p_path); if (existing.is_valid()) { if (p_take_over) { existing->path_cache = String(); ResourceCache::resources.erase(p_path); } else { ERR_FAIL_MSG(vformat("Another resource is loaded from path '%s' (possible cyclic resource inclusion).", p_path)); } } path_cache = p_path; if (!path_cache.is_empty()) { ResourceCache::resources[path_cache] = this; } } _resource_path_changed(); } String Resource::get_path() const { return path_cache; } void Resource::set_path_cache(const String &p_path) { path_cache = p_path; GDVIRTUAL_CALL(_set_path_cache, p_path); } static thread_local RandomPCG unique_id_gen = RandomPCG(0); void Resource::seed_scene_unique_id(uint32_t p_seed) { unique_id_gen.seed(p_seed); } String Resource::generate_scene_unique_id() { // Generate a unique enough hash, but still user-readable. // If it's not unique it does not matter because the saver will try again. if (unique_id_gen.get_seed() == 0) { OS::DateTime dt = OS::get_singleton()->get_datetime(); uint32_t hash = hash_murmur3_one_32(OS::get_singleton()->get_ticks_usec()); hash = hash_murmur3_one_32(dt.year, hash); hash = hash_murmur3_one_32(dt.month, hash); hash = hash_murmur3_one_32(dt.day, hash); hash = hash_murmur3_one_32(dt.hour, hash); hash = hash_murmur3_one_32(dt.minute, hash); hash = hash_murmur3_one_32(dt.second, hash); hash = hash_murmur3_one_32(Math::rand(), hash); unique_id_gen.seed(hash); } uint32_t random_num = unique_id_gen.rand(); static constexpr uint32_t characters = 5; static constexpr uint32_t char_count = ('z' - 'a'); static constexpr uint32_t base = char_count + ('9' - '0'); String id; id.resize_uninitialized(characters + 1); char32_t *ptr = id.ptrw(); for (uint32_t i = 0; i < characters; i++) { uint32_t c = random_num % base; if (c < char_count) { ptr[i] = ('a' + c); } else { ptr[i] = ('0' + (c - char_count)); } random_num /= base; } ptr[characters] = '\0'; return id; } void Resource::set_scene_unique_id(const String &p_id) { bool is_valid = true; for (int i = 0; i < p_id.length(); i++) { if (!is_ascii_identifier_char(p_id[i])) { is_valid = false; scene_unique_id = Resource::generate_scene_unique_id(); break; } } ERR_FAIL_COND_MSG(!is_valid, "The scene unique ID must contain only letters, numbers, and underscores."); scene_unique_id = p_id; } String Resource::get_scene_unique_id() const { return scene_unique_id; } void Resource::set_name(const String &p_name) { name = p_name; emit_changed(); } String Resource::get_name() const { return name; } void Resource::update_configuration_warning() { if (_update_configuration_warning) { _update_configuration_warning(); } } bool Resource::editor_can_reload_from_file() { return true; //by default yes } void Resource::connect_changed(const Callable &p_callable, uint32_t p_flags) { if (ResourceLoader::is_within_load() && !Thread::is_main_thread()) { ResourceLoader::resource_changed_connect(this, p_callable, p_flags); return; } if (!is_connected(CoreStringName(changed), p_callable) || p_flags & CONNECT_REFERENCE_COUNTED) { connect(CoreStringName(changed), p_callable, p_flags); } } void Resource::disconnect_changed(const Callable &p_callable) { if (ResourceLoader::is_within_load() && !Thread::is_main_thread()) { ResourceLoader::resource_changed_disconnect(this, p_callable); return; } if (is_connected(CoreStringName(changed), p_callable)) { disconnect(CoreStringName(changed), p_callable); } } void Resource::reset_state() { GDVIRTUAL_CALL(_reset_state); } Error Resource::copy_from(const Ref &p_resource) { ERR_FAIL_COND_V(p_resource.is_null(), ERR_INVALID_PARAMETER); if (get_class() != p_resource->get_class()) { return ERR_INVALID_PARAMETER; } _block_emit_changed(); reset_state(); // May want to reset state. List pi; p_resource->get_property_list(&pi); for (const PropertyInfo &E : pi) { if (!(E.usage & PROPERTY_USAGE_STORAGE)) { continue; } if (E.name == "resource_path") { continue; //do not change path } set(E.name, p_resource->get(E.name)); } _unblock_emit_changed(); return OK; } void Resource::reload_from_file() { String path = get_path(); if (!path.is_resource_file()) { return; } Ref s = ResourceLoader::load(ResourceLoader::path_remap(path), get_class(), ResourceFormatLoader::CACHE_MODE_IGNORE); if (s.is_null()) { return; } copy_from(s); } Variant Resource::_duplicate_recursive(const Variant &p_variant, const DuplicateParams &p_params, uint32_t p_usage) const { // Anything other than object can be simply skipped in case of a shallow copy. if (!p_params.deep && p_variant.get_type() != Variant::OBJECT) { return p_variant; } switch (p_variant.get_type()) { case Variant::OBJECT: { const Ref &sr = p_variant; bool should_duplicate = false; if (sr.is_valid()) { if ((p_usage & PROPERTY_USAGE_ALWAYS_DUPLICATE)) { should_duplicate = true; } else if ((p_usage & PROPERTY_USAGE_NEVER_DUPLICATE)) { should_duplicate = false; } else if (p_params.local_scene) { should_duplicate = sr->is_local_to_scene(); } else { switch (p_params.subres_mode) { case RESOURCE_DEEP_DUPLICATE_NONE: { should_duplicate = false; } break; case RESOURCE_DEEP_DUPLICATE_INTERNAL: { should_duplicate = p_params.deep && sr->is_built_in(); } break; case RESOURCE_DEEP_DUPLICATE_ALL: { should_duplicate = p_params.deep; } break; default: { DEV_ASSERT(false); } } if (should_duplicate) { Ref