summaryrefslogtreecommitdiff
path: root/core/resource.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'core/resource.cpp')
-rw-r--r--core/resource.cpp125
1 files changed, 40 insertions, 85 deletions
diff --git a/core/resource.cpp b/core/resource.cpp
index 8d5c441b21..3b589793ef 100644
--- a/core/resource.cpp
+++ b/core/resource.cpp
@@ -33,13 +33,13 @@
#include "core/core_string_names.h"
#include "core/io/resource_loader.h"
#include "core/os/file_access.h"
+#include "core/os/os.h"
#include "core/script_language.h"
#include "scene/main/node.h" //only so casting works
#include <stdio.h>
void Resource::emit_changed() {
-
emit_signal(CoreStringNames::get_singleton()->changed);
}
@@ -47,12 +47,11 @@ void Resource::_resource_path_changed() {
}
void Resource::set_path(const String &p_path, bool p_take_over) {
-
- if (path_cache == p_path)
+ if (path_cache == p_path) {
return;
+ }
if (path_cache != "") {
-
ResourceCache::lock->write_lock();
ResourceCache::resources.erase(path_cache);
ResourceCache::lock->write_unlock();
@@ -66,7 +65,6 @@ void Resource::set_path(const String &p_path, bool p_take_over) {
if (has_path) {
if (p_take_over) {
-
ResourceCache::lock->write_lock();
Resource **res = ResourceCache::resources.getptr(p_path);
if (res) {
@@ -84,7 +82,6 @@ void Resource::set_path(const String &p_path, bool p_take_over) {
path_cache = p_path;
if (path_cache != "") {
-
ResourceCache::lock->write_lock();
ResourceCache::resources[path_cache] = this;
ResourceCache::lock->write_unlock();
@@ -95,62 +92,58 @@ void Resource::set_path(const String &p_path, bool p_take_over) {
}
String Resource::get_path() const {
-
return path_cache;
}
void Resource::set_subindex(int p_sub_index) {
-
subindex = p_sub_index;
}
int Resource::get_subindex() const {
-
return subindex;
}
void Resource::set_name(const String &p_name) {
-
name = p_name;
_change_notify("resource_name");
}
-String Resource::get_name() const {
+String Resource::get_name() const {
return name;
}
bool Resource::editor_can_reload_from_file() {
-
return true; //by default yes
}
void Resource::reload_from_file() {
-
String path = get_path();
- if (!path.is_resource_file())
+ if (!path.is_resource_file()) {
return;
+ }
Ref<Resource> s = ResourceLoader::load(ResourceLoader::path_remap(path), get_class(), true);
- if (!s.is_valid())
+ if (!s.is_valid()) {
return;
+ }
List<PropertyInfo> pi;
s->get_property_list(&pi);
for (List<PropertyInfo>::Element *E = pi.front(); E; E = E->next()) {
-
- if (!(E->get().usage & PROPERTY_USAGE_STORAGE))
+ if (!(E->get().usage & PROPERTY_USAGE_STORAGE)) {
continue;
- if (E->get().name == "resource_path")
+ }
+ if (E->get().name == "resource_path") {
continue; //do not change path
+ }
set(E->get().name, s->get(E->get().name));
}
}
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);
@@ -160,20 +153,17 @@ Ref<Resource> Resource::duplicate_for_local_scene(Node *p_for_scene, Map<Ref<Res
r->local_scene = p_for_scene;
for (List<PropertyInfo>::Element *E = plist.front(); E; E = E->next()) {
-
- if (!(E->get().usage & PROPERTY_USAGE_STORAGE))
+ 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)) {
p = remap_cache[sr];
} else {
-
RES dupe = sr->duplicate_for_local_scene(p_for_scene, remap_cache);
p = dupe;
remap_cache[sr] = dupe;
@@ -191,22 +181,19 @@ Ref<Resource> Resource::duplicate_for_local_scene(Node *p_for_scene, Map<Ref<Res
}
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))
+ 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);
@@ -219,7 +206,6 @@ void Resource::configure_for_local_scene(Node *p_for_scene, Map<Ref<Resource>, R
}
Ref<Resource> Resource::duplicate(bool p_subresources) const {
-
List<PropertyInfo> plist;
get_property_list(&plist);
@@ -227,21 +213,19 @@ Ref<Resource> Resource::duplicate(bool p_subresources) const {
ERR_FAIL_COND_V(!r, Ref<Resource>());
for (List<PropertyInfo>::Element *E = plist.front(); E; E = E->next()) {
-
- if (!(E->get().usage & PROPERTY_USAGE_STORAGE))
+ if (!(E->get().usage & PROPERTY_USAGE_STORAGE)) {
continue;
+ }
Variant p = get(E->get().name);
if ((p.get_type() == Variant::DICTIONARY || p.get_type() == Variant::ARRAY)) {
r->set(E->get().name, p.duplicate(p_subresources));
} else if (p.get_type() == Variant::OBJECT && (p_subresources || (E->get().usage & PROPERTY_USAGE_DO_NOT_SHARE_ON_DUPLICATE))) {
-
RES sr = p;
if (sr.is_valid()) {
r->set(E->get().name, sr->duplicate(p_subresources));
}
} else {
-
r->set(E->get().name, p);
}
}
@@ -250,34 +234,27 @@ Ref<Resource> Resource::duplicate(bool p_subresources) const {
}
void Resource::_set_path(const String &p_path) {
-
set_path(p_path, false);
}
void Resource::_take_over_path(const String &p_path) {
-
set_path(p_path, true);
}
RID Resource::get_rid() const {
-
return RID();
}
void Resource::register_owner(Object *p_owner) {
-
owners.insert(p_owner->get_instance_id());
}
void Resource::unregister_owner(Object *p_owner) {
-
owners.erase(p_owner->get_instance_id());
}
void Resource::notify_change_to_owners() {
-
for (Set<ObjectID>::Element *E = owners.front(); E; E = E->next()) {
-
Object *obj = ObjectDB::get_instance(E->get());
ERR_CONTINUE_MSG(!obj, "Object was deleted, while still owning a resource."); //wtf
//TODO store string
@@ -288,14 +265,12 @@ void Resource::notify_change_to_owners() {
#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);
for (List<PropertyInfo>::Element *E = plist.front(); E; E = E->next()) {
-
if (E->get().usage & PROPERTY_USAGE_STORAGE && E->get().type == Variant::OBJECT && E->get().hint == PROPERTY_HINT_RESOURCE_TYPE) {
RES res = get(E->get().name);
if (res.is_valid()) {
@@ -310,19 +285,17 @@ uint32_t Resource::hash_edited_version() const {
#endif
void Resource::set_local_to_scene(bool p_enable) {
-
local_to_scene = p_enable;
}
bool Resource::is_local_to_scene() const {
-
return local_to_scene;
}
Node *Resource::get_local_scene() const {
-
- if (local_scene)
+ if (local_scene) {
return local_scene;
+ }
if (_get_local_scene_func) {
return _get_local_scene_func();
@@ -332,17 +305,17 @@ Node *Resource::get_local_scene() const {
}
void Resource::setup_local_to_scene() {
-
- if (get_script_instance())
+ if (get_script_instance()) {
get_script_instance()->call("_setup_local_to_scene");
+ }
}
Node *(*Resource::_get_local_scene_func)() = nullptr;
void Resource::set_as_translation_remapped(bool p_remapped) {
-
- if (remapped_list.in_list() == p_remapped)
+ if (remapped_list.in_list() == p_remapped) {
return;
+ }
if (ResourceCache::lock) {
ResourceCache::lock->write_lock();
@@ -360,7 +333,6 @@ void Resource::set_as_translation_remapped(bool p_remapped) {
}
bool Resource::is_translation_remapped() const {
-
return remapped_list.in_list();
}
@@ -406,7 +378,6 @@ int Resource::get_id_for_path(const String &p_path) const {
#endif
void Resource::_bind_methods() {
-
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);
@@ -429,20 +400,9 @@ void Resource::_bind_methods() {
}
Resource::Resource() :
- remapped_list(this) {
-
-#ifdef TOOLS_ENABLED
- last_modified_time = 0;
- import_last_modified_time = 0;
-#endif
-
- subindex = 0;
- local_to_scene = false;
- local_scene = nullptr;
-}
+ remapped_list(this) {}
Resource::~Resource() {
-
if (path_cache != "") {
ResourceCache::lock->write_lock();
ResourceCache::resources.erase(path_cache);
@@ -464,7 +424,6 @@ RWLock *ResourceCache::path_cache_lock = nullptr;
#endif
void ResourceCache::setup() {
-
lock = RWLock::create();
#ifdef TOOLS_ENABLED
path_cache_lock = RWLock::create();
@@ -472,8 +431,16 @@ void ResourceCache::setup() {
}
void ResourceCache::clear() {
- if (resources.size())
- ERR_PRINT("Resources Still in use at Exit!");
+ if (resources.size()) {
+ 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()));
+ }
+ }
+ }
resources.clear();
memdelete(lock);
@@ -483,25 +450,17 @@ void ResourceCache::clear() {
}
void ResourceCache::reload_externals() {
-
- /*
- const String *K=nullptr;
- while ((K=resources.next(K))) {
- resources[*K]->reload_external_data();
- }
- */
}
bool ResourceCache::has(const String &p_path) {
-
lock->read_lock();
bool b = resources.has(p_path);
lock->read_unlock();
return b;
}
-Resource *ResourceCache::get(const String &p_path) {
+Resource *ResourceCache::get(const String &p_path) {
lock->read_lock();
Resource **res = resources.getptr(p_path);
@@ -516,11 +475,9 @@ Resource *ResourceCache::get(const String &p_path) {
}
void ResourceCache::get_cached_resources(List<Ref<Resource>> *p_resources) {
-
lock->read_lock();
const String *K = nullptr;
while ((K = resources.next(K))) {
-
Resource *r = resources[*K];
p_resources->push_back(Ref<Resource>(r));
}
@@ -528,7 +485,6 @@ void ResourceCache::get_cached_resources(List<Ref<Resource>> *p_resources) {
}
int ResourceCache::get_cached_resource_count() {
-
lock->read_lock();
int rc = resources.size();
lock->read_unlock();
@@ -550,7 +506,6 @@ void ResourceCache::dump(const char *p_file, bool p_short) {
const String *K = nullptr;
while ((K = resources.next(K))) {
-
Resource *r = resources[*K];
if (!type_count.has(r->get_class())) {
@@ -560,15 +515,16 @@ void ResourceCache::dump(const char *p_file, bool p_short) {
type_count[r->get_class()]++;
if (!p_short) {
- if (f)
+ if (f) {
f->store_line(r->get_class() + ": " + r->get_path());
+ }
}
}
for (Map<String, int>::Element *E = type_count.front(); E; E = E->next()) {
-
- if (f)
+ if (f) {
f->store_line(E->key() + " count: " + itos(E->get()));
+ }
}
if (f) {
f->close();
@@ -576,6 +532,5 @@ void ResourceCache::dump(const char *p_file, bool p_short) {
}
lock->read_unlock();
-
#endif
}