grfio.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818
  1. // Copyright (c) Athena Dev Teams - Licensed under GNU GPL
  2. // For more information, see LICENCE in the main folder
  3. #include "../common/cbasetypes.h"
  4. #include "../common/des.h"
  5. #include "../common/malloc.h"
  6. #include "../common/showmsg.h"
  7. #include "../common/strlib.h"
  8. #include "../common/utils.h"
  9. #include "grfio.h"
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include <sys/stat.h>
  14. #include <zlib.h>
  15. //----------------------------
  16. // file entry table struct
  17. //----------------------------
  18. typedef struct _FILELIST {
  19. int srclen; // compressed size
  20. int srclen_aligned;
  21. int declen; // original size
  22. int srcpos; // position of entry in grf
  23. int next; // index of next filelist entry with same hash (-1: end of entry chain)
  24. char type;
  25. char fn[128-4*5]; // file name
  26. char* fnd; // if the file was cloned, contains name of original file
  27. char gentry; // read grf file select
  28. } FILELIST;
  29. #define FILELIST_TYPE_FILE 0x01 // entry is a file
  30. #define FILELIST_TYPE_ENCRYPT_HEADER 0x04 // encryption mode 1 (header DES only)
  31. #define FILELIST_TYPE_ENCRYPT_MIXED 0x02 // encryption mode 0 (header DES + periodic DES/shuffle)
  32. //gentry ... > 0 : data read from a grf file (gentry_table[gentry-1])
  33. //gentry ... 0 : data read from a local file (data directory)
  34. //gentry ... < 0 : entry "-(gentry)" is marked for a local file check
  35. // - if local file exists, gentry will be set to 0 (thus using the local file)
  36. // - if local file doesn't exist, sign is inverted (thus using the original file inside a grf)
  37. // (NOTE: this case is only used once (during startup) and only if GRFIO_LOCAL is enabled)
  38. // (NOTE: probably meant to be used to override grf contents by files in the data directory)
  39. //#define GRFIO_LOCAL
  40. // stores info about every loaded file
  41. FILELIST* filelist = NULL;
  42. int filelist_entrys = 0;
  43. int filelist_maxentry = 0;
  44. // stores grf file names
  45. char** gentry_table = NULL;
  46. int gentry_entrys = 0;
  47. int gentry_maxentry = 0;
  48. // the path to the data directory
  49. char data_dir[1024] = "";
  50. // little endian char array to uint conversion
  51. static unsigned int getlong(unsigned char* p)
  52. {
  53. return (p[0] << 0 | p[1] << 8 | p[2] << 16 | p[3] << 24);
  54. }
  55. static void NibbleSwap(unsigned char* src, int len)
  56. {
  57. while( len > 0 )
  58. {
  59. *src = (*src >> 4) | (*src << 4);
  60. ++src;
  61. --len;
  62. }
  63. }
  64. /// Substitutes some specific values for others, leaves rest intact. Obfuscation.
  65. /// NOTE: Operation is symmetric (calling it twice gives back the original input).
  66. static uint8_t grf_substitution(uint8_t in)
  67. {
  68. uint8_t out;
  69. switch( in )
  70. {
  71. case 0x00: out = 0x2B; break;
  72. case 0x2B: out = 0x00; break;
  73. case 0x6C: out = 0x80; break;
  74. case 0x01: out = 0x68; break;
  75. case 0x68: out = 0x01; break;
  76. case 0x48: out = 0x77; break;
  77. case 0x60: out = 0xFF; break;
  78. case 0x77: out = 0x48; break;
  79. case 0xB9: out = 0xC0; break;
  80. case 0xC0: out = 0xB9; break;
  81. case 0xFE: out = 0xEB; break;
  82. case 0xEB: out = 0xFE; break;
  83. case 0x80: out = 0x6C; break;
  84. case 0xFF: out = 0x60; break;
  85. default: out = in; break;
  86. }
  87. return out;
  88. }
  89. /* this is not used anywhere, is it ok to delete? */
  90. //static void grf_shuffle_enc(BIT64* src) {
  91. // BIT64 out;
  92. //
  93. // out.b[0] = src->b[3];
  94. // out.b[1] = src->b[4];
  95. // out.b[2] = src->b[5];
  96. // out.b[3] = src->b[0];
  97. // out.b[4] = src->b[1];
  98. // out.b[5] = src->b[6];
  99. // out.b[6] = src->b[2];
  100. // out.b[7] = grf_substitution(src->b[7]);
  101. //
  102. // *src = out;
  103. //}
  104. static void grf_shuffle_dec(BIT64* src)
  105. {
  106. BIT64 out;
  107. out.b[0] = src->b[3];
  108. out.b[1] = src->b[4];
  109. out.b[2] = src->b[6];
  110. out.b[3] = src->b[0];
  111. out.b[4] = src->b[1];
  112. out.b[5] = src->b[2];
  113. out.b[6] = src->b[5];
  114. out.b[7] = grf_substitution(src->b[7]);
  115. *src = out;
  116. }
  117. static void grf_decode_header(unsigned char* buf, size_t len)
  118. {
  119. BIT64* p = (BIT64*)buf;
  120. size_t nblocks = len / sizeof(BIT64);
  121. size_t i;
  122. // first 20 blocks are all des-encrypted
  123. for( i = 0; i < 20 && i < nblocks; ++i )
  124. des_decrypt_block(&p[i]);
  125. // the rest is plaintext, done.
  126. }
  127. static void grf_decode_full(unsigned char* buf, size_t len, int cycle)
  128. {
  129. BIT64* p = (BIT64*)buf;
  130. size_t nblocks = len / sizeof(BIT64);
  131. int dcycle, scycle;
  132. size_t i, j;
  133. // first 20 blocks are all des-encrypted
  134. for( i = 0; i < 20 && i < nblocks; ++i )
  135. des_decrypt_block(&p[i]);
  136. // after that only one of every 'dcycle' blocks is des-encrypted
  137. dcycle = cycle;
  138. // and one of every 'scycle' plaintext blocks is shuffled (starting from the 0th but skipping the 0th)
  139. scycle = 7;
  140. // so decrypt/de-shuffle periodically
  141. j = -1; // 0, adjusted to fit the ++j step
  142. for( i = 20; i < nblocks; ++i )
  143. {
  144. if( i % dcycle == 0 )
  145. {// decrypt block
  146. des_decrypt_block(&p[i]);
  147. continue;
  148. }
  149. ++j;
  150. if( j % scycle == 0 && j != 0 )
  151. {// de-shuffle block
  152. grf_shuffle_dec(&p[i]);
  153. continue;
  154. }
  155. // plaintext, do nothing.
  156. }
  157. }
  158. /// Decodes grf data.
  159. /// @param buf data to decode (in-place)
  160. /// @param len length of the data
  161. /// @param entry_type flags associated with the data
  162. /// @param entry_len true (unaligned) length of the data
  163. static void grf_decode(unsigned char* buf, size_t len, char entry_type, int entry_len)
  164. {
  165. if( entry_type & FILELIST_TYPE_ENCRYPT_MIXED )
  166. {// fully encrypted
  167. int digits;
  168. int cycle;
  169. int i;
  170. // compute number of digits of the entry length
  171. digits = 1;
  172. for( i = 10; i <= entry_len; i *= 10 )
  173. ++digits;
  174. // choose size of gap between two encrypted blocks
  175. // digits: 0 1 2 3 4 5 6 7 8 9 ...
  176. // cycle: 1 1 1 4 5 14 15 22 23 24 ...
  177. cycle = ( digits < 3 ) ? 1
  178. : ( digits < 5 ) ? digits + 1
  179. : ( digits < 7 ) ? digits + 9
  180. : digits + 15;
  181. grf_decode_full(buf, len, cycle);
  182. }
  183. else
  184. if( entry_type & FILELIST_TYPE_ENCRYPT_HEADER )
  185. {// header encrypted
  186. grf_decode_header(buf, len);
  187. }
  188. else
  189. {// plaintext
  190. ;
  191. }
  192. }
  193. /******************************************************
  194. *** Zlib Subroutines ***
  195. ******************************************************/
  196. /// zlib crc32
  197. unsigned long grfio_crc32(const unsigned char* buf, unsigned int len)
  198. {
  199. return crc32(crc32(0L, Z_NULL, 0), buf, len);
  200. }
  201. /// zlib uncompress
  202. int decode_zip(void* dest, unsigned long* destLen, const void* source, unsigned long sourceLen)
  203. {
  204. return uncompress((Bytef*)dest, destLen, (const Bytef*)source, sourceLen);
  205. }
  206. /// zlib compress
  207. int encode_zip(void* dest, unsigned long* destLen, const void* source, unsigned long sourceLen)
  208. {
  209. return compress((Bytef*)dest, destLen, (const Bytef*)source, sourceLen);
  210. }
  211. /***********************************************************
  212. *** File List Subroutines ***
  213. ***********************************************************/
  214. // file list hash table
  215. int filelist_hash[256];
  216. // initializes the table that holds the first elements of all hash chains
  217. static void hashinit(void)
  218. {
  219. int i;
  220. for (i = 0; i < 256; i++)
  221. filelist_hash[i] = -1;
  222. }
  223. // hashes a filename string into a number from {0..255}
  224. static int filehash(const char* fname)
  225. {
  226. unsigned int hash = 0;
  227. while(*fname) {
  228. hash = (hash<<1) + (hash>>7)*9 + TOLOWER(*fname);
  229. fname++;
  230. }
  231. return hash & 255;
  232. }
  233. // finds a FILELIST entry with the specified file name
  234. static FILELIST* filelist_find(const char* fname)
  235. {
  236. int hash, index;
  237. if (!filelist)
  238. return NULL;
  239. hash = filelist_hash[filehash(fname)];
  240. for (index = hash; index != -1; index = filelist[index].next)
  241. if(!strcmpi(filelist[index].fn, fname))
  242. break;
  243. return (index >= 0) ? &filelist[index] : NULL;
  244. }
  245. // returns the original file name
  246. char* grfio_find_file(const char* fname)
  247. {
  248. FILELIST *filelist = filelist_find(fname);
  249. if (!filelist) return NULL;
  250. return (!filelist->fnd ? filelist->fn : filelist->fnd);
  251. }
  252. // adds a FILELIST entry into the list of loaded files
  253. static FILELIST* filelist_add(FILELIST* entry)
  254. {
  255. int hash;
  256. #define FILELIST_ADDS 1024 // number increment of file lists `
  257. if (filelist_entrys >= filelist_maxentry) {
  258. filelist = (FILELIST *)aRealloc(filelist, (filelist_maxentry + FILELIST_ADDS) * sizeof(FILELIST));
  259. memset(filelist + filelist_maxentry, '\0', FILELIST_ADDS * sizeof(FILELIST));
  260. filelist_maxentry += FILELIST_ADDS;
  261. }
  262. memcpy (&filelist[filelist_entrys], entry, sizeof(FILELIST));
  263. hash = filehash(entry->fn);
  264. filelist[filelist_entrys].next = filelist_hash[hash];
  265. filelist_hash[hash] = filelist_entrys;
  266. filelist_entrys++;
  267. return &filelist[filelist_entrys - 1];
  268. }
  269. // adds a new FILELIST entry or overwrites an existing one
  270. static FILELIST* filelist_modify(FILELIST* entry)
  271. {
  272. FILELIST* fentry = filelist_find(entry->fn);
  273. if (fentry != NULL) {
  274. int tmp = fentry->next;
  275. memcpy(fentry, entry, sizeof(FILELIST));
  276. fentry->next = tmp;
  277. } else {
  278. fentry = filelist_add(entry);
  279. }
  280. return fentry;
  281. }
  282. // shrinks the file list array if too long
  283. static void filelist_compact(void)
  284. {
  285. if (filelist == NULL)
  286. return;
  287. if (filelist_entrys < filelist_maxentry) {
  288. filelist = (FILELIST *)aRealloc(filelist, filelist_entrys * sizeof(FILELIST));
  289. filelist_maxentry = filelist_entrys;
  290. }
  291. }
  292. /***********************************************************
  293. *** Grfio Subroutines ***
  294. ***********************************************************/
  295. /// Combines are resource path with the data folder location to create local resource path.
  296. static void grfio_localpath_create(char* buffer, size_t size, const char* filename)
  297. {
  298. unsigned int i;
  299. size_t len;
  300. len = strlen(data_dir);
  301. if( data_dir[0] == '\0' || data_dir[len-1] == '/' || data_dir[len-1] == '\\' )
  302. {
  303. safesnprintf(buffer, size, "%s%s", data_dir, filename);
  304. }
  305. else
  306. {
  307. safesnprintf(buffer, size, "%s/%s", data_dir, filename);
  308. }
  309. // normalize path
  310. for( i = 0; buffer[i] != '\0'; ++i )
  311. if( buffer[i] == '\\' )
  312. buffer[i] = '/';
  313. }
  314. /// Reads a file into a newly allocated buffer (from grf or data directory).
  315. void* grfio_reads(const char* fname, int* size)
  316. {
  317. unsigned char* buf2 = NULL;
  318. FILELIST* entry = filelist_find(fname);
  319. if( entry == NULL || entry->gentry <= 0 ) {// LocalFileCheck
  320. char lfname[256];
  321. int declen;
  322. FILE* in;
  323. grfio_localpath_create(lfname, sizeof(lfname), ( entry && entry->fnd ) ? entry->fnd : fname);
  324. in = fopen(lfname, "rb");
  325. if( in != NULL ) {
  326. fseek(in,0,SEEK_END);
  327. declen = ftell(in);
  328. fseek(in,0,SEEK_SET);
  329. buf2 = (unsigned char *)aMalloc(declen+1); // +1 for resnametable zero-termination
  330. if(fread(buf2, 1, declen, in) != declen) ShowError("An error occured in fread grfio_reads, fname=%s \n",fname);
  331. fclose(in);
  332. if( size )
  333. *size = declen;
  334. } else {
  335. if (entry != NULL && entry->gentry < 0) {
  336. entry->gentry = -entry->gentry; // local file checked
  337. } else {
  338. ShowError("grfio_reads: %s not found (local file: %s)\n", fname, lfname);
  339. return NULL;
  340. }
  341. }
  342. }
  343. if( entry != NULL && entry->gentry > 0 ) {// Archive[GRF] File Read
  344. char* grfname = gentry_table[entry->gentry - 1];
  345. FILE* in = fopen(grfname, "rb");
  346. if( in != NULL ) {
  347. int fsize = entry->srclen_aligned;
  348. unsigned char *buf = (unsigned char *)aMalloc(fsize);
  349. fseek(in, entry->srcpos, 0);
  350. if(fread(buf, 1, fsize, in) != fsize) ShowError("An error occured in fread in grfio_reads, grfname=%s\n",grfname);
  351. fclose(in);
  352. buf2 = (unsigned char *)aMalloc(entry->declen+1); // +1 for resnametable zero-termination
  353. if( entry->type & FILELIST_TYPE_FILE )
  354. {// file
  355. uLongf len;
  356. grf_decode(buf, fsize, entry->type, entry->srclen);
  357. len = entry->declen;
  358. decode_zip(buf2, &len, buf, entry->srclen);
  359. if (len != (uLong)entry->declen) {
  360. ShowError("decode_zip size mismatch err: %d != %d\n", (int)len, entry->declen);
  361. aFree(buf);
  362. aFree(buf2);
  363. return NULL;
  364. }
  365. } else {// directory?
  366. memcpy(buf2, buf, entry->declen);
  367. }
  368. if( size )
  369. *size = entry->declen;
  370. aFree(buf);
  371. } else {
  372. ShowError("grfio_reads: %s not found (GRF file: %s)\n", fname, grfname);
  373. return NULL;
  374. }
  375. }
  376. return buf2;
  377. }
  378. /// Decodes encrypted filename from a version 01xx grf index.
  379. static char* decode_filename(unsigned char* buf, int len)
  380. {
  381. int lop;
  382. for(lop=0;lop<len;lop+=8) {
  383. NibbleSwap(&buf[lop],8);
  384. des_decrypt(&buf[lop],8);
  385. }
  386. return (char*)buf;
  387. }
  388. /// Compares file extension against known large file types.
  389. /// @return true if the file should undergo full mode 0 decryption, and true otherwise.
  390. static bool isFullEncrypt(const char* fname)
  391. {
  392. static const char extensions[4][5] = { ".gnd", ".gat", ".act", ".str" };
  393. size_t i;
  394. const char* ext = strrchr(fname, '.');
  395. if( ext != NULL )
  396. for( i = 0; i < ARRAYLENGTH(extensions); ++i )
  397. if( strcmpi(ext, extensions[i]) == 0 )
  398. return false;
  399. return true;
  400. }
  401. /// Loads all entries in the specified grf file into the filelist.
  402. /// @param gentry index of the grf file name in the gentry_table
  403. static int grfio_entryread(const char* grfname, int gentry)
  404. {
  405. long grf_size,list_size;
  406. unsigned char grf_header[0x2e];
  407. int entry,entrys,ofs,grf_version;
  408. unsigned char *grf_filelist;
  409. FILE* fp = fopen(grfname, "rb");
  410. if( fp == NULL ) {
  411. ShowWarning("GRF data file not found: '%s'\n",grfname);
  412. return 1; // 1:not found error
  413. } else
  414. ShowInfo("GRF data file found: '%s'\n",grfname);
  415. fseek(fp,0,SEEK_END);
  416. grf_size = ftell(fp);
  417. fseek(fp,0,SEEK_SET);
  418. if(fread(grf_header,1,0x2e,fp) != 0x2e) { ShowError("Couldn't read all grf_header element of %s \n", grfname); }
  419. if( strcmp((const char*)grf_header,"Master of Magic") != 0 || fseek(fp,getlong(grf_header+0x1e),SEEK_CUR) != 0 ) {
  420. fclose(fp);
  421. ShowError("GRF %s read error\n", grfname);
  422. return 2; // 2:file format error
  423. }
  424. grf_version = getlong(grf_header+0x2a) >> 8;
  425. if( grf_version == 0x01 ) {// ****** Grf version 01xx ******
  426. list_size = grf_size - ftell(fp);
  427. grf_filelist = (unsigned char *) aMalloc(list_size);
  428. if(fread(grf_filelist,1,list_size,fp) != list_size) { ShowError("Couldn't read all grf_filelist element of %s \n", grfname); }
  429. fclose(fp);
  430. entrys = getlong(grf_header+0x26) - getlong(grf_header+0x22) - 7;
  431. // Get an entry
  432. for( entry = 0, ofs = 0; entry < entrys; ++entry ) {
  433. FILELIST aentry;
  434. int ofs2 = ofs+getlong(grf_filelist+ofs)+4;
  435. unsigned char type = grf_filelist[ofs2+12];
  436. if( type & FILELIST_TYPE_FILE ) {
  437. char* fname = decode_filename(grf_filelist+ofs+6, grf_filelist[ofs]-6);
  438. int srclen = getlong(grf_filelist+ofs2+0) - getlong(grf_filelist+ofs2+8) - 715;
  439. if( strlen(fname) > sizeof(aentry.fn) - 1 ) {
  440. ShowFatalError("GRF file name %s is too long\n", fname);
  441. aFree(grf_filelist);
  442. exit(EXIT_FAILURE);
  443. }
  444. type |= ( isFullEncrypt(fname) ) ? FILELIST_TYPE_ENCRYPT_MIXED : FILELIST_TYPE_ENCRYPT_HEADER;
  445. aentry.srclen = srclen;
  446. aentry.srclen_aligned = getlong(grf_filelist+ofs2+4)-37579;
  447. aentry.declen = getlong(grf_filelist+ofs2+8);
  448. aentry.srcpos = getlong(grf_filelist+ofs2+13)+0x2e;
  449. aentry.type = type;
  450. safestrncpy(aentry.fn, fname, sizeof(aentry.fn));
  451. aentry.fnd = NULL;
  452. #ifdef GRFIO_LOCAL
  453. aentry.gentry = -(gentry+1); // As Flag for making it a negative number carrying out the first time LocalFileCheck
  454. #else
  455. aentry.gentry = gentry+1; // With no first time LocalFileCheck
  456. #endif
  457. filelist_modify(&aentry);
  458. }
  459. ofs = ofs2 + 17;
  460. }
  461. aFree(grf_filelist);
  462. } else if( grf_version == 0x02 ) {// ****** Grf version 02xx ******
  463. unsigned char eheader[8];
  464. unsigned char *rBuf;
  465. uLongf rSize, eSize;
  466. if(fread(eheader,1,8,fp) != 8) ShowError("An error occured in fread while reading eheader buffer\n");
  467. rSize = getlong(eheader); // Read Size
  468. eSize = getlong(eheader+4); // Extend Size
  469. if( (long)rSize > grf_size-ftell(fp) ) {
  470. fclose(fp);
  471. ShowError("Illegal data format: GRF compress entry size\n");
  472. return 4;
  473. }
  474. rBuf = (unsigned char *)aMalloc(rSize); // Get a Read Size
  475. grf_filelist = (unsigned char *)aMalloc(eSize); // Get a Extend Size
  476. if(fread(rBuf,1,rSize,fp) != rSize) ShowError("An error occured in fread \n");
  477. fclose(fp);
  478. decode_zip(grf_filelist, &eSize, rBuf, rSize); // Decode function
  479. aFree(rBuf);
  480. entrys = getlong(grf_header+0x26) - 7;
  481. // Get an entry
  482. for( entry = 0, ofs = 0; entry < entrys; ++entry ) {
  483. FILELIST aentry;
  484. char* fname = (char*)(grf_filelist+ofs);
  485. int ofs2 = ofs + (int)strlen(fname)+1;
  486. int type = grf_filelist[ofs2+12];
  487. if( strlen(fname) > sizeof(aentry.fn)-1 ) {
  488. ShowFatalError("GRF file name %s is too long\n", fname);
  489. aFree(grf_filelist);
  490. exit(EXIT_FAILURE);
  491. }
  492. if( type & FILELIST_TYPE_FILE ) {// file
  493. aentry.srclen = getlong(grf_filelist+ofs2+0);
  494. aentry.srclen_aligned = getlong(grf_filelist+ofs2+4);
  495. aentry.declen = getlong(grf_filelist+ofs2+8);
  496. aentry.srcpos = getlong(grf_filelist+ofs2+13)+0x2e;
  497. aentry.type = type;
  498. safestrncpy(aentry.fn, fname, sizeof(aentry.fn));
  499. aentry.fnd = NULL;
  500. #ifdef GRFIO_LOCAL
  501. aentry.gentry = -(gentry+1); // As Flag for making it a negative number carrying out the first time LocalFileCheck
  502. #else
  503. aentry.gentry = gentry+1; // With no first time LocalFileCheck
  504. #endif
  505. filelist_modify(&aentry);
  506. }
  507. ofs = ofs2 + 17;
  508. }
  509. aFree(grf_filelist);
  510. } else {// ****** Grf Other version ******
  511. fclose(fp);
  512. ShowError("GRF version %04x not supported\n",getlong(grf_header+0x2a));
  513. return 4;
  514. }
  515. filelist_compact(); // Unnecessary area release of filelist
  516. return 0; // 0:no error
  517. }
  518. static bool grfio_parse_restable_row(const char* row)
  519. {
  520. char w1[256], w2[256];
  521. char src[256], dst[256];
  522. char local[256];
  523. FILELIST* entry;
  524. if( sscanf(row, "%[^#\r\n]#%[^#\r\n]#", w1, w2) != 2 )
  525. return false;
  526. if( strstr(w2, ".gat") == NULL && strstr(w2, ".rsw") == NULL )
  527. return false; // we only need the maps' GAT and RSW files
  528. sprintf(src, "data\\%s", w1);
  529. sprintf(dst, "data\\%s", w2);
  530. entry = filelist_find(dst);
  531. if( entry != NULL )
  532. {// alias for GRF resource
  533. FILELIST fentry;
  534. memcpy(&fentry, entry, sizeof(FILELIST));
  535. safestrncpy(fentry.fn, src, sizeof(fentry.fn));
  536. fentry.fnd = aStrdup(dst);
  537. filelist_modify(&fentry);
  538. return true;
  539. }
  540. grfio_localpath_create(local, sizeof(local), dst);
  541. if( exists(local) )
  542. {// alias for local resource
  543. FILELIST fentry;
  544. memset(&fentry, 0, sizeof(fentry));
  545. safestrncpy(fentry.fn, src, sizeof(fentry.fn));
  546. fentry.fnd = aStrdup(dst);
  547. filelist_modify(&fentry);
  548. return true;
  549. }
  550. return false;
  551. }
  552. /// Grfio Resource file check.
  553. static void grfio_resourcecheck(void)
  554. {
  555. char restable[256];
  556. char *ptr, *buf;
  557. int size;
  558. FILE* fp;
  559. int i = 0;
  560. // read resnametable from data directory and return if successful
  561. grfio_localpath_create(restable, sizeof(restable), "data\\resnametable.txt");
  562. fp = fopen(restable, "rb");
  563. if( fp != NULL )
  564. {
  565. char line[256];
  566. while( fgets(line, sizeof(line), fp) )
  567. {
  568. if( grfio_parse_restable_row(line) )
  569. ++i;
  570. }
  571. fclose(fp);
  572. ShowStatus("Done reading '"CL_WHITE"%d"CL_RESET"' entries in '"CL_WHITE"%s"CL_RESET"'.\n", i, "resnametable.txt");
  573. return; // we're done here!
  574. }
  575. // read resnametable from loaded GRF's, only if it cannot be loaded from the data directory
  576. buf = (char *)grfio_reads("data\\resnametable.txt", &size);
  577. if( buf != NULL )
  578. {
  579. buf[size] = '\0';
  580. ptr = buf;
  581. while( ptr - buf < size )
  582. {
  583. if( grfio_parse_restable_row(ptr) )
  584. ++i;
  585. ptr = strchr(ptr, '\n');
  586. if( ptr == NULL ) break;
  587. ptr++;
  588. }
  589. aFree(buf);
  590. ShowStatus("Done reading '"CL_WHITE"%d"CL_RESET"' entries in '"CL_WHITE"%s"CL_RESET"'.\n", i, "data\\resnametable.txt");
  591. return;
  592. }
  593. }
  594. /// Reads a grf file and adds it to the list.
  595. static int grfio_add(const char* fname)
  596. {
  597. if( gentry_entrys >= gentry_maxentry )
  598. {
  599. #define GENTRY_ADDS 4 // The number increment of gentry_table entries
  600. gentry_maxentry += GENTRY_ADDS;
  601. gentry_table = (char**)aRealloc(gentry_table, gentry_maxentry * sizeof(char*));
  602. memset(gentry_table + (gentry_maxentry - GENTRY_ADDS), 0, sizeof(char*) * GENTRY_ADDS);
  603. }
  604. gentry_table[gentry_entrys++] = aStrdup(fname);
  605. return grfio_entryread(fname, gentry_entrys - 1);
  606. }
  607. /// Finalizes grfio.
  608. void grfio_final(void)
  609. {
  610. if (filelist != NULL) {
  611. int i;
  612. for (i = 0; i < filelist_entrys; i++)
  613. if (filelist[i].fnd != NULL)
  614. aFree(filelist[i].fnd);
  615. aFree(filelist);
  616. filelist = NULL;
  617. }
  618. filelist_entrys = filelist_maxentry = 0;
  619. if (gentry_table != NULL) {
  620. int i;
  621. for (i = 0; i < gentry_entrys; i++)
  622. if (gentry_table[i] != NULL)
  623. aFree(gentry_table[i]);
  624. aFree(gentry_table);
  625. gentry_table = NULL;
  626. }
  627. gentry_entrys = gentry_maxentry = 0;
  628. }
  629. /// Initializes grfio.
  630. void grfio_init(const char* fname)
  631. {
  632. FILE* data_conf;
  633. int grf_num = 0;
  634. hashinit(); // hash table initialization
  635. data_conf = fopen(fname, "r");
  636. if( data_conf != NULL )
  637. {
  638. char line[1024];
  639. while( fgets(line, sizeof(line), data_conf) )
  640. {
  641. char w1[1024], w2[1024];
  642. if( line[0] == '/' && line[1] == '/' )
  643. continue; // skip comments
  644. if( sscanf(line, "%[^:]: %[^\r\n]", w1, w2) != 2 )
  645. continue; // skip unrecognized lines
  646. // Entry table reading
  647. if( strcmp(w1, "grf") == 0 ) // GRF file
  648. {
  649. if( grfio_add(w2) == 0 )
  650. ++grf_num;
  651. }
  652. else if( strcmp(w1,"data_dir") == 0 ) // Data directory
  653. {
  654. safestrncpy(data_dir, w2, sizeof(data_dir));
  655. }
  656. }
  657. fclose(data_conf);
  658. ShowStatus("Done reading '"CL_WHITE"%s"CL_RESET"'.\n", fname);
  659. }
  660. if( grf_num == 0 )
  661. ShowInfo("No GRF loaded, using default data directory\n");
  662. // Unneccessary area release of filelist
  663. filelist_compact();
  664. // Resource check
  665. grfio_resourcecheck();
  666. }