Skip to content

Commit

Permalink
engine: server: move pfnWriteString character replacement hack to the…
Browse files Browse the repository at this point in the history
… ALLOC_STRING, the same way as GoldSrc does
  • Loading branch information
a1batross committed May 29, 2023
1 parent d994c6d commit f49a2bc
Showing 1 changed file with 69 additions and 7 deletions.
76 changes: 69 additions & 7 deletions engine/server/sv_game.c
Original file line number Diff line number Diff line change
Expand Up @@ -3156,6 +3156,60 @@ void SV_FreeStringPool( void )
#endif
}

/*
============
SV_ProcessString
Process newly allocated string
pass NULL pointer to dst to get required length incl. null terminator
============
*/
static uint SV_ProcessString( char *dst, const char *src )
{
const char *p;
uint i = 0;

p = src;

while( *p )
{
if( *p == '\\' )
{
char replace = 0;

switch( p[1] )
{
case 'n': replace = '\n'; break;
// GoldSrc doesn't replace these symbols
// but old hack in pfnWriteString did
case 'r': replace = '\r'; break;
case 't': replace = '\t'; break;
}

if( replace )
{
if( dst )
dst[i] = replace;
i++;
p += 2;
continue;
}
}

if( dst )
dst[i] = *p;
i++;
p++;
}

// null terminator
if( dst )
dst[i] = '\0';
i++;

return i;
}

/*
=============
SV_AllocString
Expand All @@ -3168,7 +3222,8 @@ use -str64dup to disable deduplication, -str64alloc to set array size
*/
string_t GAME_EXPORT SV_AllocString( const char *szValue )
{
const char *newString = NULL;
char *newString = NULL;
uint len;
int cmp;

if( svgame.physFuncs.pfnAllocString != NULL )
Expand All @@ -3178,38 +3233,45 @@ string_t GAME_EXPORT SV_AllocString( const char *szValue )
cmp = 1;

if( !str64.allowdup )
{
for( newString = str64.poldstringbase + 1;
newString < str64.plast && ( cmp = Q_strcmp( newString, szValue ) );
newString += Q_strlen( newString ) + 1 );
}

if( cmp )
{
uint len = Q_strlen( szValue );
uint len = SV_ProcessString( NULL, szValue );

if( str64.plast - str64.poldstringbase + len + 2 > str64.maxstringarray )
if( str64.plast - str64.poldstringbase + len + 1 > str64.maxstringarray )
{
str64.plast = str64.pstringbase + 1;
str64.poldstringbase = str64.pstringbase;
str64.numoverflows++;
}

//MsgDev( D_NOTE, "SV_AllocString: %ld %s\n", str64.plast - svgame.globals->pStringBase, szValue );
memcpy( str64.plast, szValue, len + 1 );
str64.totalalloc += len + 1;
SV_ProcessString( str64.plast, szValue );
str64.totalalloc += len;

newString = str64.plast;
str64.plast += len + 1;
str64.plast += len;
}
else
{
str64.numdups++;
//MsgDev( D_NOTE, "SV_AllocString: dup %ld %s\n", newString - svgame.globals->pStringBase, szValue );
}

if( newString - str64.pstringarray > str64.maxalloc )
str64.maxalloc = newString - str64.pstringarray;

return newString - svgame.globals->pStringBase;
#else
newString = _copystring( svgame.stringspool, szValue, __FILE__, __LINE__ );
len = SV_ProcessString( NULL, szValue );
newString = Mem_Malloc( svgame.stringspool, len );
SV_ProcessString( newString, szValue );

return newString - svgame.globals->pStringBase;
#endif
}
Expand Down

0 comments on commit f49a2bc

Please sign in to comment.