file_access_pack.cpp 15 KB

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