file_access_pack.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  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. if (eof) {
  248. return 0;
  249. }
  250. uint64_t to_read = p_length;
  251. if (to_read + pos > pf.size) {
  252. eof = true;
  253. to_read = int64_t(pf.size) - int64_t(pos);
  254. }
  255. pos += p_length;
  256. if (to_read <= 0) {
  257. return 0;
  258. }
  259. f->get_buffer(p_dst, to_read);
  260. return to_read;
  261. }
  262. void FileAccessPack::set_endian_swap(bool p_swap) {
  263. FileAccess::set_endian_swap(p_swap);
  264. f->set_endian_swap(p_swap);
  265. }
  266. Error FileAccessPack::get_error() const {
  267. if (eof) {
  268. return ERR_FILE_EOF;
  269. }
  270. return OK;
  271. }
  272. void FileAccessPack::flush() {
  273. ERR_FAIL();
  274. }
  275. void FileAccessPack::store_8(uint8_t p_dest) {
  276. ERR_FAIL();
  277. }
  278. void FileAccessPack::store_buffer(const uint8_t *p_src, int p_length) {
  279. ERR_FAIL();
  280. }
  281. bool FileAccessPack::file_exists(const String &p_name) {
  282. return false;
  283. }
  284. FileAccessPack::FileAccessPack(const String &p_path, const PackedData::PackedFile &p_file) :
  285. pf(p_file),
  286. f(FileAccess::open(pf.pack, FileAccess::READ)) {
  287. ERR_FAIL_COND_MSG(!f, "Can't open pack-referenced file '" + String(pf.pack) + "'.");
  288. f->seek(pf.offset);
  289. off = pf.offset;
  290. if (pf.encrypted) {
  291. FileAccessEncrypted *fae = memnew(FileAccessEncrypted);
  292. if (!fae) {
  293. ERR_FAIL_MSG("Can't open encrypted pack-referenced file '" + String(pf.pack) + "'.");
  294. }
  295. Vector<uint8_t> key;
  296. key.resize(32);
  297. for (int i = 0; i < key.size(); i++) {
  298. key.write[i] = script_encryption_key[i];
  299. }
  300. Error err = fae->open_and_parse(f, key, FileAccessEncrypted::MODE_READ, false);
  301. if (err) {
  302. memdelete(fae);
  303. ERR_FAIL_MSG("Can't open encrypted pack-referenced file '" + String(pf.pack) + "'.");
  304. }
  305. f = fae;
  306. off = 0;
  307. }
  308. pos = 0;
  309. eof = false;
  310. }
  311. FileAccessPack::~FileAccessPack() {
  312. if (f) {
  313. f->close();
  314. memdelete(f);
  315. }
  316. }
  317. //////////////////////////////////////////////////////////////////////////////////
  318. // DIR ACCESS
  319. //////////////////////////////////////////////////////////////////////////////////
  320. Error DirAccessPack::list_dir_begin() {
  321. list_dirs.clear();
  322. list_files.clear();
  323. for (Map<String, PackedData::PackedDir *>::Element *E = current->subdirs.front(); E; E = E->next()) {
  324. list_dirs.push_back(E->key());
  325. }
  326. for (Set<String>::Element *E = current->files.front(); E; E = E->next()) {
  327. list_files.push_back(E->get());
  328. }
  329. return OK;
  330. }
  331. String DirAccessPack::get_next() {
  332. if (list_dirs.size()) {
  333. cdir = true;
  334. String d = list_dirs.front()->get();
  335. list_dirs.pop_front();
  336. return d;
  337. } else if (list_files.size()) {
  338. cdir = false;
  339. String f = list_files.front()->get();
  340. list_files.pop_front();
  341. return f;
  342. } else {
  343. return String();
  344. }
  345. }
  346. bool DirAccessPack::current_is_dir() const {
  347. return cdir;
  348. }
  349. bool DirAccessPack::current_is_hidden() const {
  350. return false;
  351. }
  352. void DirAccessPack::list_dir_end() {
  353. list_dirs.clear();
  354. list_files.clear();
  355. }
  356. int DirAccessPack::get_drive_count() {
  357. return 0;
  358. }
  359. String DirAccessPack::get_drive(int p_drive) {
  360. return "";
  361. }
  362. PackedData::PackedDir *DirAccessPack::_find_dir(String p_dir) {
  363. String nd = p_dir.replace("\\", "/");
  364. // Special handling since simplify_path() will forbid it
  365. if (p_dir == "..") {
  366. return current->parent;
  367. }
  368. bool absolute = false;
  369. if (nd.begins_with("res://")) {
  370. nd = nd.replace_first("res://", "");
  371. absolute = true;
  372. }
  373. nd = nd.simplify_path();
  374. if (nd == "") {
  375. nd = ".";
  376. }
  377. if (nd.begins_with("/")) {
  378. nd = nd.replace_first("/", "");
  379. absolute = true;
  380. }
  381. Vector<String> paths = nd.split("/");
  382. PackedData::PackedDir *pd;
  383. if (absolute) {
  384. pd = PackedData::get_singleton()->root;
  385. } else {
  386. pd = current;
  387. }
  388. for (int i = 0; i < paths.size(); i++) {
  389. String p = paths[i];
  390. if (p == ".") {
  391. continue;
  392. } else if (p == "..") {
  393. if (pd->parent) {
  394. pd = pd->parent;
  395. }
  396. } else if (pd->subdirs.has(p)) {
  397. pd = pd->subdirs[p];
  398. } else {
  399. return nullptr;
  400. }
  401. }
  402. return pd;
  403. }
  404. Error DirAccessPack::change_dir(String p_dir) {
  405. PackedData::PackedDir *pd = _find_dir(p_dir);
  406. if (pd) {
  407. current = pd;
  408. return OK;
  409. } else {
  410. return ERR_INVALID_PARAMETER;
  411. }
  412. }
  413. String DirAccessPack::get_current_dir(bool p_include_drive) {
  414. PackedData::PackedDir *pd = current;
  415. String p = current->name;
  416. while (pd->parent) {
  417. pd = pd->parent;
  418. p = pd->name.plus_file(p);
  419. }
  420. return "res://" + p;
  421. }
  422. bool DirAccessPack::file_exists(String p_file) {
  423. p_file = fix_path(p_file);
  424. PackedData::PackedDir *pd = _find_dir(p_file.get_base_dir());
  425. if (!pd) {
  426. return false;
  427. }
  428. return pd->files.has(p_file.get_file());
  429. }
  430. bool DirAccessPack::dir_exists(String p_dir) {
  431. p_dir = fix_path(p_dir);
  432. return _find_dir(p_dir) != nullptr;
  433. }
  434. Error DirAccessPack::make_dir(String p_dir) {
  435. return ERR_UNAVAILABLE;
  436. }
  437. Error DirAccessPack::rename(String p_from, String p_to) {
  438. return ERR_UNAVAILABLE;
  439. }
  440. Error DirAccessPack::remove(String p_name) {
  441. return ERR_UNAVAILABLE;
  442. }
  443. size_t DirAccessPack::get_space_left() {
  444. return 0;
  445. }
  446. String DirAccessPack::get_filesystem_type() const {
  447. return "PCK";
  448. }
  449. DirAccessPack::DirAccessPack() {
  450. current = PackedData::get_singleton()->root;
  451. }