2
0

CLodHandler.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  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 "boost/filesystem/operations.hpp"
  10. #include <boost/algorithm/string.hpp>
  11. #include <boost/algorithm/string/replace.hpp>
  12. #include <boost/thread.hpp>
  13. #ifdef max
  14. #undef max
  15. #endif
  16. /*
  17. * CLodHandler.cpp, part of VCMI engine
  18. *
  19. * Authors: listed in file AUTHORS in main folder
  20. *
  21. * License: GNU General Public License v2.0 or later
  22. * Full text of license available in license.txt file, in main folder
  23. *
  24. */
  25. DLL_EXPORT int readNormalNr (int pos, int bytCon, unsigned char * str)
  26. {
  27. int ret=0;
  28. int amp=1;
  29. if (str)
  30. {
  31. for (int i=0; i<bytCon; i++)
  32. {
  33. ret+=str[pos+i]*amp;
  34. amp<<=8;
  35. }
  36. }
  37. else return -1;
  38. return ret;
  39. }
  40. unsigned char * CLodHandler::giveFile(std::string defName, int * length)
  41. {
  42. std::transform(defName.begin(), defName.end(), defName.begin(), (int(*)(int))toupper);
  43. Entry * ourEntry = entries.znajdz(Entry(defName));
  44. if(!ourEntry) //nothing's been found
  45. {
  46. tlog1<<"Cannot find file: "<<defName;
  47. return NULL;
  48. }
  49. if(length) *length = ourEntry->realSize;
  50. mutex->lock();
  51. fseek(FLOD, ourEntry->offset, 0);
  52. unsigned char * outp;
  53. if (ourEntry->offset<0) //file is in the sprites/ folder; no compression
  54. {
  55. unsigned char * outp = new unsigned char[ourEntry->realSize];
  56. char name[30];memset(name,0,30);
  57. strcat(name, myDir.c_str());
  58. strcat(name, PATHSEPARATOR);
  59. strcat(name,ourEntry->nameStr.c_str());
  60. FILE * f = fopen(name,"rb");
  61. int result = fread(outp,1,ourEntry->realSize,f);
  62. mutex->unlock();
  63. if(result<0) {tlog1<<"Error in file reading: "<<name<<std::endl;delete[] outp; return NULL;}
  64. else
  65. return outp;
  66. }
  67. else if (ourEntry->size==0) //file is not compressed
  68. {
  69. outp = new unsigned char[ourEntry->realSize];
  70. fread((char*)outp, 1, ourEntry->realSize, FLOD);
  71. mutex->unlock();
  72. return outp;
  73. }
  74. else //we will decompress file
  75. {
  76. outp = new unsigned char[ourEntry->size];
  77. fread((char*)outp, 1, ourEntry->size, FLOD);
  78. mutex->unlock();
  79. unsigned char * decomp = NULL;
  80. int decRes = infs2(outp, ourEntry->size, ourEntry->realSize, decomp);
  81. delete[] outp;
  82. return decomp;
  83. }
  84. return NULL;
  85. }
  86. int CLodHandler::infs(unsigned char * in, int size, int realSize, std::ofstream & out, int wBits)
  87. {
  88. int ret;
  89. unsigned have;
  90. z_stream strm;
  91. unsigned char inx[NLoadHandlerHelp::fCHUNK];
  92. unsigned char outx[NLoadHandlerHelp::fCHUNK];
  93. /* allocate inflate state */
  94. strm.zalloc = Z_NULL;
  95. strm.zfree = Z_NULL;
  96. strm.opaque = Z_NULL;
  97. strm.avail_in = 0;
  98. strm.next_in = Z_NULL;
  99. ret = inflateInit2(&strm, wBits);
  100. if (ret != Z_OK)
  101. return ret;
  102. int chunkNumber = 0;
  103. do
  104. {
  105. int readBytes = 0;
  106. for(int i=0; i<NLoadHandlerHelp::fCHUNK && (chunkNumber * NLoadHandlerHelp::fCHUNK + i)<size; ++i)
  107. {
  108. inx[i] = in[chunkNumber * NLoadHandlerHelp::fCHUNK + i];
  109. ++readBytes;
  110. }
  111. ++chunkNumber;
  112. strm.avail_in = readBytes;
  113. //strm.avail_in = fread(inx, 1, NLoadHandlerHelp::fCHUNK, source);
  114. /*if (in.bad())
  115. {
  116. (void)inflateEnd(&strm);
  117. return Z_ERRNO;
  118. }*/
  119. if (strm.avail_in == 0)
  120. break;
  121. strm.next_in = inx;
  122. /* run inflate() on input until output buffer not full */
  123. do
  124. {
  125. strm.avail_out = NLoadHandlerHelp::fCHUNK;
  126. strm.next_out = outx;
  127. ret = inflate(&strm, Z_NO_FLUSH);
  128. //assert(ret != Z_STREAM_ERROR); /* state not clobbered */
  129. switch (ret)
  130. {
  131. case Z_NEED_DICT:
  132. ret = Z_DATA_ERROR; /* and fall through */
  133. case Z_DATA_ERROR:
  134. case Z_MEM_ERROR:
  135. (void)inflateEnd(&strm);
  136. return ret;
  137. }
  138. have = NLoadHandlerHelp::fCHUNK - strm.avail_out;
  139. /*if (fwrite(out, 1, have, dest) != have || ferror(dest))
  140. {
  141. (void)inflateEnd(&strm);
  142. return Z_ERRNO;
  143. }*/
  144. out.write((char*)outx, have);
  145. if(out.bad())
  146. {
  147. (void)inflateEnd(&strm);
  148. return Z_ERRNO;
  149. }
  150. } while (strm.avail_out == 0);
  151. /* done when inflate() says it's done */
  152. } while (ret != Z_STREAM_END);
  153. /* clean up and return */
  154. (void)inflateEnd(&strm);
  155. return ret == Z_STREAM_END ? Z_OK : Z_DATA_ERROR;
  156. }
  157. DLL_EXPORT int CLodHandler::infs2(unsigned char * in, int size, int realSize, unsigned char *& out, int wBits)
  158. {
  159. int ret;
  160. unsigned have;
  161. z_stream strm;
  162. unsigned char inx[NLoadHandlerHelp::fCHUNK];
  163. unsigned char outx[NLoadHandlerHelp::fCHUNK];
  164. out = new unsigned char [realSize];
  165. int latPosOut = 0;
  166. /* allocate inflate state */
  167. strm.zalloc = Z_NULL;
  168. strm.zfree = Z_NULL;
  169. strm.opaque = Z_NULL;
  170. strm.avail_in = 0;
  171. strm.next_in = Z_NULL;
  172. ret = inflateInit2(&strm, wBits);
  173. if (ret != Z_OK)
  174. return ret;
  175. int chunkNumber = 0;
  176. do
  177. {
  178. int readBytes = 0;
  179. for(int i=0; i<NLoadHandlerHelp::fCHUNK && (chunkNumber * NLoadHandlerHelp::fCHUNK + i)<size; ++i)
  180. {
  181. inx[i] = in[chunkNumber * NLoadHandlerHelp::fCHUNK + i];
  182. ++readBytes;
  183. }
  184. ++chunkNumber;
  185. strm.avail_in = readBytes;
  186. if (strm.avail_in == 0)
  187. break;
  188. strm.next_in = inx;
  189. /* run inflate() on input until output buffer not full */
  190. do
  191. {
  192. strm.avail_out = NLoadHandlerHelp::fCHUNK;
  193. strm.next_out = outx;
  194. ret = inflate(&strm, Z_NO_FLUSH);
  195. //assert(ret != Z_STREAM_ERROR); /* state not clobbered */
  196. switch (ret)
  197. {
  198. case Z_NEED_DICT:
  199. ret = Z_DATA_ERROR; /* and fall through */
  200. case Z_DATA_ERROR:
  201. case Z_MEM_ERROR:
  202. (void)inflateEnd(&strm);
  203. return ret;
  204. }
  205. have = NLoadHandlerHelp::fCHUNK - strm.avail_out;
  206. for(int oo=0; oo<have; ++oo)
  207. {
  208. out[latPosOut] = outx[oo];
  209. ++latPosOut;
  210. }
  211. } while (strm.avail_out == 0);
  212. /* done when inflate() says it's done */
  213. } while (ret != Z_STREAM_END);
  214. /* clean up and return */
  215. (void)inflateEnd(&strm);
  216. return ret == Z_STREAM_END ? Z_OK : Z_DATA_ERROR;
  217. }
  218. void CLodHandler::extract(std::string FName)
  219. {
  220. std::ofstream FOut;
  221. for (int i=0;i<totalFiles;i++)
  222. {
  223. fseek(FLOD, entries[i].offset, 0);
  224. std::string bufff = (DATA_DIR + FName.substr(0, FName.size()-4) + PATHSEPARATOR + entries[i].nameStr);
  225. unsigned char * outp;
  226. if (entries[i].size==0) //file is not compressed
  227. {
  228. outp = new unsigned char[entries[i].realSize];
  229. fread((char*)outp, 1, entries[i].realSize, FLOD);
  230. std::ofstream out;
  231. out.open(bufff.c_str(), std::ios::binary);
  232. if(!out.is_open())
  233. {
  234. tlog1<<"Unable to create "<<bufff;
  235. }
  236. else
  237. {
  238. for(int hh=0; hh<entries[i].realSize; ++hh)
  239. {
  240. out<<*(outp+hh);
  241. }
  242. out.close();
  243. }
  244. }
  245. else
  246. {
  247. outp = new unsigned char[entries[i].size];
  248. fread((char*)outp, 1, entries[i].size, FLOD);
  249. fseek(FLOD, 0, 0);
  250. std::ofstream destin;
  251. destin.open(bufff.c_str(), std::ios::binary);
  252. //int decRes = decompress(outp, entries[i].size, entries[i].realSize, bufff);
  253. int decRes = infs(outp, entries[i].size, entries[i].realSize, destin);
  254. destin.close();
  255. if(decRes!=0)
  256. {
  257. tlog1<<"LOD Extraction error"<<" "<<decRes<<" while extracting to "<<bufff<<std::endl;
  258. }
  259. }
  260. delete[] outp;
  261. }
  262. fclose(FLOD);
  263. }
  264. void CLodHandler::extractFile(std::string FName, std::string name)
  265. {
  266. std::transform(name.begin(), name.end(), name.begin(), (int(*)(int))toupper);
  267. for (int i=0;i<totalFiles;i++)
  268. {
  269. std::string buf1 = entries[i].nameStr;
  270. std::transform(buf1.begin(), buf1.end(), buf1.begin(), (int(*)(int))toupper);
  271. if(buf1!=name)
  272. continue;
  273. fseek(FLOD, entries[i].offset, 0);
  274. std::string bufff = (FName);
  275. unsigned char * outp;
  276. if (entries[i].size==0) //file is not compressed
  277. {
  278. outp = new unsigned char[entries[i].realSize];
  279. fread((char*)outp, 1, entries[i].realSize, FLOD);
  280. std::ofstream out;
  281. out.open(bufff.c_str(), std::ios::binary);
  282. if(!out.is_open())
  283. {
  284. tlog1<<"Unable to create "<<bufff;
  285. }
  286. else
  287. {
  288. for(int hh=0; hh<entries[i].realSize; ++hh)
  289. {
  290. out<<*(outp+hh);
  291. }
  292. out.close();
  293. }
  294. }
  295. else //we will decompressing file
  296. {
  297. outp = new unsigned char[entries[i].size];
  298. fread((char*)outp, 1, entries[i].size, FLOD);
  299. fseek(FLOD, 0, 0);
  300. std::ofstream destin;
  301. destin.open(bufff.c_str(), std::ios::binary);
  302. //int decRes = decompress(outp, entries[i].size, entries[i].realSize, bufff);
  303. int decRes = infs(outp, entries[i].size, entries[i].realSize, destin);
  304. destin.close();
  305. if(decRes!=0)
  306. {
  307. tlog1<<"LOD Extraction error"<<" "<<decRes<<" while extracting to "<<bufff<<std::endl;
  308. }
  309. }
  310. delete[] outp;
  311. }
  312. }
  313. int CLodHandler::readNormalNr (unsigned char* bufor, int bytCon, bool cyclic)
  314. {
  315. int ret=0;
  316. int amp=1;
  317. for (int i=0; i<bytCon; i++)
  318. {
  319. ret+=bufor[i]*amp;
  320. amp*=256;
  321. }
  322. if(cyclic && bytCon<4 && ret>=amp/2)
  323. {
  324. ret = ret-amp;
  325. }
  326. return ret;
  327. }
  328. void CLodHandler::init(std::string lodFile, std::string dirName)
  329. {
  330. myDir = dirName;
  331. std::string Ts;
  332. FLOD = fopen(lodFile.c_str(), "rb");
  333. if(!FLOD)
  334. {
  335. tlog1 << "Cannot open " << lodFile << std::endl;
  336. return;
  337. }
  338. fseek(FLOD, 8, 0);
  339. unsigned char temp[4];
  340. fread((char*)temp, 1, 4, FLOD);
  341. totalFiles = readNormalNr(temp,4);
  342. fseek(FLOD, 0x5c, 0);
  343. for (int i=0; i<totalFiles; i++)
  344. {
  345. Entry entry;
  346. char bufc = -1;
  347. bool valid = true;
  348. entry.nameStr = "";
  349. // Read name
  350. for(int kk=0; kk<16; ++kk)
  351. {
  352. fread(&bufc, 1, 1, FLOD);
  353. if (valid) {
  354. if (bufc)
  355. entry.nameStr+=toupper(bufc);
  356. else
  357. valid = false;
  358. }
  359. }
  360. fread((char*)temp, 1, 4, FLOD);
  361. entry.offset=readNormalNr(temp,4);
  362. fread((char*)temp, 1, 4, FLOD);
  363. entry.realSize=readNormalNr(temp,4);
  364. fread((char*)temp, 1, 4, FLOD);
  365. fread((char*)temp, 1, 4, FLOD);
  366. entry.size=readNormalNr(temp,4);
  367. entries.push_back(entry);
  368. }
  369. boost::filesystem::directory_iterator enddir;
  370. if(boost::filesystem::exists(dirName))
  371. {
  372. for (boost::filesystem::directory_iterator dir(dirName);dir!=enddir;dir++)
  373. {
  374. if(boost::filesystem::is_regular(dir->status()))
  375. {
  376. std::string name = dir->path().leaf();
  377. std::transform(name.begin(), name.end(), name.begin(), (int(*)(int))toupper);
  378. boost::algorithm::replace_all(name,".BMP",".PCX");
  379. Entry * e = entries.znajdz(name);
  380. if(e) //file present in .lod - overwrite its entry
  381. {
  382. e->offset = -1;
  383. e->realSize = e->size = boost::filesystem::file_size(dir->path());
  384. }
  385. else //file not present in lod - add entry for it
  386. {
  387. Entry e2;
  388. e2.offset = -1;
  389. e2.nameStr = name;
  390. e2.realSize = e2.size = boost::filesystem::file_size(dir->path());
  391. entries.push_back(e2);
  392. }
  393. }
  394. }
  395. }
  396. else
  397. {
  398. tlog1<<"Warning: No "+dirName+"/ folder!"<<std::endl;
  399. }
  400. }
  401. std::string CLodHandler::getTextFile(std::string name)
  402. {
  403. int length=-1;
  404. unsigned char* data = giveFile(name,&length);
  405. std::string ret;
  406. ret.reserve(length);
  407. for(int i=0;i<length;i++)
  408. ret+=data[i];
  409. delete [] data;
  410. return ret;
  411. }
  412. CLodHandler::CLodHandler()
  413. {
  414. mutex = new boost::mutex;
  415. FLOD = NULL;
  416. totalFiles = 0;
  417. }
  418. CLodHandler::~CLodHandler()
  419. {
  420. delete mutex;
  421. }