javascript_tools_editor_plugin.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*************************************************************************/
  2. /* javascript_tools_editor_plugin.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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. #if defined(TOOLS_ENABLED) && defined(JAVASCRIPT_ENABLED)
  31. #include "javascript_tools_editor_plugin.h"
  32. #include "core/config/engine.h"
  33. #include "core/config/project_settings.h"
  34. #include "core/io/dir_access.h"
  35. #include "core/io/file_access.h"
  36. #include "core/os/time.h"
  37. #include "editor/editor_node.h"
  38. #include <emscripten/emscripten.h>
  39. // JavaScript functions defined in library_godot_editor_tools.js
  40. extern "C" {
  41. extern void godot_js_os_download_buffer(const uint8_t *p_buf, int p_buf_size, const char *p_name, const char *p_mime);
  42. }
  43. static void _javascript_editor_init_callback() {
  44. EditorNode::get_singleton()->add_editor_plugin(memnew(JavaScriptToolsEditorPlugin(EditorNode::get_singleton())));
  45. }
  46. void JavaScriptToolsEditorPlugin::initialize() {
  47. EditorNode::add_init_callback(_javascript_editor_init_callback);
  48. }
  49. JavaScriptToolsEditorPlugin::JavaScriptToolsEditorPlugin(EditorNode *p_editor) {
  50. add_tool_menu_item("Download Project Source", callable_mp(this, &JavaScriptToolsEditorPlugin::_download_zip));
  51. }
  52. void JavaScriptToolsEditorPlugin::_download_zip(Variant p_v) {
  53. if (!Engine::get_singleton() || !Engine::get_singleton()->is_editor_hint()) {
  54. ERR_PRINT("Downloading the project as a ZIP archive is only available in Editor mode.");
  55. return;
  56. }
  57. String resource_path = ProjectSettings::get_singleton()->get_resource_path();
  58. FileAccess *src_f;
  59. zlib_filefunc_def io = zipio_create_io_from_file(&src_f);
  60. // Name the downloded ZIP file to contain the project name and download date for easier organization.
  61. // Replace characters not allowed (or risky) in Windows file names with safe characters.
  62. // In the project name, all invalid characters become an empty string so that a name
  63. // like "Platformer 2: Godette's Revenge" becomes "platformer_2-_godette-s_revenge".
  64. const String project_name = GLOBAL_GET("application/config/name");
  65. const String project_name_safe = project_name.to_lower().replace(" ", "_");
  66. const String datetime_safe =
  67. Time::get_singleton()->get_datetime_string_from_system(false, true).replace(" ", "_");
  68. const String output_name = OS::get_singleton()->get_safe_dir_name(vformat("%s_%s.zip"));
  69. const String output_path = String("/tmp").plus_file(output_name);
  70. zipFile zip = zipOpen2(output_path.utf8().get_data(), APPEND_STATUS_CREATE, nullptr, &io);
  71. const String base_path = resource_path.substr(0, resource_path.rfind("/")) + "/";
  72. _zip_recursive(resource_path, base_path, zip);
  73. zipClose(zip, nullptr);
  74. FileAccess *f = FileAccess::open(output_path, FileAccess::READ);
  75. ERR_FAIL_COND_MSG(!f, "Unable to create ZIP file.");
  76. Vector<uint8_t> buf;
  77. buf.resize(f->get_length());
  78. f->get_buffer(buf.ptrw(), buf.size());
  79. godot_js_os_download_buffer(buf.ptr(), buf.size(), output_name.utf8().get_data(), "application/zip");
  80. f->close();
  81. memdelete(f);
  82. // Remove the temporary file since it was sent to the user's native filesystem as a download.
  83. DirAccess::remove_file_or_error(output_path);
  84. }
  85. void JavaScriptToolsEditorPlugin::_zip_file(String p_path, String p_base_path, zipFile p_zip) {
  86. FileAccess *f = FileAccess::open(p_path, FileAccess::READ);
  87. if (!f) {
  88. WARN_PRINT("Unable to open file for zipping: " + p_path);
  89. return;
  90. }
  91. Vector<uint8_t> data;
  92. uint64_t len = f->get_length();
  93. data.resize(len);
  94. f->get_buffer(data.ptrw(), len);
  95. f->close();
  96. memdelete(f);
  97. String path = p_path.replace_first(p_base_path, "");
  98. zipOpenNewFileInZip(p_zip,
  99. path.utf8().get_data(),
  100. nullptr,
  101. nullptr,
  102. 0,
  103. nullptr,
  104. 0,
  105. nullptr,
  106. Z_DEFLATED,
  107. Z_DEFAULT_COMPRESSION);
  108. zipWriteInFileInZip(p_zip, data.ptr(), data.size());
  109. zipCloseFileInZip(p_zip);
  110. }
  111. void JavaScriptToolsEditorPlugin::_zip_recursive(String p_path, String p_base_path, zipFile p_zip) {
  112. DirAccess *dir = DirAccess::open(p_path);
  113. if (!dir) {
  114. WARN_PRINT("Unable to open directory for zipping: " + p_path);
  115. return;
  116. }
  117. dir->list_dir_begin();
  118. String cur = dir->get_next();
  119. while (!cur.is_empty()) {
  120. String cs = p_path.plus_file(cur);
  121. if (cur == "." || cur == ".." || cur == ".import") {
  122. // Skip
  123. } else if (dir->current_is_dir()) {
  124. String path = cs.replace_first(p_base_path, "") + "/";
  125. zipOpenNewFileInZip(p_zip,
  126. path.utf8().get_data(),
  127. nullptr,
  128. nullptr,
  129. 0,
  130. nullptr,
  131. 0,
  132. nullptr,
  133. Z_DEFLATED,
  134. Z_DEFAULT_COMPRESSION);
  135. zipCloseFileInZip(p_zip);
  136. _zip_recursive(cs, p_base_path, p_zip);
  137. } else {
  138. _zip_file(cs, p_base_path, p_zip);
  139. }
  140. cur = dir->get_next();
  141. }
  142. }
  143. #endif