From 22815d278461b0afe321c91c5bc87c74978b9c26 Mon Sep 17 00:00:00 2001 From: SNMetamorph <25657591+SNMetamorph@users.noreply.github.com> Date: Wed, 6 Apr 2022 16:06:16 +0400 Subject: [PATCH] engine: server: sv_main: added status line for dedicated server console --- engine/platform/platform.h | 6 ++++ engine/platform/win32/sys_win.c | 24 +++++++++++++- engine/server/server.h | 1 + engine/server/sv_main.c | 55 ++++++++++++++++++++++++--------- 4 files changed, 71 insertions(+), 15 deletions(-) diff --git a/engine/platform/platform.h b/engine/platform/platform.h index c25ffe29..c66a8a49 100644 --- a/engine/platform/platform.h +++ b/engine/platform/platform.h @@ -45,6 +45,12 @@ const char *Android_LoadID( void ); void Android_SaveID( const char *id ); #endif +#if XASH_WIN32 +void Platform_UpdateStatusLine( void ); +#else +static inline void Platform_UpdateStatusLine( void ) { } +#endif + /* ============================================================================== diff --git a/engine/platform/win32/sys_win.c b/engine/platform/win32/sys_win.c index 78132f0a..8af51fd2 100644 --- a/engine/platform/win32/sys_win.c +++ b/engine/platform/win32/sys_win.c @@ -15,6 +15,7 @@ GNU General Public License for more details. #include "platform/platform.h" #include "menu_int.h" +#include "server.h" #include #if XASH_TIMER == TIMER_WIN32 @@ -53,6 +54,27 @@ void Platform_ShellExecute( const char *path, const char *parms ) ShellExecute( NULL, "open", path, parms, NULL, SW_SHOW ); } +void Platform_UpdateStatusLine( void ) +{ + int clientsCount; + char szStatus[128]; + static double lastTime; + + if( host.type != HOST_DEDICATED ) + return; + + // update only every 1/2 seconds + if(( sv.time - lastTime ) < 0.5f ) + return; + + clientsCount = SV_GetConnectedClientsCount( NULL ); + Q_snprintf( szStatus, sizeof( szStatus ) - 1, "%.1f fps %2i/%2i on %16s", 1.f / sv.frametime, clientsCount, svs.maxclients, host.game.levelName ); +#ifdef XASH_WIN32 + Wcon_SetStatus( szStatus ); +#endif + lastTime = sv.time; +} + #if XASH_MESSAGEBOX == MSGBOX_WIN32 void Platform_MessageBox( const char *title, const char *message, qboolean parentMainWindow ) { @@ -71,4 +93,4 @@ void Platform_Shutdown( void ) { Wcon_DestroyConsole(); } -#endif \ No newline at end of file +#endif diff --git a/engine/server/server.h b/engine/server/server.h index 3c516372..2bfd8b18 100644 --- a/engine/server/server.h +++ b/engine/server/server.h @@ -470,6 +470,7 @@ void SV_SendResource( resource_t *pResource, sizebuf_t *msg ); void SV_SendResourceList( sv_client_t *cl ); void SV_AddToMaster( netadr_t from, sizebuf_t *msg ); qboolean SV_ProcessUserAgent( netadr_t from, const char *useragent ); +int SV_GetConnectedClientsCount( int *bots ); void Host_SetServerState( int state ); qboolean SV_IsSimulating( void ); void SV_FreeClients( void ); diff --git a/engine/server/sv_main.c b/engine/server/sv_main.c index 071164a3..b6756d47 100644 --- a/engine/server/sv_main.c +++ b/engine/server/sv_main.c @@ -16,6 +16,7 @@ GNU General Public License for more details. #include "common.h" #include "server.h" #include "net_encode.h" +#include "platform/platform.h" #define HEARTBEAT_SECONDS 300.0f // 300 seconds @@ -153,6 +154,41 @@ qboolean SV_HasActivePlayers( void ) return false; } +/* +================ +SV_GetConnectedClientsCount + +returns connected clients count (and optionally bots count) +================ +*/ +int SV_GetConnectedClientsCount(int *bots) +{ + int index; + int clients; + + clients = 0; + if( svs.clients ) + { + if( bots ) + *bots = 0; + + for( index = 0; index < svs.maxclients; index++ ) + { + if( svs.clients[index].state >= cs_connected ) + { + if( FBitSet( svs.clients[index].flags, FCL_FAKECLIENT )) + { + if( bots ) + *bots++; + } + else + clients++; + } + } + } + return clients; +} + /* =================== SV_UpdateMovevars @@ -650,6 +686,9 @@ void Host_ServerFrame( void ) // clear edict flags for next frame SV_PrepWorldFrame (); + // update dedicated server status line in console + Platform_UpdateStatusLine (); + // send a heartbeat to the master if needed Master_Heartbeat (); } @@ -738,22 +777,10 @@ void SV_AddToMaster( netadr_t from, sizebuf_t *msg ) { uint challenge; char s[MAX_INFO_STRING] = "0\n"; // skip 2 bytes of header - int clients = 0, bots = 0, index; + int clients = 0, bots = 0; int len = sizeof( s ); - if( svs.clients ) - { - for( index = 0; index < svs.maxclients; index++ ) - { - if( svs.clients[index].state >= cs_connected ) - { - if( FBitSet( svs.clients[index].flags, FCL_FAKECLIENT )) - bots++; - else clients++; - } - } - } - + clients = SV_GetConnectedClientsCount( &bots ); challenge = MSG_ReadUBitLong( msg, sizeof( uint ) << 3 ); Info_SetValueForKey( s, "protocol", va( "%d", PROTOCOL_VERSION ), len ); // protocol version