file_access_pack.cpp 15 KB

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