Factor out eleven (!) instances of identical code into check_file_exist().

git-svn-id: https://svn.eduke32.com/eduke32@2554 1a8010ca-5511-0410-912e-c29ae57300e0
This commit is contained in:
helixhorned 2012-03-28 19:41:39 +00:00
parent d8b9c9b188
commit d77e388522
4 changed files with 78 additions and 207 deletions

View file

@ -41,4 +41,6 @@ void G_AddPath(const char *buffer);
int32_t getatoken(scriptfile *sf, const tokenlist *tl, int32_t ntokens); int32_t getatoken(scriptfile *sf, const tokenlist *tl, int32_t ntokens);
int32_t check_file_exist(const char *fn);
#endif #endif

View file

@ -13,7 +13,7 @@
#include "cache1d.h" #include "cache1d.h"
#include "kplib.h" #include "kplib.h"
#include "quicklz.h" #include "quicklz.h"
#include "common.h" // tokenlist #include "common.h"
enum scripttoken_t enum scripttoken_t
{ {
@ -221,8 +221,8 @@ static int32_t defsparser(scriptfile *script)
// OLD (DEPRECATED) DEFINITION SYNTAX // OLD (DEPRECATED) DEFINITION SYNTAX
case T_DEFINETEXTURE: case T_DEFINETEXTURE:
{ {
int32_t tile,pal,fnoo,i; int32_t tile,pal,fnoo;
char *fn, *tfn = NULL; char *fn;
if (scriptfile_getsymbol(script,&tile)) break; if (scriptfile_getsymbol(script,&tile)) break;
if (scriptfile_getsymbol(script,&pal)) break; if (scriptfile_getsymbol(script,&pal)) break;
@ -232,31 +232,16 @@ static int32_t defsparser(scriptfile *script)
if (scriptfile_getnumber(script,&fnoo)) break; //y-size if (scriptfile_getnumber(script,&fnoo)) break; //y-size
if (scriptfile_getstring(script,&fn)) break; if (scriptfile_getstring(script,&fn)) break;
i = pathsearchmode; if (check_file_exist(fn))
pathsearchmode = 1;
if (findfrompath(fn,&tfn) < 0)
{
char buf[BMAX_PATH];
Bstrcpy(buf,fn);
kzfindfilestart(buf);
if (!kzfindfile(buf))
{
initprintf("Error: file \"%s\" does not exist\n",fn);
pathsearchmode = i;
break; break;
}
}
else Bfree(tfn);
pathsearchmode = i;
hicsetsubsttex(tile,pal,fn,-1.0,1.0,1.0,1.0,1.0,0); hicsetsubsttex(tile,pal,fn,-1.0,1.0,1.0,1.0,1.0,0);
} }
break; break;
case T_DEFINESKYBOX: case T_DEFINESKYBOX:
{ {
int32_t tile,pal,i,ii; int32_t tile,pal,i;
char *fn[6],happy=1,*tfn = NULL; char *fn[6],happy=1;
if (scriptfile_getsymbol(script,&tile)) break; if (scriptfile_getsymbol(script,&tile)) break;
if (scriptfile_getsymbol(script,&pal)) break; if (scriptfile_getsymbol(script,&pal)) break;
@ -264,23 +249,10 @@ static int32_t defsparser(scriptfile *script)
for (i=0; i<6; i++) for (i=0; i<6; i++)
{ {
if (scriptfile_getstring(script,&fn[i])) break; //grab the 6 faces if (scriptfile_getstring(script,&fn[i])) break; //grab the 6 faces
ii = pathsearchmode;
pathsearchmode = 1;
if (findfrompath(fn[i],&tfn) < 0)
{
char buf[BMAX_PATH];
Bstrcpy(buf,fn[i]); if (check_file_exist(fn[i]))
kzfindfilestart(buf);
if (!kzfindfile(buf))
{
initprintf("Error: file \"%s\" does not exist\n",fn[i]);
happy = 0; happy = 0;
} }
}
else Bfree(tfn);
pathsearchmode = ii;
}
if (i < 6 || !happy) break; if (i < 6 || !happy) break;
hicsetskybox(tile,pal,fn); hicsetskybox(tile,pal,fn);
} }
@ -461,8 +433,8 @@ static int32_t defsparser(scriptfile *script)
} }
case T_TILEFROMTEXTURE: case T_TILEFROMTEXTURE:
{ {
char *texturetokptr = script->ltextptr, *textureend, *fn = NULL, *tfn = NULL, *ftd = NULL; char *texturetokptr = script->ltextptr, *textureend, *fn = NULL, *ftd = NULL;
int32_t tile=-1, token, i; int32_t tile=-1, token;
int32_t alphacut = 255; int32_t alphacut = 255;
int32_t xoffset = 0, yoffset = 0, goodtogo=0; int32_t xoffset = 0, yoffset = 0, goodtogo=0;
@ -506,23 +478,9 @@ static int32_t defsparser(scriptfile *script)
alphacut = clamp(alphacut, 0, 255); alphacut = clamp(alphacut, 0, 255);
i = pathsearchmode; if (check_file_exist(fn))
pathsearchmode = 1;
if (findfrompath(fn,&tfn) < 0)
{
char buf[BMAX_PATH];
Bstrcpy(buf,fn);
kzfindfilestart(buf);
if (!kzfindfile(buf))
{
initprintf("Error: file \"%s\" does not exist\n",fn);
pathsearchmode = i;
break; break;
}
}
else Bfree(tfn);
pathsearchmode = i;
goodtogo = 1; goodtogo = 1;
} }
@ -535,7 +493,7 @@ static int32_t defsparser(scriptfile *script)
if (goodtogo) if (goodtogo)
{ {
int32_t xsiz, ysiz, j; int32_t xsiz, ysiz, i, j;
int32_t *picptr = NULL; int32_t *picptr = NULL;
palette_t *col; palette_t *col;
@ -807,8 +765,8 @@ static int32_t defsparser(scriptfile *script)
break; break;
case T_DEFINEMODELSKIN: case T_DEFINEMODELSKIN:
{ {
int32_t palnum, i; int32_t palnum;
char *skinfn, *tfn = NULL; char *skinfn;
if (scriptfile_getsymbol(script,&palnum)) break; if (scriptfile_getsymbol(script,&palnum)) break;
if (scriptfile_getstring(script,&skinfn)) break; //skin filename if (scriptfile_getstring(script,&skinfn)) break; //skin filename
@ -828,23 +786,8 @@ static int32_t defsparser(scriptfile *script)
if (seenframe) { modelskin = ++lastmodelskin; } if (seenframe) { modelskin = ++lastmodelskin; }
seenframe = 0; seenframe = 0;
i = pathsearchmode; if (check_file_exist(skinfn))
pathsearchmode = 1;
if (findfrompath(skinfn,&tfn) < 0)
{
char buf[BMAX_PATH];
Bstrcpy(buf,skinfn);
kzfindfilestart(buf);
if (!kzfindfile(buf))
{
initprintf("Error: file \"%s\" does not exist\n",skinfn);
pathsearchmode = i;
break; break;
}
}
else Bfree(tfn);
pathsearchmode = i;
#ifdef USE_OPENGL #ifdef USE_OPENGL
switch (md_defineskin(lastmodelid, skinfn, palnum, max(0,modelskin), 0, 0.0f, 1.0f, 1.0f)) switch (md_defineskin(lastmodelid, skinfn, palnum, max(0,modelskin), 0, 0.0f, 1.0f, 1.0f))
@ -1146,8 +1089,8 @@ static int32_t defsparser(scriptfile *script)
case T_SKIN: case T_DETAIL: case T_GLOW: case T_SPECULAR: case T_NORMAL: case T_SKIN: case T_DETAIL: case T_GLOW: case T_SPECULAR: case T_NORMAL:
{ {
char *skintokptr = script->ltextptr; char *skintokptr = script->ltextptr;
char *skinend, *skinfn = 0, *tfn = NULL; char *skinend, *skinfn = 0;
int32_t palnum = 0, surfnum = 0, i; int32_t palnum = 0, surfnum = 0;
double param = 1.0, specpower = 1.0, specfactor = 1.0; double param = 1.0, specpower = 1.0, specfactor = 1.0;
static const tokenlist modelskintokens[] = static const tokenlist modelskintokens[] =
@ -1210,23 +1153,8 @@ static int32_t defsparser(scriptfile *script)
break; break;
} }
i = pathsearchmode; if (check_file_exist(skinfn))
pathsearchmode = 1;
if (findfrompath(skinfn,&tfn) < 0)
{
char buf[BMAX_PATH];
Bstrcpy(buf,skinfn);
kzfindfilestart(buf);
if (!kzfindfile(buf))
{
initprintf("Error: file \"%s\" does not exist\n",skinfn);
pathsearchmode = i;
break; break;
}
}
else Bfree(tfn);
pathsearchmode = i;
#ifdef USE_OPENGL #ifdef USE_OPENGL
switch (md_defineskin(lastmodelid, skinfn, palnum, max(0,modelskin), surfnum, param, specpower, specfactor)) switch (md_defineskin(lastmodelid, skinfn, palnum, max(0,modelskin), surfnum, param, specpower, specfactor))
@ -1464,8 +1392,9 @@ static int32_t defsparser(scriptfile *script)
case T_SKYBOX: case T_SKYBOX:
{ {
char *skyboxtokptr = script->ltextptr; char *skyboxtokptr = script->ltextptr;
char *fn[6] = {0,0,0,0,0,0}, *modelend, happy=1, *tfn = NULL; char *fn[6] = {0,0,0,0,0,0};
int32_t i, tile = -1, pal = 0,ii; char *modelend;
int32_t i, tile = -1, pal = 0, happy = 1;
static const tokenlist skyboxtokens[] = static const tokenlist skyboxtokens[] =
{ {
@ -1509,23 +1438,9 @@ static int32_t defsparser(scriptfile *script)
{ {
if (!fn[i]) initprintf("Error: missing '%s filename' for skybox definition near line %s:%d\n", skyfaces[i], script->filename, scriptfile_getlinum(script,skyboxtokptr)), happy = 0; if (!fn[i]) initprintf("Error: missing '%s filename' for skybox definition near line %s:%d\n", skyfaces[i], script->filename, scriptfile_getlinum(script,skyboxtokptr)), happy = 0;
// FIXME? // FIXME?
ii = pathsearchmode; if (check_file_exist(fn[i]))
pathsearchmode = 1;
if (findfrompath(fn[i],&tfn) < 0)
{
char buf[BMAX_PATH];
Bstrcpy(buf,fn[i]);
kzfindfilestart(buf);
if (!kzfindfile(buf))
{
initprintf("Error: file \"%s\" does not exist\n",fn[i]);
happy = 0; happy = 0;
} }
}
else Bfree(tfn);
pathsearchmode = ii;
}
if (!happy) break; if (!happy) break;
hicsetskybox(tile,pal,fn); hicsetskybox(tile,pal,fn);
@ -1534,8 +1449,8 @@ static int32_t defsparser(scriptfile *script)
case T_HIGHPALOOKUP: case T_HIGHPALOOKUP:
{ {
char *highpaltokptr = script->ltextptr; char *highpaltokptr = script->ltextptr;
int32_t basepal=-1, pal=-1, oldpathsearchmode; int32_t basepal=-1, pal=-1;
char *fn = NULL, *tfn = NULL; char *fn = NULL;
char *highpalend; char *highpalend;
#ifdef POLYMER #ifdef POLYMER
int32_t fd; int32_t fd;
@ -1582,23 +1497,8 @@ static int32_t defsparser(scriptfile *script)
break; break;
} }
oldpathsearchmode = pathsearchmode; if (check_file_exist(fn))
pathsearchmode = 1;
if (findfrompath(fn,&tfn) < 0)
{
char buf[BMAX_PATH];
Bstrcpy(buf,fn);
kzfindfilestart(buf);
if (!kzfindfile(buf))
{
initprintf("Error: file \"%s\" does not exist\n",fn);
pathsearchmode = oldpathsearchmode;
break; break;
}
}
else Bfree(tfn);
pathsearchmode = oldpathsearchmode;
#ifdef POLYMER #ifdef POLYMER
fd = kopen4load(fn, 0); fd = kopen4load(fn, 0);
@ -1708,8 +1608,8 @@ static int32_t defsparser(scriptfile *script)
case T_PAL: case T_PAL:
{ {
char *paltokptr = script->ltextptr, *palend; char *paltokptr = script->ltextptr, *palend;
int32_t pal=-1, i; int32_t pal=-1;
char *fn = NULL, *tfn = NULL; char *fn = NULL;
double alphacut = -1.0, xscale = 1.0, yscale = 1.0, specpower = 1.0, specfactor = 1.0; double alphacut = -1.0, xscale = 1.0, yscale = 1.0, specpower = 1.0, specfactor = 1.0;
char flags = 0; char flags = 0;
@ -1766,23 +1666,9 @@ static int32_t defsparser(scriptfile *script)
break; break;
} }
i = pathsearchmode; if (check_file_exist(fn))
pathsearchmode = 1;
if (findfrompath(fn,&tfn) < 0)
{
char buf[BMAX_PATH];
Bstrcpy(buf,fn);
kzfindfilestart(buf);
if (!kzfindfile(buf))
{
initprintf("Error: file \"%s\" does not exist\n",fn);
pathsearchmode = i;
break; break;
}
}
else Bfree(tfn);
pathsearchmode = i;
xscale = 1.0f / xscale; xscale = 1.0f / xscale;
yscale = 1.0f / yscale; yscale = 1.0f / yscale;
@ -1792,8 +1678,8 @@ static int32_t defsparser(scriptfile *script)
case T_DETAIL: case T_GLOW: case T_SPECULAR: case T_NORMAL: case T_DETAIL: case T_GLOW: case T_SPECULAR: case T_NORMAL:
{ {
char *detailtokptr = script->ltextptr, *detailend; char *detailtokptr = script->ltextptr, *detailend;
int32_t pal = 0, i; int32_t pal = 0;
char *fn = NULL, *tfn = NULL; char *fn = NULL;
double xscale = 1.0, yscale = 1.0, specpower = 1.0, specfactor = 1.0; double xscale = 1.0, yscale = 1.0, specpower = 1.0, specfactor = 1.0;
char flags = 0; char flags = 0;
@ -1841,23 +1727,8 @@ static int32_t defsparser(scriptfile *script)
break; break;
} }
i = pathsearchmode; if (check_file_exist(fn))
pathsearchmode = 1;
if (findfrompath(fn,&tfn) < 0)
{
char buf[BMAX_PATH];
Bstrcpy(buf,fn);
kzfindfilestart(buf);
if (!kzfindfile(buf))
{
initprintf("Error: file \"%s\" does not exist\n",fn);
pathsearchmode = i;
break; break;
}
}
else Bfree(tfn);
pathsearchmode = i;
switch (token) switch (token)
{ {

View file

@ -4,6 +4,9 @@
#include "compat.h" #include "compat.h"
#include "scriptfile.h" #include "scriptfile.h"
#include "cache1d.h"
#include "kplib.h"
#include "baselayer.h"
#include "common.h" #include "common.h"
@ -66,3 +69,31 @@ int32_t getatoken(scriptfile *sf, const tokenlist *tl, int32_t ntokens)
} }
return T_ERROR; return T_ERROR;
} }
//////////
// checks from path and in ZIPs, returns 1 if NOT found
int32_t check_file_exist(const char *fn)
{
int32_t opsm = pathsearchmode;
char *tfn;
pathsearchmode = 1;
if (findfrompath(fn,&tfn) < 0)
{
char buf[BMAX_PATH];
Bstrcpy(buf,fn);
kzfindfilestart(buf);
if (!kzfindfile(buf))
{
initprintf("Error: file \"%s\" does not exist\n",fn);
pathsearchmode = opsm;
return 1;
}
}
else Bfree(tfn);
pathsearchmode = opsm;
return 0;
}

View file

@ -8272,7 +8272,7 @@ static int32_t parsedefinitions_game(scriptfile *script, int32_t preload)
case T_MUSIC: case T_MUSIC:
{ {
char *tinttokptr = script->ltextptr; char *tinttokptr = script->ltextptr;
char *ID=NULL,*fn="",*tfn = NULL; char *ID=NULL, *fn="";
char *musicend; char *musicend;
if (scriptfile_getbraces(script,&musicend)) break; if (scriptfile_getbraces(script,&musicend)) break;
@ -8290,30 +8290,14 @@ static int32_t parsedefinitions_game(scriptfile *script, int32_t preload)
} }
if (!preload) if (!preload)
{ {
int32_t i;
if (ID==NULL) if (ID==NULL)
{ {
initprintf("Error: missing ID for music definition near line %s:%d\n", script->filename, scriptfile_getlinum(script,tinttokptr)); initprintf("Error: missing ID for music definition near line %s:%d\n", script->filename, scriptfile_getlinum(script,tinttokptr));
break; break;
} }
i = pathsearchmode; if (check_file_exist(fn))
pathsearchmode = 1;
if (findfrompath(fn,&tfn) < 0)
{
char buf[BMAX_PATH];
Bstrcpy(buf,fn);
kzfindfilestart(buf);
if (!kzfindfile(buf))
{
initprintf("Error: file \"%s\" does not exist\n",fn);
pathsearchmode = i;
break; break;
}
}
else Bfree(tfn);
pathsearchmode = i;
if (S_DefineMusic(ID,fn)) if (S_DefineMusic(ID,fn))
initprintf("Error: invalid music ID on line %s:%d\n", script->filename, scriptfile_getlinum(script,tinttokptr)); initprintf("Error: invalid music ID on line %s:%d\n", script->filename, scriptfile_getlinum(script,tinttokptr));
@ -8468,7 +8452,7 @@ static int32_t parsedefinitions_game(scriptfile *script, int32_t preload)
case T_SOUND: case T_SOUND:
{ {
char *tinttokptr = script->ltextptr; char *tinttokptr = script->ltextptr;
char *fn="", *tfn = NULL; char *fn="";
int32_t num=-1; int32_t num=-1;
char *musicend; char *musicend;
@ -8487,31 +8471,14 @@ static int32_t parsedefinitions_game(scriptfile *script, int32_t preload)
} }
if (!preload) if (!preload)
{ {
int32_t i;
if (num==-1) if (num==-1)
{ {
initprintf("Error: missing ID for sound definition near line %s:%d\n", script->filename, scriptfile_getlinum(script,tinttokptr)); initprintf("Error: missing ID for sound definition near line %s:%d\n", script->filename, scriptfile_getlinum(script,tinttokptr));
break; break;
} }
i = pathsearchmode; if (check_file_exist(fn))
pathsearchmode = 1;
if (findfrompath(fn,&tfn) < 0)
{
char buf[BMAX_PATH];
Bstrcpy(buf,fn);
kzfindfilestart(buf);
if (!kzfindfile(buf))
{
initprintf("Error: file \"%s\" does not exist\n",fn);
pathsearchmode = i;
break; break;
}
}
else Bfree(tfn);
pathsearchmode = i;
if (S_DefineSound(num,fn)) if (S_DefineSound(num,fn))
initprintf("Error: invalid sound ID on line %s:%d\n", script->filename, scriptfile_getlinum(script,tinttokptr)); initprintf("Error: invalid sound ID on line %s:%d\n", script->filename, scriptfile_getlinum(script,tinttokptr));