file_access_zip.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. /*************************************************************************/
  2. /* file_access_zip.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #ifdef MINIZIP_ENABLED
  30. #include "file_access_zip.h"
  31. #include "core/os/file_access.h"
  32. ZipArchive* ZipArchive::instance = NULL;
  33. extern "C" {
  34. static void* godot_open(void* data, const char* p_fname, int mode) {
  35. if (mode & ZLIB_FILEFUNC_MODE_WRITE) {
  36. return NULL;
  37. };
  38. FileAccess* f = (FileAccess*)data;
  39. f->open(p_fname, FileAccess::READ);
  40. return f->is_open()?data:NULL;
  41. };
  42. static uLong godot_read(void* data, void* fdata, void* buf, uLong size) {
  43. FileAccess* f = (FileAccess*)data;
  44. f->get_buffer((uint8_t*)buf, size);
  45. return size;
  46. };
  47. static uLong godot_write(voidpf opaque, voidpf stream, const void* buf, uLong size) {
  48. return 0;
  49. };
  50. static long godot_tell (voidpf opaque, voidpf stream) {
  51. FileAccess* f = (FileAccess*)opaque;
  52. return f->get_pos();
  53. };
  54. static long godot_seek(voidpf opaque, voidpf stream, uLong offset, int origin) {
  55. FileAccess* f = (FileAccess*)opaque;
  56. int pos = offset;
  57. switch (origin) {
  58. case ZLIB_FILEFUNC_SEEK_CUR:
  59. pos = f->get_pos() + offset;
  60. break;
  61. case ZLIB_FILEFUNC_SEEK_END:
  62. pos = f->get_len() + offset;
  63. break;
  64. default:
  65. break;
  66. };
  67. f->seek(pos);
  68. return 0;
  69. };
  70. static int godot_close(voidpf opaque, voidpf stream) {
  71. FileAccess* f = (FileAccess*)opaque;
  72. f->close();
  73. return 0;
  74. };
  75. static int godot_testerror(voidpf opaque, voidpf stream) {
  76. FileAccess* f = (FileAccess*)opaque;
  77. return f->get_error()!=OK?1:0;
  78. };
  79. };
  80. void ZipArchive::close_handle(unzFile p_file) const {
  81. ERR_FAIL_COND(!p_file);
  82. FileAccess* f = (FileAccess*)unzGetOpaque(p_file);
  83. unzCloseCurrentFile(p_file);
  84. unzClose(p_file);
  85. memdelete(f);
  86. };
  87. unzFile ZipArchive::get_file_handle(String p_file) const {
  88. ERR_FAIL_COND_V(!file_exists(p_file), NULL);
  89. File file = files[p_file];
  90. FileAccess* f = FileAccess::open(packages[file.package].filename, FileAccess::READ);
  91. ERR_FAIL_COND_V(!f, NULL);
  92. zlib_filefunc_def io;
  93. io.opaque = f;
  94. io.zopen_file = godot_open;
  95. io.zread_file = godot_read;
  96. io.zwrite_file = godot_write;
  97. io.ztell_file = godot_tell;
  98. io.zseek_file = godot_seek;
  99. io.zclose_file = godot_close;
  100. io.zerror_file = godot_testerror;
  101. unzFile pkg = unzOpen2(packages[file.package].filename.utf8().get_data(), &io);
  102. ERR_FAIL_COND_V(!pkg, NULL);
  103. unzGoToFilePos(pkg, &file.file_pos);
  104. if (unzOpenCurrentFile(pkg) != UNZ_OK) {
  105. unzClose(pkg);
  106. ERR_FAIL_V(NULL);
  107. };
  108. return pkg;
  109. };
  110. bool ZipArchive::try_open_pack(const String& p_name) {
  111. //printf("opening pack %ls, %i, %i\n", p_name.c_str(), p_name.extension().nocasecmp_to("zip"), p_name.extension().nocasecmp_to("pcz"));
  112. if (p_name.extension().nocasecmp_to("zip") != 0 && p_name.extension().nocasecmp_to("pcz") != 0)
  113. return false;
  114. zlib_filefunc_def io;
  115. FileAccess* f = FileAccess::open(p_name, FileAccess::READ);
  116. if (!f)
  117. return false;
  118. io.opaque = f;
  119. io.zopen_file = godot_open;
  120. io.zread_file = godot_read;
  121. io.zwrite_file = godot_write;
  122. io.ztell_file = godot_tell;
  123. io.zseek_file = godot_seek;
  124. io.zclose_file = godot_close;
  125. io.zerror_file = godot_testerror;
  126. unzFile zfile = unzOpen2(p_name.utf8().get_data(), &io);
  127. ERR_FAIL_COND_V(!zfile, false);
  128. unz_global_info64 gi;
  129. int err = unzGetGlobalInfo64(zfile, &gi);
  130. ERR_FAIL_COND_V(err!=UNZ_OK, false);
  131. Package pkg;
  132. pkg.filename = p_name;
  133. pkg.zfile = zfile;
  134. packages.push_back(pkg);
  135. int pkg_num = packages.size()-1;
  136. for (unsigned int i=0;i<gi.number_entry;i++) {
  137. char filename_inzip[256];
  138. unz_file_info64 file_info;
  139. err = unzGetCurrentFileInfo64(zfile,&file_info,filename_inzip,sizeof(filename_inzip),NULL,0,NULL,0);
  140. ERR_CONTINUE(err != UNZ_OK);
  141. File f;
  142. f.package = pkg_num;
  143. unzGetFilePos(zfile, &f.file_pos);
  144. String fname = String("res://") + filename_inzip;
  145. files[fname] = f;
  146. uint8_t md5[16]={0,0,0,0,0,0,0,0 , 0,0,0,0,0,0,0,0};
  147. PackedData::get_singleton()->add_path(p_name, fname, 0, 0, md5, this);
  148. if ((i+1)<gi.number_entry) {
  149. unzGoToNextFile(zfile);
  150. };
  151. };
  152. return true;
  153. };
  154. bool ZipArchive::file_exists(String p_name) const {
  155. return files.has(p_name);
  156. };
  157. FileAccess* ZipArchive::get_file(const String& p_path, PackedData::PackedFile* p_file) {
  158. return memnew(FileAccessZip(p_path, *p_file));
  159. };
  160. ZipArchive* ZipArchive::get_singleton() {
  161. if (instance == NULL) {
  162. instance = memnew(ZipArchive);
  163. };
  164. return instance;
  165. };
  166. ZipArchive::ZipArchive() {
  167. instance = this;
  168. //fa_create_func = FileAccess::get_create_func();
  169. };
  170. ZipArchive::~ZipArchive() {
  171. for (int i=0; i<packages.size(); i++) {
  172. FileAccess* f = (FileAccess*)unzGetOpaque(packages[i].zfile);
  173. unzClose(packages[i].zfile);
  174. memdelete(f);
  175. };
  176. packages.clear();
  177. };
  178. Error FileAccessZip::_open(const String& p_path, int p_mode_flags) {
  179. close();
  180. ERR_FAIL_COND_V(p_mode_flags & FileAccess::WRITE, FAILED);
  181. ZipArchive* arch = ZipArchive::get_singleton();
  182. ERR_FAIL_COND_V(!arch, FAILED);
  183. zfile = arch->get_file_handle(p_path);
  184. ERR_FAIL_COND_V(!zfile, FAILED);
  185. int err = unzGetCurrentFileInfo64(zfile,&file_info,NULL,0,NULL,0,NULL,0);
  186. ERR_FAIL_COND_V(err != UNZ_OK, FAILED);
  187. return OK;
  188. };
  189. void FileAccessZip::close() {
  190. if (!zfile)
  191. return;
  192. ZipArchive* arch = ZipArchive::get_singleton();
  193. ERR_FAIL_COND(!arch);
  194. arch->close_handle(zfile);
  195. zfile = NULL;
  196. };
  197. bool FileAccessZip::is_open() const {
  198. return zfile != NULL;
  199. };
  200. void FileAccessZip::seek(size_t p_position) {
  201. ERR_FAIL_COND(!zfile);
  202. unzSeekCurrentFile(zfile, p_position);
  203. };
  204. void FileAccessZip::seek_end(int64_t p_position) {
  205. ERR_FAIL_COND(!zfile);
  206. unzSeekCurrentFile(zfile, get_len() + p_position);
  207. };
  208. size_t FileAccessZip::get_pos() const {
  209. ERR_FAIL_COND_V(!zfile, 0);
  210. return unztell(zfile);
  211. };
  212. size_t FileAccessZip::get_len() const {
  213. ERR_FAIL_COND_V(!zfile, 0);
  214. return file_info.uncompressed_size;
  215. };
  216. bool FileAccessZip::eof_reached() const {
  217. ERR_FAIL_COND_V(!zfile, true);
  218. return at_eof;
  219. };
  220. uint8_t FileAccessZip::get_8() const {
  221. uint8_t ret = 0;
  222. get_buffer(&ret, 1);
  223. return ret;
  224. };
  225. int FileAccessZip::get_buffer(uint8_t *p_dst,int p_length) const {
  226. ERR_FAIL_COND_V(!zfile, -1);
  227. at_eof = unzeof(zfile);
  228. if (at_eof)
  229. return 0;
  230. int read = unzReadCurrentFile(zfile, p_dst, p_length);
  231. ERR_FAIL_COND_V(read < 0, read);
  232. if (read < p_length)
  233. at_eof = true;
  234. return read;
  235. };
  236. Error FileAccessZip::get_error() const {
  237. if (!zfile) {
  238. return ERR_UNCONFIGURED;
  239. };
  240. if (eof_reached()) {
  241. return ERR_FILE_EOF;
  242. };
  243. return OK;
  244. };
  245. void FileAccessZip::store_8(uint8_t p_dest) {
  246. ERR_FAIL();
  247. };
  248. bool FileAccessZip::file_exists(const String& p_name) {
  249. return false;
  250. };
  251. FileAccessZip::FileAccessZip(const String& p_path, const PackedData::PackedFile& p_file) {
  252. zfile = NULL;
  253. _open(p_path, FileAccess::READ);
  254. };
  255. FileAccessZip::~FileAccessZip() {
  256. close();
  257. };
  258. #endif