2014-02-10 01:10:30 +00:00
|
|
|
/*************************************************************************/
|
|
|
|
/* resource_saver.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
|
|
|
/*************************************************************************/
|
2017-01-01 21:01:57 +00:00
|
|
|
/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
|
2017-04-07 22:11:42 +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_saver.h"
|
|
|
|
#include "os/file_access.h"
|
2017-07-30 20:53:40 +00:00
|
|
|
#include "project_settings.h"
|
2014-02-10 01:10:30 +00:00
|
|
|
#include "resource_loader.h"
|
2017-03-05 15:44:50 +00:00
|
|
|
#include "script_language.h"
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
ResourceFormatSaver *ResourceSaver::saver[MAX_SAVERS];
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
int ResourceSaver::saver_count = 0;
|
|
|
|
bool ResourceSaver::timestamp_on_save = false;
|
|
|
|
ResourceSavedCallback ResourceSaver::save_callback = 0;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
Error ResourceSaver::save(const String &p_path, const RES &p_resource, uint32_t p_flags) {
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
String extension = p_path.get_extension();
|
|
|
|
Error err = ERR_FILE_UNRECOGNIZED;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
for (int i = 0; i < saver_count; i++) {
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
if (!saver[i]->recognize(p_resource))
|
|
|
|
continue;
|
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
|
|
|
bool recognized = false;
|
|
|
|
saver[i]->get_recognized_extensions(p_resource, &extensions);
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
for (List<String>::Element *E = extensions.front(); E; E = E->next()) {
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (E->get().nocasecmp_to(extension.get_extension()) == 0)
|
|
|
|
recognized = true;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!recognized)
|
|
|
|
continue;
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
String old_path = p_resource->get_path();
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-07-19 20:00:46 +00:00
|
|
|
String local_path = ProjectSettings::get_singleton()->localize_path(p_path);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
RES rwcopy = p_resource;
|
2017-03-05 15:44:50 +00:00
|
|
|
if (p_flags & FLAG_CHANGE_PATH)
|
2014-02-10 01:10:30 +00:00
|
|
|
rwcopy->set_path(local_path);
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
err = saver[i]->save(p_path, p_resource, p_flags);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (err == OK) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
#ifdef TOOLS_ENABLED
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
((Resource *)p_resource.ptr())->set_edited(false);
|
2014-02-10 01:10:30 +00:00
|
|
|
if (timestamp_on_save) {
|
|
|
|
uint64_t mt = FileAccess::get_modified_time(p_path);
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
((Resource *)p_resource.ptr())->set_last_modified_time(mt);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (p_flags & FLAG_CHANGE_PATH)
|
2014-02-10 01:10:30 +00:00
|
|
|
rwcopy->set_path(old_path);
|
|
|
|
|
|
|
|
if (save_callback && p_path.begins_with("res://"))
|
|
|
|
save_callback(p_path);
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
} else {
|
|
|
|
}
|
|
|
|
}
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ResourceSaver::set_save_callback(ResourceSavedCallback p_callback) {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
save_callback = p_callback;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void ResourceSaver::get_recognized_extensions(const RES &p_resource, List<String> *p_extensions) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
for (int i = 0; i < saver_count; i++) {
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
saver[i]->get_recognized_extensions(p_resource, p_extensions);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-20 00:40:05 +00:00
|
|
|
void ResourceSaver::add_resource_format_saver(ResourceFormatSaver *p_format_saver, bool p_at_front) {
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ERR_FAIL_COND(saver_count >= MAX_SAVERS);
|
2016-07-20 00:40:05 +00:00
|
|
|
|
|
|
|
if (p_at_front) {
|
2017-03-05 15:44:50 +00:00
|
|
|
for (int i = saver_count; i > 0; i--) {
|
|
|
|
saver[i] = saver[i - 1];
|
2016-07-20 00:40:05 +00:00
|
|
|
}
|
2017-03-05 15:44:50 +00:00
|
|
|
saver[0] = p_format_saver;
|
2016-07-20 00:40:05 +00:00
|
|
|
saver_count++;
|
|
|
|
} else {
|
2017-03-05 15:44:50 +00:00
|
|
|
saver[saver_count++] = p_format_saver;
|
2016-07-20 00:40:05 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|