CLodHandler.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848
  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. if(!ourEntry) //nothing's been found
  367. {
  368. std::cerr<<"No def: "<<defName;
  369. throw(std::string("Bad def name!"));
  370. }
  371. CDefHandler * ret;
  372. fseek(FLOD, ourEntry->offset, 0);
  373. unsigned char * outp;
  374. if (ourEntry->offset<0) //file in the sprites/ folder
  375. {
  376. char * outp = new char[ourEntry->realSize];
  377. char name[120];for(int i=0;i<120;i++) name[i]='\0';
  378. strcat(name,"Sprites/");
  379. strcat(name,(char*)ourEntry->name);
  380. FILE * f = fopen(name,"rb");
  381. int result = fread(outp,1,ourEntry->realSize,f);
  382. if(result<0) {std::cout<<"Error in file reading: "<<name<<std::endl;delete[] outp; return NULL;}
  383. CDefHandler * nh = new CDefHandler();
  384. nh->openFromMemory((unsigned char*)outp, ourEntry->realSize, std::string((char*)ourEntry->name));
  385. nh->alphaTransformed = false;
  386. ret = nh;
  387. delete[] outp;
  388. }
  389. else if (ourEntry->size==0) //file is not compressed
  390. {
  391. outp = new unsigned char[ourEntry->realSize];
  392. fread((char*)outp, 1, ourEntry->realSize, FLOD);
  393. CDefHandler * nh = new CDefHandler();
  394. nh->openFromMemory(outp, ourEntry->realSize, std::string((char*)ourEntry->name));
  395. nh->alphaTransformed = false;
  396. ret = nh;
  397. delete[] outp;
  398. }
  399. else //we will decompress file
  400. {
  401. outp = new unsigned char[ourEntry->size];
  402. fread((char*)outp, 1, ourEntry->size, FLOD);
  403. fseek(FLOD, 0, 0);
  404. unsigned char * decomp = NULL;
  405. int decRes = infs2(outp, ourEntry->size, ourEntry->realSize, decomp);
  406. CDefHandler * nh = new CDefHandler();
  407. nh->openFromMemory(decomp, ourEntry->realSize, std::string((char*)ourEntry->name));
  408. nh->alphaTransformed = false;
  409. ret = nh;
  410. delete[] decomp;
  411. delete[] outp;
  412. }
  413. return ret;
  414. }
  415. CDefEssential * CLodHandler::giveDefEss(std::string defName)
  416. {
  417. CDefEssential * ret;
  418. CDefHandler * temp = giveDef(defName);
  419. ret = temp->essentialize();
  420. delete temp;
  421. return ret;
  422. }
  423. std::vector<CDefHandler *> CLodHandler::extractManyFiles(std::vector<std::string> defNamesIn)
  424. {
  425. std::vector<CDefHandler *> ret(defNamesIn.size());
  426. for(int hh=0; hh<defNamesIn.size(); ++hh)
  427. {
  428. //std::transform(defNamesIn[hh].begin(), defNamesIn[hh].end(), defNamesIn[hh].begin(), (int(*)(int))toupper);
  429. Entry * e = entries.znajdz(defNamesIn[hh]);
  430. if(!e)
  431. continue;
  432. fseek(FLOD, e->offset, 0);
  433. unsigned char * outp;
  434. if (e->size==0) //file is not compressed
  435. {
  436. outp = new unsigned char[e->realSize];
  437. fread((char*)outp, 1, e->realSize, FLOD);
  438. CDefHandler * nh = new CDefHandler;
  439. nh->openFromMemory(outp, e->realSize, std::string((char*)e->name));
  440. nh->alphaTransformed = false;
  441. ret[hh] = nh;
  442. }
  443. else //we will decompressing file
  444. {
  445. outp = new unsigned char[e->size];
  446. fread((char*)outp, 1, e->size, FLOD);
  447. fseek(FLOD, 0, 0);
  448. unsigned char * decomp = NULL;
  449. int decRes = infs2(outp, e->size, e->realSize, decomp);
  450. CDefHandler * nh = new CDefHandler;
  451. nh->openFromMemory(decomp, e->realSize, std::string((char*)e->name));
  452. nh->alphaTransformed = false;
  453. delete [] decomp;
  454. ret[hh] = nh;
  455. }
  456. delete[] outp;
  457. }
  458. return ret;
  459. }
  460. int CLodHandler::infs(unsigned char * in, int size, int realSize, std::ofstream & out, int wBits)
  461. {
  462. int ret;
  463. unsigned have;
  464. z_stream strm;
  465. unsigned char inx[NLoadHandlerHelp::fCHUNK];
  466. unsigned char outx[NLoadHandlerHelp::fCHUNK];
  467. /* allocate inflate state */
  468. strm.zalloc = Z_NULL;
  469. strm.zfree = Z_NULL;
  470. strm.opaque = Z_NULL;
  471. strm.avail_in = 0;
  472. strm.next_in = Z_NULL;
  473. ret = inflateInit2(&strm, wBits);
  474. if (ret != Z_OK)
  475. return ret;
  476. int chunkNumber = 0;
  477. do
  478. {
  479. int readBytes = 0;
  480. for(int i=0; i<NLoadHandlerHelp::fCHUNK && (chunkNumber * NLoadHandlerHelp::fCHUNK + i)<size; ++i)
  481. {
  482. inx[i] = in[chunkNumber * NLoadHandlerHelp::fCHUNK + i];
  483. ++readBytes;
  484. }
  485. ++chunkNumber;
  486. strm.avail_in = readBytes;
  487. //strm.avail_in = fread(inx, 1, NLoadHandlerHelp::fCHUNK, source);
  488. /*if (in.bad())
  489. {
  490. (void)inflateEnd(&strm);
  491. return Z_ERRNO;
  492. }*/
  493. if (strm.avail_in == 0)
  494. break;
  495. strm.next_in = inx;
  496. /* run inflate() on input until output buffer not full */
  497. do
  498. {
  499. strm.avail_out = NLoadHandlerHelp::fCHUNK;
  500. strm.next_out = outx;
  501. ret = inflate(&strm, Z_NO_FLUSH);
  502. //assert(ret != Z_STREAM_ERROR); /* state not clobbered */
  503. switch (ret)
  504. {
  505. case Z_NEED_DICT:
  506. ret = Z_DATA_ERROR; /* and fall through */
  507. case Z_DATA_ERROR:
  508. case Z_MEM_ERROR:
  509. (void)inflateEnd(&strm);
  510. return ret;
  511. }
  512. have = NLoadHandlerHelp::fCHUNK - strm.avail_out;
  513. /*if (fwrite(out, 1, have, dest) != have || ferror(dest))
  514. {
  515. (void)inflateEnd(&strm);
  516. return Z_ERRNO;
  517. }*/
  518. out.write((char*)outx, have);
  519. if(out.bad())
  520. {
  521. (void)inflateEnd(&strm);
  522. return Z_ERRNO;
  523. }
  524. } while (strm.avail_out == 0);
  525. /* done when inflate() says it's done */
  526. } while (ret != Z_STREAM_END);
  527. /* clean up and return */
  528. (void)inflateEnd(&strm);
  529. return ret == Z_STREAM_END ? Z_OK : Z_DATA_ERROR;
  530. }
  531. int CLodHandler::infs2(unsigned char * in, int size, int realSize, unsigned char *& out, int wBits)
  532. {
  533. int ret;
  534. unsigned have;
  535. z_stream strm;
  536. unsigned char inx[NLoadHandlerHelp::fCHUNK];
  537. unsigned char outx[NLoadHandlerHelp::fCHUNK];
  538. out = new unsigned char [realSize];
  539. int latPosOut = 0;
  540. /* allocate inflate state */
  541. strm.zalloc = Z_NULL;
  542. strm.zfree = Z_NULL;
  543. strm.opaque = Z_NULL;
  544. strm.avail_in = 0;
  545. strm.next_in = Z_NULL;
  546. ret = inflateInit2(&strm, wBits);
  547. if (ret != Z_OK)
  548. return ret;
  549. int chunkNumber = 0;
  550. do
  551. {
  552. int readBytes = 0;
  553. for(int i=0; i<NLoadHandlerHelp::fCHUNK && (chunkNumber * NLoadHandlerHelp::fCHUNK + i)<size; ++i)
  554. {
  555. inx[i] = in[chunkNumber * NLoadHandlerHelp::fCHUNK + i];
  556. ++readBytes;
  557. }
  558. ++chunkNumber;
  559. strm.avail_in = readBytes;
  560. //strm.avail_in = fread(inx, 1, NLoadHandlerHelp::fCHUNK, source);
  561. /*if (in.bad())
  562. {
  563. (void)inflateEnd(&strm);
  564. return Z_ERRNO;
  565. }*/
  566. if (strm.avail_in == 0)
  567. break;
  568. strm.next_in = inx;
  569. /* run inflate() on input until output buffer not full */
  570. do
  571. {
  572. strm.avail_out = NLoadHandlerHelp::fCHUNK;
  573. strm.next_out = outx;
  574. ret = inflate(&strm, Z_NO_FLUSH);
  575. //assert(ret != Z_STREAM_ERROR); /* state not clobbered */
  576. switch (ret)
  577. {
  578. case Z_NEED_DICT:
  579. ret = Z_DATA_ERROR; /* and fall through */
  580. case Z_DATA_ERROR:
  581. case Z_MEM_ERROR:
  582. (void)inflateEnd(&strm);
  583. return ret;
  584. }
  585. have = NLoadHandlerHelp::fCHUNK - strm.avail_out;
  586. /*if (fwrite(out, 1, have, dest) != have || ferror(dest))
  587. {
  588. (void)inflateEnd(&strm);
  589. return Z_ERRNO;
  590. }*/
  591. //out.write((char*)outx, have);
  592. for(int oo=0; oo<have; ++oo)
  593. {
  594. out[latPosOut] = outx[oo];
  595. ++latPosOut;
  596. }
  597. /*if(out.bad())
  598. {
  599. (void)inflateEnd(&strm);
  600. return Z_ERRNO;
  601. }*/
  602. } while (strm.avail_out == 0);
  603. /* done when inflate() says it's done */
  604. } while (ret != Z_STREAM_END);
  605. /* clean up and return */
  606. (void)inflateEnd(&strm);
  607. return ret == Z_STREAM_END ? Z_OK : Z_DATA_ERROR;
  608. }
  609. void CLodHandler::extract(std::string FName)
  610. {
  611. std::ofstream FOut;
  612. int i;
  613. //std::cout<<" done\n";
  614. for (int i=0;i<totalFiles;i++)
  615. {
  616. fseek(FLOD, entries[i].offset, 0);
  617. std::string bufff = (FName.substr(0, FName.size()-4) + "\\" + (char*)entries[i].name);
  618. unsigned char * outp;
  619. if (entries[i].size==0) //file is not compressed
  620. {
  621. outp = new unsigned char[entries[i].realSize];
  622. fread((char*)outp, 1, entries[i].realSize, FLOD);
  623. std::ofstream out;
  624. out.open(bufff.c_str(), std::ios::binary);
  625. if(!out.is_open())
  626. {
  627. std::cout<<"Unable to create "<<bufff;
  628. }
  629. else
  630. {
  631. for(int hh=0; hh<entries[i].realSize; ++hh)
  632. {
  633. out<<*(outp+hh);
  634. }
  635. out.close();
  636. }
  637. }
  638. else
  639. {
  640. outp = new unsigned char[entries[i].size];
  641. fread((char*)outp, 1, entries[i].size, FLOD);
  642. fseek(FLOD, 0, 0);
  643. std::ofstream destin;
  644. destin.open(bufff.c_str(), std::ios::binary);
  645. //int decRes = decompress(outp, entries[i].size, entries[i].realSize, bufff);
  646. int decRes = infs(outp, entries[i].size, entries[i].realSize, destin);
  647. destin.close();
  648. if(decRes!=0)
  649. {
  650. std::cout<<"LOD Extraction error"<<" "<<decRes<<" while extracting to "<<bufff<<std::endl;
  651. }
  652. }
  653. delete[] outp;
  654. }
  655. fclose(FLOD);
  656. }
  657. void CLodHandler::extractFile(std::string FName, std::string name)
  658. {
  659. std::transform(name.begin(), name.end(), name.begin(), (int(*)(int))toupper);
  660. for (int i=0;i<totalFiles;i++)
  661. {
  662. std::string buf1 = std::string((char*)entries[i].name);
  663. std::transform(buf1.begin(), buf1.end(), buf1.begin(), (int(*)(int))toupper);
  664. if(buf1!=name)
  665. continue;
  666. fseek(FLOD, entries[i].offset, 0);
  667. std::string bufff = (FName);
  668. unsigned char * outp;
  669. if (entries[i].size==0) //file is not compressed
  670. {
  671. outp = new unsigned char[entries[i].realSize];
  672. fread((char*)outp, 1, entries[i].realSize, FLOD);
  673. std::ofstream out;
  674. out.open(bufff.c_str(), std::ios::binary);
  675. if(!out.is_open())
  676. {
  677. std::cout<<"Unable to create "<<bufff;
  678. }
  679. else
  680. {
  681. for(int hh=0; hh<entries[i].realSize; ++hh)
  682. {
  683. out<<*(outp+hh);
  684. }
  685. out.close();
  686. }
  687. }
  688. else //we will decompressing file
  689. {
  690. outp = new unsigned char[entries[i].size];
  691. fread((char*)outp, 1, entries[i].size, FLOD);
  692. fseek(FLOD, 0, 0);
  693. std::ofstream destin;
  694. destin.open(bufff.c_str(), std::ios::binary);
  695. //int decRes = decompress(outp, entries[i].size, entries[i].realSize, bufff);
  696. int decRes = infs(outp, entries[i].size, entries[i].realSize, destin);
  697. destin.close();
  698. if(decRes!=0)
  699. {
  700. std::cout<<"LOD Extraction error"<<" "<<decRes<<" while extracting to "<<bufff<<std::endl;
  701. }
  702. }
  703. delete[] outp;
  704. }
  705. }
  706. int CLodHandler::readNormalNr (unsigned char* bufor, int bytCon, bool cyclic)
  707. {
  708. int ret=0;
  709. int amp=1;
  710. for (int i=0; i<bytCon; i++)
  711. {
  712. ret+=bufor[i]*amp;
  713. amp*=256;
  714. }
  715. if(cyclic && bytCon<4 && ret>=amp/2)
  716. {
  717. ret = ret-amp;
  718. }
  719. return ret;
  720. }
  721. void CLodHandler::init(std::string lodFile)
  722. {
  723. std::string Ts;
  724. FLOD = fopen(lodFile.c_str(), "rb");
  725. fseek(FLOD, 8, 0);
  726. unsigned char temp[4];
  727. fread((char*)temp, 1, 4, FLOD);
  728. totalFiles = readNormalNr(temp,4);
  729. fseek(FLOD, 0x5c, 0);
  730. for (int i=0; i<totalFiles; i++)
  731. {
  732. Entry entry;
  733. char * bufc = new char;
  734. bool appending = true;
  735. for(int kk=0; kk<12; ++kk)
  736. {
  737. //FLOD.read(bufc, 1);
  738. fread(bufc, 1, 1, FLOD);
  739. if(appending)
  740. {
  741. entry.name[kk] = toupper(*bufc);
  742. }
  743. else
  744. {
  745. entry.name[kk] = 0;
  746. appending = false;
  747. }
  748. }
  749. delete bufc;
  750. fread((char*)entry.hlam_1, 1, 4, FLOD);
  751. fread((char*)temp, 1, 4, FLOD);
  752. entry.offset=readNormalNr(temp,4);
  753. fread((char*)temp, 1, 4, FLOD);
  754. entry.realSize=readNormalNr(temp,4);
  755. fread((char*)entry.hlam_2, 1, 4, FLOD);
  756. fread((char*)temp, 1, 4, FLOD);
  757. entry.size=readNormalNr(temp,4);
  758. for (int z=0;z<12;z++)
  759. {
  760. if (entry.name[z])
  761. entry.nameStr+=entry.name[z];
  762. else break;
  763. }
  764. entries.push_back(entry);
  765. }
  766. }
  767. std::string CLodHandler::getTextFile(std::string name)
  768. {
  769. std::string ret0;
  770. std::transform(name.begin(), name.end(), name.begin(), (int(*)(int))toupper);
  771. Entry *e;
  772. e = entries.znajdz(name);
  773. if(!e)
  774. {
  775. std::cout << "Error: cannot load "<<name<<" from the .lod file!"<<std::endl;
  776. return ret0;
  777. }
  778. if(e->offset<0)
  779. {
  780. char * outp = new char[e->realSize];
  781. char name[120];for(int i=0;i<120;i++) name[i]='\0';
  782. strcat(name,"Data/");
  783. strcat(name,(char*)e->name);
  784. FILE * f = fopen(name,"rb");
  785. int result = fread(outp,1,e->realSize,f);
  786. if(result<0) {std::cout<<"Error in file reading: "<<name<<std::endl;return ret0;}
  787. for (int itr=0;itr<e->realSize;itr++)
  788. ret0+= *(outp+itr);
  789. delete[] outp;
  790. return ret0;
  791. }
  792. fseek(FLOD, e->offset, 0);
  793. unsigned char * outp;
  794. if (e->size==0) //file is not compressed
  795. {
  796. outp = new unsigned char[e->realSize];
  797. fread((char*)outp, 1, e->realSize, FLOD);
  798. std::string ret = std::string((char*)outp);
  799. delete[] outp;
  800. return ret;
  801. }
  802. else //we will decompressing file
  803. {
  804. outp = new unsigned char[e->size];
  805. fread((char*)outp, 1, e->size, FLOD);
  806. fseek(FLOD, 0, 0);
  807. unsigned char * decomp = NULL;
  808. int decRes = infs2(outp, e->size, e->realSize, decomp);
  809. std::string ret;
  810. for (int itr=0;itr<e->realSize;itr++)
  811. ret+= *((char*)decomp+itr);
  812. delete[] outp;
  813. delete[] decomp;
  814. return ret;
  815. }
  816. return ret0;
  817. }