file_access_pack.cpp 11 KB

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