Add menu bar item for "Pack Project as ZIP..."

Apply suggestions from code review

Co-authored-by: A Thousand Ships <96648715+AThousandShips@users.noreply.github.com>

Fix includes

Update editor/editor_node.cpp

Co-authored-by: Hugo Locurcio <hugo.locurcio@hugo.pro>
This commit is contained in:
Malcolm Anderson
2024-11-25 22:01:12 -08:00
parent 24d74510e5
commit 6b33037021
6 changed files with 197 additions and 80 deletions

View File

@@ -36,6 +36,7 @@
#include "core/io/file_access.h"
#include "core/os/time.h"
#include "editor/editor_node.h"
#include "editor/export/project_zip_packer.h"
#include <emscripten/emscripten.h>
@@ -61,26 +62,10 @@ void WebToolsEditorPlugin::_download_zip() {
ERR_PRINT("Downloading the project as a ZIP archive is only available in Editor mode.");
return;
}
String resource_path = ProjectSettings::get_singleton()->get_resource_path();
Ref<FileAccess> io_fa;
zlib_filefunc_def io = zipio_create_io(&io_fa);
// Name the downloaded ZIP file to contain the project name and download date for easier organization.
// 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".
const String project_name = GLOBAL_GET("application/config/name");
const String project_name_safe = project_name.to_lower().replace(" ", "_");
const String datetime_safe =
Time::get_singleton()->get_datetime_string_from_system(false, true).replace(" ", "_");
const String output_name = OS::get_singleton()->get_safe_dir_name(vformat("%s_%s.zip", project_name_safe, datetime_safe));
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);
zipFile zip = zipOpen2(output_path.utf8().get_data(), APPEND_STATUS_CREATE, nullptr, &io);
const String base_path = resource_path.substr(0, resource_path.rfind_char('/')) + "/";
_zip_recursive(resource_path, base_path, zip);
zipClose(zip, nullptr);
{
Ref<FileAccess> f = FileAccess::open(output_path, FileAccess::READ);
ERR_FAIL_COND_MSG(f.is_null(), "Unable to create ZIP file.");
@@ -93,63 +78,3 @@ void WebToolsEditorPlugin::_download_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);
}
void WebToolsEditorPlugin::_zip_file(String p_path, String p_base_path, zipFile p_zip) {
Ref<FileAccess> f = FileAccess::open(p_path, FileAccess::READ);
if (f.is_null()) {
WARN_PRINT("Unable to open file for zipping: " + p_path);
return;
}
Vector<uint8_t> data;
uint64_t len = f->get_length();
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(),
nullptr,
nullptr,
0,
nullptr,
0,
nullptr,
Z_DEFLATED,
Z_DEFAULT_COMPRESSION);
zipWriteInFileInZip(p_zip, data.ptr(), data.size());
zipCloseFileInZip(p_zip);
}
void WebToolsEditorPlugin::_zip_recursive(String p_path, String p_base_path, zipFile p_zip) {
Ref<DirAccess> dir = DirAccess::open(p_path);
if (dir.is_null()) {
WARN_PRINT("Unable to open directory for zipping: " + p_path);
return;
}
dir->list_dir_begin();
String cur = dir->get_next();
String project_data_dir_name = ProjectSettings::get_singleton()->get_project_data_dir_name();
while (!cur.is_empty()) {
String cs = p_path.path_join(cur);
if (cur == "." || cur == ".." || cur == project_data_dir_name) {
// Skip
} else if (dir->current_is_dir()) {
String path = cs.replace_first(p_base_path, "") + "/";
zipOpenNewFileInZip(p_zip,
path.utf8().get_data(),
nullptr,
nullptr,
0,
nullptr,
0,
nullptr,
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();
}
}

View File

@@ -38,8 +38,6 @@ class WebToolsEditorPlugin : public EditorPlugin {
GDCLASS(WebToolsEditorPlugin, EditorPlugin);
private:
void _zip_file(String p_path, String p_base_path, zipFile p_zip);
void _zip_recursive(String p_path, String p_base_path, zipFile p_zip);
void _download_zip();
public: