2023-01-10 14:26:54 +00:00
|
|
|
/**************************************************************************/
|
|
|
|
/* resource_loader.cpp */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* https://godotengine.org */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
|
|
|
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* */
|
|
|
|
/* 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_loader.h"
|
2018-09-11 16:13:45 +00:00
|
|
|
|
2019-02-12 12:30:56 +00:00
|
|
|
#include "core/io/resource_importer.h"
|
2018-09-11 16:13:45 +00:00
|
|
|
#include "core/os/file_access.h"
|
|
|
|
#include "core/os/os.h"
|
|
|
|
#include "core/path_remap.h"
|
|
|
|
#include "core/print_string.h"
|
|
|
|
#include "core/project_settings.h"
|
|
|
|
#include "core/translation.h"
|
|
|
|
#include "core/variant_parser.h"
|
|
|
|
|
2018-06-11 00:59:53 +00:00
|
|
|
Ref<ResourceFormatLoader> ResourceLoader::loader[ResourceLoader::MAX_LOADERS];
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
int ResourceLoader::loader_count = 0;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
Error ResourceInteractiveLoader::wait() {
|
|
|
|
Error err = poll();
|
2017-03-05 15:44:50 +00:00
|
|
|
while (err == OK) {
|
|
|
|
err = poll();
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2019-01-27 22:24:55 +00:00
|
|
|
ResourceInteractiveLoader::~ResourceInteractiveLoader() {
|
|
|
|
if (path_loading != String()) {
|
2019-02-16 20:38:09 +00:00
|
|
|
ResourceLoader::_remove_from_loading_map_and_thread(path_loading, path_loading_thread);
|
2019-01-27 22:24:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
bool ResourceFormatLoader::recognize_path(const String &p_path, const String &p_for_type) const {
|
2017-01-26 00:55:59 +00:00
|
|
|
String extension = p_path.get_extension();
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
List<String> extensions;
|
2017-03-05 15:44:50 +00:00
|
|
|
if (p_for_type == String()) {
|
2017-01-26 00:55:59 +00:00
|
|
|
get_recognized_extensions(&extensions);
|
|
|
|
} else {
|
2017-03-05 15:44:50 +00:00
|
|
|
get_recognized_extensions_for_type(p_for_type, &extensions);
|
2017-01-26 00:55:59 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
for (List<String>::Element *E = extensions.front(); E; E = E->next()) {
|
2021-05-05 10:44:11 +00:00
|
|
|
if (E->get().nocasecmp_to(extension) == 0) {
|
2014-02-10 01:10:30 +00:00
|
|
|
return true;
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
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 false;
|
|
|
|
}
|
|
|
|
|
2018-06-11 00:59:53 +00:00
|
|
|
bool ResourceFormatLoader::handles_type(const String &p_type) const {
|
|
|
|
if (get_script_instance() && get_script_instance()->has_method("handles_type")) {
|
|
|
|
// I guess custom loaders for custom resources should use "Resource"
|
|
|
|
return get_script_instance()->call("handles_type", p_type);
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
String ResourceFormatLoader::get_resource_type(const String &p_path) const {
|
|
|
|
if (get_script_instance() && get_script_instance()->has_method("get_resource_type")) {
|
|
|
|
return get_script_instance()->call("get_resource_type", p_path);
|
|
|
|
}
|
|
|
|
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void ResourceFormatLoader::get_recognized_extensions_for_type(const String &p_type, List<String> *p_extensions) const {
|
2021-05-05 10:44:11 +00:00
|
|
|
if (p_type == "" || handles_type(p_type)) {
|
2014-02-10 01:10:30 +00:00
|
|
|
get_recognized_extensions(p_extensions);
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void ResourceLoader::get_recognized_extensions_for_type(const String &p_type, List<String> *p_extensions) {
|
|
|
|
for (int i = 0; i < loader_count; i++) {
|
|
|
|
loader[i]->get_recognized_extensions_for_type(p_type, p_extensions);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ResourceInteractiveLoader::_bind_methods() {
|
2017-08-09 11:19:41 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("get_resource"), &ResourceInteractiveLoader::get_resource);
|
2017-03-05 15:44:50 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("poll"), &ResourceInteractiveLoader::poll);
|
|
|
|
ClassDB::bind_method(D_METHOD("wait"), &ResourceInteractiveLoader::wait);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_stage"), &ResourceInteractiveLoader::get_stage);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_stage_count"), &ResourceInteractiveLoader::get_stage_count);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
class ResourceInteractiveLoaderDefault : public ResourceInteractiveLoader {
|
2017-03-05 15:44:50 +00:00
|
|
|
GDCLASS(ResourceInteractiveLoaderDefault, ResourceInteractiveLoader);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
public:
|
2014-02-10 01:10:30 +00:00
|
|
|
Ref<Resource> resource;
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
virtual void set_local_path(const String &p_local_path) { /*scene->set_filename(p_local_path);*/
|
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
virtual Ref<Resource> get_resource() { return resource; }
|
|
|
|
virtual Error poll() { return ERR_FILE_EOF; }
|
|
|
|
virtual int get_stage() const { return 1; }
|
|
|
|
virtual int get_stage_count() const { return 1; }
|
2017-06-28 20:00:18 +00:00
|
|
|
virtual void set_translation_remapped(bool p_remapped) { resource->set_as_translation_remapped(p_remapped); }
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
ResourceInteractiveLoaderDefault() {}
|
|
|
|
};
|
|
|
|
|
2018-08-10 18:57:43 +00:00
|
|
|
bool ResourceFormatLoader::exists(const String &p_path) const {
|
|
|
|
return FileAccess::exists(p_path); //by default just check file
|
|
|
|
}
|
2018-08-12 10:44:38 +00:00
|
|
|
|
2018-06-11 00:59:53 +00:00
|
|
|
void ResourceFormatLoader::get_recognized_extensions(List<String> *p_extensions) const {
|
|
|
|
if (get_script_instance() && get_script_instance()->has_method("get_recognized_extensions")) {
|
|
|
|
PoolStringArray exts = get_script_instance()->call("get_recognized_extensions");
|
|
|
|
|
|
|
|
{
|
|
|
|
PoolStringArray::Read r = exts.read();
|
|
|
|
for (int i = 0; i < exts.size(); ++i) {
|
|
|
|
p_extensions->push_back(r[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-05 08:26:34 +00:00
|
|
|
// Warning: Derived classes must override either `load` or `load_interactive`. The base code
|
|
|
|
// here can trigger an infinite recursion otherwise, since `load` calls `load_interactive`
|
|
|
|
// vice versa.
|
|
|
|
|
|
|
|
Ref<ResourceInteractiveLoader> ResourceFormatLoader::load_interactive(const String &p_path, const String &p_original_path, Error *r_error) {
|
|
|
|
// Warning: See previous note about the risk of infinite recursion.
|
|
|
|
Ref<Resource> res = load(p_path, p_original_path, r_error);
|
|
|
|
if (res.is_null()) {
|
|
|
|
return Ref<ResourceInteractiveLoader>();
|
|
|
|
}
|
|
|
|
|
|
|
|
Ref<ResourceInteractiveLoaderDefault> ril = Ref<ResourceInteractiveLoaderDefault>(memnew(ResourceInteractiveLoaderDefault));
|
|
|
|
ril->resource = res;
|
|
|
|
return ril;
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
RES ResourceFormatLoader::load(const String &p_path, const String &p_original_path, Error *r_error) {
|
2021-05-05 08:26:34 +00:00
|
|
|
// Check user-defined loader if there's any. Hard fail if it returns an error.
|
2018-06-11 00:59:53 +00:00
|
|
|
if (get_script_instance() && get_script_instance()->has_method("load")) {
|
|
|
|
Variant res = get_script_instance()->call("load", p_path, p_original_path);
|
|
|
|
|
2021-05-05 08:26:34 +00:00
|
|
|
if (res.get_type() == Variant::INT) { // Error code, abort.
|
2021-05-05 10:44:11 +00:00
|
|
|
if (r_error) {
|
2018-06-11 00:59:53 +00:00
|
|
|
*r_error = (Error)res.operator int64_t();
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2021-05-05 08:26:34 +00:00
|
|
|
return RES();
|
|
|
|
} else { // Success, pass on result.
|
2021-05-05 10:44:11 +00:00
|
|
|
if (r_error) {
|
2018-06-11 00:59:53 +00:00
|
|
|
*r_error = OK;
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2018-06-11 00:59:53 +00:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2021-05-05 08:26:34 +00:00
|
|
|
// Warning: See previous note about the risk of infinite recursion.
|
2017-08-31 21:57:03 +00:00
|
|
|
Ref<ResourceInteractiveLoader> ril = load_interactive(p_path, p_original_path, r_error);
|
2021-05-05 10:44:11 +00:00
|
|
|
if (!ril.is_valid()) {
|
2014-02-10 01:10:30 +00:00
|
|
|
return RES();
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
ril->set_local_path(p_original_path);
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
while (true) {
|
2014-02-10 01:10:30 +00:00
|
|
|
Error err = ril->poll();
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (err == ERR_FILE_EOF) {
|
2021-05-05 10:44:11 +00:00
|
|
|
if (r_error) {
|
2017-03-05 15:44:50 +00:00
|
|
|
*r_error = OK;
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
return ril->get_resource();
|
|
|
|
}
|
|
|
|
|
2021-05-05 10:44:11 +00:00
|
|
|
if (r_error) {
|
2017-03-05 15:44:50 +00:00
|
|
|
*r_error = err;
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2015-08-23 23:15:56 +00:00
|
|
|
|
2019-09-25 08:28:50 +00:00
|
|
|
ERR_FAIL_COND_V_MSG(err != OK, RES(), "Failed to load resource '" + p_path + "'.");
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void ResourceFormatLoader::get_dependencies(const String &p_path, List<String> *p_dependencies, bool p_add_types) {
|
2018-06-11 00:59:53 +00:00
|
|
|
if (get_script_instance() && get_script_instance()->has_method("get_dependencies")) {
|
|
|
|
PoolStringArray deps = get_script_instance()->call("get_dependencies", p_path, p_add_types);
|
|
|
|
|
|
|
|
{
|
|
|
|
PoolStringArray::Read r = deps.read();
|
|
|
|
for (int i = 0; i < deps.size(); ++i) {
|
|
|
|
p_dependencies->push_back(r[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Error ResourceFormatLoader::rename_dependencies(const String &p_path, const Map<String, String> &p_map) {
|
|
|
|
if (get_script_instance() && get_script_instance()->has_method("rename_dependencies")) {
|
|
|
|
Dictionary deps_dict;
|
|
|
|
for (Map<String, String>::Element *E = p_map.front(); E; E = E->next()) {
|
|
|
|
deps_dict[E->key()] = E->value();
|
|
|
|
}
|
|
|
|
|
|
|
|
int64_t res = get_script_instance()->call("rename_dependencies", deps_dict);
|
|
|
|
return (Error)res;
|
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ResourceFormatLoader::_bind_methods() {
|
|
|
|
{
|
|
|
|
MethodInfo info = MethodInfo(Variant::NIL, "load", PropertyInfo(Variant::STRING, "path"), PropertyInfo(Variant::STRING, "original_path"));
|
|
|
|
info.return_val.usage |= PROPERTY_USAGE_NIL_IS_VARIANT;
|
|
|
|
ClassDB::add_virtual_method(get_class_static(), info);
|
|
|
|
}
|
|
|
|
|
|
|
|
ClassDB::add_virtual_method(get_class_static(), MethodInfo(Variant::POOL_STRING_ARRAY, "get_recognized_extensions"));
|
|
|
|
ClassDB::add_virtual_method(get_class_static(), MethodInfo(Variant::BOOL, "handles_type", PropertyInfo(Variant::STRING, "typename")));
|
|
|
|
ClassDB::add_virtual_method(get_class_static(), MethodInfo(Variant::STRING, "get_resource_type", PropertyInfo(Variant::STRING, "path")));
|
|
|
|
ClassDB::add_virtual_method(get_class_static(), MethodInfo("get_dependencies", PropertyInfo(Variant::STRING, "path"), PropertyInfo(Variant::STRING, "add_types")));
|
|
|
|
ClassDB::add_virtual_method(get_class_static(), MethodInfo(Variant::INT, "rename_dependencies", PropertyInfo(Variant::STRING, "path"), PropertyInfo(Variant::STRING, "renames")));
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////
|
|
|
|
|
2017-08-31 21:57:03 +00:00
|
|
|
RES ResourceLoader::_load(const String &p_path, const String &p_original_path, const String &p_type_hint, bool p_no_cache, Error *r_error) {
|
|
|
|
bool found = false;
|
|
|
|
|
|
|
|
// Try all loaders and pick the first match for the type hint
|
|
|
|
for (int i = 0; i < loader_count; i++) {
|
|
|
|
if (!loader[i]->recognize_path(p_path, p_type_hint)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
found = true;
|
|
|
|
RES res = loader[i]->load(p_path, p_original_path != String() ? p_original_path : p_path, r_error);
|
|
|
|
if (res.is_null()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2020-08-22 20:19:08 +00:00
|
|
|
ERR_FAIL_COND_V_MSG(found, RES(),
|
|
|
|
vformat("Failed loading resource: %s. Make sure resources have been imported by opening the project in the editor at least once.", p_path));
|
2019-08-15 02:57:49 +00:00
|
|
|
|
2020-01-08 14:11:16 +00:00
|
|
|
#ifdef TOOLS_ENABLED
|
|
|
|
FileAccessRef file_check = FileAccess::create(FileAccess::ACCESS_RESOURCES);
|
|
|
|
ERR_FAIL_COND_V_MSG(!file_check->file_exists(p_path), RES(), "Resource file not found: " + p_path + ".");
|
|
|
|
#endif
|
|
|
|
|
2019-08-15 02:57:49 +00:00
|
|
|
ERR_FAIL_V_MSG(RES(), "No loader found for resource: " + p_path + ".");
|
2017-08-31 21:57:03 +00:00
|
|
|
}
|
|
|
|
|
2019-01-27 22:24:55 +00:00
|
|
|
bool ResourceLoader::_add_to_loading_map(const String &p_path) {
|
|
|
|
bool success;
|
2021-01-27 09:43:02 +00:00
|
|
|
loading_map_mutex.lock();
|
2019-01-27 22:24:55 +00:00
|
|
|
|
2019-02-16 20:38:09 +00:00
|
|
|
LoadingMapKey key;
|
|
|
|
key.path = p_path;
|
|
|
|
key.thread = Thread::get_caller_id();
|
|
|
|
|
|
|
|
if (loading_map.has(key)) {
|
2019-01-27 22:24:55 +00:00
|
|
|
success = false;
|
|
|
|
} else {
|
2019-02-16 20:38:09 +00:00
|
|
|
loading_map[key] = true;
|
2019-01-27 22:24:55 +00:00
|
|
|
success = true;
|
|
|
|
}
|
|
|
|
|
2021-01-27 09:43:02 +00:00
|
|
|
loading_map_mutex.unlock();
|
2019-01-27 22:24:55 +00:00
|
|
|
|
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ResourceLoader::_remove_from_loading_map(const String &p_path) {
|
2021-01-27 09:43:02 +00:00
|
|
|
loading_map_mutex.lock();
|
2019-01-27 22:24:55 +00:00
|
|
|
|
2019-02-16 20:38:09 +00:00
|
|
|
LoadingMapKey key;
|
|
|
|
key.path = p_path;
|
|
|
|
key.thread = Thread::get_caller_id();
|
|
|
|
|
|
|
|
loading_map.erase(key);
|
|
|
|
|
2021-01-27 09:43:02 +00:00
|
|
|
loading_map_mutex.unlock();
|
2019-02-16 20:38:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ResourceLoader::_remove_from_loading_map_and_thread(const String &p_path, Thread::ID p_thread) {
|
2021-01-27 09:43:02 +00:00
|
|
|
loading_map_mutex.lock();
|
2019-02-16 20:38:09 +00:00
|
|
|
|
|
|
|
LoadingMapKey key;
|
|
|
|
key.path = p_path;
|
|
|
|
key.thread = p_thread;
|
|
|
|
|
|
|
|
loading_map.erase(key);
|
2019-01-27 22:24:55 +00:00
|
|
|
|
2021-01-27 09:43:02 +00:00
|
|
|
loading_map_mutex.unlock();
|
2019-01-27 22:24:55 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
RES ResourceLoader::load(const String &p_path, const String &p_type_hint, bool p_no_cache, Error *r_error) {
|
2021-05-05 10:44:11 +00:00
|
|
|
if (r_error) {
|
2017-03-05 15:44:50 +00:00
|
|
|
*r_error = ERR_CANT_OPEN;
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2015-05-04 13:17:24 +00:00
|
|
|
String local_path;
|
2021-05-05 10:44:11 +00:00
|
|
|
if (p_path.is_rel_path()) {
|
2017-03-05 15:44:50 +00:00
|
|
|
local_path = "res://" + p_path;
|
2021-05-05 10:44:11 +00:00
|
|
|
} else {
|
2017-07-19 20:00:46 +00:00
|
|
|
local_path = ProjectSettings::get_singleton()->localize_path(p_path);
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2018-09-02 16:59:33 +00:00
|
|
|
if (!p_no_cache) {
|
2019-01-27 22:24:55 +00:00
|
|
|
{
|
|
|
|
bool success = _add_to_loading_map(local_path);
|
2019-08-15 02:57:49 +00:00
|
|
|
ERR_FAIL_COND_V_MSG(!success, RES(), "Resource: '" + local_path + "' is already being loaded. Cyclic reference?");
|
2019-01-27 22:24:55 +00:00
|
|
|
}
|
|
|
|
|
2018-09-02 16:59:33 +00:00
|
|
|
//lock first if possible
|
2021-01-25 18:48:38 +00:00
|
|
|
ResourceCache::lock.read_lock();
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2018-09-02 16:59:33 +00:00
|
|
|
//get ptr
|
|
|
|
Resource **rptr = ResourceCache::resources.getptr(local_path);
|
|
|
|
|
|
|
|
if (rptr) {
|
|
|
|
RES res(*rptr);
|
|
|
|
//it is possible this resource was just freed in a thread. If so, this referencing will not work and resource is considered not cached
|
|
|
|
if (res.is_valid()) {
|
|
|
|
//referencing is fine
|
2021-05-05 10:44:11 +00:00
|
|
|
if (r_error) {
|
2018-09-02 16:59:33 +00:00
|
|
|
*r_error = OK;
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2021-01-25 18:48:38 +00:00
|
|
|
ResourceCache::lock.read_unlock();
|
2019-01-27 22:24:55 +00:00
|
|
|
_remove_from_loading_map(local_path);
|
2018-09-02 16:59:33 +00:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
}
|
2021-01-25 18:48:38 +00:00
|
|
|
ResourceCache::lock.read_unlock();
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-12-14 18:33:54 +00:00
|
|
|
bool xl_remapped = false;
|
|
|
|
String path = _path_remap(local_path, &xl_remapped);
|
|
|
|
|
2019-01-27 22:24:55 +00:00
|
|
|
if (path == "") {
|
|
|
|
if (!p_no_cache) {
|
|
|
|
_remove_from_loading_map(local_path);
|
|
|
|
}
|
2019-08-15 02:57:49 +00:00
|
|
|
ERR_FAIL_V_MSG(RES(), "Remapping '" + local_path + "' failed.");
|
2019-01-27 22:24:55 +00:00
|
|
|
}
|
2017-12-14 18:33:54 +00:00
|
|
|
|
2018-08-24 06:47:34 +00:00
|
|
|
print_verbose("Loading resource: " + path);
|
2017-08-31 21:57:03 +00:00
|
|
|
RES res = _load(path, local_path, p_type_hint, p_no_cache, r_error);
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-08-31 21:57:03 +00:00
|
|
|
if (res.is_null()) {
|
2019-01-27 22:24:55 +00:00
|
|
|
if (!p_no_cache) {
|
|
|
|
_remove_from_loading_map(local_path);
|
|
|
|
}
|
2017-08-31 21:57:03 +00:00
|
|
|
return RES();
|
|
|
|
}
|
2021-05-05 10:44:11 +00:00
|
|
|
if (!p_no_cache) {
|
2017-08-31 21:57:03 +00:00
|
|
|
res->set_path(local_path);
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2017-06-28 20:00:18 +00:00
|
|
|
|
2021-05-05 10:44:11 +00:00
|
|
|
if (xl_remapped) {
|
2017-08-31 21:57:03 +00:00
|
|
|
res->set_as_translation_remapped(true);
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2017-06-28 20:00:18 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
#ifdef TOOLS_ENABLED
|
|
|
|
|
2017-08-31 21:57:03 +00:00
|
|
|
res->set_edited(false);
|
|
|
|
if (timestamp_on_load) {
|
|
|
|
uint64_t mt = FileAccess::get_modified_time(path);
|
|
|
|
//printf("mt %s: %lli\n",remapped_path.utf8().get_data(),mt);
|
|
|
|
res->set_last_modified_time(mt);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
2017-08-31 21:57:03 +00:00
|
|
|
#endif
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2019-01-27 22:24:55 +00:00
|
|
|
if (!p_no_cache) {
|
|
|
|
_remove_from_loading_map(local_path);
|
|
|
|
}
|
|
|
|
|
2018-10-29 19:36:31 +00:00
|
|
|
if (_loaded_callback) {
|
|
|
|
_loaded_callback(res, p_path);
|
|
|
|
}
|
|
|
|
|
2017-08-31 21:57:03 +00:00
|
|
|
return res;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2018-08-10 18:57:43 +00:00
|
|
|
bool ResourceLoader::exists(const String &p_path, const String &p_type_hint) {
|
|
|
|
String local_path;
|
2021-05-05 10:44:11 +00:00
|
|
|
if (p_path.is_rel_path()) {
|
2018-08-10 18:57:43 +00:00
|
|
|
local_path = "res://" + p_path;
|
2021-05-05 10:44:11 +00:00
|
|
|
} else {
|
2018-08-10 18:57:43 +00:00
|
|
|
local_path = ProjectSettings::get_singleton()->localize_path(p_path);
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2018-08-10 18:57:43 +00:00
|
|
|
|
|
|
|
if (ResourceCache::has(local_path)) {
|
2018-08-12 10:44:38 +00:00
|
|
|
return true; // If cached, it probably exists
|
2018-08-10 18:57:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool xl_remapped = false;
|
|
|
|
String path = _path_remap(local_path, &xl_remapped);
|
|
|
|
|
|
|
|
// Try all loaders and pick the first match for the type hint
|
|
|
|
for (int i = 0; i < loader_count; i++) {
|
|
|
|
if (!loader[i]->recognize_path(path, p_type_hint)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-05-05 10:44:11 +00:00
|
|
|
if (loader[i]->exists(path)) {
|
2018-08-10 18:57:43 +00:00
|
|
|
return true;
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2018-08-10 18:57:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
Ref<ResourceInteractiveLoader> ResourceLoader::load_interactive(const String &p_path, const String &p_type_hint, bool p_no_cache, Error *r_error) {
|
2021-05-05 10:44:11 +00:00
|
|
|
if (r_error) {
|
2017-03-05 15:44:50 +00:00
|
|
|
*r_error = ERR_CANT_OPEN;
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2015-05-04 13:17:24 +00:00
|
|
|
String local_path;
|
2021-05-05 10:44:11 +00:00
|
|
|
if (p_path.is_rel_path()) {
|
2017-03-05 15:44:50 +00:00
|
|
|
local_path = "res://" + p_path;
|
2021-05-05 10:44:11 +00:00
|
|
|
} else {
|
2017-07-19 20:00:46 +00:00
|
|
|
local_path = ProjectSettings::get_singleton()->localize_path(p_path);
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2019-01-27 22:24:55 +00:00
|
|
|
if (!p_no_cache) {
|
|
|
|
bool success = _add_to_loading_map(local_path);
|
2019-08-15 02:57:49 +00:00
|
|
|
ERR_FAIL_COND_V_MSG(!success, RES(), "Resource: '" + local_path + "' is already being loaded. Cyclic reference?");
|
2016-05-29 23:22:00 +00:00
|
|
|
|
2019-01-27 22:24:55 +00:00
|
|
|
if (ResourceCache::has(local_path)) {
|
|
|
|
print_verbose("Loading resource: " + local_path + " (cached)");
|
|
|
|
Ref<Resource> res_cached = ResourceCache::get(local_path);
|
|
|
|
Ref<ResourceInteractiveLoaderDefault> ril = Ref<ResourceInteractiveLoaderDefault>(memnew(ResourceInteractiveLoaderDefault));
|
|
|
|
|
|
|
|
ril->resource = res_cached;
|
|
|
|
ril->path_loading = local_path;
|
2019-02-16 20:38:09 +00:00
|
|
|
ril->path_loading_thread = Thread::get_caller_id();
|
2019-01-27 22:24:55 +00:00
|
|
|
return ril;
|
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-12-14 18:33:54 +00:00
|
|
|
bool xl_remapped = false;
|
|
|
|
String path = _path_remap(local_path, &xl_remapped);
|
2019-01-27 22:24:55 +00:00
|
|
|
if (path == "") {
|
|
|
|
if (!p_no_cache) {
|
|
|
|
_remove_from_loading_map(local_path);
|
|
|
|
}
|
2019-08-15 02:57:49 +00:00
|
|
|
ERR_FAIL_V_MSG(RES(), "Remapping '" + local_path + "' failed.");
|
2019-01-27 22:24:55 +00:00
|
|
|
}
|
|
|
|
|
2018-08-24 06:47:34 +00:00
|
|
|
print_verbose("Loading resource: " + path);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
bool found = false;
|
|
|
|
for (int i = 0; i < loader_count; i++) {
|
2021-05-05 10:44:11 +00:00
|
|
|
if (!loader[i]->recognize_path(path, p_type_hint)) {
|
2014-02-10 01:10:30 +00:00
|
|
|
continue;
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2017-03-05 15:44:50 +00:00
|
|
|
found = true;
|
2017-08-31 21:57:03 +00:00
|
|
|
Ref<ResourceInteractiveLoader> ril = loader[i]->load_interactive(path, local_path, r_error);
|
2021-05-05 10:44:11 +00:00
|
|
|
if (ril.is_null()) {
|
2014-02-10 01:10:30 +00:00
|
|
|
continue;
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2019-01-27 22:24:55 +00:00
|
|
|
if (!p_no_cache) {
|
2014-02-10 01:10:30 +00:00
|
|
|
ril->set_local_path(local_path);
|
2019-01-27 22:24:55 +00:00
|
|
|
ril->path_loading = local_path;
|
2019-02-16 20:38:09 +00:00
|
|
|
ril->path_loading_thread = Thread::get_caller_id();
|
2019-01-27 22:24:55 +00:00
|
|
|
}
|
|
|
|
|
2021-05-05 10:44:11 +00:00
|
|
|
if (xl_remapped) {
|
2017-06-28 20:00:18 +00:00
|
|
|
ril->set_translation_remapped(true);
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
return ril;
|
|
|
|
}
|
|
|
|
|
2019-01-27 22:24:55 +00:00
|
|
|
if (!p_no_cache) {
|
|
|
|
_remove_from_loading_map(local_path);
|
|
|
|
}
|
|
|
|
|
2019-08-15 02:57:49 +00:00
|
|
|
ERR_FAIL_COND_V_MSG(found, Ref<ResourceInteractiveLoader>(), "Failed loading resource: " + path + ".");
|
|
|
|
|
|
|
|
ERR_FAIL_V_MSG(Ref<ResourceInteractiveLoader>(), "No loader found for resource: " + path + ".");
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2018-06-11 00:59:53 +00:00
|
|
|
void ResourceLoader::add_resource_format_loader(Ref<ResourceFormatLoader> p_format_loader, bool p_at_front) {
|
|
|
|
ERR_FAIL_COND(p_format_loader.is_null());
|
2017-03-05 15:44:50 +00:00
|
|
|
ERR_FAIL_COND(loader_count >= MAX_LOADERS);
|
2018-06-11 00:59:53 +00:00
|
|
|
|
2016-07-20 00:40:05 +00:00
|
|
|
if (p_at_front) {
|
2017-03-05 15:44:50 +00:00
|
|
|
for (int i = loader_count; i > 0; i--) {
|
|
|
|
loader[i] = loader[i - 1];
|
2016-07-20 00:40:05 +00:00
|
|
|
}
|
2017-03-05 15:44:50 +00:00
|
|
|
loader[0] = p_format_loader;
|
2016-07-20 00:40:05 +00:00
|
|
|
loader_count++;
|
|
|
|
} else {
|
2017-03-05 15:44:50 +00:00
|
|
|
loader[loader_count++] = p_format_loader;
|
2016-07-20 00:40:05 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2018-06-11 00:59:53 +00:00
|
|
|
void ResourceLoader::remove_resource_format_loader(Ref<ResourceFormatLoader> p_format_loader) {
|
|
|
|
ERR_FAIL_COND(p_format_loader.is_null());
|
|
|
|
|
|
|
|
// Find loader
|
|
|
|
int i = 0;
|
|
|
|
for (; i < loader_count; ++i) {
|
2021-05-05 10:44:11 +00:00
|
|
|
if (loader[i] == p_format_loader) {
|
2018-06-11 00:59:53 +00:00
|
|
|
break;
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2018-06-11 00:59:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ERR_FAIL_COND(i >= loader_count); // Not found
|
|
|
|
|
|
|
|
// Shift next loaders up
|
|
|
|
for (; i < loader_count - 1; ++i) {
|
|
|
|
loader[i] = loader[i + 1];
|
|
|
|
}
|
|
|
|
loader[loader_count - 1].unref();
|
|
|
|
--loader_count;
|
|
|
|
}
|
|
|
|
|
2017-09-20 23:59:19 +00:00
|
|
|
int ResourceLoader::get_import_order(const String &p_path) {
|
|
|
|
String path = _path_remap(p_path);
|
|
|
|
|
|
|
|
String local_path;
|
2021-05-05 10:44:11 +00:00
|
|
|
if (path.is_rel_path()) {
|
2017-09-20 23:59:19 +00:00
|
|
|
local_path = "res://" + path;
|
2021-05-05 10:44:11 +00:00
|
|
|
} else {
|
2017-09-20 23:59:19 +00:00
|
|
|
local_path = ProjectSettings::get_singleton()->localize_path(path);
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2017-09-20 23:59:19 +00:00
|
|
|
|
|
|
|
for (int i = 0; i < loader_count; i++) {
|
2021-05-05 10:44:11 +00:00
|
|
|
if (!loader[i]->recognize_path(local_path)) {
|
2017-09-20 23:59:19 +00:00
|
|
|
continue;
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2017-09-20 23:59:19 +00:00
|
|
|
/*
|
|
|
|
if (p_type_hint!="" && !loader[i]->handles_type(p_type_hint))
|
|
|
|
continue;
|
|
|
|
*/
|
|
|
|
|
|
|
|
return loader[i]->get_import_order(p_path);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-04-19 18:54:33 +00:00
|
|
|
String ResourceLoader::get_import_group_file(const String &p_path) {
|
|
|
|
String path = _path_remap(p_path);
|
|
|
|
|
|
|
|
String local_path;
|
2021-05-05 10:44:11 +00:00
|
|
|
if (path.is_rel_path()) {
|
2019-04-19 18:54:33 +00:00
|
|
|
local_path = "res://" + path;
|
2021-05-05 10:44:11 +00:00
|
|
|
} else {
|
2019-04-19 18:54:33 +00:00
|
|
|
local_path = ProjectSettings::get_singleton()->localize_path(path);
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2019-04-19 18:54:33 +00:00
|
|
|
|
|
|
|
for (int i = 0; i < loader_count; i++) {
|
2021-05-05 10:44:11 +00:00
|
|
|
if (!loader[i]->recognize_path(local_path)) {
|
2019-04-19 18:54:33 +00:00
|
|
|
continue;
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2019-04-19 18:54:33 +00:00
|
|
|
/*
|
|
|
|
if (p_type_hint!="" && !loader[i]->handles_type(p_type_hint))
|
|
|
|
continue;
|
|
|
|
*/
|
|
|
|
|
|
|
|
return loader[i]->get_import_group_file(p_path);
|
|
|
|
}
|
|
|
|
|
|
|
|
return String(); //not found
|
|
|
|
}
|
|
|
|
|
2017-08-29 22:50:58 +00:00
|
|
|
bool ResourceLoader::is_import_valid(const String &p_path) {
|
|
|
|
String path = _path_remap(p_path);
|
|
|
|
|
|
|
|
String local_path;
|
2021-05-05 10:44:11 +00:00
|
|
|
if (path.is_rel_path()) {
|
2017-08-29 22:50:58 +00:00
|
|
|
local_path = "res://" + path;
|
2021-05-05 10:44:11 +00:00
|
|
|
} else {
|
2017-08-29 22:50:58 +00:00
|
|
|
local_path = ProjectSettings::get_singleton()->localize_path(path);
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2017-08-29 22:50:58 +00:00
|
|
|
|
|
|
|
for (int i = 0; i < loader_count; i++) {
|
2021-05-05 10:44:11 +00:00
|
|
|
if (!loader[i]->recognize_path(local_path)) {
|
2017-08-29 22:50:58 +00:00
|
|
|
continue;
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2017-08-29 22:50:58 +00:00
|
|
|
/*
|
|
|
|
if (p_type_hint!="" && !loader[i]->handles_type(p_type_hint))
|
|
|
|
continue;
|
|
|
|
*/
|
|
|
|
|
|
|
|
return loader[i]->is_import_valid(p_path);
|
|
|
|
}
|
|
|
|
|
|
|
|
return false; //not found
|
|
|
|
}
|
|
|
|
|
2019-03-04 14:06:15 +00:00
|
|
|
bool ResourceLoader::is_imported(const String &p_path) {
|
|
|
|
String path = _path_remap(p_path);
|
|
|
|
|
|
|
|
String local_path;
|
2021-05-05 10:44:11 +00:00
|
|
|
if (path.is_rel_path()) {
|
2019-03-04 14:06:15 +00:00
|
|
|
local_path = "res://" + path;
|
2021-05-05 10:44:11 +00:00
|
|
|
} else {
|
2019-03-04 14:06:15 +00:00
|
|
|
local_path = ProjectSettings::get_singleton()->localize_path(path);
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2019-03-04 14:06:15 +00:00
|
|
|
|
|
|
|
for (int i = 0; i < loader_count; i++) {
|
2021-05-05 10:44:11 +00:00
|
|
|
if (!loader[i]->recognize_path(local_path)) {
|
2019-03-04 14:06:15 +00:00
|
|
|
continue;
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2019-03-04 14:06:15 +00:00
|
|
|
/*
|
|
|
|
if (p_type_hint!="" && !loader[i]->handles_type(p_type_hint))
|
|
|
|
continue;
|
|
|
|
*/
|
|
|
|
|
|
|
|
return loader[i]->is_imported(p_path);
|
|
|
|
}
|
|
|
|
|
|
|
|
return false; //not found
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void ResourceLoader::get_dependencies(const String &p_path, List<String> *p_dependencies, bool p_add_types) {
|
2017-06-28 20:00:18 +00:00
|
|
|
String path = _path_remap(p_path);
|
|
|
|
|
2015-08-23 23:15:56 +00:00
|
|
|
String local_path;
|
2021-05-05 10:44:11 +00:00
|
|
|
if (path.is_rel_path()) {
|
2017-06-28 20:00:18 +00:00
|
|
|
local_path = "res://" + path;
|
2021-05-05 10:44:11 +00:00
|
|
|
} else {
|
2017-07-19 20:00:46 +00:00
|
|
|
local_path = ProjectSettings::get_singleton()->localize_path(path);
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2015-08-23 23:15:56 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
for (int i = 0; i < loader_count; i++) {
|
2021-05-05 10:44:11 +00:00
|
|
|
if (!loader[i]->recognize_path(local_path)) {
|
2015-08-23 23:15:56 +00:00
|
|
|
continue;
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2017-01-14 11:26:56 +00:00
|
|
|
/*
|
|
|
|
if (p_type_hint!="" && !loader[i]->handles_type(p_type_hint))
|
|
|
|
continue;
|
|
|
|
*/
|
2015-08-23 23:15:56 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
loader[i]->get_dependencies(local_path, p_dependencies, p_add_types);
|
2015-08-23 23:15:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
Error ResourceLoader::rename_dependencies(const String &p_path, const Map<String, String> &p_map) {
|
2017-06-28 20:00:18 +00:00
|
|
|
String path = _path_remap(p_path);
|
|
|
|
|
2015-05-04 13:17:24 +00:00
|
|
|
String local_path;
|
2021-05-05 10:44:11 +00:00
|
|
|
if (path.is_rel_path()) {
|
2017-06-28 20:00:18 +00:00
|
|
|
local_path = "res://" + path;
|
2021-05-05 10:44:11 +00:00
|
|
|
} else {
|
2017-07-19 20:00:46 +00:00
|
|
|
local_path = ProjectSettings::get_singleton()->localize_path(path);
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2015-05-04 13:17:24 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
for (int i = 0; i < loader_count; i++) {
|
2021-05-05 10:44:11 +00:00
|
|
|
if (!loader[i]->recognize_path(local_path)) {
|
2014-02-10 01:10:30 +00:00
|
|
|
continue;
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2017-01-14 11:26:56 +00:00
|
|
|
/*
|
|
|
|
if (p_type_hint!="" && !loader[i]->handles_type(p_type_hint))
|
|
|
|
continue;
|
|
|
|
*/
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
return loader[i]->rename_dependencies(local_path, p_map);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
2015-08-23 23:15:56 +00:00
|
|
|
|
|
|
|
return OK; // ??
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
String ResourceLoader::get_resource_type(const String &p_path) {
|
2015-05-04 13:17:24 +00:00
|
|
|
String local_path;
|
2021-05-05 10:44:11 +00:00
|
|
|
if (p_path.is_rel_path()) {
|
2017-03-05 15:44:50 +00:00
|
|
|
local_path = "res://" + p_path;
|
2021-05-05 10:44:11 +00:00
|
|
|
} else {
|
2017-07-19 20:00:46 +00:00
|
|
|
local_path = ProjectSettings::get_singleton()->localize_path(p_path);
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2015-05-04 13:17:24 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
for (int i = 0; i < loader_count; i++) {
|
2014-02-10 01:10:30 +00:00
|
|
|
String result = loader[i]->get_resource_type(local_path);
|
2021-05-05 10:44:11 +00:00
|
|
|
if (result != "") {
|
2014-02-10 01:10:30 +00:00
|
|
|
return result;
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return "";
|
|
|
|
}
|
2017-06-28 20:00:18 +00:00
|
|
|
|
|
|
|
String ResourceLoader::_path_remap(const String &p_path, bool *r_translation_remapped) {
|
2017-12-14 18:33:54 +00:00
|
|
|
String new_path = p_path;
|
2017-06-28 20:00:18 +00:00
|
|
|
|
2019-12-04 15:50:43 +00:00
|
|
|
if (translation_remaps.has(p_path)) {
|
|
|
|
// translation_remaps has the following format:
|
|
|
|
// { "res://path.png": PoolStringArray( "res://path-ru.png:ru", "res://path-de.png:de" ) }
|
|
|
|
|
|
|
|
// To find the path of the remapped resource, we extract the locale name after
|
|
|
|
// the last ':' to match the project locale.
|
|
|
|
// We also fall back in case of regional locales as done in TranslationServer::translate
|
|
|
|
// (e.g. 'ru_RU' -> 'ru' if the former has no specific mapping).
|
2017-12-14 18:33:54 +00:00
|
|
|
|
2022-07-29 14:30:29 +00:00
|
|
|
// An extra remap may still be necessary afterwards due to the text -> binary converter on export.
|
|
|
|
|
2017-06-28 20:00:18 +00:00
|
|
|
String locale = TranslationServer::get_singleton()->get_locale();
|
2019-12-04 15:50:43 +00:00
|
|
|
ERR_FAIL_COND_V_MSG(locale.length() < 2, p_path, "Could not remap path '" + p_path + "' for translation as configured locale '" + locale + "' is invalid.");
|
|
|
|
String lang = TranslationServer::get_language_code(locale);
|
2017-06-28 20:00:18 +00:00
|
|
|
|
2019-12-04 15:50:43 +00:00
|
|
|
Vector<String> &res_remaps = *translation_remaps.getptr(new_path);
|
|
|
|
bool near_match = false;
|
|
|
|
|
|
|
|
for (int i = 0; i < res_remaps.size(); i++) {
|
2022-01-08 21:40:44 +00:00
|
|
|
int split = res_remaps[i].rfind(":");
|
2019-12-04 15:50:43 +00:00
|
|
|
if (split == -1) {
|
2017-06-28 20:00:18 +00:00
|
|
|
continue;
|
2019-12-04 15:50:43 +00:00
|
|
|
}
|
2017-06-28 20:00:18 +00:00
|
|
|
|
2019-12-04 15:50:43 +00:00
|
|
|
String l = res_remaps[i].right(split + 1).strip_edges();
|
|
|
|
if (l == locale) { // Exact match.
|
|
|
|
new_path = res_remaps[i].left(split);
|
2017-12-14 18:33:54 +00:00
|
|
|
break;
|
2019-12-04 15:50:43 +00:00
|
|
|
} else if (near_match) {
|
|
|
|
continue; // Already found near match, keep going for potential exact match.
|
2017-06-28 20:00:18 +00:00
|
|
|
}
|
2019-12-04 15:50:43 +00:00
|
|
|
|
|
|
|
// No exact match (e.g. locale 'ru_RU' but remap is 'ru'), let's look further
|
|
|
|
// for a near match (same language code, i.e. first 2 or 3 letters before
|
|
|
|
// regional code, if included).
|
|
|
|
if (TranslationServer::get_language_code(l) == lang) {
|
|
|
|
// Language code matches, that's a near match. Keep looking for exact match.
|
|
|
|
near_match = true;
|
|
|
|
new_path = res_remaps[i].left(split);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (r_translation_remapped) {
|
|
|
|
*r_translation_remapped = true;
|
2017-06-28 20:00:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-14 18:33:54 +00:00
|
|
|
if (path_remaps.has(new_path)) {
|
|
|
|
new_path = path_remaps[new_path];
|
2022-07-29 14:30:29 +00:00
|
|
|
} else {
|
2019-12-04 15:50:43 +00:00
|
|
|
// Try file remap.
|
2017-12-18 14:21:13 +00:00
|
|
|
Error err;
|
2022-07-29 14:30:29 +00:00
|
|
|
FileAccess *f = FileAccess::open(new_path + ".remap", FileAccess::READ, &err);
|
2017-12-18 14:21:13 +00:00
|
|
|
|
|
|
|
if (f) {
|
|
|
|
VariantParser::StreamFile stream;
|
|
|
|
stream.f = f;
|
|
|
|
|
|
|
|
String assign;
|
|
|
|
Variant value;
|
|
|
|
VariantParser::Tag next_tag;
|
|
|
|
|
|
|
|
int lines = 0;
|
|
|
|
String error_text;
|
|
|
|
while (true) {
|
|
|
|
assign = Variant();
|
|
|
|
next_tag.fields.clear();
|
|
|
|
next_tag.name = String();
|
|
|
|
|
2021-05-04 14:00:45 +00:00
|
|
|
err = VariantParser::parse_tag_assign_eof(&stream, lines, error_text, next_tag, assign, value, nullptr, true);
|
2017-12-18 14:21:13 +00:00
|
|
|
if (err == ERR_FILE_EOF) {
|
|
|
|
break;
|
|
|
|
} else if (err != OK) {
|
2021-06-16 10:56:25 +00:00
|
|
|
ERR_PRINT("Parse error: " + p_path + ".remap:" + itos(lines) + " error: " + error_text + ".");
|
2017-12-18 14:21:13 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (assign == "path") {
|
|
|
|
new_path = value;
|
|
|
|
break;
|
|
|
|
} else if (next_tag.name != "remap") {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
memdelete(f);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-14 18:33:54 +00:00
|
|
|
return new_path;
|
2017-06-28 20:00:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
String ResourceLoader::import_remap(const String &p_path) {
|
|
|
|
if (ResourceFormatImporter::get_singleton()->recognize_path(p_path)) {
|
|
|
|
return ResourceFormatImporter::get_singleton()->get_internal_resource_path(p_path);
|
|
|
|
}
|
|
|
|
|
|
|
|
return p_path;
|
|
|
|
}
|
|
|
|
|
|
|
|
String ResourceLoader::path_remap(const String &p_path) {
|
|
|
|
return _path_remap(p_path);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ResourceLoader::reload_translation_remaps() {
|
2021-01-25 18:48:38 +00:00
|
|
|
ResourceCache::lock.read_lock();
|
2017-06-28 20:00:18 +00:00
|
|
|
|
|
|
|
List<Resource *> to_reload;
|
|
|
|
SelfList<Resource> *E = remapped_list.first();
|
|
|
|
|
|
|
|
while (E) {
|
|
|
|
to_reload.push_back(E->self());
|
|
|
|
E = E->next();
|
|
|
|
}
|
|
|
|
|
2021-01-25 18:48:38 +00:00
|
|
|
ResourceCache::lock.read_unlock();
|
2017-06-28 20:00:18 +00:00
|
|
|
|
|
|
|
//now just make sure to not delete any of these resources while changing locale..
|
|
|
|
while (to_reload.front()) {
|
|
|
|
to_reload.front()->get()->reload_from_file();
|
|
|
|
to_reload.pop_front();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ResourceLoader::load_translation_remaps() {
|
2021-05-05 10:44:11 +00:00
|
|
|
if (!ProjectSettings::get_singleton()->has_setting("locale/translation_remaps")) {
|
2017-07-26 13:03:13 +00:00
|
|
|
return;
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2017-07-26 13:03:13 +00:00
|
|
|
|
2017-07-19 20:00:46 +00:00
|
|
|
Dictionary remaps = ProjectSettings::get_singleton()->get("locale/translation_remaps");
|
2017-06-28 20:00:18 +00:00
|
|
|
List<Variant> keys;
|
|
|
|
remaps.get_key_list(&keys);
|
|
|
|
for (List<Variant>::Element *E = keys.front(); E; E = E->next()) {
|
|
|
|
Array langs = remaps[E->get()];
|
|
|
|
Vector<String> lang_remaps;
|
|
|
|
lang_remaps.resize(langs.size());
|
|
|
|
for (int i = 0; i < langs.size(); i++) {
|
2018-07-25 01:11:03 +00:00
|
|
|
lang_remaps.write[i] = langs[i];
|
2017-06-28 20:00:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
translation_remaps[String(E->get())] = lang_remaps;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ResourceLoader::clear_translation_remaps() {
|
|
|
|
translation_remaps.clear();
|
2020-08-30 19:44:39 +00:00
|
|
|
while (remapped_list.first() != nullptr) {
|
|
|
|
remapped_list.remove(remapped_list.first());
|
|
|
|
}
|
2017-06-28 20:00:18 +00:00
|
|
|
}
|
|
|
|
|
2017-12-14 18:33:54 +00:00
|
|
|
void ResourceLoader::load_path_remaps() {
|
2021-05-05 10:44:11 +00:00
|
|
|
if (!ProjectSettings::get_singleton()->has_setting("path_remap/remapped_paths")) {
|
2017-12-14 18:33:54 +00:00
|
|
|
return;
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2017-12-14 18:33:54 +00:00
|
|
|
|
|
|
|
PoolVector<String> remaps = ProjectSettings::get_singleton()->get("path_remap/remapped_paths");
|
|
|
|
int rc = remaps.size();
|
|
|
|
ERR_FAIL_COND(rc & 1); //must be even
|
|
|
|
PoolVector<String>::Read r = remaps.read();
|
|
|
|
|
|
|
|
for (int i = 0; i < rc; i += 2) {
|
|
|
|
path_remaps[r[i]] = r[i + 1];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ResourceLoader::clear_path_remaps() {
|
|
|
|
path_remaps.clear();
|
|
|
|
}
|
|
|
|
|
2018-10-29 19:36:31 +00:00
|
|
|
void ResourceLoader::set_load_callback(ResourceLoadedCallback p_callback) {
|
|
|
|
_loaded_callback = p_callback;
|
|
|
|
}
|
|
|
|
|
2021-05-04 14:00:45 +00:00
|
|
|
ResourceLoadedCallback ResourceLoader::_loaded_callback = nullptr;
|
2018-10-29 19:36:31 +00:00
|
|
|
|
2018-06-11 00:59:53 +00:00
|
|
|
Ref<ResourceFormatLoader> ResourceLoader::_find_custom_resource_format_loader(String path) {
|
|
|
|
for (int i = 0; i < loader_count; ++i) {
|
|
|
|
if (loader[i]->get_script_instance() && loader[i]->get_script_instance()->get_script()->get_path() == path) {
|
|
|
|
return loader[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Ref<ResourceFormatLoader>();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ResourceLoader::add_custom_resource_format_loader(String script_path) {
|
2021-05-05 10:44:11 +00:00
|
|
|
if (_find_custom_resource_format_loader(script_path).is_valid()) {
|
2018-06-11 00:59:53 +00:00
|
|
|
return false;
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2018-06-11 00:59:53 +00:00
|
|
|
|
|
|
|
Ref<Resource> res = ResourceLoader::load(script_path);
|
|
|
|
ERR_FAIL_COND_V(res.is_null(), false);
|
|
|
|
ERR_FAIL_COND_V(!res->is_class("Script"), false);
|
|
|
|
|
|
|
|
Ref<Script> s = res;
|
|
|
|
StringName ibt = s->get_instance_base_type();
|
|
|
|
bool valid_type = ClassDB::is_parent_class(ibt, "ResourceFormatLoader");
|
2019-08-15 02:57:49 +00:00
|
|
|
ERR_FAIL_COND_V_MSG(!valid_type, false, "Script does not inherit a CustomResourceLoader: " + script_path + ".");
|
2018-06-11 00:59:53 +00:00
|
|
|
|
|
|
|
Object *obj = ClassDB::instance(ibt);
|
|
|
|
|
2021-05-04 14:00:45 +00:00
|
|
|
ERR_FAIL_COND_V_MSG(obj == nullptr, false, "Cannot instance script as custom resource loader, expected 'ResourceFormatLoader' inheritance, got: " + String(ibt) + ".");
|
2018-06-11 00:59:53 +00:00
|
|
|
|
2020-11-14 15:30:35 +00:00
|
|
|
Ref<ResourceFormatLoader> crl = Object::cast_to<ResourceFormatLoader>(obj);
|
2018-06-11 00:59:53 +00:00
|
|
|
crl->set_script(s.get_ref_ptr());
|
|
|
|
ResourceLoader::add_resource_format_loader(crl);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ResourceLoader::remove_custom_resource_format_loader(String script_path) {
|
2019-02-12 20:10:08 +00:00
|
|
|
Ref<ResourceFormatLoader> custom_loader = _find_custom_resource_format_loader(script_path);
|
2021-05-05 10:44:11 +00:00
|
|
|
if (custom_loader.is_valid()) {
|
2019-02-12 20:10:08 +00:00
|
|
|
remove_resource_format_loader(custom_loader);
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2018-06-11 00:59:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ResourceLoader::add_custom_loaders() {
|
|
|
|
// Custom loaders registration exploits global class names
|
|
|
|
|
|
|
|
String custom_loader_base_class = ResourceFormatLoader::get_class_static();
|
|
|
|
|
|
|
|
List<StringName> global_classes;
|
|
|
|
ScriptServer::get_global_class_list(&global_classes);
|
|
|
|
|
|
|
|
for (List<StringName>::Element *E = global_classes.front(); E; E = E->next()) {
|
|
|
|
StringName class_name = E->get();
|
2019-03-09 03:47:27 +00:00
|
|
|
StringName base_class = ScriptServer::get_global_class_native_base(class_name);
|
2018-06-11 00:59:53 +00:00
|
|
|
|
|
|
|
if (base_class == custom_loader_base_class) {
|
|
|
|
String path = ScriptServer::get_global_class_path(class_name);
|
|
|
|
add_custom_resource_format_loader(path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ResourceLoader::remove_custom_loaders() {
|
2021-05-04 12:20:36 +00:00
|
|
|
Vector<Ref<ResourceFormatLoader>> custom_loaders;
|
2018-06-11 00:59:53 +00:00
|
|
|
for (int i = 0; i < loader_count; ++i) {
|
|
|
|
if (loader[i]->get_script_instance()) {
|
|
|
|
custom_loaders.push_back(loader[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < custom_loaders.size(); ++i) {
|
|
|
|
remove_resource_format_loader(custom_loaders[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-27 09:43:02 +00:00
|
|
|
Mutex ResourceLoader::loading_map_mutex;
|
2019-02-16 20:38:09 +00:00
|
|
|
HashMap<ResourceLoader::LoadingMapKey, int, ResourceLoader::LoadingMapKeyHasher> ResourceLoader::loading_map;
|
2019-01-27 22:24:55 +00:00
|
|
|
|
|
|
|
void ResourceLoader::finalize() {
|
|
|
|
#ifndef NO_THREADS
|
2021-05-04 14:00:45 +00:00
|
|
|
const LoadingMapKey *K = nullptr;
|
2019-01-27 22:24:55 +00:00
|
|
|
while ((K = loading_map.next(K))) {
|
2021-06-16 10:56:25 +00:00
|
|
|
ERR_PRINT("Exited while resource is being loaded: " + K->path);
|
2019-01-27 22:24:55 +00:00
|
|
|
}
|
|
|
|
loading_map.clear();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2021-05-04 14:00:45 +00:00
|
|
|
ResourceLoadErrorNotify ResourceLoader::err_notify = nullptr;
|
|
|
|
void *ResourceLoader::err_notify_ud = nullptr;
|
2015-08-23 23:15:56 +00:00
|
|
|
|
2021-05-04 14:00:45 +00:00
|
|
|
DependencyErrorNotify ResourceLoader::dep_err_notify = nullptr;
|
|
|
|
void *ResourceLoader::dep_err_notify_ud = nullptr;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
bool ResourceLoader::abort_on_missing_resource = true;
|
|
|
|
bool ResourceLoader::timestamp_on_load = false;
|
2017-06-28 20:00:18 +00:00
|
|
|
|
|
|
|
SelfList<Resource>::List ResourceLoader::remapped_list;
|
2021-05-04 12:20:36 +00:00
|
|
|
HashMap<String, Vector<String>> ResourceLoader::translation_remaps;
|
2017-12-14 18:33:54 +00:00
|
|
|
HashMap<String, String> ResourceLoader::path_remaps;
|
2018-10-05 02:00:02 +00:00
|
|
|
|
2021-05-04 14:00:45 +00:00
|
|
|
ResourceLoaderImport ResourceLoader::import = nullptr;
|