editor_export_scene.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*************************************************************************/
  2. /* editor_export_scene.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "editor_export_scene.h"
  31. #include "editor/editor_settings.h"
  32. #include "globals.h"
  33. #include "io/resource_loader.h"
  34. #include "io/resource_saver.h"
  35. #include "os/dir_access.h"
  36. #include "os/file_access.h"
  37. #include "scene/resources/packed_scene.h"
  38. Vector<uint8_t> EditorSceneExportPlugin::custom_export(String &p_path, const Ref<EditorExportPlatform> &p_platform) {
  39. if (!EditorImportExport::get_singleton()->get_convert_text_scenes()) {
  40. return Vector<uint8_t>();
  41. }
  42. String extension = p_path.extension();
  43. //step 1 check if scene
  44. if (extension == "xml" || extension == "xres") {
  45. String type = ResourceLoader::get_resource_type(p_path);
  46. if (type != "PackedScene")
  47. return Vector<uint8_t>();
  48. } else if (extension != "tscn" && extension != "xscn") {
  49. return Vector<uint8_t>();
  50. }
  51. //step 2 check if cached
  52. uint64_t sd = 0;
  53. String smd5;
  54. String gp = Globals::get_singleton()->globalize_path(p_path);
  55. String md5 = gp.md5_text();
  56. String tmp_path = EditorSettings::get_singleton()->get_settings_path().plus_file("tmp/");
  57. bool valid = false;
  58. {
  59. //if existing, make sure it's valid
  60. FileAccessRef f = FileAccess::open(tmp_path + "scnexp-" + md5 + ".txt", FileAccess::READ);
  61. if (f) {
  62. uint64_t d = f->get_line().strip_edges().to_int64();
  63. sd = FileAccess::get_modified_time(p_path);
  64. if (d == sd) {
  65. valid = true;
  66. } else {
  67. String cmd5 = f->get_line().strip_edges();
  68. smd5 = FileAccess::get_md5(p_path);
  69. if (cmd5 == smd5) {
  70. valid = true;
  71. }
  72. }
  73. }
  74. }
  75. if (!valid) {
  76. //cache failed, convert
  77. DirAccess *da = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  78. String copy = p_path + ".convert." + extension;
  79. // a copy will allow loading the internal resources without conflicting with opened scenes
  80. da->copy(p_path, copy);
  81. //@todo for tscn use something more efficient
  82. Ref<PackedScene> copyres = ResourceLoader::load(copy, "PackedScene");
  83. da->remove(copy);
  84. memdelete(da);
  85. ERR_FAIL_COND_V(!copyres.is_valid(), Vector<uint8_t>());
  86. Error err = ResourceSaver::save(tmp_path + "scnexp-" + md5 + ".scn", copyres);
  87. copyres = Ref<PackedScene>();
  88. ERR_FAIL_COND_V(err != OK, Vector<uint8_t>());
  89. FileAccessRef f = FileAccess::open(tmp_path + "scnexp-" + md5 + ".txt", FileAccess::WRITE);
  90. if (sd == 0)
  91. sd = FileAccess::get_modified_time(p_path);
  92. if (smd5 == String())
  93. smd5 = FileAccess::get_md5(p_path);
  94. f->store_line(String::num(sd));
  95. f->store_line(smd5);
  96. f->store_line(gp); //source path for reference
  97. }
  98. Vector<uint8_t> ret = FileAccess::get_file_as_array(tmp_path + "scnexp-" + md5 + ".scn");
  99. p_path += ".converted.scn";
  100. return ret;
  101. }
  102. EditorSceneExportPlugin::EditorSceneExportPlugin() {
  103. }