Browse Source

[refactor] h3m reader
* reduce code duplication
* use binary reader

alexvins 12 years ago
parent
commit
ed3d35554e
4 changed files with 240 additions and 309 deletions
  1. 19 0
      lib/Filesystem/CBinaryReader.cpp
  2. 8 0
      lib/Filesystem/CBinaryReader.h
  3. 197 232
      lib/Mapping/MapFormatH3M.cpp
  4. 16 77
      lib/Mapping/MapFormatH3M.h

+ 19 - 0
lib/Filesystem/CBinaryReader.cpp

@@ -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;

+ 8 - 0
lib/Filesystem/CBinaryReader.h

@@ -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
+ 197 - 232
lib/Mapping/MapFormatH3M.cpp


+ 16 - 77
lib/Mapping/MapFormatH3M.h

@@ -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
-	*/
-	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
+	* Helper to read map position
 	*/
-	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;
-
-	/** current buffer reading position */
-	int pos;
+	CBinaryReader reader;
+	CInputStream * inputStream;
 
-	/** size of the map in bytes */
-	int size;
 };

Some files were not shown because too many files changed in this diff