Bläddra i källkod

* reading of .h3c headers
* a bit of readNormalNr deduplication
* minor changes

mateuszb 15 år sedan
förälder
incheckning
d0bf334394
9 ändrade filer med 128 tillägg och 115 borttagningar
  1. 3 4
      client/CBitmapHandler.cpp
  2. 4 4
      client/Graphics.cpp
  3. 21 7
      hch/CCampaignHandler.cpp
  4. 20 1
      hch/CCampaignHandler.h
  5. 0 26
      hch/CDefHandler.cpp
  6. 0 1
      hch/CDefHandler.h
  7. 68 8
      hch/CLodHandler.cpp
  8. 8 0
      hch/CLodHandler.h
  9. 4 64
      lib/map.cpp

+ 3 - 4
client/CBitmapHandler.cpp

@@ -18,7 +18,6 @@
  */
 
 boost::mutex bitmap_handler_mx;
-int readNormalNr (int pos, int bytCon, const unsigned char * str);
 
 extern DLL_EXPORT CLodHandler *bitmaph;
 
@@ -58,9 +57,9 @@ SDL_Surface * CPCXConv::getSurface() const
 	unsigned char add;
 	int it=0;
 
-	fSize = readNormalNr(it,4,pcx);it+=4;
-	width = readNormalNr(it,4,pcx);it+=4;
-	height = readNormalNr(it,4,pcx);it+=4;
+	fSize = readNormalNr(pcx, it); it+=4;
+	width = readNormalNr(pcx, it); it+=4;
+	height = readNormalNr(pcx, it); it+=4;
 	if (fSize==width*height*3)
 		check1=true;
 	else 

+ 4 - 4
client/Graphics.cpp

@@ -680,16 +680,16 @@ Font::Font(unsigned char *Data)
 	i = 32;
 	for(int ci = 0; ci < 256; ci++)
 	{
-		chars[ci].unknown1 = CDefHandler::readNormalNr(i, 4, data); i+=4;
-		chars[ci].width = CDefHandler::readNormalNr(i, 4, data); i+=4;
-		chars[ci].unknown2 = CDefHandler::readNormalNr(i, 4, data); i+=4;
+		chars[ci].unknown1 = readNormalNr(data, i); i+=4;
+		chars[ci].width = readNormalNr(data, i); i+=4;
+		chars[ci].unknown2 =readNormalNr(data, i); i+=4;
 
 		//if(ci>=30)
 		//	tlog0 << ci << ". (" << (char)ci << "). Width: " << chars[ci].width << " U1/U2:" << chars[ci].unknown1 << "/" << chars[ci].unknown2 << std::endl;
 	}
 	for(int ci = 0; ci < 256; ci++)
 	{
-		chars[ci].offset = CDefHandler::readNormalNr(i, 4, data); i+=4;
+		chars[ci].offset = readNormalNr(data, i); i+=4;
 		chars[ci].pixels = data + 4128 + chars[ci].offset;
 	}
 }

+ 21 - 7
hch/CCampaignHandler.cpp

@@ -4,10 +4,20 @@
 #include <boost/filesystem.hpp>
 #include <stdio.h>
 #include <boost/algorithm/string/predicate.hpp>
-
+#include "CLodHandler.h"
 
 namespace fs = boost::filesystem;
 
+/*
+ * CCampaignHandler.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
+ *
+ */
+
 
 std::vector<CCampaignHeader> CCampaignHandler::getCampaignHeaders()
 {
@@ -37,15 +47,19 @@ std::vector<CCampaignHeader> CCampaignHandler::getCampaignHeaders()
 
 CCampaignHeader CCampaignHandler::getHeader( const std::string & name, int size )
 {
-	FILE * input = fopen(name.c_str(), "rb");
-	char * tab = new char[size];
-	fread(tab, 1, size, input);
+	int realSize;
+	unsigned char * cmpgn = CLodHandler::getUnpackedFile(name, &realSize);
 
 	CCampaignHeader ret;
+	int it = 0;//iterator for reading
+	ret.version = readNormalNr(cmpgn, it); it+=4;
+	ret.mapVersion = readChar(cmpgn, it);
+	ret.name = readString(cmpgn, it);
+	ret.description = readString(cmpgn, it);
+	ret.difficultyChoosenByPlayer = readChar(cmpgn, it);
+	ret.music = readChar(cmpgn, it);
 
-
-
-	delete [] tab;
+	delete [] cmpgn;
 
 	return ret;
 }

+ 20 - 1
hch/CCampaignHandler.h

@@ -5,10 +5,29 @@
 #include <string>
 #include <vector>
 
+/*
+ * CCampaignHandler.h, 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
+ *
+ */
 
 class DLL_EXPORT CCampaignHeader
 {
-
+public:
+	si32 version; //5 - AB, 6 - SoD, WoG - ?!?
+	ui8 mapVersion; //CampText.txt's format
+	std::string name, description;
+	ui8 difficultyChoosenByPlayer;
+	ui8 music; //CmpMusic.txt, start from 0
+
+	template <typename Handler> void serialize(Handler &h, const int formatVersion)
+	{
+		h & version & mapVersion & name & description & difficultyChoosenByPlayer & music;
+	}
 };
 
 

+ 0 - 26
hch/CDefHandler.cpp

@@ -144,32 +144,6 @@ void CDefHandler::expand(unsigned char N,unsigned char & BL, unsigned char & BR)
 	BL = (N & 0xE0) >> 5;
 	BR = N & 0x1F;
 }
