CLodHandler.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932
  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 * CPCXConv::getSurfaceZ()
  251. {
  252. //int i=1;
  253. //int type = pcx[i];i+=2;//0 -- Version 2.5 2 -- Version 2.8, palette included 3 -- Version 2.8, use default palette 5 -- Version 3.0 or better
  254. //int bpp = pcx[3];i++;
  255. //int xmin, ymin, xmax, ymax, w, h;
  256. //xmin = readNormalNr(i,2,pcx);i+=2;
  257. //ymin = readNormalNr(i,2,pcx);i+=2;
  258. //xmax = readNormalNr(i,2,pcx);i+=2;
  259. //ymax = readNormalNr(i,2,pcx);i+=2;
  260. //w = xmax - xmin + 1; h = ymax - ymin + 1;
  261. //i+=4; //DPI is not interesting
  262. //char palette[48];
  263. //memcpy(palette,pcx+i,48); i+=48;
  264. //int reserved = pcx[i++];
  265. //int planes = pcx[i++];
  266. //int bytesPerLine = readNormalNr(i,2,pcx);i+=2;
  267. //i = 128; //to the end of header
  268. //int totalBytes = planes * bytesPerLine;
  269. //int padding = ((bytesPerLine * planes) * (8 / bpp)) - ((xmax - xmin) + 1);
  270. //char * buffer = new char[totalBytes];
  271. //int index = 0, count, value, total=0;
  272. //do
  273. //{
  274. // count=0; value=0;
  275. // unsigned char byte = pcx[i++];
  276. // if((byte & 0xC0) == 0xC0) //two top bits set
  277. // {
  278. // count = byte & 0x3f;
  279. // value = pcx[i++];
  280. // }
  281. // else
  282. // {
  283. // count = 1;
  284. // value = byte;
  285. // }
  286. // for(total+=count; count && (index < totalBytes); index++)
  287. // {
  288. // buffer[index] = value;
  289. // count--;
  290. // }
  291. //} while(index<totalBytes);
  292. //delete buffer;
  293. std::cout<<"Warning - not supported ZSoft-style .png file!!!"<<std::endl;
  294. return NULL;
  295. }
  296. SDL_Surface * CLodHandler::loadBitmap(std::string fname)
  297. {
  298. if(!fname.size())
  299. return NULL;
  300. unsigned char * pcx;
  301. std::transform(fname.begin(),fname.end(),fname.begin(),toupper);
  302. fname.replace(fname.find_first_of('.'),fname.find_first_of('.')+4,".PCX");
  303. Entry *e = entries.znajdz(fname);
  304. if(!e)
  305. {
  306. std::cout<<"File "<<fname<<" not found"<<std::endl;
  307. return NULL;
  308. }
  309. if(e->offset<0)
  310. {
  311. fname.replace(fname.find_first_of('.'),fname.find_first_of('.')+4,".BMP");
  312. fname = "Data/"+fname;
  313. FILE * f = fopen(fname.c_str(),"r");
  314. if(f)
  315. {
  316. fclose(f);
  317. return SDL_LoadBMP(fname.c_str());
  318. }
  319. else //file .bmp not present, check .pcx
  320. {
  321. //TODO: zczytywanie nawala, dokonczyc wsparcie zsoftowych pcxow
  322. char sign[3];
  323. fname.replace(fname.find_first_of('.'),fname.find_first_of('.')+4,".PCX");
  324. f = fopen(fname.c_str(),"r");
  325. if(!f)
  326. return NULL;
  327. fread(sign,1,3,f);
  328. if(sign[0]=='B' && sign[1]=='M') //BMP - ¿eby Kulex móg³ mieszaæ PCXy z BMPami
  329. {
  330. fclose(f);
  331. return SDL_LoadBMP(fname.c_str());
  332. }
  333. else //PCX
  334. {
  335. CPCXConv cp;
  336. pcx = new unsigned char[e->realSize];
  337. memcpy(pcx,sign,3);
  338. int res = fread((char*)pcx+3, 1, e->realSize-3, f);
  339. fclose(f);
  340. cp.openPCX((char*)pcx,e->realSize);
  341. if((sign[0]==10) && (sign[1]<6) && (sign[2]==1)) //ZSoft PCX
  342. {
  343. return cp.getSurfaceZ();
  344. }
  345. else //H3 PCX
  346. {
  347. return cp.getSurface();
  348. }
  349. }
  350. }
  351. }
  352. fseek(FLOD, e->offset, 0);
  353. if (e->size==0) //file is not compressed
  354. {
  355. pcx = new unsigned char[e->realSize];
  356. fread((char*)pcx, 1, e->realSize, FLOD);
  357. }
  358. else
  359. {
  360. unsigned char * pcd = new unsigned char[e->size];
  361. fread((char*)pcd, 1, e->size, FLOD);
  362. int res=infs2(pcd,e->size,e->realSize,pcx);
  363. if(res!=0)
  364. {
  365. std::cout<<"an error "<<res<<" occured while extracting file "<<fname<<std::endl;
  366. }
  367. delete [] pcd;
  368. }
  369. CPCXConv cp;
  370. cp.openPCX((char*)pcx,e->realSize);
  371. return cp.getSurface();
  372. }
  373. int CLodHandler::decompress (unsigned char * source, int size, int realSize, std::string & dest)
  374. {
  375. std::ofstream lb;
  376. lb.open("lodbuf\\buf.gz", std::ios::out|std::ios::binary);
  377. for(int i=0; i<size; ++i)
  378. {
  379. lb<<source[i];
  380. }
  381. lb.close();
  382. FILE * inputf = fopen("lodbuf\\buf.gz", "rb+");
  383. FILE * outputf = fopen(dest.c_str(), "wb+");
  384. int ret = infm(inputf, outputf);
  385. fclose(inputf);
  386. fclose(outputf);
  387. return ret;
  388. }
  389. int CLodHandler::infm(FILE *source, FILE *dest, int wBits)
  390. {
  391. int ret;
  392. unsigned have;
  393. z_stream strm;
  394. unsigned char in[NLoadHandlerHelp::fCHUNK];
  395. unsigned char out[NLoadHandlerHelp::fCHUNK];
  396. /* allocate inflate state */
  397. strm.zalloc = Z_NULL;
  398. strm.zfree = Z_NULL;
  399. strm.opaque = Z_NULL;
  400. strm.avail_in = 0;
  401. strm.next_in = Z_NULL;
  402. ret = inflateInit2(&strm, wBits);
  403. if (ret != Z_OK)
  404. return ret;
  405. /* decompress until deflate stream ends or end of file */
  406. do
  407. {
  408. strm.avail_in = fread(in, 1, NLoadHandlerHelp::fCHUNK, source);
  409. if (ferror(source)) {
  410. (void)inflateEnd(&strm);
  411. return Z_ERRNO;
  412. }
  413. if (strm.avail_in == 0)
  414. break;
  415. strm.next_in = in;
  416. /* run inflate() on input until output buffer not full */
  417. do
  418. {
  419. strm.avail_out = NLoadHandlerHelp::fCHUNK;
  420. strm.next_out = out;
  421. ret = inflate(&strm, Z_NO_FLUSH);
  422. //assert(ret != Z_STREAM_ERROR); /* state not clobbered */
  423. switch (ret)
  424. {
  425. case Z_NEED_DICT:
  426. ret = Z_DATA_ERROR; /* and fall through */
  427. case Z_DATA_ERROR:
  428. case Z_MEM_ERROR:
  429. (void)inflateEnd(&strm);
  430. return ret;
  431. }
  432. have = NLoadHandlerHelp::fCHUNK - strm.avail_out;
  433. if (fwrite(out, 1, have, dest) != have || ferror(dest))
  434. {
  435. (void)inflateEnd(&strm);
  436. return Z_ERRNO;
  437. }
  438. } while (strm.avail_out == 0);
  439. /* done when inflate() says it's done */
  440. } while (ret != Z_STREAM_END);
  441. /* clean up and return */
  442. (void)inflateEnd(&strm);
  443. return ret == Z_STREAM_END ? Z_OK : Z_DATA_ERROR;
  444. }
  445. CDefHandler * CLodHandler::giveDef(std::string defName)
  446. {
  447. std::transform(defName.begin(), defName.end(), defName.begin(), (int(*)(int))toupper);
  448. Entry * ourEntry = entries.znajdz(Entry(defName));
  449. if(!ourEntry) //nothing's been found
  450. {
  451. std::cerr<<"No def: "<<defName;
  452. throw(std::string("Bad def name!"));
  453. }
  454. CDefHandler * ret;
  455. fseek(FLOD, ourEntry->offset, 0);
  456. unsigned char * outp;
  457. if (ourEntry->offset<0) //file in the sprites/ folder
  458. {
  459. char * outp = new char[ourEntry->realSize];
  460. char name[120];for(int i=0;i<120;i++) name[i]='\0';
  461. strcat(name,"Sprites/");
  462. strcat(name,(char*)ourEntry->name);
  463. FILE * f = fopen(name,"rb");
  464. int result = fread(outp,1,ourEntry->realSize,f);
  465. if(result<0) {std::cout<<"Error in file reading: "<<name<<std::endl;delete[] outp; return NULL;}
  466. CDefHandler * nh = new CDefHandler();
  467. nh->openFromMemory((unsigned char*)outp, ourEntry->realSize, std::string((char*)ourEntry->name));
  468. nh->alphaTransformed = false;
  469. ret = nh;
  470. delete[] outp;
  471. }
  472. else if (ourEntry->size==0) //file is not compressed
  473. {
  474. outp = new unsigned char[ourEntry->realSize];
  475. fread((char*)outp, 1, ourEntry->realSize, FLOD);
  476. CDefHandler * nh = new CDefHandler();
  477. nh->openFromMemory(outp, ourEntry->realSize, std::string((char*)ourEntry->name));
  478. nh->alphaTransformed = false;
  479. ret = nh;
  480. delete[] outp;
  481. }
  482. else //we will decompress file
  483. {
  484. outp = new unsigned char[ourEntry->size];
  485. fread((char*)outp, 1, ourEntry->size, FLOD);
  486. fseek(FLOD, 0, 0);
  487. unsigned char * decomp = NULL;
  488. int decRes = infs2(outp, ourEntry->size, ourEntry->realSize, decomp);
  489. CDefHandler * nh = new CDefHandler();
  490. nh->openFromMemory(decomp, ourEntry->realSize, std::string((char*)ourEntry->name));
  491. nh->alphaTransformed = false;
  492. ret = nh;
  493. delete[] decomp;
  494. delete[] outp;
  495. }
  496. return ret;
  497. }
  498. CDefEssential * CLodHandler::giveDefEss(std::string defName)
  499. {
  500. CDefEssential * ret;
  501. CDefHandler * temp = giveDef(defName);
  502. ret = temp->essentialize();
  503. delete temp;
  504. return ret;
  505. }
  506. std::vector<CDefHandler *> CLodHandler::extractManyFiles(std::vector<std::string> defNamesIn)
  507. {
  508. std::vector<CDefHandler *> ret(defNamesIn.size());
  509. for(int hh=0; hh<defNamesIn.size(); ++hh)
  510. {
  511. //std::transform(defNamesIn[hh].begin(), defNamesIn[hh].end(), defNamesIn[hh].begin(), (int(*)(int))toupper);
  512. Entry * e = entries.znajdz(defNamesIn[hh]);
  513. if(!e)
  514. continue;
  515. fseek(FLOD, e->offset, 0);
  516. unsigned char * outp;
  517. if (e->size==0) //file is not compressed
  518. {
  519. outp = new unsigned char[e->realSize];
  520. fread((char*)outp, 1, e->realSize, FLOD);
  521. CDefHandler * nh = new CDefHandler;
  522. nh->openFromMemory(outp, e->realSize, std::string((char*)e->name));
  523. nh->alphaTransformed = false;
  524. ret[hh] = nh;
  525. }
  526. else //we will decompressing file
  527. {
  528. outp = new unsigned char[e->size];
  529. fread((char*)outp, 1, e->size, FLOD);
  530. fseek(FLOD, 0, 0);
  531. unsigned char * decomp = NULL;
  532. int decRes = infs2(outp, e->size, e->realSize, decomp);
  533. CDefHandler * nh = new CDefHandler;
  534. nh->openFromMemory(decomp, e->realSize, std::string((char*)e->name));
  535. nh->alphaTransformed = false;
  536. delete [] decomp;
  537. ret[hh] = nh;
  538. }
  539. delete[] outp;
  540. }
  541. return ret;
  542. }
  543. int CLodHandler::infs(unsigned char * in, int size, int realSize, std::ofstream & out, int wBits)
  544. {
  545. int ret;
  546. unsigned have;
  547. z_stream strm;
  548. unsigned char inx[NLoadHandlerHelp::fCHUNK];
  549. unsigned char outx[NLoadHandlerHelp::fCHUNK];
  550. /* allocate inflate state */
  551. strm.zalloc = Z_NULL;
  552. strm.zfree = Z_NULL;
  553. strm.opaque = Z_NULL;
  554. strm.avail_in = 0;
  555. strm.next_in = Z_NULL;
  556. ret = inflateInit2(&strm, wBits);
  557. if (ret != Z_OK)
  558. return ret;
  559. int chunkNumber = 0;
  560. do
  561. {
  562. int readBytes = 0;
  563. for(int i=0; i<NLoadHandlerHelp::fCHUNK && (chunkNumber * NLoadHandlerHelp::fCHUNK + i)<size; ++i)
  564. {
  565. inx[i] = in[chunkNumber * NLoadHandlerHelp::fCHUNK + i];
  566. ++readBytes;
  567. }
  568. ++chunkNumber;
  569. strm.avail_in = readBytes;
  570. //strm.avail_in = fread(inx, 1, NLoadHandlerHelp::fCHUNK, source);
  571. /*if (in.bad())
  572. {
  573. (void)inflateEnd(&strm);
  574. return Z_ERRNO;
  575. }*/
  576. if (strm.avail_in == 0)
  577. break;
  578. strm.next_in = inx;
  579. /* run inflate() on input until output buffer not full */
  580. do
  581. {
  582. strm.avail_out = NLoadHandlerHelp::fCHUNK;
  583. strm.next_out = outx;
  584. ret = inflate(&strm, Z_NO_FLUSH);
  585. //assert(ret != Z_STREAM_ERROR); /* state not clobbered */
  586. switch (ret)
  587. {
  588. case Z_NEED_DICT:
  589. ret = Z_DATA_ERROR; /* and fall through */
  590. case Z_DATA_ERROR:
  591. case Z_MEM_ERROR:
  592. (void)inflateEnd(&strm);
  593. return ret;
  594. }
  595. have = NLoadHandlerHelp::fCHUNK - strm.avail_out;
  596. /*if (fwrite(out, 1, have, dest) != have || ferror(dest))
  597. {
  598. (void)inflateEnd(&strm);
  599. return Z_ERRNO;
  600. }*/
  601. out.write((char*)outx, have);
  602. if(out.bad())
  603. {
  604. (void)inflateEnd(&strm);
  605. return Z_ERRNO;
  606. }
  607. } while (strm.avail_out == 0);
  608. /* done when inflate() says it's done */
  609. } while (ret != Z_STREAM_END);
  610. /* clean up and return */
  611. (void)inflateEnd(&strm);
  612. return ret == Z_STREAM_END ? Z_OK : Z_DATA_ERROR;
  613. }
  614. int CLodHandler::infs2(unsigned char * in, int size, int realSize, unsigned char *& out, int wBits)
  615. {
  616. int ret;
  617. unsigned have;
  618. z_stream strm;
  619. unsigned char inx[NLoadHandlerHelp::fCHUNK];
  620. unsigned char outx[NLoadHandlerHelp::fCHUNK];
  621. out = new unsigned char [realSize];
  622. int latPosOut = 0;
  623. /* allocate inflate state */
  624. strm.zalloc = Z_NULL;
  625. strm.zfree = Z_NULL;
  626. strm.opaque = Z_NULL;
  627. strm.avail_in = 0;
  628. strm.next_in = Z_NULL;
  629. ret = inflateInit2(&strm, wBits);
  630. if (ret != Z_OK)
  631. return ret;
  632. int chunkNumber = 0;
  633. do
  634. {
  635. int readBytes = 0;
  636. for(int i=0; i<NLoadHandlerHelp::fCHUNK && (chunkNumber * NLoadHandlerHelp::fCHUNK + i)<size; ++i)
  637. {
  638. inx[i] = in[chunkNumber * NLoadHandlerHelp::fCHUNK + i];
  639. ++readBytes;
  640. }
  641. ++chunkNumber;
  642. strm.avail_in = readBytes;
  643. //strm.avail_in = fread(inx, 1, NLoadHandlerHelp::fCHUNK, source);
  644. /*if (in.bad())
  645. {
  646. (void)inflateEnd(&strm);
  647. return Z_ERRNO;
  648. }*/
  649. if (strm.avail_in == 0)
  650. break;
  651. strm.next_in = inx;
  652. /* run inflate() on input until output buffer not full */
  653. do
  654. {
  655. strm.avail_out = NLoadHandlerHelp::fCHUNK;
  656. strm.next_out = outx;
  657. ret = inflate(&strm, Z_NO_FLUSH);
  658. //assert(ret != Z_STREAM_ERROR); /* state not clobbered */
  659. switch (ret)
  660. {
  661. case Z_NEED_DICT:
  662. ret = Z_DATA_ERROR; /* and fall through */
  663. case Z_DATA_ERROR:
  664. case Z_MEM_ERROR:
  665. (void)inflateEnd(&strm);
  666. return ret;
  667. }
  668. have = NLoadHandlerHelp::fCHUNK - strm.avail_out;
  669. /*if (fwrite(out, 1, have, dest) != have || ferror(dest))
  670. {
  671. (void)inflateEnd(&strm);
  672. return Z_ERRNO;
  673. }*/
  674. //out.write((char*)outx, have);
  675. for(int oo=0; oo<have; ++oo)
  676. {
  677. out[latPosOut] = outx[oo];
  678. ++latPosOut;
  679. }
  680. /*if(out.bad())
  681. {
  682. (void)inflateEnd(&strm);
  683. return Z_ERRNO;
  684. }*/
  685. } while (strm.avail_out == 0);
  686. /* done when inflate() says it's done */
  687. } while (ret != Z_STREAM_END);
  688. /* clean up and return */
  689. (void)inflateEnd(&strm);
  690. return ret == Z_STREAM_END ? Z_OK : Z_DATA_ERROR;
  691. }
  692. void CLodHandler::extract(std::string FName)
  693. {
  694. std::ofstream FOut;
  695. int i;
  696. //std::cout<<" done\n";
  697. for (int i=0;i<totalFiles;i++)
  698. {
  699. fseek(FLOD, entries[i].offset, 0);
  700. std::string bufff = (FName.substr(0, FName.size()-4) + "\\" + (char*)entries[i].name);
  701. unsigned char * outp;
  702. if (entries[i].size==0) //file is not compressed
  703. {
  704. outp = new unsigned char[entries[i].realSize];
  705. fread((char*)outp, 1, entries[i].realSize, FLOD);
  706. std::ofstream out;
  707. out.open(bufff.c_str(), std::ios::binary);
  708. if(!out.is_open())
  709. {
  710. std::cout<<"Unable to create "<<bufff;
  711. }
  712. else
  713. {
  714. for(int hh=0; hh<entries[i].realSize; ++hh)
  715. {
  716. out<<*(outp+hh);
  717. }
  718. out.close();
  719. }
  720. }
  721. else
  722. {
  723. outp = new unsigned char[entries[i].size];
  724. fread((char*)outp, 1, entries[i].size, FLOD);
  725. fseek(FLOD, 0, 0);
  726. std::ofstream destin;
  727. destin.open(bufff.c_str(), std::ios::binary);
  728. //int decRes = decompress(outp, entries[i].size, entries[i].realSize, bufff);
  729. int decRes = infs(outp, entries[i].size, entries[i].realSize, destin);
  730. destin.close();
  731. if(decRes!=0)
  732. {
  733. std::cout<<"LOD Extraction error"<<" "<<decRes<<" while extracting to "<<bufff<<std::endl;
  734. }
  735. }
  736. delete[] outp;
  737. }
  738. fclose(FLOD);
  739. }
  740. void CLodHandler::extractFile(std::string FName, std::string name)
  741. {
  742. std::transform(name.begin(), name.end(), name.begin(), (int(*)(int))toupper);
  743. for (int i=0;i<totalFiles;i++)
  744. {
  745. std::string buf1 = std::string((char*)entries[i].name);
  746. std::transform(buf1.begin(), buf1.end(), buf1.begin(), (int(*)(int))toupper);
  747. if(buf1!=name)
  748. continue;
  749. fseek(FLOD, entries[i].offset, 0);
  750. std::string bufff = (FName);
  751. unsigned char * outp;
  752. if (entries[i].size==0) //file is not compressed
  753. {
  754. outp = new unsigned char[entries[i].realSize];
  755. fread((char*)outp, 1, entries[i].realSize, FLOD);
  756. std::ofstream out;
  757. out.open(bufff.c_str(), std::ios::binary);
  758. if(!out.is_open())
  759. {
  760. std::cout<<"Unable to create "<<bufff;
  761. }
  762. else
  763. {
  764. for(int hh=0; hh<entries[i].realSize; ++hh)
  765. {
  766. out<<*(outp+hh);
  767. }
  768. out.close();
  769. }
  770. }
  771. else //we will decompressing file
  772. {
  773. outp = new unsigned char[entries[i].size];
  774. fread((char*)outp, 1, entries[i].size, FLOD);
  775. fseek(FLOD, 0, 0);
  776. std::ofstream destin;
  777. destin.open(bufff.c_str(), std::ios::binary);
  778. //int decRes = decompress(outp, entries[i].size, entries[i].realSize, bufff);
  779. int decRes = infs(outp, entries[i].size, entries[i].realSize, destin);
  780. destin.close();
  781. if(decRes!=0)
  782. {
  783. std::cout<<"LOD Extraction error"<<" "<<decRes<<" while extracting to "<<bufff<<std::endl;
  784. }
  785. }
  786. delete[] outp;
  787. }
  788. }
  789. int CLodHandler::readNormalNr (unsigned char* bufor, int bytCon, bool cyclic)
  790. {
  791. int ret=0;
  792. int amp=1;
  793. for (int i=0; i<bytCon; i++)
  794. {
  795. ret+=bufor[i]*amp;
  796. amp*=256;
  797. }
  798. if(cyclic && bytCon<4 && ret>=amp/2)
  799. {
  800. ret = ret-amp;
  801. }
  802. return ret;
  803. }
  804. void CLodHandler::init(std::string lodFile)
  805. {
  806. std::string Ts;
  807. FLOD = fopen(lodFile.c_str(), "rb");
  808. fseek(FLOD, 8, 0);
  809. unsigned char temp[4];
  810. fread((char*)temp, 1, 4, FLOD);
  811. totalFiles = readNormalNr(temp,4);
  812. fseek(FLOD, 0x5c, 0);
  813. for (int i=0; i<totalFiles; i++)
  814. {
  815. Entry entry;
  816. char * bufc = new char;
  817. bool appending = true;
  818. for(int kk=0; kk<12; ++kk)
  819. {
  820. //FLOD.read(bufc, 1);
  821. fread(bufc, 1, 1, FLOD);
  822. if(appending)
  823. {
  824. entry.name[kk] = toupper(*bufc);
  825. }
  826. else
  827. {
  828. entry.name[kk] = 0;
  829. appending = false;
  830. }
  831. }
  832. delete bufc;
  833. fread((char*)entry.hlam_1, 1, 4, FLOD);
  834. fread((char*)temp, 1, 4, FLOD);
  835. entry.offset=readNormalNr(temp,4);
  836. fread((char*)temp, 1, 4, FLOD);
  837. entry.realSize=readNormalNr(temp,4);
  838. fread((char*)entry.hlam_2, 1, 4, FLOD);
  839. fread((char*)temp, 1, 4, FLOD);
  840. entry.size=readNormalNr(temp,4);
  841. for (int z=0;z<12;z++)
  842. {
  843. if (entry.name[z])
  844. entry.nameStr+=entry.name[z];
  845. else break;
  846. }
  847. entries.push_back(entry);
  848. }
  849. }
  850. std::string CLodHandler::getTextFile(std::string name)
  851. {
  852. std::string ret0;
  853. std::transform(name.begin(), name.end(), name.begin(), (int(*)(int))toupper);
  854. Entry *e;
  855. e = entries.znajdz(name);
  856. if(!e)
  857. {
  858. std::cout << "Error: cannot load "<<name<<" from the .lod file!"<<std::endl;
  859. return ret0;
  860. }
  861. if(e->offset<0)
  862. {
  863. char * outp = new char[e->realSize];
  864. char name[120];for(int i=0;i<120;i++) name[i]='\0';
  865. strcat(name,"Data/");
  866. strcat(name,(char*)e->name);
  867. FILE * f = fopen(name,"rb");
  868. int result = fread(outp,1,e->realSize,f);
  869. if(result<0) {std::cout<<"Error in file reading: "<<name<<std::endl;return ret0;}
  870. for (int itr=0;itr<e->realSize;itr++)
  871. ret0+= *(outp+itr);
  872. delete[] outp;
  873. return ret0;
  874. }
  875. fseek(FLOD, e->offset, 0);
  876. unsigned char * outp;
  877. if (e->size==0) //file is not compressed
  878. {
  879. outp = new unsigned char[e->realSize];
  880. fread((char*)outp, 1, e->realSize, FLOD);
  881. std::string ret = std::string((char*)outp);
  882. delete[] outp;
  883. return ret;
  884. }
  885. else //we will decompressing file
  886. {
  887. outp = new unsigned char[e->size];
  888. fread((char*)outp, 1, e->size, FLOD);
  889. fseek(FLOD, 0, 0);
  890. unsigned char * decomp = NULL;
  891. int decRes = infs2(outp, e->size, e->realSize, decomp);
  892. std::string ret;
  893. for (int itr=0;itr<e->realSize;itr++)
  894. ret+= *((char*)decomp+itr);
  895. delete[] outp;
  896. delete[] decomp;
  897. return ret;
  898. }
  899. return ret0;
  900. }