CLodHandler.cpp 8.3 KB

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