mirror of
https://github.com/SpartanJ/eepp.git
synced 2026-06-02 11:36:30 +03:00
Added support for Zip files and fixed some minor bugs on cPak.
This commit is contained in:
13
Makefile
13
Makefile
@@ -1,5 +1,5 @@
|
||||
ifeq ($(DEBUGBUILD), yes)
|
||||
DEBUGFLAGS = -g -DDEBUG
|
||||
DEBUGFLAGS = -g -DDEBUG -DEE_DEBUG
|
||||
else
|
||||
DEBUGFLAGS = -O2 -DNDEBUG
|
||||
endif
|
||||
@@ -36,6 +36,7 @@ SRCSDLTTF = $(wildcard ./src/helper/SDL_ttf/*.c)
|
||||
SRCSOIL = $(wildcard ./src/helper/SOIL/*.c)
|
||||
SRCFE = $(wildcard ./src/helper/fastevents/*.c)
|
||||
SRCSTBVORBIS = $(wildcard ./src/helper/stb_vorbis/*.c)
|
||||
SRCZIPUTILS = $(wildcard ./src/helper/zip_utils/*.cpp)
|
||||
|
||||
SRCAUDIO = $(wildcard ./src/audio/*.cpp)
|
||||
SRCGAMING = $(wildcard ./src/gaming/*.cpp)
|
||||
@@ -54,6 +55,7 @@ OBJFE = $(SRCFE:.c=.o)
|
||||
OBJSDLTTF = $(SRCSDLTTF:.c=.o)
|
||||
OBJSOIL = $(SRCSOIL:.c=.o)
|
||||
OBJSTBVORBIS = $(SRCSTBVORBIS:.c=.o)
|
||||
OBJZIPUTILS = $(SRCZIPUTILS:.cpp=.o)
|
||||
|
||||
OBJAUDIO = $(SRCAUDIO:.cpp=.o)
|
||||
OBJGAMING = $(SRCGAMING:.cpp=.o)
|
||||
@@ -64,7 +66,7 @@ OBJUI = $(SRCUI:.cpp=.o)
|
||||
OBJUTILS = $(SRCUTILS:.cpp=.o)
|
||||
OBJWINDOW = $(SRCWINDOW:.cpp=.o)
|
||||
|
||||
OBJHELPERS = $(OBJGLEW) $(OBJFE) $(OBJSDLTTF) $(OBJSOIL) $(OBJSTBVORBIS)
|
||||
OBJHELPERS = $(OBJGLEW) $(OBJFE) $(OBJSDLTTF) $(OBJSOIL) $(OBJSTBVORBIS) $(OBJZIPUTILS)
|
||||
OBJMODULES = $(OBJUTILS) $(OBJMATH) $(OBJSYSTEM) $(OBJAUDIO) $(OBJWINDOW) $(OBJGRAPHICS) $(OBJGAMING) $(OBJUI)
|
||||
|
||||
OBJTEST = $(SRCTEST:.cpp=.o)
|
||||
@@ -88,10 +90,10 @@ libeepp-s.a: $(OBJHELPERS) $(OBJMODULES)
|
||||
libeepp.so: $(OBJHELPERS) $(OBJMODULES)
|
||||
$(CPP) $(LDFLAGS) -Wl,-soname,$(LIB).$(VERSION) -o $(LIBNAME) $(OBJHELPERS) $(OBJMODULES) -lfreetype -lSDL -lsndfile -lopenal -lGL -lGLU
|
||||
|
||||
$(OBJMODULES): %.o: %.cpp
|
||||
$(OBJMODULES) $(OBJZIPUTILS): %.o: %.cpp
|
||||
$(CPP) -o $@ -c $< $(CFLAGS) -I/usr/include/freetype2
|
||||
|
||||
$(OBJHELPERS): %.o: %.c
|
||||
$(OBJGLEW) $(OBJFE) $(OBJSDLTTF) $(OBJSOIL) $(OBJSTBVORBIS): %.o: %.c
|
||||
$(CC) -o $@ -c $< $(CFLAGSEXT) -DSTBI_FAILURE_USERMSG -I/usr/include/freetype2
|
||||
|
||||
test: $(EXE)
|
||||
@@ -115,6 +117,9 @@ docs:
|
||||
|
||||
clean:
|
||||
@rm -rf $(OBJHELPERS) $(OBJMODULES) $(OBJTEST) $(OBJEEIV)
|
||||
|
||||
cleantemp:
|
||||
@rm -rf $(OBJMODULES) $(OBJTEST) $(OBJEEIV)
|
||||
|
||||
cleanall: clean
|
||||
@rm -rf $(LIBNAME)
|
||||
|
||||
@@ -150,6 +150,10 @@
|
||||
<Option compilerVar="CC" />
|
||||
</Unit>
|
||||
<Unit filename="src/helper/stb_vorbis/stb_vorbis.h" />
|
||||
<Unit filename="src/helper/zip_utils/unzip.cpp" />
|
||||
<Unit filename="src/helper/zip_utils/unzip.h" />
|
||||
<Unit filename="src/helper/zip_utils/zip.cpp" />
|
||||
<Unit filename="src/helper/zip_utils/zip.h" />
|
||||
<Unit filename="src/math/base.hpp" />
|
||||
<Unit filename="src/math/math.cpp" />
|
||||
<Unit filename="src/math/math.hpp" />
|
||||
@@ -172,6 +176,8 @@
|
||||
<Unit filename="src/system/ctimeelapsed.hpp" />
|
||||
<Unit filename="src/system/ctimer.cpp" />
|
||||
<Unit filename="src/system/ctimer.hpp" />
|
||||
<Unit filename="src/system/czip.cpp" />
|
||||
<Unit filename="src/system/czip.hpp" />
|
||||
<Unit filename="src/system/singleton.hpp" />
|
||||
<Unit filename="src/test/ee.cpp" />
|
||||
<Unit filename="src/ui/base.hpp" />
|
||||
|
||||
1
src/ee.h
1
src/ee.h
@@ -54,6 +54,7 @@
|
||||
#include "system/cinifile.hpp"
|
||||
#include "system/cpack.hpp"
|
||||
#include "system/cpak.hpp"
|
||||
#include "system/czip.hpp"
|
||||
#include "system/crc4.hpp"
|
||||
using namespace EE::System;
|
||||
|
||||
|
||||
@@ -19,7 +19,7 @@ cConsole::~cConsole() {
|
||||
mCallbacks.clear();
|
||||
mCmdLog.clear();
|
||||
mLastCommands.clear();
|
||||
|
||||
|
||||
if ( mMyCallback && NULL != cInput::Instance() )
|
||||
cInput::Instance()->PopCallback( mMyCallback );
|
||||
}
|
||||
@@ -76,9 +76,9 @@ void cConsole::PrivCreate( const bool& MakeDefaultCommands, const eeRGBA& Consol
|
||||
mTBuf.Start();
|
||||
mTBuf.SupportNewLine( false );
|
||||
mTBuf.Active( false );
|
||||
|
||||
|
||||
mCon.ConModif = 0;
|
||||
|
||||
|
||||
CmdGetLog();
|
||||
}
|
||||
|
||||
@@ -124,9 +124,9 @@ void cConsole::Draw() {
|
||||
for (eeInt i = mCon.ConMax - mCon.ConModif; i >= mCon.ConMin - mCon.ConModif; i-- ) {
|
||||
if ( i < static_cast<Int16>( mCmdLog.size() ) && i >= 0 ) {
|
||||
CurY = mTempY + mY + mCurHeight - Pos * mFontSize - mFontSize * 2;
|
||||
|
||||
|
||||
mFont->Draw( mCmdLog[i], mFontSize, CurY );
|
||||
|
||||
|
||||
Pos++;
|
||||
}
|
||||
}
|
||||
@@ -146,7 +146,7 @@ void cConsole::Draw() {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if ( mShowFps ) {
|
||||
mFont->Color( eeColorA () );
|
||||
mFont->SetText( L"FPS: " + toWStr( cEngine::instance()->FPS() ) );
|
||||
@@ -212,41 +212,41 @@ void cConsole::PushText( const std::string& str ) {
|
||||
|
||||
void cConsole::PushText( const char* format, ... ) {
|
||||
char buf[256];
|
||||
|
||||
|
||||
va_list( args );
|
||||
|
||||
|
||||
va_start( args, format );
|
||||
|
||||
|
||||
#ifdef EE_COMPILER_MSVC
|
||||
int nb = _vsnprintf_s( buf, 256, 256, format, args );
|
||||
#else
|
||||
int nb = vsnprintf(buf, 256, format, args);
|
||||
#endif
|
||||
|
||||
|
||||
va_end( args );
|
||||
|
||||
|
||||
if ( nb < 256 ) {
|
||||
PrivPushText( toWStr( std::string( buf ) ) );
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
// The static size was not big enough, try again with a dynamic allocation.
|
||||
++nb;
|
||||
|
||||
|
||||
char * buf2 = new char[nb];
|
||||
|
||||
|
||||
va_start( args, format );
|
||||
|
||||
|
||||
#ifdef EE_COMPILER_MSVC
|
||||
_vsnprintf_s( buf2, nb, nb, format, args );
|
||||
#else
|
||||
vsnprintf( buf2, nb, format, args );
|
||||
#endif
|
||||
|
||||
|
||||
va_end( args );
|
||||
|
||||
|
||||
PrivPushText( toWStr( std::string( buf2 ) ) );
|
||||
|
||||
|
||||
delete [] buf2;
|
||||
}
|
||||
|
||||
@@ -318,7 +318,7 @@ void cConsole::PrivInputCallback( EE_Event* Event ) {
|
||||
PushText( tVec[i] );
|
||||
}
|
||||
tStr = tVec[ tVec.size() - 1 ];
|
||||
|
||||
|
||||
if ( (eeUint)mTBuf.CurPos() != mTBuf.Buffer().size() ) {
|
||||
std::wstring part1 = mTBuf.Buffer().substr( 0, mTBuf.CurPos() );
|
||||
std::wstring part2 = mTBuf.Buffer().substr( mTBuf.CurPos(), mTBuf.Buffer().size()-mTBuf.CurPos() );
|
||||
@@ -349,7 +349,7 @@ void cConsole::PrivInputCallback( EE_Event* Event ) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if ( Event->key.keysym.sym == SDLK_PAGEUP ) {
|
||||
if ( mCon.ConMin - mCon.ConModif > 0 )
|
||||
mCon.ConModif++;
|
||||
@@ -359,7 +359,7 @@ void cConsole::PrivInputCallback( EE_Event* Event ) {
|
||||
if ( mCon.ConModif > 0 )
|
||||
mCon.ConModif--;
|
||||
}
|
||||
|
||||
|
||||
if ( Event->key.keysym.sym == SDLK_HOME ) {
|
||||
Int16 LinesInScreen = static_cast<Int16> ( (mCurHeight / mFontSize) - 1 );
|
||||
if ( static_cast<Int16>( mCmdLog.size() ) > LinesInScreen )
|
||||
@@ -457,12 +457,12 @@ void cConsole::CmdGetTextureMemory ( const std::vector < std::wstring >& params
|
||||
std::wstring size = L" bytes";
|
||||
eeDouble mem = static_cast<eeDouble>( cTextureFactory::instance()->MemorySize() );
|
||||
Uint8 c = 0;
|
||||
|
||||
|
||||
while ( mem > 1024 ) {
|
||||
c++;
|
||||
mem = mem / 1024;
|
||||
}
|
||||
|
||||
|
||||
switch (c) {
|
||||
case 1: size = L" KB"; break;
|
||||
case 2: size = L" MB"; break;
|
||||
@@ -470,7 +470,7 @@ void cConsole::CmdGetTextureMemory ( const std::vector < std::wstring >& params
|
||||
case 4: size = L" TB"; break;
|
||||
default: size = L" WTF";
|
||||
}
|
||||
|
||||
|
||||
PushText( L"Total texture memory used: "+ toWStr( mem ) + size );
|
||||
}
|
||||
|
||||
@@ -577,7 +577,7 @@ void cConsole::CmdDir( const std::vector < std::wstring >& params ) {
|
||||
#endif
|
||||
std::string myPath = wstringTostring( params[1] );
|
||||
std::string myOrder;
|
||||
|
||||
|
||||
if ( params.size() > 2 ) {
|
||||
for ( eeUint i = 2; i < params.size(); i++ ) {
|
||||
if ( i + 1 == params.size() ) {
|
||||
@@ -590,19 +590,19 @@ void cConsole::CmdDir( const std::vector < std::wstring >& params ) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if ( IsDirectory( myPath ) ) {
|
||||
eeUint i;
|
||||
|
||||
std::vector<std::string> mFiles = GetFilesInPath( myPath );
|
||||
|
||||
std::vector<std::string> mFiles = FilesGetInPath( myPath );
|
||||
std::sort( mFiles.begin(), mFiles.end() );
|
||||
|
||||
|
||||
PushText( "Directory: " + myPath );
|
||||
|
||||
|
||||
if ( myOrder == "ff" ) {
|
||||
std::vector<std::string> mFolders;
|
||||
std::vector<std::string> mFile;
|
||||
|
||||
|
||||
for ( i = 0; i < mFiles.size(); i++ ) {
|
||||
if ( IsDirectory( myPath + Slash + mFiles[i] ) ) {
|
||||
mFolders.push_back( mFiles[i] );
|
||||
@@ -613,16 +613,16 @@ void cConsole::CmdDir( const std::vector < std::wstring >& params ) {
|
||||
|
||||
if ( mFolders.size() )
|
||||
PushText( L"Folders: " );
|
||||
|
||||
|
||||
for ( i = 0; i < mFolders.size(); i++ )
|
||||
PushText( " " + mFolders[i] );
|
||||
|
||||
|
||||
if ( mFolders.size() )
|
||||
PushText( L"Files: " );
|
||||
|
||||
|
||||
for ( i = 0; i < mFile.size(); i++ )
|
||||
PushText( " " + mFile[i] );
|
||||
|
||||
|
||||
} else {
|
||||
for ( i = 0; i < mFiles.size(); i++ )
|
||||
PushText( " " + mFiles[i] );
|
||||
|
||||
4329
src/helper/zip_utils/unzip.cpp
Normal file
4329
src/helper/zip_utils/unzip.cpp
Normal file
File diff suppressed because it is too large
Load Diff
227
src/helper/zip_utils/unzip.h
Normal file
227
src/helper/zip_utils/unzip.h
Normal file
@@ -0,0 +1,227 @@
|
||||
#ifndef _unzip_H
|
||||
#define _unzip_H
|
||||
//
|
||||
#define ZIP_STD
|
||||
#ifdef ZIP_STD
|
||||
#include <time.h>
|
||||
#define DECLARE_HANDLE(name) struct name##__ { int unused; }; typedef struct name##__ *name
|
||||
#ifndef MAX_PATH
|
||||
#define MAX_PATH 1024
|
||||
#endif
|
||||
typedef unsigned long DWORD;
|
||||
typedef char TCHAR;
|
||||
typedef FILE* HANDLE;
|
||||
typedef time_t FILETIME;
|
||||
#endif
|
||||
|
||||
// UNZIPPING functions -- for unzipping.
|
||||
// This file is a repackaged form of extracts from the zlib code available
|
||||
// at www.gzip.org/zlib, by Jean-Loup Gailly and Mark Adler. The original
|
||||
// copyright notice may be found in unzip.cpp. The repackaging was done
|
||||
// by Lucian Wischik to simplify and extend its use in Windows/C++. Also
|
||||
// encryption and unicode filenames have been added.
|
||||
|
||||
|
||||
#ifndef _zip_H
|
||||
DECLARE_HANDLE(HZIP);
|
||||
#endif
|
||||
// An HZIP identifies a zip file that has been opened
|
||||
|
||||
typedef DWORD ZRESULT;
|
||||
// return codes from any of the zip functions. Listed later.
|
||||
|
||||
typedef struct
|
||||
{ int index; // index of this file within the zip
|
||||
TCHAR name[MAX_PATH]; // filename within the zip
|
||||
DWORD attr; // attributes, as in GetFileAttributes.
|
||||
FILETIME atime,ctime,mtime;// access, create, modify filetimes
|
||||
long comp_size; // sizes of item, compressed and uncompressed. These
|
||||
long unc_size; // may be -1 if not yet known (e.g. being streamed in)
|
||||
} ZIPENTRY;
|
||||
|
||||
|
||||
HZIP OpenZip(const TCHAR *fn, const char *password);
|
||||
HZIP OpenZip(void *z,unsigned int len, const char *password);
|
||||
HZIP OpenZipHandle(HANDLE h, const char *password);
|
||||
// OpenZip - opens a zip file and returns a handle with which you can
|
||||
// subsequently examine its contents. You can open a zip file from:
|
||||
// from a pipe: OpenZipHandle(hpipe_read,0);
|
||||
// from a file (by handle): OpenZipHandle(hfile,0);
|
||||
// from a file (by name): OpenZip("c:\\test.zip","password");
|
||||
// from a memory block: OpenZip(bufstart, buflen,0);
|
||||
// If the file is opened through a pipe, then items may only be
|
||||
// accessed in increasing order, and an item may only be unzipped once,
|
||||
// although GetZipItem can be called immediately before and after unzipping
|
||||
// it. If it's opened in any other way, then full random access is possible.
|
||||
// Note: pipe input is not yet implemented.
|
||||
// Note: zip passwords are ascii, not unicode.
|
||||
// Note: for windows-ce, you cannot close the handle until after CloseZip.
|
||||
// but for real windows, the zip makes its own copy of your handle, so you
|
||||
// can close yours anytime.
|
||||
|
||||
ZRESULT GetZipItem(HZIP hz, int index, ZIPENTRY *ze);
|
||||
// GetZipItem - call this to get information about an item in the zip.
|
||||
// If index is -1 and the file wasn't opened through a pipe,
|
||||
// then it returns information about the whole zipfile
|
||||
// (and in particular ze.index returns the number of index items).
|
||||
// Note: the item might be a directory (ze.attr & FILE_ATTRIBUTE_DIRECTORY)
|
||||
// See below for notes on what happens when you unzip such an item.
|
||||
// Note: if you are opening the zip through a pipe, then random access
|
||||
// is not possible and GetZipItem(-1) fails and you can't discover the number
|
||||
// of items except by calling GetZipItem on each one of them in turn,
|
||||
// starting at 0, until eventually the call fails. Also, in the event that
|
||||
// you are opening through a pipe and the zip was itself created into a pipe,
|
||||
// then then comp_size and sometimes unc_size as well may not be known until
|
||||
// after the item has been unzipped.
|
||||
|
||||
ZRESULT FindZipItem(HZIP hz, const TCHAR *name, bool ic, int *index, ZIPENTRY *ze);
|
||||
// FindZipItem - finds an item by name. ic means 'insensitive to case'.
|
||||
// It returns the index of the item, and returns information about it.
|
||||
// If nothing was found, then index is set to -1 and the function returns
|
||||
// an error code.
|
||||
|
||||
ZRESULT UnzipItem(HZIP hz, int index, const TCHAR *fn);
|
||||
ZRESULT UnzipItem(HZIP hz, int index, void *z,unsigned int len);
|
||||
ZRESULT UnzipItemHandle(HZIP hz, int index, HANDLE h);
|
||||
// UnzipItem - given an index to an item, unzips it. You can unzip to:
|
||||
// to a pipe: UnzipItemHandle(hz,i, hpipe_write);
|
||||
// to a file (by handle): UnzipItemHandle(hz,i, hfile);
|
||||
// to a file (by name): UnzipItem(hz,i, ze.name);
|
||||
// to a memory block: UnzipItem(hz,i, buf,buflen);
|
||||
// In the final case, if the buffer isn't large enough to hold it all,
|
||||
// then the return code indicates that more is yet to come. If it was
|
||||
// large enough, and you want to know precisely how big, GetZipItem.
|
||||
// Note: zip files are normally stored with relative pathnames. If you
|
||||
// unzip with ZIP_FILENAME a relative pathname then the item gets created
|
||||
// relative to the current directory - it first ensures that all necessary
|
||||
// subdirectories have been created. Also, the item may itself be a directory.
|
||||
// If you unzip a directory with ZIP_FILENAME, then the directory gets created.
|
||||
// If you unzip it to a handle or a memory block, then nothing gets created
|
||||
// and it emits 0 bytes.
|
||||
ZRESULT SetUnzipBaseDir(HZIP hz, const TCHAR *dir);
|
||||
// if unzipping to a filename, and it's a relative filename, then it will be relative to here.
|
||||
// (defaults to current-directory).
|
||||
|
||||
|
||||
ZRESULT CloseZip(HZIP hz);
|
||||
// CloseZip - the zip handle must be closed with this function.
|
||||
|
||||
unsigned int FormatZipMessage(ZRESULT code, TCHAR *buf,unsigned int len);
|
||||
// FormatZipMessage - given an error code, formats it as a string.
|
||||
// It returns the length of the error message. If buf/len points
|
||||
// to a real buffer, then it also writes as much as possible into there.
|
||||
|
||||
|
||||
// These are the result codes:
|
||||
#define ZR_OK 0x00000000 // nb. the pseudo-code zr-recent is never returned,
|
||||
#define ZR_RECENT 0x00000001 // but can be passed to FormatZipMessage.
|
||||
// The following come from general system stuff (e.g. files not openable)
|
||||
#define ZR_GENMASK 0x0000FF00
|
||||
#define ZR_NODUPH 0x00000100 // couldn't duplicate the handle
|
||||
#define ZR_NOFILE 0x00000200 // couldn't create/open the file
|
||||
#define ZR_NOALLOC 0x00000300 // failed to allocate some resource
|
||||
#define ZR_WRITE 0x00000400 // a general error writing to the file
|
||||
#define ZR_NOTFOUND 0x00000500 // couldn't find that file in the zip
|
||||
#define ZR_MORE 0x00000600 // there's still more data to be unzipped
|
||||
#define ZR_CORRUPT 0x00000700 // the zipfile is corrupt or not a zipfile
|
||||
#define ZR_READ 0x00000800 // a general error reading the file
|
||||
#define ZR_PASSWORD 0x00001000 // we didn't get the right password to unzip the file
|
||||
// The following come from mistakes on the part of the caller
|
||||
#define ZR_CALLERMASK 0x00FF0000
|
||||
#define ZR_ARGS 0x00010000 // general mistake with the arguments
|
||||
#define ZR_NOTMMAP 0x00020000 // tried to ZipGetMemory, but that only works on mmap zipfiles, which yours wasn't
|
||||
#define ZR_MEMSIZE 0x00030000 // the memory size is too small
|
||||
#define ZR_FAILED 0x00040000 // the thing was already failed when you called this function
|
||||
#define ZR_ENDED 0x00050000 // the zip creation has already been closed
|
||||
#define ZR_MISSIZE 0x00060000 // the indicated input file size turned out mistaken
|
||||
#define ZR_PARTIALUNZ 0x00070000 // the file had already been partially unzipped
|
||||
#define ZR_ZMODE 0x00080000 // tried to mix creating/opening a zip
|
||||
// The following come from bugs within the zip library itself
|
||||
#define ZR_BUGMASK 0xFF000000
|
||||
#define ZR_NOTINITED 0x01000000 // initialisation didn't work
|
||||
#define ZR_SEEK 0x02000000 // trying to seek in an unseekable file
|
||||
#define ZR_NOCHANGE 0x04000000 // changed its mind on storage, but not allowed
|
||||
#define ZR_FLATE 0x05000000 // an internal error in the de/inflation code
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
// e.g.
|
||||
//
|
||||
// SetCurrentDirectory("c:\\docs\\stuff");
|
||||
// HZIP hz = OpenZip("c:\\stuff.zip",0);
|
||||
// ZIPENTRY ze; GetZipItem(hz,-1,&ze); int numitems=ze.index;
|
||||
// for (int i=0; i<numitems; i++)
|
||||
// { GetZipItem(hz,i,&ze);
|
||||
// UnzipItem(hz,i,ze.name);
|
||||
// }
|
||||
// CloseZip(hz);
|
||||
//
|
||||
//
|
||||
// HRSRC hrsrc = FindResource(hInstance,MAKEINTRESOURCE(1),RT_RCDATA);
|
||||
// HANDLE hglob = LoadResource(hInstance,hrsrc);
|
||||
// void *zipbuf=LockResource(hglob);
|
||||
// unsigned int ziplen=SizeofResource(hInstance,hrsrc);
|
||||
// HZIP hz = OpenZip(zipbuf, ziplen, 0);
|
||||
// - unzip to a membuffer -
|
||||
// ZIPENTRY ze; int i; FindZipItem(hz,"file.dat",true,&i,&ze);
|
||||
// char *ibuf = new char[ze.unc_size];
|
||||
// UnzipItem(hz,i, ibuf, ze.unc_size);
|
||||
// delete[] ibuf;
|
||||
// - unzip to a fixed membuff -
|
||||
// ZIPENTRY ze; int i; FindZipItem(hz,"file.dat",true,&i,&ze);
|
||||
// char ibuf[1024]; ZRESULT zr=ZR_MORE; unsigned long totsize=0;
|
||||
// while (zr==ZR_MORE)
|
||||
// { zr = UnzipItem(hz,i, ibuf,1024);
|
||||
// unsigned long bufsize=1024; if (zr==ZR_OK) bufsize=ze.unc_size-totsize;
|
||||
// totsize+=bufsize;
|
||||
// }
|
||||
// - unzip to a pipe -
|
||||
// HANDLE hwrite; HANDLE hthread=CreateWavReaderThread(&hwrite);
|
||||
// int i; ZIPENTRY ze; FindZipItem(hz,"sound.wav",true,&i,&ze);
|
||||
// UnzipItemHandle(hz,i, hwrite);
|
||||
// CloseHandle(hwrite);
|
||||
// WaitForSingleObject(hthread,INFINITE);
|
||||
// CloseHandle(hwrite); CloseHandle(hthread);
|
||||
// - finished -
|
||||
// CloseZip(hz);
|
||||
// // note: no need to free resources obtained through Find/Load/LockResource
|
||||
//
|
||||
//
|
||||
// SetCurrentDirectory("c:\\docs\\pipedzipstuff");
|
||||
// HANDLE hread,hwrite; CreatePipe(&hread,&hwrite,0,0);
|
||||
// CreateZipWriterThread(hwrite);
|
||||
// HZIP hz = OpenZipHandle(hread,0);
|
||||
// for (int i=0; ; i++)
|
||||
// { ZIPENTRY ze;
|
||||
// ZRESULT zr=GetZipItem(hz,i,&ze); if (zr!=ZR_OK) break; // no more
|
||||
// UnzipItem(hz,i, ze.name);
|
||||
// }
|
||||
// CloseZip(hz);
|
||||
//
|
||||
//
|
||||
|
||||
|
||||
|
||||
|
||||
// Now we indulge in a little skullduggery so that the code works whether
|
||||
// the user has included just zip or both zip and unzip.
|
||||
// Idea: if header files for both zip and unzip are present, then presumably
|
||||
// the cpp files for zip and unzip are both present, so we will call
|
||||
// one or the other of them based on a dynamic choice. If the header file
|
||||
// for only one is present, then we will bind to that particular one.
|
||||
ZRESULT CloseZipU(HZIP hz);
|
||||
unsigned int FormatZipMessageU(ZRESULT code, TCHAR *buf,unsigned int len);
|
||||
bool IsZipHandleU(HZIP hz);
|
||||
#ifdef _zip_H
|
||||
#undef CloseZip
|
||||
#define CloseZip(hz) (IsZipHandleU(hz)?CloseZipU(hz):CloseZipZ(hz))
|
||||
#else
|
||||
#define CloseZip CloseZipU
|
||||
#define FormatZipMessage FormatZipMessageU
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
#endif // _unzip_H
|
||||
2991
src/helper/zip_utils/zip.cpp
Normal file
2991
src/helper/zip_utils/zip.cpp
Normal file
File diff suppressed because it is too large
Load Diff
215
src/helper/zip_utils/zip.h
Normal file
215
src/helper/zip_utils/zip.h
Normal file
@@ -0,0 +1,215 @@
|
||||
#ifndef _zip_H
|
||||
#define _zip_H
|
||||
//
|
||||
#define ZIP_STD
|
||||
#ifdef ZIP_STD
|
||||
#include <time.h>
|
||||
#define DECLARE_HANDLE(name) struct name##__ { int unused; }; typedef struct name##__ *name
|
||||
#ifndef MAX_PATH
|
||||
#define MAX_PATH 1024
|
||||
#endif
|
||||
typedef unsigned long DWORD;
|
||||
typedef char TCHAR;
|
||||
typedef FILE* HANDLE;
|
||||
typedef time_t FILETIME;
|
||||
#endif
|
||||
|
||||
// ZIP functions -- for creating zip files
|
||||
// This file is a repackaged form of the Info-Zip source code available
|
||||
// at www.info-zip.org. The original copyright notice may be found in
|
||||
// zip.cpp. The repackaging was done by Lucian Wischik to simplify and
|
||||
// extend its use in Windows/C++. Also to add encryption and unicode.
|
||||
|
||||
|
||||
#ifndef _unzip_H
|
||||
DECLARE_HANDLE(HZIP);
|
||||
#endif
|
||||
// An HZIP identifies a zip file that is being created
|
||||
|
||||
typedef DWORD ZRESULT;
|
||||
// return codes from any of the zip functions. Listed later.
|
||||
|
||||
|
||||
|
||||
HZIP CreateZip(const TCHAR *fn, const char *password);
|
||||
HZIP CreateZip(void *buf,unsigned int len, const char *password);
|
||||
HZIP CreateZipHandle(HANDLE h, const char *password);
|
||||
// CreateZip - call this to start the creation of a zip file.
|
||||
// As the zip is being created, it will be stored somewhere:
|
||||
// to a pipe: CreateZipHandle(hpipe_write);
|
||||
// in a file (by handle): CreateZipHandle(hfile);
|
||||
// in a file (by name): CreateZip("c:\\test.zip");
|
||||
// in memory: CreateZip(buf, len);
|
||||
// or in pagefile memory: CreateZip(0, len);
|
||||
// The final case stores it in memory backed by the system paging file,
|
||||
// where the zip may not exceed len bytes. This is a bit friendlier than
|
||||
// allocating memory with new[]: it won't lead to fragmentation, and the
|
||||
// memory won't be touched unless needed. That means you can give very
|
||||
// large estimates of the maximum-size without too much worry.
|
||||
// As for the password, it lets you encrypt every file in the archive.
|
||||
// (This api doesn't support per-file encryption.)
|
||||
// Note: because pipes don't allow random access, the structure of a zipfile
|
||||
// created into a pipe is slightly different from that created into a file
|
||||
// or memory. In particular, the compressed-size of the item cannot be
|
||||
// stored in the zipfile until after the item itself. (Also, for an item added
|
||||
// itself via a pipe, the uncompressed-size might not either be known until
|
||||
// after.) This is not normally a problem. But if you try to unzip via a pipe
|
||||
// as well, then the unzipper will not know these things about the item until
|
||||
// after it has been unzipped. Therefore: for unzippers which don't just write
|
||||
// each item to disk or to a pipe, but instead pre-allocate memory space into
|
||||
// which to unzip them, then either you have to create the zip not to a pipe,
|
||||
// or you have to add items not from a pipe, or at least when adding items
|
||||
// from a pipe you have to specify the length.
|
||||
// Note: for windows-ce, you cannot close the handle until after CloseZip.
|
||||
// but for real windows, the zip makes its own copy of your handle, so you
|
||||
// can close yours anytime.
|
||||
|
||||
|
||||
ZRESULT ZipAdd(HZIP hz,const TCHAR *dstzn, const TCHAR *fn);
|
||||
ZRESULT ZipAdd(HZIP hz,const TCHAR *dstzn, void *src,unsigned int len);
|
||||
ZRESULT ZipAddHandle(HZIP hz,const TCHAR *dstzn, HANDLE h);
|
||||
ZRESULT ZipAddHandle(HZIP hz,const TCHAR *dstzn, HANDLE h, unsigned int len);
|
||||
ZRESULT ZipAddFolder(HZIP hz,const TCHAR *dstzn);
|
||||
// ZipAdd - call this for each file to be added to the zip.
|
||||
// dstzn is the name that the file will be stored as in the zip file.
|
||||
// The file to be added to the zip can come
|
||||
// from a pipe: ZipAddHandle(hz,"file.dat", hpipe_read);
|
||||
// from a file: ZipAddHandle(hz,"file.dat", hfile);
|
||||
// from a filen: ZipAdd(hz,"file.dat", "c:\\docs\\origfile.dat");
|
||||
// from memory: ZipAdd(hz,"subdir\\file.dat", buf,len);
|
||||
// (folder): ZipAddFolder(hz,"subdir");
|
||||
// Note: if adding an item from a pipe, and if also creating the zip file itself
|
||||
// to a pipe, then you might wish to pass a non-zero length to the ZipAddHandle
|
||||
// function. This will let the zipfile store the item's size ahead of the
|
||||
// compressed item itself, which in turn makes it easier when unzipping the
|
||||
// zipfile from a pipe.
|
||||
|
||||
ZRESULT ZipGetMemory(HZIP hz, void **buf, unsigned long *len);
|
||||
// ZipGetMemory - If the zip was created in memory, via ZipCreate(0,len),
|
||||
// then this function will return information about that memory block.
|
||||
// buf will receive a pointer to its start, and len its length.
|
||||
// Note: you can't add any more after calling this.
|
||||
|
||||
ZRESULT CloseZip(HZIP hz);
|
||||
// CloseZip - the zip handle must be closed with this function.
|
||||
|
||||
unsigned int FormatZipMessage(ZRESULT code, TCHAR *buf,unsigned int len);
|
||||
// FormatZipMessage - given an error code, formats it as a string.
|
||||
// It returns the length of the error message. If buf/len points
|
||||
// to a real buffer, then it also writes as much as possible into there.
|
||||
|
||||
|
||||
|
||||
// These are the result codes:
|
||||
#define ZR_OK 0x00000000 // nb. the pseudo-code zr-recent is never returned,
|
||||
#define ZR_RECENT 0x00000001 // but can be passed to FormatZipMessage.
|
||||
// The following come from general system stuff (e.g. files not openable)
|
||||
#define ZR_GENMASK 0x0000FF00
|
||||
#define ZR_NODUPH 0x00000100 // couldn't duplicate the handle
|
||||
#define ZR_NOFILE 0x00000200 // couldn't create/open the file
|
||||
#define ZR_NOALLOC 0x00000300 // failed to allocate some resource
|
||||
#define ZR_WRITE 0x00000400 // a general error writing to the file
|
||||
#define ZR_NOTFOUND 0x00000500 // couldn't find that file in the zip
|
||||
#define ZR_MORE 0x00000600 // there's still more data to be unzipped
|
||||
#define ZR_CORRUPT 0x00000700 // the zipfile is corrupt or not a zipfile
|
||||
#define ZR_READ 0x00000800 // a general error reading the file
|
||||
// The following come from mistakes on the part of the caller
|
||||
#define ZR_CALLERMASK 0x00FF0000
|
||||
#define ZR_ARGS 0x00010000 // general mistake with the arguments
|
||||
#define ZR_NOTMMAP 0x00020000 // tried to ZipGetMemory, but that only works on mmap zipfiles, which yours wasn't
|
||||
#define ZR_MEMSIZE 0x00030000 // the memory size is too small
|
||||
#define ZR_FAILED 0x00040000 // the thing was already failed when you called this function
|
||||
#define ZR_ENDED 0x00050000 // the zip creation has already been closed
|
||||
#define ZR_MISSIZE 0x00060000 // the indicated input file size turned out mistaken
|
||||
#define ZR_PARTIALUNZ 0x00070000 // the file had already been partially unzipped
|
||||
#define ZR_ZMODE 0x00080000 // tried to mix creating/opening a zip
|
||||
// The following come from bugs within the zip library itself
|
||||
#define ZR_BUGMASK 0xFF000000
|
||||
#define ZR_NOTINITED 0x01000000 // initialisation didn't work
|
||||
#define ZR_SEEK 0x02000000 // trying to seek in an unseekable file
|
||||
#define ZR_NOCHANGE 0x04000000 // changed its mind on storage, but not allowed
|
||||
#define ZR_FLATE 0x05000000 // an internal error in the de/inflation code
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
// e.g.
|
||||
//
|
||||
// (1) Traditional use, creating a zipfile from existing files
|
||||
// HZIP hz = CreateZip("c:\\simple1.zip",0);
|
||||
// ZipAdd(hz,"znsimple.bmp", "c:\\simple.bmp");
|
||||
// ZipAdd(hz,"znsimple.txt", "c:\\simple.txt");
|
||||
// CloseZip(hz);
|
||||
//
|
||||
// (2) Memory use, creating an auto-allocated mem-based zip file from various sources
|
||||
// HZIP hz = CreateZip(0,100000, 0);
|
||||
// // adding a conventional file...
|
||||
// ZipAdd(hz,"src1.txt", "c:\\src1.txt");
|
||||
// // adding something from memory...
|
||||
// char buf[1000]; for (int i=0; i<1000; i++) buf[i]=(char)(i&0x7F);
|
||||
// ZipAdd(hz,"file.dat", buf,1000);
|
||||
// // adding something from a pipe...
|
||||
// HANDLE hread,hwrite; CreatePipe(&hread,&hwrite,NULL,0);
|
||||
// HANDLE hthread = CreateThread(0,0,ThreadFunc,(void*)hwrite,0,0);
|
||||
// ZipAdd(hz,"unz3.dat", hread,1000); // the '1000' is optional.
|
||||
// WaitForSingleObject(hthread,INFINITE);
|
||||
// CloseHandle(hthread); CloseHandle(hread);
|
||||
// ... meanwhile DWORD WINAPI ThreadFunc(void *dat)
|
||||
// { HANDLE hwrite = (HANDLE)dat;
|
||||
// char buf[1000]={17};
|
||||
// DWORD writ; WriteFile(hwrite,buf,1000,&writ,NULL);
|
||||
// CloseHandle(hwrite);
|
||||
// return 0;
|
||||
// }
|
||||
// // and now that the zip is created, let's do something with it:
|
||||
// void *zbuf; unsigned long zlen; ZipGetMemory(hz,&zbuf,&zlen);
|
||||
// HANDLE hfz = CreateFile("test2.zip",GENERIC_WRITE,0,0,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,0);
|
||||
// DWORD writ; WriteFile(hfz,zbuf,zlen,&writ,NULL);
|
||||
// CloseHandle(hfz);
|
||||
// CloseZip(hz);
|
||||
//
|
||||
// (3) Handle use, for file handles and pipes
|
||||
// HANDLE hzread,hzwrite; CreatePipe(&hzread,&hzwrite,0,0);
|
||||
// HANDLE hthread = CreateThread(0,0,ZipReceiverThread,(void*)hzread,0,0);
|
||||
// HZIP hz = CreateZipHandle(hzwrite,0);
|
||||
// // ... add to it
|
||||
// CloseZip(hz);
|
||||
// CloseHandle(hzwrite);
|
||||
// WaitForSingleObject(hthread,INFINITE);
|
||||
// CloseHandle(hthread);
|
||||
// ... meanwhile DWORD WINAPI ZipReceiverThread(void *dat)
|
||||
// { HANDLE hread = (HANDLE)dat;
|
||||
// char buf[1000];
|
||||
// while (true)
|
||||
// { DWORD red; ReadFile(hread,buf,1000,&red,NULL);
|
||||
// // ... and do something with this zip data we're receiving
|
||||
// if (red==0) break;
|
||||
// }
|
||||
// CloseHandle(hread);
|
||||
// return 0;
|
||||
// }
|
||||
|
||||
|
||||
|
||||
// Now we indulge in a little skullduggery so that the code works whether
|
||||
// the user has included just zip or both zip and unzip.
|
||||
// Idea: if header files for both zip and unzip are present, then presumably
|
||||
// the cpp files for zip and unzip are both present, so we will call
|
||||
// one or the other of them based on a dynamic choice. If the header file
|
||||
// for only one is present, then we will bind to that particular one.
|
||||
ZRESULT CloseZipZ(HZIP hz);
|
||||
unsigned int FormatZipMessageZ(ZRESULT code, char *buf,unsigned int len);
|
||||
bool IsZipHandleZ(HZIP hz);
|
||||
#ifdef _unzip_H
|
||||
#undef CloseZip
|
||||
#define CloseZip(hz) (IsZipHandleZ(hz)?CloseZipZ(hz):CloseZipU(hz))
|
||||
#else
|
||||
#define CloseZip CloseZipZ
|
||||
#define FormatZipMessage FormatZipMessageZ
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
#endif
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
namespace EE { namespace System {
|
||||
|
||||
cPack::cPack() :
|
||||
cPack::cPack() :
|
||||
mIsOpen(false)
|
||||
{
|
||||
}
|
||||
@@ -14,4 +14,4 @@ bool cPack::IsOpen() const {
|
||||
return mIsOpen;
|
||||
}
|
||||
|
||||
}}
|
||||
}}
|
||||
|
||||
@@ -9,55 +9,56 @@ namespace EE { namespace System {
|
||||
class EE_API cPack {
|
||||
public:
|
||||
cPack();
|
||||
|
||||
|
||||
virtual ~cPack();
|
||||
|
||||
|
||||
/** Creates a new pack file */
|
||||
virtual bool Create( const std::string& path ) = 0;
|
||||
|
||||
|
||||
/** Open a pack file */
|
||||
virtual bool Open( const std::string& path ) = 0;
|
||||
|
||||
|
||||
/** Close the pack file */
|
||||
virtual bool Close() = 0;
|
||||
|
||||
|
||||
/** Add a file to the pack file
|
||||
* @param path Path to the file in the disk
|
||||
* @param inpack Path that will have the file inside the pak
|
||||
* @return True if success
|
||||
*/
|
||||
virtual bool AddFile( const std::string& path, const std::string& inpack ) = 0;
|
||||
|
||||
|
||||
/** Add a map of files to the pack file ( myMap[ myFilepath ] = myInPackFilepath ) */
|
||||
virtual bool AddFiles( std::map<std::string, std::string> paths ) = 0;
|
||||
|
||||
|
||||
/** Erase a file from the pack file. ( This will create a new pack file without that file, so, can be slow ) */
|
||||
virtual bool EraseFile( const std::string& path ) = 0;
|
||||
|
||||
|
||||
/** Erase all passed files from the pack file. ( This will create a new pack file without that file, so, can be slow ) */
|
||||
virtual bool EraseFiles( const std::vector<std::string>& paths ) = 0;
|
||||
|
||||
|
||||
/** Extract a file from the pack file */
|
||||
virtual bool ExtractFile( const std::string& path , const std::string& dest ) = 0;
|
||||
|
||||
|
||||
/** Extract a file to memory from the pack file */
|
||||
virtual bool ExtractFileToMemory( const std::string& path, std::vector<Uint8>& data ) = 0;
|
||||
|
||||
|
||||
/** Check if a file exists in the pack file and return the number of the file, otherwise return -1. */
|
||||
virtual Int32 Exists( const std::string& path ) = 0;
|
||||
|
||||
|
||||
/** Check the integrity of the pack file. \n If return 0 integrity OK. -1 wrong indentifier. -2 wrong header. */
|
||||
virtual Int8 CheckPack() = 0;
|
||||
|
||||
|
||||
/** @return a vector with all the files inside the pack file */
|
||||
virtual std::vector<std::string> GetFileList() = 0;
|
||||
|
||||
|
||||
/** @return If the pack file is open */
|
||||
virtual bool IsOpen() const;
|
||||
|
||||
/** Add a new file from memory */
|
||||
virtual bool AddFile( std::vector<Uint8>& data, const std::string& inpack ) = 0;
|
||||
protected:
|
||||
bool mIsOpen;
|
||||
|
||||
virtual bool GetFile( const std::string& path, std::vector<Uint8>& data ) = 0;
|
||||
};
|
||||
|
||||
}}
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
namespace EE { namespace System {
|
||||
|
||||
cPak::cPak() :
|
||||
cPak::cPak() :
|
||||
cPack()
|
||||
{
|
||||
}
|
||||
@@ -32,7 +32,10 @@ bool cPak::Create( const std::string& path ) {
|
||||
Open( path );
|
||||
|
||||
return true;
|
||||
} else {
|
||||
return Open( path );
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -67,9 +70,9 @@ bool cPak::Close() {
|
||||
myPak.fs.close();
|
||||
|
||||
pakFiles.clear();
|
||||
|
||||
|
||||
mIsOpen = false;
|
||||
|
||||
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
@@ -123,24 +126,23 @@ bool cPak::ExtractFileToMemory( const std::string& path, std::vector<Uint8>& dat
|
||||
return false;
|
||||
}
|
||||
|
||||
bool cPak::AddFile( const std::string& path, const std::string& inpack ) {
|
||||
if ( path.size() > 56 )
|
||||
bool cPak::AddFile( std::vector<Uint8>& data, const std::string& inpack ) {
|
||||
if ( data.size() < 1 )
|
||||
return false;
|
||||
|
||||
Uint32 fsize = FileSize( path );
|
||||
Uint32 fsize = data.size();
|
||||
|
||||
if ( myPak.fs.is_open() && FileExists( path ) ) {
|
||||
if ( myPak.fs.is_open() ) {
|
||||
if ( myPak.header.dir_length == 1 ) {
|
||||
myPak.header.dir_offset = sizeof(pakHeader) + fsize;
|
||||
myPak.header.dir_length = sizeof(pakEntry);
|
||||
myPak.pakFilesNum = 1;
|
||||
|
||||
myPak.fs.seekg( 4 , ios::beg ); // seek after head (PACK)
|
||||
myPak.fs.write( reinterpret_cast<const char*> (&myPak.header.dir_offset), sizeof( myPak.header.dir_offset ) );
|
||||
myPak.fs.write( reinterpret_cast<const char*> (&myPak.header.dir_length), sizeof( myPak.header.dir_length ) );
|
||||
|
||||
std::vector<Uint8> file;
|
||||
GetFile( path, file );
|
||||
myPak.fs.write( reinterpret_cast<const char*> (&file[0]), fsize );
|
||||
myPak.fs.write( reinterpret_cast<const char*> (&data[0]), fsize );
|
||||
|
||||
pakEntry newFile;
|
||||
StrCopy( newFile.filename, inpack.c_str(), 56 );
|
||||
@@ -149,7 +151,9 @@ bool cPak::AddFile( const std::string& path, const std::string& inpack ) {
|
||||
|
||||
myPak.fs.write( reinterpret_cast<const char*> (&newFile), sizeof( pakEntry ) );
|
||||
|
||||
file.clear();
|
||||
pakFiles.push_back( newFile );
|
||||
|
||||
data.clear();
|
||||
|
||||
return true;
|
||||
} else {
|
||||
@@ -172,15 +176,12 @@ bool cPak::AddFile( const std::string& path, const std::string& inpack ) {
|
||||
myPak.fs.write( reinterpret_cast<const char*> (&myPak.header.dir_offset), sizeof( myPak.header.dir_offset ) );
|
||||
myPak.fs.write( reinterpret_cast<const char*> (&myPak.header.dir_length), sizeof( myPak.header.dir_length ) );
|
||||
|
||||
std::vector<Uint8> file;
|
||||
GetFile( path, file );
|
||||
|
||||
myPak.fs.seekg( (myPak.header.dir_offset - fsize), ios::beg ); // Seek to the file allocation zone
|
||||
myPak.fs.write( reinterpret_cast<const char*> (&file[0]), fsize ); // Alloc the file
|
||||
myPak.fs.write( reinterpret_cast<const char*> (&data[0]), fsize ); // Alloc the file
|
||||
|
||||
// Fill the new file data on the pakEntry
|
||||
StrCopy (pakE[ myPak.pakFilesNum ].filename, inpack.c_str(), 56 );
|
||||
|
||||
|
||||
pakE[ myPak.pakFilesNum ].file_position = myPak.header.dir_offset - fsize;
|
||||
pakE[ myPak.pakFilesNum ].file_length = fsize;
|
||||
|
||||
@@ -190,7 +191,7 @@ bool cPak::AddFile( const std::string& path, const std::string& inpack ) {
|
||||
pakFiles.push_back( pakE[ myPak.pakFilesNum ] );
|
||||
myPak.pakFilesNum += 1;
|
||||
|
||||
file.clear();
|
||||
data.clear();
|
||||
pakE.clear();
|
||||
|
||||
return true;
|
||||
@@ -199,6 +200,17 @@ bool cPak::AddFile( const std::string& path, const std::string& inpack ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
bool cPak::AddFile( const std::string& path, const std::string& inpack ) {
|
||||
if ( path.size() > 56 )
|
||||
return false;
|
||||
|
||||
std::vector<Uint8> file;
|
||||
|
||||
FileGet( path, file );
|
||||
|
||||
return AddFile( file, inpack );
|
||||
}
|
||||
|
||||
bool cPak::AddFiles( std::map<std::string, std::string> paths ) {
|
||||
for( std::map<std::string, std::string>::iterator itr = paths.begin(); itr != paths.end(); itr++)
|
||||
if ( !AddFile( itr->first, itr->second ) )
|
||||
@@ -206,23 +218,6 @@ bool cPak::AddFiles( std::map<std::string, std::string> paths ) {
|
||||
return true;
|
||||
}
|
||||
|
||||
bool cPak::GetFile( const std::string& path, std::vector<Uint8>& data ) {
|
||||
if ( FileExists( path ) ) {
|
||||
std::fstream fs ( path.c_str() , std::ios::in | std::ios::binary );
|
||||
Uint32 fsize = FileSize( path );
|
||||
|
||||
data.clear();
|
||||
data.resize( fsize );
|
||||
|
||||
fs.read( reinterpret_cast<char*> (&data[0]), fsize );
|
||||
|
||||
fs.close();
|
||||
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool cPak::EraseFile( const std::string& path ) {
|
||||
std::vector<std::string> tmpv;
|
||||
tmpv.push_back( path );
|
||||
|
||||
@@ -12,76 +12,77 @@ class EE_API cPak : public cPack {
|
||||
public:
|
||||
cPak();
|
||||
~cPak();
|
||||
|
||||
|
||||
/** Creates a new pakFile */
|
||||
bool Create( const std::string& path );
|
||||
|
||||
|
||||
/** Open a pakFile */
|
||||
bool Open( const std::string& path );
|
||||
|
||||
|
||||
/** Close the pakFile */
|
||||
bool Close();
|
||||
|
||||
|
||||
/** Add a file to the pakFile
|
||||
* @param path Path to the file in the disk
|
||||
* @param inpack Path that will have the file inside the pak
|
||||
* @return True if success
|
||||
*/
|
||||
bool AddFile( const std::string& path, const std::string& inpack );
|
||||
|
||||
|
||||
/** Add a new file from memory */
|
||||
bool AddFile( std::vector<Uint8>& data, const std::string& inpack );
|
||||
|
||||
/** Add a map of files to the pakFile ( myMap[ myFilepath ] = myInPakFilepath ) */
|
||||
bool AddFiles( std::map<std::string, std::string> paths );
|
||||
|
||||
|
||||
/** Erase a file from the pakFile. ( This will create a new pakFile without that file, so, can be slow ) */
|
||||
bool EraseFile( const std::string& path );
|
||||
|
||||
|
||||
/** Erase all passed files from the pakFile. ( This will create a new pakFile without that file, so, can be slow ) */
|
||||
bool EraseFiles( const std::vector<std::string>& paths );
|
||||
|
||||
|
||||
/** Extract a file from the pakFile */
|
||||
bool ExtractFile( const std::string& path , const std::string& dest );
|
||||
|
||||
|
||||
/** Extract a file to memory from the pakFile */
|
||||
bool ExtractFileToMemory( const std::string& path, std::vector<Uint8>& data );
|
||||
|
||||
|
||||
/** Check if a file exists in the pakFile and return the number of the file, otherwise return -1. */
|
||||
Int32 Exists( const std::string& path );
|
||||
|
||||
|
||||
/** Check the integrity of the pakFile. \n If return 0 integrity OK. -1 wrong indentifier. -2 wrong header. */
|
||||
Int8 CheckPack();
|
||||
|
||||
|
||||
/** @return a vector with all the files inside the pakFile */
|
||||
std::vector<std::string> GetFileList();
|
||||
|
||||
|
||||
/** @return If the PAK file is open */
|
||||
bool IsOpen() const { return mIsOpen; }
|
||||
protected:
|
||||
|
||||
|
||||
private:
|
||||
typedef struct pakheader_t {
|
||||
char head[4]; //! Header of the file ( default: 'PACK' )
|
||||
Uint32 dir_offset; //! Offset to the first pakEntry on the pakFile
|
||||
Uint32 dir_length; //! Space ocuped by all the pakEntrys ( num of pakEntrys = dir_length / sizeof(pakEntry) )
|
||||
} pakHeader; //! The header of the file
|
||||
|
||||
|
||||
typedef struct dirsection_t {
|
||||
char filename[56]; //! File name
|
||||
Uint32 file_position; //! The file position on the file ( in bytes )
|
||||
Uint32 file_length; //! THe file length ( in bytes )
|
||||
} pakEntry; //! The stored file info
|
||||
|
||||
|
||||
typedef struct pakfile_t {
|
||||
std::fstream fs;
|
||||
pakHeader header;
|
||||
Uint32 pakFilesNum;
|
||||
std::string pakPath;
|
||||
} pakFile;
|
||||
|
||||
|
||||
pakFile myPak;
|
||||
|
||||
|
||||
std::vector<pakEntry> pakFiles;
|
||||
|
||||
bool GetFile( const std::string& path, std::vector<Uint8>& data );
|
||||
};
|
||||
|
||||
}}
|
||||
|
||||
271
src/system/czip.cpp
Normal file
271
src/system/czip.cpp
Normal file
@@ -0,0 +1,271 @@
|
||||
#include "czip.hpp"
|
||||
|
||||
namespace EE { namespace System {
|
||||
|
||||
cZip::cZip() :
|
||||
mZip(NULL)
|
||||
{
|
||||
}
|
||||
|
||||
cZip::~cZip() {
|
||||
Close();
|
||||
}
|
||||
|
||||
bool cZip::Create( const std::string& path ) {
|
||||
if ( !FileExists(path) ) {
|
||||
mZip = CreateZip( path.c_str(), 0 );
|
||||
|
||||
if ( 0 == CheckPack() ) {
|
||||
mZipPath = path;
|
||||
|
||||
mState = ZIP_CREATED;
|
||||
|
||||
mIsOpen = true;
|
||||
|
||||
return true;
|
||||
}
|
||||
} else {
|
||||
return Open( path );
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
bool cZip::Open( const std::string& path ) {
|
||||
if ( FileExists(path) ) {
|
||||
mZip = OpenZip( path.c_str(), 0 );
|
||||
|
||||
if ( 0 == CheckPack() ) {
|
||||
ZIPENTRY ze;
|
||||
|
||||
GetZipItem( mZip, -1 , &ze );
|
||||
|
||||
Uint32 numitems = ze.index;
|
||||
|
||||
for ( Uint32 zi = 0; zi < numitems; zi++ ) {
|
||||
GetZipItem( mZip, zi, &ze );
|
||||
|
||||
ZIPENTRY zet = ze;
|
||||
|
||||
zipFiles.push_back( zet );
|
||||
}
|
||||
|
||||
mZipPath = path;
|
||||
|
||||
mState = ZIP_OPEN;
|
||||
|
||||
mIsOpen = true;
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
bool cZip::Close() {
|
||||
if ( 0 == CheckPack() ) {
|
||||
CloseZip( mZip );
|
||||
|
||||
zipFiles.clear();
|
||||
|
||||
mIsOpen = false;
|
||||
|
||||
mZipPath = "";
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
bool cZip::AddFile( const std::string& path, const std::string& inpack ) {
|
||||
std::vector<Uint8> file;
|
||||
|
||||
FileGet( path, file );
|
||||
|
||||
return AddFile( file, inpack );
|
||||
}
|
||||
|
||||
bool cZip::AddFile( std::vector<Uint8>& data, const std::string& inpack ) {
|
||||
if ( 0 == CheckPack() ) {
|
||||
if ( ZIP_CREATED == mState ) {
|
||||
Int32 Result = ZipAdd( mZip , inpack.c_str(), reinterpret_cast<void*> (&data[0]), (unsigned int)data.size() );
|
||||
|
||||
if ( ZR_OK == Result )
|
||||
return true;
|
||||
} else {
|
||||
std::string ZipNewPath = mZipPath + ".temp";
|
||||
|
||||
cZip Zip;
|
||||
|
||||
if ( Zip.Create( ZipNewPath ) ) {
|
||||
for ( eeUint i = 0; i < zipFiles.size(); i++ ) {
|
||||
std::vector<Uint8> tdata;
|
||||
|
||||
ExtractFileToMemory( zipFiles[i].name, tdata );
|
||||
Zip.AddFile( tdata, zipFiles[i].name );
|
||||
}
|
||||
|
||||
Zip.AddFile( data, inpack );
|
||||
|
||||
Zip.Close();
|
||||
|
||||
std::string ZPath = mZipPath;
|
||||
|
||||
Close();
|
||||
|
||||
remove( ZPath.c_str() );
|
||||
rename( ZipNewPath.c_str(), ZPath.c_str() );
|
||||
|
||||
Open( ZPath );
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
bool cZip::AddFiles( std::map<std::string, std::string> paths ) {
|
||||
for( std::map<std::string, std::string>::iterator itr = paths.begin(); itr != paths.end(); itr++)
|
||||
if ( !AddFile( itr->first, itr->second ) )
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool cZip::EraseFile( const std::string& path ) {
|
||||
std::vector<std::string> tmpv;
|
||||
tmpv.push_back( path );
|
||||
|
||||
return EraseFiles( tmpv );
|
||||
}
|
||||
|
||||
bool cZip::EraseFiles( const std::vector<std::string>& paths ) {
|
||||
std::vector<Int32> files;
|
||||
Int32 Ex;
|
||||
Uint32 i = 0;
|
||||
std::vector<ZIPENTRY> uEntry;
|
||||
bool Remove;
|
||||
|
||||
ChangeState( ZIP_OPEN );
|
||||
|
||||
for ( i = 0; i < paths.size(); i++ ) {
|
||||
Ex = Exists( paths[i] );
|
||||
|
||||
if ( Ex == -1 )
|
||||
return false;
|
||||
else
|
||||
files.push_back( Ex );
|
||||
}
|
||||
|
||||
for ( i = 0; i < zipFiles.size(); i++ ) {
|
||||
Remove = false;
|
||||
|
||||
for ( Uint32 u = 0; u < files.size(); u++ ) {
|
||||
if ( files[u] == static_cast<Int32>(i) )
|
||||
Remove = true;
|
||||
}
|
||||
|
||||
if ( !Remove ) {
|
||||
uEntry.push_back( zipFiles[i] );
|
||||
}
|
||||
}
|
||||
|
||||
std::string ZipNewPath = mZipPath + ".temp";
|
||||
|
||||
cZip Zip;
|
||||
|
||||
if ( Zip.Create( ZipNewPath ) ) {
|
||||
for ( i = 0; i < uEntry.size(); i++ ) {
|
||||
std::vector<Uint8> data;
|
||||
|
||||
ExtractFileToMemory( uEntry[i].name, data );
|
||||
Zip.AddFile( data, uEntry[i].name );
|
||||
}
|
||||
|
||||
Zip.Close();
|
||||
|
||||
std::string ZPath = mZipPath;
|
||||
|
||||
Close();
|
||||
|
||||
remove( ZPath.c_str() );
|
||||
rename( ZipNewPath.c_str(), ZPath.c_str() );
|
||||
|
||||
Open( ZPath );
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
bool cZip::ExtractFile( const std::string& path , const std::string& dest ) {
|
||||
ChangeState( ZIP_OPEN );
|
||||
|
||||
Int32 index = Exists( path );
|
||||
|
||||
if ( 0 == CheckPack() && -1 != index ) {
|
||||
if ( ZR_OK == UnzipItem( mZip, index, dest.c_str() ) )
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
bool cZip::ExtractFileToMemory( const std::string& path, std::vector<Uint8>& data ) {
|
||||
Int32 Pos = Exists( path );
|
||||
Uint32 Result = 0;
|
||||
|
||||
if ( 0 == CheckPack() && -1 != Pos ) {
|
||||
data.clear();
|
||||
data.resize( zipFiles[Pos].unc_size );
|
||||
|
||||
Result = UnzipItem( mZip, Pos, reinterpret_cast<void*> (&data[0]), (unsigned int)zipFiles[Pos].unc_size );
|
||||
|
||||
if ( ZR_OK == Result ) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
Int32 cZip::Exists( const std::string& path ) {
|
||||
for ( Uint32 i = 0; i < zipFiles.size(); i++ )
|
||||
if ( strcmp( path.c_str(), zipFiles[i].name ) == 0 )
|
||||
return i;
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
Int8 cZip::CheckPack() {
|
||||
return NULL != mZip ? 0 : -1;
|
||||
}
|
||||
|
||||
std::vector<std::string> cZip::GetFileList() {
|
||||
std::vector<std::string> tmpv;
|
||||
|
||||
tmpv.resize( zipFiles.size() );
|
||||
|
||||
for ( Uint32 i = 0; i < zipFiles.size(); i++ )
|
||||
tmpv[i] = std::string ( zipFiles[i].name );
|
||||
|
||||
return tmpv;
|
||||
}
|
||||
|
||||
void cZip::ChangeState( const Uint32& State ) {
|
||||
if ( State == ZIP_OPEN ) {
|
||||
if ( ZIP_CREATED == mState ) {
|
||||
std::string path = mZipPath;
|
||||
Close();
|
||||
Open( path );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}}
|
||||
80
src/system/czip.hpp
Normal file
80
src/system/czip.hpp
Normal file
@@ -0,0 +1,80 @@
|
||||
#ifndef EE_SYSTEMCZIP_HPP
|
||||
#define EE_SYSTEMCZIP_HPP
|
||||
|
||||
#include "base.hpp"
|
||||
#include "cpack.hpp"
|
||||
#include "../helper/zip_utils/unzip.h"
|
||||
#include "../helper/zip_utils/zip.h"
|
||||
|
||||
namespace EE { namespace System {
|
||||
|
||||
/** @brief Base class for al packing classes */
|
||||
class EE_API cZip : public cPack {
|
||||
public:
|
||||
cZip();
|
||||
|
||||
~cZip();
|
||||
|
||||
/** Creates a new pack file */
|
||||
bool Create( const std::string& path );
|
||||
|
||||
/** Open a pack file */
|
||||
bool Open( const std::string& path );
|
||||
|
||||
/** Close the pack file */
|
||||
bool Close();
|
||||
|
||||
/** Add a file to the pack file
|
||||
* @param path Path to the file in the disk
|
||||
* @param inpack Path that will have the file inside the pak
|
||||
* @return True if success
|
||||
*/
|
||||
bool AddFile( const std::string& path, const std::string& inpack );
|
||||
|
||||
/** Add a map of files to the pack file ( myMap[ myFilepath ] = myInPackFilepath ) */
|
||||
bool AddFiles( std::map<std::string, std::string> paths );
|
||||
|
||||
/** Erase a file from the pack file. ( This will create a new pack file without that file, so, can be slow ) */
|
||||
bool EraseFile( const std::string& path );
|
||||
|
||||
/** Erase all passed files from the pack file. ( This will create a new pack file without that file, so, can be slow ) */
|
||||
bool EraseFiles( const std::vector<std::string>& paths );
|
||||
|
||||
/** Extract a file from the pack file */
|
||||
bool ExtractFile( const std::string& path , const std::string& dest );
|
||||
|
||||
/** Extract a file to memory from the pack file */
|
||||
bool ExtractFileToMemory( const std::string& path, std::vector<Uint8>& data );
|
||||
|
||||
/** Check if a file exists in the pack file and return the number of the file, otherwise return -1. */
|
||||
Int32 Exists( const std::string& path );
|
||||
|
||||
/** Check the integrity of the pack file. \n If return 0 integrity OK. -1 wrong indentifier. -2 wrong header. */
|
||||
Int8 CheckPack();
|
||||
|
||||
/** @return a vector with all the files inside the pack file */
|
||||
std::vector<std::string> GetFileList();
|
||||
|
||||
/** Add a new file from memory */
|
||||
bool AddFile( std::vector<Uint8>& data, const std::string& inpack );
|
||||
protected:
|
||||
HZIP mZip;
|
||||
|
||||
std::vector<ZIPENTRY> zipFiles;
|
||||
|
||||
std::string mZipPath;
|
||||
|
||||
Uint32 mState;
|
||||
|
||||
enum ZIP_FILE_STATE {
|
||||
ZIP_CLOSED = 0,
|
||||
ZIP_CREATED,
|
||||
ZIP_OPEN
|
||||
};
|
||||
|
||||
void ChangeState( const Uint32& State = ZIP_OPEN );
|
||||
};
|
||||
|
||||
}}
|
||||
#endif
|
||||
|
||||
@@ -3,7 +3,6 @@
|
||||
/**
|
||||
@TODO Create a basic UI system.
|
||||
@TODO Add some Surface Grid class, to create special effects. ( waved texture, and stuff like that )
|
||||
@TODO Add a generic Pak class (done), and add a cZip class for handling zip files (pending).
|
||||
@TODO Support multitexturing.
|
||||
@TODO Create a asynchronous resource loader - Check if this is posible right now
|
||||
@TODO Support color cursors ( not only black and white cursors, that really sucks ) - Imposible with SDL 1.2
|
||||
@@ -132,7 +131,8 @@ class cEETest : private cThread {
|
||||
void Screen2();
|
||||
void Screen3();
|
||||
|
||||
cPak PAK;
|
||||
cZip PAK;
|
||||
|
||||
std::vector<Uint8> tmpv;
|
||||
std::vector<Uint8> MySong;
|
||||
|
||||
@@ -200,7 +200,7 @@ void cEETest::Init() {
|
||||
|
||||
run = EE->Init(mWidth, mHeight, BitColor, Windowed, Resizeable, VSync);
|
||||
|
||||
PAK.Open( MyPath + "data/ee.pak" );
|
||||
PAK.Open( MyPath + "data/ee.zip" );
|
||||
|
||||
run = ( run && PAK.IsOpen() );
|
||||
|
||||
@@ -266,7 +266,7 @@ void cEETest::Init() {
|
||||
|
||||
Params.Border.Width( 2.f );
|
||||
Params.Border.Color( 0xFF979797 );
|
||||
Params.Background.Corners(5);
|
||||
//Params.Background.Corners(5);
|
||||
Params.Background.Colors( eeColorA( 0x66FAFAFA ), eeColorA( 0xCCFAFAFA ), eeColorA( 0xCCFAFAFA ), eeColorA( 0x66FAFAFA ) );
|
||||
cUIControlAnim * C = new cUITest( Params );
|
||||
C->Visible( true );
|
||||
@@ -322,7 +322,7 @@ void cEETest::Init() {
|
||||
|
||||
cUITextInput::CreateParams InputParams;
|
||||
InputParams.Parent( C );
|
||||
InputParams.Background.Corners(6);
|
||||
//InputParams.Background.Corners(6);
|
||||
InputParams.Border.Color(0xFF979797);
|
||||
InputParams.Background.Colors( eeColorA(0x99AAAAAA), eeColorA(0x99CCCCCC), eeColorA(0x99CCCCCC), eeColorA(0x99AAAAAA) );
|
||||
InputParams.PosSet( 10, 220 );
|
||||
@@ -973,5 +973,6 @@ void cEETest::Particles() {
|
||||
int main (int argc, char * argv []) {
|
||||
cEETest Test;
|
||||
Test.Process();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -115,7 +115,7 @@ std::string AppPath() {
|
||||
#endif
|
||||
}
|
||||
|
||||
std::vector<std::string> GetFilesInPath( const std::string& path ) {
|
||||
std::vector<std::string> FilesGetInPath( const std::string& path ) {
|
||||
std::vector<std::string> files;
|
||||
|
||||
#ifdef EE_COMPILER_MSVC
|
||||
@@ -286,4 +286,39 @@ Uint32 MakeHash( const Int8* str ) {
|
||||
return hash;
|
||||
}
|
||||
|
||||
bool FileGet( const std::string& path, std::vector<Uint8>& data ) {
|
||||
if ( FileExists( path ) ) {
|
||||
std::fstream fs ( path.c_str() , std::ios::in | std::ios::binary );
|
||||
Uint32 fsize = FileSize( path );
|
||||
|
||||
data.clear();
|
||||
data.resize( fsize );
|
||||
|
||||
fs.read( reinterpret_cast<char*> (&data[0]), fsize );
|
||||
|
||||
fs.close();
|
||||
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool FileCopy( const std::string& src, const std::string& dst ) {
|
||||
if ( FileExists( src ) ) {
|
||||
ifstream in( src.c_str() );
|
||||
ofstream out( dst.c_str() );
|
||||
|
||||
if ( in.is_open() && out.is_open() ) {
|
||||
out << in.rdbuf();
|
||||
|
||||
in.close();
|
||||
out.close();
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
}}
|
||||
|
||||
@@ -6,42 +6,56 @@
|
||||
namespace EE { namespace Utils {
|
||||
/** @return True if the file exists */
|
||||
bool EE_API FileExists(const std::string& filepath);
|
||||
|
||||
|
||||
/** @return The number of milliseconds since the EE++ library initialization. Note that this value wraps if the program runs for more than ~49 days. */
|
||||
Uint32 EE_API eeGetTicks();
|
||||
|
||||
|
||||
/** Wait a specified number of milliseconds before returning. */
|
||||
void EE_API eeSleep( const Uint32& ms );
|
||||
|
||||
|
||||
/** @return The application path ( the executable path ) */
|
||||
std::string EE_API AppPath();
|
||||
|
||||
|
||||
/** @return The files and sub directories contained by a directory */
|
||||
std::vector<std::string> EE_API GetFilesInPath( const std::string& path );
|
||||
|
||||
std::vector<std::string> EE_API FilesGetInPath( const std::string& path );
|
||||
|
||||
/** @return The size of a file */
|
||||
Uint32 EE_API FileSize( const std::string& Filepath);
|
||||
|
||||
|
||||
/** @return The System Time */
|
||||
eeDouble EE_API GetSystemTime();
|
||||
|
||||
|
||||
/** @return If directory exists, and is a directory */
|
||||
bool EE_API IsDirectory( const std::string& path );
|
||||
|
||||
|
||||
/** Creates a new directory */
|
||||
bool EE_API MakeDir( const std::string& path, const Uint16& mode = 0770 );
|
||||
|
||||
/** @return The default windows directory */
|
||||
std::string EE_API GetWindowsPath();
|
||||
|
||||
|
||||
/** @return djb2 wstring hash */
|
||||
Uint32 EE_API MakeHash( const std::wstring& str );
|
||||
|
||||
|
||||
/** @return djb2 string hash */
|
||||
Uint32 EE_API MakeHash( const std::string& str );
|
||||
|
||||
|
||||
/** @return djb2 string hash */
|
||||
Uint32 EE_API MakeHash( const Int8 *str );
|
||||
|
||||
/** Copy a file to memory
|
||||
* @param path The file path
|
||||
* @param data The vector to allocate the file in memory
|
||||
* @return True if returned the file to the vector.
|
||||
*/
|
||||
bool FileGet( const std::string& path, std::vector<Uint8>& data );
|
||||
|
||||
/** Copy a file to location.
|
||||
* @param src Source File Path
|
||||
* @param dst Destination File Path
|
||||
* @return If success.
|
||||
*/
|
||||
bool FileCopy( const std::string& src, const std::string& dst );
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user