file_access_pack.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  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-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 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, uint64_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 &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) {
  44. PathMD5 pmd5(p_path.md5_buffer());
  45. bool exists = files.has(pmd5);
  46. PackedFile pf;
  47. pf.encrypted = p_encrypted;
  48. pf.pack = p_pkg_path;
  49. pf.offset = p_ofs;
  50. pf.size = p_size;
  51. for (int i = 0; i < 16; i++) {
  52. pf.md5[i] = p_md5[i];
  53. }
  54. pf.src = p_src;
  55. if (!exists || p_replace_files) {
  56. files[pmd5] = pf;
  57. }
  58. if (!exists) {
  59. //search for dir
  60. String p = p_path.replace_first("res://", "");
  61. PackedDir *cd = root;
  62. if (p.contains("/")) { //in a subdir
  63. Vector<String> ds = p.get_base_dir().split("/");
  64. for (int j = 0; j < ds.size(); j++) {
  65. if (!cd->subdirs.has(ds[j])) {
  66. PackedDir *pd = memnew(PackedDir);
  67. pd->name = ds[j];
  68. pd->parent = cd;
  69. cd->subdirs[pd->name] = pd;
  70. cd = pd;
  71. } else {
  72. cd = cd->subdirs[ds[j]];
  73. }
  74. }
  75. }
  76. String filename = p_path.get_file();
  77. // Don't add as a file if the path points to a directory
  78. if (!filename.is_empty()) {
  79. cd->files.insert(filename);
  80. }
  81. }
  82. }
  83. void PackedData::add_pack_source(PackSource *p_source) {
  84. if (p_source != nullptr) {
  85. sources.push_back(p_source);
  86. }
  87. }
  88. PackedData *PackedData::singleton = nullptr;
  89. PackedData::PackedData() {
  90. singleton = this;
  91. root = memnew(PackedDir);
  92. add_pack_source(memnew(PackedSourcePCK));
  93. }
  94. void PackedData::_free_packed_dirs(PackedDir *p_dir) {
  95. for (const KeyValue<String, PackedDir *> &E : p_dir->subdirs) {
  96. _free_packed_dirs(E.value);
  97. }
  98. memdelete(p_dir);
  99. }
  100. PackedData::~PackedData() {
  101. for (int i = 0; i < sources.size(); i++) {
  102. memdelete(sources[i]);
  103. }
  104. _free_packed_dirs(root);
  105. }
  106. //////////////////////////////////////////////////////////////////
  107. bool PackedSourcePCK::try_open_pack(const String &p_path, bool p_replace_files, uint64_t p_offset) {
  108. FileAccess *f = FileAccess::open(p_path, FileAccess::READ);
  109. if (!f) {
  110. return false;
  111. }
  112. f->seek(p_offset);
  113. uint32_t magic = f->get_32();
  114. if (magic != PACK_HEADER_MAGIC) {
  115. // loading with offset feature not supported for self contained exe files
  116. if (p_offset != 0) {
  117. f->close();
  118. memdelete(f);
  119. ERR_FAIL_V_MSG(false, "Loading self-contained executable with offset not supported.");
  120. }
  121. //maybe at the end.... self contained exe
  122. f->seek_end();
  123. f->seek(f->get_position() - 4);
  124. magic = f->get_32();
  125. if (magic != PACK_HEADER_MAGIC) {
  126. f->close();
  127. memdelete(f);
  128. return false;
  129. }
  130. f->seek(f->get_position() - 12);
  131. uint64_t ds = f->get_64();
  132. f->seek(f->get_position() - ds - 8);
  133. magic = f->get_32();
  134. if (magic != PACK_HEADER_MAGIC) {
  135. f->close();
  136. memdelete(f);
  137. return false;
  138. }
  139. }
  140. uint32_t version = f->get_32();
  141. uint32_t ver_major = f->get_32();
  142. uint32_t ver_minor = f->get_32();
  143. f->get_32(); // patch number, not used for validation.
  144. if (version != PACK_FORMAT_VERSION) {
  145. f->close();
  146. memdelete(f);
  147. ERR_FAIL_V_MSG(false, "Pack version unsupported: " + itos(version) + ".");
  148. }
  149. if (ver_major > VERSION_MAJOR || (ver_major == VERSION_MAJOR && ver_minor > VERSION_MINOR)) {
  150. f->close();
  151. memdelete(f);
  152. ERR_FAIL_V_MSG(false, "Pack created with a newer version of the engine: " + itos(ver_major) + "." + itos(ver_minor) + ".");
  153. }
  154. uint32_t pack_flags = f->get_32();
  155. uint64_t file_base = f->get_64();
  156. bool enc_directory = (pack_flags & PACK_DIR_ENCRYPTED);
  157. for (int i = 0; i < 16; i++) {
  158. //reserved
  159. f->get_32();
  160. }
  161. int file_count = f->get_32();
  162. if (enc_directory) {
  163. FileAccessEncrypted *fae = memnew(FileAccessEncrypted);
  164. if (!fae) {
  165. f->close();
  166. memdelete(f);
  167. ERR_FAIL_V_MSG(false, "Can't open encrypted pack directory.");
  168. }
  169. Vector<uint8_t> key;
  170. key.resize(32);
  171. for (int i = 0; i < key.size(); i++) {
  172. key.write[i] = script_encryption_key[i];
  173. }
  174. Error err = fae->open_and_parse(f, key, FileAccessEncrypted::MODE_READ, false);
  175. if (err) {
  176. f->close();
  177. memdelete(f);
  178. memdelete(fae);
  179. ERR_FAIL_V_MSG(false, "Can't open encrypted pack directory.");
  180. }
  181. f = fae;
  182. }
  183. for (int i = 0; i < file_count; i++) {
  184. uint32_t sl = f->get_32();
  185. CharString cs;
  186. cs.resize(sl + 1);
  187. f->get_buffer((uint8_t *)cs.ptr(), sl);
  188. cs[sl] = 0;
  189. String path;
  190. path.parse_utf8(cs.ptr());
  191. uint64_t ofs = file_base + f->get_64();
  192. uint64_t size = f->get_64();
  193. uint8_t md5[16];
  194. f->get_buffer(md5, 16);
  195. uint32_t flags = f->get_32();
  196. PackedData::get_singleton()->add_path(p_path, path, ofs + p_offset, size, md5, this, p_replace_files, (flags & PACK_FILE_ENCRYPTED));
  197. }
  198. f->close();
  199. memdelete(f);
  200. return true;
  201. }
  202. FileAccess *PackedSourcePCK::get_file(const String &p_path, PackedData::PackedFile *p_file) {
  203. return memnew(FileAccessPack(p_path, *p_file));
  204. }
  205. //////////////////////////////////////////////////////////////////
  206. Error FileAccessPack::_open(const String &p_path, int p_mode_flags) {
  207. ERR_FAIL_V(ERR_UNAVAILABLE);
  208. return ERR_UNAVAILABLE;
  209. }
  210. void FileAccessPack::close() {
  211. f->close();
  212. }
  213. bool FileAccessPack::is_open() const {
  214. return f->is_open();
  215. }
  216. void FileAccessPack::seek(uint64_t p_position) {
  217. if (p_position > pf.size) {
  218. eof = true;
  219. } else {
  220. eof = false;
  221. }
  222. f->seek(off + p_position);
  223. pos = p_position;
  224. }
  225. void FileAccessPack::seek_end(int64_t p_position) {
  226. seek(pf.size + p_position);
  227. }
  228. uint64_t FileAccessPack::get_position() const {
  229. return pos;
  230. }
  231. uint64_t FileAccessPack::get_length() const {
  232. return pf.size;
  233. }
  234. bool FileAccessPack::eof_reached() const {
  235. return eof;
  236. }
  237. uint8_t FileAccessPack::get_8() const {
  238. if (pos >= pf.size) {
  239. eof = true;
  240. return 0;
  241. }
  242. pos++;
  243. return f->get_8();
  244. }
  245. uint64_t FileAccessPack::get_buffer(uint8_t *p_dst, uint64_t p_length) const {
  246. ERR_FAIL_COND_V(!p_dst && p_length > 0, -1);
  247. if (eof) {
  248. return 0;
  249. }
  250. int64_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_big_endian(bool p_big_endian) {
  263. FileAccess::set_big_endian(p_big_endian);
  264. f->set_big_endian(p_big_endian);
  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, uint64_t 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 (const KeyValue<String, PackedData::PackedDir *> &E : current->subdirs) {
  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.is_empty()) {
  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. uint64_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. }