javascript_tools_editor_plugin.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 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));
  45. }
  46. void JavaScriptToolsEditorPlugin::initialize() {
  47. EditorNode::add_init_callback(_javascript_editor_init_callback);
  48. }
  49. JavaScriptToolsEditorPlugin::JavaScriptToolsEditorPlugin() {
  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. zlib_filefunc_def io = zipio_create_io();
  59. // Name the downloaded ZIP file to contain the project name and download date for easier organization.
  60. // Replace characters not allowed (or risky) in Windows file names with safe characters.
  61. // In the project name, all invalid characters become an empty string so that a name
  62. // like "Platformer 2: Godette's Revenge" becomes "platformer_2-_godette-s_revenge".
  63. const String project_name = GLOBAL_GET("application/config/name");
  64. const String project_name_safe = project_name.to_lower().replace(" ", "_");
  65. const String datetime_safe =
  66. Time::get_singleton()->get_datetime_string_from_system(false, true).replace(" ", "_");
  67. const String output_name = OS::get_singleton()->get_safe_dir_name(vformat("%s_%s.zip"));
  68. const String output_path = String("/tmp").plus_file(output_name);
  69. zipFile zip = zipOpen2(output_path.utf8().get_data(), APPEND_STATUS_CREATE, nullptr, &io);
  70. const String base_path = resource_path.substr(0, resource_path.rfind("/")) + "/";
  71. _zip_recursive(resource_path, base_path, zip);
  72. zipClose(zip, nullptr);
  73. {
  74. Ref<FileAccess> f = FileAccess::open(output_path, FileAccess::READ);
  75. ERR_FAIL_COND_MSG(f.is_null(), "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. }
  81. // Remove the temporary file since it was sent to the user's native filesystem as a download.
  82. DirAccess::remove_file_or_error(output_path);
  83. }
  84. void JavaScriptToolsEditorPlugin::_zip_file(String p_path, String p_base_path, zipFile p_zip) {
  85. Ref<FileAccess> f = FileAccess::open(p_path, FileAccess::READ);
  86. if (f.is_null()) {
  87. WARN_PRINT("Unable to open file for zipping: " + p_path);
  88. return;
  89. }
  90. Vector<uint8_t> data;
  91. uint64_t len = f->get_length();
  92. data.resize(len);
  93. f->get_buffer(data.ptrw(), len);
  94. String path = p_path.replace_first(p_base_path, "");
  95. zipOpenNewFileInZip(p_zip,
  96. path.utf8().get_data(),
  97. nullptr,
  98. nullptr,
  99. 0,
  100. nullptr,
  101. 0,
  102. nullptr,
  103. Z_DEFLATED,
  104. Z_DEFAULT_COMPRESSION);
  105. zipWriteInFileInZip(p_zip, data.ptr(), data.size());
  106. zipCloseFileInZip(p_zip);
  107. }
  108. void JavaScriptToolsEditorPlugin::_zip_recursive(String p_path, String p_base_path, zipFile p_zip) {
  109. Ref<DirAccess> dir = DirAccess::open(p_path);
  110. if (!dir) {
  111. WARN_PRINT("Unable to open directory for zipping: " + p_path);
  112. return;
  113. }
  114. dir->list_dir_begin();
  115. String cur = dir->get_next();
  116. String project_data_dir_name = ProjectSettings::get_singleton()->get_project_data_dir_name();
  117. while (!cur.is_empty()) {
  118. String cs = p_path.plus_file(cur);
  119. if (cur == "." || cur == ".." || cur == project_data_dir_name) {
  120. // Skip
  121. } else if (dir->current_is_dir()) {
  122. String path = cs.replace_first(p_base_path, "") + "/";
  123. zipOpenNewFileInZip(p_zip,
  124. path.utf8().get_data(),
  125. nullptr,
  126. nullptr,
  127. 0,
  128. nullptr,
  129. 0,
  130. nullptr,
  131. Z_DEFLATED,
  132. Z_DEFAULT_COMPRESSION);
  133. zipCloseFileInZip(p_zip);
  134. _zip_recursive(cs, p_base_path, p_zip);
  135. } else {
  136. _zip_file(cs, p_base_path, p_zip);
  137. }
  138. cur = dir->get_next();
  139. }
  140. }
  141. #endif