2010-05-25 10:56:00 +00:00
|
|
|
//-------------------------------------------------------------------------
|
|
|
|
/*
|
|
|
|
Copyright (C) 2010 EDuke32 developers and contributors
|
|
|
|
|
|
|
|
This file is part of EDuke32.
|
|
|
|
|
|
|
|
EDuke32 is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU General Public License version 2
|
|
|
|
as published by the Free Software Foundation.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
|
|
|
|
See the GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
|
|
*/
|
|
|
|
//-------------------------------------------------------------------------
|
|
|
|
|
2006-07-07 18:41:05 +00:00
|
|
|
#include "compat.h"
|
|
|
|
#include "baselayer.h"
|
|
|
|
|
|
|
|
#include "scriptfile.h"
|
|
|
|
#include "cache1d.h"
|
|
|
|
#include "crc32.h"
|
|
|
|
|
2006-07-22 05:20:25 +00:00
|
|
|
#include "duke3d.h"
|
|
|
|
#include "grpscan.h"
|
2006-07-07 18:41:05 +00:00
|
|
|
|
2006-11-15 01:16:55 +00:00
|
|
|
struct grpfile grpfiles[numgrpfiles] =
|
2007-08-25 01:05:00 +00:00
|
|
|
{
|
2011-03-04 08:50:58 +00:00
|
|
|
{ "Duke Nukem 3D", 0xBBC9CE44, 26524524, GAME_DUKE, NULL },
|
|
|
|
{ "Duke Nukem 3D: Atomic Edition", 0xF514A6AC, 44348015, GAME_DUKE, NULL },
|
|
|
|
{ "Duke Nukem 3D: Atomic Edition", 0xFD3DCFF1, 44356548, GAME_DUKE, NULL },
|
|
|
|
{ "Duke Nukem 3D Shareware", 0x983AD923, 11035779, GAME_DUKE, NULL },
|
|
|
|
{ "Duke Nukem 3D Mac Shareware", 0xC5F71561, 10444391, GAME_DUKE, NULL },
|
|
|
|
{ "NAM", 0x75C1F07B, 43448927, GAME_NAM, NULL },
|
|
|
|
{ "Napalm", 0x3DE1589A, 44365728, GAME_NAM, NULL },
|
|
|
|
{ "WW2GI", 0x907B82BF, 77939508, GAME_WW2, NULL },
|
2007-08-25 01:05:00 +00:00
|
|
|
};
|
2006-07-07 18:41:05 +00:00
|
|
|
struct grpfile *foundgrps = NULL;
|
|
|
|
|
|
|
|
#define GRPCACHEFILE "grpfiles.cache"
|
2006-11-15 01:16:55 +00:00
|
|
|
static struct grpcache
|
|
|
|
{
|
2006-07-07 18:41:05 +00:00
|
|
|
struct grpcache *next;
|
2009-01-09 09:29:17 +00:00
|
|
|
int32_t size;
|
|
|
|
int32_t mtime;
|
|
|
|
int32_t crcval;
|
2009-07-12 01:55:34 +00:00
|
|
|
char name[BMAX_PATH];
|
2006-11-15 01:16:55 +00:00
|
|
|
}
|
|
|
|
*grpcache = NULL, *usedgrpcache = NULL;
|
2006-07-07 18:41:05 +00:00
|
|
|
|
2009-01-09 09:29:17 +00:00
|
|
|
static int32_t LoadGroupsCache(void)
|
2006-07-07 18:41:05 +00:00
|
|
|
{
|
|
|
|
struct grpcache *fg;
|
|
|
|
|
2009-01-09 09:29:17 +00:00
|
|
|
int32_t fsize, fmtime, fcrcval;
|
2006-07-07 18:41:05 +00:00
|
|
|
char *fname;
|
|
|
|
|
|
|
|
scriptfile *script;
|
|
|
|
|
|
|
|
script = scriptfile_fromfile(GRPCACHEFILE);
|
|
|
|
if (!script) return -1;
|
|
|
|
|
2006-11-15 01:16:55 +00:00
|
|
|
while (!scriptfile_eof(script))
|
|
|
|
{
|
2006-07-07 18:41:05 +00:00
|
|
|
if (scriptfile_getstring(script, &fname)) break; // filename
|
|
|
|
if (scriptfile_getnumber(script, &fsize)) break; // filesize
|
|
|
|
if (scriptfile_getnumber(script, &fmtime)) break; // modification time
|
|
|
|
if (scriptfile_getnumber(script, &fcrcval)) break; // crc checksum
|
|
|
|
|
2008-06-29 10:40:37 +00:00
|
|
|
fg = Bcalloc(1, sizeof(struct grpcache));
|
2006-07-07 18:41:05 +00:00
|
|
|
fg->next = grpcache;
|
|
|
|
grpcache = fg;
|
|
|
|
|
2009-02-02 01:49:14 +00:00
|
|
|
Bstrncpy(fg->name, fname, BMAX_PATH);
|
2006-07-07 18:41:05 +00:00
|
|
|
fg->size = fsize;
|
|
|
|
fg->mtime = fmtime;
|
|
|
|
fg->crcval = fcrcval;
|
|
|
|
}
|
|
|
|
|
|
|
|
scriptfile_close(script);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void FreeGroupsCache(void)
|
|
|
|
{
|
|
|
|
struct grpcache *fg;
|
|
|
|
|
2006-11-15 01:16:55 +00:00
|
|
|
while (grpcache)
|
|
|
|
{
|
2006-07-07 18:41:05 +00:00
|
|
|
fg = grpcache->next;
|
2009-10-07 06:47:35 +00:00
|
|
|
Bfree(grpcache);
|
2006-07-07 18:41:05 +00:00
|
|
|
grpcache = fg;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-09 09:29:17 +00:00
|
|
|
int32_t ScanGroups(void)
|
2006-07-07 18:41:05 +00:00
|
|
|
{
|
|
|
|
CACHE1D_FIND_REC *srch, *sidx;
|
|
|
|
struct grpcache *fg, *fgg;
|
|
|
|
struct grpfile *grp;
|
|
|
|
char *fn;
|
|
|
|
struct Bstat st;
|
2009-06-13 21:06:45 +00:00
|
|
|
#define BUFFER_SIZE (1024 * 1024 * 8)
|
|
|
|
uint8_t *buf = Bmalloc(BUFFER_SIZE);
|
|
|
|
|
2009-06-13 21:09:09 +00:00
|
|
|
if (!buf)
|
2009-06-13 21:06:45 +00:00
|
|
|
{
|
|
|
|
initprintf("Error allocating %d byte buffer to scan GRPs!\n", BUFFER_SIZE);
|
|
|
|
return 0;
|
|
|
|
}
|
2006-07-07 18:41:05 +00:00
|
|
|
|
2010-05-18 00:30:30 +00:00
|
|
|
initprintf("Searching for game data...\n");
|
2006-07-07 18:41:05 +00:00
|
|
|
|
|
|
|
LoadGroupsCache();
|
|
|
|
|
|
|
|
srch = klistpath("/", "*.grp", CACHE1D_FIND_FILE);
|
|
|
|
|
2006-11-15 01:16:55 +00:00
|
|
|
for (sidx = srch; sidx; sidx = sidx->next)
|
|
|
|
{
|
|
|
|
for (fg = grpcache; fg; fg = fg->next)
|
|
|
|
{
|
2006-07-07 18:41:05 +00:00
|
|
|
if (!Bstrcmp(fg->name, sidx->name)) break;
|
|
|
|
}
|
|
|
|
|
2006-11-15 01:16:55 +00:00
|
|
|
if (fg)
|
|
|
|
{
|
2006-07-07 18:41:05 +00:00
|
|
|
if (findfrompath(sidx->name, &fn)) continue; // failed to resolve the filename
|
2006-11-15 01:16:55 +00:00
|
|
|
if (Bstat(fn, &st))
|
|
|
|
{
|
2009-10-07 06:47:35 +00:00
|
|
|
Bfree(fn);
|
2006-11-15 01:16:55 +00:00
|
|
|
continue;
|
|
|
|
} // failed to stat the file
|
2009-10-07 06:47:35 +00:00
|
|
|
Bfree(fn);
|
2006-11-15 01:16:55 +00:00
|
|
|
if (fg->size == st.st_size && fg->mtime == st.st_mtime)
|
|
|
|
{
|
2008-06-29 10:40:37 +00:00
|
|
|
grp = (struct grpfile *)Bcalloc(1, sizeof(struct grpfile));
|
2009-10-07 06:47:35 +00:00
|
|
|
grp->name = Bstrdup(sidx->name);
|
2006-07-07 18:41:05 +00:00
|
|
|
grp->crcval = fg->crcval;
|
|
|
|
grp->size = fg->size;
|
|
|
|
grp->next = foundgrps;
|
|
|
|
foundgrps = grp;
|
|
|
|
|
2008-06-29 10:40:37 +00:00
|
|
|
fgg = (struct grpcache *)Bcalloc(1, sizeof(struct grpcache));
|
2006-07-07 18:41:05 +00:00
|
|
|
strcpy(fgg->name, fg->name);
|
|
|
|
fgg->size = fg->size;
|
|
|
|
fgg->mtime = fg->mtime;
|
|
|
|
fgg->crcval = fg->crcval;
|
|
|
|
fgg->next = usedgrpcache;
|
|
|
|
usedgrpcache = fgg;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2009-01-09 09:29:17 +00:00
|
|
|
int32_t b, fh;
|
|
|
|
int32_t crcval;
|
2006-07-07 18:41:05 +00:00
|
|
|
|
|
|
|
fh = openfrompath(sidx->name, BO_RDONLY|BO_BINARY, BS_IREAD);
|
|
|
|
if (fh < 0) continue;
|
|
|
|
if (fstat(fh, &st)) continue;
|
|
|
|
|
|
|
|
initprintf(" Checksumming %s...", sidx->name);
|
2009-01-09 09:29:17 +00:00
|
|
|
crc32init((uint32_t *)&crcval);
|
2006-11-15 01:16:55 +00:00
|
|
|
do
|
|
|
|
{
|
2009-06-13 21:06:45 +00:00
|
|
|
b = read(fh, buf, BUFFER_SIZE);
|
2009-01-09 09:29:17 +00:00
|
|
|
if (b > 0) crc32block((uint32_t *)&crcval, (uint8_t *)buf, b);
|
2006-11-15 01:16:55 +00:00
|
|
|
}
|
2009-06-13 21:06:45 +00:00
|
|
|
while (b == BUFFER_SIZE);
|
2009-01-09 09:29:17 +00:00
|
|
|
crc32finish((uint32_t *)&crcval);
|
2006-07-07 18:41:05 +00:00
|
|
|
close(fh);
|
|
|
|
initprintf(" Done\n");
|
|
|
|
|
2008-06-29 10:40:37 +00:00
|
|
|
grp = (struct grpfile *)Bcalloc(1, sizeof(struct grpfile));
|
2009-10-07 06:47:35 +00:00
|
|
|
grp->name = Bstrdup(sidx->name);
|
2006-07-07 18:41:05 +00:00
|
|
|
grp->crcval = crcval;
|
|
|
|
grp->size = st.st_size;
|
|
|
|
grp->next = foundgrps;
|
|
|
|
foundgrps = grp;
|
|
|
|
|
2008-06-29 10:40:37 +00:00
|
|
|
fgg = (struct grpcache *)Bcalloc(1, sizeof(struct grpcache));
|
2009-02-02 01:49:14 +00:00
|
|
|
Bstrncpy(fgg->name, sidx->name, BMAX_PATH);
|
2006-07-07 18:41:05 +00:00
|
|
|
fgg->size = st.st_size;
|
|
|
|
fgg->mtime = st.st_mtime;
|
|
|
|
fgg->crcval = crcval;
|
|
|
|
fgg->next = usedgrpcache;
|
|
|
|
usedgrpcache = fgg;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
klistfree(srch);
|
|
|
|
FreeGroupsCache();
|
|
|
|
|
2006-11-15 01:16:55 +00:00
|
|
|
if (usedgrpcache)
|
|
|
|
{
|
2009-01-09 09:29:17 +00:00
|
|
|
int32_t i = 0;
|
2006-07-07 18:41:05 +00:00
|
|
|
FILE *fp;
|
|
|
|
fp = fopen(GRPCACHEFILE, "wt");
|
2006-11-15 01:16:55 +00:00
|
|
|
if (fp)
|
|
|
|
{
|
|
|
|
for (fg = usedgrpcache; fg; fg=fgg)
|
|
|
|
{
|
2006-07-07 18:41:05 +00:00
|
|
|
fgg = fg->next;
|
|
|
|
fprintf(fp, "\"%s\" %d %d %d\n", fg->name, fg->size, fg->mtime, fg->crcval);
|
2009-10-07 06:47:35 +00:00
|
|
|
Bfree(fg);
|
2008-08-22 23:10:54 +00:00
|
|
|
i++;
|
2006-07-07 18:41:05 +00:00
|
|
|
}
|
|
|
|
fclose(fp);
|
|
|
|
}
|
2008-12-10 11:36:53 +00:00
|
|
|
// initprintf("Found %d recognized GRP %s.\n",i,i>1?"files":"file");
|
2009-06-13 21:06:45 +00:00
|
|
|
if (buf)
|
|
|
|
Bfree(buf);
|
2008-08-22 23:10:54 +00:00
|
|
|
return 0;
|
2006-07-07 18:41:05 +00:00
|
|
|
}
|
2010-05-16 22:53:08 +00:00
|
|
|
|
|
|
|
initprintf("Found no recognized game data!\n");
|
|
|
|
|
2009-06-13 21:06:45 +00:00
|
|
|
if (buf)
|
|
|
|
Bfree(buf);
|
2010-05-16 22:53:08 +00:00
|
|
|
|
2006-07-07 18:41:05 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void FreeGroups(void)
|
|
|
|
{
|
|
|
|
struct grpfile *fg;
|
|
|
|
|
2006-11-15 01:16:55 +00:00
|
|
|
while (foundgrps)
|
|
|
|
{
|
2006-07-07 18:41:05 +00:00
|
|
|
fg = foundgrps->next;
|
2010-08-02 08:13:51 +00:00
|
|
|
Bfree((char *)foundgrps->name);
|
2009-10-07 06:47:35 +00:00
|
|
|
Bfree(foundgrps);
|
2006-07-07 18:41:05 +00:00
|
|
|
foundgrps = fg;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|