-int CDefHandler::readNormalNr (int pos, int bytCon, const unsigned char * str, bool cyclic)
-{
-	int ret=0;
-	int amp=1;
-	if (str)
-	{
-		for (int i=0; i<bytCon; i++)
-		{
-			ret+=str[pos+i]*amp;
-			amp*=256;
-		}
-	}
-	//else 
-	//{
-	//	for (int i=0; i<bytCon; i++)
-	//	{
-	//		ret+=FDef[pos+i]*amp;
-	//		amp*=256;
-	//	}
-	//}
-	if(cyclic && bytCon<4 && ret>=amp/2)
-	{
-		ret = ret-amp;
-	}
-	return ret;
-}
 
 SDL_Surface * CDefHandler::getSprite (int SIndex, const unsigned char * FDef, const BMPPalette * palette) const
 {

+ 0 - 1
hch/CDefHandler.h

@@ -88,7 +88,6 @@ public:
 
 	CDefHandler(); //c-tor
 	~CDefHandler(); //d-tor
-	static int readNormalNr (int pos, int bytCon, const unsigned char * str=NULL, bool cyclic=false);
 	SDL_Surface * getSprite (int SIndex, const unsigned char * FDef, const BMPPalette * palette) const; //zapisuje klatke o zadanym numerze do "testtt.bmp"
 	void openDef(std::string name);
 	static void expand(unsigned char N,unsigned char & BL, unsigned char & BR);

+ 68 - 8
hch/CLodHandler.cpp

@@ -27,19 +27,35 @@
  *
  */
 
-DLL_EXPORT int readNormalNr (int pos, int bytCon, const unsigned char * str)
+int readNormalNr (const unsigned char * bufor, int pos, int bytCon, bool cyclic)
 {
 	int ret=0;
 	int amp=1;
-	if (str)
+	for (int ir=0; ir<bytCon; ir++)
 	{
-		for (int i=0; i<bytCon; i++)
-		{
-			ret+=str[pos+i]*amp;
-			amp<<=8;
-		}
+		ret+=bufor[pos+ir]*amp;
+		amp*=256;
+	}
+	if(cyclic && bytCon<4 && ret>=amp/2)
+	{
+		ret = ret-amp;
+	}
+	return ret;
+}
+
+char readChar(const unsigned char * bufor, int &i)
+{
+	return bufor[i++];
+}
+
+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++];
 	}
-	else return -1;
 	return ret;
 }
 
@@ -279,3 +295,47 @@ CLodHandler::~CLodHandler()
 {
 	delete mutex;
 }
+
+unsigned char * CLodHandler::getUnpackedFile( const std::string & path, int * sizeOut )
+{
+	const int bufsize = 65536;
+	int mapsize = 0;
+
+	gzFile map = gzopen(path.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;
+	}
+
+	*sizeOut = mapsize;
+	return initTable;
+}

+ 8 - 0
hch/CLodHandler.h

@@ -37,6 +37,12 @@ struct LodEntry {
 	Uint32 size;				/* little endian */
 };
 
+DLL_EXPORT int readNormalNr (const unsigned char * bufor, int pos, int bytCon = 4, bool cyclic = false);
+
+DLL_EXPORT char readChar(const unsigned char * bufor, int &i);
+
+DLL_EXPORT std::string readString(const unsigned char * bufor, int &i);
+
 struct Entry
 {
 	// Info extracted from LOD file
@@ -76,6 +82,8 @@ public:
 	std::string getTextFile(std::string name); //extracts one file
 	void extractFile(std::string FName, std::string name); //extracts a specific file
 	void init(std::string lodFile, std::string dirName);
+
+	static unsigned char * getUnpackedFile(const std::string & path, int * sizeOut); //loads given file, decompresses and returns
 };
 
 

+ 4 - 64
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;