CLodHandler.cpp 10 KB

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