web_tools_editor_plugin.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. #include "core/config/engine.h"
  32. #include "core/config/project_settings.h"
  33. #include "core/io/dir_access.h"
  34. #include "core/io/file_access.h"
  35. #include "core/os/time.h"
  36. #include "editor/editor_node.h"
  37. #include "editor/export/project_zip_packer.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. const String output_name = ProjectZIPPacker::get_project_zip_safe_name();
  58. const String output_path = String("/tmp").path_join(output_name);
  59. ProjectZIPPacker::pack_project_zip(output_path);
  60. {
  61. Ref<FileAccess> f = FileAccess::open(output_path, FileAccess::READ);
  62. ERR_FAIL_COND_MSG(f.is_null(), "Unable to create ZIP file.");
  63. Vector<uint8_t> buf;
  64. buf.resize(f->get_length());
  65. f->get_buffer(buf.ptrw(), buf.size());
  66. godot_js_os_download_buffer(buf.ptr(), buf.size(), output_name.utf8().get_data(), "application/zip");
  67. }
  68. // Remove the temporary file since it was sent to the user's native filesystem as a download.
  69. DirAccess::remove_file_or_error(output_path);
  70. }