CLodHandler.cpp 20 KB

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