javascript_tools_editor_plugin.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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/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. static void _javascript_editor_init_callback() {
  39. EditorNode::get_singleton()->add_editor_plugin(memnew(JavaScriptToolsEditorPlugin(EditorNode::get_singleton())));
  40. }
  41. void JavaScriptToolsEditorPlugin::initialize() {
  42. EditorNode::add_init_callback(_javascript_editor_init_callback);
  43. }
  44. JavaScriptToolsEditorPlugin::JavaScriptToolsEditorPlugin(EditorNode *p_editor) {
  45. Variant v;
  46. add_tool_menu_item("Download Project Source", this, "_download_zip", v);
  47. }
  48. void JavaScriptToolsEditorPlugin::_download_zip(Variant p_v) {
  49. if (!Engine::get_singleton() || !Engine::get_singleton()->is_editor_hint()) {
  50. WARN_PRINT("Project download is only available in Editor mode");
  51. return;
  52. }
  53. String resource_path = ProjectSettings::get_singleton()->get_resource_path();
  54. FileAccess *src_f;
  55. zlib_filefunc_def io = zipio_create_io_from_file(&src_f);
  56. zipFile zip = zipOpen2("/tmp/project.zip", APPEND_STATUS_CREATE, NULL, &io);
  57. String base_path = resource_path.substr(0, resource_path.rfind("/")) + "/";
  58. _zip_recursive(resource_path, base_path, zip);
  59. zipClose(zip, NULL);
  60. EM_ASM({
  61. const path = "/tmp/project.zip";
  62. const size = FS.stat(path)["size"];
  63. const buf = new Uint8Array(size);
  64. const fd = FS.open(path, "r");
  65. FS.read(fd, buf, 0, size);
  66. FS.close(fd);
  67. FS.unlink(path);
  68. const blob = new Blob([buf], { type: "application/zip" });
  69. const url = window.URL.createObjectURL(blob);
  70. const a = document.createElement("a");
  71. a.href = url;
  72. a.download = "project.zip";
  73. a.style.display = "none";
  74. document.body.appendChild(a);
  75. a.click();
  76. a.remove();
  77. window.URL.revokeObjectURL(url);
  78. });
  79. }
  80. void JavaScriptToolsEditorPlugin::_bind_methods() {
  81. ClassDB::bind_method("_download_zip", &JavaScriptToolsEditorPlugin::_download_zip);
  82. }
  83. void JavaScriptToolsEditorPlugin::_zip_file(String p_path, String p_base_path, zipFile p_zip) {
  84. FileAccess *f = FileAccess::open(p_path, FileAccess::READ);
  85. if (!f) {
  86. WARN_PRINT("Unable to open file for zipping: " + p_path);
  87. return;
  88. }
  89. Vector<uint8_t> data;
  90. int len = f->get_len();
  91. data.resize(len);
  92. f->get_buffer(data.ptrw(), len);
  93. f->close();
  94. memdelete(f);
  95. String path = p_path.replace_first(p_base_path, "");
  96. zipOpenNewFileInZip(p_zip,
  97. path.utf8().get_data(),
  98. NULL,
  99. NULL,
  100. 0,
  101. NULL,
  102. 0,
  103. NULL,
  104. Z_DEFLATED,
  105. Z_DEFAULT_COMPRESSION);
  106. zipWriteInFileInZip(p_zip, data.ptr(), data.size());
  107. zipCloseFileInZip(p_zip);
  108. }
  109. void JavaScriptToolsEditorPlugin::_zip_recursive(String p_path, String p_base_path, zipFile p_zip) {
  110. DirAccess *dir = DirAccess::open(p_path);
  111. if (!dir) {
  112. WARN_PRINT("Unable to open dir for zipping: " + p_path);
  113. return;
  114. }
  115. dir->list_dir_begin();
  116. String cur = dir->get_next();
  117. while (!cur.empty()) {
  118. String cs = p_path.plus_file(cur);
  119. if (cur == "." || cur == ".." || cur == ".import") {
  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. NULL,
  126. NULL,
  127. 0,
  128. NULL,
  129. 0,
  130. NULL,
  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