| 
									
										
										
										
											2023-01-05 13:25:55 +01:00
										 |  |  | /**************************************************************************/ | 
					
						
							|  |  |  | /*  file_access_zip.cpp                                                   */ | 
					
						
							|  |  |  | /**************************************************************************/ | 
					
						
							|  |  |  | /*                         This file is part of:                          */ | 
					
						
							|  |  |  | /*                             GODOT ENGINE                               */ | 
					
						
							|  |  |  | /*                        https://godotengine.org                         */ | 
					
						
							|  |  |  | /**************************************************************************/ | 
					
						
							|  |  |  | /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ | 
					
						
							|  |  |  | /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur.                  */ | 
					
						
							|  |  |  | /*                                                                        */ | 
					
						
							|  |  |  | /* Permission is hereby granted, free of charge, to any person obtaining  */ | 
					
						
							|  |  |  | /* a copy of this software and associated documentation files (the        */ | 
					
						
							|  |  |  | /* "Software"), to deal in the Software without restriction, including    */ | 
					
						
							|  |  |  | /* without limitation the rights to use, copy, modify, merge, publish,    */ | 
					
						
							|  |  |  | /* distribute, sublicense, and/or sell copies of the Software, and to     */ | 
					
						
							|  |  |  | /* permit persons to whom the Software is furnished to do so, subject to  */ | 
					
						
							|  |  |  | /* the following conditions:                                              */ | 
					
						
							|  |  |  | /*                                                                        */ | 
					
						
							|  |  |  | /* The above copyright notice and this permission notice shall be         */ | 
					
						
							|  |  |  | /* included in all copies or substantial portions of the Software.        */ | 
					
						
							|  |  |  | /*                                                                        */ | 
					
						
							|  |  |  | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,        */ | 
					
						
							|  |  |  | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF     */ | 
					
						
							|  |  |  | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ | 
					
						
							|  |  |  | /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY   */ | 
					
						
							|  |  |  | /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,   */ | 
					
						
							|  |  |  | /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE      */ | 
					
						
							|  |  |  | /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.                 */ | 
					
						
							|  |  |  | /**************************************************************************/ | 
					
						
							| 
									
										
										
										
											2018-01-05 00:50:27 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | #ifdef MINIZIP_ENABLED
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "file_access_zip.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-11 14:51:48 +02:00
										 |  |  | #include "core/io/file_access.h"
 | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-02 01:20:12 +02:00
										 |  |  | ZipArchive *ZipArchive::instance = nullptr; | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | extern "C" { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-23 11:08:58 +02:00
										 |  |  | struct ZipData { | 
					
						
							|  |  |  | 	Ref<FileAccess> f; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void *godot_open(voidpf opaque, const char *p_fname, int mode) { | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	if (mode & ZLIB_FILEFUNC_MODE_WRITE) { | 
					
						
							| 
									
										
										
										
											2020-04-02 01:20:12 +02:00
										 |  |  | 		return nullptr; | 
					
						
							| 
									
										
										
										
											2018-12-08 15:07:33 -05:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-02 15:04:00 +08:00
										 |  |  | 	Ref<FileAccess> f = FileAccess::open(String::utf8(p_fname), FileAccess::READ); | 
					
						
							| 
									
										
										
										
											2022-03-23 11:08:58 +02:00
										 |  |  | 	ERR_FAIL_COND_V(f.is_null(), nullptr); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-23 11:08:58 +02:00
										 |  |  | 	ZipData *zd = memnew(ZipData); | 
					
						
							|  |  |  | 	zd->f = f; | 
					
						
							|  |  |  | 	return zd; | 
					
						
							| 
									
										
										
										
											2018-12-08 15:07:33 -05:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-23 11:08:58 +02:00
										 |  |  | static uLong godot_read(voidpf opaque, voidpf stream, void *buf, uLong size) { | 
					
						
							|  |  |  | 	ZipData *zd = (ZipData *)stream; | 
					
						
							|  |  |  | 	zd->f->get_buffer((uint8_t *)buf, size); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	return size; | 
					
						
							| 
									
										
										
										
											2018-12-08 15:07:33 -05:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | static uLong godot_write(voidpf opaque, voidpf stream, const void *buf, uLong size) { | 
					
						
							|  |  |  | 	return 0; | 
					
						
							| 
									
										
										
										
											2018-12-08 15:07:33 -05:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | static long godot_tell(voidpf opaque, voidpf stream) { | 
					
						
							| 
									
										
										
										
											2022-03-23 11:08:58 +02:00
										 |  |  | 	ZipData *zd = (ZipData *)stream; | 
					
						
							|  |  |  | 	return zd->f->get_position(); | 
					
						
							| 
									
										
										
										
											2018-12-08 15:07:33 -05:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | static long godot_seek(voidpf opaque, voidpf stream, uLong offset, int origin) { | 
					
						
							| 
									
										
										
										
											2022-03-23 11:08:58 +02:00
										 |  |  | 	ZipData *zd = (ZipData *)stream; | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-26 18:51:13 +01:00
										 |  |  | 	uint64_t pos = offset; | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	switch (origin) { | 
					
						
							|  |  |  | 		case ZLIB_FILEFUNC_SEEK_CUR: | 
					
						
							| 
									
										
										
										
											2022-03-23 11:08:58 +02:00
										 |  |  | 			pos = zd->f->get_position() + offset; | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 			break; | 
					
						
							|  |  |  | 		case ZLIB_FILEFUNC_SEEK_END: | 
					
						
							| 
									
										
										
										
											2022-03-23 11:08:58 +02:00
										 |  |  | 			pos = zd->f->get_length() + offset; | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 			break; | 
					
						
							|  |  |  | 		default: | 
					
						
							|  |  |  | 			break; | 
					
						
							| 
									
										
										
										
											2018-12-08 15:07:33 -05:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-23 11:08:58 +02:00
										 |  |  | 	zd->f->seek(pos); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	return 0; | 
					
						
							| 
									
										
										
										
											2018-12-08 15:07:33 -05:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | static int godot_close(voidpf opaque, voidpf stream) { | 
					
						
							| 
									
										
										
										
											2022-03-23 11:08:58 +02:00
										 |  |  | 	ZipData *zd = (ZipData *)stream; | 
					
						
							|  |  |  | 	memdelete(zd); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	return 0; | 
					
						
							| 
									
										
										
										
											2018-12-08 15:07:33 -05:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | static int godot_testerror(voidpf opaque, voidpf stream) { | 
					
						
							| 
									
										
										
										
											2022-03-23 11:08:58 +02:00
										 |  |  | 	ZipData *zd = (ZipData *)stream; | 
					
						
							|  |  |  | 	return zd->f->get_error() != OK ? 1 : 0; | 
					
						
							| 
									
										
										
										
											2018-12-08 15:07:33 -05:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-25 22:35:00 -03:00
										 |  |  | static voidpf godot_alloc(voidpf opaque, uInt items, uInt size) { | 
					
						
							| 
									
										
										
										
											2021-09-07 23:25:35 -05:00
										 |  |  | 	return memalloc((size_t)items * size); | 
					
						
							| 
									
										
										
										
											2018-12-08 15:07:33 -05:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-25 22:35:00 -03:00
										 |  |  | static void godot_free(voidpf opaque, voidpf address) { | 
					
						
							|  |  |  | 	memfree(address); | 
					
						
							| 
									
										
										
										
											2018-12-08 15:07:33 -05:00
										 |  |  | } | 
					
						
							|  |  |  | } // extern "C"
 | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | void ZipArchive::close_handle(unzFile p_file) const { | 
					
						
							| 
									
										
										
										
											2023-09-28 11:40:18 +02:00
										 |  |  | 	ERR_FAIL_NULL_MSG(p_file, "Cannot close a file if none is open."); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	unzCloseCurrentFile(p_file); | 
					
						
							|  |  |  | 	unzClose(p_file); | 
					
						
							| 
									
										
										
										
											2018-12-08 15:07:33 -05:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | unzFile ZipArchive::get_file_handle(String p_file) const { | 
					
						
							| 
									
										
										
										
											2020-04-02 01:20:12 +02:00
										 |  |  | 	ERR_FAIL_COND_V_MSG(!file_exists(p_file), nullptr, "File '" + p_file + " doesn't exist."); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	File file = files[p_file]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	zlib_filefunc_def io; | 
					
						
							| 
									
										
										
										
											2021-04-27 16:19:21 +02:00
										 |  |  | 	memset(&io, 0, sizeof(io)); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-14 13:53:43 +08:00
										 |  |  | 	io.opaque = nullptr; | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	io.zopen_file = godot_open; | 
					
						
							|  |  |  | 	io.zread_file = godot_read; | 
					
						
							|  |  |  | 	io.zwrite_file = godot_write; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	io.ztell_file = godot_tell; | 
					
						
							|  |  |  | 	io.zseek_file = godot_seek; | 
					
						
							|  |  |  | 	io.zclose_file = godot_close; | 
					
						
							|  |  |  | 	io.zerror_file = godot_testerror; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-25 22:35:00 -03:00
										 |  |  | 	io.alloc_mem = godot_alloc; | 
					
						
							|  |  |  | 	io.free_mem = godot_free; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	unzFile pkg = unzOpen2(packages[file.package].filename.utf8().get_data(), &io); | 
					
						
							| 
									
										
										
										
											2023-09-28 11:40:18 +02:00
										 |  |  | 	ERR_FAIL_NULL_V_MSG(pkg, nullptr, "Cannot open file '" + packages[file.package].filename + "'."); | 
					
						
							| 
									
										
										
										
											2015-05-25 22:35:00 -03:00
										 |  |  | 	int unz_err = unzGoToFilePos(pkg, &file.file_pos); | 
					
						
							| 
									
										
										
										
											2017-04-06 13:18:41 +02:00
										 |  |  | 	if (unz_err != UNZ_OK || unzOpenCurrentFile(pkg) != UNZ_OK) { | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 		unzClose(pkg); | 
					
						
							| 
									
										
										
										
											2020-04-02 01:20:12 +02:00
										 |  |  | 		ERR_FAIL_V(nullptr); | 
					
						
							| 
									
										
										
										
											2018-12-08 15:07:33 -05:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	return pkg; | 
					
						
							| 
									
										
										
										
											2018-12-08 15:07:33 -05:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-26 18:51:13 +01:00
										 |  |  | bool ZipArchive::try_open_pack(const String &p_path, bool p_replace_files, uint64_t p_offset = 0) { | 
					
						
							| 
									
										
										
										
											2020-07-13 11:22:06 -05:00
										 |  |  | 	// load with offset feature only supported for PCK files
 | 
					
						
							|  |  |  | 	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."); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 	if (p_path.get_extension().nocasecmp_to("zip") != 0 && p_path.get_extension().nocasecmp_to("pcz") != 0) { | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 		return false; | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	zlib_filefunc_def io; | 
					
						
							| 
									
										
										
										
											2021-05-14 13:53:43 +08:00
										 |  |  | 	memset(&io, 0, sizeof(io)); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-14 13:53:43 +08:00
										 |  |  | 	io.opaque = nullptr; | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	io.zopen_file = godot_open; | 
					
						
							|  |  |  | 	io.zread_file = godot_read; | 
					
						
							|  |  |  | 	io.zwrite_file = godot_write; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	io.ztell_file = godot_tell; | 
					
						
							|  |  |  | 	io.zseek_file = godot_seek; | 
					
						
							|  |  |  | 	io.zclose_file = godot_close; | 
					
						
							|  |  |  | 	io.zerror_file = godot_testerror; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-11 15:10:05 -04:00
										 |  |  | 	unzFile zfile = unzOpen2(p_path.utf8().get_data(), &io); | 
					
						
							| 
									
										
										
										
											2023-09-28 11:40:18 +02:00
										 |  |  | 	ERR_FAIL_NULL_V(zfile, false); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	unz_global_info64 gi; | 
					
						
							|  |  |  | 	int err = unzGetGlobalInfo64(zfile, &gi); | 
					
						
							|  |  |  | 	ERR_FAIL_COND_V(err != UNZ_OK, false); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	Package pkg; | 
					
						
							| 
									
										
										
										
											2017-08-11 15:10:05 -04:00
										 |  |  | 	pkg.filename = p_path; | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	pkg.zfile = zfile; | 
					
						
							|  |  |  | 	packages.push_back(pkg); | 
					
						
							|  |  |  | 	int pkg_num = packages.size() - 1; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-22 18:45:08 +02:00
										 |  |  | 	for (uint64_t i = 0; i < gi.number_entry; i++) { | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 		char filename_inzip[256]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		unz_file_info64 file_info; | 
					
						
							| 
									
										
										
										
											2020-04-02 01:20:12 +02:00
										 |  |  | 		err = unzGetCurrentFileInfo64(zfile, &file_info, filename_inzip, sizeof(filename_inzip), nullptr, 0, nullptr, 0); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 		ERR_CONTINUE(err != UNZ_OK); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		File f; | 
					
						
							|  |  |  | 		f.package = pkg_num; | 
					
						
							|  |  |  | 		unzGetFilePos(zfile, &f.file_pos); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-06 11:34:10 +02:00
										 |  |  | 		String fname = String("res://") + String::utf8(filename_inzip); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 		files[fname] = f; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-13 18:03:28 -03:00
										 |  |  | 		uint8_t md5[16] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; | 
					
						
							| 
									
										
										
										
											2020-04-28 20:51:29 +03:00
										 |  |  | 		PackedData::get_singleton()->add_path(p_path, fname, 1, 0, md5, this, p_replace_files, false); | 
					
						
							| 
									
										
										
										
											2020-07-27 13:43:20 +03:00
										 |  |  | 		//printf("packed data add path %s, %s\n", p_name.utf8().get_data(), fname.utf8().get_data());
 | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		if ((i + 1) < gi.number_entry) { | 
					
						
							|  |  |  | 			unzGoToNextFile(zfile); | 
					
						
							| 
									
										
										
										
											2018-12-08 15:07:33 -05:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	return true; | 
					
						
							| 
									
										
										
										
											2018-12-08 15:07:33 -05:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | bool ZipArchive::file_exists(String p_name) const { | 
					
						
							|  |  |  | 	return files.has(p_name); | 
					
						
							| 
									
										
										
										
											2018-12-08 15:07:33 -05:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-23 11:08:58 +02:00
										 |  |  | Ref<FileAccess> ZipArchive::get_file(const String &p_path, PackedData::PackedFile *p_file) { | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	return memnew(FileAccessZip(p_path, *p_file)); | 
					
						
							| 
									
										
										
										
											2018-12-08 15:07:33 -05:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | ZipArchive *ZipArchive::get_singleton() { | 
					
						
							| 
									
										
										
										
											2020-04-02 01:20:12 +02:00
										 |  |  | 	if (instance == nullptr) { | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 		instance = memnew(ZipArchive); | 
					
						
							| 
									
										
										
										
											2018-12-08 15:07:33 -05:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	return instance; | 
					
						
							| 
									
										
										
										
											2018-12-08 15:07:33 -05:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | ZipArchive::ZipArchive() { | 
					
						
							|  |  |  | 	instance = this; | 
					
						
							| 
									
										
										
										
											2018-12-08 15:07:33 -05:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | ZipArchive::~ZipArchive() { | 
					
						
							|  |  |  | 	for (int i = 0; i < packages.size(); i++) { | 
					
						
							|  |  |  | 		unzClose(packages[i].zfile); | 
					
						
							| 
									
										
										
										
											2018-12-08 15:07:33 -05:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	packages.clear(); | 
					
						
							| 
									
										
										
										
											2018-12-08 15:07:33 -05:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-05 13:01:31 +02:00
										 |  |  | Error FileAccessZip::open_internal(const String &p_path, int p_mode_flags) { | 
					
						
							| 
									
										
										
										
											2022-04-12 11:15:02 +03:00
										 |  |  | 	_close(); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	ERR_FAIL_COND_V(p_mode_flags & FileAccess::WRITE, FAILED); | 
					
						
							|  |  |  | 	ZipArchive *arch = ZipArchive::get_singleton(); | 
					
						
							| 
									
										
										
										
											2023-09-09 16:11:33 +02:00
										 |  |  | 	ERR_FAIL_NULL_V(arch, FAILED); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	zfile = arch->get_file_handle(p_path); | 
					
						
							| 
									
										
										
										
											2023-09-28 11:40:18 +02:00
										 |  |  | 	ERR_FAIL_NULL_V(zfile, FAILED); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-02 01:20:12 +02:00
										 |  |  | 	int err = unzGetCurrentFileInfo64(zfile, &file_info, nullptr, 0, nullptr, 0, nullptr, 0); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	ERR_FAIL_COND_V(err != UNZ_OK, FAILED); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return OK; | 
					
						
							| 
									
										
										
										
											2018-12-08 15:07:33 -05:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-12 11:15:02 +03:00
										 |  |  | void FileAccessZip::_close() { | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 	if (!zfile) { | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 		return; | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	ZipArchive *arch = ZipArchive::get_singleton(); | 
					
						
							| 
									
										
										
										
											2023-09-09 16:11:33 +02:00
										 |  |  | 	ERR_FAIL_NULL(arch); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	arch->close_handle(zfile); | 
					
						
							| 
									
										
										
										
											2020-04-02 01:20:12 +02:00
										 |  |  | 	zfile = nullptr; | 
					
						
							| 
									
										
										
										
											2018-12-08 15:07:33 -05:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | bool FileAccessZip::is_open() const { | 
					
						
							| 
									
										
										
										
											2020-04-02 01:20:12 +02:00
										 |  |  | 	return zfile != nullptr; | 
					
						
							| 
									
										
										
										
											2018-12-08 15:07:33 -05:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-26 18:51:13 +01:00
										 |  |  | void FileAccessZip::seek(uint64_t p_position) { | 
					
						
							| 
									
										
										
										
											2023-09-28 11:40:18 +02:00
										 |  |  | 	ERR_FAIL_NULL(zfile); | 
					
						
							| 
									
										
										
										
											2019-03-26 18:51:13 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	unzSeekCurrentFile(zfile, p_position); | 
					
						
							| 
									
										
										
										
											2018-12-08 15:07:33 -05:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | void FileAccessZip::seek_end(int64_t p_position) { | 
					
						
							| 
									
										
										
										
											2023-09-28 11:40:18 +02:00
										 |  |  | 	ERR_FAIL_NULL(zfile); | 
					
						
							| 
									
										
										
										
											2021-05-25 07:58:49 +01:00
										 |  |  | 	unzSeekCurrentFile(zfile, get_length() + p_position); | 
					
						
							| 
									
										
										
										
											2018-12-08 15:07:33 -05:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-26 18:51:13 +01:00
										 |  |  | uint64_t FileAccessZip::get_position() const { | 
					
						
							| 
									
										
										
										
											2023-09-28 11:40:18 +02:00
										 |  |  | 	ERR_FAIL_NULL_V(zfile, 0); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	return unztell(zfile); | 
					
						
							| 
									
										
										
										
											2018-12-08 15:07:33 -05:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-25 07:58:49 +01:00
										 |  |  | uint64_t FileAccessZip::get_length() const { | 
					
						
							| 
									
										
										
										
											2023-09-28 11:40:18 +02:00
										 |  |  | 	ERR_FAIL_NULL_V(zfile, 0); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	return file_info.uncompressed_size; | 
					
						
							| 
									
										
										
										
											2018-12-08 15:07:33 -05:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | bool FileAccessZip::eof_reached() const { | 
					
						
							| 
									
										
										
										
											2023-09-28 11:40:18 +02:00
										 |  |  | 	ERR_FAIL_NULL_V(zfile, true); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	return at_eof; | 
					
						
							| 
									
										
										
										
											2018-12-08 15:07:33 -05:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | uint8_t FileAccessZip::get_8() const { | 
					
						
							|  |  |  | 	uint8_t ret = 0; | 
					
						
							|  |  |  | 	get_buffer(&ret, 1); | 
					
						
							|  |  |  | 	return ret; | 
					
						
							| 
									
										
										
										
											2018-12-08 15:07:33 -05:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-26 18:51:13 +01:00
										 |  |  | uint64_t FileAccessZip::get_buffer(uint8_t *p_dst, uint64_t p_length) const { | 
					
						
							| 
									
										
										
										
											2021-03-16 22:55:11 +01:00
										 |  |  | 	ERR_FAIL_COND_V(!p_dst && p_length > 0, -1); | 
					
						
							| 
									
										
										
										
											2023-09-28 11:40:18 +02:00
										 |  |  | 	ERR_FAIL_NULL_V(zfile, -1); | 
					
						
							| 
									
										
										
										
											2019-03-26 18:51:13 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	at_eof = unzeof(zfile); | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 	if (at_eof) { | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 		return 0; | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2019-03-26 18:51:13 +01:00
										 |  |  | 	int64_t read = unzReadCurrentFile(zfile, p_dst, p_length); | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	ERR_FAIL_COND_V(read < 0, read); | 
					
						
							| 
									
										
										
										
											2019-03-26 18:51:13 +01:00
										 |  |  | 	if ((uint64_t)read < p_length) { | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 		at_eof = true; | 
					
						
							| 
									
										
										
										
											2020-05-14 16:41:43 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	return read; | 
					
						
							| 
									
										
										
										
											2018-12-08 15:07:33 -05:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | Error FileAccessZip::get_error() const { | 
					
						
							|  |  |  | 	if (!zfile) { | 
					
						
							|  |  |  | 		return ERR_UNCONFIGURED; | 
					
						
							| 
									
										
										
										
											2018-12-08 15:07:33 -05:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 	if (eof_reached()) { | 
					
						
							|  |  |  | 		return ERR_FILE_EOF; | 
					
						
							| 
									
										
										
										
											2018-12-08 15:07:33 -05:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	return OK; | 
					
						
							| 
									
										
										
										
											2018-12-08 15:07:33 -05:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-22 12:56:02 +07:00
										 |  |  | void FileAccessZip::flush() { | 
					
						
							|  |  |  | 	ERR_FAIL(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | void FileAccessZip::store_8(uint8_t p_dest) { | 
					
						
							|  |  |  | 	ERR_FAIL(); | 
					
						
							| 
									
										
										
										
											2018-12-08 15:07:33 -05:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | bool FileAccessZip::file_exists(const String &p_name) { | 
					
						
							|  |  |  | 	return false; | 
					
						
							| 
									
										
										
										
											2018-12-08 15:07:33 -05:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-16 15:25:32 +02:00
										 |  |  | void FileAccessZip::close() { | 
					
						
							|  |  |  | 	_close(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-12 17:01:17 +02:00
										 |  |  | FileAccessZip::FileAccessZip(const String &p_path, const PackedData::PackedFile &p_file) { | 
					
						
							| 
									
										
										
										
											2022-09-26 20:47:00 -07:00
										 |  |  | 	open_internal(p_path, FileAccess::READ); | 
					
						
							| 
									
										
										
										
											2018-12-08 15:07:33 -05:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | FileAccessZip::~FileAccessZip() { | 
					
						
							| 
									
										
										
										
											2022-04-12 11:15:02 +03:00
										 |  |  | 	_close(); | 
					
						
							| 
									
										
										
										
											2018-12-08 15:07:33 -05:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2014-02-09 22:10:30 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-12 17:01:17 +02:00
										 |  |  | #endif // MINIZIP_ENABLED
 |