javascript_tools_editor_plugin.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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) 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. #if defined(TOOLS_ENABLED) && defined(JAVASCRIPT_ENABLED)
  31. #include "javascript_tools_editor_plugin.h"
  32. #include "core/engine.h"
  33. #include "core/os/dir_access.h"
  34. #include "core/os/file_access.h"
  35. #include "core/project_settings.h"
  36. #include "editor/editor_node.h"
  37. #include <emscripten/emscripten.h>
  38. // JavaScript functions defined in library_godot_editor_tools.js
  39. extern "C" {
  40. extern void godot_js_os_download_buffer(const uint8_t *p_buf, int p_buf_size, const char *p_name, const char *p_mime);
  41. }
  42. static void _javascript_editor_init_callback() {
  43. EditorNode::get_singleton()->add_editor_plugin(memnew(JavaScriptToolsEditorPlugin(EditorNode::get_singleton())));
  44. }
  45. void JavaScriptToolsEditorPlugin::initialize() {
  46. EditorNode::add_init_callback(_javascript_editor_init_callback);
  47. }
  48. JavaScriptToolsEditorPlugin::JavaScriptToolsEditorPlugin(EditorNode *p_editor) {
  49. Variant v;
  50. add_tool_menu_item("Download Project Source", this, "_download_zip", v);
  51. }
  52. void JavaScriptToolsEditorPlugin::_download_zip(Variant p_v) {
  53. if (!Engine::get_singleton() || !Engine::get_singleton()->is_editor_hint()) {
  54. WARN_PRINT("Project download 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. zipFile zip = zipOpen2("/tmp/project.zip", APPEND_STATUS_CREATE, NULL, &io);
  61. String base_path = resource_path.substr(0, resource_path.rfind("/")) + "/";
  62. _zip_recursive(resource_path, base_path, zip);
  63. zipClose(zip, NULL);
  64. FileAccess *f = FileAccess::open("/tmp/project.zip", FileAccess::READ);
  65. ERR_FAIL_COND_MSG(!f, "Unable to create zip file");
  66. Vector<uint8_t> buf;
  67. buf.resize(f->get_len());
  68. f->get_buffer(buf.ptrw(), buf.size());
  69. godot_js_os_download_buffer(buf.ptr(), buf.size(), "project.zip", "application/zip");
  70. }
  71. void JavaScriptToolsEditorPlugin::_bind_methods() {
  72. ClassDB::bind_method("_download_zip", &JavaScriptToolsEditorPlugin::_download_zip);
  73. }
  74. void JavaScriptToolsEditorPlugin::_zip_file(String p_path, String p_base_path, zipFile p_zip) {
  75. FileAccess *f = FileAccess::open(p_path, FileAccess::READ);
  76. if (!f) {
  77. WARN_PRINT("Unable to open file for zipping: " + p_path);
  78. return;
  79. }
  80. Vector<uint8_t> data;
  81. uint64_t len = f->get_len();
  82. data.resize(len);
  83. f->get_buffer(data.ptrw(), len);
  84. f->close();
  85. memdelete(f);
  86. String path = p_path.replace_first(p_base_path, "");
  87. zipOpenNewFileInZip(p_zip,
  88. path.utf8().get_data(),
  89. NULL,
  90. NULL,
  91. 0,
  92. NULL,
  93. 0,
  94. NULL,
  95. Z_DEFLATED,
  96. Z_DEFAULT_COMPRESSION);
  97. zipWriteInFileInZip(p_zip, data.ptr(), data.size());
  98. zipCloseFileInZip(p_zip);
  99. }
  100. void JavaScriptToolsEditorPlugin::_zip_recursive(String p_path, String p_base_path, zipFile p_zip) {
  101. DirAccess *dir = DirAccess::open(p_path);
  102. if (!dir) {
  103. WARN_PRINT("Unable to open dir for zipping: " + p_path);
  104. return;
  105. }
  106. dir->list_dir_begin();
  107. String cur = dir->get_next();
  108. String project_data_dir_name = ProjectSettings::get_singleton()->get_project_data_dir_name();
  109. while (!cur.empty()) {
  110. String cs = p_path.plus_file(cur);
  111. if (cur == "." || cur == ".." || cur == project_data_dir_name) {
  112. // Skip
  113. } else if (dir->current_is_dir()) {
  114. String path = cs.replace_first(p_base_path, "") + "/";
  115. zipOpenNewFileInZip(p_zip,
  116. path.utf8().get_data(),
  117. NULL,
  118. NULL,
  119. 0,
  120. NULL,
  121. 0,
  122. NULL,
  123. Z_DEFLATED,
  124. Z_DEFAULT_COMPRESSION);
  125. zipCloseFileInZip(p_zip);
  126. _zip_recursive(cs, p_base_path, p_zip);
  127. } else {
  128. _zip_file(cs, p_base_path, p_zip);
  129. }
  130. cur = dir->get_next();
  131. }
  132. }
  133. #endif