mirror of
https://github.com/vcmi/vcmi.git
synced 2024-11-24 08:32:34 +02:00
[refactor] h3m reader
* reduce code duplication * use binary reader
This commit is contained in:
parent
124d732171
commit
ed3d35554e
@ -79,6 +79,25 @@ INSTANTIATE(si64, readInt64)
|
||||
|
||||
#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::stringstream ss;
|
||||
|
@ -73,6 +73,14 @@ public:
|
||||
ui64 readUInt64();
|
||||
si64 readInt64();
|
||||
|
||||
std::string readString();
|
||||
|
||||
inline bool readBool()
|
||||
{
|
||||
return readUInt8() != 0;
|
||||
}
|
||||
|
||||
void skip(int count);
|
||||
private:
|
||||
/**
|
||||
* Reads any integer. Advances the read pointer by its size.
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -15,9 +15,10 @@
|
||||
#include "../GameConstants.h"
|
||||
#include "../ResourceSet.h"
|
||||
|
||||
#include "../vcmi_endian.h"
|
||||
#include "../int3.h"
|
||||
|
||||
#include "../Filesystem/CBinaryReader.h"
|
||||
|
||||
class CGHeroInstance;
|
||||
class CArtifactInstance;
|
||||
class CGObjectInstance;
|
||||
@ -209,10 +210,18 @@ private:
|
||||
*/
|
||||
void readEvents();
|
||||
|
||||
/**
|
||||
* read optional message and optional guards
|
||||
*/
|
||||
void readMessageAndGuards(std::string& message, CCreatureSet * guards);
|
||||
|
||||
void readSpells(std::set<TSpell> & dest);
|
||||
|
||||
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
|
||||
* @param dest destination vector, shall be filed with "true" values
|
||||
* @param byteCount size in bytes of bimask
|
||||
@ -230,81 +239,17 @@ private:
|
||||
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()
|
||||
{
|
||||
int3 p;
|
||||
p.x = readUI8();
|
||||
p.y = readUI8();
|
||||
p.z = readUI8();
|
||||
p.x = reader.readUInt8();
|
||||
p.y = reader.readUInt8();
|
||||
p.z = reader.readUInt8();
|
||||
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 */
|
||||
CMap * map;
|
||||
|
||||
@ -314,13 +259,7 @@ private:
|
||||
*/
|
||||
std::unique_ptr<CMapHeader> mapHeader;
|
||||
|
||||
/** pointer to the array containing the map data;
|
||||
* TODO replace with CBinaryReader later (this makes pos & size redundant) */
|
||||
ui8 * buffer;
|
||||
CBinaryReader reader;
|
||||
CInputStream * inputStream;
|
||||
|
||||
/** current buffer reading position */
|
||||
int pos;
|
||||
|
||||
/** size of the map in bytes */
|
||||
int size;
|
||||
};
|
Loading…
Reference in New Issue
Block a user