CLodHandler.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827
  1. #include "stdafx.h"
  2. #include "CLodHandler.h"
  3. #include <sstream>
  4. #include <algorithm>
  5. #include <cstring>
  6. #include "boost/filesystem.hpp" // includes all needed Boost.Filesystem declarations
  7. int readNormalNr (int pos, int bytCon, unsigned char * str)
  8. {
  9. int ret=0;
  10. int amp=1;
  11. if (str)
  12. {
  13. for (int i=0; i<bytCon; i++)
  14. {
  15. ret+=str[pos+i]*amp;
  16. amp*=256;
  17. }
  18. }
  19. else return -1;
  20. return ret;
  21. }
  22. void CPCXConv::openPCX(char * PCX, int len)
  23. {
  24. pcxs=len;
  25. pcx=(unsigned char*)PCX;
  26. /*pcx = new unsigned char[len];
  27. for (int i=0;i<len;i++)
  28. pcx[i]=PCX[i];*/
  29. }
  30. void CPCXConv::fromFile(std::string path)
  31. {
  32. std::ifstream * is = new std::ifstream();
  33. is -> open(path.c_str(),std::ios::binary);
  34. is->seekg(0,std::ios::end); // na koniec
  35. pcxs = is->tellg(); // read length
  36. is->seekg(0,std::ios::beg); // wracamy na poczatek
  37. pcx = new unsigned char[pcxs]; // allocate memory
  38. is->read((char*)pcx, pcxs); // read map file to buffer
  39. is->close();
  40. delete is;
  41. }
  42. void CPCXConv::saveBMP(std::string path)
  43. {
  44. std::ofstream os;
  45. os.open(path.c_str(), std::ios::binary);
  46. os.write((char*)bmp,bmps);
  47. os.close();
  48. }
  49. void CPCXConv::convert()
  50. {
  51. BMPHeader bh;
  52. BMPPalette pal[256];
  53. Epcxformat format;
  54. int fSize,i,y;
  55. bool check1, check2;
  56. unsigned char add;
  57. int it=0;
  58. std::stringstream out;
  59. fSize = readNormalNr(it,4,pcx);it+=4;
  60. bh.x = readNormalNr(it,4,pcx);it+=4;
  61. bh.y = readNormalNr(it,4,pcx);it+=4;
  62. if (fSize==bh.x*bh.y*3)
  63. check1=true;
  64. else
  65. check1=false;
  66. if (fSize==bh.x*bh.y)
  67. check2=true;
  68. else
  69. check2=false;
  70. if (check1)
  71. format=Epcxformat::PCX24B;
  72. else if (check2)
  73. format=Epcxformat::PCX8B;
  74. else
  75. return;
  76. add=(int)(4*(((float)1)-(((float)bh.x/(float)4)-((int)((float)bh.x/(float)4)))));
  77. if (add==4)
  78. add=0;
  79. bh._h3=bh.x*bh.y;
  80. if (format==Epcxformat::PCX8B)
  81. {
  82. bh._c1=0x436;
  83. bh._c2=0x28;
  84. bh._c3=1;
  85. bh._c4=8;
  86. //bh.dataSize2=bh.dataSize1=maxx*maxy;
  87. bh.dataSize1=bh.x;
  88. bh.dataSize2=bh.y;
  89. bh.fullSize = bh.dataSize1+436;
  90. }
  91. else
  92. {
  93. bh._c1=0x36;
  94. bh._c2=0x28;
  95. bh._c3=1;
  96. bh._c4=0x18;
  97. //bh.dataSize2=bh.dataSize1=0xB12;
  98. bh.dataSize1=bh.x;
  99. bh.dataSize2=bh.y;
  100. bh.fullSize=(bh.x+add)*bh.y*3+36+18;
  101. bh._h3*=3;
  102. }
  103. if (format==Epcxformat::PCX8B)
  104. {
  105. it = pcxs-256*3;
  106. for (int i=0;i<256;i++)
  107. {
  108. pal[i].R=pcx[it++];
  109. pal[i].G=pcx[it++];
  110. pal[i].B=pcx[it++];
  111. pal[i].F='\0';
  112. }
  113. }
  114. out<<"BM";
  115. bh.print(out);
  116. if (format==Epcxformat::PCX8B)
  117. {
  118. for (int i=0;i<256;i++)
  119. {
  120. out<<pal[i].B;
  121. out<<pal[i].G;
  122. out<<pal[i].R;
  123. out<<pal[i].F;
  124. }
  125. for (y=bh.y;y>0;y--)
  126. {
  127. it=0xC+(y-1)*bh.x;
  128. for (int j=0;j<bh.x;j++)
  129. out<<pcx[it+j];
  130. if (add>0)
  131. {
  132. for (int j=0;j<add;j++)
  133. out<<'\0'; //bylo z buforu, ale onnie byl incjalizowany (?!)
  134. }
  135. }
  136. }
  137. else
  138. {
  139. for (y=bh.y; y>0; y--)
  140. {
  141. it=0xC+(y-1)*bh.x*3;
  142. for (int j=0;j<bh.x*3;j++)
  143. out<<pcx[it+j];
  144. if (add>0)
  145. {
  146. for (int j=0;j<add*3;j++)
  147. out<<'\0'; //bylo z buforu, ale onnie byl incjalizowany (?!)
  148. }
  149. }
  150. }
  151. std::string temp = out.str();
  152. bmp = new unsigned char[temp.length()];
  153. bmps=temp.length();
  154. for (int a=0;a<temp.length();a++)
  155. {
  156. bmp[a]=temp[a];
  157. }
  158. }
  159. SDL_Surface * CPCXConv::getSurface()
  160. {
  161. SDL_Surface * ret;
  162. BMPHeader bh;
  163. BMPPalette pal[256];
  164. Epcxformat format;
  165. int fSize, maxx, maxy,i,y;
  166. bool check1, check2;
  167. unsigned char add;
  168. int it=0;
  169. fSize = readNormalNr(it,4,pcx);it+=4;
  170. maxx = readNormalNr(it,4,pcx);it+=4;
  171. maxy = readNormalNr(it,4,pcx);it+=4;
  172. if (fSize==maxx*maxy*3)
  173. check1=true;
  174. else
  175. check1=false;
  176. if (fSize==maxx*maxy)
  177. check2=true;
  178. else
  179. check2=false;
  180. if (check1)
  181. format=Epcxformat::PCX24B;
  182. else if (check2)
  183. format=Epcxformat::PCX8B;
  184. else
  185. return NULL;
  186. add=(int)(4*(((float)1)-(((float)maxx/(float)4)-((int)((float)maxx/(float)4)))));
  187. if (add==4)
  188. add=0;
  189. if (format==Epcxformat::PCX8B)
  190. {
  191. it = pcxs-256*3;
  192. for (int i=0;i<256;i++)
  193. {
  194. pal[i].R=pcx[it++];
  195. pal[i].G=pcx[it++];
  196. pal[i].B=pcx[it++];
  197. pal[i].F='\0';
  198. }
  199. }
  200. #if SDL_BYTEORDER == SDL_BIG_ENDIAN
  201. int rmask = 0xff000000;
  202. int gmask = 0x00ff0000;
  203. int bmask = 0x0000ff00;
  204. int amask = 0x000000ff;
  205. #else
  206. int rmask = 0x000000ff;
  207. int gmask = 0x0000ff00;
  208. int bmask = 0x00ff0000;
  209. int amask = 0xff000000;
  210. #endif
  211. ret = SDL_CreateRGBSurface(SDL_SWSURFACE, maxx, maxy, (format==Epcxformat::PCX8B ? 8 : 24), rmask, gmask, bmask, amask);
  212. if (format==Epcxformat::PCX8B)
  213. {
  214. for(int i=0; i<256; ++i)
  215. {
  216. SDL_Color pr;
  217. pr.r = pal[i].R;
  218. pr.g = pal[i].G;
  219. pr.b = pal[i].B;
  220. pr.unused = pal[i].F;
  221. (*(ret->format->palette->colors+i)) = pr;
  222. }
  223. for(y=maxy; y>0; --y)
  224. {
  225. it = 0xc + (y-1)*maxx;
  226. for(int j=0; j<maxx; ++j)
  227. *((char*)ret->pixels + ret->format->BytesPerPixel * (y*(maxx+add) + j)) = pcx[it+j];
  228. for(int j=0; j<add; ++j)
  229. *((char*)ret->pixels + ret->format->BytesPerPixel * (y*(maxx+add) + maxx + j)) = 0;
  230. }
  231. }
  232. else
  233. {
  234. for(int y=maxy; y>0; --y)
  235. {
  236. it = 0xc + (y-1)*maxx*3;
  237. for(int j=0; j<maxx*3; ++j)
  238. {
  239. *((char*)ret->pixels + (y*(maxx+add) + j)) = pcx[it+j];
  240. }
  241. for(int j=0; j<add*3; ++j)
  242. {
  243. *((char*)ret->pixels + (y*(maxx+add) + j)) = 0;
  244. }
  245. }
  246. }
  247. return ret;
  248. }
  249. SDL_Surface * CLodHandler::loadBitmap(std::string fname)
  250. {
  251. unsigned char * pcx;
  252. std::transform(fname.begin(),fname.end(),fname.begin(),toupper);
  253. fname.replace(fname.find_first_of('.'),fname.find_first_of('.')+4,".PCX");
  254. int index=-1;
  255. for (int i=0;i<entries.size();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==fname)
  260. {
  261. index=i;
  262. break;
  263. }
  264. }
  265. FLOD.seekg(entries[index].offset,std::ios_base::beg);
  266. if (entries[index].size==0) //file is not compressed
  267. {
  268. pcx = new unsigned char[entries[index].realSize];
  269. FLOD.read((char*)pcx,entries[index].realSize);
  270. }
  271. else
  272. {
  273. unsigned char * pcd = new unsigned char[entries[index].size];
  274. FLOD.read((char*)pcd,entries[index].size);
  275. infs2(pcd,entries[index].size,entries[index].realSize,pcx);
  276. }
  277. CPCXConv cp;
  278. cp.openPCX((char*)pcx,entries[index].realSize);
  279. cp.convert();
  280. cp.saveBMP("vctemp.bmp");
  281. SDL_Surface * ret = SDL_LoadBMP("vctemp.bmp");
  282. boost::filesystem::path p("vctemp.bmp");
  283. boost::filesystem::remove(p);
  284. //return cp.getSurface();
  285. return ret;
  286. }
  287. int CLodHandler::decompress (unsigned char * source, int size, int realSize, std::string & dest)
  288. {
  289. std::ofstream lb;
  290. lb.open("lodbuf\\buf.gz", std::ios::out|std::ios::binary);
  291. for(int i=0; i<size; ++i)
  292. {
  293. lb<<source[i];
  294. }
  295. lb.close();
  296. FILE * inputf = fopen("lodbuf\\buf.gz", "rb+");
  297. FILE * outputf = fopen(dest.c_str(), "wb+");
  298. int ret = infm(inputf, outputf);
  299. fclose(inputf);
  300. fclose(outputf);
  301. return ret;
  302. }
  303. int CLodHandler::infm(FILE *source, FILE *dest, int wBits)
  304. {
  305. int ret;
  306. unsigned have;
  307. z_stream strm;
  308. unsigned char in[NLoadHandlerHelp::fCHUNK];
  309. unsigned char out[NLoadHandlerHelp::fCHUNK];
  310. /* allocate inflate state */
  311. strm.zalloc = Z_NULL;
  312. strm.zfree = Z_NULL;
  313. strm.opaque = Z_NULL;
  314. strm.avail_in = 0;
  315. strm.next_in = Z_NULL;
  316. ret = inflateInit2(&strm, wBits);
  317. if (ret != Z_OK)
  318. return ret;
  319. /* decompress until deflate stream ends or end of file */
  320. do
  321. {
  322. strm.avail_in = fread(in, 1, NLoadHandlerHelp::fCHUNK, source);
  323. if (ferror(source)) {
  324. (void)inflateEnd(&strm);
  325. return Z_ERRNO;
  326. }
  327. if (strm.avail_in == 0)
  328. break;
  329. strm.next_in = in;
  330. /* run inflate() on input until output buffer not full */
  331. do
  332. {
  333. strm.avail_out = NLoadHandlerHelp::fCHUNK;
  334. strm.next_out = out;
  335. ret = inflate(&strm, Z_NO_FLUSH);
  336. //assert(ret != Z_STREAM_ERROR); /* state not clobbered */
  337. switch (ret)
  338. {
  339. case Z_NEED_DICT:
  340. ret = Z_DATA_ERROR; /* and fall through */
  341. case Z_DATA_ERROR:
  342. case Z_MEM_ERROR:
  343. (void)inflateEnd(&strm);
  344. return ret;
  345. }
  346. have = NLoadHandlerHelp::fCHUNK - strm.avail_out;
  347. if (fwrite(out, 1, have, dest) != have || ferror(dest))
  348. {
  349. (void)inflateEnd(&strm);
  350. return Z_ERRNO;
  351. }
  352. } while (strm.avail_out == 0);
  353. /* done when inflate() says it's done */
  354. } while (ret != Z_STREAM_END);
  355. /* clean up and return */
  356. (void)inflateEnd(&strm);
  357. return ret == Z_STREAM_END ? Z_OK : Z_DATA_ERROR;
  358. }
  359. CDefHandler * CLodHandler::giveDef(std::string defName) // TODO: zamienic
  360. {
  361. std::vector<std::string> pom;
  362. pom.push_back(defName);
  363. return extractManyFiles(pom)[0];
  364. }
  365. std::vector<CDefHandler *> CLodHandler::extractManyFiles(std::vector<std::string> defNamesIn)
  366. {
  367. std::vector<CDefHandler *> ret(defNamesIn.size());
  368. for(int hh=0; hh<defNamesIn.size(); ++hh)
  369. {
  370. std::transform(defNamesIn[hh].begin(), defNamesIn[hh].end(), defNamesIn[hh].begin(), (int(*)(int))toupper);
  371. }
  372. std::ofstream FOut;
  373. int i;
  374. std::vector<char> found(defNamesIn.size(), 0);
  375. for (int i=0;i<totalFiles;i++)
  376. {
  377. //std::cout<<'\r'<<"Reading defs: "<<(100.0*i)/((float)(totalFiles))<<"% ";
  378. std::string buf1 = std::string((char*)entries[i].name);
  379. std::transform(buf1.begin(), buf1.end(), buf1.begin(), (int(*)(int))toupper);
  380. bool exists = false;
  381. int curDef;
  382. for(int hh=0; hh<defNamesIn.size(); ++hh)
  383. {
  384. if(buf1==defNamesIn[hh])
  385. {
  386. exists = true;
  387. found[hh] = '\1';
  388. curDef = hh;
  389. break;
  390. }
  391. }
  392. if(!exists)
  393. continue;
  394. FLOD.seekg(entries[i].offset,std::ios_base::beg);
  395. //std::string bufff = (lodName.substr(0, lodName.size()-4) + "\\" + (char*)entries[i].name);
  396. unsigned char * outp;
  397. if (entries[i].size==0) //file is not compressed
  398. {
  399. outp = new unsigned char[entries[i].realSize];
  400. FLOD.read((char*)outp, entries[i].realSize);
  401. CDefHandler * nh = new CDefHandler;
  402. nh->openFromMemory(outp, entries[i].realSize, std::string((char*)entries[i].name));
  403. ret[curDef] = nh;
  404. }
  405. else //we will decompressing file
  406. {
  407. outp = new unsigned char[entries[i].size];
  408. FLOD.read((char*)outp, entries[i].size);
  409. FLOD.seekg(0, std::ios_base::beg);
  410. unsigned char * decomp = NULL;
  411. int decRes = infs2(outp, entries[i].size, entries[i].realSize, decomp);
  412. CDefHandler * nh = new CDefHandler;
  413. nh->openFromMemory(decomp, entries[i].realSize, std::string((char*)entries[i].name));
  414. ret[curDef] = nh;
  415. }
  416. delete outp;
  417. }
  418. std::cout<<'\r'<<"Reading defs: 100% "<<std::endl;
  419. for(int hh=0; hh<found.size(); ++hh)
  420. {
  421. if(!found[hh])
  422. {
  423. for(int ff=0; ff<hh; ++ff)
  424. {
  425. if(defNamesIn[ff]==defNamesIn[hh])
  426. {
  427. ret[hh]=ret[ff];
  428. }
  429. }
  430. }
  431. }
  432. //std::cout<<"*** Archive: "+FName+" closed\n";
  433. return ret;
  434. }
  435. int CLodHandler::infs(unsigned char * in, int size, int realSize, std::ofstream & out, int wBits)
  436. {
  437. int ret;
  438. unsigned have;
  439. z_stream strm;
  440. unsigned char inx[NLoadHandlerHelp::fCHUNK];
  441. unsigned char outx[NLoadHandlerHelp::fCHUNK];
  442. /* allocate inflate state */
  443. strm.zalloc = Z_NULL;
  444. strm.zfree = Z_NULL;
  445. strm.opaque = Z_NULL;
  446. strm.avail_in = 0;
  447. strm.next_in = Z_NULL;
  448. ret = inflateInit2(&strm, wBits);
  449. if (ret != Z_OK)
  450. return ret;
  451. int chunkNumber = 0;
  452. do
  453. {
  454. int readBytes = 0;
  455. for(int i=0; i<NLoadHandlerHelp::fCHUNK && (chunkNumber * NLoadHandlerHelp::fCHUNK + i)<size; ++i)
  456. {
  457. inx[i] = in[chunkNumber * NLoadHandlerHelp::fCHUNK + i];
  458. ++readBytes;
  459. }
  460. ++chunkNumber;
  461. strm.avail_in = readBytes;
  462. //strm.avail_in = fread(inx, 1, NLoadHandlerHelp::fCHUNK, source);
  463. /*if (in.bad())
  464. {
  465. (void)inflateEnd(&strm);
  466. return Z_ERRNO;
  467. }*/
  468. if (strm.avail_in == 0)
  469. break;
  470. strm.next_in = inx;
  471. /* run inflate() on input until output buffer not full */
  472. do
  473. {
  474. strm.avail_out = NLoadHandlerHelp::fCHUNK;
  475. strm.next_out = outx;
  476. ret = inflate(&strm, Z_NO_FLUSH);
  477. //assert(ret != Z_STREAM_ERROR); /* state not clobbered */
  478. switch (ret)
  479. {
  480. case Z_NEED_DICT:
  481. ret = Z_DATA_ERROR; /* and fall through */
  482. case Z_DATA_ERROR:
  483. case Z_MEM_ERROR:
  484. (void)inflateEnd(&strm);
  485. return ret;
  486. }
  487. have = NLoadHandlerHelp::fCHUNK - strm.avail_out;
  488. /*if (fwrite(out, 1, have, dest) != have || ferror(dest))
  489. {
  490. (void)inflateEnd(&strm);
  491. return Z_ERRNO;
  492. }*/
  493. out.write((char*)outx, have);
  494. if(out.bad())
  495. {
  496. (void)inflateEnd(&strm);
  497. return Z_ERRNO;
  498. }
  499. } while (strm.avail_out == 0);
  500. /* done when inflate() says it's done */
  501. } while (ret != Z_STREAM_END);
  502. /* clean up and return */
  503. (void)inflateEnd(&strm);
  504. return ret == Z_STREAM_END ? Z_OK : Z_DATA_ERROR;
  505. }
  506. int CLodHandler::infs2(unsigned char * in, int size, int realSize, unsigned char *& out, int wBits)
  507. {
  508. int ret;
  509. unsigned have;
  510. z_stream strm;
  511. unsigned char inx[NLoadHandlerHelp::fCHUNK];
  512. unsigned char outx[NLoadHandlerHelp::fCHUNK];
  513. out = new unsigned char [realSize];
  514. int latPosOut = 0;
  515. /* allocate inflate state */
  516. strm.zalloc = Z_NULL;
  517. strm.zfree = Z_NULL;
  518. strm.opaque = Z_NULL;
  519. strm.avail_in = 0;
  520. strm.next_in = Z_NULL;
  521. ret = inflateInit2(&strm, wBits);
  522. if (ret != Z_OK)
  523. return ret;
  524. int chunkNumber = 0;
  525. do
  526. {
  527. int readBytes = 0;
  528. for(int i=0; i<NLoadHandlerHelp::fCHUNK && (chunkNumber * NLoadHandlerHelp::fCHUNK + i)<size; ++i)
  529. {
  530. inx[i] = in[chunkNumber * NLoadHandlerHelp::fCHUNK + i];
  531. ++readBytes;
  532. }
  533. ++chunkNumber;
  534. strm.avail_in = readBytes;
  535. //strm.avail_in = fread(inx, 1, NLoadHandlerHelp::fCHUNK, source);
  536. /*if (in.bad())
  537. {
  538. (void)inflateEnd(&strm);
  539. return Z_ERRNO;
  540. }*/
  541. if (strm.avail_in == 0)
  542. break;
  543. strm.next_in = inx;
  544. /* run inflate() on input until output buffer not full */
  545. do
  546. {
  547. strm.avail_out = NLoadHandlerHelp::fCHUNK;
  548. strm.next_out = outx;
  549. ret = inflate(&strm, Z_NO_FLUSH);
  550. //assert(ret != Z_STREAM_ERROR); /* state not clobbered */
  551. switch (ret)
  552. {
  553. case Z_NEED_DICT:
  554. ret = Z_DATA_ERROR; /* and fall through */
  555. case Z_DATA_ERROR:
  556. case Z_MEM_ERROR:
  557. (void)inflateEnd(&strm);
  558. return ret;
  559. }
  560. have = NLoadHandlerHelp::fCHUNK - strm.avail_out;
  561. /*if (fwrite(out, 1, have, dest) != have || ferror(dest))
  562. {
  563. (void)inflateEnd(&strm);
  564. return Z_ERRNO;
  565. }*/
  566. //out.write((char*)outx, have);
  567. for(int oo=0; oo<have; ++oo)
  568. {
  569. out[latPosOut] = outx[oo];
  570. ++latPosOut;
  571. }
  572. /*if(out.bad())
  573. {
  574. (void)inflateEnd(&strm);
  575. return Z_ERRNO;
  576. }*/
  577. } while (strm.avail_out == 0);
  578. /* done when inflate() says it's done */
  579. } while (ret != Z_STREAM_END);
  580. /* clean up and return */
  581. (void)inflateEnd(&strm);
  582. return ret == Z_STREAM_END ? Z_OK : Z_DATA_ERROR;
  583. }
  584. void CLodHandler::extract(std::string FName)
  585. {
  586. std::ofstream FOut;
  587. int i;
  588. //std::cout<<" done\n";
  589. for (int i=0;i<totalFiles;i++)
  590. {
  591. FLOD.seekg(entries[i].offset,std::ios_base::beg);
  592. std::string bufff = (FName.substr(0, FName.size()-4) + "\\" + (char*)entries[i].name);
  593. unsigned char * outp;
  594. if (entries[i].size==0) //file is not compressed
  595. {
  596. outp = new unsigned char[entries[i].realSize];
  597. FLOD.read((char*)outp, entries[i].realSize);
  598. std::ofstream out;
  599. out.open(bufff.c_str(), std::ios::binary);
  600. if(!out.is_open())
  601. {
  602. std::cout<<"Unable to create "<<bufff;
  603. }
  604. else
  605. {
  606. for(int hh=0; hh<entries[i].realSize; ++hh)
  607. {
  608. out<<*(outp+hh);
  609. }
  610. out.close();
  611. }
  612. }
  613. else
  614. {
  615. outp = new unsigned char[entries[i].size];
  616. FLOD.read((char*)outp, entries[i].size);
  617. FLOD.seekg(0, std::ios_base::beg);
  618. std::ofstream destin;
  619. destin.open(bufff.c_str(), std::ios::binary);
  620. //int decRes = decompress(outp, entries[i].size, entries[i].realSize, bufff);
  621. int decRes = infs(outp, entries[i].size, entries[i].realSize, destin);
  622. destin.close();
  623. if(decRes!=0)
  624. {
  625. std::cout<<"LOD Extraction error"<<" "<<decRes<<" while extracting to "<<bufff<<std::endl;
  626. }
  627. }
  628. //for (int j=0; j<entries[i].size; j++)
  629. // FOut << outp[j];
  630. //FOut.flush();
  631. delete outp;
  632. //FOut.close();
  633. //std::cout<<"*** done\n";
  634. }
  635. FLOD.close();
  636. //std::cout<<"*** Archive: "+FName+" closed\n";
  637. }
  638. void CLodHandler::extractFile(std::string FName, std::string name)
  639. {
  640. //std::ifstream FLOD;
  641. std::ofstream FOut;
  642. int i;
  643. //std::string Ts;
  644. ////std::cout<<"*** Loading FAT ... \n";
  645. //FLOD.open(FName.c_str(),std::ios::binary);
  646. ////std::cout<<"*** Archive: "+FName+" loaded\n";
  647. //FLOD.seekg(8,std::ios_base::beg);
  648. //unsigned char temp[4];
  649. //FLOD.read((char*)temp,4);
  650. //totalFiles = readNormalNr(temp,4);
  651. //FLOD.seekg(0x5c,std::ios_base::beg);
  652. //entries.reserve(totalFiles);
  653. ////std::cout<<"*** Loading FAT ...\n";
  654. //for (int i=0; i<totalFiles; i++)
  655. //{
  656. // entries.push_back(Entry());
  657. // //FLOD.read((char*)entries[i].name,12);
  658. // char * bufc = new char;
  659. // bool appending = true;
  660. // for(int kk=0; kk<12; ++kk)
  661. // {
  662. // FLOD.read(bufc, 1);
  663. // if(appending)
  664. // {
  665. // entries[i].name[kk] = *bufc;
  666. // }
  667. // else
  668. // {
  669. // entries[i].name[kk] = 0;
  670. // appending = false;
  671. // }
  672. // }
  673. // delete bufc;
  674. // FLOD.read((char*)entries[i].hlam_1,4);
  675. // FLOD.read((char*)temp,4);
  676. // entries[i].offset=readNormalNr(temp,4);
  677. // FLOD.read((char*)temp,4);
  678. // entries[i].realSize=readNormalNr(temp,4);
  679. // FLOD.read((char*)entries[i].hlam_2,4);
  680. // FLOD.read((char*)temp,4);
  681. // entries[i].size=readNormalNr(temp,4);
  682. //}
  683. //std::cout<<" done\n";
  684. std::transform(name.begin(), name.end(), name.begin(), (int(*)(int))toupper);
  685. for (int i=0;i<totalFiles;i++)
  686. {
  687. std::string buf1 = std::string((char*)entries[i].name);
  688. std::transform(buf1.begin(), buf1.end(), buf1.begin(), (int(*)(int))toupper);
  689. if(buf1!=name)
  690. continue;
  691. FLOD.seekg(entries[i].offset,std::ios_base::beg);
  692. std::string bufff = (FName.substr(0, FName.size()-4) + "\\" + (char*)entries[i].name);
  693. unsigned char * outp;
  694. if (entries[i].size==0) //file is not compressed
  695. {
  696. outp = new unsigned char[entries[i].realSize];
  697. FLOD.read((char*)outp, entries[i].realSize);
  698. std::ofstream out;
  699. out.open(bufff.c_str(), std::ios::binary);
  700. if(!out.is_open())
  701. {
  702. std::cout<<"Unable to create "<<bufff;
  703. }
  704. else
  705. {
  706. for(int hh=0; hh<entries[i].realSize; ++hh)
  707. {
  708. out<<*(outp+hh);
  709. }
  710. out.close();
  711. }
  712. }
  713. else //we will decompressing file
  714. {
  715. outp = new unsigned char[entries[i].size];
  716. FLOD.read((char*)outp, entries[i].size);
  717. FLOD.seekg(0, std::ios_base::beg);
  718. std::ofstream destin;
  719. destin.open(bufff.c_str(), std::ios::binary);
  720. //int decRes = decompress(outp, entries[i].size, entries[i].realSize, bufff);
  721. int decRes = infs(outp, entries[i].size, entries[i].realSize, destin);
  722. destin.close();
  723. if(decRes!=0)
  724. {
  725. std::cout<<"LOD Extraction error"<<" "<<decRes<<" while extracting to "<<bufff<<std::endl;
  726. }
  727. }
  728. //for (int j=0; j<entries[i].size; j++)
  729. // FOut << outp[j];
  730. //FOut.flush();
  731. delete outp;
  732. //FOut.close();
  733. //std::cout<<"*** done\n";
  734. }
  735. FLOD.close();
  736. //std::cout<<"*** Archive: "+FName+" closed\n";
  737. }
  738. int CLodHandler::readNormalNr (unsigned char* bufor, int bytCon, bool cyclic)
  739. {
  740. int ret=0;
  741. int amp=1;
  742. for (int i=0; i<bytCon; i++)
  743. {
  744. ret+=bufor[i]*amp;
  745. amp*=256;
  746. }
  747. if(cyclic && bytCon<4 && ret>=amp/2)
  748. {
  749. ret = ret-amp;
  750. }
  751. return ret;
  752. }
  753. void CLodHandler::init(std::string lodFile)
  754. {
  755. FLOD;
  756. std::string Ts;
  757. //std::cout<<"*** Loading FAT ... \n";
  758. FLOD.open(lodFile.c_str(),std::ios::binary);
  759. //std::cout<<"*** Archive: "+FName+" loaded\n";
  760. FLOD.seekg(8,std::ios_base::beg);
  761. unsigned char temp[4];
  762. FLOD.read((char*)temp,4);
  763. totalFiles = readNormalNr(temp,4);
  764. FLOD.seekg(0x5c,std::ios_base::beg);
  765. entries.reserve(totalFiles);
  766. //std::cout<<"*** Loading FAT ...\n";
  767. for (int i=0; i<totalFiles; i++)
  768. {
  769. entries.push_back(Entry());
  770. //FLOD.read((char*)entries[i].name,12);
  771. char * bufc = new char;
  772. bool appending = true;
  773. for(int kk=0; kk<12; ++kk)
  774. {
  775. FLOD.read(bufc, 1);
  776. if(appending)
  777. {
  778. entries[i].name[kk] = toupper(*bufc);
  779. }
  780. else
  781. {
  782. entries[i].name[kk] = 0;
  783. appending = false;
  784. }
  785. }
  786. delete bufc;
  787. FLOD.read((char*)entries[i].hlam_1,4);
  788. FLOD.read((char*)temp,4);
  789. entries[i].offset=readNormalNr(temp,4);
  790. FLOD.read((char*)temp,4);
  791. entries[i].realSize=readNormalNr(temp,4);
  792. FLOD.read((char*)entries[i].hlam_2,4);
  793. FLOD.read((char*)temp,4);
  794. entries[i].size=readNormalNr(temp,4);
  795. }
  796. }