From b3ac146ba9bf2c00c7cc1d813b2a3e1f49286e51 Mon Sep 17 00:00:00 2001 From: AlexVinS Date: Tue, 11 Aug 2015 14:44:47 +0300 Subject: [PATCH] Define bridge interface to minizip IOAPI --- lib/CMakeLists.txt | 1 + lib/VCMI_lib.cbp | 3 + lib/filesystem/CInputOutputStream.h | 9 +++ lib/filesystem/CMemoryBuffer.cpp | 2 +- lib/filesystem/CMemoryBuffer.h | 8 +- lib/filesystem/COutputStream.h | 2 +- lib/filesystem/MinizipExtensions.cpp | 106 +++++++++++++++++++++++++++ lib/filesystem/MinizipExtensions.h | 34 +++++++++ 8 files changed, 159 insertions(+), 6 deletions(-) create mode 100644 lib/filesystem/CInputOutputStream.h create mode 100644 lib/filesystem/MinizipExtensions.cpp create mode 100644 lib/filesystem/MinizipExtensions.h diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt index dacb58b81..f70513ad6 100644 --- a/lib/CMakeLists.txt +++ b/lib/CMakeLists.txt @@ -19,6 +19,7 @@ set(lib_SRCS filesystem/CZipLoader.cpp filesystem/Filesystem.cpp filesystem/ResourceID.cpp + filesystem/MinizipExtensions.cpp mapObjects/CArmedInstance.cpp mapObjects/CBank.cpp diff --git a/lib/VCMI_lib.cbp b/lib/VCMI_lib.cbp index 6914feb79..d3409c191 100644 --- a/lib/VCMI_lib.cbp +++ b/lib/VCMI_lib.cbp @@ -222,6 +222,7 @@ + @@ -234,6 +235,8 @@ + + diff --git a/lib/filesystem/CInputOutputStream.h b/lib/filesystem/CInputOutputStream.h new file mode 100644 index 000000000..55c88b360 --- /dev/null +++ b/lib/filesystem/CInputOutputStream.h @@ -0,0 +1,9 @@ +#pragma once + +#include "CInputStream.h" +#include "COutputStream.h" + +class CInputOutputStream: public CInputStream, public COutputStream +{ + +}; diff --git a/lib/filesystem/CMemoryBuffer.cpp b/lib/filesystem/CMemoryBuffer.cpp index 659458ede..8acbf586a 100644 --- a/lib/filesystem/CMemoryBuffer.cpp +++ b/lib/filesystem/CMemoryBuffer.cpp @@ -18,7 +18,7 @@ CMemoryBuffer::CMemoryBuffer(): } -si64 CMemoryBuffer::write(ui8 * data, si64 size) +si64 CMemoryBuffer::write(const ui8 * data, si64 size) { buffer.reserve(tell()+size); diff --git a/lib/filesystem/CMemoryBuffer.h b/lib/filesystem/CMemoryBuffer.h index 04a028966..74f8f2c58 100644 --- a/lib/filesystem/CMemoryBuffer.h +++ b/lib/filesystem/CMemoryBuffer.h @@ -11,14 +11,14 @@ */ -#include "CInputStream.h" -#include "COutputStream.h" +#include "CInputOutputStream.h" + /** * A class which provides IO memory buffer. */ -class DLL_LINKAGE CMemoryBuffer : public CInputStream, public COutputStream +class DLL_LINKAGE CMemoryBuffer : public CInputOutputStream { public: typedef std::vector TBuffer; @@ -36,7 +36,7 @@ public: * @param size The number of bytes to write. * @return the number of bytes written actually. */ - si64 write(ui8 * data, si64 size) override; + si64 write(const ui8 * data, si64 size) override; /** * Reads n bytes from the stream into the data buffer. diff --git a/lib/filesystem/COutputStream.h b/lib/filesystem/COutputStream.h index ba4a70f13..86181a20e 100644 --- a/lib/filesystem/COutputStream.h +++ b/lib/filesystem/COutputStream.h @@ -30,5 +30,5 @@ public: * @param size The number of bytes to write. * @return the number of bytes written actually. */ - virtual si64 write(ui8 * data, si64 size) = 0; + virtual si64 write(const ui8 * data, si64 size) = 0; }; diff --git a/lib/filesystem/MinizipExtensions.cpp b/lib/filesystem/MinizipExtensions.cpp new file mode 100644 index 000000000..3c8f86ee6 --- /dev/null +++ b/lib/filesystem/MinizipExtensions.cpp @@ -0,0 +1,106 @@ +/* + * MinizipExtensions.cpp, part of VCMI engine + * + * Authors: listed in file AUTHORS in main folder + * + * License: GNU General Public License v2.0 or later + * Full text of license available in license.txt file, in main folder + * + */ +#include "StdInc.h" + +#include "MinizipExtensions.h" + +static voidpf ZCALLBACK openFileProxy(voidpf opaque, const void * filename, int mode) +{ + assert(opaque != nullptr); + + std::string filename_s; + + if(filename != nullptr) + filename_s = (const char *)filename; + + return ((CIOApi *)opaque)->openFile(filename_s, mode); +} + +static uLong ZCALLBACK readFileProxy(voidpf opaque, voidpf stream, void * buf, uLong size) +{ + assert(opaque != nullptr); + assert(stream != nullptr); + + CInputOutputStream * actualStream = static_cast(stream); + + return actualStream->read((ui8 *)buf, size); +} + +static uLong ZCALLBACK writeFileProxy(voidpf opaque, voidpf stream, const void * buf, uLong size) +{ + assert(opaque != nullptr); + assert(stream != nullptr); + + CInputOutputStream * actualStream = static_cast(stream); + return (uLong)actualStream->write((const ui8 *)buf, size); +} + +static ZPOS64_T ZCALLBACK tellFileProxy(voidpf opaque, voidpf stream) +{ + assert(opaque != nullptr); + assert(stream != nullptr); + + CInputOutputStream * actualStream = static_cast(stream); + return actualStream->tell(); +} + +static long ZCALLBACK seekFileProxy(voidpf opaque, voidpf stream, ZPOS64_T offset, int origin) +{ + assert(opaque != nullptr); + assert(stream != nullptr); + + CInputOutputStream * actualStream = static_cast(stream); + + long ret = 0; + switch (origin) + { + case ZLIB_FILEFUNC_SEEK_CUR : + actualStream->skip(offset);//TODO: should we check actual skipped? + break; + case ZLIB_FILEFUNC_SEEK_END : + ret = -1; + break; + case ZLIB_FILEFUNC_SEEK_SET : + ret = actualStream->seek(offset); + break; + default: ret = -1; + } + return ret; +} + +static int ZCALLBACK closeFileProxy(voidpf opaque, voidpf stream) +{ + assert(opaque != nullptr); + assert(stream != nullptr); + + CInputOutputStream * actualStream = static_cast(stream); + + delete actualStream; + + return 0; +} + +static int ZCALLBACK errorFileProxy(voidpf opaque, voidpf stream) +{ + return 0; +} + +void CIOApi::fillApiStructure(zlib_filefunc64_def & api) +{ + api.opaque = this; + api.zopen64_file = &openFileProxy; + api.zread_file = &readFileProxy; + api.zwrite_file = &writeFileProxy; + api.ztell64_file = &tellFileProxy; + api.zseek64_file = &seekFileProxy; + api.zclose_file = &closeFileProxy; + api.zerror_file = &errorFileProxy; +} + diff --git a/lib/filesystem/MinizipExtensions.h b/lib/filesystem/MinizipExtensions.h new file mode 100644 index 000000000..f5289e906 --- /dev/null +++ b/lib/filesystem/MinizipExtensions.h @@ -0,0 +1,34 @@ +#pragma once + +/* + * MinizipExtensions.h, part of VCMI engine + * + * Authors: listed in file AUTHORS in main folder + * + * License: GNU General Public License v2.0 or later + * Full text of license available in license.txt file, in main folder + * + */ + +#ifdef USE_SYSTEM_MINIZIP +#include +#include +#include +#else +#include "../minizip/unzip.h" +#include "../minizip/zip.h" +#include "../minizip/ioapi.h" +#endif + +#include "CInputOutputStream.h" + +class CIOApi +{ +public: + virtual ~CIOApi(){}; + + void fillApiStructure(zlib_filefunc64_def & api); + + virtual CInputOutputStream * openFile(const std::string & filename, int mode) = 0; + +};