javascript_tools_editor_plugin.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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/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_editor_download_file(const char *p_path, 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. godot_js_editor_download_file("/tmp/project.zip", "project.zip", "application/zip");
  65. }
  66. void JavaScriptToolsEditorPlugin::_bind_methods() {
  67. ClassDB::bind_method("_download_zip", &JavaScriptToolsEditorPlugin::_download_zip);
  68. }
  69. void JavaScriptToolsEditorPlugin::_zip_file(String p_path, String p_base_path, zipFile p_zip) {
  70. FileAccess *f = FileAccess::open(p_path, FileAccess::READ);
  71. if (!f) {
  72. WARN_PRINT("Unable to open file for zipping: " + p_path);
  73. return;
  74. }
  75. Vector<uint8_t> data;
  76. uint64_t len = f->get_len();
  77. data.resize(len);
  78. f->get_buffer(data.ptrw(), len);
  79. f->close();
  80. memdelete(f);
  81. String path = p_path.replace_first(p_base_path, "");
  82. zipOpenNewFileInZip(p_zip,
  83. path.utf8().get_data(),
  84. NULL,
  85. NULL,
  86. 0,
  87. NULL,
  88. 0,
  89. NULL,
  90. Z_DEFLATED,
  91. Z_DEFAULT_COMPRESSION);
  92. zipWriteInFileInZip(p_zip, data.ptr(), data.size());
  93. zipCloseFileInZip(p_zip);
  94. }
  95. void JavaScriptToolsEditorPlugin::_zip_recursive(String p_path, String p_base_path, zipFile p_zip) {
  96. DirAccess *dir = DirAccess::open(p_path);
  97. if (!dir) {
  98. WARN_PRINT("Unable to open dir for zipping: " + p_path);
  99. return;
  100. }
  101. dir->list_dir_begin();
  102. String cur = dir->get_next();
  103. while (!cur.empty()) {
  104. String cs = p_path.plus_file(cur);
  105. if (cur == "." || cur == ".." || cur == ".import") {
  106. // Skip
  107. } else if (dir->current_is_dir()) {
  108. String path = cs.replace_first(p_base_path, "") + "/";
  109. zipOpenNewFileInZip(p_zip,
  110. path.utf8().get_data(),
  111. NULL,
  112. NULL,
  113. 0,
  114. NULL,
  115. 0,
  116. NULL,
  117. Z_DEFLATED,
  118. Z_DEFAULT_COMPRESSION);
  119. zipCloseFileInZip(p_zip);
  120. _zip_recursive(cs, p_base_path, p_zip);
  121. } else {
  122. _zip_file(cs, p_base_path, p_zip);
  123. }
  124. cur = dir->get_next();
  125. }
  126. }
  127. #endif