mirror of
https://github.com/vcmi/vcmi.git
synced 2024-12-26 22:57:00 +02:00
Add support of CIOApi to CZipStream and CZipLoader (unused, untested)
This commit is contained in:
parent
b3ac146ba9
commit
0040b459c3
@ -14,11 +14,15 @@
|
||||
*
|
||||
*/
|
||||
|
||||
CZipStream::CZipStream(const std::string & archive, unz_file_pos filepos)
|
||||
CZipStream::CZipStream(std::shared_ptr<CIOApi> api, const std::string & archive, unz_file_pos filepos)
|
||||
{
|
||||
file = unzOpen(archive.c_str());
|
||||
zlib_filefunc64_def zlibApi;
|
||||
|
||||
zlibApi = api->getApiStructure();
|
||||
|
||||
file = unzOpen2_64(archive.c_str(), &zlibApi);
|
||||
unzGoToFilePos(file, &filepos);
|
||||
unzOpenCurrentFile(file);
|
||||
unzOpenCurrentFile(file);
|
||||
}
|
||||
|
||||
CZipStream::~CZipStream()
|
||||
@ -46,7 +50,9 @@ ui32 CZipStream::calculateCRC32()
|
||||
return info.crc;
|
||||
}
|
||||
|
||||
CZipLoader::CZipLoader(const std::string & mountPoint, const std::string & archive):
|
||||
CZipLoader::CZipLoader(const std::string & mountPoint, const std::string & archive, std::shared_ptr<CIOApi> api):
|
||||
ioApi(api),
|
||||
zlibApi(ioApi->getApiStructure()),
|
||||
archiveName(archive),
|
||||
mountPoint(mountPoint),
|
||||
files(listFiles(mountPoint, archive))
|
||||
@ -58,7 +64,7 @@ std::unordered_map<ResourceID, unz_file_pos> CZipLoader::listFiles(const std::st
|
||||
{
|
||||
std::unordered_map<ResourceID, unz_file_pos> ret;
|
||||
|
||||
unzFile file = unzOpen(archive.c_str());
|
||||
unzFile file = unzOpen2_64(archive.c_str(), &zlibApi);
|
||||
|
||||
if (unzGoToFirstFile(file) == UNZ_OK)
|
||||
{
|
||||
@ -85,7 +91,7 @@ std::unordered_map<ResourceID, unz_file_pos> CZipLoader::listFiles(const std::st
|
||||
|
||||
std::unique_ptr<CInputStream> CZipLoader::load(const ResourceID & resourceName) const
|
||||
{
|
||||
return std::unique_ptr<CInputStream>(new CZipStream(archiveName, files.at(resourceName)));
|
||||
return std::unique_ptr<CInputStream>(new CZipStream(ioApi, archiveName, files.at(resourceName)));
|
||||
}
|
||||
|
||||
bool CZipLoader::existsResource(const ResourceID & resourceName) const
|
||||
|
@ -15,12 +15,7 @@
|
||||
#include "ResourceID.h"
|
||||
#include "CCompressedStream.h"
|
||||
|
||||
// Necessary here in order to get all types
|
||||
#ifdef USE_SYSTEM_MINIZIP
|
||||
#include <minizip/unzip.h>
|
||||
#else
|
||||
#include "../minizip/unzip.h"
|
||||
#endif
|
||||
#include "MinizipExtensions.h"
|
||||
|
||||
class DLL_LINKAGE CZipStream : public CBufferedStream
|
||||
{
|
||||
@ -29,10 +24,12 @@ class DLL_LINKAGE CZipStream : public CBufferedStream
|
||||
public:
|
||||
/**
|
||||
* @brief constructs zip stream from already opened file
|
||||
* @param api virtual filesystem interface
|
||||
* @param archive path to archive to open
|
||||
* @param filepos position of file to open
|
||||
*/
|
||||
CZipStream(const std::string & archive, unz_file_pos filepos);
|
||||
CZipStream(std::shared_ptr<CIOApi> api, const std::string & archive, unz_file_pos filepos);
|
||||
|
||||
~CZipStream();
|
||||
|
||||
si64 getSize() override;
|
||||
@ -44,6 +41,9 @@ protected:
|
||||
|
||||
class DLL_LINKAGE CZipLoader : public ISimpleResourceLoader
|
||||
{
|
||||
std::shared_ptr<CIOApi> ioApi;
|
||||
zlib_filefunc64_def zlibApi;
|
||||
|
||||
std::string archiveName;
|
||||
std::string mountPoint;
|
||||
|
||||
@ -51,8 +51,8 @@ class DLL_LINKAGE CZipLoader : public ISimpleResourceLoader
|
||||
|
||||
std::unordered_map<ResourceID, unz_file_pos> listFiles(const std::string & mountPoint, const std::string &archive);
|
||||
public:
|
||||
CZipLoader(const std::string & mountPoint, const std::string & archive);
|
||||
|
||||
CZipLoader(const std::string & mountPoint, const std::string & archive, std::shared_ptr<CIOApi> api = std::shared_ptr<CIOApi>(new CDefaultIOApi()));
|
||||
|
||||
/// Interface implementation
|
||||
/// @see ISimpleResourceLoader
|
||||
std::unique_ptr<CInputStream> load(const ResourceID & resourceName) const override;
|
||||
|
@ -11,7 +11,7 @@
|
||||
|
||||
#include "MinizipExtensions.h"
|
||||
|
||||
static voidpf ZCALLBACK openFileProxy(voidpf opaque, const void * filename, int mode)
|
||||
voidpf ZCALLBACK CIOApi::openFileProxy(voidpf opaque, const void * filename, int mode)
|
||||
{
|
||||
assert(opaque != nullptr);
|
||||
|
||||
@ -23,7 +23,7 @@ static voidpf ZCALLBACK openFileProxy(voidpf opaque, const void * filename, int
|
||||
return ((CIOApi *)opaque)->openFile(filename_s, mode);
|
||||
}
|
||||
|
||||
static uLong ZCALLBACK readFileProxy(voidpf opaque, voidpf stream, void * buf, uLong size)
|
||||
uLong ZCALLBACK CIOApi::readFileProxy(voidpf opaque, voidpf stream, void * buf, uLong size)
|
||||
{
|
||||
assert(opaque != nullptr);
|
||||
assert(stream != nullptr);
|
||||
@ -33,7 +33,7 @@ static uLong ZCALLBACK readFileProxy(voidpf opaque, voidpf stream, void * buf, u
|
||||
return actualStream->read((ui8 *)buf, size);
|
||||
}
|
||||
|
||||
static uLong ZCALLBACK writeFileProxy(voidpf opaque, voidpf stream, const void * buf, uLong size)
|
||||
uLong ZCALLBACK CIOApi::writeFileProxy(voidpf opaque, voidpf stream, const void * buf, uLong size)
|
||||
{
|
||||
assert(opaque != nullptr);
|
||||
assert(stream != nullptr);
|
||||
@ -42,7 +42,7 @@ static uLong ZCALLBACK writeFileProxy(voidpf opaque, voidpf stream, const void *
|
||||
return (uLong)actualStream->write((const ui8 *)buf, size);
|
||||
}
|
||||
|
||||
static ZPOS64_T ZCALLBACK tellFileProxy(voidpf opaque, voidpf stream)
|
||||
ZPOS64_T ZCALLBACK CIOApi::tellFileProxy(voidpf opaque, voidpf stream)
|
||||
{
|
||||
assert(opaque != nullptr);
|
||||
assert(stream != nullptr);
|
||||
@ -51,7 +51,7 @@ static ZPOS64_T ZCALLBACK tellFileProxy(voidpf opaque, voidpf stream)
|
||||
return actualStream->tell();
|
||||
}
|
||||
|
||||
static long ZCALLBACK seekFileProxy(voidpf opaque, voidpf stream, ZPOS64_T offset, int origin)
|
||||
long ZCALLBACK CIOApi::seekFileProxy(voidpf opaque, voidpf stream, ZPOS64_T offset, int origin)
|
||||
{
|
||||
assert(opaque != nullptr);
|
||||
assert(stream != nullptr);
|
||||
@ -75,7 +75,7 @@ static long ZCALLBACK seekFileProxy(voidpf opaque, voidpf stream, ZPOS64_T offs
|
||||
return ret;
|
||||
}
|
||||
|
||||
static int ZCALLBACK closeFileProxy(voidpf opaque, voidpf stream)
|
||||
int ZCALLBACK CIOApi::closeFileProxy(voidpf opaque, voidpf stream)
|
||||
{
|
||||
assert(opaque != nullptr);
|
||||
assert(stream != nullptr);
|
||||
@ -87,14 +87,16 @@ static int ZCALLBACK closeFileProxy(voidpf opaque, voidpf stream)
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int ZCALLBACK errorFileProxy(voidpf opaque, voidpf stream)
|
||||
int ZCALLBACK CIOApi::errorFileProxy(voidpf opaque, voidpf stream)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
void CIOApi::fillApiStructure(zlib_filefunc64_def & api)
|
||||
///CIOApi
|
||||
zlib_filefunc64_def CIOApi::getApiStructure() const
|
||||
{
|
||||
api.opaque = this;
|
||||
zlib_filefunc64_def api;
|
||||
api.opaque = (void *) this;
|
||||
api.zopen64_file = &openFileProxy;
|
||||
api.zread_file = &readFileProxy;
|
||||
api.zwrite_file = &writeFileProxy;
|
||||
@ -102,5 +104,40 @@ void CIOApi::fillApiStructure(zlib_filefunc64_def & api)
|
||||
api.zseek64_file = &seekFileProxy;
|
||||
api.zclose_file = &closeFileProxy;
|
||||
api.zerror_file = &errorFileProxy;
|
||||
|
||||
return api;
|
||||
}
|
||||
|
||||
CDefaultIOApi::CDefaultIOApi()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
CDefaultIOApi::~CDefaultIOApi()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
zlib_filefunc64_def CDefaultIOApi::getApiStructure() const
|
||||
{
|
||||
zlib_filefunc64_def api;
|
||||
|
||||
fill_fopen64_filefunc(&api);
|
||||
|
||||
return api;
|
||||
}
|
||||
|
||||
CInputOutputStream * CDefaultIOApi::openFile(const std::string& filename, int mode) const
|
||||
{
|
||||
throw new std::runtime_error("CDefaultIOApi::openFile call not expected.");
|
||||
}
|
||||
|
||||
CZipArchive::CZipArchive(const CIOApi* api)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
CZipArchive::~CZipArchive()
|
||||
{
|
||||
|
||||
}
|
||||
|
@ -22,13 +22,45 @@
|
||||
|
||||
#include "CInputOutputStream.h"
|
||||
|
||||
class CIOApi
|
||||
class DLL_LINKAGE CIOApi
|
||||
{
|
||||
public:
|
||||
virtual ~CIOApi(){};
|
||||
|
||||
void fillApiStructure(zlib_filefunc64_def & api);
|
||||
|
||||
virtual CInputOutputStream * openFile(const std::string & filename, int mode) = 0;
|
||||
virtual zlib_filefunc64_def getApiStructure() const;
|
||||
|
||||
protected:
|
||||
virtual CInputOutputStream * openFile(const std::string & filename, int mode) const = 0;
|
||||
|
||||
private:
|
||||
static voidpf ZCALLBACK openFileProxy(voidpf opaque, const void * filename, int mode);
|
||||
static uLong ZCALLBACK readFileProxy(voidpf opaque, voidpf stream, void * buf, uLong size);
|
||||
static uLong ZCALLBACK writeFileProxy(voidpf opaque, voidpf stream, const void * buf, uLong size);
|
||||
static ZPOS64_T ZCALLBACK tellFileProxy(voidpf opaque, voidpf stream);
|
||||
static long ZCALLBACK seekFileProxy(voidpf opaque, voidpf stream, ZPOS64_T offset, int origin);
|
||||
static int ZCALLBACK closeFileProxy(voidpf opaque, voidpf stream);
|
||||
static int ZCALLBACK errorFileProxy(voidpf opaque, voidpf stream);
|
||||
};
|
||||
|
||||
|
||||
class DLL_LINKAGE CDefaultIOApi: public CIOApi
|
||||
{
|
||||
public:
|
||||
CDefaultIOApi();
|
||||
~CDefaultIOApi();
|
||||
|
||||
zlib_filefunc64_def getApiStructure() const override;
|
||||
|
||||
protected:
|
||||
CInputOutputStream * openFile(const std::string & filename, int mode) const override;
|
||||
};
|
||||
|
||||
class CZipArchive
|
||||
{
|
||||
public:
|
||||
explicit CZipArchive(const CIOApi * api);
|
||||
virtual ~CZipArchive();
|
||||
|
||||
private:
|
||||
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user