| 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 | uint64_t time = FileAccess::get_modified_time(p_path); |
| 76 | if (time == 0) { |
| 77 | time = Time::get_singleton()->get_unix_time_from_system(); |
| 78 | } |
| 79 | Dictionary tz = Time::get_singleton()->get_time_zone_from_system(); |
| 80 | time += tz["bias"].operator int() * 60; |
| 81 | Dictionary dt = Time::get_singleton()->get_datetime_dict_from_unix_time(time); |
| 82 | |
| 83 | zip_fileinfo zipfi; |
| 84 | zipfi.tmz_date.tm_year = dt["year"]; |
| 85 | zipfi.tmz_date.tm_mon = dt["month"].operator int() - 1; // Note: "tm" month range - 0..11, Godot month range - 1..12, https://www.cplusplus.com/reference/ctime/tm/ |
| 86 | zipfi.tmz_date.tm_mday = dt["day"]; |
| 87 | zipfi.tmz_date.tm_hour = dt["hour"]; |
| 88 | zipfi.tmz_date.tm_min = dt["minute"]; |
| 89 | zipfi.tmz_date.tm_sec = dt["second"]; |
| 90 | zipfi.dosDate = 0; |
| 91 | |
| 92 | // 0100000: regular file type |
| 93 | // 0000755: permissions rwxr-xr-x |
| 94 | // 0000644: permissions rw-r--r-- |
| 95 | uint32_t _mode = FileAccess::get_unix_permissions(p_path); |
| 96 | if (_mode == 0) { |
| 97 | _mode = 0100644; |
| 98 | } else { |
| 99 | _mode |= 0100000; |
| 100 | } |
| 101 | zipfi.external_fa = (_mode << 16L) | ((_mode & 0200) ? 0 : 1); // UUUUUUUUUUUUUUUU0000000000ADVSHR: Unix permissions (U) + DOS read-only flag (R). |
| 102 | zipfi.internal_fa = 0; |
| 103 | |
| 104 | String path = p_path.trim_prefix(p_base_path); |
| 105 | zipOpenNewFileInZip4(p_zip, |
| 106 | path.utf8().get_data(), |
| 107 | &zipfi, |
| 108 | nullptr, |
| 109 | 0, |
| 110 | nullptr, |
| 111 | 0, |
| 112 | nullptr, |
| 113 | Z_DEFLATED, |
| 114 | Z_DEFAULT_COMPRESSION, |
| 115 | 0, |
| 116 | -MAX_WBITS, |
| 117 | DEF_MEM_LEVEL, |
| 118 | Z_DEFAULT_STRATEGY, |
| 119 | nullptr, |
| 120 | 0, |
| 121 | 0x0314, // "version made by", 0x03 - Unix, 0x14 - ZIP specification version 2.0, required to store Unix file permissions |
| 122 | 1 << 11); // Bit 11 is the language encoding flag. When set, filename and comment fields must be encoded using UTF-8. |
| 123 | zipWriteInFileInZip(p_zip, data.ptr(), data.size()); |
| 124 | zipCloseFileInZip(p_zip); |
| 125 | } |
| 126 | |
| 127 | void ProjectZIPPacker::_zip_recursive(const String &p_path, const String &p_base_path, zipFile p_zip) { |
| 128 | Ref<DirAccess> dir = DirAccess::open(p_path); |
| 129 | if (dir.is_null()) { |
| 130 | WARN_PRINT("Unable to open directory for zipping: " + p_path); |
| 131 | return; |
| 132 | } |
| 133 | dir->list_dir_begin(); |
| 134 | String cur = dir->get_next(); |
| 135 | String project_data_dir_name = ProjectSettings::get_singleton()->get_project_data_dir_name(); |
| 136 | while (!cur.is_empty()) { |
| 137 | String cs = p_path.path_join(cur); |
| 138 | if (cur == "." || cur == ".." || cur == project_data_dir_name) { |
| 139 | // Skip |
| 140 | } else if (dir->current_is_dir()) { |
| 141 | String path = cs.trim_prefix(p_base_path) + "/"; |
| 142 | zipOpenNewFileInZip4(p_zip, |
| 143 | path.utf8().get_data(), |
| 144 | nullptr, |
| 145 | nullptr, |
| 146 | 0, |
| 147 | nullptr, |
| 148 | 0, |
| 149 | nullptr, |
| 150 | Z_DEFLATED, |
| 151 | Z_DEFAULT_COMPRESSION, |
| 152 | 0, |
| 153 | -MAX_WBITS, |
| 154 | DEF_MEM_LEVEL, |
| 155 | Z_DEFAULT_STRATEGY, |
| 156 | nullptr, |
| 157 | 0, |
| 158 | 0x0314, // "version made by", 0x03 - Unix, 0x14 - ZIP specification version 2.0, required to store Unix file permissions |
| 159 | 1 << 11); // Bit 11 is the language encoding flag. When set, filename and comment fields must be encoded using UTF-8. |
| 160 | zipCloseFileInZip(p_zip); |
| 161 | _zip_recursive(cs, p_base_path, p_zip); |
| 162 | } else { |
| 163 | _zip_file(cs, p_base_path, p_zip); |
| 164 | } |
| 165 | cur = dir->get_next(); |
| 166 | } |
| 167 | } |