summaryrefslogtreecommitdiff
path: root/engine/cl_steamauth.cpp
blob: e5cbc83b46b28a83f3a37617eb7e00af5e31bef0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: steam state machine that handles authenticating steam users
//
//=============================================================================//
#ifdef _WIN32
#if !defined( _X360 )
#include "winlite.h"
#include <winsock2.h> // INADDR_ANY defn
#endif
#elif POSIX
#include <netinet/in.h>
#endif

#include <utlbuffer.h>
#include "cl_steamauth.h"
#include "interface.h"
#include "filesystem_engine.h"
#include "tier0/icommandline.h"
#include "tier0/vprof.h"
#include "host.h"
#include "cmd.h"
#include "common.h"
#ifndef SWDS
#include "vgui_baseui_interface.h"
#endif

#pragma warning( disable: 4355 ) // disables ' 'this' : used in base member initializer list'

//-----------------------------------------------------------------------------
// Purpose: singleton accessor
//-----------------------------------------------------------------------------
static CSteam3Client s_Steam3Client;
CSteam3Client  &Steam3Client()
{
	return s_Steam3Client;
}


//-----------------------------------------------------------------------------
// Purpose: Constructor
//-----------------------------------------------------------------------------
CSteam3Client::CSteam3Client() 
#if !defined(NO_STEAM)
:
			m_CallbackClientGameServerDeny( this, &CSteam3Client::OnClientGameServerDeny ),
			m_CallbackGameServerChangeRequested( this, &CSteam3Client::OnGameServerChangeRequested ),
			m_CallbackGameOverlayActivated( this, &CSteam3Client::OnGameOverlayActivated ),
			m_CallbackPersonaStateChanged( this, &CSteam3Client::OnPersonaUpdated ),
			m_CallbackLowBattery( this, &CSteam3Client::OnLowBattery )
#endif
{
	m_bActive = false;
	m_bGSSecure = false;
	m_hAuthTicket = k_HAuthTicketInvalid;
	m_unIP = 0;
	m_usPort = 0;
	m_nTicketSize = 0;
}


//-----------------------------------------------------------------------------
// Purpose: Destructor
//-----------------------------------------------------------------------------
CSteam3Client::~CSteam3Client()
{
	Shutdown();
}


//-----------------------------------------------------------------------------
// Purpose: Unload the steam3 engine
//-----------------------------------------------------------------------------
void CSteam3Client::Shutdown()
{	
	if ( !m_bActive )
		return;

	m_bActive = false;	
#if !defined( NO_STEAM )
	SteamAPI_Shutdown();
	Clear(); // clear our interface pointers now they are invalid
#endif
}


//-----------------------------------------------------------------------------
// Purpose: Initialize the steam3 connection
//-----------------------------------------------------------------------------
void CSteam3Client::Activate()
{
	if ( m_bActive )
		return;

	m_bActive = true;
	m_bGSSecure = false;

#if !defined( NO_STEAM )
	SteamAPI_InitSafe(); // ignore failure, that will fall out later when they don't get a valid logon cookie
	SteamAPI_SetTryCatchCallbacks( false ); // We don't use exceptions, so tell steam not to use try/catch in callback handlers
	Init(); // Steam API context init
#endif
}


