2023-01-05 13:25:55 +01:00
|
|
|
/**************************************************************************/
|
|
|
|
/* 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. */
|
|
|
|
/**************************************************************************/
|
2020-09-17 18:01:36 +02:00
|
|
|
|
2022-08-28 20:27:45 +02:00
|
|
|
#include "web_tools_editor_plugin.h"
|
2020-09-17 18:01:36 +02:00
|
|
|
|
2023-06-08 14:51:32 +02:00
|
|
|
#if defined(TOOLS_ENABLED) && defined(WEB_ENABLED)
|
|
|
|
|
2020-11-07 23:33:38 +01:00
|
|
|
#include "core/config/engine.h"
|
|
|
|
#include "core/config/project_settings.h"
|
2021-06-11 14:51:48 +02:00
|
|
|
#include "core/io/dir_access.h"
|
|
|
|
#include "core/io/file_access.h"
|
2021-06-13 23:48:33 +02:00
|
|
|
#include "core/os/time.h"
|
2020-09-17 18:01:36 +02:00
|
|
|
#include "editor/editor_node.h"
|
|
|
|
|
|
|
|
#include <emscripten/emscripten.h>
|
|
|
|
|
2022-08-28 20:27:45 +02:00
|
|
|
// Web functions defined in library_godot_editor_tools.js
|
2020-10-23 18:33:20 +02:00
|
|
|
extern "C" {
|
2021-06-14 12:32:03 +02:00
|
|
|
extern void godot_js_os_download_buffer(const uint8_t *p_buf, int p_buf_size, const char *p_name, const char *p_mime);
|
2020-10-23 18:33:20 +02:00
|
|
|
}
|
|
|
|
|
2022-08-28 20:27:45 +02:00
|
|
|
static void _web_editor_init_callback() {
|
|
|
|
EditorNode::get_singleton()->add_editor_plugin(memnew(WebToolsEditorPlugin));
|
2020-09-17 18:01:36 +02:00
|
|
|
}
|
|
|
|
|
2022-08-28 20:27:45 +02:00
|
|
|
void WebToolsEditorPlugin::initialize() {
|
|
|
|
EditorNode::add_init_callback(_web_editor_init_callback);
|
2020-09-17 18:01:36 +02:00
|
|
|
}
|
|
|
|
|
2022-08-28 20:27:45 +02:00
|
|
|
WebToolsEditorPlugin::WebToolsEditorPlugin() {
|
|
|
|
add_tool_menu_item("Download Project Source", callable_mp(this, &WebToolsEditorPlugin::_download_zip));
|
2020-09-17 18:01:36 +02:00
|
|
|
}
|
|
|
|
|
2023-01-20 00:00:53 +01:00
|
|
|
void WebToolsEditorPlugin::_download_zip() {
|
2020-09-17 18:01:36 +02:00
|
|
|
if (!Engine::get_singleton() || !Engine::get_singleton()->is_editor_hint()) {
|
2021-06-13 23:48:33 +02:00
|
|
|
ERR_PRINT("Downloading the project as a ZIP archive is only available in Editor mode.");
|
2020-09-17 18:01:36 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
String resource_path = ProjectSettings::get_singleton()->get_resource_path();
|
|
|
|
|
2022-05-11 14:15:58 +02:00
|
|
|
Ref<FileAccess> io_fa;
|
|
|
|
zlib_filefunc_def io = zipio_create_io(&io_fa);
|
2021-06-13 23:48:33 +02:00
|
|
|
|
Fix various typos
Found via ` codespell -q 3 -S ./thirdparty,*.po,./DONORS.md -L ackward,ang,ans,ba,beng,cas,childs,childrens,dof,doubleclick,expct,fave,findn,gird,hist,inout,leapyear,lod,nd,numer,ois,ony,paket,seeked,sinc,switchs,te,uint,varn`
Update editor/import/resource_importer_layered_texture.cpp
Co-authored-by: Raul Santos <raulsntos@gmail.com>
Update doc/classes/TileSetScenesCollectionSource.xml
Co-authored-by: Raul Santos <raulsntos@gmail.com>
Update scene/gui/graph_edit.cpp
Co-authored-by: Raul Santos <raulsntos@gmail.com>
Update scene/resources/animation.cpp
Co-authored-by: Raul Santos <raulsntos@gmail.com>
Update scene/resources/animation.cpp
Co-authored-by: Raul Santos <raulsntos@gmail.com>
Update scene/resources/animation.cpp
Co-authored-by: Raul Santos <raulsntos@gmail.com>
Update scene/gui/rich_text_label.cpp
Co-authored-by: Raul Santos <raulsntos@gmail.com>
Revert previously committed change
2022-01-02 07:03:58 +01:00
|
|
|
// Name the downloaded ZIP file to contain the project name and download date for easier organization.
|
2021-06-13 23:48:33 +02:00
|
|
|
// Replace characters not allowed (or risky) in Windows file names with safe characters.
|
|
|
|
// In the project name, all invalid characters become an empty string so that a name
|
|
|
|
// like "Platformer 2: Godette's Revenge" becomes "platformer_2-_godette-s_revenge".
|
2021-09-12 19:07:18 +02:00
|
|
|
const String project_name = GLOBAL_GET("application/config/name");
|
|
|
|
const String project_name_safe = project_name.to_lower().replace(" ", "_");
|
2021-06-13 23:48:33 +02:00
|
|
|
const String datetime_safe =
|
|
|
|
Time::get_singleton()->get_datetime_string_from_system(false, true).replace(" ", "_");
|
2023-03-21 20:20:52 +01:00
|
|
|
const String output_name = OS::get_singleton()->get_safe_dir_name(vformat("%s_%s.zip", project_name_safe, datetime_safe));
|
2022-08-30 02:34:01 +02:00
|
|
|
const String output_path = String("/tmp").path_join(output_name);
|
2021-06-13 23:48:33 +02:00
|
|
|
|
|
|
|
zipFile zip = zipOpen2(output_path.utf8().get_data(), APPEND_STATUS_CREATE, nullptr, &io);
|
|
|
|
const String base_path = resource_path.substr(0, resource_path.rfind("/")) + "/";
|
2020-09-17 18:01:36 +02:00
|
|
|
_zip_recursive(resource_path, base_path, zip);
|
2021-04-29 11:47:24 +02:00
|
|
|
zipClose(zip, nullptr);
|
2022-04-12 09:12:40 +02:00
|
|
|
{
|
|
|
|
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");
|
|
|
|
}
|
2021-06-13 23:48:33 +02:00
|
|
|
|
|
|
|
// Remove the temporary file since it was sent to the user's native filesystem as a download.
|
|
|
|
DirAccess::remove_file_or_error(output_path);
|
2020-09-17 18:01:36 +02:00
|
|
|
}
|
|
|
|
|
2022-08-28 20:27:45 +02:00
|
|
|
void WebToolsEditorPlugin::_zip_file(String p_path, String p_base_path, zipFile p_zip) {
|
2022-03-23 10:08:58 +01:00
|
|
|
Ref<FileAccess> f = FileAccess::open(p_path, FileAccess::READ);
|
|
|
|
if (f.is_null()) {
|
2020-09-17 18:01:36 +02:00
|
|
|
WARN_PRINT("Unable to open file for zipping: " + p_path);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Vector<uint8_t> data;
|
2021-05-25 08:58:49 +02:00
|
|
|
uint64_t len = f->get_length();
|
2020-09-17 18:01:36 +02:00
|
|
|
data.resize(len);
|
|
|
|
f->get_buffer(data.ptrw(), len);
|
|
|
|
|
|
|
|
String path = p_path.replace_first(p_base_path, "");
|
|
|
|
zipOpenNewFileInZip(p_zip,
|
|
|
|
path.utf8().get_data(),
|
2021-04-29 11:47:24 +02:00
|
|
|
nullptr,
|
|
|
|
nullptr,
|
2020-09-17 18:01:36 +02:00
|
|
|
0,
|
2021-04-29 11:47:24 +02:00
|
|
|
nullptr,
|
2020-09-17 18:01:36 +02:00
|
|
|
0,
|
2021-04-29 11:47:24 +02:00
|
|
|
nullptr,
|
2020-09-17 18:01:36 +02:00
|
|
|
Z_DEFLATED,
|
|
|
|
Z_DEFAULT_COMPRESSION);
|
|
|
|
zipWriteInFileInZip(p_zip, data.ptr(), data.size());
|
|
|
|
zipCloseFileInZip(p_zip);
|
|
|
|
}
|
|
|
|
|
2022-08-28 20:27:45 +02:00
|
|
|
void WebToolsEditorPlugin::_zip_recursive(String p_path, String p_base_path, zipFile p_zip) {
|
2022-03-23 10:08:58 +01:00
|
|
|
Ref<DirAccess> dir = DirAccess::open(p_path);
|
2022-05-10 23:56:32 +02:00
|
|
|
if (dir.is_null()) {
|
2021-06-13 23:48:33 +02:00
|
|
|
WARN_PRINT("Unable to open directory for zipping: " + p_path);
|
2020-09-17 18:01:36 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
dir->list_dir_begin();
|
|
|
|
String cur = dir->get_next();
|
2021-09-10 17:32:29 +02:00
|
|
|
String project_data_dir_name = ProjectSettings::get_singleton()->get_project_data_dir_name();
|
2020-12-29 09:54:59 +01:00
|
|
|
while (!cur.is_empty()) {
|
2022-08-30 02:34:01 +02:00
|
|
|
String cs = p_path.path_join(cur);
|
2021-09-10 17:32:29 +02:00
|
|
|
if (cur == "." || cur == ".." || cur == project_data_dir_name) {
|
2020-09-17 18:01:36 +02:00
|
|
|
// Skip
|
|
|
|
} else if (dir->current_is_dir()) {
|
|
|
|
String path = cs.replace_first(p_base_path, "") + "/";
|
|
|
|
zipOpenNewFileInZip(p_zip,
|
|
|
|
path.utf8().get_data(),
|
2021-04-29 11:47:24 +02:00
|
|
|
nullptr,
|
|
|
|
nullptr,
|
2020-09-17 18:01:36 +02:00
|
|
|
0,
|
2021-04-29 11:47:24 +02:00
|
|
|
nullptr,
|
2020-09-17 18:01:36 +02:00
|
|
|
0,
|
2021-04-29 11:47:24 +02:00
|
|
|
nullptr,
|
2020-09-17 18:01:36 +02:00
|
|
|
Z_DEFLATED,
|
|
|
|
Z_DEFAULT_COMPRESSION);
|
|
|
|
zipCloseFileInZip(p_zip);
|
|
|
|
_zip_recursive(cs, p_base_path, p_zip);
|
|
|
|
} else {
|
|
|
|
_zip_file(cs, p_base_path, p_zip);
|
|
|
|
}
|
|
|
|
cur = dir->get_next();
|
|
|
|
}
|
|
|
|
}
|
2023-06-08 14:51:32 +02:00
|
|
|
|
|
|
|
#endif // TOOLS_ENABLED && WEB_ENABLED
|