CLodHandler.cpp 11 KB

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