file_access_pack.cpp 11 KB

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