2022-08-14 15:43:49 +02:00
//=============================================================================//
//
// Purpose:
//
//-----------------------------------------------------------------------------
//
//=============================================================================//
# include "core/stdafx.h"
2022-08-27 18:57:56 +02:00
# include "tier0/threadtools.h"
2022-08-22 01:56:20 +02:00
# include "tier0/frametask.h"
2022-08-14 15:43:49 +02:00
# include "tier1/cmd.h"
# include "tier1/cvar.h"
# include "engine/net.h"
2022-08-22 01:56:20 +02:00
# include "engine/host_state.h"
2022-08-28 17:40:03 +02:00
# ifndef CLIENT_DLL
# include "engine/server/server.h"
# endif // !CLIENT_DLL
2022-08-14 15:43:49 +02:00
# include "vpc/keyvalues.h"
# include "pylon.h"
# include "listmanager.h"
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
CServerListManager : : CServerListManager ( void )
: m_HostingStatus ( EHostStatus_t : : NOT_HOSTING )
, m_ServerVisibility ( EServerVisibility_t : : OFFLINE )
{
}
//-----------------------------------------------------------------------------
2022-08-27 18:57:56 +02:00
// Purpose: get server list from pylon
2022-08-14 15:43:49 +02:00
// Input : &svMessage -
2022-08-27 18:57:56 +02:00
// Output : amount of servers found
2022-08-14 15:43:49 +02:00
//-----------------------------------------------------------------------------
2022-08-27 18:57:56 +02:00
size_t CServerListManager : : RefreshServerList ( string & svMessage )
2022-08-14 15:43:49 +02:00
{
2022-08-27 18:57:56 +02:00
ClearServerList ( ) ;
vector < NetGameServer_t > vServerList = g_pMasterServer - > GetServerList ( svMessage ) ;
std : : lock_guard < std : : mutex > l ( m_Mutex ) ;
m_vServerList = vServerList ;
return m_vServerList . size ( ) ;
}
//-----------------------------------------------------------------------------
// Purpose: clears the server list
//-----------------------------------------------------------------------------
void CServerListManager : : ClearServerList ( void )
{
std : : lock_guard < std : : mutex > l ( m_Mutex ) ;
2022-08-14 15:43:49 +02:00
m_vServerList . clear ( ) ;
}
//-----------------------------------------------------------------------------
// Purpose: Launch server with given parameters
//-----------------------------------------------------------------------------
void CServerListManager : : LaunchServer ( void ) const
{
# ifndef CLIENT_DLL
2022-08-27 18:57:56 +02:00
if ( ! ThreadInMainThread ( ) )
{
g_TaskScheduler - > Dispatch ( [ this ] ( )
{
this - > LaunchServer ( ) ;
} , 0 ) ;
return ;
}
DevMsg ( eDLL_T : : ENGINE , " Starting server with name: \" %s \" map: \" %s \" playlist: \" %s \" \n " , m_Server . m_svHostName . c_str ( ) , m_Server . m_svHostMap . c_str ( ) , m_Server . m_svPlaylist . c_str ( ) ) ;
2022-08-14 15:43:49 +02:00
/*
2022-09-09 19:47:31 +02:00
* Playlist gets parsed in two instances , first in KeyValues : : ParsePlaylists with all the necessary values .
2022-08-14 15:43:49 +02:00
* Then when you would normally call launchplaylist which calls StartPlaylist it would cmd call mp_gamemode which parses the gamemode specific part of the playlist . .
*/
KeyValues : : ParsePlaylists ( m_Server . m_svPlaylist . c_str ( ) ) ;
mp_gamemode - > SetValue ( m_Server . m_svPlaylist . c_str ( ) ) ;
2022-08-22 01:56:20 +02:00
2022-08-28 17:40:03 +02:00
if ( g_pServer - > IsActive ( ) )
2022-08-22 01:56:20 +02:00
{
2022-08-27 18:57:56 +02:00
ProcessCommand ( fmt : : format ( " {:s} \" {:s} \" " , " changelevel " , m_Server . m_svHostMap ) . c_str ( ) ) ;
2022-08-22 01:56:20 +02:00
}
else // Initial launch.
{
2022-08-27 18:57:56 +02:00
ProcessCommand ( fmt : : format ( " {:s} \" {:s} \" " , " map " , m_Server . m_svHostMap ) . c_str ( ) ) ;
2022-08-22 01:56:20 +02:00
}
2022-08-14 15:43:49 +02:00
# endif // !CLIENT_DLL
}
//-----------------------------------------------------------------------------
// Purpose: connects to specified server
// Input : &svIp -
// &svPort -
// &svNetKey -
//-----------------------------------------------------------------------------
void CServerListManager : : ConnectToServer ( const string & svIp , const string & svPort , const string & svNetKey ) const
{
2022-08-27 23:45:58 +02:00
if ( ! ThreadInMainThread ( ) )
{
g_TaskScheduler - > Dispatch ( [ this , svIp , svPort , svNetKey ] ( )
{
this - > ConnectToServer ( svIp , svPort , svNetKey ) ;
} , 0 ) ;
return ;
}
2022-08-14 15:43:49 +02:00
if ( ! svNetKey . empty ( ) )
{
NET_SetKey ( svNetKey ) ;
}
ProcessCommand ( fmt : : format ( " {:s} \" [{:s}]:{:s} \" " , " connect " , svIp , svPort ) . c_str ( ) ) ;
}
//-----------------------------------------------------------------------------
// Purpose: connects to specified server
// Input : &svServer -
// &svNetKey -
//-----------------------------------------------------------------------------
void CServerListManager : : ConnectToServer ( const string & svServer , const string & svNetKey ) const
{
2022-08-27 23:45:58 +02:00
if ( ! ThreadInMainThread ( ) )
{
g_TaskScheduler - > Dispatch ( [ this , svServer , svNetKey ] ( )
{
this - > ConnectToServer ( svServer , svNetKey ) ;
} , 0 ) ;
return ;
}
2022-08-14 15:43:49 +02:00
if ( ! svNetKey . empty ( ) )
{
NET_SetKey ( svNetKey ) ;
}
ProcessCommand ( fmt : : format ( " {:s} \" {:s} \" " , " connect " , svServer ) . c_str ( ) ) ;
}
//-----------------------------------------------------------------------------
// Purpose: executes submitted commands in a separate thread
// Input : *pszCommand -
//-----------------------------------------------------------------------------
void CServerListManager : : ProcessCommand ( const char * pszCommand ) const
{
Cbuf_AddText ( Cbuf_GetCurrentPlayer ( ) , pszCommand , cmd_source_t : : kCommandSrcCode ) ;
2022-08-27 18:57:56 +02:00
//g_TaskScheduler->Dispatch(Cbuf_Execute, 0); // Run in main thread.
2022-08-14 15:43:49 +02:00
}
CServerListManager * g_pServerListManager = new CServerListManager ( ) ;