2023-01-05 13:25:55 +01:00
|
|
|
/**************************************************************************/
|
|
|
|
/* 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. */
|
|
|
|
/**************************************************************************/
|
2018-01-05 00:50:27 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
#include "resource.h"
|
2017-01-16 08:04:19 +01:00
|
|
|
|
2021-06-11 14:51:48 +02:00
|
|
|
#include "core/io/file_access.h"
|
2018-09-11 18:13:45 +02:00
|
|
|
#include "core/io/resource_loader.h"
|
2021-07-21 02:36:56 +02:00
|
|
|
#include "core/math/math_funcs.h"
|
2020-11-07 23:33:38 +01:00
|
|
|
#include "core/object/script_language.h"
|
2020-06-08 16:39:08 +02:00
|
|
|
#include "core/os/os.h"
|
2017-08-06 00:48:29 +02:00
|
|
|
#include "scene/main/node.h" //only so casting works
|
2018-09-11 18:13:45 +02:00
|
|
|
|
2017-01-16 08:04:19 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
void Resource::emit_changed() {
|
2024-09-05 09:48:13 +02:00
|
|
|
if (ResourceLoader::is_within_load() && !Thread::is_main_thread()) {
|
|
|
|
ResourceLoader::resource_changed_emit(this);
|
|
|
|
return;
|
2024-07-12 16:03:01 +02:00
|
|
|
}
|
2024-09-05 09:48:13 +02:00
|
|
|
|
|
|
|
emit_signal(CoreStringName(changed));
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void Resource::_resource_path_changed() {
|
|
|
|
}
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
void Resource::set_path(const String &p_path, bool p_take_over) {
|
2020-05-14 16:41:43 +02:00
|
|
|
if (path_cache == p_path) {
|
2014-02-10 02:10:30 +01:00
|
|
|
return;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2022-06-22 13:46:46 +02:00
|
|
|
if (p_path.is_empty()) {
|
|
|
|
p_take_over = false; // Can't take over an empty path
|
|
|
|
}
|
|
|
|
|
2024-08-28 11:27:01 +02:00
|
|
|
{
|
|
|
|
MutexLock lock(ResourceCache::lock);
|
2022-06-22 13:46:46 +02:00
|
|
|
|
2024-08-28 11:27:01 +02:00
|
|
|
if (!path_cache.is_empty()) {
|
|
|
|
ResourceCache::resources.erase(path_cache);
|
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2024-08-28 11:27:01 +02:00
|
|
|
path_cache = "";
|
2017-01-07 22:25:37 +01:00
|
|
|
|
2024-08-28 11:27:01 +02:00
|
|
|
Ref<Resource> existing = ResourceCache::get_ref(p_path);
|
2017-01-07 22:25:37 +01:00
|
|
|
|
2024-08-28 11:27:01 +02:00
|
|
|
if (existing.is_valid()) {
|
|
|
|
if (p_take_over) {
|
|
|
|
existing->path_cache = String();
|
|
|
|
ResourceCache::resources.erase(p_path);
|
|
|
|
} else {
|
|
|
|
ERR_FAIL_MSG("Another resource is loaded from path '" + p_path + "' (possible cyclic resource inclusion).");
|
|
|
|
}
|
2014-06-28 04:21:45 +02:00
|
|
|
}
|
2022-06-22 13:46:46 +02:00
|
|
|
|
2024-08-28 11:27:01 +02:00
|
|
|
path_cache = p_path;
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2024-08-28 11:27:01 +02:00
|
|
|
if (!path_cache.is_empty()) {
|
|
|
|
ResourceCache::resources[path_cache] = this;
|
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
_resource_path_changed();
|
|
|
|
}
|
|
|
|
|
|
|
|
String Resource::get_path() const {
|
|
|
|
return path_cache;
|
|
|
|
}
|
|
|
|
|
2023-08-05 02:07:16 +02:00
|
|
|
void Resource::set_path_cache(const String &p_path) {
|
|
|
|
path_cache = p_path;
|
|
|
|
}
|
|
|
|
|
2021-07-21 02:36:56 +02:00
|
|
|
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.
|
2022-09-08 07:36:10 +02:00
|
|
|
OS::DateTime dt = OS::get_singleton()->get_datetime();
|
2022-06-18 16:20:55 +02:00
|
|
|
uint32_t hash = hash_murmur3_one_32(OS::get_singleton()->get_ticks_usec());
|
2022-09-08 07:36:10 +02:00
|
|
|
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);
|
2022-06-18 16:20:55 +02:00
|
|
|
hash = hash_murmur3_one_32(Math::rand(), hash);
|
2021-07-21 02:36:56 +02:00
|
|
|
|
|
|
|
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;
|
|
|
|
for (uint32_t i = 0; i < characters; i++) {
|
|
|
|
uint32_t c = hash % base;
|
|
|
|
if (c < char_count) {
|
|
|
|
id += String::chr('a' + c);
|
|
|
|
} else {
|
|
|
|
id += String::chr('0' + (c - char_count));
|
|
|
|
}
|
|
|
|
hash /= base;
|
|
|
|
}
|
|
|
|
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Resource::set_scene_unique_id(const String &p_id) {
|
2024-02-08 19:26:55 +01:00
|
|
|
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.");
|
2021-07-21 02:36:56 +02:00
|
|
|
scene_unique_id = p_id;
|
2015-06-22 05:03:19 +02:00
|
|
|
}
|
|
|
|
|
2021-07-21 02:36:56 +02:00
|
|
|
String Resource::get_scene_unique_id() const {
|
|
|
|
return scene_unique_id;
|
2015-06-22 05:03:19 +02:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
void Resource::set_name(const String &p_name) {
|
|
|
|
name = p_name;
|
2021-10-14 13:28:45 +02:00
|
|
|
emit_changed();
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
2020-05-14 14:29:06 +02:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
String Resource::get_name() const {
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
2021-04-27 17:43:49 +02:00
|
|
|
void Resource::update_configuration_warning() {
|
|
|
|
if (_update_configuration_warning) {
|
|
|
|
_update_configuration_warning();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-27 18:17:20 +02:00
|
|
|
bool Resource::editor_can_reload_from_file() {
|
|
|
|
return true; //by default yes
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2023-07-03 21:29:37 +02:00
|
|
|
void Resource::connect_changed(const Callable &p_callable, uint32_t p_flags) {
|
2024-09-05 09:48:13 +02:00
|
|
|
if (ResourceLoader::is_within_load() && !Thread::is_main_thread()) {
|
|
|
|
ResourceLoader::resource_changed_connect(this, p_callable, p_flags);
|
2024-07-12 16:03:01 +02:00
|
|
|
return;
|
|
|
|
}
|
2024-09-05 09:48:13 +02:00
|
|
|
|
2023-09-04 17:01:33 +02:00
|
|
|
if (!is_connected(CoreStringName(changed), p_callable) || p_flags & CONNECT_REFERENCE_COUNTED) {
|
|
|
|
connect(CoreStringName(changed), p_callable, p_flags);
|
2023-07-03 21:29:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Resource::disconnect_changed(const Callable &p_callable) {
|
2024-09-05 09:48:13 +02:00
|
|
|
if (ResourceLoader::is_within_load() && !Thread::is_main_thread()) {
|
|
|
|
ResourceLoader::resource_changed_disconnect(this, p_callable);
|
2024-07-12 16:03:01 +02:00
|
|
|
return;
|
|
|
|
}
|
2024-09-05 09:48:13 +02:00
|
|
|
|
2023-09-04 17:01:33 +02:00
|
|
|
if (is_connected(CoreStringName(changed), p_callable)) {
|
|
|
|
disconnect(CoreStringName(changed), p_callable);
|
2023-07-03 21:29:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-11 18:18:45 +01:00
|
|
|
void Resource::reset_state() {
|
|
|
|
}
|
2023-07-03 21:29:37 +02:00
|
|
|
|
2021-02-11 18:18:45 +01:00
|
|
|
Error Resource::copy_from(const Ref<Resource> &p_resource) {
|
|
|
|
ERR_FAIL_COND_V(p_resource.is_null(), ERR_INVALID_PARAMETER);
|
|
|
|
if (get_class() != p_resource->get_class()) {
|
|
|
|
return ERR_INVALID_PARAMETER;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2016-06-27 18:17:20 +02:00
|
|
|
|
2023-07-03 21:29:37 +02:00
|
|
|
reset_state(); // May want to reset state.
|
2016-06-27 18:17:20 +02:00
|
|
|
|
|
|
|
List<PropertyInfo> pi;
|
2021-02-11 18:18:45 +01:00
|
|
|
p_resource->get_property_list(&pi);
|
2016-06-27 18:17:20 +02:00
|
|
|
|
2021-07-24 15:46:25 +02:00
|
|
|
for (const PropertyInfo &E : pi) {
|
2021-07-16 05:45:57 +02:00
|
|
|
if (!(E.usage & PROPERTY_USAGE_STORAGE)) {
|
2016-06-27 18:17:20 +02:00
|
|
|
continue;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2021-07-16 05:45:57 +02:00
|
|
|
if (E.name == "resource_path") {
|
2016-06-27 18:17:20 +02:00
|
|
|
continue; //do not change path
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2016-06-27 18:17:20 +02:00
|
|
|
|
2021-07-16 05:45:57 +02:00
|
|
|
set(E.name, p_resource->get(E.name));
|
2016-06-27 18:17:20 +02:00
|
|
|
}
|
2021-02-11 18:18:45 +01:00
|
|
|
return OK;
|
|
|
|
}
|
2024-03-07 20:19:05 +01:00
|
|
|
|
2021-02-11 18:18:45 +01:00
|
|
|
void Resource::reload_from_file() {
|
|
|
|
String path = get_path();
|
|
|
|
if (!path.is_resource_file()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ref<Resource> s = ResourceLoader::load(ResourceLoader::path_remap(path), get_class(), ResourceFormatLoader::CACHE_MODE_IGNORE);
|
|
|
|
|
|
|
|
if (!s.is_valid()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
copy_from(s);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2023-01-17 21:14:19 +01:00
|
|
|
void Resource::_dupe_sub_resources(Variant &r_variant, Node *p_for_scene, HashMap<Ref<Resource>, Ref<Resource>> &p_remap_cache) {
|
|
|
|
switch (r_variant.get_type()) {
|
|
|
|
case Variant::ARRAY: {
|
|
|
|
Array a = r_variant;
|
|
|
|
for (int i = 0; i < a.size(); i++) {
|
|
|
|
_dupe_sub_resources(a[i], p_for_scene, p_remap_cache);
|
|
|
|
}
|
|
|
|
} break;
|
|
|
|
case Variant::DICTIONARY: {
|
|
|
|
Dictionary d = r_variant;
|
|
|
|
List<Variant> keys;
|
|
|
|
d.get_key_list(&keys);
|
|
|
|
for (Variant &k : keys) {
|
|
|
|
if (k.get_type() == Variant::OBJECT) {
|
|
|
|
// Replace in dictionary key.
|
|
|
|
Ref<Resource> sr = k;
|
|
|
|
if (sr.is_valid() && sr->is_local_to_scene()) {
|
|
|
|
if (p_remap_cache.has(sr)) {
|
|
|
|
d[p_remap_cache[sr]] = d[k];
|
|
|
|
d.erase(k);
|
|
|
|
} else {
|
|
|
|
Ref<Resource> dupe = sr->duplicate_for_local_scene(p_for_scene, p_remap_cache);
|
|
|
|
d[dupe] = d[k];
|
|
|
|
d.erase(k);
|
|
|
|
p_remap_cache[sr] = dupe;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
_dupe_sub_resources(k, p_for_scene, p_remap_cache);
|
|
|
|
}
|
|
|
|
|
|
|
|
_dupe_sub_resources(d[k], p_for_scene, p_remap_cache);
|
|
|
|
}
|
|
|
|
} break;
|
|
|
|
case Variant::OBJECT: {
|
|
|
|
Ref<Resource> sr = r_variant;
|
|
|
|
if (sr.is_valid() && sr->is_local_to_scene()) {
|
|
|
|
if (p_remap_cache.has(sr)) {
|
|
|
|
r_variant = p_remap_cache[sr];
|
|
|
|
} else {
|
|
|
|
Ref<Resource> dupe = sr->duplicate_for_local_scene(p_for_scene, p_remap_cache);
|
|
|
|
r_variant = dupe;
|
|
|
|
p_remap_cache[sr] = dupe;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} break;
|
|
|
|
default: {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ref<Resource> Resource::duplicate_for_local_scene(Node *p_for_scene, HashMap<Ref<Resource>, Ref<Resource>> &p_remap_cache) {
|
2017-01-10 05:04:31 +01:00
|
|
|
List<PropertyInfo> plist;
|
|
|
|
get_property_list(&plist);
|
|
|
|
|
2021-06-18 00:03:09 +02:00
|
|
|
Ref<Resource> r = Object::cast_to<Resource>(ClassDB::instantiate(get_class()));
|
2020-11-18 23:45:30 +01:00
|
|
|
ERR_FAIL_COND_V(r.is_null(), Ref<Resource>());
|
2017-01-10 05:04:31 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
r->local_scene = p_for_scene;
|
2017-01-10 05:04:31 +01:00
|
|
|
|
2021-07-24 15:46:25 +02:00
|
|
|
for (const PropertyInfo &E : plist) {
|
2021-07-16 05:45:57 +02:00
|
|
|
if (!(E.usage & PROPERTY_USAGE_STORAGE)) {
|
2017-01-10 05:04:31 +01:00
|
|
|
continue;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2023-01-17 21:14:19 +01:00
|
|
|
Variant p = get(E.name).duplicate(true);
|
|
|
|
|
|
|
|
_dupe_sub_resources(p, p_for_scene, p_remap_cache);
|
2017-01-10 05:04:31 +01:00
|
|
|
|
2021-07-16 05:45:57 +02:00
|
|
|
r->set(E.name, p);
|
2017-01-10 05:04:31 +01:00
|
|
|
}
|
|
|
|
|
2020-11-18 23:45:30 +01:00
|
|
|
return r;
|
2017-01-10 05:04:31 +01:00
|
|
|
}
|
|
|
|
|
2023-01-17 21:14:19 +01:00
|
|
|
void Resource::_find_sub_resources(const Variant &p_variant, HashSet<Ref<Resource>> &p_resources_found) {
|
|
|
|
switch (p_variant.get_type()) {
|
|
|
|
case Variant::ARRAY: {
|
|
|
|
Array a = p_variant;
|
|
|
|
for (int i = 0; i < a.size(); i++) {
|
|
|
|
_find_sub_resources(a[i], p_resources_found);
|
|
|
|
}
|
|
|
|
} break;
|
|
|
|
case Variant::DICTIONARY: {
|
|
|
|
Dictionary d = p_variant;
|
|
|
|
List<Variant> keys;
|
|
|
|
d.get_key_list(&keys);
|
|
|
|
for (const Variant &k : keys) {
|
|
|
|
_find_sub_resources(k, p_resources_found);
|
|
|
|
_find_sub_resources(d[k], p_resources_found);
|
|
|
|
}
|
|
|
|
} break;
|
|
|
|
case Variant::OBJECT: {
|
|
|
|
Ref<Resource> r = p_variant;
|
|
|
|
if (r.is_valid()) {
|
|
|
|
p_resources_found.insert(r);
|
|
|
|
}
|
|
|
|
} break;
|
|
|
|
default: {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Resource::configure_for_local_scene(Node *p_for_scene, HashMap<Ref<Resource>, Ref<Resource>> &p_remap_cache) {
|
2017-12-04 19:55:20 +01:00
|
|
|
List<PropertyInfo> plist;
|
|
|
|
get_property_list(&plist);
|
|
|
|
|
2023-07-08 13:38:27 +02:00
|
|
|
reset_local_to_scene();
|
2017-12-04 19:55:20 +01:00
|
|
|
local_scene = p_for_scene;
|
|
|
|
|
2021-07-24 15:46:25 +02:00
|
|
|
for (const PropertyInfo &E : plist) {
|
2021-07-16 05:45:57 +02:00
|
|
|
if (!(E.usage & PROPERTY_USAGE_STORAGE)) {
|
2017-12-04 19:55:20 +01:00
|
|
|
continue;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2021-07-16 05:45:57 +02:00
|
|
|
Variant p = get(E.name);
|
2023-01-17 21:14:19 +01:00
|
|
|
|
|
|
|
HashSet<Ref<Resource>> sub_resources;
|
|
|
|
_find_sub_resources(p, sub_resources);
|
|
|
|
|
|
|
|
for (Ref<Resource> sr : sub_resources) {
|
|
|
|
if (sr->is_local_to_scene()) {
|
|
|
|
if (!p_remap_cache.has(sr)) {
|
|
|
|
sr->configure_for_local_scene(p_for_scene, p_remap_cache);
|
|
|
|
p_remap_cache[sr] = sr;
|
2017-12-04 19:55:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-17 12:36:47 +02:00
|
|
|
Ref<Resource> Resource::duplicate(bool p_subresources) const {
|
2014-02-10 02:10:30 +01:00
|
|
|
List<PropertyInfo> plist;
|
|
|
|
get_property_list(&plist);
|
|
|
|
|
2022-04-07 12:23:40 +02:00
|
|
|
Ref<Resource> r = static_cast<Resource *>(ClassDB::instantiate(get_class()));
|
2020-11-17 10:13:41 +01:00
|
|
|
ERR_FAIL_COND_V(r.is_null(), Ref<Resource>());
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2021-07-24 15:46:25 +02:00
|
|
|
for (const PropertyInfo &E : plist) {
|
2021-07-16 05:45:57 +02:00
|
|
|
if (!(E.usage & PROPERTY_USAGE_STORAGE)) {
|
2014-02-10 02:10:30 +01:00
|
|
|
continue;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2021-07-16 05:45:57 +02:00
|
|
|
Variant p = get(E.name);
|
2018-07-02 20:08:35 +02:00
|
|
|
|
2023-01-21 20:18:57 +01:00
|
|
|
switch (p.get_type()) {
|
|
|
|
case Variant::Type::DICTIONARY:
|
|
|
|
case Variant::Type::ARRAY:
|
|
|
|
case Variant::Type::PACKED_BYTE_ARRAY:
|
|
|
|
case Variant::Type::PACKED_COLOR_ARRAY:
|
|
|
|
case Variant::Type::PACKED_INT32_ARRAY:
|
|
|
|
case Variant::Type::PACKED_INT64_ARRAY:
|
|
|
|
case Variant::Type::PACKED_FLOAT32_ARRAY:
|
|
|
|
case Variant::Type::PACKED_FLOAT64_ARRAY:
|
|
|
|
case Variant::Type::PACKED_STRING_ARRAY:
|
|
|
|
case Variant::Type::PACKED_VECTOR2_ARRAY:
|
2024-04-08 16:51:34 +02:00
|
|
|
case Variant::Type::PACKED_VECTOR3_ARRAY:
|
|
|
|
case Variant::Type::PACKED_VECTOR4_ARRAY: {
|
2023-01-21 20:18:57 +01:00
|
|
|
r->set(E.name, p.duplicate(p_subresources));
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case Variant::Type::OBJECT: {
|
|
|
|
if (!(E.usage & PROPERTY_USAGE_NEVER_DUPLICATE) && (p_subresources || (E.usage & PROPERTY_USAGE_ALWAYS_DUPLICATE))) {
|
|
|
|
Ref<Resource> sr = p;
|
|
|
|
if (sr.is_valid()) {
|
|
|
|
r->set(E.name, sr->duplicate(p_subresources));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
r->set(E.name, p);
|
|
|
|
}
|
|
|
|
} break;
|
|
|
|
|
|
|
|
default: {
|
|
|
|
r->set(E.name, p);
|
2018-07-02 20:08:35 +02:00
|
|
|
}
|
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2020-11-17 10:13:41 +01:00
|
|
|
return r;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
void Resource::_set_path(const String &p_path) {
|
|
|
|
set_path(p_path, false);
|
2014-06-30 06:28:05 +02:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
void Resource::_take_over_path(const String &p_path) {
|
|
|
|
set_path(p_path, true);
|
2014-06-30 06:28:05 +02:00
|
|
|
}
|
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
RID Resource::get_rid() const {
|
2024-01-17 15:03:52 +01:00
|
|
|
RID ret;
|
|
|
|
if (!GDVIRTUAL_CALL(_get_rid, ret)) {
|
|
|
|
#ifndef DISABLE_DEPRECATED
|
|
|
|
if (_get_extension() && _get_extension()->get_rid) {
|
|
|
|
ret = RID::from_uint64(_get_extension()->get_rid(_get_extension_instance()));
|
2022-03-10 08:17:38 +01:00
|
|
|
}
|
2024-01-17 15:03:52 +01:00
|
|
|
#endif
|
2022-03-10 08:17:38 +01:00
|
|
|
}
|
2024-01-17 15:03:52 +01:00
|
|
|
return ret;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2016-05-27 19:18:40 +02:00
|
|
|
#ifdef TOOLS_ENABLED
|
|
|
|
|
2024-03-07 17:45:13 +01:00
|
|
|
uint32_t Resource::hash_edited_version_for_preview() const {
|
2022-06-18 16:20:55 +02:00
|
|
|
uint32_t hash = hash_murmur3_one_32(get_edited_version());
|
2016-05-27 19:18:40 +02:00
|
|
|
|
|
|
|
List<PropertyInfo> plist;
|
|
|
|
get_property_list(&plist);
|
|
|
|
|
2021-07-24 15:46:25 +02:00
|
|
|
for (const PropertyInfo &E : plist) {
|
2021-07-16 05:45:57 +02:00
|
|
|
if (E.usage & PROPERTY_USAGE_STORAGE && E.type == Variant::OBJECT && E.hint == PROPERTY_HINT_RESOURCE_TYPE) {
|
2022-05-03 01:43:50 +02:00
|
|
|
Ref<Resource> res = get(E.name);
|
2016-05-27 19:18:40 +02:00
|
|
|
if (res.is_valid()) {
|
2024-03-07 17:45:13 +01:00
|
|
|
hash = hash_murmur3_one_32(res->hash_edited_version_for_preview(), hash);
|
2016-05-27 19:18:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return hash;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2017-01-10 05:04:31 +01:00
|
|
|
void Resource::set_local_to_scene(bool p_enable) {
|
2017-03-05 16:44:50 +01:00
|
|
|
local_to_scene = p_enable;
|
2017-01-10 05:04:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Resource::is_local_to_scene() const {
|
|
|
|
return local_to_scene;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
Node *Resource::get_local_scene() const {
|
2020-05-14 16:41:43 +02:00
|
|
|
if (local_scene) {
|
2017-01-10 05:04:31 +01:00
|
|
|
return local_scene;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2017-01-10 05:04:31 +01:00
|
|
|
|
|
|
|
if (_get_local_scene_func) {
|
|
|
|
return _get_local_scene_func();
|
|
|
|
}
|
|
|
|
|
2020-04-02 01:20:12 +02:00
|
|
|
return nullptr;
|
2017-01-10 05:04:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void Resource::setup_local_to_scene() {
|
2021-08-22 03:52:44 +02:00
|
|
|
emit_signal(SNAME("setup_local_to_scene_requested"));
|
2022-10-08 14:47:58 +02:00
|
|
|
GDVIRTUAL_CALL(_setup_local_to_scene);
|
2017-01-10 05:04:31 +01:00
|
|
|
}
|
|
|
|
|
2023-07-08 13:38:27 +02:00
|
|
|
void Resource::reset_local_to_scene() {
|
|
|
|
// Restores the state as if setup_local_to_scene() hadn't been called.
|
|
|
|
}
|
|
|
|
|
2020-04-02 01:20:12 +02:00
|
|
|
Node *(*Resource::_get_local_scene_func)() = nullptr;
|
2021-04-27 17:43:49 +02:00
|
|
|
void (*Resource::_update_configuration_warning)() = nullptr;
|
2017-01-10 05:04:31 +01:00
|
|
|
|
2017-06-28 22:00:18 +02:00
|
|
|
void Resource::set_as_translation_remapped(bool p_remapped) {
|
2020-05-14 16:41:43 +02:00
|
|
|
if (remapped_list.in_list() == p_remapped) {
|
2017-06-28 22:00:18 +02:00
|
|
|
return;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2017-06-28 22:00:18 +02:00
|
|
|
|
2024-08-28 11:27:01 +02:00
|
|
|
MutexLock lock(ResourceCache::lock);
|
2017-06-28 22:00:18 +02:00
|
|
|
|
|
|
|
if (p_remapped) {
|
|
|
|
ResourceLoader::remapped_list.add(&remapped_list);
|
|
|
|
} else {
|
|
|
|
ResourceLoader::remapped_list.remove(&remapped_list);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-11 19:35:23 +02:00
|
|
|
#ifdef TOOLS_ENABLED
|
|
|
|
//helps keep IDs same number when loading/saving scenes. -1 clears ID and it Returns -1 when no id stored
|
2021-07-21 02:36:56 +02:00
|
|
|
void Resource::set_id_for_path(const String &p_path, const String &p_id) {
|
2021-12-09 10:42:46 +01:00
|
|
|
if (p_id.is_empty()) {
|
2021-01-18 14:01:38 +01:00
|
|
|
ResourceCache::path_cache_lock.write_lock();
|
2019-07-16 18:46:40 +02:00
|
|
|
ResourceCache::resource_path_cache[p_path].erase(get_path());
|
2021-01-18 14:01:38 +01:00
|
|
|
ResourceCache::path_cache_lock.write_unlock();
|
2019-04-11 19:35:23 +02:00
|
|
|
} else {
|
2021-01-18 14:01:38 +01:00
|
|
|
ResourceCache::path_cache_lock.write_lock();
|
2019-07-16 18:46:40 +02:00
|
|
|
ResourceCache::resource_path_cache[p_path][get_path()] = p_id;
|
2021-01-18 14:01:38 +01:00
|
|
|
ResourceCache::path_cache_lock.write_unlock();
|
2019-04-11 19:35:23 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-21 02:36:56 +02:00
|
|
|
String Resource::get_id_for_path(const String &p_path) const {
|
2021-01-18 14:01:38 +01:00
|
|
|
ResourceCache::path_cache_lock.read_lock();
|
2019-07-16 18:46:40 +02:00
|
|
|
if (ResourceCache::resource_path_cache[p_path].has(get_path())) {
|
2021-07-21 02:36:56 +02:00
|
|
|
String result = ResourceCache::resource_path_cache[p_path][get_path()];
|
2021-01-18 14:01:38 +01:00
|
|
|
ResourceCache::path_cache_lock.read_unlock();
|
2019-07-16 18:46:40 +02:00
|
|
|
return result;
|
2019-04-11 19:35:23 +02:00
|
|
|
} else {
|
2021-01-18 14:01:38 +01:00
|
|
|
ResourceCache::path_cache_lock.read_unlock();
|
2021-07-21 02:36:56 +02:00
|
|
|
return "";
|
2019-04-11 19:35:23 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2017-01-10 05:04:31 +01:00
|
|
|
void Resource::_bind_methods() {
|
2017-03-05 16:44:50 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("set_path", "path"), &Resource::_set_path);
|
|
|
|
ClassDB::bind_method(D_METHOD("take_over_path", "path"), &Resource::_take_over_path);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_path"), &Resource::get_path);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_name", "name"), &Resource::set_name);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_name"), &Resource::get_name);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_rid"), &Resource::get_rid);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_local_to_scene", "enable"), &Resource::set_local_to_scene);
|
|
|
|
ClassDB::bind_method(D_METHOD("is_local_to_scene"), &Resource::is_local_to_scene);
|
2017-08-09 13:19:41 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("get_local_scene"), &Resource::get_local_scene);
|
2017-03-05 16:44:50 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("setup_local_to_scene"), &Resource::setup_local_to_scene);
|
2023-07-03 21:29:37 +02:00
|
|
|
|
2024-02-08 19:26:55 +01:00
|
|
|
ClassDB::bind_static_method("Resource", D_METHOD("generate_scene_unique_id"), &Resource::generate_scene_unique_id);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_scene_unique_id", "id"), &Resource::set_scene_unique_id);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_scene_unique_id"), &Resource::get_scene_unique_id);
|
|
|
|
|
2021-01-13 15:24:51 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("emit_changed"), &Resource::emit_changed);
|
2017-01-10 05:04:31 +01:00
|
|
|
|
2017-08-09 13:19:41 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("duplicate", "subresources"), &Resource::duplicate, DEFVAL(false));
|
2017-03-05 16:44:50 +01:00
|
|
|
ADD_SIGNAL(MethodInfo("changed"));
|
2021-08-22 03:52:44 +02:00
|
|
|
ADD_SIGNAL(MethodInfo("setup_local_to_scene_requested"));
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
ADD_GROUP("Resource", "resource_");
|
2018-11-08 15:30:02 +01:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "resource_local_to_scene"), "set_local_to_scene", "is_local_to_scene");
|
2017-03-05 16:44:50 +01:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::STRING, "resource_path", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_EDITOR), "set_path", "get_path");
|
2022-11-29 15:43:08 +01:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::STRING, "resource_name"), "set_name", "get_name");
|
2024-02-08 19:26:55 +01:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::STRING, "resource_scene_unique_id", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE), "set_scene_unique_id", "get_scene_unique_id");
|
2022-03-10 08:17:38 +01:00
|
|
|
|
2022-10-08 14:47:58 +02:00
|
|
|
GDVIRTUAL_BIND(_setup_local_to_scene);
|
2024-01-17 15:03:52 +01:00
|
|
|
GDVIRTUAL_BIND(_get_rid);
|
2017-01-10 05:04:31 +01:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-12-06 21:36:34 +01:00
|
|
|
Resource::Resource() :
|
2020-05-12 17:01:17 +02:00
|
|
|
remapped_list(this) {}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
Resource::~Resource() {
|
2024-05-13 11:51:36 +02:00
|
|
|
if (unlikely(path_cache.is_empty())) {
|
|
|
|
return;
|
2017-01-07 22:25:37 +01:00
|
|
|
}
|
2024-05-13 11:51:36 +02:00
|
|
|
|
2024-08-28 11:27:01 +02:00
|
|
|
MutexLock lock(ResourceCache::lock);
|
2024-05-13 11:51:36 +02:00
|
|
|
// Only unregister from the cache if this is the actual resource listed there.
|
|
|
|
// (Other resources can have the same value in `path_cache` if loaded with `CACHE_IGNORE`.)
|
|
|
|
HashMap<String, Resource *>::Iterator E = ResourceCache::resources.find(path_cache);
|
|
|
|
if (likely(E && E->value == this)) {
|
|
|
|
ResourceCache::resources.remove(E);
|
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
HashMap<String, Resource *> ResourceCache::resources;
|
2019-07-16 18:46:40 +02:00
|
|
|
#ifdef TOOLS_ENABLED
|
2021-07-21 02:36:56 +02:00
|
|
|
HashMap<String, HashMap<String, String>> ResourceCache::resource_path_cache;
|
2019-07-16 18:46:40 +02:00
|
|
|
#endif
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2022-06-22 13:46:46 +02:00
|
|
|
Mutex ResourceCache::lock;
|
2019-07-16 18:46:40 +02:00
|
|
|
#ifdef TOOLS_ENABLED
|
2021-01-18 14:01:38 +01:00
|
|
|
RWLock ResourceCache::path_cache_lock;
|
2019-07-16 18:46:40 +02:00
|
|
|
#endif
|
2017-01-07 22:25:37 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
void ResourceCache::clear() {
|
2023-11-28 13:19:34 +01:00
|
|
|
if (!resources.is_empty()) {
|
2020-06-08 16:39:08 +02:00
|
|
|
if (OS::get_singleton()->is_stdout_verbose()) {
|
2023-11-28 13:19:34 +01:00
|
|
|
ERR_PRINT(vformat("%d resources still in use at exit.", resources.size()));
|
2022-05-08 10:09:19 +02:00
|
|
|
for (const KeyValue<String, Resource *> &E : resources) {
|
|
|
|
print_line(vformat("Resource still in use: %s (%s)", E.key, E.value->get_class()));
|
2020-06-08 16:39:08 +02:00
|
|
|
}
|
2023-11-28 13:19:34 +01:00
|
|
|
} else {
|
|
|
|
ERR_PRINT(vformat("%d resources still in use at exit (run with --verbose for details).", resources.size()));
|
2020-06-08 16:39:08 +02:00
|
|
|
}
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
resources.clear();
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
bool ResourceCache::has(const String &p_path) {
|
2024-08-28 11:27:01 +02:00
|
|
|
Resource **res = nullptr;
|
2022-06-22 13:46:46 +02:00
|
|
|
|
2024-08-28 11:27:01 +02:00
|
|
|
{
|
|
|
|
MutexLock mutex_lock(lock);
|
2017-01-07 22:25:37 +01:00
|
|
|
|
2024-08-28 11:27:01 +02:00
|
|
|
res = resources.getptr(p_path);
|
2022-06-22 13:46:46 +02:00
|
|
|
|
2024-08-28 11:27:01 +02:00
|
|
|
if (res && (*res)->get_reference_count() == 0) {
|
|
|
|
// This resource is in the process of being deleted, ignore its existence.
|
|
|
|
(*res)->path_cache = String();
|
|
|
|
resources.erase(p_path);
|
|
|
|
res = nullptr;
|
|
|
|
}
|
|
|
|
}
|
2022-06-22 13:46:46 +02:00
|
|
|
|
|
|
|
if (!res) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
2020-05-14 14:29:06 +02:00
|
|
|
|
2022-06-22 13:46:46 +02:00
|
|
|
Ref<Resource> ResourceCache::get_ref(const String &p_path) {
|
|
|
|
Ref<Resource> ref;
|
2024-08-28 11:27:01 +02:00
|
|
|
{
|
|
|
|
MutexLock mutex_lock(lock);
|
|
|
|
Resource **res = resources.getptr(p_path);
|
2017-01-07 22:25:37 +01:00
|
|
|
|
2024-08-28 11:27:01 +02:00
|
|
|
if (res) {
|
|
|
|
ref = Ref<Resource>(*res);
|
|
|
|
}
|
2017-01-07 22:25:37 +01:00
|
|
|
|
2024-08-28 11:27:01 +02:00
|
|
|
if (res && !ref.is_valid()) {
|
|
|
|
// This resource is in the process of being deleted, ignore its existence
|
|
|
|
(*res)->path_cache = String();
|
|
|
|
resources.erase(p_path);
|
|
|
|
res = nullptr;
|
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2022-06-22 13:46:46 +02:00
|
|
|
return ref;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2020-03-17 07:33:00 +01:00
|
|
|
void ResourceCache::get_cached_resources(List<Ref<Resource>> *p_resources) {
|
2024-08-28 11:27:01 +02:00
|
|
|
MutexLock mutex_lock(lock);
|
2023-02-20 05:15:22 +01:00
|
|
|
|
|
|
|
LocalVector<String> to_remove;
|
|
|
|
|
2022-05-08 10:09:19 +02:00
|
|
|
for (KeyValue<String, Resource *> &E : resources) {
|
2023-02-20 05:15:22 +01:00
|
|
|
Ref<Resource> ref = Ref<Resource>(E.value);
|
|
|
|
|
|
|
|
if (!ref.is_valid()) {
|
|
|
|
// This resource is in the process of being deleted, ignore its existence
|
|
|
|
E.value->path_cache = String();
|
|
|
|
to_remove.push_back(E.key);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
p_resources->push_back(ref);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const String &E : to_remove) {
|
|
|
|
resources.erase(E);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int ResourceCache::get_cached_resource_count() {
|
2024-08-28 11:27:01 +02:00
|
|
|
MutexLock mutex_lock(lock);
|
|
|
|
return resources.size();
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|