2016-06-18 12:46:12 +00:00
|
|
|
/*************************************************************************/
|
|
|
|
/* editor_export_scene.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2017-08-27 12:11:45 +00:00
|
|
|
/* https://godotengine.org */
|
2016-06-18 12:46:12 +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) */
|
2016-06-18 12:46:12 +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. */
|
|
|
|
/*************************************************************************/
|
2015-12-04 13:18:28 +00:00
|
|
|
#include "editor_export_scene.h"
|
2017-03-18 23:36:26 +00:00
|
|
|
#include "editor/editor_settings.h"
|
|
|
|
#include "globals.h"
|
2015-12-04 13:18:28 +00:00
|
|
|
#include "io/resource_loader.h"
|
|
|
|
#include "io/resource_saver.h"
|
|
|
|
#include "os/dir_access.h"
|
|
|
|
#include "os/file_access.h"
|
|
|
|
#include "scene/resources/packed_scene.h"
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
Vector<uint8_t> EditorSceneExportPlugin::custom_export(String &p_path, const Ref<EditorExportPlatform> &p_platform) {
|
2015-12-04 13:18:28 +00:00
|
|
|
|
|
|
|
if (!EditorImportExport::get_singleton()->get_convert_text_scenes()) {
|
|
|
|
return Vector<uint8_t>();
|
|
|
|
}
|
|
|
|
|
|
|
|
String extension = p_path.extension();
|
|
|
|
|
|
|
|
//step 1 check if scene
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
if (extension == "xml" || extension == "xres") {
|
2015-12-04 13:18:28 +00:00
|
|
|
|
|
|
|
String type = ResourceLoader::get_resource_type(p_path);
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
if (type != "PackedScene")
|
2015-12-04 13:18:28 +00:00
|
|
|
return Vector<uint8_t>();
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
} else if (extension != "tscn" && extension != "xscn") {
|
2015-12-04 13:18:28 +00:00
|
|
|
return Vector<uint8_t>();
|
|
|
|
}
|
|
|
|
|
|
|
|
//step 2 check if cached
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
uint64_t sd = 0;
|
2015-12-04 13:18:28 +00:00
|
|
|
String smd5;
|
|
|
|
String gp = Globals::get_singleton()->globalize_path(p_path);
|
2017-03-18 23:36:26 +00:00
|
|
|
String md5 = gp.md5_text();
|
2015-12-04 13:18:28 +00:00
|
|
|
String tmp_path = EditorSettings::get_singleton()->get_settings_path().plus_file("tmp/");
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
bool valid = false;
|
2015-12-04 13:18:28 +00:00
|
|
|
{
|
|
|
|
//if existing, make sure it's valid
|
2017-03-18 23:36:26 +00:00
|
|
|
FileAccessRef f = FileAccess::open(tmp_path + "scnexp-" + md5 + ".txt", FileAccess::READ);
|
2015-12-04 13:18:28 +00:00
|
|
|
if (f) {
|
|
|
|
|
|
|
|
uint64_t d = f->get_line().strip_edges().to_int64();
|
|
|
|
sd = FileAccess::get_modified_time(p_path);
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
if (d == sd) {
|
|
|
|
valid = true;
|
2015-12-04 13:18:28 +00:00
|
|
|
} else {
|
|
|
|
String cmd5 = f->get_line().strip_edges();
|
|
|
|
smd5 = FileAccess::get_md5(p_path);
|
2017-03-18 23:36:26 +00:00
|
|
|
if (cmd5 == smd5) {
|
|
|
|
valid = true;
|
2015-12-04 13:18:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!valid) {
|
|
|
|
//cache failed, convert
|
|
|
|
DirAccess *da = DirAccess::create(DirAccess::ACCESS_RESOURCES);
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
String copy = p_path + ".convert." + extension;
|
2015-12-04 13:18:28 +00:00
|
|
|
|
|
|
|
// a copy will allow loading the internal resources without conflicting with opened scenes
|
2017-03-18 23:36:26 +00:00
|
|
|
da->copy(p_path, copy);
|
2015-12-04 13:18:28 +00:00
|
|
|
|
|
|
|
//@todo for tscn use something more efficient
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
Ref<PackedScene> copyres = ResourceLoader::load(copy, "PackedScene");
|
2015-12-04 13:18:28 +00:00
|
|
|
|
|
|
|
da->remove(copy);
|
|
|
|
|
|
|
|
memdelete(da);
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
ERR_FAIL_COND_V(!copyres.is_valid(), Vector<uint8_t>());
|
2015-12-04 13:18:28 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
Error err = ResourceSaver::save(tmp_path + "scnexp-" + md5 + ".scn", copyres);
|
2015-12-04 13:18:28 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
copyres = Ref<PackedScene>();
|
2015-12-04 13:18:28 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
ERR_FAIL_COND_V(err != OK, Vector<uint8_t>());
|
2015-12-04 13:18:28 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
FileAccessRef f = FileAccess::open(tmp_path + "scnexp-" + md5 + ".txt", FileAccess::WRITE);
|
2015-12-04 13:18:28 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
if (sd == 0)
|
2015-12-04 13:18:28 +00:00
|
|
|
sd = FileAccess::get_modified_time(p_path);
|
2017-03-18 23:36:26 +00:00
|
|
|
if (smd5 == String())
|
2015-12-04 13:18:28 +00:00
|
|
|
smd5 = FileAccess::get_md5(p_path);
|
|
|
|
|
|
|
|
f->store_line(String::num(sd));
|
|
|
|
f->store_line(smd5);
|
|
|
|
f->store_line(gp); //source path for reference
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
Vector<uint8_t> ret = FileAccess::get_file_as_array(tmp_path + "scnexp-" + md5 + ".scn");
|
2015-12-04 13:18:28 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
p_path += ".converted.scn";
|
2015-12-04 13:18:28 +00:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
EditorSceneExportPlugin::EditorSceneExportPlugin() {
|
2015-12-04 13:18:28 +00:00
|
|
|
}
|