Skip to content

Commit

Permalink
engine: server: query: use common SV_GetPlayerCount, fix random styli…
Browse files Browse the repository at this point in the history
…ng issues
  • Loading branch information
a1batross committed Jun 9, 2023
1 parent d9ef1d4 commit 278ff22
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 50 deletions.
2 changes: 1 addition & 1 deletion engine/server/sv_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -3102,7 +3102,7 @@ void SV_ConnectionlessPacket( netadr_t from, sizebuf_t *msg )
else if( !Q_strcmp( pcmd, "netinfo" )) SV_BuildNetAnswer( from );
else if( !Q_strcmp( pcmd, "s" )) SV_AddToMaster( from, msg );
else if( !Q_strcmp( pcmd, "i" )) NET_SendPacket( NS_SERVER, 5, "\xFF\xFF\xFF\xFFj", from ); // A2A_PING
else if( SV_SourceQuery_HandleConnnectionlessPacket( pcmd, from ) ) return;
else if( SV_SourceQuery_HandleConnnectionlessPacket( pcmd, from )) { } // function handles replies
else if( !Q_strcmp( pcmd, "c" ) && sv_nat.value && NET_IsMasterAdr( from ))
{
netadr_t to;
Expand Down
80 changes: 31 additions & 49 deletions engine/server/sv_query.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,28 +35,16 @@ SV_SourceQuery_Details
void SV_SourceQuery_Details( netadr_t from )
{
sizebuf_t buf;
char answer[2048] = "";
char version[128] = "";

int i, bot_count = 0, client_count = 0;
char answer[2048];
int i, bot_count, client_count;
int is_private = 0;

if ( svs.clients )
{
for ( i = 0; i < svs.maxclients; i++ )
{
if ( svs.clients[i].state >= cs_connected )
{
if ( svs.clients[i].edict->v.flags & FL_FAKECLIENT )
bot_count++;

client_count++;
}
}
}
is_private = ( sv_password.string[0] && ( Q_stricmp( sv_password.string, "none" ) || !Q_strlen( sv_password.string ) ) ? 1 : 0 );
SV_GetPlayerCount( &client_count, &bot_count );
client_count += bot_count; // bots are counted as players in this reply
if( COM_CheckStringEmpty( sv_password.string ) && Q_stricmp( sv_password.string, "none" ))
is_private = 1;

MSG_Init( &buf, "TSourceEngineQuery", answer, sizeof( answer ));
MSG_Init( &buf, "TSourceEngineQuery", answer, sizeof( answer ));

MSG_WriteLong( &buf, SOURCE_QUERY_CONNECTIONLESS );
MSG_WriteByte( &buf, SOURCE_QUERY_DETAILS );
Expand All @@ -65,17 +53,17 @@ void SV_SourceQuery_Details( netadr_t from )
MSG_WriteString( &buf, hostname.string );
MSG_WriteString( &buf, sv.name );
MSG_WriteString( &buf, GI->gamefolder );
MSG_WriteString( &buf, svgame.dllFuncs.pfnGetGameDescription( ) );
MSG_WriteString( &buf, svgame.dllFuncs.pfnGetGameDescription( ));

MSG_WriteShort( &buf, 0 );
MSG_WriteByte( &buf, client_count );
MSG_WriteByte( &buf, svs.maxclients );
MSG_WriteByte( &buf, bot_count );

MSG_WriteByte( &buf, Host_IsDedicated( ) ? 'd' : 'l' );
#if defined( _WIN32 )
#if XASH_WIN32
MSG_WriteByte( &buf, 'w' );
#elif defined( __APPLE__ )
#elif XASH_APPLE
MSG_WriteByte( &buf, 'm' );
#else
MSG_WriteByte( &buf, 'l' );
Expand All @@ -95,34 +83,33 @@ SV_SourceQuery_Rules
void SV_SourceQuery_Rules( netadr_t from )
{
sizebuf_t buf;
char answer[1024 * 8] = "";

char answer[1024 * 8];
cvar_t *cvar;
int cvar_count = 0;

for ( cvar = Cvar_GetList( ); cvar; cvar = cvar->next )
for( cvar = Cvar_GetList( ); cvar; cvar = cvar->next )
{
if ( cvar->flags & FCVAR_SERVER )
if( FBitSet( cvar->flags, FCVAR_SERVER ))
cvar_count++;
}
if ( cvar_count <= 0 )
if( cvar_count <= 0 )
return;

MSG_Init( &buf, "TSourceEngineQueryRules", answer, sizeof( answer ) );
MSG_Init( &buf, "TSourceEngineQueryRules", answer, sizeof( answer ));

MSG_WriteLong( &buf, SOURCE_QUERY_CONNECTIONLESS );
MSG_WriteByte( &buf, SOURCE_QUERY_RULES_RESPONSE );
MSG_WriteShort( &buf, cvar_count );

for ( cvar = Cvar_GetList( ); cvar; cvar = cvar->next )
for( cvar = Cvar_GetList( ); cvar; cvar = cvar->next )
{
if ( !( cvar->flags & FCVAR_SERVER ) )
if( !FBitSet( cvar->flags, FCVAR_SERVER ))
continue;

MSG_WriteString( &buf, cvar->name );

if ( cvar->flags & FCVAR_PROTECTED )
MSG_WriteString( &buf, ( Q_strlen( cvar->string ) > 0 && Q_stricmp( cvar->string, "none" ) ) ? "1" : "0" );
if( FBitSet( cvar->flags, FCVAR_PROTECTED ))
MSG_WriteString( &buf, ( COM_CheckStringEmpty( cvar->string ) && Q_stricmp( cvar->string, "none" )) ? "1" : "0" );
else
MSG_WriteString( &buf, cvar->string );
}
Expand All @@ -137,38 +124,33 @@ SV_SourceQuery_Players
void SV_SourceQuery_Players( netadr_t from )
{
sizebuf_t buf;
char answer[1024 * 8] = "";
char answer[1024 * 8];
int i, client_count, bot_count_unused;

int i, client_count = 0;
SV_GetPlayerCount( &client_count, &bot_count_unused );

if ( svs.clients )
{
for ( i = 0; i < svs.maxclients; i++ )
{
if ( svs.clients[i].state >= cs_connected )
client_count++;
}
}
if ( client_count <= 0 )
if( client_count <= 0 )
return;

MSG_Init( &buf, "TSourceEngineQueryPlayers", answer, sizeof( answer ) );
MSG_Init( &buf, "TSourceEngineQueryPlayers", answer, sizeof( answer ));

MSG_WriteLong( &buf, SOURCE_QUERY_CONNECTIONLESS );
MSG_WriteByte( &buf, SOURCE_QUERY_PLAYERS_RESPONSE );
MSG_WriteByte( &buf, client_count );

for ( i = 0; i < svs.maxclients; i++ )
for( i = 0; i < svs.maxclients; i++ )
{
sv_client_t *cl = &svs.clients[i];

if ( cl->state < cs_connected )
if( cl->state < cs_connected )
continue;

MSG_WriteByte( &buf, i );
MSG_WriteString( &buf, cl->name );
MSG_WriteLong( &buf, cl->edict->v.frags );
MSG_WriteFloat( &buf, ( cl->edict->v.flags & FL_FAKECLIENT ) ? -1.0 : ( host.realtime - cl->connecttime ) );
if( FBitSet( cl->flags, FCL_FAKECLIENT ))
MSG_WriteFloat( &buf, -1.0f );
else MSG_WriteFloat( &buf, host.realtime - cl->connecttime );
}
NET_SendPacket( NS_SERVER, MSG_GetNumBytesWritten( &buf ), MSG_GetData( &buf ), from );
}
Expand All @@ -182,7 +164,7 @@ qboolean SV_SourceQuery_HandleConnnectionlessPacket( const char *c, netadr_t fro
{
int request = c[0];

switch ( request )
switch( request )
{
case SOURCE_QUERY_INFO:
SV_SourceQuery_Details( from );
Expand All @@ -200,4 +182,4 @@ qboolean SV_SourceQuery_HandleConnnectionlessPacket( const char *c, netadr_t fro
return false;
}
return false;
}
}

0 comments on commit 278ff22

Please sign in to comment.