2014-02-10 09:10:30 +08:00
|
|
|
/*************************************************************************/
|
|
|
|
/* resource_saver.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
|
|
|
/*************************************************************************/
|
2022-01-04 04:27:34 +08:00
|
|
|
/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2022 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_saver.h"
|
2020-11-08 06:33:38 +08:00
|
|
|
#include "core/config/project_settings.h"
|
2021-06-11 20:51:48 +08:00
|
|
|
#include "core/io/file_access.h"
|
2018-09-12 00:13:45 +08:00
|
|
|
#include "core/io/resource_loader.h"
|
2020-11-08 06:33:38 +08:00
|
|
|
#include "core/object/script_language.h"
|
2014-02-10 09:10:30 +08:00
|
|
|
|
2018-06-11 08:59:53 +08:00
|
|
|
Ref<ResourceFormatSaver> ResourceSaver::saver[MAX_SAVERS];
|
2014-02-10 09:10:30 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
int ResourceSaver::saver_count = 0;
|
|
|
|
bool ResourceSaver::timestamp_on_save = false;
|
2020-04-02 07:20:12 +08:00
|
|
|
ResourceSavedCallback ResourceSaver::save_callback = nullptr;
|
2021-07-24 03:01:18 +08:00
|
|
|
ResourceSaverGetResourceIDForPath ResourceSaver::save_get_id_for_path = nullptr;
|
2014-02-10 09:10:30 +08:00
|
|
|
|
2018-06-11 08:59:53 +08:00
|
|
|
Error ResourceFormatSaver::save(const String &p_path, const RES &p_resource, uint32_t p_flags) {
|
2021-08-22 09:52:44 +08:00
|
|
|
int64_t res;
|
|
|
|
if (GDVIRTUAL_CALL(_save, p_path, p_resource, p_flags, res)) {
|
|
|
|
return (Error)res;
|
2018-06-11 08:59:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return ERR_METHOD_NOT_FOUND;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ResourceFormatSaver::recognize(const RES &p_resource) const {
|
2021-08-22 09:52:44 +08:00
|
|
|
bool success;
|
|
|
|
if (GDVIRTUAL_CALL(_recognize, p_resource, success)) {
|
|
|
|
return success;
|
2018-06-11 08:59:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ResourceFormatSaver::get_recognized_extensions(const RES &p_resource, List<String> *p_extensions) const {
|
2021-08-22 09:52:44 +08:00
|
|
|
PackedStringArray exts;
|
|
|
|
if (GDVIRTUAL_CALL(_get_recognized_extensions, p_resource, exts)) {
|
|
|
|
const String *r = exts.ptr();
|
|
|
|
for (int i = 0; i < exts.size(); ++i) {
|
|
|
|
p_extensions->push_back(r[i]);
|
2018-06-11 08:59:53 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ResourceFormatSaver::_bind_methods() {
|
2021-08-22 09:52:44 +08:00
|
|
|
GDVIRTUAL_BIND(_save, "path", "resource", "flags");
|
|
|
|
GDVIRTUAL_BIND(_recognize, "resource");
|
|
|
|
GDVIRTUAL_BIND(_get_recognized_extensions, "resource");
|
2018-06-11 08:59:53 +08:00
|
|
|
}
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
Error ResourceSaver::save(const String &p_path, const RES &p_resource, uint32_t p_flags) {
|
|
|
|
String extension = p_path.get_extension();
|
|
|
|
Error err = ERR_FILE_UNRECOGNIZED;
|
2014-02-10 09:10:30 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
for (int i = 0; i < saver_count; i++) {
|
2020-05-14 22:41:43 +08:00
|
|
|
if (!saver[i]->recognize(p_resource)) {
|
2014-02-10 09:10:30 +08:00
|
|
|
continue;
|
2020-05-14 22:41:43 +08:00
|
|
|
}
|
2016-03-09 07:00:52 +08:00
|
|
|
|
2014-02-10 09:10:30 +08:00
|
|
|
List<String> extensions;
|
2017-03-05 23:44:50 +08:00
|
|
|
bool recognized = false;
|
|
|
|
saver[i]->get_recognized_extensions(p_resource, &extensions);
|
2016-03-09 07:00:52 +08:00
|
|
|
|
2021-07-24 21:46:25 +08:00
|
|
|
for (const String &E : extensions) {
|
2021-07-16 11:45:57 +08:00
|
|
|
if (E.nocasecmp_to(extension) == 0) {
|
2017-03-05 23:44:50 +08:00
|
|
|
recognized = true;
|
2020-05-14 22:41:43 +08:00
|
|
|
}
|
2014-02-10 09:10:30 +08:00
|
|
|
}
|
|
|
|
|
2020-05-14 22:41:43 +08:00
|
|
|
if (!recognized) {
|
2014-02-10 09:10:30 +08:00
|
|
|
continue;
|
2020-05-14 22:41:43 +08:00
|
|
|
}
|
2016-03-09 07:00:52 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
String old_path = p_resource->get_path();
|
2016-03-09 07:00:52 +08:00
|
|
|
|
2017-07-20 04:00:46 +08:00
|
|
|
String local_path = ProjectSettings::get_singleton()->localize_path(p_path);
|
2014-02-10 09:10:30 +08:00
|
|
|
|
|
|
|
RES rwcopy = p_resource;
|
2020-05-14 22:41:43 +08:00
|
|
|
if (p_flags & FLAG_CHANGE_PATH) {
|
2014-02-10 09:10:30 +08:00
|
|
|
rwcopy->set_path(local_path);
|
2020-05-14 22:41:43 +08:00
|
|
|
}
|
2014-02-10 09:10:30 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
err = saver[i]->save(p_path, p_resource, p_flags);
|
2014-02-10 09:10:30 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
if (err == OK) {
|
2014-02-10 09:10:30 +08:00
|
|
|
#ifdef TOOLS_ENABLED
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
((Resource *)p_resource.ptr())->set_edited(false);
|
2014-02-10 09:10:30 +08:00
|
|
|
if (timestamp_on_save) {
|
|
|
|
uint64_t mt = FileAccess::get_modified_time(p_path);
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
((Resource *)p_resource.ptr())->set_last_modified_time(mt);
|
2014-02-10 09:10:30 +08:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2020-05-14 22:41:43 +08:00
|
|
|
if (p_flags & FLAG_CHANGE_PATH) {
|
2014-02-10 09:10:30 +08:00
|
|
|
rwcopy->set_path(old_path);
|
2020-05-14 22:41:43 +08:00
|
|
|
}
|
2014-02-10 09:10:30 +08:00
|
|
|
|
2020-05-14 22:41:43 +08:00
|
|
|
if (save_callback && p_path.begins_with("res://")) {
|
2018-10-30 03:36:31 +08:00
|
|
|
save_callback(p_resource, p_path);
|
2020-05-14 22:41:43 +08:00
|
|
|
}
|
2014-02-10 09:10:30 +08:00
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
}
|
2016-03-09 07:00:52 +08:00
|
|
|
|
2014-02-10 09:10:30 +08:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ResourceSaver::set_save_callback(ResourceSavedCallback p_callback) {
|
2017-03-05 23:44:50 +08:00
|
|
|
save_callback = p_callback;
|
2014-02-10 09:10:30 +08:00
|
|
|
}
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
void ResourceSaver::get_recognized_extensions(const RES &p_resource, List<String> *p_extensions) {
|
|
|
|
for (int i = 0; i < saver_count; i++) {
|
|
|
|
saver[i]->get_recognized_extensions(p_resource, p_extensions);
|
2014-02-10 09:10:30 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-11 08:59:53 +08:00
|
|
|
void ResourceSaver::add_resource_format_saver(Ref<ResourceFormatSaver> p_format_saver, bool p_at_front) {
|
2019-09-25 16:28:50 +08:00
|
|
|
ERR_FAIL_COND_MSG(p_format_saver.is_null(), "It's not a reference to a valid ResourceFormatSaver object.");
|
2017-03-05 23:44:50 +08:00
|
|
|
ERR_FAIL_COND(saver_count >= MAX_SAVERS);
|
2016-07-20 08:40:05 +08:00
|
|
|
|
|
|
|
if (p_at_front) {
|
2017-03-05 23:44:50 +08:00
|
|
|
for (int i = saver_count; i > 0; i--) {
|
|
|
|
saver[i] = saver[i - 1];
|
2016-07-20 08:40:05 +08:00
|
|
|
}
|
2017-03-05 23:44:50 +08:00
|
|
|
saver[0] = p_format_saver;
|
2016-07-20 08:40:05 +08:00
|
|
|
saver_count++;
|
|
|
|
} else {
|
2017-03-05 23:44:50 +08:00
|
|
|
saver[saver_count++] = p_format_saver;
|
2016-07-20 08:40:05 +08:00
|
|
|
}
|
2014-02-10 09:10:30 +08:00
|
|
|
}
|
2018-06-11 08:59:53 +08:00
|
|
|
|
|
|
|
void ResourceSaver::remove_resource_format_saver(Ref<ResourceFormatSaver> p_format_saver) {
|
2019-09-25 16:28:50 +08:00
|
|
|
ERR_FAIL_COND_MSG(p_format_saver.is_null(), "It's not a reference to a valid ResourceFormatSaver object.");
|
2018-06-11 08:59:53 +08:00
|
|
|
|
|
|
|
// Find saver
|
|
|
|
int i = 0;
|
|
|
|
for (; i < saver_count; ++i) {
|
2020-05-14 22:41:43 +08:00
|
|
|
if (saver[i] == p_format_saver) {
|
2018-06-11 08:59:53 +08:00
|
|
|
break;
|
2020-05-14 22:41:43 +08:00
|
|
|
}
|
2018-06-11 08:59:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ERR_FAIL_COND(i >= saver_count); // Not found
|
|
|
|
|
|
|
|
// Shift next savers up
|
|
|
|
for (; i < saver_count - 1; ++i) {
|
|
|
|
saver[i] = saver[i + 1];
|
|
|
|
}
|
|
|
|
saver[saver_count - 1].unref();
|
|
|
|
--saver_count;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ref<ResourceFormatSaver> ResourceSaver::_find_custom_resource_format_saver(String path) {
|
|
|
|
for (int i = 0; i < saver_count; ++i) {
|
|
|
|
if (saver[i]->get_script_instance() && saver[i]->get_script_instance()->get_script()->get_path() == path) {
|
|
|
|
return saver[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Ref<ResourceFormatSaver>();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ResourceSaver::add_custom_resource_format_saver(String script_path) {
|
2020-05-14 22:41:43 +08:00
|
|
|
if (_find_custom_resource_format_saver(script_path).is_valid()) {
|
2018-06-11 08:59:53 +08:00
|
|
|
return false;
|
2020-05-14 22:41:43 +08:00
|
|
|
}
|
2018-06-11 08:59:53 +08: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, "ResourceFormatSaver");
|
2019-08-15 10:57:49 +08:00
|
|
|
ERR_FAIL_COND_V_MSG(!valid_type, false, "Script does not inherit a CustomResourceSaver: " + script_path + ".");
|
2018-06-11 08:59:53 +08:00
|
|
|
|
2021-06-18 06:03:09 +08:00
|
|
|
Object *obj = ClassDB::instantiate(ibt);
|
2018-06-11 08:59:53 +08:00
|
|
|
|
2020-04-02 07:20:12 +08:00
|
|
|
ERR_FAIL_COND_V_MSG(obj == nullptr, false, "Cannot instance script as custom resource saver, expected 'ResourceFormatSaver' inheritance, got: " + String(ibt) + ".");
|
2018-06-11 08:59:53 +08:00
|
|
|
|
2020-11-15 22:14:06 +08:00
|
|
|
Ref<ResourceFormatSaver> crl = Object::cast_to<ResourceFormatSaver>(obj);
|
2020-02-14 03:03:10 +08:00
|
|
|
crl->set_script(s);
|
2018-06-11 08:59:53 +08:00
|
|
|
ResourceSaver::add_resource_format_saver(crl);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ResourceSaver::remove_custom_resource_format_saver(String script_path) {
|
2019-02-13 04:10:08 +08:00
|
|
|
Ref<ResourceFormatSaver> custom_saver = _find_custom_resource_format_saver(script_path);
|
2020-05-14 22:41:43 +08:00
|
|
|
if (custom_saver.is_valid()) {
|
2019-02-13 04:10:08 +08:00
|
|
|
remove_resource_format_saver(custom_saver);
|
2020-05-14 22:41:43 +08:00
|
|
|
}
|
2018-06-11 08:59:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ResourceSaver::add_custom_savers() {
|
|
|
|
// Custom resource savers exploits global class names
|
|
|
|
|
|
|
|
String custom_saver_base_class = ResourceFormatSaver::get_class_static();
|
|
|
|
|
|
|
|
List<StringName> global_classes;
|
|
|
|
ScriptServer::get_global_class_list(&global_classes);
|
|
|
|
|
2021-07-24 21:46:25 +08:00
|
|
|
for (const StringName &class_name : global_classes) {
|
2019-03-09 11:47:27 +08:00
|
|
|
StringName base_class = ScriptServer::get_global_class_native_base(class_name);
|
2018-06-11 08:59:53 +08:00
|
|
|
|
|
|
|
if (base_class == custom_saver_base_class) {
|
|
|
|
String path = ScriptServer::get_global_class_path(class_name);
|
|
|
|
add_custom_resource_format_saver(path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ResourceSaver::remove_custom_savers() {
|
2020-03-17 14:33:00 +08:00
|
|
|
Vector<Ref<ResourceFormatSaver>> custom_savers;
|
2018-06-11 08:59:53 +08:00
|
|
|
for (int i = 0; i < saver_count; ++i) {
|
|
|
|
if (saver[i]->get_script_instance()) {
|
|
|
|
custom_savers.push_back(saver[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < custom_savers.size(); ++i) {
|
|
|
|
remove_resource_format_saver(custom_savers[i]);
|
|
|
|
}
|
|
|
|
}
|
2021-07-24 03:01:18 +08:00
|
|
|
|
|
|
|
ResourceUID::ID ResourceSaver::get_resource_id_for_path(const String &p_path, bool p_generate) {
|
|
|
|
if (save_get_id_for_path) {
|
|
|
|
return save_get_id_for_path(p_path, p_generate);
|
|
|
|
}
|
|
|
|
return ResourceUID::INVALID_ID;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ResourceSaver::set_get_resource_id_for_path(ResourceSaverGetResourceIDForPath p_callback) {
|
|
|
|
save_get_id_for_path = p_callback;
|
|
|
|
}
|