file_access_pack.cpp 19 KB

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