2009-04-16 14:14:13 +03:00
# ifndef __CLODHANDLER_H__
# define __CLODHANDLER_H__
# include "../global.h"
# include <vector>
# include <string>
2010-11-09 13:27:58 +02:00
# include <fstream>
# include <set>
# include <map>
2011-01-14 18:52:37 +02:00
# include <boost/unordered_set.hpp>
2009-04-16 14:14:13 +03:00
2009-04-15 17:03:31 +03:00
/*
* CLodhandler . 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
*
2009-04-16 14:14:13 +03:00
*/
2009-05-26 07:52:34 +03:00
namespace boost
2009-10-04 04:31:14 +03:00
{ class mutex ; }
2009-04-16 14:14:13 +03:00
namespace NLoadHandlerHelp
{
const int dmHelp = 0 , dmNoExtractingMask = 1 ;
//std::string P1,P2,CurDir;
const int fCHUNK = 50000 ;
2009-10-04 04:31:14 +03:00
}
2009-04-16 14:14:13 +03:00
2009-10-11 02:25:34 +03:00
struct LodEntry {
char filename [ 16 ] ;
2010-07-16 17:22:18 +03:00
ui32 offset ; /* little endian */
ui32 uncompressedSize ; /* little endian */
ui32 unused ; /* little endian */
ui32 size ; /* little endian */
2009-10-11 02:25:34 +03:00
} ;
2010-02-09 15:48:14 +02:00
DLL_EXPORT int readNormalNr ( const unsigned char * bufor , int pos , int bytCon = 4 , bool cyclic = false ) ;
DLL_EXPORT char readChar ( const unsigned char * bufor , int & i ) ;
DLL_EXPORT std : : string readString ( const unsigned char * bufor , int & i ) ;
2010-11-09 13:27:58 +02:00
enum LodFileType {
FILE_ANY ,
FILE_TEXT ,
FILE_ANIMATION ,
FILE_MASK ,
FILE_CAMPAIGN ,
FILE_MAP ,
FILE_FONT ,
FILE_GRAPHICS ,
FILE_OTHER
} ;
2011-01-14 18:52:37 +02:00
2009-04-16 14:14:13 +03:00
struct Entry
{
2009-08-02 18:18:35 +03:00
// Info extracted from LOD file
2010-11-09 13:27:58 +02:00
std : : string name ,
realName ; //for external files - case\extension may not match
2009-04-16 14:14:13 +03:00
int offset , //from beginning
realSize , //size without compression
size ; //and with
2010-11-09 13:27:58 +02:00
LodFileType type ; // file type determined by extension
2009-08-02 18:18:35 +03:00
2010-11-09 13:27:58 +02:00
bool operator = = ( const Entry & comp ) const
2009-04-16 14:14:13 +03:00
{
2010-11-09 13:27:58 +02:00
return ( type = = comp . type | | comp . type = = FILE_ANY ) & & name = = comp . name ;
2009-04-16 14:14:13 +03:00
}
2010-11-09 13:27:58 +02:00
Entry ( std : : string con , LodFileType TYPE ) : name ( con ) , type ( TYPE ) { } ;
Entry ( std : : string con ) : name ( con ) { } ;
2009-04-16 14:14:13 +03:00
Entry ( ) { } ;
} ;
2009-06-26 18:41:19 +03:00
2011-01-14 18:52:37 +02:00
template < >
struct boost : : hash < Entry > : public std : : unary_function < Entry , std : : size_t > {
private :
boost : : hash < std : : string > stringHasher ;
public :
std : : size_t operator ( ) ( Entry const & en ) const
{
//do NOT improve this hash function as we need same-name hash collisions for find to work properly
return stringHasher ( en . name ) ;
}
} ;
2009-06-26 18:41:19 +03:00
class DLL_EXPORT CLodHandler
2009-04-16 14:14:13 +03:00
{
2010-11-09 13:27:58 +02:00
std : : map < std : : string , LodFileType > extMap ; // to convert extensions to file type
2009-10-11 02:25:34 +03:00
std : : ifstream LOD ;
2009-04-16 14:14:13 +03:00
unsigned int totalFiles ;
2009-05-26 07:52:34 +03:00
boost : : mutex * mutex ;
2009-04-16 14:14:13 +03:00
std : : string myDir ; //load files from this dir instead of .lod file
2010-11-09 13:27:58 +02:00
void initEntry ( Entry & e , const std : : string name ) ;
int infs2 ( unsigned char * in , int size , int realSize , unsigned char * & out , int wBits = 15 ) ; //zlib fast handler
2009-10-11 02:46:42 +03:00
public :
2010-11-09 13:27:58 +02:00
2011-01-14 18:52:37 +02:00
boost : : unordered_set < Entry > entries ;
2009-10-11 02:46:42 +03:00
2009-06-23 11:14:49 +03:00
CLodHandler ( ) ;
~ CLodHandler ( ) ;
2010-11-09 13:27:58 +02:00
void init ( const std : : string lodFile , const std : : string dirName ) ;
unsigned char * giveFile ( const std : : string defName , LodFileType type = FILE_ANY , int * length = NULL ) ; //returns pointer to the decompressed data - it must be deleted when no longer needed!
bool haveFile ( const std : : string name , LodFileType type = FILE_ANY ) ; //check if file is present in lod
std : : string getTextFile ( const std : : string name , LodFileType type = FILE_TEXT ) ; //extracts one file
void extractFile ( const std : : string FName , const std : : string name ) ; //extracts a specific file
2010-02-09 15:48:14 +02:00
static unsigned char * getUnpackedFile ( const std : : string & path , int * sizeOut ) ; //loads given file, decompresses and returns
2009-04-16 14:14:13 +03:00
} ;
# endif // __CLODHANDLER_H__