file_access_pack.cpp 13 KB

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