2018-04-13 18:23:45 +02:00
|
|
|
/*
|
|
|
|
infostring.c - network info strings
|
|
|
|
Copyright (C) 2008 Uncle Mike
|
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "common.h"
|
|
|
|
|
|
|
|
#define MAX_KV_SIZE 128
|
|
|
|
|
|
|
|
/*
|
|
|
|
=======================================================================
|
|
|
|
|
|
|
|
INFOSTRING STUFF
|
2018-05-28 17:17:25 +02:00
|
|
|
|
2018-04-13 18:23:45 +02:00
|
|
|
=======================================================================
|
|
|
|
*/
|
|
|
|
/*
|
|
|
|
===============
|
|
|
|
Info_Print
|
|
|
|
|
|
|
|
printing current key-value pair
|
|
|
|
===============
|
|
|
|
*/
|
|
|
|
void Info_Print( const char *s )
|
|
|
|
{
|
|
|
|
char key[MAX_KV_SIZE];
|
|
|
|
char value[MAX_KV_SIZE];
|
|
|
|
int l, count;
|
|
|
|
char *o;
|
|
|
|
|
|
|
|
if( *s == '\\' ) s++;
|
|
|
|
|
|
|
|
while( *s )
|
|
|
|
{
|
|
|
|
count = 0;
|
|
|
|
o = key;
|
|
|
|
|
|
|
|
while( count < (MAX_KV_SIZE - 1) && *s && *s != '\\' )
|
|
|
|
{
|
|
|
|
*o++ = *s++;
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
|
|
|
|
l = o - key;
|
|
|
|
if( l < 20 )
|
|
|
|
{
|
|
|
|
memset( o, ' ', 20 - l );
|
|
|
|
key[20] = 0;
|
|
|
|
}
|
|
|
|
else *o = 0;
|
|
|
|
|
|
|
|
Con_Printf( "%s", key );
|
|
|
|
|
|
|
|
if( !*s )
|
|
|
|
{
|
|
|
|
Con_Printf( "(null)\n" );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
count = 0;
|
|
|
|
o = value;
|
|
|
|
s++;
|
|
|
|
while( count < (MAX_KV_SIZE - 1) && *s && *s != '\\' )
|
|
|
|
{
|
|
|
|
*o++ = *s++;
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
*o = 0;
|
|
|
|
|
|
|
|
if( *s ) s++;
|
|
|
|
Con_Printf( "%s\n", value );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
==============
|
|
|
|
Info_IsValid
|
|
|
|
|
|
|
|
check infostring for potential problems
|
|
|
|
==============
|
|
|
|
*/
|
|
|
|
qboolean Info_IsValid( const char *s )
|
|
|
|
{
|
|
|
|
char key[MAX_KV_SIZE];
|
|
|
|
char value[MAX_KV_SIZE];
|
|
|
|
int count;
|
|
|
|
char *o;
|
|
|
|
|
|
|
|
if( *s == '\\' ) s++;
|
|
|
|
|
|
|
|
while( *s )
|
|
|
|
{
|
|
|
|
count = 0;
|
|
|
|
o = key;
|
|
|
|
|
|
|
|
while( count < (MAX_KV_SIZE - 1) && *s && *s != '\\' )
|
|
|
|
{
|
|
|
|
*o++ = *s++;
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
*o = 0;
|
|
|
|
|
|
|
|
if( !*s ) return false;
|
|
|
|
|
|
|
|
count = 0;
|
|
|
|
o = value;
|
|
|
|
s++;
|
|
|
|
while( count < (MAX_KV_SIZE - 1) && *s && *s != '\\' )
|
|
|
|
{
|
|
|
|
*o++ = *s++;
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
*o = 0;
|
|
|
|
|
2020-11-22 07:49:39 +01:00
|
|
|
if( !COM_CheckStringEmpty( value ) )
|
2018-04-13 18:23:45 +02:00
|
|
|
return false;
|
|
|
|
|
|
|
|
if( *s ) s++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-11-24 01:52:08 +01:00
|
|
|
#if !XASH_DEDICATED
|
2018-04-13 18:23:45 +02:00
|
|
|
/*
|
|
|
|
==============
|
|
|
|
Info_WriteVars
|
|
|
|
|
|
|
|
==============
|
|
|
|
*/
|
|
|
|
void Info_WriteVars( file_t *f )
|
|
|
|
{
|
|
|
|
char *s = CL_Userinfo();
|
|
|
|
char pkey[MAX_SERVERINFO_STRING];
|
|
|
|
static char value[4][MAX_SERVERINFO_STRING]; // use two buffers so compares work without stomping on each other
|
|
|
|
static int valueindex;
|
|
|
|
convar_t *pcvar;
|
|
|
|
char *o;
|
|
|
|
|
|
|
|
valueindex = (valueindex + 1) % 4;
|
|
|
|
if( *s == '\\' ) s++;
|
|
|
|
|
|
|
|
while( 1 )
|
|
|
|
{
|
|
|
|
o = pkey;
|
|
|
|
while( *s != '\\' )
|
|
|
|
{
|
|
|
|
if( !*s ) return;
|
|
|
|
*o++ = *s++;
|
|
|
|
}
|
|
|
|
*o = 0;
|
|
|
|
s++;
|
|
|
|
|
|
|
|
o = value[valueindex];
|
|
|
|
|
|
|
|
while( *s != '\\' && *s )
|
|
|
|
{
|
|
|
|
if( !*s ) return;
|
|
|
|
*o++ = *s++;
|
|
|
|
}
|
|
|
|
*o = 0;
|
|
|
|
|
|
|
|
pcvar = Cvar_FindVar( pkey );
|
|
|
|
|
|
|
|
if( !pcvar && pkey[0] != '*' ) // don't store out star keys
|
|
|
|
FS_Printf( f, "setinfo \"%s\" \"%s\"\n", pkey, value[valueindex] );
|
|
|
|
|
|
|
|
if( !*s ) return;
|
|
|
|
s++;
|
|
|
|
}
|
|
|
|
}
|
2018-04-18 17:32:30 +02:00
|
|
|
#endif // XASH_DEDICATED
|
2018-04-13 18:23:45 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
===============
|
|
|
|
Info_ValueForKey
|
|
|
|
|
|
|
|
Searches the string for the given
|
|
|
|
key and returns the associated value, or an empty string.
|
|
|
|
===============
|
|
|
|
*/
|
2023-06-19 05:53:32 +02:00
|
|
|
const char *GAME_EXPORT Info_ValueForKey( const char *s, const char *key )
|
2018-04-13 18:23:45 +02:00
|
|
|
{
|
|
|
|
char pkey[MAX_KV_SIZE];
|
|
|
|
static char value[4][MAX_KV_SIZE]; // use two buffers so compares work without stomping on each other
|
|
|
|
static int valueindex;
|
|
|
|
int count;
|
|
|
|
char *o;
|
2021-01-03 02:28:45 +01:00
|
|
|
|
2018-04-13 18:23:45 +02:00
|
|
|
valueindex = (valueindex + 1) % 4;
|
|
|
|
if( *s == '\\' ) s++;
|
|
|
|
|
|
|
|
while( 1 )
|
|
|
|
{
|
|
|
|
count = 0;
|
|
|
|
o = pkey;
|
|
|
|
|
|
|
|
while( count < (MAX_KV_SIZE - 1) && *s != '\\' )
|
|
|
|
{
|
|
|
|
if( !*s ) return "";
|
|
|
|
*o++ = *s++;
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
|
|
|
|
*o = 0;
|
|
|
|
s++;
|
|
|
|
|
|
|
|
o = value[valueindex];
|
|
|
|
count = 0;
|
|
|
|
|
|
|
|
while( count < (MAX_KV_SIZE - 1) && *s && *s != '\\' )
|
|
|
|
{
|
|
|
|
if( !*s ) return "";
|
|
|
|
*o++ = *s++;
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
*o = 0;
|
|
|
|
|
|
|
|
if( !Q_strcmp( key, pkey ))
|
|
|
|
return value[valueindex];
|
|
|
|
if( !*s ) return "";
|
|
|
|
s++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-19 02:15:54 +01:00
|
|
|
qboolean GAME_EXPORT Info_RemoveKey( char *s, const char *key )
|
2018-04-13 18:23:45 +02:00
|
|
|
{
|
|
|
|
char *start;
|
|
|
|
char pkey[MAX_KV_SIZE];
|
|
|
|
char value[MAX_KV_SIZE];
|
|
|
|
int cmpsize = Q_strlen( key );
|
|
|
|
int count;
|
|
|
|
char *o;
|
|
|
|
|
|
|
|
if( cmpsize > ( MAX_KV_SIZE - 1 ))
|
|
|
|
cmpsize = MAX_KV_SIZE - 1;
|
|
|
|
|
2020-11-22 07:49:39 +01:00
|
|
|
if( Q_strchr( key, '\\' ))
|
2018-04-13 18:23:45 +02:00
|
|
|
return false;
|
|
|
|
|
|
|
|
while( 1 )
|
|
|
|
{
|
|
|
|
start = s;
|
|
|
|
if( *s == '\\' ) s++;
|
|
|
|
count = 0;
|
|
|
|
o = pkey;
|
|
|
|
|
|
|
|
while( count < (MAX_KV_SIZE - 1) && *s != '\\' )
|
|
|
|
{
|
|
|
|
if( !*s ) return false;
|
|
|
|
*o++ = *s++;
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
*o = 0;
|
|
|
|
s++;
|
|
|
|
|
|
|
|
count = 0;
|
|
|
|
o = value;
|
|
|
|
|
|
|
|
while( count < (MAX_KV_SIZE - 1) && *s != '\\' && *s )
|
|
|
|
{
|
|
|
|
if( !*s ) return false;
|
|
|
|
*o++ = *s++;
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
*o = 0;
|
|
|
|
|
|
|
|
if( !Q_strncmp( key, pkey, cmpsize ))
|
|
|
|
{
|
2023-04-24 01:18:40 +02:00
|
|
|
size_t size = Q_strlen( s ) + 1;
|
|
|
|
|
|
|
|
memmove( start, s, size ); // remove this part
|
2018-04-13 18:23:45 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( !*s ) return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Info_RemovePrefixedKeys( char *start, char prefix )
|
|
|
|
{
|
|
|
|
char *s, *o;
|
|
|
|
char pkey[MAX_KV_SIZE];
|
|
|
|
char value[MAX_KV_SIZE];
|
|
|
|
int count;
|
|
|
|
|
|
|
|
s = start;
|
|
|
|
|
|
|
|
while( 1 )
|
|
|
|
{
|
|
|
|
if( *s == '\\' ) s++;
|
|
|
|
|
|
|
|
count = 0;
|
|
|
|
o = pkey;
|
|
|
|
|
|
|
|
while( count < (MAX_KV_SIZE - 1) && *s != '\\' )
|
|
|
|
{
|
|
|
|
if( !*s ) return;
|
|
|
|
*o++ = *s++;
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
*o = 0;
|
|
|
|
s++;
|
|
|
|
|
|
|
|
count = 0;
|
|
|
|
o = value;
|
|
|
|
|
|
|
|
while( count < (MAX_KV_SIZE - 1) && *s && *s != '\\' )
|
|
|
|
{
|
|
|
|
if( !*s ) return;
|
|
|
|
*o++ = *s++;
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
*o = 0;
|
|
|
|
|
|
|
|
if( pkey[0] == prefix )
|
|
|
|
{
|
|
|
|
Info_RemoveKey( start, pkey );
|
|
|
|
s = start;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( !*s ) return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-27 17:48:17 +01:00
|
|
|
static qboolean Info_IsKeyImportant( const char *key )
|
2018-04-13 18:23:45 +02:00
|
|
|
{
|
|
|
|
if( key[0] == '*' )
|
|
|
|
return true;
|
|
|
|
if( !Q_strcmp( key, "name" ))
|
|
|
|
return true;
|
|
|
|
if( !Q_strcmp( key, "model" ))
|
|
|
|
return true;
|
|
|
|
if( !Q_strcmp( key, "rate" ))
|
|
|
|
return true;
|
|
|
|
if( !Q_strcmp( key, "topcolor" ))
|
|
|
|
return true;
|
|
|
|
if( !Q_strcmp( key, "bottomcolor" ))
|
|
|
|
return true;
|
|
|
|
if( !Q_strcmp( key, "cl_updaterate" ))
|
|
|
|
return true;
|
|
|
|
if( !Q_strcmp( key, "cl_lw" ))
|
|
|
|
return true;
|
|
|
|
if( !Q_strcmp( key, "cl_lc" ))
|
|
|
|
return true;
|
|
|
|
if( !Q_strcmp( key, "cl_nopred" ))
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2024-01-27 17:48:17 +01:00
|
|
|
static char *Info_FindLargestKey( char *s )
|
2018-04-13 18:23:45 +02:00
|
|
|
{
|
|
|
|
char key[MAX_KV_SIZE];
|
|
|
|
char value[MAX_KV_SIZE];
|
|
|
|
static char largest_key[128];
|
|
|
|
int largest_size = 0;
|
|
|
|
int l, count;
|
|
|
|
char *o;
|
|
|
|
|
|
|
|
*largest_key = 0;
|
|
|
|
|
|
|
|
if( *s == '\\' ) s++;
|
|
|
|
|
|
|
|
while( *s )
|
|
|
|
{
|
|
|
|
int size = 0;
|
|
|
|
|
|
|
|
count = 0;
|
|
|
|
o = key;
|
|
|
|
|
|
|
|
while( count < (MAX_KV_SIZE - 1) && *s && *s != '\\' )
|
|
|
|
{
|
|
|
|
*o++ = *s++;
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
|
|
|
|
l = o - key;
|
|
|
|
*o = 0;
|
|
|
|
size = Q_strlen( key );
|
|
|
|
|
|
|
|
if( !*s ) return largest_key;
|
|
|
|
|
|
|
|
count = 0;
|
|
|
|
o = value;
|
|
|
|
s++;
|
|
|
|
while( count < (MAX_KV_SIZE - 1) && *s && *s != '\\' )
|
|
|
|
{
|
|
|
|
*o++ = *s++;
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
*o = 0;
|
|
|
|
|
|
|
|
if( *s ) s++;
|
|
|
|
|
|
|
|
size += Q_strlen( value );
|
|
|
|
|
|
|
|
if(( size > largest_size ) && !Info_IsKeyImportant( key ))
|
|
|
|
{
|
|
|
|
Q_strncpy( largest_key, key, sizeof( largest_key ));
|
|
|
|
largest_size = size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return largest_key;
|
|
|
|
}
|
|
|
|
|
|
|
|
qboolean Info_SetValueForStarKey( char *s, const char *key, const char *value, int maxsize )
|
|
|
|
{
|
|
|
|
char new[1024], *v;
|
|
|
|
int c, team;
|
|
|
|
|
2020-11-22 07:49:39 +01:00
|
|
|
if( Q_strchr( key, '\\' ) || Q_strchr( value, '\\' ))
|
2018-04-13 18:23:45 +02:00
|
|
|
{
|
2018-10-04 08:08:48 +02:00
|
|
|
Con_Printf( S_ERROR "SetValueForKey: can't use keys or values with a \\\n" );
|
2018-04-13 18:23:45 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( Q_strstr( key, ".." ) || Q_strstr( value, ".." ))
|
|
|
|
return false;
|
|
|
|
|
2020-11-22 07:49:39 +01:00
|
|
|
if( Q_strchr( key, '\"' ) || Q_strchr( value, '\"' ))
|
2018-04-13 18:23:45 +02:00
|
|
|
{
|
2018-10-04 08:08:48 +02:00
|
|
|
Con_Printf( S_ERROR "SetValueForKey: can't use keys or values with a \"\n" );
|
2018-04-13 18:23:45 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( Q_strlen( key ) > ( MAX_KV_SIZE - 1 ) || Q_strlen( value ) > ( MAX_KV_SIZE - 1 ))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
Info_RemoveKey( s, key );
|
|
|
|
|
2020-11-22 07:49:39 +01:00
|
|
|
if( !COM_CheckString( value ) )
|
2018-04-13 18:23:45 +02:00
|
|
|
return true; // just clear variable
|
|
|
|
|
|
|
|
Q_snprintf( new, sizeof( new ), "\\%s\\%s", key, value );
|
|
|
|
if( Q_strlen( new ) + Q_strlen( s ) > maxsize )
|
|
|
|
{
|
|
|
|
// no more room in buffer to add key/value
|
|
|
|
if( Info_IsKeyImportant( key ))
|
|
|
|
{
|
|
|
|
// keep removing the largest key/values until we have room
|
|
|
|
char *largekey;
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
largekey = Info_FindLargestKey( s );
|
|
|
|
Info_RemoveKey( s, largekey );
|
|
|
|
} while((( Q_strlen( new ) + Q_strlen( s )) >= maxsize ) && *largekey != 0 );
|
|
|
|
|
|
|
|
if( largekey[0] == 0 )
|
|
|
|
{
|
|
|
|
// no room to add setting
|
|
|
|
return true; // info changed, new value can't saved
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// no room to add setting
|
|
|
|
return true; // info changed, new value can't saved
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// only copy ascii values
|
|
|
|
s += Q_strlen( s );
|
|
|
|
v = new;
|
|
|
|
|
|
|
|
team = ( Q_stricmp( key, "team" ) == 0 ) ? true : false;
|
|
|
|
|
|
|
|
while( *v )
|
|
|
|
{
|
|
|
|
c = (byte)*v++;
|
|
|
|
if( team ) c = Q_tolower( c );
|
|
|
|
if( c > 13 ) *s++ = c;
|
|
|
|
}
|
|
|
|
*s = 0;
|
|
|
|
|
|
|
|
// all done
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
qboolean Info_SetValueForKey( char *s, const char *key, const char *value, int maxsize )
|
|
|
|
{
|
|
|
|
if( key[0] == '*' )
|
|
|
|
{
|
2018-10-04 08:08:48 +02:00
|
|
|
Con_Printf( S_ERROR "Can't set *keys\n" );
|
2018-04-13 18:23:45 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Info_SetValueForStarKey( s, key, value, maxsize );
|
2018-04-23 22:07:54 +02:00
|
|
|
}
|
2023-01-27 02:43:28 +01:00
|
|
|
|
|
|
|
qboolean Info_SetValueForKeyf( char *s, const char *key, int maxsize, const char *format, ... )
|
|
|
|
{
|
|
|
|
char value[MAX_VA_STRING];
|
|
|
|
va_list args;
|
|
|
|
|
|
|
|
va_start( args, format );
|
|
|
|
Q_vsnprintf( value, sizeof( value ), format, args );
|
|
|
|
va_end( args );
|
|
|
|
|
|
|
|
return Info_SetValueForKey( s, key, value, maxsize );
|
|
|
|
}
|
|
|
|
|