//-----------------------------------------------------------------------------
// Purpose: Get the steam3 logon cookie to use
//-----------------------------------------------------------------------------
void CSteam3Client::GetAuthSessionTicket( void *pTicket, int cbMaxTicket, uint32 *pcbTicket, uint32 unIP, uint16 usPort, uint64 unGSSteamID,  bool bSecure )
{
#ifdef NO_STEAM
	m_bGSSecure = bSecure;
#else
	CSteamID steamIDGS( unGSSteamID );

	// Assume failure
	*pcbTicket = 0;

	// We must have interface pointers
	if ( !SteamUser() )
	{
		Warning( "No SteamUser interface.  Cannot perform steam authentication\n" );
		return;
	}

	// Make sure we have a valid Steam ID
	CSteamID steamID = SteamUser()->GetSteamID();
	if ( !steamID.IsValid() )
	{
		Warning( "Our steam ID %s is not valid.  Steam must be running and you must be logged in\n", steamID.Render() );
		return;
	}

	// Get a new ticket, if we don't already have one for this server
	if ( m_hAuthTicket == k_HAuthTicketInvalid
		|| m_unIP != unIP
		|| m_usPort != usPort
		|| m_bGSSecure != bSecure
		|| m_steamIDGS != steamIDGS
		|| m_nTicketSize <= 0 )
	{

		// Cancel any previously issues ticket
		if ( m_hAuthTicket != k_HAuthTicketInvalid )
			SteamUser()->CancelAuthTicket( m_hAuthTicket );

		// Shove the GS ID in the first bits
		*(uint64*)m_arbTicketData = SteamUser()->GetSteamID().ConvertToUint64();

		// Ask Steam for a ticket
		m_nTicketSize = 0;
		m_hAuthTicket = SteamUser()->GetAuthSessionTicket( m_arbTicketData+sizeof(uint64), sizeof(m_arbTicketData)-sizeof(uint64), &m_nTicketSize );
		if ( m_hAuthTicket == k_HAuthTicketInvalid || m_nTicketSize <= 0 )
		{
			// Failed!
			Assert( m_hAuthTicket != k_HAuthTicketInvalid );
			Assert( m_nTicketSize > 0 );
			m_hAuthTicket = k_HAuthTicketInvalid;
			m_nTicketSize = 0;
			Warning( "ISteamUser::GetAuthSessionTicket failed to return a valid ticket\n" );
		}
		else
		{
			// Got valid ticket.  Remember its properties
			m_nTicketSize += sizeof(uint64);
			m_unIP = unIP;
			m_usPort = usPort;
			m_bGSSecure = bSecure;
			m_steamIDGS = steamIDGS;
		}
	}

	// Give them back the ticket data, if we were able to get one, and it will fit
	*pcbTicket = 0;
	if ( m_nTicketSize > 0 )
	{
		if ( cbMaxTicket >= (int)m_nTicketSize )
		{
			memcpy( pTicket, m_arbTicketData, m_nTicketSize );
			*pcbTicket = m_nTicketSize;
		}
		else
		{
			Assert( cbMaxTicket >= (int)m_nTicketSize );
		}
	}

	// Tell the steam backend about the server we are playing on - so that it can broadcast this to our friends and they can join.
	// This may be something that you should NOT do if you are on a listen server - or you know that the game is not joinable
	// for some reason.
	#ifdef _DEBUG
		Msg( "Sending AdvertiseGame %s %s (%s)\n", steamIDGS.Render(), netadr_t(unIP, usPort).ToString(), m_bGSSecure ? "secure" : "insecure" );
	#endif
	SteamUser()->AdvertiseGame( steamIDGS, unIP, usPort );
#endif
}


//-----------------------------------------------------------------------------
// Purpose: Tell steam that we are leaving a server
//-----------------------------------------------------------------------------
void CSteam3Client::CancelAuthTicket()
{
	m_bGSSecure = false;
	if ( !SteamUser() )
		return;

#if !defined( NO_STEAM )
	if ( m_hAuthTicket != k_HAuthTicketInvalid )
		SteamUser()->CancelAuthTicket( m_hAuthTicket );
	#ifdef _DEBUG
		Msg( "Clearing auth ticket, sending void AdvertiseGame\n" );
	#endif
	CSteamID steamIDGS; // invalid steamID means not playing anywhere
	SteamUser()->AdvertiseGame( steamIDGS, 0, 0 );
	m_hAuthTicket = k_HAuthTicketInvalid;
#endif
}


//-----------------------------------------------------------------------------
// Purpose: Process any callbacks we may have
//-----------------------------------------------------------------------------
void CSteam3Client::RunFrame()
{
#if !defined( NO_STEAM )
	VPROF_BUDGET( "CSteam3Client::RunFrame", VPROF_BUDGETGROUP_STEAM );
	SteamAPI_RunCallbacks();
#endif
}


