2014-02-10 09:10:30 +08:00
|
|
|
/*************************************************************************/
|
|
|
|
/* resource.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2017-08-27 20:16:55 +08:00
|
|
|
/* https://godotengine.org */
|
2014-02-10 09:10:30 +08:00
|
|
|
/*************************************************************************/
|
2019-01-01 19:53:14 +08:00
|
|
|
/* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
|
2014-02-10 09:10:30 +08: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-05 07:50:27 +08:00
|
|
|
|
2014-02-10 09:10:30 +08:00
|
|
|
#include "resource.h"
|
2017-01-16 15:04:19 +08:00
|
|
|
|
2018-09-12 00:13:45 +08:00
|
|
|
#include "core/core_string_names.h"
|
|
|
|
#include "core/io/resource_loader.h"
|
|
|
|
#include "core/os/file_access.h"
|
|
|
|
#include "core/script_language.h"
|
2017-08-06 06:48:29 +08:00
|
|
|
#include "scene/main/node.h" //only so casting works
|
2018-09-12 00:13:45 +08:00
|
|
|
|
2017-01-16 15:04:19 +08:00
|
|
|
#include <stdio.h>
|
|
|
|
|
2014-02-10 09:10:30 +08:00
|
|
|
void Resource::emit_changed() {
|
|
|
|
|
|
|
|
emit_signal(CoreStringNames::get_singleton()->changed);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Resource::_resource_path_changed() {
|
|
|
|
}
|
2016-03-09 07:00:52 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
void Resource::set_path(const String &p_path, bool p_take_over) {
|
2014-02-10 09:10:30 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
if (path_cache == p_path)
|
2014-02-10 09:10:30 +08:00
|
|
|
return;
|
2016-03-09 07:00:52 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
if (path_cache != "") {
|
2016-03-09 07:00:52 +08:00
|
|
|
|
2017-01-08 05:25:37 +08:00
|
|
|
ResourceCache::lock->write_lock();
|
2014-02-10 09:10:30 +08:00
|
|
|
ResourceCache::resources.erase(path_cache);
|
2017-01-08 05:25:37 +08:00
|
|
|
ResourceCache::lock->write_unlock();
|
2014-02-10 09:10:30 +08:00
|
|
|
}
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
path_cache = "";
|
2017-01-08 05:25:37 +08:00
|
|
|
|
|
|
|
ResourceCache::lock->read_lock();
|
2017-03-05 23:44:50 +08:00
|
|
|
bool has_path = ResourceCache::resources.has(p_path);
|
2017-01-08 05:25:37 +08:00
|
|
|
ResourceCache::lock->read_unlock();
|
|
|
|
|
|
|
|
if (has_path) {
|
2014-06-28 10:21:45 +08:00
|
|
|
if (p_take_over) {
|
|
|
|
|
2017-01-08 05:25:37 +08:00
|
|
|
ResourceCache::lock->write_lock();
|
2014-06-28 10:21:45 +08:00
|
|
|
ResourceCache::resources.get(p_path)->set_name("");
|
2017-01-08 05:25:37 +08:00
|
|
|
ResourceCache::lock->write_unlock();
|
2014-06-28 10:21:45 +08:00
|
|
|
} else {
|
2017-01-08 05:25:37 +08:00
|
|
|
ResourceCache::lock->read_lock();
|
2017-03-05 23:44:50 +08:00
|
|
|
bool exists = ResourceCache::resources.has(p_path);
|
2017-01-08 05:25:37 +08:00
|
|
|
ResourceCache::lock->read_unlock();
|
|
|
|
|
2019-08-15 10:57:49 +08:00
|
|
|
ERR_FAIL_COND_MSG(exists, "Another resource is loaded from path: " + p_path + " (possible cyclic resource inclusion).");
|
2014-06-28 10:21:45 +08:00
|
|
|
}
|
|
|
|
}
|
2017-03-05 23:44:50 +08:00
|
|
|
path_cache = p_path;
|
2016-03-09 07:00:52 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
if (path_cache != "") {
|
2016-03-09 07:00:52 +08:00
|
|
|
|
2017-01-08 05:25:37 +08:00
|
|
|
ResourceCache::lock->write_lock();
|
2017-03-05 23:44:50 +08:00
|
|
|
ResourceCache::resources[path_cache] = this;
|
2017-01-08 05:25:37 +08:00
|
|
|
ResourceCache::lock->write_unlock();
|
2014-02-10 09:10:30 +08:00
|
|
|
}
|
|
|
|
|
2017-01-10 12:04:31 +08:00
|
|
|
_change_notify("resource_path");
|
2014-02-10 09:10:30 +08:00
|
|
|
_resource_path_changed();
|
|
|
|
}
|
|
|
|
|
|
|
|
String Resource::get_path() const {
|
2016-03-09 07:00:52 +08:00
|
|
|
|
2014-02-10 09:10:30 +08:00
|
|
|
return path_cache;
|
|
|
|
}
|
|
|
|
|
2015-06-22 11:03:19 +08:00
|
|
|
void Resource::set_subindex(int p_sub_index) {
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
subindex = p_sub_index;
|
2015-06-22 11:03:19 +08:00
|
|
|
}
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
int Resource::get_subindex() const {
|
2015-06-22 11:03:19 +08:00
|
|
|
|
|
|
|
return subindex;
|
|
|
|
}
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
void Resource::set_name(const String &p_name) {
|
2015-06-22 11:03:19 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
name = p_name;
|
2017-01-10 12:04:31 +08:00
|
|
|
_change_notify("resource_name");
|
2014-02-10 09:10:30 +08:00
|
|
|
}
|
|
|
|
String Resource::get_name() const {
|
|
|
|
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
2016-06-28 00:17:20 +08:00
|
|
|
bool Resource::editor_can_reload_from_file() {
|
2014-02-10 09:10:30 +08:00
|
|
|
|
2016-06-28 00:17:20 +08:00
|
|
|
return true; //by default yes
|
2014-02-10 09:10:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Resource::reload_from_file() {
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
String path = get_path();
|
2016-06-28 00:17:20 +08:00
|
|
|
if (!path.is_resource_file())
|
|
|
|
return;
|
|
|
|
|
2017-06-29 04:00:18 +08:00
|
|
|
Ref<Resource> s = ResourceLoader::load(ResourceLoader::path_remap(path), get_class(), true);
|
2016-06-28 00:17:20 +08:00
|
|
|
|
|
|
|
if (!s.is_valid())
|
|
|
|
return;
|
|
|
|
|
|
|
|
List<PropertyInfo> pi;
|
|
|
|
s->get_property_list(&pi);
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
for (List<PropertyInfo>::Element *E = pi.front(); E; E = E->next()) {
|
2016-06-28 00:17:20 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
if (!(E->get().usage & PROPERTY_USAGE_STORAGE))
|
2016-06-28 00:17:20 +08:00
|
|
|
continue;
|
2017-03-05 23:44:50 +08:00
|
|
|
if (E->get().name == "resource_path")
|
2016-06-28 00:17:20 +08:00
|
|
|
continue; //do not change path
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
set(E->get().name, s->get(E->get().name));
|
2016-06-28 00:17:20 +08:00
|
|
|
}
|
2014-02-10 09:10:30 +08:00
|
|
|
}
|
|
|
|
|
2017-01-10 12:04:31 +08:00
|
|
|
Ref<Resource> Resource::duplicate_for_local_scene(Node *p_for_scene, Map<Ref<Resource>, Ref<Resource> > &remap_cache) {
|
|
|
|
|
|
|
|
List<PropertyInfo> plist;
|
|
|
|
get_property_list(&plist);
|
|
|
|
|
2018-08-21 03:35:36 +08:00
|
|
|
Resource *r = Object::cast_to<Resource>(ClassDB::instance(get_class()));
|
2017-03-05 23:44:50 +08:00
|
|
|
ERR_FAIL_COND_V(!r, Ref<Resource>());
|
2017-01-10 12:04:31 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
r->local_scene = p_for_scene;
|
2017-01-10 12:04:31 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
for (List<PropertyInfo>::Element *E = plist.front(); E; E = E->next()) {
|
2017-01-10 12:04:31 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
if (!(E->get().usage & PROPERTY_USAGE_STORAGE))
|
2017-01-10 12:04:31 +08:00
|
|
|
continue;
|
|
|
|
Variant p = get(E->get().name);
|
2017-03-05 23:44:50 +08:00
|
|
|
if (p.get_type() == Variant::OBJECT) {
|
2017-01-10 12:04:31 +08:00
|
|
|
|
|
|
|
RES sr = p;
|
|
|
|
if (sr.is_valid()) {
|
|
|
|
|
|
|
|
if (sr->is_local_to_scene()) {
|
|
|
|
if (remap_cache.has(sr)) {
|
2017-03-05 23:44:50 +08:00
|
|
|
p = remap_cache[sr];
|
2017-01-10 12:04:31 +08:00
|
|
|
} else {
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
RES dupe = sr->duplicate_for_local_scene(p_for_scene, remap_cache);
|
|
|
|
p = dupe;
|
|
|
|
remap_cache[sr] = dupe;
|
2017-01-10 12:04:31 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
r->set(E->get().name, p);
|
2017-01-10 12:04:31 +08:00
|
|
|
}
|
|
|
|
|
2018-08-21 03:35:36 +08:00
|
|
|
RES res = Ref<Resource>(r);
|
|
|
|
|
|
|
|
return res;
|
2017-01-10 12:04:31 +08:00
|
|
|
}
|
|
|
|
|
2017-12-05 02:55:20 +08:00
|
|
|
void Resource::configure_for_local_scene(Node *p_for_scene, Map<Ref<Resource>, Ref<Resource> > &remap_cache) {
|
|
|
|
|
|
|
|
List<PropertyInfo> plist;
|
|
|
|
get_property_list(&plist);
|
|
|
|
|
|
|
|
local_scene = p_for_scene;
|
|
|
|
|
|
|
|
for (List<PropertyInfo>::Element *E = plist.front(); E; E = E->next()) {
|
|
|
|
|
|
|
|
if (!(E->get().usage & PROPERTY_USAGE_STORAGE))
|
|
|
|
continue;
|
|
|
|
Variant p = get(E->get().name);
|
|
|
|
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 18:36:47 +08:00
|
|
|
Ref<Resource> Resource::duplicate(bool p_subresources) const {
|
2014-02-10 09:10:30 +08:00
|
|
|
|
|
|
|
List<PropertyInfo> plist;
|
|
|
|
get_property_list(&plist);
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
Resource *r = (Resource *)ClassDB::instance(get_class());
|
|
|
|
ERR_FAIL_COND_V(!r, Ref<Resource>());
|
2014-02-10 09:10:30 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
for (List<PropertyInfo>::Element *E = plist.front(); E; E = E->next()) {
|
2014-02-10 09:10:30 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
if (!(E->get().usage & PROPERTY_USAGE_STORAGE))
|
2014-02-10 09:10:30 +08:00
|
|
|
continue;
|
2018-07-03 02:08:35 +08:00
|
|
|
Variant p = get(E->get().name);
|
|
|
|
|
|
|
|
if ((p.get_type() == Variant::DICTIONARY || p.get_type() == Variant::ARRAY)) {
|
2018-09-01 19:56:17 +08:00
|
|
|
r->set(E->get().name, p.duplicate(p_subresources));
|
2018-07-03 02:08:35 +08:00
|
|
|
} else if (p.get_type() == Variant::OBJECT && (p_subresources || (E->get().usage & PROPERTY_USAGE_DO_NOT_SHARE_ON_DUPLICATE))) {
|
2014-02-10 09:10:30 +08:00
|
|
|
|
|
|
|
RES sr = p;
|
2018-07-03 02:08:35 +08:00
|
|
|
if (sr.is_valid()) {
|
|
|
|
r->set(E->get().name, sr->duplicate(p_subresources));
|
|
|
|
}
|
|
|
|
} else {
|
2014-02-10 09:10:30 +08:00
|
|
|
|
2018-07-03 02:08:35 +08:00
|
|
|
r->set(E->get().name, p);
|
|
|
|
}
|
2014-02-10 09:10:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return Ref<Resource>(r);
|
|
|
|
}
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
void Resource::_set_path(const String &p_path) {
|
2014-02-10 09:10:30 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
set_path(p_path, false);
|
2014-06-30 12:28:05 +08:00
|
|
|
}
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
void Resource::_take_over_path(const String &p_path) {
|
2014-06-30 12:28:05 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
set_path(p_path, true);
|
2014-06-30 12:28:05 +08:00
|
|
|
}
|
|
|
|
|
2014-02-10 09:10:30 +08:00
|
|
|
RID Resource::get_rid() const {
|
|
|
|
|
|
|
|
return RID();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Resource::register_owner(Object *p_owner) {
|
|
|
|
|
2017-08-07 18:17:31 +08:00
|
|
|
owners.insert(p_owner->get_instance_id());
|
2014-02-10 09:10:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Resource::unregister_owner(Object *p_owner) {
|
|
|
|
|
2017-08-07 18:17:31 +08:00
|
|
|
owners.erase(p_owner->get_instance_id());
|
2014-02-10 09:10:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Resource::notify_change_to_owners() {
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
for (Set<ObjectID>::Element *E = owners.front(); E; E = E->next()) {
|
2014-02-10 09:10:30 +08:00
|
|
|
|
|
|
|
Object *obj = ObjectDB::get_instance(E->get());
|
2019-08-15 10:57:49 +08:00
|
|
|
ERR_CONTINUE_MSG(!obj, "Object was deleted, while still owning a resource."); //wtf
|
2014-02-10 09:10:30 +08:00
|
|
|
//TODO store string
|
2017-03-05 23:44:50 +08:00
|
|
|
obj->call("resource_changed", RES(this));
|
2014-02-10 09:10:30 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-28 01:18:40 +08: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);
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
for (List<PropertyInfo>::Element *E = plist.front(); E; E = E->next()) {
|
2016-05-28 01:18:40 +08:00
|
|
|
|
2018-06-22 05:46:17 +08:00
|
|
|
if (E->get().usage & PROPERTY_USAGE_STORAGE && E->get().type == Variant::OBJECT && E->get().hint == PROPERTY_HINT_RESOURCE_TYPE) {
|
2016-05-28 01:18:40 +08:00
|
|
|
RES res = get(E->get().name);
|
|
|
|
if (res.is_valid()) {
|
2017-03-05 23:44:50 +08:00
|
|
|
hash = hash_djb2_one_32(res->hash_edited_version(), hash);
|
2016-05-28 01:18:40 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return hash;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2017-01-10 12:04:31 +08:00
|
|
|
void Resource::set_local_to_scene(bool p_enable) {
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
local_to_scene = p_enable;
|
2017-01-10 12:04:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Resource::is_local_to_scene() const {
|
|
|
|
|
|
|
|
return local_to_scene;
|
|
|
|
}
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
Node *Resource::get_local_scene() const {
|
2017-01-10 12:04:31 +08:00
|
|
|
|
|
|
|
if (local_scene)
|
|
|
|
return local_scene;
|
|
|
|
|
|
|
|
if (_get_local_scene_func) {
|
|
|
|
return _get_local_scene_func();
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Resource::setup_local_to_scene() {
|
|
|
|
|
|
|
|
if (get_script_instance())
|
|
|
|
get_script_instance()->call("_setup_local_to_scene");
|
|
|
|
}
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
Node *(*Resource::_get_local_scene_func)() = NULL;
|
2017-01-10 12:04:31 +08:00
|
|
|
|
2017-06-29 04:00:18 +08:00
|
|
|
void Resource::set_as_translation_remapped(bool p_remapped) {
|
|
|
|
|
|
|
|
if (remapped_list.in_list() == p_remapped)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (ResourceCache::lock) {
|
|
|
|
ResourceCache::lock->write_lock();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (p_remapped) {
|
|
|
|
ResourceLoader::remapped_list.add(&remapped_list);
|
|
|
|
} else {
|
|
|
|
ResourceLoader::remapped_list.remove(&remapped_list);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ResourceCache::lock) {
|
|
|
|
ResourceCache::lock->write_unlock();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Resource::is_translation_remapped() const {
|
|
|
|
|
|
|
|
return remapped_list.in_list();
|
|
|
|
}
|
|
|
|
|
2019-04-12 01:35:23 +08:00
|
|
|
#ifdef TOOLS_ENABLED
|
|
|
|
//helps keep IDs same number when loading/saving scenes. -1 clears ID and it Returns -1 when no id stored
|
|
|
|
void Resource::set_id_for_path(const String &p_path, int p_id) {
|
|
|
|
if (p_id == -1) {
|
|
|
|
id_for_path.erase(p_path);
|
|
|
|
} else {
|
|
|
|
id_for_path[p_path] = p_id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int Resource::get_id_for_path(const String &p_path) const {
|
|
|
|
|
|
|
|
if (id_for_path.has(p_path)) {
|
|
|
|
return id_for_path[p_path];
|
|
|
|
} else {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2017-01-10 12:04:31 +08:00
|
|
|
void Resource::_bind_methods() {
|
|
|
|
|
2017-03-05 23:44:50 +08: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 19:19:41 +08:00
|
|
|
ClassDB::bind_method(D_METHOD("get_local_scene"), &Resource::get_local_scene);
|
2017-03-05 23:44:50 +08:00
|
|
|
ClassDB::bind_method(D_METHOD("setup_local_to_scene"), &Resource::setup_local_to_scene);
|
2017-01-10 12:04:31 +08:00
|
|
|
|
2017-08-09 19:19:41 +08:00
|
|
|
ClassDB::bind_method(D_METHOD("duplicate", "subresources"), &Resource::duplicate, DEFVAL(false));
|
2017-03-05 23:44:50 +08:00
|
|
|
ADD_SIGNAL(MethodInfo("changed"));
|
|
|
|
ADD_GROUP("Resource", "resource_");
|
2018-11-08 22:30:02 +08:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "resource_local_to_scene"), "set_local_to_scene", "is_local_to_scene");
|
2017-03-05 23:44:50 +08:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::STRING, "resource_path", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_EDITOR), "set_path", "get_path");
|
2018-11-08 22:30:02 +08:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::STRING, "resource_name"), "set_name", "get_name");
|
2017-01-10 12:04:31 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
BIND_VMETHOD(MethodInfo("_setup_local_to_scene"));
|
2017-01-10 12:04:31 +08:00
|
|
|
}
|
2014-02-10 09:10:30 +08:00
|
|
|
|
2017-12-07 04:36:34 +08:00
|
|
|
Resource::Resource() :
|
|
|
|
remapped_list(this) {
|
2014-02-10 09:10:30 +08:00
|
|
|
|
|
|
|
#ifdef TOOLS_ENABLED
|
2017-03-05 23:44:50 +08:00
|
|
|
last_modified_time = 0;
|
|
|
|
import_last_modified_time = 0;
|
2014-02-10 09:10:30 +08:00
|
|
|
#endif
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
subindex = 0;
|
|
|
|
local_to_scene = false;
|
|
|
|
local_scene = NULL;
|
2014-02-10 09:10:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Resource::~Resource() {
|
2016-03-09 07:00:52 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
if (path_cache != "") {
|
2017-01-08 05:25:37 +08:00
|
|
|
ResourceCache::lock->write_lock();
|
2014-02-10 09:10:30 +08:00
|
|
|
ResourceCache::resources.erase(path_cache);
|
2017-01-08 05:25:37 +08:00
|
|
|
ResourceCache::lock->write_unlock();
|
|
|
|
}
|
2014-02-10 09:10:30 +08:00
|
|
|
if (owners.size()) {
|
2019-08-15 10:57:49 +08:00
|
|
|
WARN_PRINT("Resource is still owned.");
|
2014-02-10 09:10:30 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
HashMap<String, Resource *> ResourceCache::resources;
|
2014-02-10 09:10:30 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
RWLock *ResourceCache::lock = NULL;
|
2017-01-08 05:25:37 +08:00
|
|
|
|
|
|
|
void ResourceCache::setup() {
|
|
|
|
|
|
|
|
lock = RWLock::create();
|
|
|
|
}
|
|
|
|
|
2014-02-10 09:10:30 +08:00
|
|
|
void ResourceCache::clear() {
|
|
|
|
if (resources.size())
|
|
|
|
ERR_PRINT("Resources Still in use at Exit!");
|
2016-03-09 07:00:52 +08:00
|
|
|
|
2014-02-10 09:10:30 +08:00
|
|
|
resources.clear();
|
2017-01-08 05:25:37 +08:00
|
|
|
memdelete(lock);
|
2014-02-10 09:10:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ResourceCache::reload_externals() {
|
|
|
|
|
2017-01-14 19:26:56 +08:00
|
|
|
/*
|
|
|
|
const String *K=NULL;
|
|
|
|
while ((K=resources.next(K))) {
|
|
|
|
resources[*K]->reload_external_data();
|
|
|
|
}
|
|
|
|
*/
|
2014-02-10 09:10:30 +08:00
|
|
|
}
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
bool ResourceCache::has(const String &p_path) {
|
2014-02-10 09:10:30 +08:00
|
|
|
|
2017-01-15 01:03:38 +08:00
|
|
|
lock->read_lock();
|
2017-01-08 05:25:37 +08:00
|
|
|
bool b = resources.has(p_path);
|
2017-01-15 01:03:38 +08:00
|
|
|
lock->read_unlock();
|
2017-01-08 05:25:37 +08:00
|
|
|
|
|
|
|
return b;
|
2014-02-10 09:10:30 +08:00
|
|
|
}
|
2017-03-05 23:44:50 +08:00
|
|
|
Resource *ResourceCache::get(const String &p_path) {
|
2016-03-09 07:00:52 +08:00
|
|
|
|
2017-01-08 05:25:37 +08:00
|
|
|
lock->read_lock();
|
2016-03-09 07:00:52 +08:00
|
|
|
|
2014-02-10 09:10:30 +08:00
|
|
|
Resource **res = resources.getptr(p_path);
|
2017-01-08 05:25:37 +08:00
|
|
|
|
|
|
|
lock->read_unlock();
|
|
|
|
|
2014-02-10 09:10:30 +08:00
|
|
|
if (!res) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2016-03-09 07:00:52 +08:00
|
|
|
|
2014-02-10 09:10:30 +08:00
|
|
|
return *res;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ResourceCache::get_cached_resources(List<Ref<Resource> > *p_resources) {
|
|
|
|
|
2017-01-08 05:25:37 +08:00
|
|
|
lock->read_lock();
|
2017-03-05 23:44:50 +08:00
|
|
|
const String *K = NULL;
|
|
|
|
while ((K = resources.next(K))) {
|
2014-02-10 09:10:30 +08:00
|
|
|
|
|
|
|
Resource *r = resources[*K];
|
2017-03-05 23:44:50 +08:00
|
|
|
p_resources->push_back(Ref<Resource>(r));
|
2014-02-10 09:10:30 +08:00
|
|
|
}
|
2017-01-08 05:25:37 +08:00
|
|
|
lock->read_unlock();
|
2014-02-10 09:10:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int ResourceCache::get_cached_resource_count() {
|
|
|
|
|
2017-01-08 05:25:37 +08:00
|
|
|
lock->read_lock();
|
|
|
|
int rc = resources.size();
|
|
|
|
lock->read_unlock();
|
|
|
|
|
|
|
|
return rc;
|
2014-02-10 09:10:30 +08:00
|
|
|
}
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
void ResourceCache::dump(const char *p_file, bool p_short) {
|
2014-02-10 09:10:30 +08:00
|
|
|
#ifdef DEBUG_ENABLED
|
2017-01-08 05:25:37 +08:00
|
|
|
lock->read_lock();
|
2014-02-10 09:10:30 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
Map<String, int> type_count;
|
2014-02-10 09:10:30 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
FileAccess *f = NULL;
|
2014-02-10 09:10:30 +08:00
|
|
|
if (p_file) {
|
2017-03-05 23:44:50 +08:00
|
|
|
f = FileAccess::open(p_file, FileAccess::WRITE);
|
2014-02-10 09:10:30 +08:00
|
|
|
ERR_FAIL_COND(!f);
|
|
|
|
}
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
const String *K = NULL;
|
|
|
|
while ((K = resources.next(K))) {
|
2014-02-10 09:10:30 +08:00
|
|
|
|
|
|
|
Resource *r = resources[*K];
|
|
|
|
|
2017-01-03 10:03:46 +08:00
|
|
|
if (!type_count.has(r->get_class())) {
|
2017-03-05 23:44:50 +08:00
|
|
|
type_count[r->get_class()] = 0;
|
2014-02-10 09:10:30 +08:00
|
|
|
}
|
|
|
|
|
2017-01-03 10:03:46 +08:00
|
|
|
type_count[r->get_class()]++;
|
2014-02-10 09:10:30 +08:00
|
|
|
|
|
|
|
if (!p_short) {
|
|
|
|
if (f)
|
2017-03-05 23:44:50 +08:00
|
|
|
f->store_line(r->get_class() + ": " + r->get_path());
|
2014-02-10 09:10:30 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
for (Map<String, int>::Element *E = type_count.front(); E; E = E->next()) {
|
2014-02-10 09:10:30 +08:00
|
|
|
|
|
|
|
if (f)
|
2017-03-05 23:44:50 +08:00
|
|
|
f->store_line(E->key() + " count: " + itos(E->get()));
|
2014-02-10 09:10:30 +08:00
|
|
|
}
|
|
|
|
if (f) {
|
|
|
|
f->close();
|
|
|
|
memdelete(f);
|
|
|
|
}
|
|
|
|
|
2017-01-08 05:25:37 +08:00
|
|
|
lock->read_unlock();
|
|
|
|
|
2014-02-10 09:10:30 +08:00
|
|
|
#endif
|
|
|
|
}
|