file_access_pack.cpp 12 KB

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