2019-04-13 12:21:56 -04:00
/* Copyright (C) 2019 Mr Goldberg
This file is part of the Goldberg Emulator
The Goldberg Emulator is free software ; you can redistribute it and / or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation ; either
version 3 of the License , or ( at your option ) any later version .
The Goldberg Emulator 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
Lesser General Public License for more details .
You should have received a copy of the GNU Lesser General Public
License along with the Goldberg Emulator ; if not , see
< http : //www.gnu.org/licenses/>. */
2023-12-27 09:21:59 +02:00
# include "dll/steam_gameserver.h"
2024-02-27 21:17:53 +02:00
# include "dll/source_query.h"
2019-04-13 12:21:56 -04:00
# define SEND_SERVER_RATE 5.0
2024-05-03 01:29:57 +03:00
2019-04-13 12:21:56 -04:00
Steam_GameServer : : Steam_GameServer ( class Settings * settings , class Networking * network , class SteamCallBacks * callbacks )
{
this - > network = network ;
this - > settings = settings ;
this - > callbacks = callbacks ;
2023-12-21 20:25:37 +01:00
auth_manager = new Auth_Manager ( settings , network , callbacks ) ;
2024-05-03 01:29:57 +03:00
server_data . set_id ( settings - > get_local_steam_id ( ) . ConvertToUint64 ( ) ) ;
2019-04-13 12:21:56 -04:00
}
Steam_GameServer : : ~ Steam_GameServer ( )
{
2023-12-21 20:25:37 +01:00
delete auth_manager ;
2024-05-03 01:29:57 +03:00
auth_manager = nullptr ;
2019-04-13 12:21:56 -04:00
}
2024-05-03 01:29:57 +03:00
2024-02-27 21:17:53 +02:00
std : : vector < std : : pair < CSteamID , Gameserver_Player_Info_t > > * Steam_GameServer : : get_players ( )
{
return & players ;
}
2019-04-13 12:21:56 -04:00
//
// Basic server data. These properties, if set, must be set before before calling LogOn. They
// may not be changed after logged in.
//
/// This is called by SteamGameServer_Init, and you will usually not need to call it directly
bool Steam_GameServer : : InitGameServer ( uint32 unIP , uint16 usGamePort , uint16 usQueryPort , uint32 unFlags , AppId_t nGameAppId , const char * pchVersionString )
{
2024-04-06 07:49:07 +02:00
PRINT_DEBUG_ENTRY ( ) ;
2019-04-13 12:21:56 -04:00
std : : lock_guard < std : : recursive_mutex > lock ( global_mutex ) ;
if ( logged_in ) return false ; // may not be changed after logged in.
2022-08-05 18:45:33 -04:00
if ( ! pchVersionString ) pchVersionString = " " ;
2019-04-13 12:21:56 -04:00
std : : string version ( pchVersionString ) ;
version . erase ( std : : remove ( version . begin ( ) , version . end ( ) , ' ' ) , version . end ( ) ) ;
version . erase ( std : : remove ( version . begin ( ) , version . end ( ) , ' . ' ) , version . end ( ) ) ;
2024-04-06 07:49:07 +02:00
PRINT_DEBUG ( " version trimmed '%s' " , version . c_str ( ) ) ;
2024-03-23 07:22:47 +02:00
2022-08-05 18:45:33 -04:00
try {
2024-03-23 07:22:47 +02:00
auto ver = std : : stoul ( version ) ;
server_data . set_version ( ver ) ;
2024-04-06 07:49:07 +02:00
PRINT_DEBUG ( " set version to %lu " , ver ) ;
2022-08-05 18:45:33 -04:00
} catch ( . . . ) {
2024-04-06 07:49:07 +02:00
PRINT_DEBUG ( " not a number: %s " , pchVersionString ) ;
2022-08-05 18:45:33 -04:00
server_data . set_version ( 0 ) ;
}
2019-04-13 12:21:56 -04:00
server_data . set_ip ( unIP ) ;
server_data . set_port ( usGamePort ) ;
server_data . set_query_port ( usQueryPort ) ;
server_data . set_offline ( false ) ;
2024-02-27 21:17:53 +02:00
if ( ! settings - > disable_source_query )
network - > startQuery ( { unIP , usQueryPort } ) ;
2019-04-13 12:21:56 -04:00
if ( ! settings - > get_local_game_id ( ) . AppID ( ) ) settings - > set_game_id ( CGameID ( nGameAppId ) ) ;
2019-05-20 12:35:55 -04:00
//TODO: flags should be k_unServerFlag
2019-04-13 12:21:56 -04:00
flags = unFlags ;
policy_response_called = false ;
call_servers_connected = false ;
call_servers_disconnected = false ;
return true ;
}
/// Game product identifier. This is currently used by the master server for version checking purposes.
/// It's a required field, but will eventually will go away, and the AppID will be used for this purpose.
void Steam_GameServer : : SetProduct ( const char * pszProduct )
{
2024-04-06 07:49:07 +02:00
PRINT_DEBUG ( " %s " , pszProduct ) ;
2019-04-13 12:21:56 -04:00
std : : lock_guard < std : : recursive_mutex > lock ( global_mutex ) ;
2024-02-27 21:17:53 +02:00
// pszGameDescription should be used instead of pszProduct for accurate information
// Example: 'Counter-Strike: Source' instead of 'cstrike'
2019-04-13 12:21:56 -04:00
server_data . set_product ( pszProduct ) ;
}
/// Description of the game. This is a required field and is displayed in the steam server browser....for now.
/// This is a required field, but it will go away eventually, as the data should be determined from the AppID.
void Steam_GameServer : : SetGameDescription ( const char * pszGameDescription )
{
2024-04-06 07:49:07 +02:00
PRINT_DEBUG ( " %s " , pszGameDescription ) ;
2019-04-13 12:21:56 -04:00
std : : lock_guard < std : : recursive_mutex > lock ( global_mutex ) ;
server_data . set_game_description ( pszGameDescription ) ;
2024-02-27 21:17:53 +02:00
//server_data.set_product(pszGameDescription);
2019-04-13 12:21:56 -04:00
}
/// If your game is a "mod," pass the string that identifies it. The default is an empty string, meaning
/// this application is the original game, not a mod.
///
/// @see k_cbMaxGameServerGameDir
void Steam_GameServer : : SetModDir ( const char * pszModDir )
{
2024-04-06 07:49:07 +02:00
PRINT_DEBUG ( " %s " , pszModDir ) ;
2019-04-13 12:21:56 -04:00
std : : lock_guard < std : : recursive_mutex > lock ( global_mutex ) ;
server_data . set_mod_dir ( pszModDir ) ;
}
/// Is this is a dedicated server? The default value is false.
void Steam_GameServer : : SetDedicatedServer ( bool bDedicated )
{
2024-04-06 07:49:07 +02:00
PRINT_DEBUG ( " %i " , bDedicated ) ;
2019-04-13 12:21:56 -04:00
std : : lock_guard < std : : recursive_mutex > lock ( global_mutex ) ;
server_data . set_dedicated_server ( bDedicated ) ;
}
//
// Login
//
/// Begin process to login to a persistent game server account
///
/// You need to register for callbacks to determine the result of this operation.
/// @see SteamServersConnected_t
/// @see SteamServerConnectFailure_t
/// @see SteamServersDisconnected_t
void Steam_GameServer : : LogOn ( const char * pszToken )
{
2024-04-06 07:49:07 +02:00
PRINT_DEBUG ( " %s " , pszToken ) ;
2019-04-13 12:21:56 -04:00
std : : lock_guard < std : : recursive_mutex > lock ( global_mutex ) ;
call_servers_connected = true ;
logged_in = true ;
}
2019-05-20 12:35:55 -04:00
void Steam_GameServer : : LogOn (
const char * pszAccountName ,
const char * pszPassword
)
{
2024-04-06 07:49:07 +02:00
PRINT_DEBUG ( " %s %s " , pszAccountName , pszPassword ) ;
2019-05-20 12:35:55 -04:00
LogOn ( pszAccountName ) ;
}
2019-04-13 12:21:56 -04:00
/// Login to a generic, anonymous account.
///
/// Note: in previous versions of the SDK, this was automatically called within SteamGameServer_Init,
/// but this is no longer the case.
void Steam_GameServer : : LogOnAnonymous ( )
{
2024-04-06 07:49:07 +02:00
PRINT_DEBUG_ENTRY ( ) ;
2019-04-13 12:21:56 -04:00
std : : lock_guard < std : : recursive_mutex > lock ( global_mutex ) ;
call_servers_connected = true ;
logged_in = true ;
}
2019-05-20 12:35:55 -04:00
void Steam_GameServer : : LogOn ( )
{
2024-04-06 07:49:07 +02:00
PRINT_DEBUG_ENTRY ( ) ;
2019-05-20 12:35:55 -04:00
LogOnAnonymous ( ) ;
}
2019-04-13 12:21:56 -04:00
/// Begin process of logging game server out of steam
void Steam_GameServer : : LogOff ( )
{
2024-04-06 07:49:07 +02:00
PRINT_DEBUG_ENTRY ( ) ;
2019-04-13 12:21:56 -04:00
std : : lock_guard < std : : recursive_mutex > lock ( global_mutex ) ;
if ( logged_in ) {
call_servers_disconnected = true ;
}
logged_in = false ;
}
// status functions
bool Steam_GameServer : : BLoggedOn ( )
{
2024-04-06 07:49:07 +02:00
PRINT_DEBUG_ENTRY ( ) ;
2019-04-13 12:21:56 -04:00
std : : lock_guard < std : : recursive_mutex > lock ( global_mutex ) ;
return logged_in ;
}
bool Steam_GameServer : : BSecure ( )
{
2024-04-06 07:49:07 +02:00
PRINT_DEBUG_ENTRY ( ) ;
2019-04-13 12:21:56 -04:00
std : : lock_guard < std : : recursive_mutex > lock ( global_mutex ) ;
2024-02-27 21:17:53 +02:00
if ( ! policy_response_called ) {
server_data . set_secure ( 0 ) ;
return false ;
}
const bool res = ! ! ( flags & k_unServerFlagSecure ) ;
server_data . set_secure ( res ) ;
return res ;
2019-04-13 12:21:56 -04:00
}
CSteamID Steam_GameServer : : GetSteamID ( )
{
2024-04-06 07:49:07 +02:00
PRINT_DEBUG_ENTRY ( ) ;
2019-04-13 12:21:56 -04:00
std : : lock_guard < std : : recursive_mutex > lock ( global_mutex ) ;
2019-05-11 14:44:06 -04:00
if ( ! logged_in ) return k_steamIDNil ;
2019-04-13 12:21:56 -04:00
return settings - > get_local_steam_id ( ) ;
}
/// Returns true if the master server has requested a restart.
/// Only returns true once per request.
bool Steam_GameServer : : WasRestartRequested ( )
{
2024-04-06 07:49:07 +02:00
PRINT_DEBUG_ENTRY ( ) ;
2019-04-13 12:21:56 -04:00
std : : lock_guard < std : : recursive_mutex > lock ( global_mutex ) ;
return false ;
}
//
// Server state. These properties may be changed at any time.
//
/// Max player count that will be reported to server browser and client queries
void Steam_GameServer : : SetMaxPlayerCount ( int cPlayersMax )
{
2024-04-06 07:49:07 +02:00
PRINT_DEBUG ( " %i " , cPlayersMax ) ;
2019-04-13 12:21:56 -04:00
std : : lock_guard < std : : recursive_mutex > lock ( global_mutex ) ;
server_data . set_max_player_count ( cPlayersMax ) ;
}
/// Number of bots. Default value is zero
void Steam_GameServer : : SetBotPlayerCount ( int cBotplayers )
{
2024-04-06 07:49:07 +02:00
PRINT_DEBUG ( " %i " , cBotplayers ) ;
2019-04-13 12:21:56 -04:00
std : : lock_guard < std : : recursive_mutex > lock ( global_mutex ) ;
server_data . set_bot_player_count ( cBotplayers ) ;
}
/// Set the name of server as it will appear in the server browser
///
/// @see k_cbMaxGameServerName
void Steam_GameServer : : SetServerName ( const char * pszServerName )
{
2024-04-06 07:49:07 +02:00
PRINT_DEBUG ( " %s " , pszServerName ) ;
2019-04-13 12:21:56 -04:00
std : : lock_guard < std : : recursive_mutex > lock ( global_mutex ) ;
server_data . set_server_name ( pszServerName ) ;
}
/// Set name of map to report in the server browser
///
/// @see k_cbMaxGameServerName
void Steam_GameServer : : SetMapName ( const char * pszMapName )
{
2024-04-06 07:49:07 +02:00
PRINT_DEBUG ( " %s " , pszMapName ) ;
2019-04-13 12:21:56 -04:00
std : : lock_guard < std : : recursive_mutex > lock ( global_mutex ) ;
server_data . set_map_name ( pszMapName ) ;
}
/// Let people know if your server will require a password
void Steam_GameServer : : SetPasswordProtected ( bool bPasswordProtected )
{
2024-04-06 07:49:07 +02:00
PRINT_DEBUG ( " %i " , bPasswordProtected ) ;
2019-04-13 12:21:56 -04:00
std : : lock_guard < std : : recursive_mutex > lock ( global_mutex ) ;
server_data . set_password_protected ( bPasswordProtected ) ;
}
/// Spectator server. The default value is zero, meaning the service
/// is not used.
void Steam_GameServer : : SetSpectatorPort ( uint16 unSpectatorPort )
{
2024-04-06 07:49:07 +02:00
PRINT_DEBUG_ENTRY ( ) ;
2019-04-13 12:21:56 -04:00
std : : lock_guard < std : : recursive_mutex > lock ( global_mutex ) ;
server_data . set_spectator_port ( unSpectatorPort ) ;
}
/// Name of the spectator server. (Only used if spectator port is nonzero.)
///
/// @see k_cbMaxGameServerMapName
void Steam_GameServer : : SetSpectatorServerName ( const char * pszSpectatorServerName )
{
2024-04-06 07:49:07 +02:00
PRINT_DEBUG ( " %s " , pszSpectatorServerName ) ;
2019-04-13 12:21:56 -04:00
std : : lock_guard < std : : recursive_mutex > lock ( global_mutex ) ;
server_data . set_spectator_server_name ( pszSpectatorServerName ) ;
}
/// Call this to clear the whole list of key/values that are sent in rules queries.
void Steam_GameServer : : ClearAllKeyValues ( )
{
2024-04-06 07:49:07 +02:00
PRINT_DEBUG_ENTRY ( ) ;
2019-04-13 12:21:56 -04:00
std : : lock_guard < std : : recursive_mutex > lock ( global_mutex ) ;
server_data . clear_values ( ) ;
}
/// Call this to add/update a key/value pair.
void Steam_GameServer : : SetKeyValue ( const char * pKey , const char * pValue )
{
2024-04-06 07:49:07 +02:00
PRINT_DEBUG ( " %s %s " , pKey , pValue ) ;
2019-04-13 12:21:56 -04:00
std : : lock_guard < std : : recursive_mutex > lock ( global_mutex ) ;
( * server_data . mutable_values ( ) ) [ std : : string ( pKey ) ] = std : : string ( pValue ) ;
}
/// Sets a string defining the "gametags" for this server, this is optional, but if it is set
/// it allows users to filter in the matchmaking/server-browser interfaces based on the value
///
/// @see k_cbMaxGameServerTags
void Steam_GameServer : : SetGameTags ( const char * pchGameTags )
{
2024-04-06 07:49:07 +02:00
PRINT_DEBUG ( " %s " , pchGameTags ) ;
2019-04-13 12:21:56 -04:00
std : : lock_guard < std : : recursive_mutex > lock ( global_mutex ) ;
server_data . set_tags ( pchGameTags ) ;
}
/// Sets a string defining the "gamedata" for this server, this is optional, but if it is set
/// it allows users to filter in the matchmaking/server-browser interfaces based on the value
/// don't set this unless it actually changes, its only uploaded to the master once (when
/// acknowledged)
///
/// @see k_cbMaxGameServerGameData
void Steam_GameServer : : SetGameData ( const char * pchGameData )
{
2024-04-06 07:49:07 +02:00
PRINT_DEBUG ( " %s " , pchGameData ) ;
2019-04-13 12:21:56 -04:00
std : : lock_guard < std : : recursive_mutex > lock ( global_mutex ) ;
server_data . set_gamedata ( pchGameData ) ;
}
/// Region identifier. This is an optional field, the default value is empty, meaning the "world" region
void Steam_GameServer : : SetRegion ( const char * pszRegion )
{
2024-04-06 07:49:07 +02:00
PRINT_DEBUG ( " %s " , pszRegion ) ;
2019-04-13 12:21:56 -04:00
std : : lock_guard < std : : recursive_mutex > lock ( global_mutex ) ;
server_data . set_region ( pszRegion ) ;
}
//
// Player list management / authentication
//
// Handles receiving a new connection from a Steam user. This call will ask the Steam
// servers to validate the users identity, app ownership, and VAC status. If the Steam servers
// are off-line, then it will validate the cached ticket itself which will validate app ownership
// and identity. The AuthBlob here should be acquired on the game client using SteamUser()->InitiateGameConnection()
// and must then be sent up to the game server for authentication.
//
// Return Value: returns true if the users ticket passes basic checks. pSteamIDUser will contain the Steam ID of this user. pSteamIDUser must NOT be NULL
// If the call succeeds then you should expect a GSClientApprove_t or GSClientDeny_t callback which will tell you whether authentication
// for the user has succeeded or failed (the steamid in the callback will match the one returned by this call)
bool Steam_GameServer : : SendUserConnectAndAuthenticate ( uint32 unIPClient , const void * pvAuthBlob , uint32 cubAuthBlobSize , CSteamID * pSteamIDUser )
{
2024-04-06 07:49:07 +02:00
PRINT_DEBUG ( " %u %u " , unIPClient , cubAuthBlobSize ) ;
2019-04-13 12:21:56 -04:00
std : : lock_guard < std : : recursive_mutex > lock ( global_mutex ) ;
2024-02-27 21:17:53 +02:00
bool res = auth_manager - > SendUserConnectAndAuthenticate ( unIPClient , pvAuthBlob , cubAuthBlobSize , pSteamIDUser ) ;
if ( res ) {
std : : pair < CSteamID , Gameserver_Player_Info_t > infos ;
infos . first = * pSteamIDUser ;
infos . second . join_time = std : : chrono : : steady_clock : : now ( ) ;
infos . second . score = 0 ;
infos . second . name = " unnamed " ;
players . emplace_back ( std : : move ( infos ) ) ;
}
return res ;
2019-04-13 12:21:56 -04:00
}
2022-07-31 15:49:45 -04:00
void Steam_GameServer : : SendUserConnectAndAuthenticate ( CSteamID steamIDUser , uint32 unIPClient , void * pvAuthBlob , uint32 cubAuthBlobSize )
{
2024-04-06 07:49:07 +02:00
PRINT_DEBUG_ENTRY ( ) ;
2022-07-31 15:49:45 -04:00
SendUserConnectAndAuthenticate ( unIPClient , pvAuthBlob , cubAuthBlobSize , NULL ) ;
}
2019-04-13 12:21:56 -04:00
// Creates a fake user (ie, a bot) which will be listed as playing on the server, but skips validation.
//
// Return Value: Returns a SteamID for the user to be tracked with, you should call HandleUserDisconnect()
// when this user leaves the server just like you would for a real user.
CSteamID Steam_GameServer : : CreateUnauthenticatedUserConnection ( )
{
2024-04-06 07:49:07 +02:00
PRINT_DEBUG_ENTRY ( ) ;
2019-04-13 12:21:56 -04:00
std : : lock_guard < std : : recursive_mutex > lock ( global_mutex ) ;
2024-02-27 21:17:53 +02:00
CSteamID bot_id = auth_manager - > fakeUser ( ) ;
std : : pair < CSteamID , Gameserver_Player_Info_t > infos ;
infos . first = bot_id ;
infos . second . join_time = std : : chrono : : steady_clock : : now ( ) ;
infos . second . score = 0 ;
infos . second . name = " unnamed " ;
players . emplace_back ( std : : move ( infos ) ) ;
return bot_id ;
2019-04-13 12:21:56 -04:00
}
// Should be called whenever a user leaves our game server, this lets Steam internally
// track which users are currently on which servers for the purposes of preventing a single
// account being logged into multiple servers, showing who is currently on a server, etc.
void Steam_GameServer : : SendUserDisconnect ( CSteamID steamIDUser )
{
2024-04-06 07:49:07 +02:00
PRINT_DEBUG_ENTRY ( ) ;
2019-04-13 12:21:56 -04:00
std : : lock_guard < std : : recursive_mutex > lock ( global_mutex ) ;
2024-02-27 21:17:53 +02:00
auto player_it = std : : find_if ( players . begin ( ) , players . end ( ) , [ & steamIDUser ] ( std : : pair < CSteamID , Gameserver_Player_Info_t > & player )
{
return player . first = = steamIDUser ;
} ) ;
if ( player_it ! = players . end ( ) )
{
players . erase ( player_it ) ;
}
2023-12-21 20:25:37 +01:00
auth_manager - > endAuth ( steamIDUser ) ;
2019-04-13 12:21:56 -04:00
}
// Update the data to be displayed in the server browser and matchmaking interfaces for a user
// currently connected to the server. For regular users you must call this after you receive a
// GSUserValidationSuccess callback.
//
// Return Value: true if successful, false if failure (ie, steamIDUser wasn't for an active player)
bool Steam_GameServer : : BUpdateUserData ( CSteamID steamIDUser , const char * pchPlayerName , uint32 uScore )
{
2024-04-06 07:49:07 +02:00
PRINT_DEBUG ( " %llu %s %u " , steamIDUser . ConvertToUint64 ( ) , pchPlayerName , uScore ) ;
2024-02-27 21:17:53 +02:00
std : : lock_guard < std : : recursive_mutex > lock ( global_mutex ) ;
auto player_it = std : : find_if ( players . begin ( ) , players . end ( ) , [ & steamIDUser ] ( std : : pair < CSteamID , Gameserver_Player_Info_t > & player )
{
return player . first = = steamIDUser ;
} ) ;
if ( player_it ! = players . end ( ) )
{
if ( pchPlayerName ! = nullptr )
player_it - > second . name = pchPlayerName ;
player_it - > second . score = uScore ;
return true ;
}
return false ;
2019-04-13 12:21:56 -04:00
}
2019-05-20 12:35:55 -04:00
// You shouldn't need to call this as it is called internally by SteamGameServer_Init() and can only be called once.
//
// To update the data in this call which may change during the servers lifetime see UpdateServerStatus() below.
//
// Input: nGameAppID - The Steam assigned AppID for the game
// unServerFlags - Any applicable combination of flags (see k_unServerFlag____ constants below)
// unGameIP - The IP Address the server is listening for client connections on (might be INADDR_ANY)
// unGamePort - The port which the server is listening for client connections on
// unSpectatorPort - the port on which spectators can join to observe the server, 0 if spectating is not supported
// usQueryPort - The port which the ISteamMasterServerUpdater API should use in order to listen for matchmaking requests
// pchGameDir - A unique string identifier for your game
// pchVersion - The current version of the server as a string like 1.0.0.0
// bLanMode - Is this a LAN only server?
//
// bugbug jmccaskey - figure out how to remove this from the API and only expose via SteamGameServer_Init... or make this actually used,
// and stop calling it in SteamGameServer_Init()?
bool Steam_GameServer : : BSetServerType ( uint32 unServerFlags , uint32 unGameIP , uint16 unGamePort ,
uint16 unSpectatorPort , uint16 usQueryPort , const char * pchGameDir , const char * pchVersion , bool bLANMode )
{
2024-04-06 07:49:07 +02:00
PRINT_DEBUG_ENTRY ( ) ;
2019-05-20 12:35:55 -04:00
std : : lock_guard < std : : recursive_mutex > lock ( global_mutex ) ;
server_data . set_ip ( unGameIP ) ;
server_data . set_port ( unGamePort ) ;
server_data . set_query_port ( usQueryPort ) ;
server_data . set_spectator_port ( unSpectatorPort ) ;
std : : string version ( pchVersion ) ;
version . erase ( std : : remove ( version . begin ( ) , version . end ( ) , ' ' ) , version . end ( ) ) ;
version . erase ( std : : remove ( version . begin ( ) , version . end ( ) , ' . ' ) , version . end ( ) ) ;
server_data . set_version ( stoi ( version ) ) ;
2020-01-15 10:11:37 -05:00
flags = unServerFlags ;
2019-05-20 12:35:55 -04:00
//TODO?
return true ;
}
2022-07-31 15:49:45 -04:00
bool Steam_GameServer : : BSetServerType ( int32 nGameAppId , uint32 unServerFlags , uint32 unGameIP , uint16 unGamePort ,
uint16 unSpectatorPort , uint16 usQueryPort , const char * pchGameDir , const char * pchVersion , bool bLANMode )
{
return BSetServerType ( unServerFlags , unGameIP , unGamePort , unSpectatorPort , usQueryPort , pchGameDir , pchVersion , bLANMode ) ;
}
2019-05-20 12:35:55 -04:00
// Updates server status values which shows up in the server browser and matchmaking APIs
void Steam_GameServer : : UpdateServerStatus ( int cPlayers , int cPlayersMax , int cBotPlayers ,
const char * pchServerName , const char * pSpectatorServerName ,
const char * pchMapName )
{
2024-04-06 07:49:07 +02:00
PRINT_DEBUG_ENTRY ( ) ;
2019-05-20 12:35:55 -04:00
std : : lock_guard < std : : recursive_mutex > lock ( global_mutex ) ;
server_data . set_num_players ( cPlayers ) ;
server_data . set_max_player_count ( cPlayersMax ) ;
server_data . set_bot_player_count ( cBotPlayers ) ;
server_data . set_server_name ( pchServerName ) ;
server_data . set_spectator_server_name ( pSpectatorServerName ) ;
server_data . set_map_name ( pchMapName ) ;
}
// This can be called if spectator goes away or comes back (passing 0 means there is no spectator server now).
void Steam_GameServer : : UpdateSpectatorPort ( uint16 unSpectatorPort )
{
2024-04-06 07:49:07 +02:00
PRINT_DEBUG_ENTRY ( ) ;
2019-05-20 12:35:55 -04:00
SetSpectatorPort ( unSpectatorPort ) ;
}
// Sets a string defining the "gametype" for this server, this is optional, but if it is set
// it allows users to filter in the matchmaking/server-browser interfaces based on the value
void Steam_GameServer : : SetGameType ( const char * pchGameType )
{
2024-04-06 07:49:07 +02:00
PRINT_DEBUG_ENTRY ( ) ;
2019-05-20 12:35:55 -04:00
std : : lock_guard < std : : recursive_mutex > lock ( global_mutex ) ;
}
// Ask if a user has a specific achievement for this game, will get a callback on reply
bool Steam_GameServer : : BGetUserAchievementStatus ( CSteamID steamID , const char * pchAchievementName )
{
2024-04-06 07:49:07 +02:00
PRINT_DEBUG_ENTRY ( ) ;
2019-05-20 12:35:55 -04:00
std : : lock_guard < std : : recursive_mutex > lock ( global_mutex ) ;
return false ;
}
2019-04-13 12:21:56 -04:00
// New auth system APIs - do not mix with the old auth system APIs.
// ----------------------------------------------------------------
// Retrieve ticket to be sent to the entity who wishes to authenticate you ( using BeginAuthSession API ).
// pcbTicket retrieves the length of the actual ticket.
HAuthTicket Steam_GameServer : : GetAuthSessionTicket ( void * pTicket , int cbMaxTicket , uint32 * pcbTicket )
2023-05-08 22:36:51 -04:00
{
2024-04-06 07:49:07 +02:00
PRINT_DEBUG_ENTRY ( ) ;
2023-05-08 22:36:51 -04:00
return GetAuthSessionTicket ( pTicket , cbMaxTicket , pcbTicket , NULL ) ;
}
// SteamNetworkingIdentity is an optional parameter to hold the public IP address of the entity you are connecting to
// if an IP address is passed Steam will only allow the ticket to be used by an entity with that IP address
HAuthTicket Steam_GameServer : : GetAuthSessionTicket ( void * pTicket , int cbMaxTicket , uint32 * pcbTicket , const SteamNetworkingIdentity * pSnid )
2019-04-13 12:21:56 -04:00
{
2024-04-06 07:49:07 +02:00
PRINT_DEBUG_ENTRY ( ) ;
2019-04-13 12:21:56 -04:00
std : : lock_guard < std : : recursive_mutex > lock ( global_mutex ) ;
2023-12-22 09:05:19 +02:00
if ( ! pTicket ) return k_HAuthTicketInvalid ;
2023-12-21 20:25:37 +01:00
return auth_manager - > getTicket ( pTicket , cbMaxTicket , pcbTicket ) ;
2019-04-13 12:21:56 -04:00
}
// Authenticate ticket ( from GetAuthSessionTicket ) from entity steamID to be sure it is valid and isnt reused
// Registers for callbacks if the entity goes offline or cancels the ticket ( see ValidateAuthTicketResponse_t callback and EAuthSessionResponse )
EBeginAuthSessionResult Steam_GameServer : : BeginAuthSession ( const void * pAuthTicket , int cbAuthTicket , CSteamID steamID )
{
2024-04-06 07:49:07 +02:00
PRINT_DEBUG ( " %i %llu " , cbAuthTicket , steamID . ConvertToUint64 ( ) ) ;
2019-04-13 12:21:56 -04:00
std : : lock_guard < std : : recursive_mutex > lock ( global_mutex ) ;
2024-02-27 21:17:53 +02:00
std : : pair < CSteamID , Gameserver_Player_Info_t > infos ;
infos . first = steamID ;
infos . second . join_time = std : : chrono : : steady_clock : : now ( ) ;
infos . second . score = 0 ;
infos . second . name = " unnamed " ;
players . emplace_back ( std : : move ( infos ) ) ;
2023-12-21 20:25:37 +01:00
return auth_manager - > beginAuth ( pAuthTicket , cbAuthTicket , steamID ) ;
2019-04-13 12:21:56 -04:00
}
// Stop tracking started by BeginAuthSession - called when no longer playing game with this entity
void Steam_GameServer : : EndAuthSession ( CSteamID steamID )
{
2024-04-06 07:49:07 +02:00
PRINT_DEBUG ( " %llu " , steamID . ConvertToUint64 ( ) ) ;
2019-04-13 12:21:56 -04:00
std : : lock_guard < std : : recursive_mutex > lock ( global_mutex ) ;
2024-02-27 21:17:53 +02:00
auto player_it = std : : find_if ( players . begin ( ) , players . end ( ) , [ & steamID ] ( std : : pair < CSteamID , Gameserver_Player_Info_t > & player )
{
return player . first = = steamID ;
} ) ;
if ( player_it ! = players . end ( ) )
{
players . erase ( player_it ) ;
}
2023-12-21 20:25:37 +01:00
auth_manager - > endAuth ( steamID ) ;
2019-04-13 12:21:56 -04:00
}
// Cancel auth ticket from GetAuthSessionTicket, called when no longer playing game with the entity you gave the ticket to
void Steam_GameServer : : CancelAuthTicket ( HAuthTicket hAuthTicket )
{
2024-04-06 07:49:07 +02:00
PRINT_DEBUG_ENTRY ( ) ;
2019-04-13 12:21:56 -04:00
std : : lock_guard < std : : recursive_mutex > lock ( global_mutex ) ;
2023-12-21 20:25:37 +01:00
auth_manager - > cancelTicket ( hAuthTicket ) ;
2019-04-13 12:21:56 -04:00
}
// After receiving a user's authentication data, and passing it to SendUserConnectAndAuthenticate, use this function
// to determine if the user owns downloadable content specified by the provided AppID.
EUserHasLicenseForAppResult Steam_GameServer : : UserHasLicenseForApp ( CSteamID steamID , AppId_t appID )
{
2024-04-06 07:49:07 +02:00
PRINT_DEBUG_ENTRY ( ) ;
2023-12-19 22:17:29 +02:00
std : : lock_guard < std : : recursive_mutex > lock ( global_mutex ) ;
2019-04-13 12:21:56 -04:00
return k_EUserHasLicenseResultHasLicense ;
}
// Ask if a user in in the specified group, results returns async by GSUserGroupStatus_t
// returns false if we're not connected to the steam servers and thus cannot ask
bool Steam_GameServer : : RequestUserGroupStatus ( CSteamID steamIDUser , CSteamID steamIDGroup )
{
2024-04-06 07:49:07 +02:00
PRINT_DEBUG_ENTRY ( ) ;
2019-04-13 12:21:56 -04:00
std : : lock_guard < std : : recursive_mutex > lock ( global_mutex ) ;
return true ;
}
// these two functions s are deprecated, and will not return results
// they will be removed in a future version of the SDK
void Steam_GameServer : : GetGameplayStats ( )
{
2024-04-11 06:40:29 +02:00
PRINT_DEBUG_TODO ( ) ;
2023-12-19 22:17:29 +02:00
std : : lock_guard < std : : recursive_mutex > lock ( global_mutex ) ;
2019-04-13 12:21:56 -04:00
}
STEAM_CALL_RESULT ( GSReputation_t )
SteamAPICall_t Steam_GameServer : : GetServerReputation ( )
{
2024-04-11 06:40:29 +02:00
PRINT_DEBUG_TODO ( ) ;
2019-04-13 12:21:56 -04:00
std : : lock_guard < std : : recursive_mutex > lock ( global_mutex ) ;
return 0 ;
}
// Returns the public IP of the server according to Steam, useful when the server is
// behind NAT and you want to advertise its IP in a lobby for other clients to directly
// connect to
2019-12-09 12:49:55 -05:00
uint32 Steam_GameServer : : GetPublicIP_old ( )
2019-04-13 12:21:56 -04:00
{
2024-04-06 07:49:07 +02:00
PRINT_DEBUG_ENTRY ( ) ;
2019-04-13 12:21:56 -04:00
std : : lock_guard < std : : recursive_mutex > lock ( global_mutex ) ;
uint32 ip = network - > getOwnIP ( ) ;
2024-04-06 07:49:07 +02:00
PRINT_DEBUG ( " %X " , ip ) ;
2019-04-13 12:21:56 -04:00
return ip ;
}
2019-12-09 12:49:55 -05:00
SteamIPAddress_t Steam_GameServer : : GetPublicIP ( )
{
2024-04-06 07:49:07 +02:00
PRINT_DEBUG_ENTRY ( ) ;
2019-12-09 12:49:55 -05:00
SteamIPAddress_t ip = SteamIPAddress_t : : IPv4Any ( ) ;
ip . m_unIPv4 = GetPublicIP_old ( ) ;
return ip ;
}
2019-04-13 12:21:56 -04:00
2021-04-09 14:53:20 -04:00
void Steam_GameServer : : GetPublicIP_fix ( SteamIPAddress_t * out )
{
2024-04-06 07:49:07 +02:00
PRINT_DEBUG_ENTRY ( ) ;
2021-04-09 14:53:20 -04:00
if ( out ) * out = GetPublicIP ( ) ;
}
2019-04-13 12:21:56 -04:00
// These are in GameSocketShare mode, where instead of ISteamGameServer creating its own
// socket to talk to the master server on, it lets the game use its socket to forward messages
// back and forth. This prevents us from requiring server ops to open up yet another port
// in their firewalls.
//
// the IP address and port should be in host order, i.e 127.0.0.1 == 0x7f000001
// These are used when you've elected to multiplex the game server's UDP socket
// rather than having the master server updater use its own sockets.
//
// Source games use this to simplify the job of the server admins, so they
// don't have to open up more ports on their firewalls.
// Call this when a packet that starts with 0xFFFFFFFF comes in. That means
// it's for us.
bool Steam_GameServer : : HandleIncomingPacket ( const void * pData , int cbData , uint32 srcIP , uint16 srcPort )
{
2024-04-06 07:49:07 +02:00
PRINT_DEBUG ( " %i %X %i " , cbData , srcIP , srcPort ) ;
2019-04-13 12:21:56 -04:00
std : : lock_guard < std : : recursive_mutex > lock ( global_mutex ) ;
2024-02-27 21:17:53 +02:00
if ( settings - > disable_source_query ) return true ;
Gameserver_Outgoing_Packet packet ;
packet . data = std : : move ( Source_Query : : handle_source_query ( pData , cbData , server_data ) ) ;
if ( packet . data . empty ( ) )
return false ;
packet . ip = srcIP ;
packet . port = srcPort ;
outgoing_packets . emplace_back ( std : : move ( packet ) ) ;
2019-04-13 12:21:56 -04:00
return true ;
}
// AFTER calling HandleIncomingPacket for any packets that came in that frame, call this.
// This gets a packet that the master server updater needs to send out on UDP.
// It returns the length of the packet it wants to send, or 0 if there are no more packets to send.
// Call this each frame until it returns 0.
int Steam_GameServer : : GetNextOutgoingPacket ( void * pOut , int cbMaxOut , uint32 * pNetAdr , uint16 * pPort )
{
2024-04-06 07:49:07 +02:00
PRINT_DEBUG_ENTRY ( ) ;
2019-04-13 12:21:56 -04:00
std : : lock_guard < std : : recursive_mutex > lock ( global_mutex ) ;
2024-02-27 21:17:53 +02:00
if ( settings - > disable_source_query ) return 0 ;
2024-06-04 14:58:16 +03:00
if ( outgoing_packets . empty ( ) ) return 0 ;
2019-04-13 12:21:56 -04:00
2024-06-04 14:58:16 +03:00
if ( cbMaxOut > 0 ) {
if ( outgoing_packets . back ( ) . data . size ( ) < static_cast < size_t > ( cbMaxOut ) ) {
cbMaxOut = static_cast < int > ( outgoing_packets . back ( ) . data . size ( ) ) ;
}
if ( pOut ) memcpy ( pOut , outgoing_packets . back ( ) . data . data ( ) , cbMaxOut ) ;
}
2019-04-13 12:21:56 -04:00
if ( pNetAdr ) * pNetAdr = outgoing_packets . back ( ) . ip ;
if ( pPort ) * pPort = outgoing_packets . back ( ) . port ;
outgoing_packets . pop_back ( ) ;
return cbMaxOut ;
}
//
// Control heartbeats / advertisement with master server
//
// Call this as often as you like to tell the master server updater whether or not
// you want it to be active (default: off).
void Steam_GameServer : : EnableHeartbeats ( bool bActive )
{
2024-04-06 07:49:07 +02:00
PRINT_DEBUG_TODO ( ) ;
2019-04-13 12:21:56 -04:00
}
2021-12-06 16:51:17 -05:00
/// Indicate whether you wish to be listed on the master server list
/// and/or respond to server browser / LAN discovery packets.
/// The server starts with this value set to false. You should set all
/// relevant server parameters before enabling advertisement on the server.
///
/// (This function used to be named EnableHeartbeats, so if you are wondering
/// where that function went, it's right here. It does the same thing as before,
/// the old name was just confusing.)
void Steam_GameServer : : SetAdvertiseServerActive ( bool bActive )
{
2024-04-06 07:49:07 +02:00
PRINT_DEBUG ( " %i " , bActive ) ;
2021-12-06 16:51:17 -05:00
EnableHeartbeats ( bActive ) ;
}
2019-04-13 12:21:56 -04:00
// You usually don't need to modify this.
// Pass -1 to use the default value for iHeartbeatInterval.
// Some mods change this.
void Steam_GameServer : : SetHeartbeatInterval ( int iHeartbeatInterval )
{
2024-04-06 07:49:07 +02:00
PRINT_DEBUG_TODO ( ) ;
2019-04-13 12:21:56 -04:00
}
2021-12-06 16:51:17 -05:00
void Steam_GameServer : : SetMasterServerHeartbeatInterval_DEPRECATED ( int iHeartbeatInterval )
{
2024-04-06 07:49:07 +02:00
PRINT_DEBUG_TODO ( ) ;
2021-12-06 16:51:17 -05:00
}
2019-04-13 12:21:56 -04:00
// Force a heartbeat to steam at the next opportunity
void Steam_GameServer : : ForceHeartbeat ( )
{
2024-04-06 07:49:07 +02:00
PRINT_DEBUG_TODO ( ) ;
2019-04-13 12:21:56 -04:00
}
2021-12-06 16:51:17 -05:00
void Steam_GameServer : : ForceMasterServerHeartbeat_DEPRECATED ( )
{
2024-04-06 07:49:07 +02:00
PRINT_DEBUG_TODO ( ) ;
2024-11-08 20:06:05 +02:00
ForceHeartbeat ( ) ;
2021-12-06 16:51:17 -05:00
}
2019-04-13 12:21:56 -04:00
// associate this game server with this clan for the purposes of computing player compat
STEAM_CALL_RESULT ( AssociateWithClanResult_t )
SteamAPICall_t Steam_GameServer : : AssociateWithClan ( CSteamID steamIDClan )
{
2024-04-06 07:49:07 +02:00
PRINT_DEBUG_TODO ( ) ;
2019-04-13 12:21:56 -04:00
std : : lock_guard < std : : recursive_mutex > lock ( global_mutex ) ;
return 0 ;
}
// ask if any of the current players dont want to play with this new player - or vice versa
STEAM_CALL_RESULT ( ComputeNewPlayerCompatibilityResult_t )
SteamAPICall_t Steam_GameServer : : ComputeNewPlayerCompatibility ( CSteamID steamIDNewPlayer )
{
2024-04-06 07:49:07 +02:00
PRINT_DEBUG_TODO ( ) ;
2019-04-13 12:21:56 -04:00
std : : lock_guard < std : : recursive_mutex > lock ( global_mutex ) ;
return 0 ;
}
2024-05-03 01:29:57 +03:00
2019-04-13 12:21:56 -04:00
void Steam_GameServer : : RunCallbacks ( )
{
bool temp_call_servers_connected = call_servers_connected ;
bool temp_call_servers_disconnected = call_servers_disconnected ;
call_servers_disconnected = call_servers_connected = false ;
if ( temp_call_servers_connected ) {
2024-04-06 07:49:07 +02:00
PRINT_DEBUG ( " SteamServersConnected_t " ) ;
2019-04-13 12:21:56 -04:00
SteamServersConnected_t data ;
2019-07-14 16:25:16 -04:00
callbacks - > addCBResult ( data . k_iCallback , & data , sizeof ( data ) , 0.1 ) ;
2019-04-13 12:21:56 -04:00
}
if ( logged_in & & ! policy_response_called ) {
2024-04-06 07:49:07 +02:00
PRINT_DEBUG ( " GSPolicyResponse_t " ) ;
2019-04-13 12:21:56 -04:00
GSPolicyResponse_t data ;
2020-01-15 10:11:37 -05:00
data . m_bSecure = ! ! ( flags & k_unServerFlagSecure ) ;
2019-07-14 16:25:16 -04:00
callbacks - > addCBResult ( data . k_iCallback , & data , sizeof ( data ) , 0.11 ) ;
2019-04-13 12:21:56 -04:00
policy_response_called = true ;
}
if ( logged_in & & check_timedout ( last_sent_server_info , SEND_SERVER_RATE ) ) {
2024-04-06 07:49:07 +02:00
PRINT_DEBUG ( " Sending Gameserver " ) ;
2019-04-13 12:21:56 -04:00
Common_Message msg ;
msg . set_source_id ( settings - > get_local_steam_id ( ) . ConvertToUint64 ( ) ) ;
server_data . set_appid ( settings - > get_local_game_id ( ) . AppID ( ) ) ;
msg . set_allocated_gameserver ( new Gameserver ( server_data ) ) ;
2023-12-21 20:25:37 +01:00
msg . mutable_gameserver ( ) - > set_num_players ( auth_manager - > countInboundAuth ( ) ) ;
2019-04-13 12:21:56 -04:00
network - > sendToAllIndividuals ( & msg , true ) ;
last_sent_server_info = std : : chrono : : high_resolution_clock : : now ( ) ;
}
if ( temp_call_servers_disconnected ) {
2024-11-08 20:06:05 +02:00
PRINT_DEBUG ( " Gameserver is disconnected " ) ;
SteamServersDisconnected_t data { } ;
2019-04-13 12:21:56 -04:00
data . m_eResult = k_EResultOK ;
callbacks - > addCBResult ( data . k_iCallback , & data , sizeof ( data ) ) ;
if ( ! logged_in ) {
2024-11-08 20:06:05 +02:00
PRINT_DEBUG ( " notifying all that Gameserver is not logged in " ) ;
Common_Message msg { } ;
2019-04-13 12:21:56 -04:00
msg . set_source_id ( settings - > get_local_steam_id ( ) . ConvertToUint64 ( ) ) ;
msg . set_allocated_gameserver ( new Gameserver ( server_data ) ) ;
msg . mutable_gameserver ( ) - > set_offline ( true ) ;
network - > sendToAllIndividuals ( & msg , true ) ;
2024-02-27 21:17:53 +02:00
// Shutdown Source Query
network - > shutDownQuery ( ) ;
// And empty the queue if needed
outgoing_packets . clear ( ) ;
2019-04-13 12:21:56 -04:00
}
}
}