javascript_tools_editor_plugin.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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/os/dir_access.h"
  35. #include "core/os/file_access.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. add_tool_menu_item("Download Project Source", callable_mp(this, &JavaScriptToolsEditorPlugin::_download_zip));
  50. }
  51. void JavaScriptToolsEditorPlugin::_download_zip(Variant p_v) {
  52. if (!Engine::get_singleton() || !Engine::get_singleton()->is_editor_hint()) {
  53. WARN_PRINT("Project download is only available in Editor mode");
  54. return;
  55. }
  56. String resource_path = ProjectSettings::get_singleton()->get_resource_path();
  57. FileAccess *src_f;
  58. zlib_filefunc_def io = zipio_create_io_from_file(&src_f);
  59. zipFile zip = zipOpen2("/tmp/project.zip", APPEND_STATUS_CREATE, NULL, &io);
  60. String base_path = resource_path.substr(0, resource_path.rfind("/")) + "/";
  61. _zip_recursive(resource_path, base_path, zip);
  62. zipClose(zip, NULL);
  63. godot_js_editor_download_file("/tmp/project.zip", "project.zip", "application/zip");
  64. }
  65. void JavaScriptToolsEditorPlugin::_zip_file(String p_path, String p_base_path, zipFile p_zip) {
  66. FileAccess *f = FileAccess::open(p_path, FileAccess::READ);
  67. if (!f) {
  68. WARN_PRINT("Unable to open file for zipping: " + p_path);
  69. return;
  70. }
  71. Vector<uint8_t> data;
  72. int len = f->get_len();
  73. data.resize(len);
  74. f->get_buffer(data.ptrw(), len);
  75. f->close();
  76. memdelete(f);
  77. String path = p_path.replace_first(p_base_path, "");
  78. zipOpenNewFileInZip(p_zip,
  79. path.utf8().get_data(),
  80. NULL,
  81. NULL,
  82. 0,
  83. NULL,
  84. 0,
  85. NULL,
  86. Z_DEFLATED,
  87. Z_DEFAULT_COMPRESSION);
  88. zipWriteInFileInZip(p_zip, data.ptr(), data.size());
  89. zipCloseFileInZip(p_zip);
  90. }
  91. void JavaScriptToolsEditorPlugin::_zip_recursive(String p_path, String p_base_path, zipFile p_zip) {
  92. DirAccess *dir = DirAccess::open(p_path);
  93. if (!dir) {
  94. WARN_PRINT("Unable to open dir for zipping: " + p_path);
  95. return;
  96. }
  97. dir->list_dir_begin();
  98. String cur = dir->get_next();
  99. while (!cur.is_empty()) {
  100. String cs = p_path.plus_file(cur);
  101. if (cur == "." || cur == ".." || cur == ".import") {
  102. // Skip
  103. } else if (dir->current_is_dir()) {
  104. String path = cs.replace_first(p_base_path, "") + "/";
  105. zipOpenNewFileInZip(p_zip,
  106. path.utf8().get_data(),
  107. NULL,
  108. NULL,
  109. 0,
  110. NULL,
  111. 0,
  112. NULL,
  113. Z_DEFLATED,
  114. Z_DEFAULT_COMPRESSION);
  115. zipCloseFileInZip(p_zip);
  116. _zip_recursive(cs, p_base_path, p_zip);
  117. } else {
  118. _zip_file(cs, p_base_path, p_zip);
  119. }
  120. cur = dir->get_next();
  121. }
  122. }
  123. #endif