file_access_zip.cpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. /*************************************************************************/
  2. /* file_access_zip.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. #ifdef MINIZIP_ENABLED
  31. #include "file_access_zip.h"
  32. #include "core/os/file_access.h"
  33. ZipArchive *ZipArchive::instance = NULL;
  34. extern "C" {
  35. static void *godot_open(void *data, const char *p_fname, int mode) {
  36. if (mode & ZLIB_FILEFUNC_MODE_WRITE) {
  37. return NULL;
  38. }
  39. FileAccess *f = (FileAccess *)data;
  40. f->open(p_fname, FileAccess::READ);
  41. return f->is_open() ? data : NULL;
  42. }
  43. static uLong godot_read(void *data, void *fdata, void *buf, uLong size) {
  44. FileAccess *f = (FileAccess *)data;
  45. f->get_buffer((uint8_t *)buf, size);
  46. return size;
  47. }
  48. static uLong godot_write(voidpf opaque, voidpf stream, const void *buf, uLong size) {
  49. return 0;
  50. }
  51. static long godot_tell(voidpf opaque, voidpf stream) {
  52. FileAccess *f = (FileAccess *)opaque;
  53. return f->get_position();
  54. }
  55. static long godot_seek(voidpf opaque, voidpf stream, uLong offset, int origin) {
  56. FileAccess *f = (FileAccess *)opaque;
  57. int pos = offset;
  58. switch (origin) {
  59. case ZLIB_FILEFUNC_SEEK_CUR:
  60. pos = f->get_position() + offset;
  61. break;
  62. case ZLIB_FILEFUNC_SEEK_END:
  63. pos = f->get_len() + offset;
  64. break;
  65. default:
  66. break;
  67. }
  68. f->seek(pos);
  69. return 0;
  70. }
  71. static int godot_close(voidpf opaque, voidpf stream) {
  72. FileAccess *f = (FileAccess *)opaque;
  73. f->close();
  74. return 0;
  75. }
  76. static int godot_testerror(voidpf opaque, voidpf stream) {
  77. FileAccess *f = (FileAccess *)opaque;
  78. return f->get_error() != OK ? 1 : 0;
  79. }
  80. static voidpf godot_alloc(voidpf opaque, uInt items, uInt size) {
  81. return memalloc(items * size);
  82. }
  83. static void godot_free(voidpf opaque, voidpf address) {
  84. memfree(address);
  85. }
  86. } // extern "C"
  87. void ZipArchive::close_handle(unzFile p_file) const {
  88. ERR_FAIL_COND_MSG(!p_file, "Cannot close a file if none is open.");
  89. FileAccess *f = (FileAccess *)unzGetOpaque(p_file);
  90. unzCloseCurrentFile(p_file);
  91. unzClose(p_file);
  92. memdelete(f);
  93. }
  94. unzFile ZipArchive::get_file_handle(String p_file) const {
  95. ERR_FAIL_COND_V_MSG(!file_exists(p_file), NULL, "File '" + p_file + " doesn't exist.");
  96. File file = files[p_file];
  97. FileAccess *f = FileAccess::open(packages[file.package].filename, FileAccess::READ);
  98. ERR_FAIL_COND_V_MSG(!f, NULL, "Cannot open file '" + packages[file.package].filename + "'.");
  99. zlib_filefunc_def io;
  100. memset(&io, 0, sizeof(io));
  101. io.opaque = f;
  102. io.zopen_file = godot_open;
  103. io.zread_file = godot_read;
  104. io.zwrite_file = godot_write;
  105. io.ztell_file = godot_tell;
  106. io.zseek_file = godot_seek;
  107. io.zclose_file = godot_close;
  108. io.zerror_file = godot_testerror;
  109. io.alloc_mem = godot_alloc;
  110. io.free_mem = godot_free;
  111. unzFile pkg = unzOpen2(packages[file.package].filename.utf8().get_data(), &io);
  112. ERR_FAIL_COND_V(!pkg, NULL);
  113. int unz_err = unzGoToFilePos(pkg, &file.file_pos);
  114. if (unz_err != UNZ_OK || unzOpenCurrentFile(pkg) != UNZ_OK) {
  115. unzClose(pkg);
  116. ERR_FAIL_V(NULL);
  117. }
  118. return pkg;
  119. }
  120. bool ZipArchive::try_open_pack(const String &p_path, bool p_replace_files, size_t p_offset = 0) {
  121. //printf("opening zip pack %ls, %i, %i\n", p_name.c_str(), p_name.extension().nocasecmp_to("zip"), p_name.extension().nocasecmp_to("pcz"));
  122. // load with offset feature only supported for PCK files
  123. ERR_FAIL_COND_V_MSG(p_offset != 0, false, "Invalid PCK data. Note that loading files with a non-zero offset isn't supported with ZIP archives.");
  124. if (p_path.get_extension().nocasecmp_to("zip") != 0 && p_path.get_extension().nocasecmp_to("pcz") != 0)
  125. return false;
  126. zlib_filefunc_def io;
  127. FileAccess *fa = FileAccess::open(p_path, FileAccess::READ);
  128. if (!fa)
  129. return false;
  130. io.opaque = fa;
  131. io.zopen_file = godot_open;
  132. io.zread_file = godot_read;
  133. io.zwrite_file = godot_write;
  134. io.ztell_file = godot_tell;
  135. io.zseek_file = godot_seek;
  136. io.zclose_file = godot_close;
  137. io.zerror_file = godot_testerror;
  138. unzFile zfile = unzOpen2(p_path.utf8().get_data(), &io);
  139. ERR_FAIL_COND_V(!zfile, false);
  140. unz_global_info64 gi;
  141. int err = unzGetGlobalInfo64(zfile, &gi);
  142. ERR_FAIL_COND_V(err != UNZ_OK, false);
  143. Package pkg;
  144. pkg.filename = p_path;
  145. pkg.zfile = zfile;
  146. packages.push_back(pkg);
  147. int pkg_num = packages.size() - 1;
  148. for (uint64_t i = 0; i < gi.number_entry; i++) {
  149. char filename_inzip[256];
  150. unz_file_info64 file_info;
  151. err = unzGetCurrentFileInfo64(zfile, &file_info, filename_inzip, sizeof(filename_inzip), NULL, 0, NULL, 0);
  152. ERR_CONTINUE(err != UNZ_OK);
  153. File f;
  154. f.package = pkg_num;
  155. unzGetFilePos(zfile, &f.file_pos);
  156. String fname = String("res://") + filename_inzip;
  157. files[fname] = f;
  158. uint8_t md5[16] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
  159. PackedData::get_singleton()->add_path(p_path, fname, 1, 0, md5, this, p_replace_files);
  160. //printf("packed data add path %ls, %ls\n", p_name.c_str(), fname.c_str());
  161. if ((i + 1) < gi.number_entry) {
  162. unzGoToNextFile(zfile);
  163. }
  164. }
  165. return true;
  166. }
  167. bool ZipArchive::file_exists(String p_name) const {
  168. return files.has(p_name);
  169. }
  170. FileAccess *ZipArchive::get_file(const String &p_path, PackedData::PackedFile *p_file) {
  171. return memnew(FileAccessZip(p_path, *p_file));
  172. }
  173. ZipArchive *ZipArchive::get_singleton() {
  174. if (instance == NULL) {
  175. instance = memnew(ZipArchive);
  176. }
  177. return instance;
  178. }
  179. ZipArchive::ZipArchive() {
  180. instance = this;
  181. //fa_create_func = FileAccess::get_create_func();
  182. }
  183. ZipArchive::~ZipArchive() {
  184. for (int i = 0; i < packages.size(); i++) {
  185. FileAccess *f = (FileAccess *)unzGetOpaque(packages[i].zfile);
  186. unzClose(packages[i].zfile);
  187. memdelete(f);
  188. }
  189. packages.clear();
  190. }
  191. Error FileAccessZip::_open(const String &p_path, int p_mode_flags) {
  192. close();
  193. ERR_FAIL_COND_V(p_mode_flags & FileAccess::WRITE, FAILED);
  194. ZipArchive *arch = ZipArchive::get_singleton();
  195. ERR_FAIL_COND_V(!arch, FAILED);
  196. zfile = arch->get_file_handle(p_path);
  197. ERR_FAIL_COND_V(!zfile, FAILED);
  198. int err = unzGetCurrentFileInfo64(zfile, &file_info, NULL, 0, NULL, 0, NULL, 0);
  199. ERR_FAIL_COND_V(err != UNZ_OK, FAILED);
  200. return OK;
  201. }
  202. void FileAccessZip::close() {
  203. if (!zfile)
  204. return;
  205. ZipArchive *arch = ZipArchive::get_singleton();
  206. ERR_FAIL_COND(!arch);
  207. arch->close_handle(zfile);
  208. zfile = NULL;
  209. }
  210. bool FileAccessZip::is_open() const {
  211. return zfile != NULL;
  212. }
  213. void FileAccessZip::seek(size_t p_position) {
  214. ERR_FAIL_COND(!zfile);
  215. unzSeekCurrentFile(zfile, p_position);
  216. }
  217. void FileAccessZip::seek_end(int64_t p_position) {
  218. ERR_FAIL_COND(!zfile);
  219. unzSeekCurrentFile(zfile, get_len() + p_position);
  220. }
  221. size_t FileAccessZip::get_position() const {
  222. ERR_FAIL_COND_V(!zfile, 0);
  223. return unztell(zfile);
  224. }
  225. size_t FileAccessZip::get_len() const {
  226. ERR_FAIL_COND_V(!zfile, 0);
  227. return file_info.uncompressed_size;
  228. }
  229. bool FileAccessZip::eof_reached() const {
  230. ERR_FAIL_COND_V(!zfile, true);
  231. return at_eof;
  232. }
  233. uint8_t FileAccessZip::get_8() const {
  234. uint8_t ret = 0;
  235. get_buffer(&ret, 1);
  236. return ret;
  237. }
  238. int FileAccessZip::get_buffer(uint8_t *p_dst, int p_length) const {
  239. ERR_FAIL_COND_V(!p_dst && p_length > 0, -1);
  240. ERR_FAIL_COND_V(p_length < 0, -1);
  241. ERR_FAIL_COND_V(!zfile, -1);
  242. at_eof = unzeof(zfile);
  243. if (at_eof)
  244. return 0;
  245. int read = unzReadCurrentFile(zfile, p_dst, p_length);
  246. ERR_FAIL_COND_V(read < 0, read);
  247. if (read < p_length)
  248. at_eof = true;
  249. return read;
  250. }
  251. Error FileAccessZip::get_error() const {
  252. if (!zfile) {
  253. return ERR_UNCONFIGURED;
  254. }
  255. if (eof_reached()) {
  256. return ERR_FILE_EOF;
  257. }
  258. return OK;
  259. }
  260. void FileAccessZip::flush() {
  261. ERR_FAIL();
  262. }
  263. void FileAccessZip::store_8(uint8_t p_dest) {
  264. ERR_FAIL();
  265. }
  266. bool FileAccessZip::file_exists(const String &p_name) {
  267. return false;
  268. }
  269. FileAccessZip::FileAccessZip(const String &p_path, const PackedData::PackedFile &p_file) :
  270. zfile(NULL) {
  271. _open(p_path, FileAccess::READ);
  272. }
  273. FileAccessZip::~FileAccessZip() {
  274. close();
  275. }
  276. #endif