CLodHandler.cpp 11 KB

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