CLodHandler.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. #define VCMI_DLL
  2. #include "../stdafx.h"
  3. #include "zlib.h"
  4. #include "CLodHandler.h"
  5. #include <sstream>
  6. #include <algorithm>
  7. #include <cctype>
  8. #include <cstring>
  9. #include <iostream>
  10. #include <fstream>
  11. #include "boost/filesystem/operations.hpp"
  12. #include <boost/algorithm/string.hpp>
  13. #include <boost/algorithm/string/replace.hpp>
  14. #include <boost/thread.hpp>
  15. #include <SDL_endian.h>
  16. #ifdef max
  17. #undef max
  18. #endif
  19. /*
  20. * CLodHandler.cpp, part of VCMI engine
  21. *
  22. * Authors: listed in file AUTHORS in main folder
  23. *
  24. * License: GNU General Public License v2.0 or later
  25. * Full text of license available in license.txt file, in main folder
  26. *
  27. */
  28. int readNormalNr (const unsigned char * bufor, int pos, int bytCon, bool cyclic)
  29. {
  30. int ret=0;
  31. int amp=1;
  32. for (int ir=0; ir<bytCon; ir++)
  33. {
  34. ret+=bufor[pos+ir]*amp;
  35. amp*=256;
  36. }
  37. if(cyclic && bytCon<4 && ret>=amp/2)
  38. {
  39. ret = ret-amp;
  40. }
  41. return ret;
  42. }
  43. char readChar(const unsigned char * bufor, int &i)
  44. {
  45. return bufor[i++];
  46. }
  47. std::string readString(const unsigned char * bufor, int &i)
  48. {
  49. int len = readNormalNr(bufor,i); i+=4;
  50. std::string ret; ret.reserve(len);
  51. for(int gg=0; gg<len; ++gg)
  52. {
  53. ret += bufor[i++];
  54. }
  55. return ret;
  56. }
  57. unsigned char * CLodHandler::giveFile(std::string defName, int * length)
  58. {
  59. std::transform(defName.begin(), defName.end(), defName.begin(), (int(*)(int))toupper);
  60. Entry * ourEntry = entries.znajdz(Entry(defName));
  61. if(!ourEntry) //nothing's been found
  62. {
  63. tlog1 << "Cannot find file: " << defName << std::endl;
  64. return NULL;
  65. }
  66. if(length) *length = ourEntry->realSize;
  67. mutex->lock();
  68. unsigned char * outp;
  69. if (ourEntry->offset<0) //file is in the sprites/ folder; no compression
  70. {
  71. int result;
  72. unsigned char * outp = new unsigned char[ourEntry->realSize];
  73. FILE * f = fopen((myDir + "/" + ourEntry->nameStr).c_str(), "rb");
  74. if (f) {
  75. result = fread(outp,1,ourEntry->realSize,f);
  76. fclose(f);
  77. } else
  78. result = -1;
  79. mutex->unlock();
  80. if(result<0) {
  81. tlog1<<"Error in file reading: " << myDir << "/" << ourEntry->nameStr << std::endl;
  82. delete[] outp;
  83. return NULL;
  84. } else
  85. return outp;
  86. }
  87. else if (ourEntry->size==0) //file is not compressed
  88. {
  89. outp = new unsigned char[ourEntry->realSize];
  90. LOD.seekg(ourEntry->offset, std::ios::beg);
  91. LOD.read((char*)outp, ourEntry->realSize);
  92. mutex->unlock();
  93. return outp;
  94. }
  95. else //we will decompress file
  96. {
  97. outp = new unsigned char[ourEntry->size];
  98. LOD.seekg(ourEntry->offset, std::ios::beg);
  99. LOD.read((char*)outp, ourEntry->size);
  100. mutex->unlock();
  101. unsigned char * decomp = NULL;
  102. int decRes = infs2(outp, ourEntry->size, ourEntry->realSize, decomp);
  103. delete[] outp;
  104. return decomp;
  105. }
  106. return NULL;
  107. }
  108. DLL_EXPORT int CLodHandler::infs2(unsigned char * in, int size, int realSize, unsigned char *& out, int wBits)
  109. {
  110. int ret;
  111. unsigned have;
  112. z_stream strm;
  113. out = new unsigned char [realSize];
  114. int latPosOut = 0;
  115. /* allocate inflate state */
  116. strm.zalloc = Z_NULL;
  117. strm.zfree = Z_NULL;
  118. strm.opaque = Z_NULL;
  119. strm.avail_in = 0;
  120. strm.next_in = Z_NULL;
  121. ret = inflateInit2(&strm, wBits);
  122. if (ret != Z_OK)
  123. return ret;
  124. int chunkNumber = 0;
  125. do
  126. {
  127. if(size < chunkNumber * NLoadHandlerHelp::fCHUNK)
  128. break;
  129. strm.avail_in = std::min(NLoadHandlerHelp::fCHUNK, size - chunkNumber * NLoadHandlerHelp::fCHUNK);
  130. if (strm.avail_in == 0)
  131. break;
  132. strm.next_in = in + chunkNumber * NLoadHandlerHelp::fCHUNK;
  133. /* run inflate() on input until output buffer not full */
  134. do
  135. {
  136. strm.avail_out = realSize - latPosOut;
  137. strm.next_out = out + latPosOut;
  138. ret = inflate(&strm, Z_NO_FLUSH);
  139. //assert(ret != Z_STREAM_ERROR); /* state not clobbered */
  140. bool breakLoop = false;
  141. switch (ret)
  142. {
  143. case Z_STREAM_END:
  144. breakLoop = true;
  145. break;
  146. case Z_NEED_DICT:
  147. ret = Z_DATA_ERROR; /* and fall through */
  148. case Z_DATA_ERROR:
  149. case Z_MEM_ERROR:
  150. (void)inflateEnd(&strm);
  151. return ret;
  152. }
  153. if(breakLoop)
  154. break;
  155. have = realSize - latPosOut - strm.avail_out;
  156. latPosOut += have;
  157. } while (strm.avail_out == 0);
  158. ++chunkNumber;
  159. /* done when inflate() says it's done */
  160. } while (ret != Z_STREAM_END);
  161. /* clean up and return */
  162. (void)inflateEnd(&strm);
  163. return ret == Z_STREAM_END ? Z_OK : Z_DATA_ERROR;
  164. }
  165. void CLodHandler::extractFile(std::string FName, std::string name)
  166. {
  167. int len; //length of file to write
  168. unsigned char * outp = giveFile(name, &len);
  169. std::ofstream out;
  170. out.open(FName.c_str(), std::ios::binary);
  171. if(!out.is_open())
  172. {
  173. tlog1<<"Unable to create "<<FName<<std::endl;
  174. }
  175. else
  176. {
  177. out.write(reinterpret_cast<char*>(outp), len);
  178. out.close();
  179. }
  180. }
  181. void CLodHandler::init(std::string lodFile, std::string dirName)
  182. {
  183. myDir = dirName;
  184. std::string Ts;
  185. Uint32 temp;
  186. LOD.open(lodFile.c_str(), std::ios::in | std::ios::binary);
  187. if (!LOD.is_open())
  188. {
  189. tlog1 << "Cannot open " << lodFile << std::endl;
  190. return;
  191. }
  192. LOD.seekg(8);
  193. LOD.read((char *)&temp, 4);
  194. totalFiles = SDL_SwapLE32(temp);
  195. LOD.seekg(0x5c, std::ios::beg);
  196. struct LodEntry *lodEntries = new struct LodEntry[totalFiles];
  197. LOD.read((char *)lodEntries, sizeof(struct LodEntry) * totalFiles);
  198. for (unsigned int i=0; i<totalFiles; i++)
  199. {
  200. Entry entry;
  201. entry.nameStr = lodEntries[i].filename;
  202. std::transform(entry.nameStr.begin(), entry.nameStr.end(),
  203. entry.nameStr.begin(), toupper);
  204. entry.offset= SDL_SwapLE32(lodEntries[i].offset);
  205. entry.realSize = SDL_SwapLE32(lodEntries[i].uncompressedSize);
  206. entry.size = SDL_SwapLE32(lodEntries[i].size);
  207. entries.push_back(entry);
  208. }
  209. delete [] lodEntries;
  210. boost::filesystem::directory_iterator enddir;
  211. if(boost::filesystem::exists(dirName))
  212. {
  213. for (boost::filesystem::directory_iterator dir(dirName);dir!=enddir;dir++)
  214. {
  215. if(boost::filesystem::is_regular(dir->status()))
  216. {
  217. std::string name = dir->path().leaf();
  218. std::string realname = name;
  219. std::transform(name.begin(), name.end(), name.begin(), (int(*)(int))toupper);
  220. boost::algorithm::replace_all(name,".BMP",".PCX");
  221. Entry * e = entries.znajdz(name);
  222. if(e) //file present in .lod - overwrite its entry
  223. {
  224. e->offset = -1;
  225. e->realName = realname;
  226. e->realSize = e->size = boost::filesystem::file_size(dir->path());
  227. }
  228. else //file not present in lod - add entry for it
  229. {
  230. Entry e2;
  231. e2.offset = -1;
  232. e2.nameStr = name;
  233. e2.realName = realname;
  234. e2.realSize = e2.size = boost::filesystem::file_size(dir->path());
  235. entries.push_back(e2);
  236. }
  237. }
  238. }
  239. }
  240. else
  241. {
  242. tlog1<<"Warning: No "+dirName+"/ folder!"<<std::endl;
  243. }
  244. }
  245. std::string CLodHandler::getTextFile(std::string name)
  246. {
  247. int length=-1;
  248. unsigned char* data = giveFile(name,&length);
  249. if (!data) {
  250. tlog1<<"Fatal error. Missing game file: " << name << ". Aborting!"<<std::endl;
  251. exit(1);
  252. }
  253. std::string ret(data, data+length);
  254. delete [] data;
  255. return ret;
  256. }
  257. CLodHandler::CLodHandler()
  258. {
  259. mutex = new boost::mutex;
  260. totalFiles = 0;
  261. }
  262. CLodHandler::~CLodHandler()
  263. {
  264. delete mutex;
  265. }
  266. unsigned char * CLodHandler::getUnpackedFile( const std::string & path, int * sizeOut )
  267. {
  268. const int bufsize = 65536;
  269. int mapsize = 0;
  270. gzFile map = gzopen(path.c_str(), "rb");
  271. assert(map);
  272. std::vector<unsigned char *> mapstr;
  273. // Read a map by chunks
  274. // We could try to read the map size directly (cf RFC 1952) and then read
  275. // directly the whole map, but that would create more problems.
  276. do {
  277. unsigned char *buf = new unsigned char[bufsize];
  278. int ret = gzread(map, buf, bufsize);
  279. if (ret == 0 || ret == -1) {
  280. delete [] buf;
  281. break;
  282. }
  283. mapstr.push_back(buf);
  284. mapsize += ret;
  285. } while(1);
  286. gzclose(map);
  287. // Now that we know the uncompressed size, reassemble the chunks
  288. unsigned char *initTable = new unsigned char[mapsize];
  289. std::vector<unsigned char *>::iterator it;
  290. int offset;
  291. int tocopy = mapsize;
  292. for (it = mapstr.begin(), offset = 0;
  293. it != mapstr.end();
  294. it++, offset+=bufsize ) {
  295. memcpy(&initTable[offset], *it, tocopy > bufsize ? bufsize : tocopy);
  296. tocopy -= bufsize;
  297. delete [] *it;
  298. }
  299. *sizeOut = mapsize;
  300. return initTable;
  301. }