Sfoglia il codice sorgente

Wip on zip serialize fixes

AlexVinS 10 anni fa
parent
commit
f2164abf1b

+ 2 - 0
lib/filesystem/CMemoryBuffer.h

@@ -75,6 +75,8 @@ public:
 	 * @return the length in bytes of the stream.
 	 */
 	si64 getSize() override;
+	
+	const TBuffer & getBuffer(){return buffer;}
 
 private:
 	/** Actual data. */

+ 3 - 0
lib/filesystem/CZipLoader.cpp

@@ -66,6 +66,9 @@ std::unordered_map<ResourceID, unz_file_pos> CZipLoader::listFiles(const std::st
 	std::unordered_map<ResourceID, unz_file_pos> ret;
 
 	unzFile file = unzOpen2_64(archive.c_str(), &zlibApi);
+	
+	if(file == nullptr)
+		logGlobal->errorStream() << archive << " failed to open";
 
 	if (unzGoToFirstFile(file) == UNZ_OK)
 	{

+ 6 - 2
lib/filesystem/CZipSaver.cpp

@@ -18,7 +18,7 @@ CZipOutputStream::CZipOutputStream(CZipSaver * owner_, zipFile archive, const st
 {
 	//zip_fileinfo fileInfo;
 	
-	zipOpenNewFileInZip(handle,
+	int status = zipOpenNewFileInZip(handle,
                        archiveFilename.c_str(),
                        nullptr,//todo: use fileInfo,
                        nullptr,
@@ -28,6 +28,10 @@ CZipOutputStream::CZipOutputStream(CZipSaver * owner_, zipFile archive, const st
                        "",
                        Z_DEFLATED,
                        Z_DEFAULT_COMPRESSION);
+    
+    if(status != ZIP_OK)
+		throw new std::runtime_error("CZipOutputStream: zipOpenNewFileInZip failed");
+	
 	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);
 	
 	if (handle == nullptr)
-		throw new std::runtime_error("Failed to create archive");	
+		throw new std::runtime_error("CZipSaver: Failed to create archive");	
 }
 
 CZipSaver::~CZipSaver()

+ 2 - 2
lib/filesystem/MinizipExtensions.cpp

@@ -68,10 +68,10 @@ long ZCALLBACK CIOApi::seekFileProxy(voidpf  opaque, voidpf stream, ZPOS64_T off
         actualStream->skip(offset);//TODO: should we check actual skipped? 
         break;
     case ZLIB_FILEFUNC_SEEK_END :
-    	ret = -1;
+    	actualStream->seek(actualStream->getSize() - offset);
         break;
     case ZLIB_FILEFUNC_SEEK_SET :
-    	ret = actualStream->seek(offset);
+    	actualStream->seek(offset);
         break;
     default: ret = -1;
     }

+ 2 - 1
lib/mapping/MapFormatJson.cpp

@@ -306,6 +306,7 @@ void CMapSaverJson::saveHeader()
 		auto s = out.str();
 		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.");
 	}	
 }

+ 17 - 4
test/CMapFormatTest.cpp

@@ -19,8 +19,11 @@
 #include "../lib/rmg/CMapGenerator.h"
 #include "../lib/mapping/MapFormatJson.h"
 
+#include "../lib/VCMIDirs.h"
+
 #include "MapComparer.h"
 
+
 static const int TEST_RANDOM_SEED = 1337;
 
 static std::unique_ptr<CMap> initialMap;
@@ -60,6 +63,17 @@ BOOST_AUTO_TEST_CASE(CMapFormatVCMI_Simple)
 			CMapSaverJson saver(&serializeBuffer);
 			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);
 		{
 			CMapLoaderJson loader(&serializeBuffer);
@@ -71,10 +85,9 @@ BOOST_AUTO_TEST_CASE(CMapFormatVCMI_Simple)
 
 		logGlobal->info("CMapFormatVCMI_Simple finish");
 	}
-	catch(const std::exception & e)
+	catch(...)
 	{
-		logGlobal->info("CMapFormatVCMI_Simple crash");
-		logGlobal-> errorStream() << e.what();
-		throw;
+		handleException();
+		BOOST_FAIL("Test case crashed");
 	}
 }