#define VCMI_DLL #include "../stdafx.h" #include "zlib.h" #include "CLodHandler.h" #include #include #include #include #include "boost/filesystem/operations.hpp" #include #include #include #ifdef max #undef max #endif /* * CLodHandler.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 * */ DLL_EXPORT int readNormalNr (int pos, int bytCon, unsigned char * str) { int ret=0; int amp=1; if (str) { for (int i=0; irealSize; mutex->lock(); fseek(FLOD, ourEntry->offset, 0); unsigned char * outp; if (ourEntry->offset<0) //file is in the sprites/ folder; no compression { unsigned char * outp = new unsigned char[ourEntry->realSize]; char name[30];memset(name,0,30); strcat(name, myDir.c_str()); strcat(name, PATHSEPARATOR); strcat(name,ourEntry->nameStr.c_str()); FILE * f = fopen(name,"rb"); int result = fread(outp,1,ourEntry->realSize,f); mutex->unlock(); if(result<0) {tlog1<<"Error in file reading: "<size==0) //file is not compressed { outp = new unsigned char[ourEntry->realSize]; fread((char*)outp, 1, ourEntry->realSize, FLOD); mutex->unlock(); return outp; } else //we will decompress file { outp = new unsigned char[ourEntry->size]; fread((char*)outp, 1, ourEntry->size, FLOD); mutex->unlock(); unsigned char * decomp = NULL; int decRes = infs2(outp, ourEntry->size, ourEntry->realSize, decomp); delete[] outp; return decomp; } return NULL; } int CLodHandler::infs(unsigned char * in, int size, int realSize, std::ofstream & out, int wBits) { int ret; unsigned have; z_stream strm; unsigned char inx[NLoadHandlerHelp::fCHUNK]; unsigned char outx[NLoadHandlerHelp::fCHUNK]; /* allocate inflate state */ strm.zalloc = Z_NULL; strm.zfree = Z_NULL; strm.opaque = Z_NULL; strm.avail_in = 0; strm.next_in = Z_NULL; ret = inflateInit2(&strm, wBits); if (ret != Z_OK) return ret; int chunkNumber = 0; do { int readBytes = 0; for(int i=0; i=amp/2) { ret = ret-amp; } return ret; } void CLodHandler::init(std::string lodFile, std::string dirName) { myDir = dirName; std::string Ts; FLOD = fopen(lodFile.c_str(), "rb"); if(!FLOD) { tlog1 << "Cannot open " << lodFile << std::endl; return; } fseek(FLOD, 8, 0); unsigned char temp[4]; fread((char*)temp, 1, 4, FLOD); totalFiles = readNormalNr(temp,4); fseek(FLOD, 0x5c, 0); for (int i=0; istatus())) { std::string name = dir->path().leaf(); std::transform(name.begin(), name.end(), name.begin(), (int(*)(int))toupper); boost::algorithm::replace_all(name,".BMP",".PCX"); Entry * e = entries.znajdz(name); if(e) //file present in .lod - overwrite its entry { e->offset = -1; e->realSize = e->size = boost::filesystem::file_size(dir->path()); } else //file not present in lod - add entry for it { Entry e2; e2.offset = -1; e2.nameStr = name; e2.realSize = e2.size = boost::filesystem::file_size(dir->path()); entries.push_back(e2); } } } } else { tlog1<<"Warning: No "+dirName+"/ folder!"<