file_access_pack.cpp 13 KB

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