file_access_pack.cpp 11 KB

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