Dynamically allocate gamevar label space & add null pointer checks for quotes and gamevar labels

git-svn-id: https://svn.eduke32.com/eduke32@102 1a8010ca-5511-0410-912e-c29ae57300e0
This commit is contained in:
terminx 2006-04-24 00:49:44 +00:00
parent 7563c7ec1f
commit ee521c4d58
6 changed files with 154 additions and 87 deletions

View file

@ -713,7 +713,7 @@ enum gamevarflags {
typedef struct {
long lValue;
char szLabel[MAXVARLABEL];
char *szLabel;
unsigned long dwFlags;
long *plValues; // array of values when 'per-player', or 'per-actor'

View file

@ -44,7 +44,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#include "util_lib.h"
#define VERSION ""
#define VERSION " 1.4.0svn"
#define HEAD "EDuke32"VERSION" (shareware mode)"
#define HEAD2 "EDuke32"VERSION
@ -172,6 +172,9 @@ int txgametext_(int small, int starttile, int x,int y,char *t,char s,char p,shor
newx = 0;
oldt = t;
if(t == NULL)
return -1;
if(centre)
{
while(*t)
@ -2052,6 +2055,12 @@ void operatefta(void)
k -= 4;
}
if(fta_quotes[ps[screenpeek].ftq] == NULL)
{
OSD_Printf("%s %d null quote %d\n",__FILE__,__LINE__,ps[screenpeek].ftq);
return;
}
j = ps[screenpeek].fta;
if (j > 4)
gametext(320>>1,k,fta_quotes[ps[screenpeek].ftq],0,2+8+16);
@ -2063,23 +2072,26 @@ void operatefta(void)
void FTA(short q,struct player_struct *p)
{
if( ud.fta_on == 1)
if(fta_quotes[p->ftq] != NULL)
{
if( p->fta > 0 && q != 115 && q != 116 )
if( p->ftq == 115 || p->ftq == 116 ) return;
p->fta = 100;
if( p->ftq != q || q == 26 )
// || q == 26 || q == 115 || q ==116 || q == 117 || q == 122 )
if( ud.fta_on == 1)
{
p->ftq = q;
pub = NUMPAGES;
pus = NUMPAGES;
if (p == &ps[screenpeek])
OSD_Printf("%s\n",fta_quotes[q]);
if( p->fta > 0 && q != 115 && q != 116 )
if( p->ftq == 115 || p->ftq == 116 ) return;
p->fta = 100;
if( p->ftq != q || q == 26 )
// || q == 26 || q == 115 || q ==116 || q == 117 || q == 122 )
{
p->ftq = q;
pub = NUMPAGES;
pus = NUMPAGES;
if (p == &ps[screenpeek])
OSD_Printf("%s\n",fta_quotes[q]);
}
}
}
} else OSD_Printf("%s %d null quote %d\n",__FILE__,__LINE__,p->ftq);
}
void showtwoscreens(void)
@ -7274,7 +7286,6 @@ FAKE_F3:
Bstrcpy(fta_quotes[26],&music_fn[0][music_select][0]);
Bstrcat(fta_quotes[26],". USE SHIFT-F5 TO CHANGE.");
FTA(26,&ps[myconnectindex]);
}
if(KB_KeyPressed( sc_F8 ))

View file

@ -1050,13 +1050,14 @@ void ReadGameVars(long fil)
for(i=0;i<iGameVarCount;i++)
{
kdfread(&(aGameVars[i]),sizeof(MATTGAMEVAR),1,fil);
aGameVars[i].szLabel=Bmalloc(sizeof(char) * MAXVARLABEL);
kdfread(aGameVars[i].szLabel,sizeof(char) * MAXVARLABEL, 1, fil);
}
// Bsprintf(g_szBuf,"CP:%s %d",__FILE__,__LINE__);
// AddLog(g_szBuf);
for(i=0;i<iGameVarCount;i++)
{
if(aGameVars[i].dwFlags & GAMEVAR_FLAG_PERPLAYER)
aGameVars[i].plValues=SafeMalloc(sizeof(long) * MAXPLAYERS);
else if( aGameVars[i].dwFlags & GAMEVAR_FLAG_PERACTOR)
@ -1074,7 +1075,6 @@ void ReadGameVars(long fil)
// AddLog(g_szBuf);
for(i=0;i<iGameVarCount;i++)
{
if(aGameVars[i].dwFlags & GAMEVAR_FLAG_PERPLAYER)
{
//Bsprintf(g_szBuf,"Reading value array for %s (%d)",aGameVars[i].szLabel,sizeof(long) * MAXPLAYERS);
@ -1138,6 +1138,7 @@ void SaveGameVars(FILE *fil)
for(i=0;i<iGameVarCount;i++)
{
dfwrite(&(aGameVars[i]),sizeof(MATTGAMEVAR),1,fil);
dfwrite(aGameVars[i].szLabel,sizeof(char) * MAXVARLABEL, 1, fil);
}
// dfwrite(&aGameVars,sizeof(aGameVars),1,fil);
@ -1253,31 +1254,34 @@ char AddGameVar(char *pszLabel, long lValue, unsigned long dwFlags)
}
for(i=0;i<iGameVarCount;i++)
{
if( Bstrcmp(pszLabel,aGameVars[i].szLabel) == 0 )
if(aGameVars[i].szLabel != NULL)
{
// found it...
if(aGameVars[i].dwFlags & GAMEVAR_FLAG_PLONG)
if( Bstrcmp(pszLabel,aGameVars[i].szLabel) == 0 )
{
// warning++;
// initprintf("%s:%ld: warning: Internal gamevar '%s' cannot be redefined.\n",compilefile,line_number,label+(labelcnt<<6));
ReportError(-1);
initprintf("%s:%ld: warning: cannot redefine internal gamevar `%s'.\n",compilefile,line_number,label+(labelcnt<<6));
return 0;
}
else if((aGameVars[i].dwFlags & GAMEVAR_FLAG_DEFAULT) || (aGameVars[i].dwFlags & GAMEVAR_FLAG_SYSTEM))
{
//Bsprintf(g_szBuf,"Replacing %s at %d",pszLabel,i);
//AddLog(g_szBuf);
//b=1;
// it's OK to replace
break;
}
else
{
// it's a duplicate in error
warning++;
ReportError(WARNING_DUPLICATEDEFINITION);
return 0;
// found it...
if(aGameVars[i].dwFlags & GAMEVAR_FLAG_PLONG)
{
// warning++;
// initprintf("%s:%ld: warning: Internal gamevar '%s' cannot be redefined.\n",compilefile,line_number,label+(labelcnt<<6));
ReportError(-1);
initprintf("%s:%ld: warning: cannot redefine internal gamevar `%s'.\n",compilefile,line_number,label+(labelcnt<<6));
return 0;
}
else if((aGameVars[i].dwFlags & GAMEVAR_FLAG_DEFAULT) || (aGameVars[i].dwFlags & GAMEVAR_FLAG_SYSTEM))
{
//Bsprintf(g_szBuf,"Replacing %s at %d",pszLabel,i);
//AddLog(g_szBuf);
//b=1;
// it's OK to replace
break;
}
else
{
// it's a duplicate in error
warning++;
ReportError(WARNING_DUPLICATEDEFINITION);
return 0;
}
}
}
}
@ -1300,11 +1304,15 @@ char AddGameVar(char *pszLabel, long lValue, unsigned long dwFlags)
}
else
{
if(aGameVars[i].szLabel == NULL)
aGameVars[i].szLabel=Bmalloc(sizeof(char) * MAXVARLABEL);
Bstrcpy(aGameVars[i].szLabel,pszLabel);
aGameVars[i].dwFlags=dwFlags;
aGameVars[i].lValue=lValue;
if(!(dwFlags & GAMEVAR_FLAG_NODEFAULT))
{
if(aDefaultGameVars[i].szLabel == NULL)
aDefaultGameVars[i].szLabel=Bmalloc(sizeof(char) * MAXVARLABEL);
Bstrcpy(aDefaultGameVars[i].szLabel,pszLabel);
aDefaultGameVars[i].dwFlags=dwFlags;
aDefaultGameVars[i].lValue=lValue;
@ -1365,9 +1373,12 @@ int GetGameID(char *szGameLabel)
for(i=0;i<iGameVarCount;i++)
{
if( Bstrcmp(szGameLabel, aGameVars[i].szLabel) == 0 )
if(aGameVars[i].szLabel != NULL)
{
return i;
if( Bstrcmp(szGameLabel, aGameVars[i].szLabel) == 0 )
{
return i;
}
}
}
return -1;
@ -1472,9 +1483,12 @@ long GetGameVar(char *szGameLabel, long lDefault, short sActor, short sPlayer)
int i;
for(i=0;i<iGameVarCount;i++)
{
if( Bstrcmp(szGameLabel, aGameVars[i].szLabel) == 0 )
if(aGameVars[i].szLabel != NULL)
{
return GetGameVarID(i, sActor, sPlayer);
if( Bstrcmp(szGameLabel, aGameVars[i].szLabel) == 0 )
{
return GetGameVarID(i, sActor, sPlayer);
}
}
}
return lDefault;
@ -1485,17 +1499,20 @@ long *GetGameValuePtr(char *szGameLabel)
int i;
for(i=0;i<iGameVarCount;i++)
{
if( Bstrcmp(szGameLabel, aGameVars[i].szLabel) == 0 )
if(aGameVars[i].szLabel != NULL)
{
if(aGameVars[i].dwFlags & (GAMEVAR_FLAG_PERACTOR | GAMEVAR_FLAG_PERPLAYER))
if( Bstrcmp(szGameLabel, aGameVars[i].szLabel) == 0 )
{
if(!aGameVars[i].plValues)
if(aGameVars[i].dwFlags & (GAMEVAR_FLAG_PERACTOR | GAMEVAR_FLAG_PERPLAYER))
{
AddLog("INTERNAL ERROR: NULL array !!!");
if(!aGameVars[i].plValues)
{
AddLog("INTERNAL ERROR: NULL array !!!");
}
return aGameVars[i].plValues;
}
return aGameVars[i].plValues;
return &(aGameVars[i].lValue);
}
return &(aGameVars[i].lValue);
}
}
//Bsprintf(g_szBuf,"Could not find value '%s'\n",szGameLabel);
@ -1508,9 +1525,12 @@ long GetDefID(char *szGameLabel)
int i;
for(i=0;i<iGameVarCount;i++)
{
if( Bstrcmp(szGameLabel, aGameVars[i].szLabel) == 0 )
if(aGameVars[i].szLabel != NULL)
{
return i;
if( Bstrcmp(szGameLabel, aGameVars[i].szLabel) == 0 )
{
return i;
}
}
}
return -1;
@ -2138,11 +2158,14 @@ char parsecommand(void)
#if 0
for(i=0;i<iGameVarCount;i++)
{
if( Bstrcmp(label+(labelcnt<<6),aGameVars[i].szLabel) == 0 )
if(aGameVars[i].szLabel != NULL)
{
warning++;
initprintf(" * WARNING.(L%ld) duplicate Game definition `%s' ignored.\n",line_number,label+(labelcnt<<6));
break;
if( Bstrcmp(label+(labelcnt<<6),aGameVars[i].szLabel) == 0 )
{
warning++;
initprintf(" * WARNING.(L%ld) duplicate Game definition `%s' ignored.\n",line_number,label+(labelcnt<<6));
break;
}
}
}
#endif
@ -4563,9 +4586,7 @@ repeatcase:
}
}
if (tw == CON_DEFINEQUOTE)
{
*(fta_quotes[k]+i) = '\0';
}
else
{
*(fta_quotes[redefined_quote_count]+i) = '\0';
@ -4893,7 +4914,9 @@ void FreeGameVars(void)
for(i=0;i<MAXGAMEVARS;i++)
{
aGameVars[i].lValue=0;
aGameVars[i].szLabel[0]=0;
if(aGameVars[i].szLabel)
Bfree(aGameVars[i].szLabel);
aGameVars[i].szLabel=NULL;
aGameVars[i].dwFlags=0;
if(aGameVars[i].plValues)
@ -4914,7 +4937,12 @@ void ClearGameVars(void)
for(i=0;i<MAXGAMEVARS;i++)
{
aGameVars[i].lValue=0;
aGameVars[i].szLabel[0]=0;
if(aGameVars[i].szLabel)
Bfree(aGameVars[i].szLabel);
if(aDefaultGameVars[i].szLabel)
Bfree(aDefaultGameVars[i].szLabel);
aGameVars[i].szLabel=NULL;
aDefaultGameVars[i].szLabel=NULL;
aGameVars[i].dwFlags=0;
if(aGameVars[i].plValues)
@ -5855,6 +5883,9 @@ void loadefs(char *filenam)
initprintf("\nCompiled code size: %ld/%ld bytes\n",(unsigned)(scriptptr-script),MAXSCRIPTSIZE);
initprintf("%ld/%ld labels, %d/%d variables\n",labelcnt,min((sizeof(sector)/sizeof(long)),(sizeof(sprite)/(1<<6))),iGameVarCount,MAXGAMEVARS);
initprintf("%ld event definitions, %ld defined actors\n\n",j,k);
for(i=0;i<124;i++)
if(fta_quotes[i] == NULL)
fta_quotes[i] = Bmalloc(sizeof(char) * MAXQUOTELEN);
}
}

View file

@ -3408,7 +3408,9 @@ char parse(void)
insptr++;
q = *insptr++;
i = *insptr++;
Bstrcpy(fta_quotes[q],redefined_quotes[i]);
if(fta_quotes[q] != NULL && redefined_quotes[i] != NULL)
Bstrcpy(fta_quotes[q],redefined_quotes[i]);
else OSD_Printf("%s %d null quote %d %d\n",__FILE__,__LINE__,q,i);
break;
}
@ -4107,16 +4109,23 @@ SKIPJIBS:
switch(tw)
{
case CON_GETPNAME:
if (ud.user_name[j][0])
Bsprintf(fta_quotes[i],"%s",ud.user_name[j]);
else
Bsprintf(fta_quotes[i],"%d",j);
if(fta_quotes[i] != NULL)
{
if (ud.user_name[j][0])
Bsprintf(fta_quotes[i],"%s",ud.user_name[j]);
else
Bsprintf(fta_quotes[i],"%d",j);
}
break;
case CON_QSTRCAT:
Bstrncat(fta_quotes[i],fta_quotes[j],(MAXQUOTELEN-1)-Bstrlen(fta_quotes[i]));
if(fta_quotes[i] != NULL && fta_quotes[j] != NULL)
Bstrncat(fta_quotes[i],fta_quotes[j],(MAXQUOTELEN-1)-Bstrlen(fta_quotes[i]));
else OSD_Printf("%s %d null quote %d %d\n",__FILE__,__LINE__,i,j);
break;
case CON_QSTRCPY:
Bstrcpy(fta_quotes[j],fta_quotes[i]);
if(fta_quotes[i] != NULL && fta_quotes[j] != NULL)
Bstrcpy(fta_quotes[j],fta_quotes[i]);
else OSD_Printf("%s %d null quote %d %d\n",__FILE__,__LINE__,i,j);
break;
case CON_CHANGESPRITESTAT:
changespritestat(i,j);
@ -4614,9 +4623,11 @@ SKIPJIBS:
x2=GetGameVarID(*insptr++,g_i,g_p);
y2=GetGameVarID(*insptr++,g_i,g_p);
}
if (tw == CON_MINITEXT) minitextshade(x,y,fta_quotes[q],shade,pal,26);
else if (tw == CON_GAMETEXT) txgametext(tilenum,x>>1,y,fta_quotes[q],shade,pal,orientation,x1,y1,x2,y2);
if (tw == CON_MINITEXT && fta_quotes[q] != NULL) minitextshade(x,y,fta_quotes[q],shade,pal,26);
else if (tw == CON_GAMETEXT && fta_quotes[q] != NULL) txgametext(tilenum,x>>1,y,fta_quotes[q],shade,pal,orientation,x1,y1,x2,y2);
else if (tw == CON_DIGITALNUMBER) txdigitalnumber(tilenum,x,y,q,shade,pal,orientation,x1,y1,x2,y2);
if((tw == CON_MINITEXT || tw == CON_GAMETEXT) && fta_quotes[q] == NULL)
OSD_Printf("%s %d null quote %d\n",__FILE__,__LINE__,q);
break;
}
@ -5302,13 +5313,19 @@ SKIPJIBS:
{
long var1, var2, var3, var4;
insptr++;
Bstrcpy(tempbuf,fta_quotes[*insptr++]);
var1 = GetGameVarID(*insptr++, g_i, g_p);
var2 = GetGameVarID(*insptr++, g_i, g_p);
var3 = GetGameVarID(*insptr++, g_i, g_p);
var4 = GetGameVarID(*insptr++, g_i, g_p);
Bsprintf(fta_quotes[122],tempbuf,var1,var2,var3,var4);
FTA(122,&ps[g_p]);
if(fta_quotes[*insptr] != NULL)
{
Bstrcpy(tempbuf,fta_quotes[*insptr++]);
var1 = GetGameVarID(*insptr++, g_i, g_p);
var2 = GetGameVarID(*insptr++, g_i, g_p);
var3 = GetGameVarID(*insptr++, g_i, g_p);
var4 = GetGameVarID(*insptr++, g_i, g_p);
Bsprintf(fta_quotes[122],tempbuf,var1,var2,var3,var4);
FTA(122,&ps[g_p]);
} else {
OSD_Printf("%s %d null quote %d\n",__FILE__,__LINE__,*insptr);
insptr += 5;
}
break;
}
@ -6523,13 +6540,22 @@ good:
case CON_QUOTE:
insptr++;
FTA(*insptr++,&ps[g_p]);
if(fta_quotes[*insptr] != NULL)
FTA(*insptr++,&ps[g_p]);
else {
OSD_Printf("%s %d null quote %d\n",__FILE__,__LINE__,*insptr);
insptr++;
}
break;
case CON_USERQUOTE:
insptr++;
adduserquote(fta_quotes[*insptr]);
insptr++;
if(fta_quotes[*insptr] != NULL)
adduserquote(fta_quotes[*insptr++]);
else {
OSD_Printf("%s %d null quote %d\n",__FILE__,__LINE__,*insptr);
insptr++;
}
break;
case CON_IFINOUTERSPACE:

View file

@ -2234,8 +2234,8 @@ cheat_for_port_credits:
}
enabled = 1;
switch (io) {
case 0: if (x==io) ud.crosshair = 1-ud.crosshair;
modval(0,1,(int *)&ud.crosshair,1,probey==io);
case 0: if (x==io) ud.crosshair = (ud.crosshair==3)?0:ud.crosshair+1;
modval(0,3,(int *)&ud.crosshair,1,probey==io);
gametextpal(d,yy, ud.crosshair ? "On" : "Off", MENUHIGHLIGHT(io), 0); break;
case 1: if (x==io) ud.levelstats = 1-ud.levelstats;
modval(0,1,(int *)&ud.levelstats,1,probey==io);

View file

@ -359,10 +359,9 @@ int osdcmd_setvar(const osdfuncparm_t *parm)
varval = Batol(parm->parms[1]);
for(i=0;i<iGameVarCount;i++)
if(Bstrcmp(varname, aGameVars[i].szLabel))
{
SetGameVarID(i, varval, ps[myconnectindex].i, myconnectindex);
}
if(aGameVars[i].szLabel != NULL)
if(Bstrcmp(varname, aGameVars[i].szLabel) == 0)
SetGameVarID(i, varval, ps[myconnectindex].i, myconnectindex);
return OSDCMD_OK;
}