web_tools_editor_plugin.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /**************************************************************************/
  2. /* web_tools_editor_plugin.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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 "web_tools_editor_plugin.h"
  31. #if defined(TOOLS_ENABLED) && defined(WEB_ENABLED)
  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. // Web 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 _web_editor_init_callback() {
  44. EditorNode::get_singleton()->add_editor_plugin(memnew(WebToolsEditorPlugin));
  45. }
  46. void WebToolsEditorPlugin::initialize() {
  47. EditorNode::add_init_callback(_web_editor_init_callback);
  48. }
  49. WebToolsEditorPlugin::WebToolsEditorPlugin() {
  50. add_tool_menu_item("Download Project Source", callable_mp(this, &WebToolsEditorPlugin::_download_zip));
  51. }
  52. void WebToolsEditorPlugin::_download_zip() {
  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. Ref<FileAccess> io_fa;
  59. zlib_filefunc_def io = zipio_create_io(&io_fa);
  60. // Name the downloaded 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", project_name_safe, datetime_safe));
  69. const String output_path = String("/tmp").path_join(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. {
  75. Ref<FileAccess> f = FileAccess::open(output_path, FileAccess::READ);
  76. ERR_FAIL_COND_MSG(f.is_null(), "Unable to create ZIP file.");
  77. Vector<uint8_t> buf;
  78. buf.resize(f->get_length());
  79. f->get_buffer(buf.ptrw(), buf.size());
  80. godot_js_os_download_buffer(buf.ptr(), buf.size(), output_name.utf8().get_data(), "application/zip");
  81. }
  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 WebToolsEditorPlugin::_zip_file(String p_path, String p_base_path, zipFile p_zip) {
  86. Ref<FileAccess> f = FileAccess::open(p_path, FileAccess::READ);
  87. if (f.is_null()) {
  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. String path = p_path.replace_first(p_base_path, "");
  96. zipOpenNewFileInZip(p_zip,
  97. path.utf8().get_data(),
  98. nullptr,
  99. nullptr,
  100. 0,
  101. nullptr,
  102. 0,
  103. nullptr,
  104. Z_DEFLATED,
  105. Z_DEFAULT_COMPRESSION);
  106. zipWriteInFileInZip(p_zip, data.ptr(), data.size());
  107. zipCloseFileInZip(p_zip);
  108. }
  109. void WebToolsEditorPlugin::_zip_recursive(String p_path, String p_base_path, zipFile p_zip) {
  110. Ref<DirAccess> dir = DirAccess::open(p_path);
  111. if (dir.is_null()) {
  112. WARN_PRINT("Unable to open directory for zipping: " + p_path);
  113. return;
  114. }
  115. dir->list_dir_begin();
  116. String cur = dir->get_next();
  117. String project_data_dir_name = ProjectSettings::get_singleton()->get_project_data_dir_name();
  118. while (!cur.is_empty()) {
  119. String cs = p_path.path_join(cur);
  120. if (cur == "." || cur == ".." || cur == project_data_dir_name) {
  121. // Skip
  122. } else if (dir->current_is_dir()) {
  123. String path = cs.replace_first(p_base_path, "") + "/";
  124. zipOpenNewFileInZip(p_zip,
  125. path.utf8().get_data(),
  126. nullptr,
  127. nullptr,
  128. 0,
  129. nullptr,
  130. 0,
  131. nullptr,
  132. Z_DEFLATED,
  133. Z_DEFAULT_COMPRESSION);
  134. zipCloseFileInZip(p_zip);
  135. _zip_recursive(cs, p_base_path, p_zip);
  136. } else {
  137. _zip_file(cs, p_base_path, p_zip);
  138. }
  139. cur = dir->get_next();
  140. }
  141. }
  142. #endif // TOOLS_ENABLED && WEB_ENABLED