| 1 | /**************************************************************************/ |
| 2 | /* file_access_pack.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 "file_access_pack.h" |
| 32 | |
| 33 | #include "core/io/file_access_encrypted.h" |
| 34 | #include "core/io/file_access_patched.h" |
| 35 | #include "core/object/script_language.h" |
| 36 | #include "core/os/os.h" |
| 37 | #include "core/version.h" |
| 38 | |
| 39 | Error PackedData::add_pack(const String &p_path, bool p_replace_files, uint64_t p_offset) { |
| 40 | for (int i = 0; i < sources.size(); i++) { |
| 41 | if (sources[i]->try_open_pack(p_path, p_replace_files, p_offset)) { |
| 42 | return OK; |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | return ERR_FILE_UNRECOGNIZED; |
| 47 | } |
| 48 | |
| 49 | void PackedData::add_path(const String &p_pkg_path, const String &p_path, uint64_t p_ofs, uint64_t p_size, const uint8_t *p_md5, PackSource *p_src, bool p_replace_files, bool p_encrypted, bool p_bundle, bool p_delta) { |
| 50 | String simplified_path = p_path.simplify_path().trim_prefix("res://"); |
| 51 | PathMD5 pmd5(simplified_path.md5_buffer()); |
| 52 | |
| 53 | bool exists = files.has(pmd5); |
| 54 | |
| 55 | PackedFile pf; |
| 56 | pf.encrypted = p_encrypted; |
| 57 | pf.bundle = p_bundle; |
| 58 | pf.delta = p_delta; |
| 59 | pf.pack = p_pkg_path; |
| 60 | pf.offset = p_ofs; |
| 61 | pf.size = p_size; |
| 62 | for (int i = 0; i < 16; i++) { |
| 63 | pf.md5[i] = p_md5[i]; |
| 64 | } |
| 65 | pf.src = p_src; |
| 66 | |
| 67 | if (p_delta) { |
| 68 | delta_patches[pmd5].push_back(pf); |
| 69 | } else if (!exists || p_replace_files) { |
| 70 | files[pmd5] = pf; |
| 71 | delta_patches[pmd5].clear(); |
| 72 | } |
| 73 | |
| 74 | if (!exists) { |
| 75 | // Search for directory. |
| 76 | PackedDir *cd = root; |
| 77 | |
| 78 | if (simplified_path.contains_char('/')) { // In a subdirectory. |
| 79 | Vector<String> ds = simplified_path.get_base_dir().split("/"); |
| 80 | |
| 81 | for (int j = 0; j < ds.size(); j++) { |
| 82 | if (!cd->subdirs.has(ds[j])) { |
| 83 | PackedDir *pd = memnew(PackedDir); |
| 84 | pd->name = ds[j]; |
| 85 | pd->parent = cd; |
| 86 | cd->subdirs[pd->name] = pd; |
| 87 | cd = pd; |
| 88 | } else { |
| 89 | cd = cd->subdirs[ds[j]]; |
| 90 | } |
| 91 | } |
| 92 | } |
| 93 | String filename = simplified_path.get_file(); |
| 94 | // Don't add as a file if the path points to a directory. |
| 95 | if (!filename.is_empty()) { |
| 96 | cd->files.insert(filename); |
| 97 | } |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | void PackedData::remove_path(const String &p_path) { |
| 102 | String simplified_path = p_path.simplify_path().trim_prefix("res://"); |
| 103 | PathMD5 pmd5(simplified_path.md5_buffer()); |
| 104 | if (!files.has(pmd5)) { |
| 105 | return; |
| 106 | } |
| 107 | |
| 108 | // Search for directory. |
| 109 | PackedDir *cd = root; |
| 110 | |
| 111 | if (simplified_path.contains_char('/')) { // In a subdirectory. |
| 112 | Vector<String> ds = simplified_path.get_base_dir().split("/"); |
| 113 | |
| 114 | for (int j = 0; j < ds.size(); j++) { |
| 115 | if (!cd->subdirs.has(ds[j])) { |
| 116 | return; // Subdirectory does not exist, do not bother creating. |
| 117 | } else { |
| 118 | cd = cd->subdirs[ds[j]]; |
| 119 | } |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | cd->files.erase(simplified_path.get_file()); |
| 124 | |
| 125 | files.erase(pmd5); |
| 126 | } |
| 127 | |
| 128 | void PackedData::add_pack_source(PackSource *p_source) { |
| 129 | if (p_source != nullptr) { |
| 130 | sources.push_back(p_source); |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | uint8_t *PackedData::get_file_hash(const String &p_path) { |
| 135 | String simplified_path = p_path.simplify_path().trim_prefix("res://"); |
| 136 | PathMD5 pmd5(simplified_path.md5_buffer()); |
| 137 | HashMap<PathMD5, PackedFile, PathMD5>::Iterator E = files.find(pmd5); |
| 138 | if (!E) { |
| 139 | return nullptr; |
| 140 | } |
| 141 | |
| 142 | return E->value.md5; |
| 143 | } |
| 144 | |
| 145 | Vector<PackedData::PackedFile> PackedData::get_delta_patches(const String &p_path) const { |
| 146 | String simplified_path = p_path.simplify_path().trim_prefix("res://"); |
| 147 | PathMD5 pmd5(simplified_path.md5_buffer()); |
| 148 | HashMap<PathMD5, Vector<PackedFile>, PathMD5>::ConstIterator E = delta_patches.find(pmd5); |
| 149 | if (!E) { |
| 150 | return Vector<PackedFile>(); |
| 151 | } |
| 152 | |
| 153 | return E->value; |
| 154 | } |
| 155 | |
| 156 | bool PackedData::has_delta_patches(const String &p_path) const { |
| 157 | String simplified_path = p_path.simplify_path().trim_prefix("res://"); |
| 158 | PathMD5 pmd5(simplified_path.md5_buffer()); |
| 159 | HashMap<PathMD5, Vector<PackedFile>, PathMD5>::ConstIterator E = delta_patches.find(pmd5); |
| 160 | if (!E) { |
| 161 | return false; |
| 162 | } |
| 163 | |
| 164 | return !E->value.is_empty(); |
| 165 | } |
| 166 | |
| 167 | HashSet<String> PackedData::get_file_paths() const { |
| 168 | HashSet<String> file_paths; |
| 169 | _get_file_paths(root, root->name, file_paths); |
| 170 | return file_paths; |
| 171 | } |
| 172 | |
| 173 | void PackedData::_get_file_paths(PackedDir *p_dir, const String &p_parent_dir, HashSet<String> &r_paths) const { |
| 174 | for (const String &E : p_dir->files) { |
| 175 | r_paths.insert(p_parent_dir.path_join(E)); |
| 176 | } |
| 177 | |
| 178 | for (const KeyValue<String, PackedDir *> &E : p_dir->subdirs) { |
| 179 | _get_file_paths(E.value, p_parent_dir.path_join(E.key), r_paths); |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | void PackedData::clear() { |
| 184 | files.clear(); |
| 185 | delta_patches.clear(); |
| 186 | _free_packed_dirs(root); |
| 187 | root = memnew(PackedDir); |
| 188 | } |
| 189 | |
| 190 | PackedData::PackedData() { |
| 191 | singleton = this; |
| 192 | root = memnew(PackedDir); |
| 193 | |
| 194 | add_pack_source(memnew(PackedSourcePCK)); |
| 195 | } |
| 196 | |
| 197 | void PackedData::_free_packed_dirs(PackedDir *p_dir) { |
| 198 | for (const KeyValue<String, PackedDir *> &E : p_dir->subdirs) { |
| 199 | _free_packed_dirs(E.value); |
| 200 | } |
| 201 | memdelete(p_dir); |
| 202 | } |
| 203 | |
| 204 | PackedData::~PackedData() { |
| 205 | if (singleton == this) { |
| 206 | singleton = nullptr; |
| 207 | } |
| 208 | |
| 209 | for (int i = 0; i < sources.size(); i++) { |
| 210 | memdelete(sources[i]); |
| 211 | } |
| 212 | _free_packed_dirs(root); |
| 213 | } |
| 214 | |
| 215 | ////////////////////////////////////////////////////////////////// |
| 216 | |
| 217 | bool PackedSourcePCK::try_open_pack(const String &p_path, bool p_replace_files, uint64_t p_offset) { |
| 218 | Ref<FileAccess> f = FileAccess::open(p_path, FileAccess::READ); |
| 219 | if (f.is_null()) { |
| 220 | return false; |
| 221 | } |
| 222 | |
| 223 | bool pck_header_found = false; |
| 224 | |
| 225 | // Search for the header at the start offset - standalone PCK file. |
| 226 | f->seek(p_offset); |
| 227 | uint32_t magic = f->get_32(); |
| 228 | if (magic == PACK_HEADER_MAGIC) { |
| 229 | pck_header_found = true; |
| 230 | } |
| 231 | |
| 232 | // Search for the header in the executable "pck" section - self contained executable. |
| 233 | if (!pck_header_found) { |
| 234 | // Loading with offset feature not supported for self contained exe files. |
| 235 | if (p_offset != 0) { |
| 236 | ERR_FAIL_V_MSG(false, "Loading self-contained executable with offset not supported."); |
| 237 | } |
| 238 | |
| 239 | int64_t pck_off = OS::get_singleton()->get_embedded_pck_offset(); |
| 240 | if (pck_off != 0) { |
| 241 | // Search for the header, in case PCK start and section have different alignment. |
| 242 | for (int i = 0; i < 8; i++) { |
| 243 | f->seek(pck_off); |
| 244 | magic = f->get_32(); |
| 245 | if (magic == PACK_HEADER_MAGIC) { |
| 246 | #ifdef DEBUG_ENABLED |
| 247 | print_verbose("PCK header found in executable pck section, loading from offset 0x" + String::num_int64(pck_off - 4, 16)); |
| 248 | #endif |
| 249 | pck_header_found = true; |
| 250 | break; |
| 251 | } |
| 252 | pck_off++; |
| 253 | } |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | // Search for the header at the end of file - self contained executable. |
| 258 | if (!pck_header_found) { |
| 259 | // Loading with offset feature not supported for self contained exe files. |
| 260 | if (p_offset != 0) { |
| 261 | ERR_FAIL_V_MSG(false, "Loading self-contained executable with offset not supported."); |
| 262 | } |
| 263 | |
| 264 | f->seek_end(); |
| 265 | f->seek(f->get_position() - 4); |
| 266 | magic = f->get_32(); |
| 267 | |
| 268 | if (magic == PACK_HEADER_MAGIC) { |
| 269 | f->seek(f->get_position() - 12); |
| 270 | uint64_t ds = f->get_64(); |
| 271 | f->seek(f->get_position() - ds - 8); |
| 272 | magic = f->get_32(); |
| 273 | if (magic == PACK_HEADER_MAGIC) { |
| 274 | #ifdef DEBUG_ENABLED |
| 275 | print_verbose("PCK header found at the end of executable, loading from offset 0x" + String::num_int64(f->get_position() - 4, 16)); |
| 276 | #endif |
| 277 | pck_header_found = true; |
| 278 | } |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | if (!pck_header_found) { |
| 283 | return false; |
| 284 | } |
| 285 | |
| 286 | int64_t pck_start_pos = f->get_position() - 4; |
| 287 | |
| 288 | // Read header. |
| 289 | uint32_t version = f->get_32(); |
| 290 | uint32_t ver_major = f->get_32(); |
| 291 | uint32_t ver_minor = f->get_32(); |
| 292 | uint32_t ver_patch = f->get_32(); // Not used for validation. |
| 293 | |
| 294 | ERR_FAIL_COND_V_MSG(version != PACK_FORMAT_VERSION_V3 && version != PACK_FORMAT_VERSION_V2, false, vformat("Pack version unsupported: %d.", version)); |
| 295 | ERR_FAIL_COND_V_MSG(ver_major > GODOT_VERSION_MAJOR || (ver_major == GODOT_VERSION_MAJOR && ver_minor > GODOT_VERSION_MINOR), false, vformat("Pack created with a newer version of the engine: %d.%d.%d.", ver_major, ver_minor, ver_patch)); |
| 296 | |
| 297 | uint32_t pack_flags = f->get_32(); |
| 298 | bool enc_directory = (pack_flags & PACK_DIR_ENCRYPTED); |
| 299 | bool rel_filebase = (pack_flags & PACK_REL_FILEBASE); // Note: Always enabled for V3. |
| 300 | bool sparse_bundle = (pack_flags & PACK_SPARSE_BUNDLE); |
| 301 | |
| 302 | uint64_t file_base = f->get_64(); |
| 303 | if ((version == PACK_FORMAT_VERSION_V3) || (version == PACK_FORMAT_VERSION_V2 && rel_filebase)) { |
| 304 | file_base += pck_start_pos; |
| 305 | } |
| 306 | |
| 307 | if (version == PACK_FORMAT_VERSION_V3) { |
| 308 | // V3: Read directory offset and skip reserved part of the header. |
| 309 | uint64_t dir_offset = f->get_64() + pck_start_pos; |
| 310 | f->seek(dir_offset); |
| 311 | } else if (version == PACK_FORMAT_VERSION_V2) { |
| 312 | // V2: Directory directly after the header. |
| 313 | for (int i = 0; i < 16; i++) { |
| 314 | f->get_32(); // Reserved. |
| 315 | } |
| 316 | } |
| 317 | |
| 318 | // Read directory. |
| 319 | int file_count = f->get_32(); |
| 320 | if (enc_directory) { |
| 321 | Ref<FileAccessEncrypted> fae; |
| 322 | fae.instantiate(); |
| 323 | ERR_FAIL_COND_V_MSG(fae.is_null(), false, "Can't open encrypted pack directory."); |
| 324 | |
| 325 | Vector<uint8_t> key; |
| 326 | key.resize(32); |
| 327 | for (int i = 0; i < key.size(); i++) { |
| 328 | key.write[i] = script_encryption_key[i]; |
| 329 | } |
| 330 | |
| 331 | Error err = fae->open_and_parse(f, key, FileAccessEncrypted::MODE_READ, false); |
| 332 | ERR_FAIL_COND_V_MSG(err, false, "Can't open encrypted pack directory."); |
| 333 | f = fae; |
| 334 | } |
| 335 | |
| 336 | for (int i = 0; i < file_count; i++) { |
| 337 | uint32_t sl = f->get_32(); |
| 338 | CharString cs; |
| 339 | cs.resize_uninitialized(sl + 1); |
| 340 | f->get_buffer((uint8_t *)cs.ptr(), sl); |
| 341 | cs[sl] = 0; |
| 342 | |
| 343 | String path = String::utf8(cs.ptr(), sl); |
| 344 | uint64_t ofs = f->get_64(); |
| 345 | uint64_t size = f->get_64(); |
| 346 | uint8_t md5[16]; |
| 347 | f->get_buffer(md5, 16); |
| 348 | uint32_t flags = f->get_32(); |
| 349 | |
| 350 | if (flags & PACK_FILE_REMOVAL) { // The file was removed. |
| 351 | PackedData::get_singleton()->remove_path(path); |
| 352 | } else { |
| 353 | PackedData::get_singleton()->add_path(p_path, path, file_base + ofs, size, md5, this, p_replace_files, (flags & PACK_FILE_ENCRYPTED), sparse_bundle, (flags & PACK_FILE_DELTA)); |
| 354 | } |
| 355 | } |
| 356 | |
| 357 | return true; |
| 358 | } |
| 359 | |
| 360 | Ref<FileAccess> PackedSourcePCK::get_file(const String &p_path, PackedData::PackedFile *p_file) { |
| 361 | Ref<FileAccess> file(memnew(FileAccessPack(p_path, *p_file))); |
| 362 | |
| 363 | if (PackedData::get_singleton()->has_delta_patches(p_path)) { |
| 364 | Ref<FileAccessPatched> file_patched; |
| 365 | file_patched.instantiate(); |
| 366 | Error err = file_patched->open_custom(file); |
| 367 | ERR_FAIL_COND_V(err != OK, Ref<FileAccess>()); |
| 368 | file = file_patched; |
| 369 | } |
| 370 | |
| 371 | return file; |
| 372 | } |
| 373 | |
| 374 | ////////////////////////////////////////////////////////////////// |
| 375 | |
| 376 | bool PackedSourceDirectory::try_open_pack(const String &p_path, bool p_replace_files, uint64_t p_offset) { |
| 377 | // Load with offset feature only supported for PCK files. |
| 378 | ERR_FAIL_COND_V_MSG(p_offset != 0, false, "Invalid PCK data. Note that loading files with a non-zero offset isn't supported with directories."); |
| 379 | |
| 380 | if (p_path != "res://") { |
| 381 | return false; |
| 382 | } |
| 383 | add_directory(p_path, p_replace_files); |
| 384 | return true; |
| 385 | } |
| 386 | |
| 387 | Ref<FileAccess> PackedSourceDirectory::get_file(const String &p_path, PackedData::PackedFile *p_file) { |
| 388 | Ref<FileAccess> ret = FileAccess::create_for_path(p_path); |
| 389 | ret->reopen(p_path, FileAccess::READ); |
| 390 | return ret; |
| 391 | } |
| 392 | |
| 393 | void PackedSourceDirectory::add_directory(const String &p_path, bool p_replace_files) { |
| 394 | Ref<DirAccess> da = DirAccess::open(p_path); |
| 395 | if (da.is_null()) { |
| 396 | return; |
| 397 | } |
| 398 | da->set_include_hidden(true); |
| 399 | |
| 400 | for (const String &file_name : da->get_files()) { |
| 401 | String file_path = p_path.path_join(file_name); |
| 402 | uint8_t md5[16] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; |
| 403 | PackedData::get_singleton()->add_path(p_path, file_path, 0, 0, md5, this, p_replace_files, false, false, false); |
| 404 | } |
| 405 | |
| 406 | for (const String &sub_dir_name : da->get_directories()) { |
| 407 | String sub_dir_path = p_path.path_join(sub_dir_name); |
| 408 | add_directory(sub_dir_path, p_replace_files); |
| 409 | } |
| 410 | } |
| 411 | |
| 412 | ////////////////////////////////////////////////////////////////// |
| 413 | |
| 414 | Error FileAccessPack::open_internal(const String &p_path, int p_mode_flags) { |
| 415 | ERR_PRINT("Can't open pack-referenced file."); |
| 416 | return ERR_UNAVAILABLE; |
| 417 | } |
| 418 | |
| 419 | bool FileAccessPack::is_open() const { |
| 420 | if (f.is_valid()) { |
| 421 | return f->is_open(); |
| 422 | } else { |
| 423 | return false; |
| 424 | } |
| 425 | } |
| 426 | |
| 427 | void FileAccessPack::seek(uint64_t p_position) { |
| 428 | ERR_FAIL_COND_MSG(f.is_null(), "File must be opened before use."); |
| 429 | |
| 430 | if (p_position > pf.size) { |
| 431 | eof = true; |
| 432 | } else { |
| 433 | eof = false; |
| 434 | } |
| 435 | |
| 436 | f->seek(off + p_position); |
| 437 | pos = p_position; |
| 438 | } |
| 439 | |
| 440 | void FileAccessPack::seek_end(int64_t p_position) { |
| 441 | seek(pf.size + p_position); |
| 442 | } |
| 443 | |
| 444 | uint64_t FileAccessPack::get_position() const { |
| 445 | return pos; |
| 446 | } |
| 447 | |
| 448 | uint64_t FileAccessPack::get_length() const { |
| 449 | return pf.size; |
| 450 | } |
| 451 | |
| 452 | bool FileAccessPack::eof_reached() const { |
| 453 | return eof; |
| 454 | } |
| 455 | |
| 456 | uint64_t FileAccessPack::get_buffer(uint8_t *p_dst, uint64_t p_length) const { |
| 457 | ERR_FAIL_COND_V_MSG(f.is_null(), -1, "File must be opened before use."); |
| 458 | ERR_FAIL_COND_V(!p_dst && p_length > 0, -1); |
| 459 | |
| 460 | if (eof) { |
| 461 | return 0; |
| 462 | } |
| 463 | |
| 464 | int64_t to_read = p_length; |
| 465 | if (to_read + pos > pf.size) { |
| 466 | eof = true; |
| 467 | to_read = (int64_t)pf.size - (int64_t)pos; |
| 468 | } |
| 469 | |
| 470 | pos += to_read; |
| 471 | |
| 472 | if (to_read <= 0) { |
| 473 | return 0; |
| 474 | } |
| 475 | f->get_buffer(p_dst, to_read); |
| 476 | |
| 477 | return to_read; |
| 478 | } |
| 479 | |
| 480 | void FileAccessPack::set_big_endian(bool p_big_endian) { |
| 481 | ERR_FAIL_COND_MSG(f.is_null(), "File must be opened before use."); |
| 482 | |
| 483 | FileAccess::set_big_endian(p_big_endian); |
| 484 | f->set_big_endian(p_big_endian); |
| 485 | } |
| 486 | |
| 487 | Error FileAccessPack::get_error() const { |
| 488 | if (eof) { |
| 489 | return ERR_FILE_EOF; |
| 490 | } |
| 491 | return OK; |
| 492 | } |
| 493 | |
| 494 | void FileAccessPack::flush() { |
| 495 | ERR_FAIL(); |
| 496 | } |
| 497 | |
| 498 | bool FileAccessPack::store_buffer(const uint8_t *p_src, uint64_t p_length) { |
| 499 | ERR_FAIL_V(false); |
| 500 | } |
| 501 | |
| 502 | bool FileAccessPack::file_exists(const String &p_name) { |
| 503 | return false; |
| 504 | } |
| 505 | |
| 506 | void FileAccessPack::close() { |
| 507 | f = Ref<FileAccess>(); |
| 508 | } |
| 509 | |
| 510 | FileAccessPack::FileAccessPack(const String &p_path, const PackedData::PackedFile &p_file) { |
| 511 | path = p_path; |
| 512 | pf = p_file; |
| 513 | if (pf.bundle) { |
| 514 | String simplified_path = p_path.simplify_path(); |
| 515 | f = FileAccess::open(simplified_path, FileAccess::READ | FileAccess::SKIP_PACK); |
| 516 | ERR_FAIL_COND_MSG(f.is_null(), vformat(R"(Can't open pack-referenced file "%s" from sparse pack "%s".)", simplified_path, pf.pack)); |
| 517 | off = 0; // For the sparse pack offset is always zero. |
| 518 | } else { |
| 519 | f = FileAccess::open(pf.pack, FileAccess::READ); |
| 520 | ERR_FAIL_COND_MSG(f.is_null(), vformat(R"(Can't open pack-referenced file "%s" from pack "%s".)", p_path, pf.pack)); |
| 521 | f->seek(pf.offset); |
| 522 | off = pf.offset; |
| 523 | } |
| 524 | |
| 525 | if (pf.encrypted) { |
| 526 | Ref<FileAccessEncrypted> fae; |
| 527 | fae.instantiate(); |
| 528 | ERR_FAIL_COND_MSG(fae.is_null(), vformat(R"(Can't open encrypted pack-referenced file "%s" from pack "%s".)", p_path, pf.pack)); |
| 529 | |
| 530 | Vector<uint8_t> key; |
| 531 | key.resize(32); |
| 532 | for (int i = 0; i < key.size(); i++) { |
| 533 | key.write[i] = script_encryption_key[i]; |
| 534 | } |
| 535 | |
| 536 | Error err = fae->open_and_parse(f, key, FileAccessEncrypted::MODE_READ, false); |
| 537 | ERR_FAIL_COND_MSG(err, vformat(R"(Can't open encrypted pack-referenced file "%s" from pack "%s".)", p_path, pf.pack)); |
| 538 | f = fae; |
| 539 | off = 0; |
| 540 | } |
| 541 | pos = 0; |
| 542 | eof = false; |
| 543 | } |
| 544 | |
| 545 | ////////////////////////////////////////////////////////////////////////////////// |
| 546 | // DIR ACCESS |
| 547 | ////////////////////////////////////////////////////////////////////////////////// |
| 548 | |
| 549 | Error DirAccessPack::list_dir_begin() { |
| 550 | list_dirs.clear(); |
| 551 | list_files.clear(); |
| 552 | |
| 553 | for (const KeyValue<String, PackedData::PackedDir *> &E : current->subdirs) { |
| 554 | list_dirs.push_back(E.key); |
| 555 | } |
| 556 | |
| 557 | for (const String &E : current->files) { |
| 558 | list_files.push_back(E); |
| 559 | } |
| 560 | |
| 561 | return OK; |
| 562 | } |
| 563 | |
| 564 | String DirAccessPack::get_next() { |
| 565 | if (list_dirs.size()) { |
| 566 | cdir = true; |
| 567 | String d = list_dirs.front()->get(); |
| 568 | list_dirs.pop_front(); |
| 569 | return d; |
| 570 | } else if (list_files.size()) { |
| 571 | cdir = false; |
| 572 | String f = list_files.front()->get(); |
| 573 | list_files.pop_front(); |
| 574 | return f; |
| 575 | } else { |
| 576 | return String(); |
| 577 | } |
| 578 | } |
| 579 | |
| 580 | bool DirAccessPack::current_is_dir() const { |
| 581 | return cdir; |
| 582 | } |
| 583 | |
| 584 | bool DirAccessPack::current_is_hidden() const { |
| 585 | return false; |
| 586 | } |
| 587 | |
| 588 | void DirAccessPack::list_dir_end() { |
| 589 | list_dirs.clear(); |
| 590 | list_files.clear(); |
| 591 | } |
| 592 | |
| 593 | int DirAccessPack::get_drive_count() { |
| 594 | return 0; |
| 595 | } |
| 596 | |
| 597 | String DirAccessPack::get_drive(int p_drive) { |
| 598 | return ""; |
| 599 | } |
| 600 | |
| 601 | PackedData::PackedDir *DirAccessPack::_find_dir(const String &p_dir) { |
| 602 | String nd = p_dir.replace_char('\\', '/'); |
| 603 | |
| 604 | // Special handling since simplify_path() will forbid it |
| 605 | if (p_dir == "..") { |
| 606 | return current->parent; |
| 607 | } |
| 608 | |
| 609 | bool absolute = false; |
| 610 | if (nd.begins_with("res://")) { |
| 611 | nd = nd.replace_first("res://", ""); |
| 612 | absolute = true; |
| 613 | } |
| 614 | |
| 615 | nd = nd.simplify_path(); |
| 616 | |
| 617 | if (nd.is_empty()) { |
| 618 | nd = "."; |
| 619 | } |
| 620 | |
| 621 | if (nd.begins_with("/")) { |
| 622 | nd = nd.replace_first("/", ""); |
| 623 | absolute = true; |
| 624 | } |
| 625 | |
| 626 | Vector<String> paths = nd.split("/"); |
| 627 | |
| 628 | PackedData::PackedDir *pd; |
| 629 | |
| 630 | if (absolute) { |
| 631 | pd = PackedData::get_singleton()->root; |
| 632 | } else { |
| 633 | pd = current; |
| 634 | } |
| 635 | |
| 636 | for (int i = 0; i < paths.size(); i++) { |
| 637 | const String &p = paths[i]; |
| 638 | if (p == ".") { |
| 639 | continue; |
| 640 | } else if (p == "..") { |
| 641 | if (pd->parent) { |
| 642 | pd = pd->parent; |
| 643 | } |
| 644 | } else if (pd->subdirs.has(p)) { |
| 645 | pd = pd->subdirs[p]; |
| 646 | |
| 647 | } else { |
| 648 | return nullptr; |
| 649 | } |
| 650 | } |
| 651 | |
| 652 | return pd; |
| 653 | } |
| 654 | |
| 655 | Error DirAccessPack::change_dir(String p_dir) { |
| 656 | PackedData::PackedDir *pd = _find_dir(p_dir); |
| 657 | if (pd) { |
| 658 | current = pd; |
| 659 | return OK; |
| 660 | } else { |
| 661 | return ERR_INVALID_PARAMETER; |
| 662 | } |
| 663 | } |
| 664 | |
| 665 | String DirAccessPack::get_current_dir(bool p_include_drive) const { |
| 666 | PackedData::PackedDir *pd = current; |
| 667 | String p = current->name; |
| 668 | |
| 669 | while (pd->parent) { |
| 670 | pd = pd->parent; |
| 671 | p = pd->name.path_join(p); |
| 672 | } |
| 673 | |
| 674 | return "res://" + p; |
| 675 | } |
| 676 | |
| 677 | bool DirAccessPack::file_exists(String p_file) { |
| 678 | PackedData::PackedDir *pd = _find_dir(p_file.get_base_dir()); |
| 679 | if (!pd) { |
| 680 | return false; |
| 681 | } |
| 682 | return pd->files.has(p_file.get_file()); |
| 683 | } |
| 684 | |
| 685 | bool DirAccessPack::dir_exists(String p_dir) { |
| 686 | return _find_dir(p_dir) != nullptr; |
| 687 | } |
| 688 | |
| 689 | Error DirAccessPack::make_dir(String p_dir) { |
| 690 | return ERR_UNAVAILABLE; |
| 691 | } |
| 692 | |
| 693 | Error DirAccessPack::rename(String p_from, String p_to) { |
| 694 | return ERR_UNAVAILABLE; |
| 695 | } |
| 696 | |
| 697 | Error DirAccessPack::remove(String p_name) { |
| 698 | return ERR_UNAVAILABLE; |
| 699 | } |
| 700 | |
| 701 | uint64_t DirAccessPack::get_space_left() { |
| 702 | return 0; |
| 703 | } |
| 704 | |
| 705 | String DirAccessPack::get_filesystem_type() const { |
| 706 | return "PCK"; |
| 707 | } |
| 708 | |
| 709 | DirAccessPack::DirAccessPack() { |
| 710 | current = PackedData::get_singleton()->root; |
| 711 | } |