file_access_pack.cpp 16 KB

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