2014-02-10 01:10:30 +00:00
|
|
|
/*************************************************************************/
|
|
|
|
/* resource_preloader.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
|
|
|
/*************************************************************************/
|
2021-01-01 19:13:46 +00:00
|
|
|
/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2021 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_preloader.h"
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void ResourcePreloader::_set_resources(const Array &p_data) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
resources.clear();
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ERR_FAIL_COND(p_data.size() != 2);
|
|
|
|
PoolVector<String> names = p_data[0];
|
|
|
|
Array resdata = p_data[1];
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ERR_FAIL_COND(names.size() != resdata.size());
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
for (int i = 0; i < resdata.size(); i++) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
String name = names[i];
|
2014-02-10 01:10:30 +00:00
|
|
|
RES resource = resdata[i];
|
2017-03-05 15:44:50 +00:00
|
|
|
ERR_CONTINUE(!resource.is_valid());
|
|
|
|
resources[name] = resource;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
//add_resource(name,resource);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Array ResourcePreloader::_get_resources() const {
|
|
|
|
|
2017-01-07 21:25:37 +00:00
|
|
|
PoolVector<String> names;
|
2014-02-10 01:10:30 +00:00
|
|
|
Array arr;
|
|
|
|
arr.resize(resources.size());
|
|
|
|
names.resize(resources.size());
|
|
|
|
|
|
|
|
Set<String> sorted_names;
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
for (Map<StringName, RES>::Element *E = resources.front(); E; E = E->next()) {
|
2014-02-10 01:10:30 +00:00
|
|
|
sorted_names.insert(E->key());
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
int i = 0;
|
|
|
|
for (Set<String>::Element *E = sorted_names.front(); E; E = E->next()) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
names.set(i, E->get());
|
|
|
|
arr[i] = resources[E->get()];
|
2014-02-10 01:10:30 +00:00
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
Array res;
|
|
|
|
res.push_back(names);
|
|
|
|
res.push_back(arr);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void ResourcePreloader::add_resource(const StringName &p_name, const RES &p_resource) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
ERR_FAIL_COND(p_resource.is_null());
|
|
|
|
if (resources.has(p_name)) {
|
|
|
|
|
|
|
|
StringName new_name;
|
2017-03-05 15:44:50 +00:00
|
|
|
int idx = 2;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
while (true) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
new_name = p_name.operator String() + " " + itos(idx);
|
2014-02-10 01:10:30 +00:00
|
|
|
if (resources.has(new_name)) {
|
|
|
|
idx++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
add_resource(new_name, p_resource);
|
2014-02-10 01:10:30 +00:00
|
|
|
} else {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
resources[p_name] = p_resource;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void ResourcePreloader::remove_resource(const StringName &p_name) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ERR_FAIL_COND(!resources.has(p_name));
|
2014-02-10 01:10:30 +00:00
|
|
|
resources.erase(p_name);
|
|
|
|
}
|
2017-03-05 15:44:50 +00:00
|
|
|
void ResourcePreloader::rename_resource(const StringName &p_from_name, const StringName &p_to_name) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ERR_FAIL_COND(!resources.has(p_from_name));
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
RES res = resources[p_from_name];
|
|
|
|
|
|
|
|
resources.erase(p_from_name);
|
2017-03-05 15:44:50 +00:00
|
|
|
add_resource(p_to_name, res);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
bool ResourcePreloader::has_resource(const StringName &p_name) const {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
return resources.has(p_name);
|
|
|
|
}
|
2017-03-05 15:44:50 +00:00
|
|
|
RES ResourcePreloader::get_resource(const StringName &p_name) const {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ERR_FAIL_COND_V(!resources.has(p_name), RES());
|
2014-02-10 01:10:30 +00:00
|
|
|
return resources[p_name];
|
|
|
|
}
|
|
|
|
|
2017-01-07 21:25:37 +00:00
|
|
|
PoolVector<String> ResourcePreloader::_get_resource_list() const {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-01-07 21:25:37 +00:00
|
|
|
PoolVector<String> res;
|
2014-02-10 01:10:30 +00:00
|
|
|
res.resize(resources.size());
|
2017-03-05 15:44:50 +00:00
|
|
|
int i = 0;
|
|
|
|
for (Map<StringName, RES>::Element *E = resources.front(); E; E = E->next(), i++) {
|
|
|
|
res.set(i, E->key());
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ResourcePreloader::get_resource_list(List<StringName> *p_list) {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
for (Map<StringName, RES>::Element *E = resources.front(); E; E = E->next()) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
p_list->push_back(E->key());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ResourcePreloader::_bind_methods() {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("_set_resources"), &ResourcePreloader::_set_resources);
|
|
|
|
ClassDB::bind_method(D_METHOD("_get_resources"), &ResourcePreloader::_get_resources);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-08-09 11:19:41 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("add_resource", "name", "resource"), &ResourcePreloader::add_resource);
|
2017-03-05 15:44:50 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("remove_resource", "name"), &ResourcePreloader::remove_resource);
|
|
|
|
ClassDB::bind_method(D_METHOD("rename_resource", "name", "newname"), &ResourcePreloader::rename_resource);
|
|
|
|
ClassDB::bind_method(D_METHOD("has_resource", "name"), &ResourcePreloader::has_resource);
|
2017-08-09 11:19:41 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("get_resource", "name"), &ResourcePreloader::get_resource);
|
2017-03-05 15:44:50 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("get_resource_list"), &ResourcePreloader::_get_resource_list);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2018-01-11 22:35:12 +00:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "resources", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL), "_set_resources", "_get_resources");
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ResourcePreloader::ResourcePreloader() {
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|