1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-07-05 00:49:09 +02:00

[refactor] h3m reader

* reduce code duplication
* use binary reader
This commit is contained in:
alexvins
2013-02-07 17:02:15 +00:00
parent 124d732171
commit ed3d35554e
4 changed files with 405 additions and 477 deletions

View File

@ -79,6 +79,25 @@ INSTANTIATE(si64, readInt64)
#undef INSTANTIATE #undef INSTANTIATE
std::string CBinaryReader::readString()
{
int len = readUInt32();
assert(len >= 0 && len <= 500000); //not too long
std::string ret;
ret.reserve(len);
for(int gg = 0; gg < len; ++gg)
{
ret += readInt8();
}
return ret;
}
void CBinaryReader::skip(int count)
{
stream->skip(count);
}
std::string CBinaryReader::getEndOfStreamExceptionMsg(long bytesToRead) const std::string CBinaryReader::getEndOfStreamExceptionMsg(long bytesToRead) const
{ {
std::stringstream ss; std::stringstream ss;

View File

@ -73,6 +73,14 @@ public:
ui64 readUInt64(); ui64 readUInt64();
si64 readInt64(); si64 readInt64();
std::string readString();
inline bool readBool()
{
return readUInt8() != 0;
}
void skip(int count);
private: private:
/** /**
* Reads any integer. Advances the read pointer by its size. * Reads any integer. Advances the read pointer by its size.

File diff suppressed because it is too large Load Diff

View File

@ -15,9 +15,10 @@
#include "../GameConstants.h" #include "../GameConstants.h"
#include "../ResourceSet.h" #include "../ResourceSet.h"
#include "../vcmi_endian.h"
#include "../int3.h" #include "../int3.h"
#include "../Filesystem/CBinaryReader.h"
class CGHeroInstance; class CGHeroInstance;
class CArtifactInstance; class CArtifactInstance;
class CGObjectInstance; class CGObjectInstance;
@ -209,10 +210,18 @@ private:
*/ */
void readEvents(); void readEvents();
/**
* read optional message and optional guards
*/
void readMessageAndGuards(std::string& message, CCreatureSet * guards);
void readSpells(std::set<TSpell> & dest); void readSpells(std::set<TSpell> & dest);
void readResourses(TResources& resources); void readResourses(TResources& resources);
template <class Indenifier>
void readBitmask(std::set<Indenifier> &dest, const int byteCount, const int limit, bool negate = true);
/** Reads bitmask to boolean vector /** Reads bitmask to boolean vector
* @param dest destination vector, shall be filed with "true" values * @param dest destination vector, shall be filed with "true" values
* @param byteCount size in bytes of bimask * @param byteCount size in bytes of bimask
@ -230,81 +239,17 @@ private:
ui8 reverse(ui8 arg); ui8 reverse(ui8 arg);
/** /**
* Helper to read ui8 from buffer * Helper to read map position
*/ */
inline ui8 readUI8()
{
return buffer[pos++];
}
/**
* Helper to read si8 from buffer
*/
inline si8 readSI8()
{
return static_cast<si8>(buffer[pos++]);
}
/**
* Helper to read ui16 from buffer
*/
inline ui16 readUI16()
{
ui16 ret = read_le_u16(buffer+pos);
pos +=2;
return ret;
}
/**
* Helper to read ui32 from buffer
*/
inline ui32 readUI32()
{
ui32 ret = read_le_u32(buffer+pos);
pos +=4;
return ret;
}
/**
* Helper to read 8bit flag from buffer
*/
inline bool readBool()
{
return readUI8() != 0;
}
/**
* Helper to read string from buffer
*/
inline std::string readString()
{
return ::readString(buffer,pos);
}
/**
* Helper to skip unused data inbuffer
*/
inline void skip(const int count)
{
pos += count;
}
inline int3 readInt3() inline int3 readInt3()
{ {
int3 p; int3 p;
p.x = readUI8(); p.x = reader.readUInt8();
p.y = readUI8(); p.y = reader.readUInt8();
p.z = readUI8(); p.z = reader.readUInt8();
return p; return p;
} }
/**
* Init buffer / size.
*
* @param stream the stream which serves as the data input
*/
void initBuffer(CInputStream * stream);
/** ptr to the map object which gets filled by data from the buffer */ /** ptr to the map object which gets filled by data from the buffer */
CMap * map; CMap * map;
@ -314,13 +259,7 @@ private:
*/ */
std::unique_ptr<CMapHeader> mapHeader; std::unique_ptr<CMapHeader> mapHeader;
/** pointer to the array containing the map data; CBinaryReader reader;
* TODO replace with CBinaryReader later (this makes pos & size redundant) */ CInputStream * inputStream;
ui8 * buffer;
/** current buffer reading position */
int pos;
/** size of the map in bytes */
int size;
}; };