file_access_zip.cpp 9.4 KB

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