file_access_pack.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  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, size_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 &pkg_path, const String &path, uint64_t ofs, uint64_t size, const uint8_t *p_md5, PackSource *p_src, bool p_replace_files) {
  42. PathMD5 pmd5(path.md5_buffer());
  43. //printf("adding path %ls, %lli, %lli\n", path.c_str(), pmd5.a, pmd5.b);
  44. bool exists = files.has(pmd5);
  45. PackedFile pf;
  46. pf.pack = pkg_path;
  47. pf.offset = ofs;
  48. pf.size = 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 = 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 = 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, size_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. for (int i = 0; i < 16; i++) {
  155. //reserved
  156. f->get_32();
  157. }
  158. int file_count = f->get_32();
  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 = f->get_64();
  168. uint64_t size = f->get_64();
  169. uint8_t md5[16];
  170. f->get_buffer(md5, 16);
  171. PackedData::get_singleton()->add_path(p_path, path, ofs + p_offset, size, md5, this, p_replace_files);
  172. };
  173. f->close();
  174. memdelete(f);
  175. return true;
  176. };
  177. FileAccess *PackedSourcePCK::get_file(const String &p_path, PackedData::PackedFile *p_file) {
  178. return memnew(FileAccessPack(p_path, *p_file));
  179. };
  180. //////////////////////////////////////////////////////////////////
  181. Error FileAccessPack::_open(const String &p_path, int p_mode_flags) {
  182. ERR_FAIL_V(ERR_UNAVAILABLE);
  183. return ERR_UNAVAILABLE;
  184. }
  185. void FileAccessPack::close() {
  186. f->close();
  187. }
  188. bool FileAccessPack::is_open() const {
  189. return f->is_open();
  190. }
  191. void FileAccessPack::seek(size_t p_position) {
  192. if (p_position > pf.size) {
  193. eof = true;
  194. } else {
  195. eof = false;
  196. }
  197. f->seek(pf.offset + p_position);
  198. pos = p_position;
  199. }
  200. void FileAccessPack::seek_end(int64_t p_position) {
  201. seek(pf.size + p_position);
  202. }
  203. size_t FileAccessPack::get_position() const {
  204. return pos;
  205. }
  206. size_t FileAccessPack::get_len() const {
  207. return pf.size;
  208. }
  209. bool FileAccessPack::eof_reached() const {
  210. return eof;
  211. }
  212. uint8_t FileAccessPack::get_8() const {
  213. if (pos >= pf.size) {
  214. eof = true;
  215. return 0;
  216. }
  217. pos++;
  218. return f->get_8();
  219. }
  220. int FileAccessPack::get_buffer(uint8_t *p_dst, int p_length) const {
  221. ERR_FAIL_COND_V(!p_dst && p_length > 0, -1);
  222. ERR_FAIL_COND_V(p_length < 0, -1);
  223. if (eof) {
  224. return 0;
  225. }
  226. uint64_t to_read = p_length;
  227. if (to_read + pos > pf.size) {
  228. eof = true;
  229. to_read = int64_t(pf.size) - int64_t(pos);
  230. }
  231. pos += p_length;
  232. if (to_read <= 0) {
  233. return 0;
  234. }
  235. f->get_buffer(p_dst, to_read);
  236. return to_read;
  237. }
  238. void FileAccessPack::set_endian_swap(bool p_swap) {
  239. FileAccess::set_endian_swap(p_swap);
  240. f->set_endian_swap(p_swap);
  241. }
  242. Error FileAccessPack::get_error() const {
  243. if (eof) {
  244. return ERR_FILE_EOF;
  245. }
  246. return OK;
  247. }
  248. void FileAccessPack::flush() {
  249. ERR_FAIL();
  250. }
  251. void FileAccessPack::store_8(uint8_t p_dest) {
  252. ERR_FAIL();
  253. }
  254. void FileAccessPack::store_buffer(const uint8_t *p_src, int p_length) {
  255. ERR_FAIL();
  256. }
  257. bool FileAccessPack::file_exists(const String &p_name) {
  258. return false;
  259. }
  260. FileAccessPack::FileAccessPack(const String &p_path, const PackedData::PackedFile &p_file) :
  261. pf(p_file),
  262. f(FileAccess::open(pf.pack, FileAccess::READ)) {
  263. ERR_FAIL_COND_MSG(!f, "Can't open pack-referenced file '" + String(pf.pack) + "'.");
  264. f->seek(pf.offset);
  265. pos = 0;
  266. eof = false;
  267. }
  268. FileAccessPack::~FileAccessPack() {
  269. if (f) {
  270. memdelete(f);
  271. }
  272. }
  273. //////////////////////////////////////////////////////////////////////////////////
  274. // DIR ACCESS
  275. //////////////////////////////////////////////////////////////////////////////////
  276. Error DirAccessPack::list_dir_begin() {
  277. list_dirs.clear();
  278. list_files.clear();
  279. for (Map<String, PackedData::PackedDir *>::Element *E = current->subdirs.front(); E; E = E->next()) {
  280. list_dirs.push_back(E->key());
  281. }
  282. for (Set<String>::Element *E = current->files.front(); E; E = E->next()) {
  283. list_files.push_back(E->get());
  284. }
  285. return OK;
  286. }
  287. String DirAccessPack::get_next() {
  288. if (list_dirs.size()) {
  289. cdir = true;
  290. String d = list_dirs.front()->get();
  291. list_dirs.pop_front();
  292. return d;
  293. } else if (list_files.size()) {
  294. cdir = false;
  295. String f = list_files.front()->get();
  296. list_files.pop_front();
  297. return f;
  298. } else {
  299. return String();
  300. }
  301. }
  302. bool DirAccessPack::current_is_dir() const {
  303. return cdir;
  304. }
  305. bool DirAccessPack::current_is_hidden() const {
  306. return false;
  307. }
  308. void DirAccessPack::list_dir_end() {
  309. list_dirs.clear();
  310. list_files.clear();
  311. }
  312. int DirAccessPack::get_drive_count() {
  313. return 0;
  314. }
  315. String DirAccessPack::get_drive(int p_drive) {
  316. return "";
  317. }
  318. PackedData::PackedDir *DirAccessPack::_find_dir(String p_dir) {
  319. String nd = p_dir.replace("\\", "/");
  320. // Special handling since simplify_path() will forbid it
  321. if (p_dir == "..") {
  322. return current->parent;
  323. }
  324. bool absolute = false;
  325. if (nd.begins_with("res://")) {
  326. nd = nd.replace_first("res://", "");
  327. absolute = true;
  328. }
  329. nd = nd.simplify_path();
  330. if (nd == "") {
  331. nd = ".";
  332. }
  333. if (nd.begins_with("/")) {
  334. nd = nd.replace_first("/", "");
  335. absolute = true;
  336. }
  337. Vector<String> paths = nd.split("/");
  338. PackedData::PackedDir *pd;
  339. if (absolute) {
  340. pd = PackedData::get_singleton()->root;
  341. } else {
  342. pd = current;
  343. }
  344. for (int i = 0; i < paths.size(); i++) {
  345. String p = paths[i];
  346. if (p == ".") {
  347. continue;
  348. } else if (p == "..") {
  349. if (pd->parent) {
  350. pd = pd->parent;
  351. }
  352. } else if (pd->subdirs.has(p)) {
  353. pd = pd->subdirs[p];
  354. } else {
  355. return nullptr;
  356. }
  357. }
  358. return pd;
  359. }
  360. Error DirAccessPack::change_dir(String p_dir) {
  361. PackedData::PackedDir *pd = _find_dir(p_dir);
  362. if (pd) {
  363. current = pd;
  364. return OK;
  365. } else {
  366. return ERR_INVALID_PARAMETER;
  367. }
  368. }
  369. String DirAccessPack::get_current_dir() {
  370. PackedData::PackedDir *pd = current;
  371. String p = current->name;
  372. while (pd->parent) {
  373. pd = pd->parent;
  374. p = pd->name.plus_file(p);
  375. }
  376. return "res://" + p;
  377. }
  378. bool DirAccessPack::file_exists(String p_file) {
  379. p_file = fix_path(p_file);
  380. PackedData::PackedDir *pd = _find_dir(p_file.get_base_dir());
  381. if (!pd) {
  382. return false;
  383. }
  384. return pd->files.has(p_file.get_file());
  385. }
  386. bool DirAccessPack::dir_exists(String p_dir) {
  387. p_dir = fix_path(p_dir);
  388. return _find_dir(p_dir) != nullptr;
  389. }
  390. Error DirAccessPack::make_dir(String p_dir) {
  391. return ERR_UNAVAILABLE;
  392. }
  393. Error DirAccessPack::rename(String p_from, String p_to) {
  394. return ERR_UNAVAILABLE;
  395. }
  396. Error DirAccessPack::remove(String p_name) {
  397. return ERR_UNAVAILABLE;
  398. }
  399. size_t DirAccessPack::get_space_left() {
  400. return 0;
  401. }
  402. String DirAccessPack::get_filesystem_type() const {
  403. return "PCK";
  404. }
  405. DirAccessPack::DirAccessPack() {
  406. current = PackedData::get_singleton()->root;
  407. cdir = false;
  408. }
  409. DirAccessPack::~DirAccessPack() {
  410. }