summaryrefslogtreecommitdiff
path: root/gcsdk/accountdetails.cpp
blob: 507b9d1b3b0046dc2fbc58febb6598f793f6996f (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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: Holds the CAccountDetails class.
//
//=============================================================================

#include "stdafx.h"
#include "accountdetails.h"
#include "rtime.h"
#include "gcsdk_gcmessages.pb.h"

#include "memdbgon.h" // needs to be the last include in the file

namespace GCSDK
{
GCConVar cv_account_details_cache_time( "account_details_cache_time", "600" );
GCConVar cv_account_details_failure_cache_time( "account_details_failure_cache_time", "10" );
GCConVar account_details_timeout( "account_details_timeout", "10" );
GCConVar cv_persona_name_cache_time( "persona_name_cache_time", "60" );
GCConVar cv_persona_name_failure_cache_time( "persona_name_failure_cache_time", "10" );
GCConVar cv_persona_name_batch_size( "persona_name_batch_size", "100" );
GCConVar persona_name_timeout( "persona_name_timeout", "10" );

const char *kszAccountDetailsKey = "AccountDetails-v001";

//-----------------------------------------------------------------------------
// Purpose: Constructor
//-----------------------------------------------------------------------------
CAccountDetails::CAccountDetails()
:	m_rtimeCached( CRTime::RTime32TimeCur() ),
	m_bValid( false ),
	m_bPublicProfile( false ),
	m_bVacBanned( false ),
	m_bCyberCafe( false ),
	m_bSchoolAccount( false ),
	m_bFreeTrialAccount( false ),
	m_bSubscribed( false ),
	m_bLowViolence( false ),
	m_bLimited( false ),
	m_bAccountLocked( false ),
	m_bCommunityBanned( false ),
	m_bTradeBanned( false ),
	m_bIsSteamGuardEnabled( false ),
	m_bIsPhoneVerified( false ),
	m_bIsTwoFactorAuthEnabled( false ),
	m_bIsPhoneIdentifying( false ),
	m_unPackage( 0 ),
	m_rtimeVACBanEnd( 0 ),
	m_unSteamLevel( 0 ),
	m_unFriendCount( 0 ),
	m_rtimeAccountCreated( 0 ),
	m_rtimeTwoFactorEnabled( 0 ),
	m_rtimePhoneVerified( 0 ),
	m_unPhoneID( 0 )
{
}

//-----------------------------------------------------------------------------
// Purpose: Initialize a fresh CAccountDetails with data from Steam
//-----------------------------------------------------------------------------
void CAccountDetails::Init( CGCSystemMsg_GetAccountDetails_Response &msgResponse )
{
	m_bValid = true;
	m_sAccountName = msgResponse.account_name().c_str();
	m_bPublicProfile = msgResponse.is_profile_public();
	m_bPublicInventory = msgResponse.is_inventory_public();
	m_bVacBanned = msgResponse.is_vac_banned(); 
	m_bCyberCafe = msgResponse.is_cyber_cafe(); 
	m_bSchoolAccount = msgResponse.is_school_account(); 
	m_bFreeTrialAccount = msgResponse.is_free_trial_account(); 
	m_bSubscribed = msgResponse.is_subscribed(); 
	m_bLowViolence = msgResponse.is_low_violence(); 
	m_bLimited = msgResponse.is_limited(); 
	m_bAccountLocked = msgResponse.is_account_locked_down();
	m_bCommunityBanned = msgResponse.is_community_banned();
	m_bTradeBanned = msgResponse.is_trade_banned();
	m_unPackage = msgResponse.package();
	m_rtimeVACBanEnd = msgResponse.suspension_end_time();
	m_sCurrency = msgResponse.currency().c_str();
	m_unSteamLevel = msgResponse.steam_level();
	m_unFriendCount = msgResponse.friend_count();
	m_rtimeAccountCreated = msgResponse.account_creation_time();
	m_bIsSteamGuardEnabled = msgResponse.is_steamguard_enabled();
	m_bIsPhoneVerified = msgResponse.is_phone_verified();
	m_bIsTwoFactorAuthEnabled = msgResponse.is_two_factor_auth_enabled();
	m_bIsPhoneIdentifying = msgResponse.is_phone_identifying();
	m_rtimeTwoFactorEnabled = msgResponse.two_factor_enabled_time();
	m_rtimePhoneVerified = msgResponse.phone_verification_time();
	m_unPhoneID = msgResponse.phone_id();
}


//-----------------------------------------------------------------------------
// Purpose: Returns true if it's time to remove this entry from the cache
//-----------------------------------------------------------------------------
bool CAccountDetails::BIsExpired() const
{
	int nCacheSeconds = BIsValid() ? cv_account_details_cache_time.GetInt() : cv_account_details_failure_cache_time.GetInt();

	return m_rtimeCached + nCacheSeconds < CRTime::RTime32TimeCur();
}


//-----------------------------------------------------------------------------
// Purpose: Reverts this to an invalid record
//-----------------------------------------------------------------------------
void CAccountDetails::Reset()
{
	m_bValid = false;
	m_rtimeCached = CRTime::RTime32TimeCur();
}


#ifdef DBGFLAG_VALIDATE
//-----------------------------------------------------------------------------
// Purpose: Claims all the memory for the AccountDetails object
//-----------------------------------------------------------------------------
void CAccountDetails::Validate( CValidator &validator, const char *pchName )
{
	VALIDATE_SCOPE();
	ValidateObj( m_sAccountName );
}
#endif // DBGFLAG_VALIDATE


//-----------------------------------------------------------------------------
// Purpose: Sends a message to Steam to get a CAccountDetails object
//-----------------------------------------------------------------------------
class CGCJobSendGetAccountDetailsRequest : public CGCJob
{
	CAccountDetailsManager *m_pManager;
	CSteamID m_SteamID;

public:
	CGCJobSendGetAccountDetailsRequest( CGCBase *pGC, CAccountDetailsManager *pManager, const CSteamID &steamID )
		: CGCJob( pGC ), m_pManager( pManager ), m_SteamID( steamID ) {}
	virtual bool BYieldingRunGCJob()
	{
		// Yield immediately to be sure that the calling job gets in the wakeup list
		BYield();

		// These requests should come back very quickly, so if they don't we shouldn't wait very long
		// jamming up the system
		SetJobTimeout( account_details_timeout.GetInt() );

		// Get an empty account details object
		CAccountDetails *pAccount = m_pManager->m_hashAccountDetailsCache.PvRecordFind( m_SteamID.GetAccountID() );
		if ( NULL == pAccount )
		{
			pAccount = m_pManager->m_hashAccountDetailsCache.PvRecordInsert( m_SteamID.GetAccountID() );
		}
		else
		{
			// If the record isn't expired, why is it there?
			Assert( pAccount->BIsExpired() );
			pAccount->Reset();
		}		

		CProtoBufMsg< CGCSystemMsg_GetAccountDetails > msgReqeust( k_EGCMsgGetAccountDetails );
		CProtoBufMsg< CGCSystemMsg_GetAccountDetails_Response > msgReply;
		msgReqeust.Body().set_steamid( m_SteamID.ConvertToUint64() );
		msgReqeust.Body().set_appid( GGCBase()->GetAppID() );
		msgReqeust.ExpectingReply( GJobCur().GetJobID() );

		// try to get the account details at most 2 times
		const int kMaxTries = 2;
		for ( int iTries = 0; iTries < kMaxTries; iTries++ )
		{
			if( !m_pGC->BSendSystemMessage( msgReqeust ) )
			{
				EmitWarning( SPEW_GC, 2, "Unable to send GetAccountDetails system message\n" );
				continue;
			}

			// All of our request messages are identical, so if we get our replies
			// mixed up, it's OK.  Bypass the system used to protect us against
			// mismatched replies.
			ClearFailedToReceivedMsgType( k_EGCMsgGetAccountDetailsResponse );

			// Wait for the reply
			if( !BYieldingWaitForMsg( &msgReply, k_EGCMsgGetAccountDetailsResponse ) )
			{
				EmitWarning( SPEW_GC, 2, "Timeout waiting for GetAccountDetails reply for SteamID %s\n", m_SteamID.Render() );
				continue;
			}

			if ( k_EResultOK != msgReply.GetEResult() )
			{
				EmitInfo( SPEW_GC, 4, 4, "GetAccountDetails request failed with result %d for SteamID %s\n", msgReply.GetEResult(), m_SteamID.Render() );
				break;
			}

			Assert( msgReply.Body().eresult_deprecated() == k_EResultOK );

			// Sanity check the response
			if ( msgReply.Body().has_accountid() && msgReply.Body().accountid() != m_SteamID.GetAccountID() )
			{
				static bool bHasAlerted = false;
				if ( !bHasAlerted )
				{
					GGCBase()->PostAlert( k_EAlertTypeInfo, true, CFmtStr( "GetAccountDetails got a response for account %d, but we were expecting a response for account %s\n", msgReply.Body().accountid(), m_SteamID.Render() ) );
					bHasAlerted = true;
				}

				EmitError( SPEW_GC, "GetAccountDetails got a response for account %d, but we were expecting a response for account %s\n", msgReply.Body().accountid(), m_SteamID.Render() );
				break;
			}

			// All responses should have this
			if ( !msgReply.Body().has_account_name() )
			{
				EmitError( SPEW_GC, "GetAccountDetails got a response with missing fields for SteamID %s\n", m_SteamID.Render() );
				break;
			}

			pAccount->Init( msgReply.Body() );
			m_pManager->CachePersonaName( CSteamID( m_SteamID ), msgReply.Body().persona_name().c_str() );

			// We got a response, so we shouldn't try again
			break;
		}

		m_pManager->WakeWaitingAccountDetailsJobs( m_SteamID );
		return true;
	}
};


//-----------------------------------------------------------------------------
// Purpose: Constructor
//-----------------------------------------------------------------------------
CCachedPersonaName::CCachedPersonaName()
	:	m_rtimeCached( 0 )	// This initialization value is important because it makes it expired on creation, 
							// and the rest of the code will only ask Steam for a name if the cached entry is expired
	,	m_nLoading( 0 )
	,	m_bPreloading( false )
{
}


//-----------------------------------------------------------------------------
// Purpose: Destructor
//-----------------------------------------------------------------------------
CCachedPersonaName::~CCachedPersonaName()
{
	Assert( !BIsLoading() );
}


//-----------------------------------------------------------------------------
// Purpose: Sets the newly retrieved persona name
//-----------------------------------------------------------------------------
void CCachedPersonaName::Init( const char *pchPersonaName )
{
	m_sPersonaName = pchPersonaName;
	m_rtimeCached = CRTime::RTime32TimeCur();
	m_bPreloading = false;
}


//-----------------------------------------------------------------------------
// Purpose: Returns true if it's time to remove this entry from the cache
//-----------------------------------------------------------------------------
bool CCachedPersonaName::BIsExpired() const
{
	int nCacheSeconds = BIsValid() ? cv_persona_name_cache_time.GetInt() : cv_persona_name_failure_cache_time.GetInt();

	return m_rtimeCached + nCacheSeconds < CRTime::RTime32TimeCur();
}


//-----------------------------------------------------------------------------
// Purpose: Returns true if there is an actual cached name
//-----------------------------------------------------------------------------
bool CCachedPersonaName::BIsValid() const
{
	return !m_sPersonaName.IsEmpty();
}


//-----------------------------------------------------------------------------
// Purpose: Reverts this to an invalid record
//-----------------------------------------------------------------------------
void CCachedPersonaName::Reset()
{
	m_sPersonaName.Clear();
	m_rtimeCached = CRTime::RTime32TimeCur();
	m_bPreloading = false;
}


//-----------------------------------------------------------------------------
// Purpose: Gets the cached string
//-----------------------------------------------------------------------------
const char *CCachedPersonaName::GetPersonaName() const
{
	return BIsValid() ? m_sPersonaName.Get() : "[unknown]";
}


//-----------------------------------------------------------------------------
// Purpose: Returns if this name is loading
//-----------------------------------------------------------------------------
bool CCachedPersonaName::BIsLoading() const
{
	return m_nLoading > 0 || m_bPreloading;
}


//-----------------------------------------------------------------------------
// Purpose: Sets that this name has been preloaded
//-----------------------------------------------------------------------------
void CCachedPersonaName::SetPreloading()
{
	m_bPreloading = true;
}


//-----------------------------------------------------------------------------
// Purpose: Sets that a job is waiting for this name to be loaded
//-----------------------------------------------------------------------------
void CCachedPersonaName::AddLoadingRef()
{
	m_nLoading++;
}


//-----------------------------------------------------------------------------
// Purpose: Releases the loading ref
//-----------------------------------------------------------------------------
void CCachedPersonaName::ReleaseLoadingRef()
{
	DbgVerify( --m_nLoading >= 0 );
}


#ifdef DBGFLAG_VALIDATE
//-----------------------------------------------------------------------------
// Purpose: Claims all the memory for the CCachedPersonaName object
//-----------------------------------------------------------------------------
void CCachedPersonaName::Validate( CValidator &validator, const char *pchName )
{
	VALIDATE_SCOPE();
	ValidateObj( m_sPersonaName );
}
#endif // DBGFLAG_VALIDATE


//-----------------------------------------------------------------------------
// Purpose: Sends a message to Steam to get a CAccountDetails object
//-----------------------------------------------------------------------------
class CGCJobSendGetPersonaNamesRequest : public CGCJob
{
	CAccountDetailsManager *m_pManager;
	CUtlVector<CSteamID> m_vecSteamIDs;

public:
	CGCJobSendGetPersonaNamesRequest( CGCBase *pGC, CAccountDetailsManager *pManager, CUtlVector<CSteamID> &vecSteamIDs )
		: CGCJob( pGC ), m_pManager( pManager )
	{
		m_vecSteamIDs.Swap( vecSteamIDs );
	}

	virtual bool BYieldingRunGCJob()
	{
		// Yield immediately to be sure that the calling job gets in the wakeup list
		BYield();

		// These requests should come back very quickly, so if they don't we shouldn't wait very long
		// jamming up the system
		SetJobTimeout( persona_name_timeout.GetInt() );

		CProtoBufMsg< CMsgGCGetPersonaNames > msgReqeust( k_EGCMsgGetPersonaNames );
		msgReqeust.ExpectingReply( GJobCur().GetJobID() );

		FOR_EACH_VEC( m_vecSteamIDs, i )
		{
			msgReqeust.Body().add_steamids( m_vecSteamIDs[i].ConvertToUint64() );
		}
		
		CProtoBufMsg< CMsgGCGetPersonaNames_Response > msgReply;
		if( !m_pGC->BSendSystemMessage( msgReqeust ) ||
			!BYieldingWaitForMsg( &msgReply, k_EGCMsgGetPersonaNamesResponse ) )
		{
			FOR_EACH_VEC( m_vecSteamIDs, i )
			{
				m_pManager->CachePersonaNameFailure( m_vecSteamIDs[i] );
			}

			//if we are shutting down, don't bother reporting this issue, we know we won't be able to get persona names
			if( !GGCBase()->GetIsShuttingDown() )
			{
				EmitWarning( SPEW_GC, 2, "GetPersonaNames request failed for %d IDs:", m_vecSteamIDs.Count() );
				FOR_EACH_VEC( m_vecSteamIDs, nID )
				{
					EmitWarning( SPEW_GC, 2, " %s", m_vecSteamIDs[ nID ].Render() );
				}
				EmitWarning( SPEW_GC, 2, "\n" );
			}
		}
		else
		{
			for ( int i = 0; i < msgReply.Body().succeeded_lookups_size(); i++ )
			{
				const CMsgGCGetPersonaNames_Response_PersonaName &result = msgReply.Body().succeeded_lookups( i );
				m_pManager->CachePersonaName( CSteamID( result.steamid() ), result.persona_name().c_str() );
			}
			for ( int i = 0; i < msgReply.Body().failed_lookup_steamids_size(); i++ )
			{
				m_pManager->CachePersonaNameFailure( CSteamID( msgReply.Body().failed_lookup_steamids( i ) ) );
			}
		}

		FOR_EACH_VEC( m_vecSteamIDs, i )
		{
			m_pManager->WakeWaitingPersonaNameJobs( m_vecSteamIDs[i] );
		}
		return true;
	}
};


//-----------------------------------------------------------------------------
// Purpose: Constructor
//-----------------------------------------------------------------------------
CAccountDetailsManager::CAccountDetailsManager()
	: m_hashAccountDetailsCache( k_nAccountDetailsRunInterval / k_cMicroSecPerShellFrame )
	, m_hashPersonaNameCache( k_nAccountDetailsRunInterval / k_cMicroSecPerShellFrame )
{
	m_hashAccountDetailsCache.Init( k_cAccountDetailsInit, k_cBucketAccountDetails );
	m_hashPersonaNameCache.Init( k_cAccountDetailsInit, k_cBucketAccountDetails );
}


//-----------------------------------------------------------------------------
// Purpose: Work to be done once per frame
//-----------------------------------------------------------------------------
void CAccountDetailsManager::MarkFrame()
{
	m_hashAccountDetailsCache.StartFrameSchedule( true );
	m_hashPersonaNameCache.StartFrameSchedule( true );
	SendBatchedPersonaNamesRequest();
}


//-----------------------------------------------------------------------------
// Purpose: Gets a CAccountDetails object
//-----------------------------------------------------------------------------
CAccountDetails *CAccountDetailsManager::YieldingGetAccountDetails( const CSteamID &steamID, bool bForceReload )
{
	AssertRunningJob();
	if( !steamID.IsValid() || !steamID.BIndividualAccount() )
		return NULL;

	// Check the local cache
	CAccountDetails *pAccountDetails = NULL;
	if ( BFindAccountDetailsInLocalCache( steamID, &pAccountDetails ) )
	{
		if ( pAccountDetails && bForceReload )
		{
			// Clear it, continue with fresh load
			m_hashAccountDetailsCache.Remove( pAccountDetails );
		}
		else
		{
			return pAccountDetails;
		}
	}

	// Not in the local cache, ask Steam
	int iMapIndex = m_mapQueuedAccountDetailsRequests.Find( steamID );
	if ( !m_mapQueuedAccountDetailsRequests.IsValidIndex( iMapIndex ) )
	{
		iMapIndex = m_mapQueuedAccountDetailsRequests.Insert( steamID );
		CGCJobSendGetAccountDetailsRequest *pJob = new CGCJobSendGetAccountDetailsRequest( GGCBase(), this, steamID );
		pJob->StartJob( NULL );
	}

	m_mapQueuedAccountDetailsRequests[iMapIndex].AddToTail( GJobCur().GetJobID() );
	GJobCur().BYieldingWaitForWorkItem();

	// Check again, if it's not there then it's not there
	BFindAccountDetailsInLocalCache( steamID, &pAccountDetails );
	return pAccountDetails;
}


//-----------------------------------------------------------------------------
// Purpose: Finds an AccountDetails object in the local cache. Returns true
//	if it was found, false if it should be checked remotely
//-----------------------------------------------------------------------------
bool CAccountDetailsManager::BFindAccountDetailsInLocalCache( const CSteamID &steamID, CAccountDetails **ppAccount )
{
	CAccountDetails *pAccountLocal = m_hashAccountDetailsCache.PvRecordFind( steamID.GetAccountID() );
	if( NULL == pAccountLocal || pAccountLocal->BIsExpired() )
		return false;

	*ppAccount = pAccountLocal->BIsValid() ? pAccountLocal : NULL;
	return true;
}


//-----------------------------------------------------------------------------
// Purpose: Wakes any jobs waiting on this result
//-----------------------------------------------------------------------------
void CAccountDetailsManager::WakeWaitingAccountDetailsJobs( const CSteamID &steamID )
{
	int iMapIndex = m_mapQueuedAccountDetailsRequests.Find( steamID );
	if ( !m_mapQueuedAccountDetailsRequests.IsValidIndex( iMapIndex ) )
		return; 

	CCopyableUtlVector<JobID_t> &vecJobsWaiting = m_mapQueuedAccountDetailsRequests[iMapIndex];
	FOR_EACH_VEC( vecJobsWaiting, i )
	{
		GGCBase()->GetJobMgr().BRouteWorkItemCompletedDelayed( vecJobsWaiting[i], false );
	}
	m_mapQueuedAccountDetailsRequests.RemoveAt( iMapIndex );
}


//-----------------------------------------------------------------------------
// Purpose: Gets a persona name for a user
//-----------------------------------------------------------------------------
const char *CAccountDetailsManager::YieldingGetPersonaName( const CSteamID &steamID )
{
	VPROF_BUDGET( "CAccountDetailsManager::YieldingGetPersonaName", VPROF_BUDGETGROUP_STEAM );

	AssertRunningJob();

	if( !steamID.IsValid() || !steamID.BIndividualAccount() )
		return "[unknown]";

	// Check the local cache
	CCachedPersonaName *pPersonaName = FindOrCreateCachedPersonaName( steamID );
	if ( !pPersonaName->BIsExpired() )
		return pPersonaName->GetPersonaName(); 

	// Not in the local cache, ask Steam
	pPersonaName->AddLoadingRef();

	// Queue the request and start a lookup job if we have enough pending
	int iMapIndex = m_mapQueuedPersonaNameRequests.Find( steamID );
	if ( !m_mapQueuedPersonaNameRequests.IsValidIndex( iMapIndex ) )
	{
		iMapIndex = m_mapQueuedPersonaNameRequests.Insert( steamID );
		m_vecPendingPersonaNameLookups.AddToTail( steamID );
		if ( m_vecPendingPersonaNameLookups.Count() >= cv_persona_name_batch_size.GetInt() )
		{
			SendBatchedPersonaNamesRequest();
		}
	}

	m_mapQueuedPersonaNameRequests[iMapIndex].AddToTail( GJobCur().GetJobID() );
	GJobCur().BYieldingWaitForWorkItem();

	// At this point we'll either have a persona name or we won't
	pPersonaName->ReleaseLoadingRef();
	return pPersonaName->GetPersonaName();
}


//-----------------------------------------------------------------------------
// Purpose: Let's the system know that we should load the persona name for this
//	user, but does not block on it
//-----------------------------------------------------------------------------
void CAccountDetailsManager::PreloadPersonaName( const CSteamID &steamID )
{
	if( !steamID.IsValid() || !steamID.BIndividualAccount() )
		return;

	// See if we already have it
	CCachedPersonaName *pCachedName = FindOrCreateCachedPersonaName( steamID );
	if ( !pCachedName->BIsExpired() || pCachedName->BIsLoading() )
		return;

	// Queue the request and start a lookup job if we have enough pending
	pCachedName->SetPreloading();
	m_mapQueuedPersonaNameRequests.Insert( steamID );
	m_vecPendingPersonaNameLookups.AddToTail( steamID );
	if ( m_vecPendingPersonaNameLookups.Count() >= cv_persona_name_batch_size.GetInt() )
	{
		SendBatchedPersonaNamesRequest();
	}
}


//-----------------------------------------------------------------------------
// Purpose: Sends a batch of persona name requests
//-----------------------------------------------------------------------------
void CAccountDetailsManager::SendBatchedPersonaNamesRequest()
{
	if ( 0 == m_vecPendingPersonaNameLookups.Count() )
		return;

	// Start the job. This swaps out our buffer with an empty one
	CGCJobSendGetPersonaNamesRequest *pJob = new CGCJobSendGetPersonaNamesRequest( GGCBase(), this, m_vecPendingPersonaNameLookups );
	pJob->StartJob( NULL );
}


//-----------------------------------------------------------------------------
// Purpose: Caches a persona name
//-----------------------------------------------------------------------------
void CAccountDetailsManager::CachePersonaName( const CSteamID &steamID, const char *pchPersonaName )
{
	CCachedPersonaName *pCachedPersonaName = FindOrCreateCachedPersonaName( steamID );
	pCachedPersonaName->Init( pchPersonaName );
}


//-----------------------------------------------------------------------------
// Purpose: Remembers that we failed to cache a persona name
//-----------------------------------------------------------------------------
void CAccountDetailsManager::CachePersonaNameFailure( const CSteamID &steamID )
{
	CCachedPersonaName *pCachedPersonaName = FindOrCreateCachedPersonaName( steamID );
	pCachedPersonaName->Reset();
}


//-----------------------------------------------------------------------------
// Purpose: Purges a specific persona name from the cache
//-----------------------------------------------------------------------------
void CAccountDetailsManager::ClearCachedPersonaName( const CSteamID &steamID )
{
	CCachedPersonaName *pPersonaNameLocal = m_hashPersonaNameCache.PvRecordFind( steamID.GetAccountID() );
	if( NULL != pPersonaNameLocal && !pPersonaNameLocal->BIsLoading() )
	{
		m_hashPersonaNameCache.Remove( pPersonaNameLocal );
	}
}


//-----------------------------------------------------------------------------
// Purpose: Gets a CCachedPersonaName record
//-----------------------------------------------------------------------------
CCachedPersonaName *CAccountDetailsManager::FindOrCreateCachedPersonaName( const CSteamID &steamID )
{
	CCachedPersonaName *pPersonaName = m_hashPersonaNameCache.PvRecordFind( steamID.GetAccountID() );
	if ( NULL != pPersonaName )
		return pPersonaName;
	else
		return m_hashPersonaNameCache.PvRecordInsert( steamID.GetAccountID() );
}


//-----------------------------------------------------------------------------
// Purpose: Wakes any jobs waiting on this result
//-----------------------------------------------------------------------------
void CAccountDetailsManager::WakeWaitingPersonaNameJobs( const CSteamID &steamID )
{
	int iMapIndex = m_mapQueuedPersonaNameRequests.Find( steamID );
	if ( !m_mapQueuedPersonaNameRequests.IsValidIndex( iMapIndex ) )
		return; 

	CCopyableUtlVector<JobID_t> &vecJobsWaiting = m_mapQueuedPersonaNameRequests[iMapIndex];
	FOR_EACH_VEC( vecJobsWaiting, i )
	{
		GGCBase()->GetJobMgr().BRouteWorkItemCompletedDelayed( vecJobsWaiting[i], false );
	}
	m_mapQueuedPersonaNameRequests.RemoveAt( iMapIndex );
}


//-----------------------------------------------------------------------------
// Purpose: Purges old data from the cache. Returns true if there is more
//	work to do
//-----------------------------------------------------------------------------
bool CAccountDetailsManager::BExpireRecords( CLimitTimer &limitTimer )
{
	VPROF_BUDGET( "Expire account details", VPROF_BUDGETGROUP_STEAM );

	for ( CAccountDetails *pDetails = m_hashAccountDetailsCache.PvRecordRun(); NULL != pDetails; pDetails = m_hashAccountDetailsCache.PvRecordRun() )
	{
		if ( pDetails->BIsExpired() )
		{
			m_hashAccountDetailsCache.Remove( pDetails );
		}

		if ( limitTimer.BLimitReached() )
			return true;
	}

	for ( CCachedPersonaName *pPersonaName = m_hashPersonaNameCache.PvRecordRun(); NULL != pPersonaName; pPersonaName = m_hashPersonaNameCache.PvRecordRun() )
	{
		if ( pPersonaName->BIsExpired() && !pPersonaName->BIsLoading() )
		{
			m_hashPersonaNameCache.Remove( pPersonaName );
		}

		if ( limitTimer.BLimitReached() )
			return true;
	}

	return false;
}


//-----------------------------------------------------------------------------
// Purpose: Prints status
//-----------------------------------------------------------------------------
void CAccountDetailsManager::Dump() const
{
	int nJobsWaiting = 0;
	FOR_EACH_MAP_FAST( m_mapQueuedAccountDetailsRequests, iMap )
	{
		nJobsWaiting += m_mapQueuedAccountDetailsRequests[iMap].Count();
	}

	EmitInfo( SPEW_CONSOLE, SPEW_ALWAYS, LOG_ALWAYS, "\tAccount Details: %d cached, %d lookups in flight, %d jobs waiting\n", m_hashAccountDetailsCache.Count(), m_mapQueuedAccountDetailsRequests.Count(), nJobsWaiting );

	nJobsWaiting = 0;
	FOR_EACH_MAP_FAST( m_mapQueuedPersonaNameRequests, iMap )
	{
		nJobsWaiting += m_mapQueuedPersonaNameRequests[iMap].Count();
	}

	EmitInfo( SPEW_CONSOLE, SPEW_ALWAYS, LOG_ALWAYS, "\tPersona Names: %d cached, %d lookups in flight, %d jobs waiting\n", m_hashPersonaNameCache.Count(), m_mapQueuedPersonaNameRequests.Count(), nJobsWaiting );
}

} // namespace GCSDK