file_access_pack.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562
  1. /*************************************************************************/
  2. /* file_access_pack.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  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/version.h"
  34. #include <stdio.h>
  35. Error PackedData::add_pack(const String &p_path, bool p_replace_files, size_t p_offset) {
  36. for (int i = 0; i < sources.size(); i++) {
  37. if (sources[i]->try_open_pack(p_path, p_replace_files, p_offset)) {
  38. return OK;
  39. }
  40. }
  41. return ERR_FILE_UNRECOGNIZED;
  42. }
  43. void PackedData::add_path(const String &pkg_path, const String &path, uint64_t ofs, uint64_t size, const uint8_t *p_md5, PackSource *p_src, bool p_replace_files, bool p_encrypted) {
  44. PathMD5 pmd5(path.md5_buffer());
  45. //printf("adding path %s, %lli, %lli\n", path.utf8().get_data(), pmd5.a, pmd5.b);
  46. bool exists = files.has(pmd5);
  47. PackedFile pf;
  48. pf.encrypted = p_encrypted;
  49. pf.pack = pkg_path;
  50. pf.offset = ofs;
  51. pf.size = 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 = path.replace_first("res://", "");
  62. PackedDir *cd = root;
  63. if (p.find("/") != -1) { //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 = 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 (Map<String, PackedDir *>::Element *E = p_dir->subdirs.front(); E; E = E->next()) {
  97. _free_packed_dirs(E->get());
  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, size_t p_offset) {
  109. FileAccess *f = FileAccess::open(p_path, FileAccess::READ);
  110. if (!f) {
  111. return false;
  112. }
  113. f->seek(p_offset);
  114. uint32_t magic = f->get_32();
  115. if (magic != PACK_HEADER_MAGIC) {
  116. // loading with offset feature not supported for self contained exe files
  117. if (p_offset != 0) {
  118. f->close();
  119. memdelete(f);
  120. ERR_FAIL_V_MSG(false, "Loading self-contained executable with offset not supported.");
  121. }
  122. //maybe at the end.... self contained exe
  123. f->seek_end();
  124. f->seek(f->get_position() - 4);
  125. magic = f->get_32();
  126. if (magic != PACK_HEADER_MAGIC) {
  127. f->close();
  128. memdelete(f);
  129. return false;
  130. }
  131. f->seek(f->get_position() - 12);
  132. uint64_t ds = f->get_64();
  133. f->seek(f->get_position() - ds - 8);
  134. magic = f->get_32();
  135. if (magic != PACK_HEADER_MAGIC) {
  136. f->close();
  137. memdelete(f);
  138. return false;
  139. }
  140. }
  141. uint32_t version = f->get_32();
  142. uint32_t ver_major = f->get_32();
  143. uint32_t ver_minor = f->get_32();
  144. f->get_32(); // patch number, not used for validation.
  145. if (version != PACK_FORMAT_VERSION) {
  146. f->close();
  147. memdelete(f);
  148. ERR_FAIL_V_MSG(false, "Pack version unsupported: " + itos(version) + ".");
  149. }
  150. if (ver_major > VERSION_MAJOR || (ver_major == VERSION_MAJOR && ver_minor > VERSION_MINOR)) {
  151. f->close();
  152. memdelete(f);
  153. ERR_FAIL_V_MSG(false, "Pack created with a newer version of the engine: " + itos(ver_major) + "." + itos(ver_minor) + ".");
  154. }
  155. uint32_t pack_flags = f->get_32();
  156. uint64_t file_base = f->get_64();
  157. bool enc_directory = (pack_flags & PACK_DIR_ENCRYPTED);
  158. for (int i = 0; i < 16; i++) {
  159. //reserved
  160. f->get_32();
  161. }
  162. int file_count = f->get_32();
  163. if (enc_directory) {
  164. FileAccessEncrypted *fae = memnew(FileAccessEncrypted);
  165. if (!fae) {
  166. f->close();
  167. memdelete(f);
  168. ERR_FAIL_V_MSG(false, "Can't open encrypted pack directory.");
  169. }
  170. Vector<uint8_t> key;
  171. key.resize(32);
  172. for (int i = 0; i < key.size(); i++) {
  173. key.write[i] = script_encryption_key[i];
  174. }
  175. Error err = fae->open_and_parse(f, key, FileAccessEncrypted::MODE_READ, false);
  176. if (err) {
  177. f->close();
  178. memdelete(f);
  179. memdelete(fae);
  180. ERR_FAIL_V_MSG(false, "Can't open encrypted pack directory.");
  181. }
  182. f = fae;
  183. }
  184. for (int i = 0; i < file_count; i++) {
  185. uint32_t sl = f->get_32();
  186. CharString cs;
  187. cs.resize(sl + 1);
  188. f->get_buffer((uint8_t *)cs.ptr(), sl);
  189. cs[sl] = 0;
  190. String path;
  191. path.parse_utf8(cs.ptr());
  192. uint64_t ofs = file_base + f->get_64();
  193. uint64_t size = f->get_64();
  194. uint8_t md5[16];
  195. f->get_buffer(md5, 16);
  196. uint32_t flags = f->get_32();
  197. PackedData::get_singleton()->add_path(p_path, path, ofs + p_offset, size, md5, this, p_replace_files, (flags & PACK_FILE_ENCRYPTED));
  198. }
  199. f->close();
  200. memdelete(f);
  201. return true;
  202. }
  203. FileAccess *PackedSourcePCK::get_file(const String &p_path, PackedData::PackedFile *p_file) {
  204. return memnew(FileAccessPack(p_path, *p_file));
  205. }
  206. //////////////////////////////////////////////////////////////////
  207. Error FileAccessPack::_open(const String &p_path, int p_mode_flags) {
  208. ERR_FAIL_V(ERR_UNAVAILABLE);
  209. return ERR_UNAVAILABLE;
  210. }
  211. void FileAccessPack::close() {
  212. f->close();
  213. }
  214. bool FileAccessPack::is_open() const {
  215. return f->is_open();
  216. }
  217. void FileAccessPack::seek(size_t p_position) {
  218. if (p_position > pf.size) {
  219. eof = true;
  220. } else {
  221. eof = false;
  222. }
  223. f->seek(off + p_position);
  224. pos = p_position;
  225. }
  226. void FileAccessPack::seek_end(int64_t p_position) {
  227. seek(pf.size + p_position);
  228. }
  229. size_t FileAccessPack::get_position() const {
  230. return pos;
  231. }
  232. size_t FileAccessPack::get_len() const {
  233. return pf.size;
  234. }
  235. bool FileAccessPack::eof_reached() const {
  236. return eof;
  237. }
  238. uint8_t FileAccessPack::get_8() const {
  239. if (pos >= pf.size) {
  240. eof = true;
  241. return 0;
  242. }
  243. pos++;
  244. return f->get_8();
  245. }
  246. int FileAccessPack::get_buffer(uint8_t *p_dst, int p_length) const {
  247. ERR_FAIL_COND_V(!p_dst, -1);
  248. ERR_FAIL_COND_V(p_length < 0, -1);
  249. if (eof) {
  250. return 0;
  251. }
  252. uint64_t to_read = p_length;
  253. if (to_read + pos > pf.size) {
  254. eof = true;
  255. to_read = int64_t(pf.size) - int64_t(pos);
  256. }
  257. pos += p_length;
  258. if (to_read <= 0) {
  259. return 0;
  260. }
  261. f->get_buffer(p_dst, to_read);
  262. return to_read;
  263. }
  264. void FileAccessPack::set_endian_swap(bool p_swap) {
  265. FileAccess::set_endian_swap(p_swap);
  266. f->set_endian_swap(p_swap);
  267. }
  268. Error FileAccessPack::get_error() const {
  269. if (eof) {
  270. return ERR_FILE_EOF;
  271. }
  272. return OK;
  273. }
  274. void FileAccessPack::flush() {
  275. ERR_FAIL();
  276. }
  277. void FileAccessPack::store_8(uint8_t p_dest) {
  278. ERR_FAIL();
  279. }
  280. void FileAccessPack::store_buffer(const uint8_t *p_src, int p_length) {
  281. ERR_FAIL();
  282. }
  283. bool FileAccessPack::file_exists(const String &p_name) {
  284. return false;
  285. }
  286. FileAccessPack::FileAccessPack(const String &p_path, const PackedData::PackedFile &p_file) :
  287. pf(p_file),
  288. f(FileAccess::open(pf.pack, FileAccess::READ)) {
  289. ERR_FAIL_COND_MSG(!f, "Can't open pack-referenced file '" + String(pf.pack) + "'.");
  290. f->seek(pf.offset);
  291. off = pf.offset;
  292. if (pf.encrypted) {
  293. FileAccessEncrypted *fae = memnew(FileAccessEncrypted);
  294. if (!fae) {
  295. ERR_FAIL_MSG("Can't open encrypted pack-referenced file '" + String(pf.pack) + "'.");
  296. }
  297. Vector<uint8_t> key;
  298. key.resize(32);
  299. for (int i = 0; i < key.size(); i++) {
  300. key.write[i] = script_encryption_key[i];
  301. }
  302. Error err = fae->open_and_parse(f, key, FileAccessEncrypted::MODE_READ, false);
  303. if (err) {
  304. memdelete(fae);
  305. ERR_FAIL_MSG("Can't open encrypted pack-referenced file '" + String(pf.pack) + "'.");
  306. }
  307. f = fae;
  308. off = 0;
  309. }
  310. pos = 0;
  311. eof = false;
  312. }
  313. FileAccessPack::~FileAccessPack() {
  314. if (f) {
  315. f->close();
  316. memdelete(f);
  317. }
  318. }
  319. //////////////////////////////////////////////////////////////////////////////////
  320. // DIR ACCESS
  321. //////////////////////////////////////////////////////////////////////////////////
  322. Error DirAccessPack::list_dir_begin() {
  323. list_dirs.clear();
  324. list_files.clear();
  325. for (Map<String, PackedData::PackedDir *>::Element *E = current->subdirs.front(); E; E = E->next()) {
  326. list_dirs.push_back(E->key());
  327. }
  328. for (Set<String>::Element *E = current->files.front(); E; E = E->next()) {
  329. list_files.push_back(E->get());
  330. }
  331. return OK;
  332. }
  333. String DirAccessPack::get_next() {
  334. if (list_dirs.size()) {
  335. cdir = true;
  336. String d = list_dirs.front()->get();
  337. list_dirs.pop_front();
  338. return d;
  339. } else if (list_files.size()) {
  340. cdir = false;
  341. String f = list_files.front()->get();
  342. list_files.pop_front();
  343. return f;
  344. } else {
  345. return String();
  346. }
  347. }
  348. bool DirAccessPack::current_is_dir() const {
  349. return cdir;
  350. }
  351. bool DirAccessPack::current_is_hidden() const {
  352. return false;
  353. }
  354. void DirAccessPack::list_dir_end() {
  355. list_dirs.clear();
  356. list_files.clear();
  357. }
  358. int DirAccessPack::get_drive_count() {
  359. return 0;
  360. }
  361. String DirAccessPack::get_drive(int p_drive) {
  362. return "";
  363. }
  364. PackedData::PackedDir *DirAccessPack::_find_dir(String p_dir) {
  365. String nd = p_dir.replace("\\", "/");
  366. // Special handling since simplify_path() will forbid it
  367. if (p_dir == "..") {
  368. return current->parent;
  369. }
  370. bool absolute = false;
  371. if (nd.begins_with("res://")) {
  372. nd = nd.replace_first("res://", "");
  373. absolute = true;
  374. }
  375. nd = nd.simplify_path();
  376. if (nd == "") {
  377. nd = ".";
  378. }
  379. if (nd.begins_with("/")) {
  380. nd = nd.replace_first("/", "");
  381. absolute = true;
  382. }
  383. Vector<String> paths = nd.split("/");
  384. PackedData::PackedDir *pd;
  385. if (absolute) {
  386. pd = PackedData::get_singleton()->root;
  387. } else {
  388. pd = current;
  389. }
  390. for (int i = 0; i < paths.size(); i++) {
  391. String p = paths[i];
  392. if (p == ".") {
  393. continue;
  394. } else if (p == "..") {
  395. if (pd->parent) {
  396. pd = pd->parent;
  397. }
  398. } else if (pd->subdirs.has(p)) {
  399. pd = pd->subdirs[p];
  400. } else {
  401. return nullptr;
  402. }
  403. }
  404. return pd;
  405. }
  406. Error DirAccessPack::change_dir(String p_dir) {
  407. PackedData::PackedDir *pd = _find_dir(p_dir);
  408. if (pd) {
  409. current = pd;
  410. return OK;
  411. } else {
  412. return ERR_INVALID_PARAMETER;
  413. }
  414. }
  415. String DirAccessPack::get_current_dir(bool p_include_drive) {
  416. PackedData::PackedDir *pd = current;
  417. String p = current->name;
  418. while (pd->parent) {
  419. pd = pd->parent;
  420. p = pd->name.plus_file(p);
  421. }
  422. return "res://" + p;
  423. }
  424. bool DirAccessPack::file_exists(String p_file) {
  425. p_file = fix_path(p_file);
  426. PackedData::PackedDir *pd = _find_dir(p_file.get_base_dir());
  427. if (!pd) {
  428. return false;
  429. }
  430. return pd->files.has(p_file.get_file());
  431. }
  432. bool DirAccessPack::dir_exists(String p_dir) {
  433. p_dir = fix_path(p_dir);
  434. return _find_dir(p_dir) != nullptr;
  435. }
  436. Error DirAccessPack::make_dir(String p_dir) {
  437. return ERR_UNAVAILABLE;
  438. }
  439. Error DirAccessPack::rename(String p_from, String p_to) {
  440. return ERR_UNAVAILABLE;
  441. }
  442. Error DirAccessPack::remove(String p_name) {
  443. return ERR_UNAVAILABLE;
  444. }
  445. size_t DirAccessPack::get_space_left() {
  446. return 0;
  447. }
  448. String DirAccessPack::get_filesystem_type() const {
  449. return "PCK";
  450. }
  451. DirAccessPack::DirAccessPack() {
  452. current = PackedData::get_singleton()->root;
  453. }