2014-02-10 01:10:30 +00:00
|
|
|
/*************************************************************************/
|
|
|
|
/* resource.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2017-08-27 12:11:45 +00:00
|
|
|
/* https://godotengine.org */
|
2014-02-10 01:10:30 +00:00
|
|
|
/*************************************************************************/
|
2017-01-01 21:01:57 +00:00
|
|
|
/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
|
2017-04-07 22:45:00 +00:00
|
|
|
/* Copyright (c) 2014-2017 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. */
|
|
|
|
/*************************************************************************/
|
|
|
|
#include "resource.h"
|
|
|
|
#include "core_string_names.h"
|
2016-06-27 16:17:20 +00:00
|
|
|
#include "io/resource_loader.h"
|
2017-03-18 23:36:26 +00:00
|
|
|
#include "os/file_access.h"
|
|
|
|
#include <stdio.h>
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
void ResourceImportMetadata::set_editor(const String &p_editor) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
editor = p_editor;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
String ResourceImportMetadata::get_editor() const {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
return editor;
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
void ResourceImportMetadata::add_source(const String &p_path, const String &p_md5) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
Source s;
|
2017-03-18 23:36:26 +00:00
|
|
|
s.md5 = p_md5;
|
|
|
|
s.path = p_path;
|
2014-02-10 01:10:30 +00:00
|
|
|
sources.push_back(s);
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
String ResourceImportMetadata::get_source_path(int p_idx) const {
|
|
|
|
ERR_FAIL_INDEX_V(p_idx, sources.size(), String());
|
2014-02-10 01:10:30 +00:00
|
|
|
return sources[p_idx].path;
|
|
|
|
}
|
2017-03-18 23:36:26 +00:00
|
|
|
String ResourceImportMetadata::get_source_md5(int p_idx) const {
|
|
|
|
ERR_FAIL_INDEX_V(p_idx, sources.size(), String());
|
2014-02-10 01:10:30 +00:00
|
|
|
return sources[p_idx].md5;
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
void ResourceImportMetadata::set_source_md5(int p_idx, const String &p_md5) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
ERR_FAIL_INDEX(p_idx, sources.size());
|
|
|
|
sources[p_idx].md5 = p_md5;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
void ResourceImportMetadata::remove_source(int p_idx) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
ERR_FAIL_INDEX(p_idx, sources.size());
|
2014-02-10 01:10:30 +00:00
|
|
|
sources.remove(p_idx);
|
|
|
|
}
|
|
|
|
|
|
|
|
int ResourceImportMetadata::get_source_count() const {
|
|
|
|
|
|
|
|
return sources.size();
|
|
|
|
}
|
2017-03-18 23:36:26 +00:00
|
|
|
void ResourceImportMetadata::set_option(const String &p_key, const Variant &p_value) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
if (p_value.get_type() == Variant::NIL) {
|
2014-02-10 01:10:30 +00:00
|
|
|
options.erase(p_key);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ERR_FAIL_COND(p_value.get_type() == Variant::OBJECT);
|
|
|
|
ERR_FAIL_COND(p_value.get_type() == Variant::_RID);
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
options[p_key] = p_value;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
bool ResourceImportMetadata::has_option(const String &p_key) const {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
return options.has(p_key);
|
|
|
|
}
|
2016-07-17 16:25:21 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
Variant ResourceImportMetadata::get_option(const String &p_key) const {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
ERR_FAIL_COND_V(!options.has(p_key), Variant());
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
return options[p_key];
|
|
|
|
}
|
|
|
|
|
|
|
|
void ResourceImportMetadata::get_options(List<String> *r_options) const {
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
for (Map<String, Variant>::Element *E = options.front(); E; E = E->next()) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
r_options->push_back(E->key());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
StringArray ResourceImportMetadata::_get_options() const {
|
|
|
|
|
|
|
|
StringArray option_names;
|
|
|
|
option_names.resize(options.size());
|
2017-03-18 23:36:26 +00:00
|
|
|
int i = 0;
|
|
|
|
for (Map<String, Variant>::Element *E = options.front(); E; E = E->next()) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
option_names.set(i++, E->key());
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return option_names;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ResourceImportMetadata::_bind_methods() {
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
ObjectTypeDB::bind_method(_MD("set_editor", "name"), &ResourceImportMetadata::set_editor);
|
|
|
|
ObjectTypeDB::bind_method(_MD("get_editor"), &ResourceImportMetadata::get_editor);
|
|
|
|
ObjectTypeDB::bind_method(_MD("add_source", "path", "md5"), &ResourceImportMetadata::add_source, "");
|
|
|
|
ObjectTypeDB::bind_method(_MD("get_source_path", "idx"), &ResourceImportMetadata::get_source_path);
|
|
|
|
ObjectTypeDB::bind_method(_MD("get_source_md5", "idx"), &ResourceImportMetadata::get_source_md5);
|
|
|
|
ObjectTypeDB::bind_method(_MD("set_source_md5", "idx", "md5"), &ResourceImportMetadata::set_source_md5);
|
|
|
|
ObjectTypeDB::bind_method(_MD("remove_source", "idx"), &ResourceImportMetadata::remove_source);
|
|
|
|
ObjectTypeDB::bind_method(_MD("get_source_count"), &ResourceImportMetadata::get_source_count);
|
|
|
|
ObjectTypeDB::bind_method(_MD("set_option", "key", "value"), &ResourceImportMetadata::set_option);
|
|
|
|
ObjectTypeDB::bind_method(_MD("get_option", "key"), &ResourceImportMetadata::get_option);
|
|
|
|
ObjectTypeDB::bind_method(_MD("get_options"), &ResourceImportMetadata::_get_options);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ResourceImportMetadata::ResourceImportMetadata() {
|
|
|
|
}
|
|
|
|
|
|
|
|
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-18 23:36:26 +00:00
|
|
|
void Resource::set_path(const String &p_path, bool p_take_over) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
if (path_cache == p_path)
|
2014-02-10 01:10:30 +00:00
|
|
|
return;
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
if (path_cache != "") {
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
ResourceCache::resources.erase(path_cache);
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
path_cache = "";
|
|
|
|
if (ResourceCache::resources.has(p_path)) {
|
2014-06-28 02:21:45 +00:00
|
|
|
if (p_take_over) {
|
|
|
|
|
|
|
|
ResourceCache::resources.get(p_path)->set_name("");
|
|
|
|
} else {
|
2017-03-18 23:36:26 +00:00
|
|
|
ERR_EXPLAIN("Another resource is loaded from path: " + p_path);
|
|
|
|
ERR_FAIL_COND(ResourceCache::resources.has(p_path));
|
2014-06-28 02:21:45 +00:00
|
|
|
}
|
|
|
|
}
|
2017-03-18 23:36:26 +00:00
|
|
|
path_cache = p_path;
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
if (path_cache != "") {
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
ResourceCache::resources[path_cache] = this;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_change_notify("resource/path");
|
|
|
|
_resource_path_changed();
|
|
|
|
}
|
|
|
|
|
|
|
|
String Resource::get_path() const {
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
return path_cache;
|
|
|
|
}
|
|
|
|
|
2015-06-22 03:03:19 +00:00
|
|
|
void Resource::set_subindex(int p_sub_index) {
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
subindex = p_sub_index;
|
2015-06-22 03:03:19 +00:00
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
int Resource::get_subindex() const {
|
2015-06-22 03:03:19 +00:00
|
|
|
|
|
|
|
return subindex;
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
void Resource::set_name(const String &p_name) {
|
2015-06-22 03:03:19 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
name = p_name;
|
2014-02-10 01:10:30 +00:00
|
|
|
_change_notify("resource/name");
|
|
|
|
}
|
|
|
|
String Resource::get_name() const {
|
|
|
|
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
2016-06-27 16:17:20 +00:00
|
|
|
bool Resource::editor_can_reload_from_file() {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2016-06-27 16:17:20 +00:00
|
|
|
return true; //by default yes
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Resource::reload_from_file() {
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
String path = get_path();
|
2016-06-27 16:17:20 +00:00
|
|
|
if (!path.is_resource_file())
|
|
|
|
return;
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
Ref<Resource> s = ResourceLoader::load(path, get_type(), true);
|
2016-06-27 16:17:20 +00:00
|
|
|
|
|
|
|
if (!s.is_valid())
|
|
|
|
return;
|
|
|
|
|
|
|
|
List<PropertyInfo> pi;
|
|
|
|
s->get_property_list(&pi);
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
for (List<PropertyInfo>::Element *E = pi.front(); E; E = E->next()) {
|
2016-06-27 16:17:20 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
if (!(E->get().usage & PROPERTY_USAGE_STORAGE))
|
2016-06-27 16:17:20 +00:00
|
|
|
continue;
|
2017-03-18 23:36:26 +00:00
|
|
|
if (E->get().name == "resource/path")
|
2016-06-27 16:17:20 +00:00
|
|
|
continue; //do not change path
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
set(E->get().name, s->get(E->get().name));
|
2016-06-27 16:17:20 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Ref<Resource> Resource::duplicate(bool p_subresources) {
|
|
|
|
|
|
|
|
List<PropertyInfo> plist;
|
|
|
|
get_property_list(&plist);
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
Resource *r = (Resource *)ObjectTypeDB::instance(get_type());
|
|
|
|
ERR_FAIL_COND_V(!r, Ref<Resource>());
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
for (List<PropertyInfo>::Element *E = plist.front(); E; E = E->next()) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
if (!(E->get().usage & PROPERTY_USAGE_STORAGE))
|
2014-02-10 01:10:30 +00:00
|
|
|
continue;
|
|
|
|
Variant p = get(E->get().name);
|
2017-03-18 23:36:26 +00:00
|
|
|
if (p.get_type() == Variant::OBJECT && p_subresources) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
RES sr = p;
|
|
|
|
if (sr.is_valid())
|
2017-03-18 23:36:26 +00:00
|
|
|
p = sr->duplicate(true);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
r->set(E->get().name, p);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return Ref<Resource>(r);
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
void Resource::_set_path(const String &p_path) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
set_path(p_path, false);
|
2014-06-30 04:28:05 +00:00
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
void Resource::_take_over_path(const String &p_path) {
|
2014-06-30 04:28:05 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
set_path(p_path, true);
|
2014-06-30 04:28:05 +00:00
|
|
|
}
|
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
void Resource::_bind_methods() {
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
ObjectTypeDB::bind_method(_MD("set_path", "path"), &Resource::_set_path);
|
|
|
|
ObjectTypeDB::bind_method(_MD("take_over_path", "path"), &Resource::_take_over_path);
|
|
|
|
ObjectTypeDB::bind_method(_MD("get_path"), &Resource::get_path);
|
|
|
|
ObjectTypeDB::bind_method(_MD("set_name", "name"), &Resource::set_name);
|
|
|
|
ObjectTypeDB::bind_method(_MD("get_name"), &Resource::get_name);
|
|
|
|
ObjectTypeDB::bind_method(_MD("get_rid"), &Resource::get_rid);
|
|
|
|
ObjectTypeDB::bind_method(_MD("set_import_metadata", "metadata"), &Resource::set_import_metadata);
|
|
|
|
ObjectTypeDB::bind_method(_MD("get_import_metadata"), &Resource::get_import_metadata);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
ObjectTypeDB::bind_method(_MD("duplicate", "subresources"), &Resource::duplicate, DEFVAL(false));
|
|
|
|
ADD_SIGNAL(MethodInfo("changed"));
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::STRING, "resource/path", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_EDITOR), _SCS("set_path"), _SCS("get_path"));
|
|
|
|
ADD_PROPERTYNZ(PropertyInfo(Variant::STRING, "resource/name"), _SCS("set_name"), _SCS("get_name"));
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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() {
|
|
|
|
|
2017-03-18 23:36:26 +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());
|
|
|
|
ERR_EXPLAIN("Object was deleted, while still owning a resource");
|
|
|
|
ERR_CONTINUE(!obj); //wtf
|
|
|
|
//TODO store string
|
2017-03-18 23:36:26 +00:00
|
|
|
obj->call("resource_changed", RES(this));
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
void Resource::set_import_metadata(const Ref<ResourceImportMetadata> &p_metadata) {
|
2014-02-10 01:10:30 +00:00
|
|
|
#ifdef TOOLS_ENABLED
|
2017-03-18 23:36:26 +00:00
|
|
|
import_metadata = p_metadata;
|
2014-02-10 01:10:30 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
Ref<ResourceImportMetadata> Resource::get_import_metadata() const {
|
|
|
|
|
|
|
|
#ifdef TOOLS_ENABLED
|
|
|
|
return import_metadata;
|
|
|
|
#else
|
|
|
|
return Ref<ResourceImportMetadata>();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
for (List<PropertyInfo>::Element *E = plist.front(); E; E = E->next()) {
|
2016-05-27 17:18:40 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
if (E->get().type == Variant::OBJECT && E->get().hint == PROPERTY_HINT_RESOURCE_TYPE) {
|
2016-05-27 17:18:40 +00:00
|
|
|
RES res = get(E->get().name);
|
|
|
|
if (res.is_valid()) {
|
2017-03-18 23:36:26 +00:00
|
|
|
hash = hash_djb2_one_32(res->hash_edited_version(), hash);
|
2016-05-27 17:18:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return hash;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
Resource::Resource() {
|
|
|
|
|
|
|
|
#ifdef TOOLS_ENABLED
|
2017-03-18 23:36:26 +00:00
|
|
|
last_modified_time = 0;
|
2014-02-10 01:10:30 +00:00
|
|
|
#endif
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
subindex = 0;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Resource::~Resource() {
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
if (path_cache != "")
|
2014-02-10 01:10:30 +00:00
|
|
|
ResourceCache::resources.erase(path_cache);
|
|
|
|
if (owners.size()) {
|
|
|
|
WARN_PRINT("Resource is still owned");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
HashMap<String, Resource *> ResourceCache::resources;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
void ResourceCache::clear() {
|
|
|
|
if (resources.size())
|
|
|
|
ERR_PRINT("Resources Still in use at Exit!");
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
resources.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ResourceCache::reload_externals() {
|
|
|
|
|
|
|
|
GLOBAL_LOCK_FUNCTION
|
|
|
|
|
|
|
|
//const String *K=NULL;
|
|
|
|
//while ((K=resources.next(K))) {
|
2017-03-18 23:36:26 +00:00
|
|
|
// resources[*K]->reload_external_data();
|
|
|
|
// }
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
bool ResourceCache::has(const String &p_path) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
GLOBAL_LOCK_FUNCTION
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
return resources.has(p_path);
|
|
|
|
}
|
2017-03-18 23:36:26 +00:00
|
|
|
Resource *ResourceCache::get(const String &p_path) {
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
GLOBAL_LOCK_FUNCTION
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
Resource **res = resources.getptr(p_path);
|
|
|
|
if (!res) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
return *res;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ResourceCache::get_cached_resources(List<Ref<Resource> > *p_resources) {
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
const String *K = NULL;
|
|
|
|
while ((K = resources.next(K))) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
Resource *r = resources[*K];
|
2017-03-18 23:36:26 +00:00
|
|
|
p_resources->push_back(Ref<Resource>(r));
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int ResourceCache::get_cached_resource_count() {
|
|
|
|
|
|
|
|
return resources.size();
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
void ResourceCache::dump(const char *p_file, bool p_short) {
|
2014-02-10 01:10:30 +00:00
|
|
|
#ifdef DEBUG_ENABLED
|
|
|
|
GLOBAL_LOCK_FUNCTION
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
Map<String, int> type_count;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
FileAccess *f = NULL;
|
2014-02-10 01:10:30 +00:00
|
|
|
if (p_file) {
|
2017-03-18 23:36:26 +00:00
|
|
|
f = FileAccess::open(p_file, FileAccess::WRITE);
|
2014-02-10 01:10:30 +00:00
|
|
|
ERR_FAIL_COND(!f);
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
const String *K = NULL;
|
|
|
|
while ((K = resources.next(K))) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
Resource *r = resources[*K];
|
|
|
|
|
|
|
|
if (!type_count.has(r->get_type())) {
|
2017-03-18 23:36:26 +00:00
|
|
|
type_count[r->get_type()] = 0;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type_count[r->get_type()]++;
|
|
|
|
|
|
|
|
if (!p_short) {
|
|
|
|
if (f)
|
2017-03-18 23:36:26 +00:00
|
|
|
f->store_line(r->get_type() + ": " + r->get_path());
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
for (Map<String, int>::Element *E = type_count.front(); E; E = E->next()) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
if (f)
|
2017-03-18 23:36:26 +00:00
|
|
|
f->store_line(E->key() + " count: " + itos(E->get()));
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
if (f) {
|
|
|
|
f->close();
|
|
|
|
memdelete(f);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
}
|