mirror of
https://github.com/vcmi/vcmi.git
synced 2025-06-17 00:07:41 +02:00
* reading of .h3c headers
* a bit of readNormalNr deduplication * minor changes
This commit is contained in:
68
lib/map.cpp
68
lib/map.cpp
@ -139,35 +139,7 @@ static EDefType getDefType(CGDefInfo * a)
|
||||
return TERRAINOBJ_DEF; // nothing to be handled
|
||||
}
|
||||
}
|
||||
static int readNormalNr (const unsigned char * bufor, int pos, int bytCon = 4, bool cyclic = false)
|
||||
{
|
||||
int ret=0;
|
||||
int amp=1;
|
||||
for (int ir=0; ir<bytCon; ir++)
|
||||
{
|
||||
ret+=bufor[pos+ir]*amp;
|
||||
amp*=256;
|
||||
}
|
||||
if(cyclic && bytCon<4 && ret>=amp/2)
|
||||
{
|
||||
ret = ret-amp;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
static char readChar(const unsigned char * bufor, int &i)
|
||||
{
|
||||
return bufor[i++];
|
||||
}
|
||||
static std::string readString(const unsigned char * bufor, int &i)
|
||||
{
|
||||
int len = readNormalNr(bufor,i); i+=4;
|
||||
std::string ret; ret.reserve(len);
|
||||
for(int gg=0; gg<len; ++gg)
|
||||
{
|
||||
ret += bufor[i++];
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
static CCreatureSet readCreatureSet(const unsigned char * bufor, int &i, int number, bool version) //version==true for >RoE maps
|
||||
{
|
||||
if(version)
|
||||
@ -558,50 +530,18 @@ void Mapa::addBlockVisTiles(CGObjectInstance * obj)
|
||||
}
|
||||
Mapa::Mapa(std::string filename)
|
||||
{
|
||||
const int bufsize = 65536;
|
||||
int mapsize = 0;
|
||||
|
||||
tlog0<<"Opening map file: "<<filename<<"\t "<<std::flush;
|
||||
gzFile map = gzopen(filename.c_str(),"rb");
|
||||
std::vector<unsigned char *> mapstr;
|
||||
|
||||
// Read a map by chunks
|
||||
// We could try to read the map size directly (cf RFC 1952) and then read
|
||||
// directly the whole map, but that would create more problems.
|
||||
do {
|
||||
unsigned char *buf = new unsigned char[bufsize];
|
||||
|
||||
int ret = gzread(map, buf, bufsize);
|
||||
if (ret == 0 || ret == -1) {
|
||||
delete [] buf;
|
||||
break;
|
||||
}
|
||||
|
||||
mapstr.push_back(buf);
|
||||
mapsize += ret;
|
||||
} while(1);
|
||||
|
||||
gzclose(map);
|
||||
|
||||
// Now that we know the uncompressed size, reassemble the chunks
|
||||
unsigned char *initTable = new unsigned char[mapsize];
|
||||
|
||||
std::vector<unsigned char *>::iterator it;
|
||||
int offset;
|
||||
int tocopy = mapsize;
|
||||
for (it = mapstr.begin(), offset = 0;
|
||||
it != mapstr.end();
|
||||
it++, offset+=bufsize ) {
|
||||
memcpy(&initTable[offset], *it, tocopy > bufsize ? bufsize : tocopy);
|
||||
tocopy -= bufsize;
|
||||
delete [] *it;
|
||||
}
|
||||
//load file and decompress
|
||||
unsigned char * initTable = CLodHandler::getUnpackedFile(filename, &mapsize);
|
||||
|
||||
tlog0<<"done."<<std::endl;
|
||||
|
||||
// Compute checksum
|
||||
boost::crc_32_type result;
|
||||
result.process_bytes(initTable,mapsize);
|
||||
result.process_bytes(initTable, mapsize);
|
||||
checksum = result.checksum();
|
||||
tlog0 << "\tOur map checksum: "<<result.checksum() << std::endl;
|
||||
|
||||
|
Reference in New Issue
Block a user