Godot Engine – Multi-platform 2D and 3D game engine
[Web Editor] Make easier to download files from the project
Mariano Suligoy41a44b0a74a9
diff --git a/editor/docks/filesystem_dock.cpp b/editor/docks/filesystem_dock.cppindex f0ea4c0099e..4e0d2c650f4 100644--- a/editor/docks/filesystem_dock.cpp+++ b/editor/docks/filesystem_dock.cpp@@ -78,6 +78,14 @@#include "scene/resources/packed_scene.h"#include "servers/display/display_server.h"+#ifdef WEB_ENABLED+#include "core/os/time.h"+#include "editor/export/project_zip_packer.h"+extern "C" {+extern void godot_js_os_download_buffer(const uint8_t *p_buf, int p_buf_size, const char *p_name, const char *p_mime);+}+#endif // WEB_ENABLED+Control *FileSystemTree::make_custom_tooltip(const String &p_text) const {TreeItem *item = get_item_at_position(get_local_mouse_position());if (!item) {@@ -2292,6 +2300,45 @@ void FileSystemDock::_file_option(int p_option, const Vector<String> &p_selectedOS::get_singleton()->shell_show_in_file_manager(dir, true);} break;+ case FILE_MENU_DOWNLOAD: {+#ifdef WEB_ENABLED+ String fpath = ProjectSettings::get_singleton()->globalize_path(current_path);+ const bool is_directory = DirAccess::dir_exists_absolute(fpath);+ if (is_directory) {+ String output_name;+ if (current_path == "res://") {+ output_name = ProjectZIPPacker::get_project_zip_safe_name();+ } else {+ output_name = fpath.rstrip("/").get_file().to_lower().replace_char(' ', '_');+ const String datetime_safe =+ Time::get_singleton()->get_datetime_string_from_system(false, true).replace_char(' ', '_');+ output_name = vformat("%s_%s.zip", output_name, datetime_safe);+ }+ const String output_path = String("/tmp").path_join(output_name);+ ProjectZIPPacker::pack_zip_absolute_path(output_path, fpath);++ {+ Ref<FileAccess> f = FileAccess::open(output_path, FileAccess::READ);+ ERR_FAIL_COND_MSG(f.is_null(), "Unable to create ZIP file.");+ LocalVector<uint8_t> buf;+ buf.resize(f->get_length());+ f->get_buffer(buf.ptr(), buf.size());+ godot_js_os_download_buffer(buf.ptr(), buf.size(), output_name.utf8().get_data(), "application/zip");+ }++ // Remove the temporary file since it was sent to the user's native filesystem as a download.+ DirAccess::remove_file_or_error(output_path);+ } else {+ Ref<FileAccess> f = FileAccess::open(current_path, FileAccess::READ);+ ERR_FAIL_COND_MSG(f.is_null(), vformat("Failed to open file %s", current_path));+ LocalVector<uint8_t> buf;+ buf.resize(f->get_length());+ f->get_buffer(buf.ptr(), buf.size());+ godot_js_os_download_buffer(buf.ptr(), buf.size(), current_path.get_file().utf8().get_data(), "application/octet-stream");+ }+#endif // WEB_ENABLED+ } break;+case FILE_MENU_OPEN_EXTERNAL: {for (const String &fpath : p_selected) {if (fpath.ends_with("/")) {@@ -2814,6 +2861,10 @@ int FileSystemDock::_get_menu_option_from_key(const Ref<InputEventKey> &p_key) {return FILE_MENU_OPEN_EXTERNAL;} else if (ED_IS_SHORTCUT("filesystem_dock/open_in_terminal", p_key)) {return FILE_MENU_OPEN_IN_TERMINAL;+#endif+#ifdef WEB_ENABLED+ } else if (ED_IS_SHORTCUT("filesystem_dock/download_source", p_key)) {+ return FILE_MENU_DOWNLOAD;#endif} else if (ED_IS_SHORTCUT("filesystem_dock/focus_path", p_key)) {return EXTRA_FOCUS_PATH;@@ -3675,19 +3726,27 @@ void FileSystemDock::_file_and_folders_fill_popup(PopupMenu *p_popup, const Vectp_popup->add_separator();added_separator = true;}+ {+ // Opening the system file manager is not supported on the Android and web editors.+ const bool is_directory = fpath.ends_with("/");- // Opening the system file manager is not supported on the Android and web editors.- const bool is_directory = fpath.ends_with("/");+ p_popup->add_icon_shortcut(get_editor_theme_icon(SNAME("Terminal")), ED_GET_SHORTCUT("filesystem_dock/open_in_terminal"), FILE_MENU_OPEN_IN_TERMINAL);+ p_popup->set_item_text(p_popup->get_item_index(FILE_MENU_OPEN_IN_TERMINAL), is_directory ? TTRC("Open in Terminal") : TTRC("Open Folder in Terminal"));- p_popup->add_icon_shortcut(get_editor_theme_icon(SNAME("Terminal")), ED_GET_SHORTCUT("filesystem_dock/open_in_terminal"), FILE_MENU_OPEN_IN_TERMINAL);- p_popup->set_item_text(p_popup->get_item_index(FILE_MENU_OPEN_IN_TERMINAL), is_directory ? TTRC("Open in Terminal") : TTRC("Open Folder in Terminal"));+ if (!is_directory) {+ p_popup->add_icon_shortcut(get_editor_theme_icon(SNAME("ExternalLink")), ED_GET_SHORTCUT("filesystem_dock/open_in_external_program"), FILE_MENU_OPEN_EXTERNAL);+ }- if (!is_directory) {- p_popup->add_icon_shortcut(get_editor_theme_icon(SNAME("ExternalLink")), ED_GET_SHORTCUT("filesystem_dock/open_in_external_program"), FILE_MENU_OPEN_EXTERNAL);+ p_popup->add_icon_shortcut(get_editor_theme_icon(SNAME("Filesystem")), ED_GET_SHORTCUT("filesystem_dock/show_in_explorer"), FILE_MENU_SHOW_IN_EXPLORER);+ p_popup->set_item_text(p_popup->get_item_index(FILE_MENU_SHOW_IN_EXPLORER), is_directory ? OS::get_singleton()->get_platform_string(OS::PLATFORM_STRING_FILE_MANAGER_OPEN) : OS::get_singleton()->get_platform_string(OS::PLATFORM_STRING_FILE_MANAGER_SHOW));+ }+#endif+#ifdef WEB_ENABLED+ {+ const bool is_directory = fpath.ends_with("/");+ p_popup->add_icon_shortcut(get_editor_theme_icon(SNAME("Download")), ED_GET_SHORTCUT("filesystem_dock/download_source"), FILE_MENU_DOWNLOAD);+ p_popup->set_item_text(p_popup->get_item_index(FILE_MENU_DOWNLOAD), is_directory ? TTRC("Download Folder as ZIP") : TTRC("Download File"));}-- p_popup->add_icon_shortcut(get_editor_theme_icon(SNAME("Filesystem")), ED_GET_SHORTCUT("filesystem_dock/show_in_explorer"), FILE_MENU_SHOW_IN_EXPLORER);- p_popup->set_item_text(p_popup->get_item_index(FILE_MENU_SHOW_IN_EXPLORER), is_directory ? OS::get_singleton()->get_platform_string(OS::PLATFORM_STRING_FILE_MANAGER_OPEN) : OS::get_singleton()->get_platform_string(OS::PLATFORM_STRING_FILE_MANAGER_SHOW));#endifcurrent_path = fpath;@@ -4524,7 +4583,9 @@ FileSystemDock::FileSystemDock() {ED_SHORTCUT("filesystem_dock/open_in_external_program", TTRC("Open in External Program"), KeyModifierMask::CMD_OR_CTRL | KeyModifierMask::ALT | Key::E);ED_SHORTCUT("filesystem_dock/open_in_terminal", TTRC("Open in Terminal"), KeyModifierMask::CMD_OR_CTRL | KeyModifierMask::ALT | Key::T);#endif-+#ifdef WEB_ENABLED+ ED_SHORTCUT("filesystem_dock/download_source", TTRC("Download File(s)"), Key::NONE);+#endifED_SHORTCUT("filesystem_dock/focus_path", TTRC("Focus Path"), KeyModifierMask::CMD_OR_CTRL | Key::L);// Allow both Cmd + L and Cmd + Shift + G to match Safari's and Finder's shortcuts respectively.ED_SHORTCUT_OVERRIDE_ARRAY("filesystem_dock/focus_path", "macos",diff --git a/editor/docks/filesystem_dock.h b/editor/docks/filesystem_dock.hindex 06db08e0905..c0c2c325b04 100644--- a/editor/docks/filesystem_dock.h+++ b/editor/docks/filesystem_dock.h@@ -127,6 +127,7 @@ private:FILE_MENU_SHOW_IN_EXPLORER,FILE_MENU_OPEN_EXTERNAL,FILE_MENU_OPEN_IN_TERMINAL,+ FILE_MENU_DOWNLOAD, // Web editor onlyFILE_MENU_COPY_PATH,FILE_MENU_COPY_ABSOLUTE_PATH,FILE_MENU_COPY_UID,diff --git a/editor/editor_node.cpp b/editor/editor_node.cppindex c1fd51ff686..4f1b132ab13 100644--- a/editor/editor_node.cpp+++ b/editor/editor_node.cpp@@ -223,6 +223,12 @@#include <cstdlib>+#ifdef WEB_ENABLED+extern "C" {+extern void godot_js_os_download_buffer(const uint8_t *p_buf, int p_buf_size, const char *p_name, const char *p_mime);+}+#endif // WEB_ENABLED+EditorNode *EditorNode::singleton = nullptr;static const String EDITOR_NODE_CONFIG_SECTION = "EditorNode";@@ -3781,6 +3787,25 @@ void EditorNode::_menu_option_confirm(int p_option, bool p_confirmed) {OS::get_singleton()->ensure_user_data_dir();OS::get_singleton()->shell_show_in_file_manager(OS::get_singleton()->get_user_data_dir(), true);} break;+ case PROJECT_DOWNLOAD_SOURCE: {+#ifdef WEB_ENABLED+ const String output_name = ProjectZIPPacker::get_project_zip_safe_name();+ const String output_path = String("/tmp").path_join(output_name);+ ProjectZIPPacker::pack_project_zip(output_path);++ {+ Ref<FileAccess> f = FileAccess::open(output_path, FileAccess::READ);+ ERR_FAIL_COND_MSG(f.is_null(), "Unable to create ZIP file.");+ LocalVector<uint8_t> buf;+ buf.resize(f->get_length());+ f->get_buffer(buf.ptr(), buf.size());+ godot_js_os_download_buffer(buf.ptr(), buf.size(), output_name.utf8().get_data(), "application/zip");+ }++ // Remove the temporary file since it was sent to the user's native filesystem as a download.+ DirAccess::remove_file_or_error(output_path);+#endif+ } break;case SCENE_QUIT:case PROJECT_QUIT_TO_PROJECT_MANAGER:case TOOLS_CLEAR_PROJECT_CACHE:@@ -8173,7 +8198,10 @@ void EditorNode::_build_project_menu(bool p_dark_mode) {project_menu->add_separator();project_menu->add_icon_shortcut(get_editor_theme_native_menu_icon(SNAME("ResourcePreloader"), menu_type == MENU_TYPE_GLOBAL, p_dark_mode), ED_GET_SHORTCUT("editor/export"), PROJECT_EXPORT);+#ifndef WEB_ENABLED+ // In Web editor "Download Project Source" option is used insteadproject_menu->add_item(TTRC("Pack Project as ZIP..."), PROJECT_PACK_AS_ZIP);+#endifproject_menu->add_item(TTRC("Setup Android Build..."), PROJECT_SETUP_ANDROID_BUILD);#ifndef ANDROID_ENABLEDproject_menu->add_item(TTRC("Open User Data Folder"), PROJECT_OPEN_USER_DATA_FOLDER);@@ -8191,6 +8219,9 @@ void EditorNode::_build_project_menu(bool p_dark_mode) {project_menu->add_submenu_node_item(TTRC("Tools"), tool_menu);project_menu->add_separator();+#ifdef WEB_ENABLED+ project_menu->add_icon_shortcut(get_editor_theme_native_menu_icon(SNAME("Download"), menu_type == MENU_TYPE_GLOBAL, p_dark_mode), ED_GET_SHORTCUT("editor/download_project_source"), PROJECT_DOWNLOAD_SOURCE);+#endifproject_menu->add_shortcut(ED_GET_SHORTCUT("editor/reload_current_project"), PROJECT_RELOAD_CURRENT_PROJECT);project_menu->add_icon_shortcut(get_editor_theme_native_menu_icon(SNAME("Close"), menu_type == MENU_TYPE_GLOBAL, p_dark_mode), ED_GET_SHORTCUT("editor/quit_to_project_list"), PROJECT_QUIT_TO_PROJECT_MANAGER, true);}@@ -9122,6 +9153,10 @@ EditorNode::EditorNode() {ED_SHORTCUT_AND_COMMAND("editor/upgrade_project", TTRC("Upgrade Project Files..."));ED_SHORTCUT_AND_COMMAND("editor/clear_project_cache", TTRC("Clear Project Cache..."));+#ifdef WEB_ENABLED+ ED_SHORTCUT_AND_COMMAND("editor/download_project_source", TTRC("Download Project Source"));+#endif+ED_SHORTCUT_AND_COMMAND("editor/reload_current_project", TTRC("Reload Current Project"));ED_SHORTCUT_AND_COMMAND("editor/quit_to_project_list", TTRC("Quit to Project List"), KeyModifierMask::CTRL + KeyModifierMask::SHIFT + Key::Q);ED_SHORTCUT_OVERRIDE("editor/quit_to_project_list", "macos", KeyModifierMask::META + KeyModifierMask::CTRL + KeyModifierMask::ALT + Key::Q);diff --git a/editor/editor_node.h b/editor/editor_node.hindex f1e765dc459..59825e8c26b 100644--- a/editor/editor_node.h+++ b/editor/editor_node.h@@ -178,6 +178,7 @@ public:PROJECT_PACK_AS_ZIP,PROJECT_SETUP_ANDROID_BUILD,PROJECT_OPEN_USER_DATA_FOLDER,+ PROJECT_DOWNLOAD_SOURCE, // Web editor onlyPROJECT_RELOAD_CURRENT_PROJECT,PROJECT_QUIT_TO_PROJECT_MANAGER,diff --git a/editor/export/project_zip_packer.cpp b/editor/export/project_zip_packer.cppindex f624fe47fb9..f018bbeb4cb 100644--- a/editor/export/project_zip_packer.cpp+++ b/editor/export/project_zip_packer.cpp@@ -63,6 +63,24 @@ void ProjectZIPPacker::pack_project_zip(const String &p_path) {zipClose(zip, nullptr);}+void ProjectZIPPacker::pack_zip_absolute_path(const String &p_output_path, const String &p_source_absolute_path) {+ ERR_FAIL_COND_MSG(!DirAccess::dir_exists_absolute(p_source_absolute_path), vformat("Path %s doesn't exist or is not a directory.", p_source_absolute_path));+ Ref<FileAccess> io_fa;+ zlib_filefunc_def io = zipio_create_io(&io_fa);++ String base_path = p_source_absolute_path.rstrip("/");+ if (base_path.begins_with("res://")) {+ // This allows to find the parent dir of res:// if needed+ base_path = ProjectSettings::get_singleton()->globalize_path(base_path);+ }+ base_path = base_path.get_base_dir(); // This will force to include the root dir in the zip++ zipFile zip = zipOpen2(p_output_path.utf8().get_data(), APPEND_STATUS_CREATE, nullptr, &io);++ _zip_recursive(p_source_absolute_path, base_path + "/", zip);+ zipClose(zip, nullptr);+}+void ProjectZIPPacker::_zip_file(const String &p_path, const String &p_base_path, zipFile p_zip) {Ref<FileAccess> f = FileAccess::open(p_path, FileAccess::READ);if (f.is_null()) {diff --git a/editor/export/project_zip_packer.h b/editor/export/project_zip_packer.hindex 1fbbd86790d..2da89a58926 100644--- a/editor/export/project_zip_packer.h+++ b/editor/export/project_zip_packer.h@@ -41,4 +41,5 @@ class ProjectZIPPacker {public:static String get_project_zip_safe_name();static void pack_project_zip(const String &p_path);+ static void pack_zip_absolute_path(const String &p_output_path, const String &p_source_absolute_path);};diff --git a/editor/icons/Download.svg b/editor/icons/Download.svgnew file mode 100644index 00000000000..9dd73dccf68--- /dev/null+++ b/editor/icons/Download.svg@@ -0,0 +1 @@+<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16"><g fill="none" stroke="#e0e0e0" stroke-linejoin="round" stroke-width="2"><path d="M8 1v9l4-4m-4 4L4 6"/><path stroke-linecap="round" d="M2 10v4h12v-4"/></g></svg>diff --git a/platform/web/editor/web_tools_editor_plugin.cpp b/platform/web/editor/web_tools_editor_plugin.cppdeleted file mode 100644index 32de94fc93a..00000000000--- a/platform/web/editor/web_tools_editor_plugin.cpp+++ /dev/null@@ -1,79 +0,0 @@-/**************************************************************************/-/* web_tools_editor_plugin.cpp */-/**************************************************************************/-/* This file is part of: */-/* GODOT ENGINE */-/* https://godotengine.org */-/**************************************************************************/-/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */-/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */-/* */-/* Permission is hereby granted, free of charge, to any person obtaining */-/* a copy of this software and associated documentation files (the */-/* "Software"), to deal in the Software without restriction, including */-/* without limitation the rights to use, copy, modify, merge, publish, */-/* distribute, sublicense, and/or sell copies of the Software, and to */-/* permit persons to whom the Software is furnished to do so, subject to */-/* the following conditions: */-/* */-/* The above copyright notice and this permission notice shall be */-/* included in all copies or substantial portions of the Software. */-/* */-/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */-/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */-/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */-/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */-/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */-/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */-/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */-/**************************************************************************/--#include "web_tools_editor_plugin.h"--#include "core/config/engine.h"-#include "core/io/dir_access.h"-#include "core/io/file_access.h"-#include "core/object/callable_mp.h"-#include "editor/editor_node.h"-#include "editor/export/project_zip_packer.h"--#include <emscripten/emscripten.h>--// Web functions defined in library_godot_editor_tools.js-extern "C" {-extern void godot_js_os_download_buffer(const uint8_t *p_buf, int p_buf_size, const char *p_name, const char *p_mime);-}--static void _web_editor_init_callback() {- EditorNode::get_singleton()->add_editor_plugin(memnew(WebToolsEditorPlugin));-}--void WebToolsEditorPlugin::initialize() {- EditorNode::add_init_callback(_web_editor_init_callback);-}--WebToolsEditorPlugin::WebToolsEditorPlugin() {- add_tool_menu_item("Download Project Source", callable_mp(this, &WebToolsEditorPlugin::_download_zip));-}--void WebToolsEditorPlugin::_download_zip() {- if (!Engine::get_singleton() || !Engine::get_singleton()->is_editor_hint()) {- ERR_PRINT("Downloading the project as a ZIP archive is only available in Editor mode.");- return;- }- const String output_name = ProjectZIPPacker::get_project_zip_safe_name();- const String output_path = String("/tmp").path_join(output_name);- ProjectZIPPacker::pack_project_zip(output_path);-- {- Ref<FileAccess> f = FileAccess::open(output_path, FileAccess::READ);- ERR_FAIL_COND_MSG(f.is_null(), "Unable to create ZIP file.");- Vector<uint8_t> buf;- buf.resize(f->get_length());- f->get_buffer(buf.ptrw(), buf.size());- godot_js_os_download_buffer(buf.ptr(), buf.size(), output_name.utf8().get_data(), "application/zip");- }-- // Remove the temporary file since it was sent to the user's native filesystem as a download.- DirAccess::remove_file_or_error(output_path);-}diff --git a/platform/web/editor/web_tools_editor_plugin.h b/platform/web/editor/web_tools_editor_plugin.hdeleted file mode 100644index b095ca434eb..00000000000--- a/platform/web/editor/web_tools_editor_plugin.h+++ /dev/null@@ -1,45 +0,0 @@-/**************************************************************************/-/* web_tools_editor_plugin.h */-/**************************************************************************/-/* This file is part of: */-/* GODOT ENGINE */-/* https://godotengine.org */-/**************************************************************************/-/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */-/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */-/* */-/* Permission is hereby granted, free of charge, to any person obtaining */-/* a copy of this software and associated documentation files (the */-/* "Software"), to deal in the Software without restriction, including */-/* without limitation the rights to use, copy, modify, merge, publish, */-/* distribute, sublicense, and/or sell copies of the Software, and to */-/* permit persons to whom the Software is furnished to do so, subject to */-/* the following conditions: */-/* */-/* The above copyright notice and this permission notice shall be */-/* included in all copies or substantial portions of the Software. */-/* */-/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */-/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */-/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */-/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */-/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */-/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */-/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */-/**************************************************************************/--#pragma once--#include "editor/plugins/editor_plugin.h"--class WebToolsEditorPlugin : public EditorPlugin {- GDCLASS(WebToolsEditorPlugin, EditorPlugin);--private:- void _download_zip();--public:- static void initialize();-- WebToolsEditorPlugin();-};diff --git a/platform/web/web_main.cpp b/platform/web/web_main.cppindex 7f8ba32f24b..7de15bd9893 100644--- a/platform/web/web_main.cpp+++ b/platform/web/web_main.cpp@@ -43,7 +43,6 @@#include "core/io/file_access.h"#include "editor/editor_debugger_server_messageport.h"#include "editor/editor_node.h"-#include "editor/web_tools_editor_plugin.h"#include "scene/main/scene_tree.h"#include "scene/main/window.h" // SceneTree only forward declares it.#endif@@ -138,7 +137,6 @@ extern EMSCRIPTEN_KEEPALIVE int godot_web_main(int argc, char *argv[]) {os = new OS_Web();#ifdef TOOLS_ENABLED- WebToolsEditorPlugin::initialize();EditorNode::add_init_callback(&EditorDebuggerServerMessagePort::initialize);#endif