| 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 | |
| 31 | #include "web_tools_editor_plugin.h" |
| 32 | |
| 33 | #include "core/config/engine.h" |
| 34 | #include "core/config/project_settings.h" |
| 35 | #include "core/io/dir_access.h" |
| 36 | #include "core/io/file_access.h" |
| 37 | #include "core/os/time.h" |
| 38 | #include "editor/editor_node.h" |
| 39 | #include "editor/export/project_zip_packer.h" |
| 40 | |
| 41 | #include <emscripten/emscripten.h> |
| 42 | |
| 43 | // Web functions defined in library_godot_editor_tools.js |
| 44 | extern "C" { |
| 45 | extern void godot_js_os_download_buffer(const uint8_t *p_buf, int p_buf_size, const char *p_name, const char *p_mime); |
| 46 | } |
| 47 | |
| 48 | static void _web_editor_init_callback() { |
| 49 | EditorNode::get_singleton()->add_editor_plugin(memnew(WebToolsEditorPlugin)); |
| 50 | } |
| 51 | |
| 52 | void WebToolsEditorPlugin::initialize() { |
| 53 | EditorNode::add_init_callback(_web_editor_init_callback); |
| 54 | } |
| 55 | |
| 56 | WebToolsEditorPlugin::WebToolsEditorPlugin() { |
| 57 | add_tool_menu_item("Download Project Source", callable_mp(this, &WebToolsEditorPlugin::_download_zip)); |
| 58 | } |
| 59 | |
| 60 | void WebToolsEditorPlugin::_download_zip() { |
| 61 | if (!Engine::get_singleton() || !Engine::get_singleton()->is_editor_hint()) { |
| 62 | ERR_PRINT("Downloading the project as a ZIP archive is only available in Editor mode."); |
| 63 | return; |
| 64 | } |
| 65 | const String output_name = ProjectZIPPacker::get_project_zip_safe_name(); |
| 66 | const String output_path = String("/tmp").path_join(output_name); |
| 67 | ProjectZIPPacker::pack_project_zip(output_path); |
| 68 | |
| 69 | { |
| 70 | Ref<FileAccess> f = FileAccess::open(output_path, FileAccess::READ); |
| 71 | ERR_FAIL_COND_MSG(f.is_null(), "Unable to create ZIP file."); |
| 72 | Vector<uint8_t> buf; |
| 73 | buf.resize(f->get_length()); |
| 74 | f->get_buffer(buf.ptrw(), buf.size()); |
| 75 | godot_js_os_download_buffer(buf.ptr(), buf.size(), output_name.utf8().get_data(), "application/zip"); |
| 76 | } |
| 77 | |
| 78 | // Remove the temporary file since it was sent to the user's native filesystem as a download. |
| 79 | DirAccess::remove_file_or_error(output_path); |
| 80 | } |