file_access_pack.cpp 14 KB

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