Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/editor/export/project_zip_packer.cpp
9903 views
1
/**************************************************************************/
2
/* project_zip_packer.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 "project_zip_packer.h"
32
33
#include "core/config/project_settings.h"
34
#include "core/io/dir_access.h"
35
#include "core/io/file_access.h"
36
#include "core/os/os.h"
37
#include "core/os/time.h"
38
39
String ProjectZIPPacker::get_project_zip_safe_name() {
40
// Name the downloaded ZIP file to contain the project name and download date for easier organization.
41
// Replace characters not allowed (or risky) in Windows file names with safe characters.
42
// In the project name, all invalid characters become an empty string so that a name
43
// like "Platformer 2: Godette's Revenge" becomes "platformer_2-_godette-s_revenge".
44
const String project_name = GLOBAL_GET("application/config/name");
45
const String project_name_safe = project_name.to_lower().replace_char(' ', '_');
46
const String datetime_safe =
47
Time::get_singleton()->get_datetime_string_from_system(false, true).replace_char(' ', '_');
48
const String output_name = OS::get_singleton()->get_safe_dir_name(vformat("%s_%s.zip", project_name_safe, datetime_safe));
49
return output_name;
50
}
51
52
void ProjectZIPPacker::pack_project_zip(const String &p_path) {
53
Ref<FileAccess> io_fa;
54
zlib_filefunc_def io = zipio_create_io(&io_fa);
55
56
String resource_path = ProjectSettings::get_singleton()->get_resource_path();
57
const String base_path = resource_path.substr(0, resource_path.rfind_char('/')) + "/";
58
59
zipFile zip = zipOpen2(p_path.utf8().get_data(), APPEND_STATUS_CREATE, nullptr, &io);
60
_zip_recursive(resource_path, base_path, zip);
61
zipClose(zip, nullptr);
62
}
63
64
void ProjectZIPPacker::_zip_file(const String &p_path, const String &p_base_path, zipFile p_zip) {
65
Ref<FileAccess> f = FileAccess::open(p_path, FileAccess::READ);
66
if (f.is_null()) {
67
WARN_PRINT("Unable to open file for zipping: " + p_path);
68
return;
69
}
70
Vector<uint8_t> data;
71
uint64_t len = f->get_length();
72
data.resize(len);
73
f->get_buffer(data.ptrw(), len);
74
75
String path = p_path.replace_first(p_base_path, "");
76
zipOpenNewFileInZip(p_zip,
77
path.utf8().get_data(),
78
nullptr,
79
nullptr,
80
0,
81
nullptr,
82
0,
83
nullptr,
84
Z_DEFLATED,
85
Z_DEFAULT_COMPRESSION);
86
zipWriteInFileInZip(p_zip, data.ptr(), data.size());
87
zipCloseFileInZip(p_zip);
88
}
89
90
void ProjectZIPPacker::_zip_recursive(const String &p_path, const String &p_base_path, zipFile p_zip) {
91
Ref<DirAccess> dir = DirAccess::open(p_path);
92
if (dir.is_null()) {
93
WARN_PRINT("Unable to open directory for zipping: " + p_path);
94
return;
95
}
96
dir->list_dir_begin();
97
String cur = dir->get_next();
98
String project_data_dir_name = ProjectSettings::get_singleton()->get_project_data_dir_name();
99
while (!cur.is_empty()) {
100
String cs = p_path.path_join(cur);
101
if (cur == "." || cur == ".." || cur == project_data_dir_name) {
102
// Skip
103
} else if (dir->current_is_dir()) {
104
String path = cs.replace_first(p_base_path, "") + "/";
105
zipOpenNewFileInZip(p_zip,
106
path.utf8().get_data(),
107
nullptr,
108
nullptr,
109
0,
110
nullptr,
111
0,
112
nullptr,
113
Z_DEFLATED,
114
Z_DEFAULT_COMPRESSION);
115
zipCloseFileInZip(p_zip);
116
_zip_recursive(cs, p_base_path, p_zip);
117
} else {
118
_zip_file(cs, p_base_path, p_zip);
119
}
120
cur = dir->get_next();
121
}
122
}
123
124