2014-02-10 01:10:30 +00:00
|
|
|
/*************************************************************************/
|
|
|
|
/* resource.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2017-08-27 12:16:55 +00:00
|
|
|
/* https://godotengine.org */
|
2014-02-10 01:10:30 +00:00
|
|
|
/*************************************************************************/
|
2022-01-03 20:27:34 +00:00
|
|
|
/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
|
2014-02-10 01:10:30 +00:00
|
|
|
/* */
|
|
|
|
/* 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-04 23:50:27 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
#include "resource.h"
|
2017-01-16 07:04:19 +00:00
|
|
|
|
2018-09-11 16:13:45 +00:00
|
|
|
#include "core/core_string_names.h"
|
2021-06-11 12:51:48 +00:00
|
|
|
#include "core/io/file_access.h"
|
2018-09-11 16:13:45 +00:00
|
|
|
#include "core/io/resource_loader.h"
|
2021-07-21 00:36:56 +00:00
|
|
|
#include "core/math/math_funcs.h"
|
2020-11-07 22:33:38 +00:00
|
|
|
#include "core/object/script_language.h"
|
2020-06-08 14:39:08 +00:00
|
|
|
#include "core/os/os.h"
|
2017-08-05 22:48:29 +00:00
|
|
|
#include "scene/main/node.h" //only so casting works
|
2018-09-11 16:13:45 +00:00
|
|
|
|
2017-01-16 07:04:19 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
void Resource::emit_changed() {
|
|
|
|
emit_signal(CoreStringNames::get_singleton()->changed);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Resource::_resource_path_changed() {
|
|
|
|
}
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void Resource::set_path(const String &p_path, bool p_take_over) {
|
2020-05-14 14:41:43 +00:00
|
|
|
if (path_cache == p_path) {
|
2014-02-10 01:10:30 +00:00
|
|
|
return;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2021-12-09 09:42:46 +00:00
|
|
|
if (!path_cache.is_empty()) {
|
2021-01-18 13:01:38 +00:00
|
|
|
ResourceCache::lock.write_lock();
|
2014-02-10 01:10:30 +00:00
|
|
|
ResourceCache::resources.erase(path_cache);
|
2021-01-18 13:01:38 +00:00
|
|
|
ResourceCache::lock.write_unlock();
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
path_cache = "";
|
2017-01-07 21:25:37 +00:00
|
|
|
|
2021-01-18 13:01:38 +00:00
|
|
|
ResourceCache::lock.read_lock();
|
2017-03-05 15:44:50 +00:00
|
|
|
bool has_path = ResourceCache::resources.has(p_path);
|
2021-01-18 13:01:38 +00:00
|
|
|
ResourceCache::lock.read_unlock();
|
2017-01-07 21:25:37 +00:00
|
|
|
|
|
|
|
if (has_path) {
|
2014-06-28 02:21:45 +00:00
|
|
|
if (p_take_over) {
|
2021-01-18 13:01:38 +00:00
|
|
|
ResourceCache::lock.write_lock();
|
2019-12-12 06:30:13 +00:00
|
|
|
Resource **res = ResourceCache::resources.getptr(p_path);
|
|
|
|
if (res) {
|
|
|
|
(*res)->set_name("");
|
|
|
|
}
|
2021-01-18 13:01:38 +00:00
|
|
|
ResourceCache::lock.write_unlock();
|
2014-06-28 02:21:45 +00:00
|
|
|
} else {
|
2021-01-18 13:01:38 +00:00
|
|
|
ResourceCache::lock.read_lock();
|
2017-03-05 15:44:50 +00:00
|
|
|
bool exists = ResourceCache::resources.has(p_path);
|
2021-01-18 13:01:38 +00:00
|
|
|
ResourceCache::lock.read_unlock();
|
2017-01-07 21:25:37 +00:00
|
|
|
|
2019-09-25 08:28:50 +00:00
|
|
|
ERR_FAIL_COND_MSG(exists, "Another resource is loaded from path '" + p_path + "' (possible cyclic resource inclusion).");
|
2014-06-28 02:21:45 +00:00
|
|
|
}
|
|
|
|
}
|
2017-03-05 15:44:50 +00:00
|
|
|
path_cache = p_path;
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2021-12-09 09:42:46 +00:00
|
|
|
if (!path_cache.is_empty()) {
|
2021-01-18 13:01:38 +00:00
|
|
|
ResourceCache::lock.write_lock();
|
2017-03-05 15:44:50 +00:00
|
|
|
ResourceCache::resources[path_cache] = this;
|
2021-01-18 13:01:38 +00:00
|
|
|
ResourceCache::lock.write_unlock();
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_resource_path_changed();
|
|
|
|
}
|
|
|
|
|
|
|
|
String Resource::get_path() const {
|
|
|
|
return path_cache;
|
|
|
|
}
|
|
|
|
|
2021-07-21 00:36:56 +00: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.
|
|
|
|
OS::Date date = OS::get_singleton()->get_date();
|
|
|
|
OS::Time time = OS::get_singleton()->get_time();
|
|
|
|
uint32_t hash = hash_djb2_one_32(OS::get_singleton()->get_ticks_usec());
|
|
|
|
hash = hash_djb2_one_32(date.year, hash);
|
|
|
|
hash = hash_djb2_one_32(date.month, hash);
|
|
|
|
hash = hash_djb2_one_32(date.day, hash);
|
|
|
|
hash = hash_djb2_one_32(time.hour, hash);
|
|
|
|
hash = hash_djb2_one_32(time.minute, hash);
|
|
|
|
hash = hash_djb2_one_32(time.second, hash);
|
|
|
|
hash = hash_djb2_one_32(Math::rand(), hash);
|
|
|
|
|
|
|
|
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) {
|
|
|
|
scene_unique_id = p_id;
|
2015-06-22 03:03:19 +00:00
|
|
|
}
|
|
|
|
|
2021-07-21 00:36:56 +00:00
|
|
|
String Resource::get_scene_unique_id() const {
|
|
|
|
return scene_unique_id;
|
2015-06-22 03:03:19 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void Resource::set_name(const String &p_name) {
|
|
|
|
name = p_name;
|
2021-10-14 11:28:45 +00:00
|
|
|
emit_changed();
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
2020-05-14 12:29:06 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
String Resource::get_name() const {
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
2021-04-27 15:43:49 +00:00
|
|
|
void Resource::update_configuration_warning() {
|
|
|
|
if (_update_configuration_warning) {
|
|
|
|
_update_configuration_warning();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-27 16:17:20 +00:00
|
|
|
bool Resource::editor_can_reload_from_file() {
|
|
|
|
return true; //by default yes
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2021-02-11 17:18:45 +00:00
|
|
|
void Resource::reset_state() {
|
|
|
|
}
|
|
|
|
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 14:41:43 +00:00
|
|
|
}
|
2016-06-27 16:17:20 +00:00
|
|
|
|
2021-02-11 17:18:45 +00:00
|
|
|
reset_state(); //may want to reset state
|
2016-06-27 16:17:20 +00:00
|
|
|
|
|
|
|
List<PropertyInfo> pi;
|
2021-02-11 17:18:45 +00:00
|
|
|
p_resource->get_property_list(&pi);
|
2016-06-27 16:17:20 +00:00
|
|
|
|
2021-07-24 13:46:25 +00:00
|
|
|
for (const PropertyInfo &E : pi) {
|
2021-07-16 03:45:57 +00:00
|
|
|
if (!(E.usage & PROPERTY_USAGE_STORAGE)) {
|
2016-06-27 16:17:20 +00:00
|
|
|
continue;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2021-07-16 03:45:57 +00:00
|
|
|
if (E.name == "resource_path") {
|
2016-06-27 16:17:20 +00:00
|
|
|
continue; //do not change path
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2016-06-27 16:17:20 +00:00
|
|
|
|
2021-07-16 03:45:57 +00:00
|
|
|
set(E.name, p_resource->get(E.name));
|
2016-06-27 16:17:20 +00:00
|
|
|
}
|
2021-02-11 17:18:45 +00:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
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 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2020-03-17 06:33:00 +00:00
|
|
|
Ref<Resource> Resource::duplicate_for_local_scene(Node *p_for_scene, Map<Ref<Resource>, Ref<Resource>> &remap_cache) {
|
2017-01-10 04:04:31 +00:00
|
|
|
List<PropertyInfo> plist;
|
|
|
|
get_property_list(&plist);
|
|
|
|
|
2021-06-17 22:03:09 +00:00
|
|
|
Ref<Resource> r = Object::cast_to<Resource>(ClassDB::instantiate(get_class()));
|
2020-11-18 22:45:30 +00:00
|
|
|
ERR_FAIL_COND_V(r.is_null(), Ref<Resource>());
|
2017-01-10 04:04:31 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
r->local_scene = p_for_scene;
|
2017-01-10 04:04:31 +00:00
|
|
|
|
2021-07-24 13:46:25 +00:00
|
|
|
for (const PropertyInfo &E : plist) {
|
2021-07-16 03:45:57 +00:00
|
|
|
if (!(E.usage & PROPERTY_USAGE_STORAGE)) {
|
2017-01-10 04:04:31 +00:00
|
|
|
continue;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2021-07-16 03:45:57 +00:00
|
|
|
Variant p = get(E.name);
|
2017-03-05 15:44:50 +00:00
|
|
|
if (p.get_type() == Variant::OBJECT) {
|
2017-01-10 04:04:31 +00:00
|
|
|
RES sr = p;
|
|
|
|
if (sr.is_valid()) {
|
|
|
|
if (sr->is_local_to_scene()) {
|
|
|
|
if (remap_cache.has(sr)) {
|
2017-03-05 15:44:50 +00:00
|
|
|
p = remap_cache[sr];
|
2017-01-10 04:04:31 +00:00
|
|
|
} else {
|
2017-03-05 15:44:50 +00:00
|
|
|
RES dupe = sr->duplicate_for_local_scene(p_for_scene, remap_cache);
|
|
|
|
p = dupe;
|
|
|
|
remap_cache[sr] = dupe;
|
2017-01-10 04:04:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-16 03:45:57 +00:00
|
|
|
r->set(E.name, p);
|
2017-01-10 04:04:31 +00:00
|
|
|
}
|
|
|
|
|
2020-11-18 22:45:30 +00:00
|
|
|
return r;
|
2017-01-10 04:04:31 +00:00
|
|
|
}
|
|
|
|
|
2020-03-17 06:33:00 +00:00
|
|
|
void Resource::configure_for_local_scene(Node *p_for_scene, Map<Ref<Resource>, Ref<Resource>> &remap_cache) {
|
2017-12-04 18:55:20 +00:00
|
|
|
List<PropertyInfo> plist;
|
|
|
|
get_property_list(&plist);
|
|
|
|
|
|
|
|
local_scene = p_for_scene;
|
|
|
|
|
2021-07-24 13:46:25 +00:00
|
|
|
for (const PropertyInfo &E : plist) {
|
2021-07-16 03:45:57 +00:00
|
|
|
if (!(E.usage & PROPERTY_USAGE_STORAGE)) {
|
2017-12-04 18:55:20 +00:00
|
|
|
continue;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2021-07-16 03:45:57 +00:00
|
|
|
Variant p = get(E.name);
|
2017-12-04 18:55:20 +00:00
|
|
|
if (p.get_type() == Variant::OBJECT) {
|
|
|
|
RES sr = p;
|
|
|
|
if (sr.is_valid()) {
|
|
|
|
if (sr->is_local_to_scene()) {
|
|
|
|
if (!remap_cache.has(sr)) {
|
|
|
|
sr->configure_for_local_scene(p_for_scene, remap_cache);
|
|
|
|
remap_cache[sr] = sr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-17 10:36:47 +00:00
|
|
|
Ref<Resource> Resource::duplicate(bool p_subresources) const {
|
2014-02-10 01:10:30 +00:00
|
|
|
List<PropertyInfo> plist;
|
|
|
|
get_property_list(&plist);
|
|
|
|
|
2021-06-17 22:03:09 +00:00
|
|
|
Ref<Resource> r = (Resource *)ClassDB::instantiate(get_class());
|
2020-11-17 09:13:41 +00:00
|
|
|
ERR_FAIL_COND_V(r.is_null(), Ref<Resource>());
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2021-07-24 13:46:25 +00:00
|
|
|
for (const PropertyInfo &E : plist) {
|
2021-07-16 03:45:57 +00:00
|
|
|
if (!(E.usage & PROPERTY_USAGE_STORAGE)) {
|
2014-02-10 01:10:30 +00:00
|
|
|
continue;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2021-07-16 03:45:57 +00:00
|
|
|
Variant p = get(E.name);
|
2018-07-02 18:08:35 +00:00
|
|
|
|
|
|
|
if ((p.get_type() == Variant::DICTIONARY || p.get_type() == Variant::ARRAY)) {
|
2021-07-16 03:45:57 +00:00
|
|
|
r->set(E.name, p.duplicate(p_subresources));
|
|
|
|
} else if (p.get_type() == Variant::OBJECT && (p_subresources || (E.usage & PROPERTY_USAGE_DO_NOT_SHARE_ON_DUPLICATE))) {
|
2014-02-10 01:10:30 +00:00
|
|
|
RES sr = p;
|
2018-07-02 18:08:35 +00:00
|
|
|
if (sr.is_valid()) {
|
2021-07-16 03:45:57 +00:00
|
|
|
r->set(E.name, sr->duplicate(p_subresources));
|
2018-07-02 18:08:35 +00:00
|
|
|
}
|
|
|
|
} else {
|
2021-07-16 03:45:57 +00:00
|
|
|
r->set(E.name, p);
|
2018-07-02 18:08:35 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2020-11-17 09:13:41 +00:00
|
|
|
return r;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void Resource::_set_path(const String &p_path) {
|
|
|
|
set_path(p_path, false);
|
2014-06-30 04:28:05 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void Resource::_take_over_path(const String &p_path) {
|
|
|
|
set_path(p_path, true);
|
2014-06-30 04:28:05 +00:00
|
|
|
}
|
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
RID Resource::get_rid() const {
|
2022-03-10 07:17:38 +00:00
|
|
|
if (get_script_instance()) {
|
|
|
|
Callable::CallError ce;
|
|
|
|
RID ret = get_script_instance()->callp(SNAME("_get_rid"), nullptr, 0, ce);
|
|
|
|
if (ce.error == Callable::CallError::CALL_OK && ret.is_valid()) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (_get_extension() && _get_extension()->get_rid) {
|
|
|
|
RID ret;
|
|
|
|
ret.from_uint64(_get_extension()->get_rid(_get_extension_instance()));
|
|
|
|
if (ret.is_valid()) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
return RID();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Resource::register_owner(Object *p_owner) {
|
2017-08-07 10:17:31 +00:00
|
|
|
owners.insert(p_owner->get_instance_id());
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Resource::unregister_owner(Object *p_owner) {
|
2017-08-07 10:17:31 +00:00
|
|
|
owners.erase(p_owner->get_instance_id());
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Resource::notify_change_to_owners() {
|
2017-03-05 15:44:50 +00:00
|
|
|
for (Set<ObjectID>::Element *E = owners.front(); E; E = E->next()) {
|
2014-02-10 01:10:30 +00:00
|
|
|
Object *obj = ObjectDB::get_instance(E->get());
|
2019-08-15 02:57:49 +00:00
|
|
|
ERR_CONTINUE_MSG(!obj, "Object was deleted, while still owning a resource."); //wtf
|
2014-02-10 01:10:30 +00:00
|
|
|
//TODO store string
|
2017-03-05 15:44:50 +00:00
|
|
|
obj->call("resource_changed", RES(this));
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-27 17:18:40 +00:00
|
|
|
#ifdef TOOLS_ENABLED
|
|
|
|
|
|
|
|
uint32_t Resource::hash_edited_version() const {
|
|
|
|
uint32_t hash = hash_djb2_one_32(get_edited_version());
|
|
|
|
|
|
|
|
List<PropertyInfo> plist;
|
|
|
|
get_property_list(&plist);
|
|
|
|
|
2021-07-24 13:46:25 +00:00
|
|
|
for (const PropertyInfo &E : plist) {
|
2021-07-16 03:45:57 +00:00
|
|
|
if (E.usage & PROPERTY_USAGE_STORAGE && E.type == Variant::OBJECT && E.hint == PROPERTY_HINT_RESOURCE_TYPE) {
|
|
|
|
RES res = get(E.name);
|
2016-05-27 17:18:40 +00:00
|
|
|
if (res.is_valid()) {
|
2017-03-05 15:44:50 +00:00
|
|
|
hash = hash_djb2_one_32(res->hash_edited_version(), hash);
|
2016-05-27 17:18:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return hash;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2017-01-10 04:04:31 +00:00
|
|
|
void Resource::set_local_to_scene(bool p_enable) {
|
2017-03-05 15:44:50 +00:00
|
|
|
local_to_scene = p_enable;
|
2017-01-10 04:04:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Resource::is_local_to_scene() const {
|
|
|
|
return local_to_scene;
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
Node *Resource::get_local_scene() const {
|
2020-05-14 14:41:43 +00:00
|
|
|
if (local_scene) {
|
2017-01-10 04:04:31 +00:00
|
|
|
return local_scene;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2017-01-10 04:04:31 +00:00
|
|
|
|
|
|
|
if (_get_local_scene_func) {
|
|
|
|
return _get_local_scene_func();
|
|
|
|
}
|
|
|
|
|
2020-04-01 23:20:12 +00:00
|
|
|
return nullptr;
|
2017-01-10 04:04:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Resource::setup_local_to_scene() {
|
2021-08-22 01:52:44 +00:00
|
|
|
// Can't use GDVIRTUAL in Resource, so this will have to be done with a signal
|
|
|
|
emit_signal(SNAME("setup_local_to_scene_requested"));
|
2017-01-10 04:04:31 +00:00
|
|
|
}
|
|
|
|
|
2020-04-01 23:20:12 +00:00
|
|
|
Node *(*Resource::_get_local_scene_func)() = nullptr;
|
2021-04-27 15:43:49 +00:00
|
|
|
void (*Resource::_update_configuration_warning)() = nullptr;
|
2017-01-10 04:04:31 +00:00
|
|
|
|
2017-06-28 20:00:18 +00:00
|
|
|
void Resource::set_as_translation_remapped(bool p_remapped) {
|
2020-05-14 14:41:43 +00:00
|
|
|
if (remapped_list.in_list() == p_remapped) {
|
2017-06-28 20:00:18 +00:00
|
|
|
return;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2017-06-28 20:00:18 +00:00
|
|
|
|
2021-01-18 13:01:38 +00:00
|
|
|
ResourceCache::lock.write_lock();
|
2017-06-28 20:00:18 +00:00
|
|
|
|
|
|
|
if (p_remapped) {
|
|
|
|
ResourceLoader::remapped_list.add(&remapped_list);
|
|
|
|
} else {
|
|
|
|
ResourceLoader::remapped_list.remove(&remapped_list);
|
|
|
|
}
|
|
|
|
|
2021-01-18 13:01:38 +00:00
|
|
|
ResourceCache::lock.write_unlock();
|
2017-06-28 20:00:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Resource::is_translation_remapped() const {
|
|
|
|
return remapped_list.in_list();
|
|
|
|
}
|
|
|
|
|
2019-04-11 17:35:23 +00: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 00:36:56 +00:00
|
|
|
void Resource::set_id_for_path(const String &p_path, const String &p_id) {
|
2021-12-09 09:42:46 +00:00
|
|
|
if (p_id.is_empty()) {
|
2021-01-18 13:01:38 +00:00
|
|
|
ResourceCache::path_cache_lock.write_lock();
|
2019-07-16 16:46:40 +00:00
|
|
|
ResourceCache::resource_path_cache[p_path].erase(get_path());
|
2021-01-18 13:01:38 +00:00
|
|
|
ResourceCache::path_cache_lock.write_unlock();
|
2019-04-11 17:35:23 +00:00
|
|
|
} else {
|
2021-01-18 13:01:38 +00:00
|
|
|
ResourceCache::path_cache_lock.write_lock();
|
2019-07-16 16:46:40 +00:00
|
|
|
ResourceCache::resource_path_cache[p_path][get_path()] = p_id;
|
2021-01-18 13:01:38 +00:00
|
|
|
ResourceCache::path_cache_lock.write_unlock();
|
2019-04-11 17:35:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-21 00:36:56 +00:00
|
|
|
String Resource::get_id_for_path(const String &p_path) const {
|
2021-01-18 13:01:38 +00:00
|
|
|
ResourceCache::path_cache_lock.read_lock();
|
2019-07-16 16:46:40 +00:00
|
|
|
if (ResourceCache::resource_path_cache[p_path].has(get_path())) {
|
2021-07-21 00:36:56 +00:00
|
|
|
String result = ResourceCache::resource_path_cache[p_path][get_path()];
|
2021-01-18 13:01:38 +00:00
|
|
|
ResourceCache::path_cache_lock.read_unlock();
|
2019-07-16 16:46:40 +00:00
|
|
|
return result;
|
2019-04-11 17:35:23 +00:00
|
|
|
} else {
|
2021-01-18 13:01:38 +00:00
|
|
|
ResourceCache::path_cache_lock.read_unlock();
|
2021-07-21 00:36:56 +00:00
|
|
|
return "";
|
2019-04-11 17:35:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2017-01-10 04:04:31 +00:00
|
|
|
void Resource::_bind_methods() {
|
2017-03-05 15:44:50 +00: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 11:19:41 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("get_local_scene"), &Resource::get_local_scene);
|
2017-03-05 15:44:50 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("setup_local_to_scene"), &Resource::setup_local_to_scene);
|
2021-01-13 14:24:51 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("emit_changed"), &Resource::emit_changed);
|
2017-01-10 04:04:31 +00:00
|
|
|
|
2017-08-09 11:19:41 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("duplicate", "subresources"), &Resource::duplicate, DEFVAL(false));
|
2017-03-05 15:44:50 +00:00
|
|
|
ADD_SIGNAL(MethodInfo("changed"));
|
2021-08-22 01:52:44 +00:00
|
|
|
ADD_SIGNAL(MethodInfo("setup_local_to_scene_requested"));
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ADD_GROUP("Resource", "resource_");
|
2018-11-08 14:30:02 +00:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "resource_local_to_scene"), "set_local_to_scene", "is_local_to_scene");
|
2017-03-05 15:44:50 +00:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::STRING, "resource_path", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_EDITOR), "set_path", "get_path");
|
2020-02-20 21:58:05 +00:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::STRING_NAME, "resource_name"), "set_name", "get_name");
|
2022-03-10 07:17:38 +00:00
|
|
|
|
|
|
|
MethodInfo get_rid_bind("_get_rid");
|
|
|
|
get_rid_bind.return_val.type = Variant::RID;
|
|
|
|
|
|
|
|
::ClassDB::add_virtual_method(get_class_static(), get_rid_bind, true, Vector<String>(), true);
|
2017-01-10 04:04:31 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-12-06 20:36:34 +00:00
|
|
|
Resource::Resource() :
|
2020-05-12 15:01:17 +00:00
|
|
|
remapped_list(this) {}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
Resource::~Resource() {
|
2021-12-09 09:42:46 +00:00
|
|
|
if (!path_cache.is_empty()) {
|
2021-01-18 13:01:38 +00:00
|
|
|
ResourceCache::lock.write_lock();
|
2014-02-10 01:10:30 +00:00
|
|
|
ResourceCache::resources.erase(path_cache);
|
2021-01-18 13:01:38 +00:00
|
|
|
ResourceCache::lock.write_unlock();
|
2017-01-07 21:25:37 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
if (owners.size()) {
|
2019-08-15 02:57:49 +00:00
|
|
|
WARN_PRINT("Resource is still owned.");
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
HashMap<String, Resource *> ResourceCache::resources;
|
2019-07-16 16:46:40 +00:00
|
|
|
#ifdef TOOLS_ENABLED
|
2021-07-21 00:36:56 +00:00
|
|
|
HashMap<String, HashMap<String, String>> ResourceCache::resource_path_cache;
|
2019-07-16 16:46:40 +00:00
|
|
|
#endif
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2021-01-18 13:01:38 +00:00
|
|
|
RWLock ResourceCache::lock;
|
2019-07-16 16:46:40 +00:00
|
|
|
#ifdef TOOLS_ENABLED
|
2021-01-18 13:01:38 +00:00
|
|
|
RWLock ResourceCache::path_cache_lock;
|
2019-07-16 16:46:40 +00:00
|
|
|
#endif
|
2017-01-07 21:25:37 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
void ResourceCache::clear() {
|
2020-05-14 14:41:43 +00:00
|
|
|
if (resources.size()) {
|
2020-06-08 14:39:08 +00:00
|
|
|
ERR_PRINT("Resources still in use at exit (run with --verbose for details).");
|
|
|
|
if (OS::get_singleton()->is_stdout_verbose()) {
|
|
|
|
const String *K = nullptr;
|
|
|
|
while ((K = resources.next(K))) {
|
|
|
|
Resource *r = resources[*K];
|
|
|
|
print_line(vformat("Resource still in use: %s (%s)", *K, r->get_class()));
|
|
|
|
}
|
|
|
|
}
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
resources.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ResourceCache::reload_externals() {
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
bool ResourceCache::has(const String &p_path) {
|
2021-01-18 13:01:38 +00:00
|
|
|
lock.read_lock();
|
2017-01-07 21:25:37 +00:00
|
|
|
bool b = resources.has(p_path);
|
2021-01-18 13:01:38 +00:00
|
|
|
lock.read_unlock();
|
2017-01-07 21:25:37 +00:00
|
|
|
|
|
|
|
return b;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
2020-05-14 12:29:06 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
Resource *ResourceCache::get(const String &p_path) {
|
2021-01-18 13:01:38 +00:00
|
|
|
lock.read_lock();
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
Resource **res = resources.getptr(p_path);
|
2017-01-07 21:25:37 +00:00
|
|
|
|
2021-01-18 13:01:38 +00:00
|
|
|
lock.read_unlock();
|
2017-01-07 21:25:37 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
if (!res) {
|
2020-04-01 23:20:12 +00:00
|
|
|
return nullptr;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
return *res;
|
|
|
|
}
|
|
|
|
|
2020-03-17 06:33:00 +00:00
|
|
|
void ResourceCache::get_cached_resources(List<Ref<Resource>> *p_resources) {
|
2021-01-18 13:01:38 +00:00
|
|
|
lock.read_lock();
|
2020-04-01 23:20:12 +00:00
|
|
|
const String *K = nullptr;
|
2017-03-05 15:44:50 +00:00
|
|
|
while ((K = resources.next(K))) {
|
2014-02-10 01:10:30 +00:00
|
|
|
Resource *r = resources[*K];
|
2017-03-05 15:44:50 +00:00
|
|
|
p_resources->push_back(Ref<Resource>(r));
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
2021-01-18 13:01:38 +00:00
|
|
|
lock.read_unlock();
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int ResourceCache::get_cached_resource_count() {
|
2021-01-18 13:01:38 +00:00
|
|
|
lock.read_lock();
|
2017-01-07 21:25:37 +00:00
|
|
|
int rc = resources.size();
|
2021-01-18 13:01:38 +00:00
|
|
|
lock.read_unlock();
|
2017-01-07 21:25:37 +00:00
|
|
|
|
|
|
|
return rc;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void ResourceCache::dump(const char *p_file, bool p_short) {
|
2014-02-10 01:10:30 +00:00
|
|
|
#ifdef DEBUG_ENABLED
|
2021-01-18 13:01:38 +00:00
|
|
|
lock.read_lock();
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
Map<String, int> type_count;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2022-03-23 09:08:58 +00:00
|
|
|
Ref<FileAccess> f;
|
2014-02-10 01:10:30 +00:00
|
|
|
if (p_file) {
|
2022-01-06 09:34:10 +00:00
|
|
|
f = FileAccess::open(String::utf8(p_file), FileAccess::WRITE);
|
2022-03-23 09:08:58 +00:00
|
|
|
ERR_FAIL_COND_MSG(f.is_null(), "Cannot create file at path '" + String::utf8(p_file) + "'.");
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2020-04-01 23:20:12 +00:00
|
|
|
const String *K = nullptr;
|
2017-03-05 15:44:50 +00:00
|
|
|
while ((K = resources.next(K))) {
|
2014-02-10 01:10:30 +00:00
|
|
|
Resource *r = resources[*K];
|
|
|
|
|
2017-01-03 02:03:46 +00:00
|
|
|
if (!type_count.has(r->get_class())) {
|
2017-03-05 15:44:50 +00:00
|
|
|
type_count[r->get_class()] = 0;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-01-03 02:03:46 +00:00
|
|
|
type_count[r->get_class()]++;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
if (!p_short) {
|
2022-03-23 09:08:58 +00:00
|
|
|
if (f.is_valid()) {
|
2017-03-05 15:44:50 +00:00
|
|
|
f->store_line(r->get_class() + ": " + r->get_path());
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-09 20:13:42 +00:00
|
|
|
for (const KeyValue<String, int> &E : type_count) {
|
2022-03-23 09:08:58 +00:00
|
|
|
if (f.is_valid()) {
|
2021-08-09 20:13:42 +00:00
|
|
|
f->store_line(E.key + " count: " + itos(E.value));
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2021-01-18 13:01:38 +00:00
|
|
|
lock.read_unlock();
|
2021-02-28 11:10:47 +00:00
|
|
|
#else
|
|
|
|
WARN_PRINT("ResourceCache::dump only with in debug builds.");
|
2014-02-10 01:10:30 +00:00
|
|
|
#endif
|
|
|
|
}
|