mirror of
				https://github.com/vcmi/vcmi.git
				synced 2025-10-31 00:07:39 +02:00 
			
		
		
		
	Wip on zip serialize fixes
This commit is contained in:
		| @@ -75,6 +75,8 @@ public: | |||||||
| 	 * @return the length in bytes of the stream. | 	 * @return the length in bytes of the stream. | ||||||
| 	 */ | 	 */ | ||||||
| 	si64 getSize() override; | 	si64 getSize() override; | ||||||
|  | 	 | ||||||
|  | 	const TBuffer & getBuffer(){return buffer;} | ||||||
|  |  | ||||||
| private: | private: | ||||||
| 	/** Actual data. */ | 	/** Actual data. */ | ||||||
|   | |||||||
| @@ -66,6 +66,9 @@ std::unordered_map<ResourceID, unz_file_pos> CZipLoader::listFiles(const std::st | |||||||
| 	std::unordered_map<ResourceID, unz_file_pos> ret; | 	std::unordered_map<ResourceID, unz_file_pos> ret; | ||||||
|  |  | ||||||
| 	unzFile file = unzOpen2_64(archive.c_str(), &zlibApi); | 	unzFile file = unzOpen2_64(archive.c_str(), &zlibApi); | ||||||
|  | 	 | ||||||
|  | 	if(file == nullptr) | ||||||
|  | 		logGlobal->errorStream() << archive << " failed to open"; | ||||||
|  |  | ||||||
| 	if (unzGoToFirstFile(file) == UNZ_OK) | 	if (unzGoToFirstFile(file) == UNZ_OK) | ||||||
| 	{ | 	{ | ||||||
|   | |||||||
| @@ -18,7 +18,7 @@ CZipOutputStream::CZipOutputStream(CZipSaver * owner_, zipFile archive, const st | |||||||
| { | { | ||||||
| 	//zip_fileinfo fileInfo; | 	//zip_fileinfo fileInfo; | ||||||
| 	 | 	 | ||||||
| 	zipOpenNewFileInZip(handle, | 	int status = zipOpenNewFileInZip(handle, | ||||||
|                        archiveFilename.c_str(), |                        archiveFilename.c_str(), | ||||||
|                        nullptr,//todo: use fileInfo, |                        nullptr,//todo: use fileInfo, | ||||||
|                        nullptr, |                        nullptr, | ||||||
| @@ -28,6 +28,10 @@ CZipOutputStream::CZipOutputStream(CZipSaver * owner_, zipFile archive, const st | |||||||
|                        "", |                        "", | ||||||
|                        Z_DEFLATED, |                        Z_DEFLATED, | ||||||
|                        Z_DEFAULT_COMPRESSION); |                        Z_DEFAULT_COMPRESSION); | ||||||
|  |      | ||||||
|  |     if(status != ZIP_OK) | ||||||
|  | 		throw new std::runtime_error("CZipOutputStream: zipOpenNewFileInZip failed"); | ||||||
|  | 	 | ||||||
| 	owner->activeStream = this; | 	owner->activeStream = this; | ||||||
| } | } | ||||||
|  |  | ||||||
| @@ -57,7 +61,7 @@ CZipSaver::CZipSaver(std::shared_ptr<CIOApi> api, const std::string & path): | |||||||
| 	handle = zipOpen2_64(path.c_str(), APPEND_STATUS_CREATE, nullptr, &zipApi); | 	handle = zipOpen2_64(path.c_str(), APPEND_STATUS_CREATE, nullptr, &zipApi); | ||||||
| 	 | 	 | ||||||
| 	if (handle == nullptr) | 	if (handle == nullptr) | ||||||
| 		throw new std::runtime_error("Failed to create archive");	 | 		throw new std::runtime_error("CZipSaver: Failed to create archive");	 | ||||||
| } | } | ||||||
|  |  | ||||||
| CZipSaver::~CZipSaver() | CZipSaver::~CZipSaver() | ||||||
|   | |||||||
| @@ -68,10 +68,10 @@ long ZCALLBACK CIOApi::seekFileProxy(voidpf  opaque, voidpf stream, ZPOS64_T off | |||||||
|         actualStream->skip(offset);//TODO: should we check actual skipped?  |         actualStream->skip(offset);//TODO: should we check actual skipped?  | ||||||
|         break; |         break; | ||||||
|     case ZLIB_FILEFUNC_SEEK_END : |     case ZLIB_FILEFUNC_SEEK_END : | ||||||
|     	ret = -1; |     	actualStream->seek(actualStream->getSize() - offset); | ||||||
|         break; |         break; | ||||||
|     case ZLIB_FILEFUNC_SEEK_SET : |     case ZLIB_FILEFUNC_SEEK_SET : | ||||||
|     	ret = actualStream->seek(offset); |     	actualStream->seek(offset); | ||||||
|         break; |         break; | ||||||
|     default: ret = -1; |     default: ret = -1; | ||||||
|     } |     } | ||||||
|   | |||||||
| @@ -306,6 +306,7 @@ void CMapSaverJson::saveHeader() | |||||||
| 		auto s = out.str(); | 		auto s = out.str(); | ||||||
| 		std::unique_ptr<COutputStream> stream = saver.addFile(HEADER_FILE_NAME); | 		std::unique_ptr<COutputStream> stream = saver.addFile(HEADER_FILE_NAME); | ||||||
| 		 | 		 | ||||||
| 		stream->write((const ui8*)s.c_str(), s.size()); | 		if (stream->write((const ui8*)s.c_str(), s.size()) != s.size()) | ||||||
|  | 			throw new std::runtime_error("CMapSaverJson::saveHeader() zip compression failed."); | ||||||
| 	}	 | 	}	 | ||||||
| } | } | ||||||
|   | |||||||
| @@ -19,8 +19,11 @@ | |||||||
| #include "../lib/rmg/CMapGenerator.h" | #include "../lib/rmg/CMapGenerator.h" | ||||||
| #include "../lib/mapping/MapFormatJson.h" | #include "../lib/mapping/MapFormatJson.h" | ||||||
|  |  | ||||||
|  | #include "../lib/VCMIDirs.h" | ||||||
|  |  | ||||||
| #include "MapComparer.h" | #include "MapComparer.h" | ||||||
|  |  | ||||||
|  |  | ||||||
| static const int TEST_RANDOM_SEED = 1337; | static const int TEST_RANDOM_SEED = 1337; | ||||||
|  |  | ||||||
| static std::unique_ptr<CMap> initialMap; | static std::unique_ptr<CMap> initialMap; | ||||||
| @@ -60,6 +63,17 @@ BOOST_AUTO_TEST_CASE(CMapFormatVCMI_Simple) | |||||||
| 			CMapSaverJson saver(&serializeBuffer); | 			CMapSaverJson saver(&serializeBuffer); | ||||||
| 			saver.saveMap(initialMap); | 			saver.saveMap(initialMap); | ||||||
| 		} | 		} | ||||||
|  | 		 | ||||||
|  | 		#if 1 | ||||||
|  | 		{ | ||||||
|  | 			std::ofstream tmp((VCMIDirs::get().userDataPath()/"temp.zip").string()); | ||||||
|  | 			tmp.write((const char *)&serializeBuffer.getBuffer()[0],serializeBuffer.getSize()); | ||||||
|  | 			tmp.flush(); | ||||||
|  | 		} | ||||||
|  | 		 | ||||||
|  | 		 | ||||||
|  | 		#endif // 1 | ||||||
|  | 		 | ||||||
| 		serializeBuffer.seek(0); | 		serializeBuffer.seek(0); | ||||||
| 		{ | 		{ | ||||||
| 			CMapLoaderJson loader(&serializeBuffer); | 			CMapLoaderJson loader(&serializeBuffer); | ||||||
| @@ -71,10 +85,9 @@ BOOST_AUTO_TEST_CASE(CMapFormatVCMI_Simple) | |||||||
|  |  | ||||||
| 		logGlobal->info("CMapFormatVCMI_Simple finish"); | 		logGlobal->info("CMapFormatVCMI_Simple finish"); | ||||||
| 	} | 	} | ||||||
| 	catch(const std::exception & e) | 	catch(...) | ||||||
| 	{ | 	{ | ||||||
| 		logGlobal->info("CMapFormatVCMI_Simple crash"); | 		handleException(); | ||||||
| 		logGlobal-> errorStream() << e.what(); | 		BOOST_FAIL("Test case crashed"); | ||||||
| 		throw; |  | ||||||
| 	} | 	} | ||||||
| } | } | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user