#if !defined(NO_STEAM)
//-----------------------------------------------------------------------------
// Purpose: Disconnect the user from their current server
//-----------------------------------------------------------------------------
void CSteam3Client::OnClientGameServerDeny( ClientGameServerDeny_t *pClientGameServerDeny )
{
	if ( pClientGameServerDeny->m_uAppID == GetSteamAppID() )
	{
		const char *pszReason = "Unknown";
		switch ( pClientGameServerDeny->m_uReason )
		{
			case ( k_EDenyInvalidVersion ) : pszReason = "Invalid version"; break;
			case ( k_EDenyGeneric ) : pszReason = "Kicked"; break;
			case ( k_EDenyNotLoggedOn ) : pszReason = "Not logged on"; break;
			case ( k_EDenyNoLicense ) : pszReason = "No license"; break;
			case ( k_EDenyCheater ) : pszReason = "VAC banned "; break;
			case ( k_EDenyLoggedInElseWhere ) : pszReason = "Dropped from server"; break;
			case ( k_EDenyUnknownText ) : pszReason = "Unknown"; break;
			case ( k_EDenyIncompatibleAnticheat ) : pszReason = "Incompatible Anti Cheat"; break;
			case ( k_EDenyMemoryCorruption ) : pszReason = "Memory corruption"; break;
			case ( k_EDenyIncompatibleSoftware ) : pszReason = "Incompatible software"; break;
			case ( k_EDenySteamConnectionLost ) : pszReason = "Steam connection lost"; break;
			case ( k_EDenySteamConnectionError ) : pszReason = "Steam connection error"; break;
			case ( k_EDenySteamResponseTimedOut ) : pszReason = "Response timed out"; break;
			case ( k_EDenySteamValidationStalled ) : pszReason = "Verification failed"; break;
		}

		Warning( "Disconnect: %s\n", pszReason );

		Host_Disconnect( true );
	}
	
}


extern ConVar	password;
//-----------------------------------------------------------------------------
// Purpose: Disconnect the user from their current server
//-----------------------------------------------------------------------------
void CSteam3Client::OnGameServerChangeRequested( GameServerChangeRequested_t *pGameServerChangeRequested )
{
	password.SetValue( pGameServerChangeRequested->m_rgchPassword );
	Msg( "Connecting to %s\n", pGameServerChangeRequested->m_rgchServer );
	Cbuf_AddText( va( "connect %s steam\n", pGameServerChangeRequested->m_rgchServer ) );
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CSteam3Client::OnGameOverlayActivated( GameOverlayActivated_t *pGameOverlayActivated )
{
#ifndef SWDS
	if ( Host_IsSinglePlayerGame() )
	{
		if ( !EngineVGui()->IsGameUIVisible() && 
			!EngineVGui()->IsConsoleVisible() )
		{
			if ( pGameOverlayActivated->m_bActive )
				Cbuf_AddText( "setpause" );
			else
				Cbuf_AddText( "unpause" );
		}
	}
#endif
}

extern void UpdateNameFromSteamID( IConVar *pConVar, CSteamID *pSteamID );

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CSteam3Client::OnPersonaUpdated( PersonaStateChange_t *pPersonaStateChanged )
{
	if ( pPersonaStateChanged->m_nChangeFlags & k_EPersonaChangeName )
	{
		if ( SteamUtils() && SteamFriends() && SteamUser() )
		{
			CSteamID steamID = SteamUser()->GetSteamID();
			IConVar *pConVar = g_pCVar->FindVar( "name" );
			UpdateNameFromSteamID( pConVar, &steamID );
		}
	}
}


//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CSteam3Client::OnLowBattery( LowBatteryPower_t *pLowBat )
{
	// on the 9min, 5 min and 1 min warnings tell the engine to fire off a save
	switch(  pLowBat->m_nMinutesBatteryLeft )
	{
	case 9: 
	case 5: 
	case 1: 
		Cbuf_AddText( "save LowBattery_AutoSave" );
		break;

	default:
		break;
	}
}

#endif