CLodHandler.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. #include "StdInc.h"
  2. #include "CLodHandler.h"
  3. #include "vcmi_endian.h"
  4. #ifdef max
  5. #undef max
  6. #endif
  7. /*
  8. * CLodHandler.cpp, part of VCMI engine
  9. *
  10. * Authors: listed in file AUTHORS in main folder
  11. *
  12. * License: GNU General Public License v2.0 or later
  13. * Full text of license available in license.txt file, in main folder
  14. *
  15. */
  16. std::string readString(const ui8 * bufor, int &i)
  17. {
  18. int len = read_le_u32(bufor + i); i+=4;
  19. assert(len >= 0 && len <= 500000); //not too long
  20. std::string ret; ret.reserve(len);
  21. for(int gg=0; gg<len; ++gg)
  22. {
  23. ret += bufor[i++];
  24. }
  25. return ret;
  26. }
  27. /*
  28. void CLodHandler::convertName(std::string &filename, std::string *extension)
  29. {
  30. std::transform(filename.begin(), filename.end(), filename.begin(), toupper);
  31. size_t dotPos = filename.find_last_of("/.");
  32. if ( dotPos != std::string::npos && filename[dotPos] == '.')
  33. {
  34. if (extension)
  35. *extension = filename.substr(dotPos);
  36. filename.erase(dotPos);
  37. }
  38. }
  39. ui8 * CLodHandler::giveFile(std::string fname, LodFileType type, int * length)
  40. {
  41. convertName(fname);
  42. boost::unordered_set<Entry>::const_iterator en_it = entries.find(Entry(fname, type));
  43. if(en_it == entries.end()) //nothing's been found
  44. {
  45. tlog1 << "Cannot find file: " << fname << std::endl;
  46. return NULL;
  47. }
  48. Entry ourEntry = *en_it;
  49. if(length) *length = ourEntry.realSize;
  50. boost::unique_lock<boost::mutex> lock(*mutex);
  51. ui8 * outp;
  52. if (ourEntry.offset<0) //file is in the sprites/ folder; no compression
  53. {
  54. int result;
  55. outp = new ui8[ourEntry.realSize];
  56. FILE * f = fopen((myDir + "/" + ourEntry.realName).c_str(), "rb");
  57. if (f)
  58. {
  59. result = fread(outp,1,ourEntry.realSize,f);
  60. fclose(f);
  61. }
  62. else
  63. result = -1;
  64. if(result<0)
  65. {
  66. tlog1<<"Error in file reading: " << ourEntry.realName << std::endl;
  67. perror("Last error was: ");//print system error message
  68. delete[] outp;
  69. return NULL;
  70. }
  71. else
  72. return outp;
  73. }
  74. else if (ourEntry.size==0) //file is not compressed
  75. {
  76. outp = new ui8[ourEntry.realSize];
  77. LOD.seekg(ourEntry.offset, std::ios::beg);
  78. LOD.read((char*)outp, ourEntry.realSize);
  79. return outp;
  80. }
  81. else //we will decompress file
  82. {
  83. outp = new ui8[ourEntry.size];
  84. LOD.seekg(ourEntry.offset, std::ios::beg);
  85. LOD.read((char*)outp, ourEntry.size);
  86. ui8 * decomp = NULL;
  87. infs2(outp, ourEntry.size, ourEntry.realSize, decomp);
  88. delete[] outp;
  89. return decomp;
  90. }
  91. return NULL;
  92. }
  93. std::string CLodHandler::getFileName(std::string lodFile, LodFileType type)
  94. {
  95. convertName(lodFile);
  96. boost::unordered_set<Entry>::const_iterator it = entries.find(Entry(lodFile, type));
  97. if (it != entries.end())
  98. return it->realName;
  99. return "";
  100. }
  101. bool CLodHandler::haveFile(std::string name, LodFileType type)
  102. {
  103. convertName(name);
  104. return vstd::contains(entries, Entry(name, type));
  105. }
  106. DLL_LINKAGE int CLodHandler::infs2(ui8 * in, int size, int realSize, ui8 *& out, int wBits)
  107. {
  108. int ret;
  109. unsigned have;
  110. z_stream strm;
  111. out = new ui8 [realSize];
  112. int latPosOut = 0;
  113. // allocate inflate state
  114. strm.zalloc = Z_NULL;
  115. strm.zfree = Z_NULL;
  116. strm.opaque = Z_NULL;
  117. strm.avail_in = 0;
  118. strm.next_in = Z_NULL;
  119. ret = inflateInit2(&strm, wBits);
  120. if (ret != Z_OK)
  121. return ret;
  122. int chunkNumber = 0;
  123. do
  124. {
  125. if(size < chunkNumber * NLoadHandlerHelp::fCHUNK)
  126. break;
  127. strm.avail_in = std::min(NLoadHandlerHelp::fCHUNK, size - chunkNumber * NLoadHandlerHelp::fCHUNK);
  128. if (strm.avail_in == 0)
  129. break;
  130. strm.next_in = in + chunkNumber * NLoadHandlerHelp::fCHUNK;
  131. // run inflate() on input until output buffer not full
  132. do
  133. {
  134. strm.avail_out = realSize - latPosOut;
  135. strm.next_out = out + latPosOut;
  136. ret = inflate(&strm, Z_NO_FLUSH);
  137. //assert(ret != Z_STREAM_ERROR); // state not clobbered
  138. bool breakLoop = false;
  139. switch (ret)
  140. {
  141. case Z_STREAM_END:
  142. breakLoop = true;
  143. break;
  144. case Z_NEED_DICT:
  145. ret = Z_DATA_ERROR; // and fall through
  146. case Z_DATA_ERROR:
  147. case Z_MEM_ERROR:
  148. (void)inflateEnd(&strm);
  149. return ret;
  150. }
  151. if(breakLoop)
  152. break;
  153. have = realSize - latPosOut - strm.avail_out;
  154. latPosOut += have;
  155. } while (strm.avail_out == 0);
  156. ++chunkNumber;
  157. // done when inflate() says it's done
  158. } while (ret != Z_STREAM_END);
  159. // clean up and return
  160. (void)inflateEnd(&strm);
  161. return ret == Z_STREAM_END ? Z_OK : Z_DATA_ERROR;
  162. }
  163. void CLodHandler::extractFile(const std::string FName, const std::string name, LodFileType type)
  164. {
  165. int len; //length of file to write
  166. ui8 * outp = giveFile(name, type, &len);
  167. std::ofstream out;
  168. out.open(FName.c_str(), std::ios::binary);
  169. if(!out.is_open())
  170. {
  171. tlog1<<"Unable to create "<<FName<<std::endl;
  172. }
  173. else
  174. {
  175. out.write(reinterpret_cast<char*>(outp), len);
  176. out.close();
  177. }
  178. }
  179. void CLodHandler::initEntry(Entry &e, std::string name)
  180. {
  181. std::string ext;
  182. convertName(name, &ext);
  183. e.name = name;
  184. std::map<std::string, LodFileType>::iterator it = extMap.find(ext);
  185. if (it == extMap.end())
  186. e.type = FILE_OTHER;
  187. else
  188. e.type = it->second;
  189. }
  190. void CLodHandler::init(const std::string lodFile, const std::string dirName)
  191. {
  192. #define EXT(NAME, TYPE) extMap.insert(std::pair<std::string, LodFileType>(NAME, TYPE));
  193. EXT(".TXT", FILE_TEXT);
  194. EXT(".JSON",FILE_TEXT);
  195. EXT(".DEF", FILE_ANIMATION);
  196. EXT(".MSK", FILE_MASK);
  197. EXT(".MSG", FILE_MASK);
  198. EXT(".H3C", FILE_CAMPAIGN);
  199. EXT(".H3M", FILE_MAP);
  200. EXT(".FNT", FILE_FONT);
  201. EXT(".BMP", FILE_GRAPHICS);
  202. EXT(".JPG", FILE_GRAPHICS);
  203. EXT(".PCX", FILE_GRAPHICS);
  204. EXT(".PNG", FILE_GRAPHICS);
  205. EXT(".TGA", FILE_GRAPHICS);
  206. #undef EXT
  207. myDir = dirName;
  208. LOD.open(lodFile.c_str(), std::ios::in | std::ios::binary);
  209. if (!LOD.is_open())
  210. {
  211. tlog1 << "Cannot open " << lodFile << std::endl;
  212. return;
  213. }
  214. Uint32 temp;
  215. LOD.seekg(8);
  216. LOD.read((char *)&temp, 4);
  217. totalFiles = SDL_SwapLE32(temp);
  218. LOD.seekg(0x5c, std::ios::beg);
  219. if(!LOD)
  220. {
  221. tlog2 << lodFile << " doesn't store anything!\n";
  222. return;
  223. }
  224. struct LodEntry *lodEntries = new struct LodEntry[totalFiles];
  225. LOD.read((char *)lodEntries, sizeof(struct LodEntry) * totalFiles);
  226. for (ui32 i=0; i<totalFiles; i++)
  227. {
  228. Entry entry;
  229. initEntry(entry, lodEntries[i].filename);
  230. entry.offset= SDL_SwapLE32(lodEntries[i].offset);
  231. entry.realSize = SDL_SwapLE32(lodEntries[i].uncompressedSize);
  232. entry.size = SDL_SwapLE32(lodEntries[i].size);
  233. entries.insert(entry);
  234. }
  235. delete [] lodEntries;
  236. boost::filesystem::recursive_directory_iterator enddir;
  237. if(boost::filesystem::exists(dirName))
  238. {
  239. std::vector<std::string> path;
  240. for (boost::filesystem::recursive_directory_iterator dir(dirName); dir!=enddir; dir++)
  241. {
  242. //If a directory was found - add name to vector to recreate full path later
  243. if (boost::filesystem::is_directory(dir->status()))
  244. {
  245. path.resize(dir.level()+1);
  246. path.back() = dir->path().leaf().string();
  247. }
  248. if(boost::filesystem::is_regular(dir->status()))
  249. {
  250. Entry e;
  251. //we can't get relative path with boost at the moment - need to create path to file manually
  252. for (size_t i=0; i<dir.level() && i<path.size(); i++)
  253. e.realName += path[i] + '/';
  254. e.realName += dir->path().leaf().string();
  255. initEntry(e, e.realName);
  256. if(vstd::contains(entries, e)) //file present in .lod - overwrite its entry
  257. entries.erase(e);
  258. e.offset = -1;
  259. e.realSize = e.size = boost::filesystem::file_size(dir->path());
  260. entries.insert(e);
  261. }
  262. }
  263. }
  264. else
  265. {
  266. if (!dirName.empty())
  267. tlog1<<"Warning: No "+dirName+"/ folder!"<<std::endl;
  268. }
  269. }
  270. std::string CLodHandler::getTextFile(std::string name, LodFileType type)
  271. {
  272. int length=-1;
  273. ui8* data = giveFile(name, type, &length);
  274. if (!data) {
  275. tlog1<<"Fatal error. Missing game file: " << name << ". Aborting!"<<std::endl;
  276. exit(1);
  277. }
  278. std::string ret(data, data+length);
  279. delete [] data;
  280. return ret;
  281. }
  282. CLodHandler::CLodHandler()
  283. {
  284. mutex = new boost::mutex;
  285. totalFiles = 0;
  286. }
  287. CLodHandler::~CLodHandler()
  288. {
  289. delete mutex;
  290. }
  291. //It is possible to use uncompress function from zlib but we need to know decompressed size (not present in compressed data)
  292. ui8 * CLodHandler::getUnpackedData(ui8 *data, size_t inputSize, int * outputSize)
  293. {
  294. std::string filename = GVCMIDirs.UserPath + "/tmp_gzip";
  295. FILE * file = fopen(filename.c_str(), "wb");
  296. fwrite(data, 1, inputSize, file);
  297. fclose(file);
  298. ui8 * ret = getUnpackedFile(filename, outputSize);
  299. remove(filename.c_str());
  300. delete [] data;
  301. return ret;
  302. }
  303. ui8 * CLodHandler::getUnpackedFile( const std::string & path, int * sizeOut )
  304. {
  305. const int bufsize = 65536;
  306. int mapsize = 0;
  307. gzFile map = gzopen(path.c_str(), "rb");
  308. assert(map);
  309. std::vector<ui8 *> mapstr;
  310. // Read a map by chunks
  311. // We could try to read the map size directly (cf RFC 1952) and then read
  312. // directly the whole map, but that would create more problems.
  313. do {
  314. ui8 *buf = new ui8[bufsize];
  315. int ret = gzread(map, buf, bufsize);
  316. if (ret == 0 || ret == -1) {
  317. delete [] buf;
  318. break;
  319. }
  320. mapstr.push_back(buf);
  321. mapsize += ret;
  322. } while(1);
  323. gzclose(map);
  324. // Now that we know the uncompressed size, reassemble the chunks
  325. ui8 *initTable = new ui8[mapsize];
  326. std::vector<ui8 *>::iterator it;
  327. int offset;
  328. int tocopy = mapsize;
  329. for (it = mapstr.begin(), offset = 0;
  330. it != mapstr.end();
  331. it++, offset+=bufsize ) {
  332. memcpy(&initTable[offset], *it, tocopy > bufsize ? bufsize : tocopy);
  333. tocopy -= bufsize;
  334. delete [] *it;
  335. }
  336. *sizeOut = mapsize;
  337. return initTable;
  338. }
  339. */