Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

182 lines
5.4 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. #include "cbase.h"
  3. #include "clientsteamcontext.h"
  4. // memdbgon must be the last include file in a .cpp file!!!
  5. #include "tier0/memdbgon.h"
  6. static CClientSteamContext g_ClientSteamContext;
  7. CClientSteamContext &ClientSteamContext()
  8. {
  9. return g_ClientSteamContext;
  10. }
  11. CSteamAPIContext *steamapicontext = &g_ClientSteamContext;
  12. //-----------------------------------------------------------------------------
  13. CClientSteamContext::CClientSteamContext()
  14. #if !defined(NO_STEAM)
  15. :
  16. m_CallbackSteamServersDisconnected( this, &CClientSteamContext::OnSteamServersDisconnected ),
  17. m_CallbackSteamServerConnectFailure( this, &CClientSteamContext::OnSteamServerConnectFailure ),
  18. m_CallbackSteamServersConnected( this, &CClientSteamContext::OnSteamServersConnected )
  19. #ifdef TF_CLIENT_DLL
  20. , m_GameJoinRequested( this, &CClientSteamContext::OnGameJoinRequested )
  21. #endif // TF_CLIENT_DLL
  22. #endif
  23. {
  24. m_bActive = false;
  25. m_bLoggedOn = false;
  26. m_nAppID = 0;
  27. }
  28. //-----------------------------------------------------------------------------
  29. CClientSteamContext::~CClientSteamContext()
  30. {
  31. }
  32. //-----------------------------------------------------------------------------
  33. // Purpose: Unload the steam3 engine
  34. //-----------------------------------------------------------------------------
  35. void CClientSteamContext::Shutdown()
  36. {
  37. if ( !m_bActive )
  38. return;
  39. m_bActive = false;
  40. m_bLoggedOn = false;
  41. #if !defined( NO_STEAM )
  42. Clear(); // Steam API context shutdown
  43. #endif
  44. }
  45. //-----------------------------------------------------------------------------
  46. // Purpose: Initialize the steam3 connection
  47. //-----------------------------------------------------------------------------
  48. void CClientSteamContext::Activate()
  49. {
  50. if ( m_bActive )
  51. return;
  52. m_bActive = true;
  53. #if !defined( NO_STEAM )
  54. SteamAPI_InitSafe(); // ignore failure, that will fall out later when they don't get a valid logon cookie
  55. SteamAPI_SetTryCatchCallbacks( false ); // We don't use exceptions, so tell steam not to use try/catch in callback handlers
  56. Init(); // Steam API context init
  57. UpdateLoggedOnState();
  58. Msg( "CClientSteamContext logged on = %d\n", m_bLoggedOn );
  59. #endif
  60. }
  61. void CClientSteamContext::UpdateLoggedOnState()
  62. {
  63. bool bPreviousLoggedOn = m_bLoggedOn;
  64. m_bLoggedOn = ( SteamUser() && SteamUtils() && SteamUser()->BLoggedOn() );
  65. if ( !bPreviousLoggedOn && m_bLoggedOn )
  66. {
  67. // update Steam info
  68. m_SteamIDLocalPlayer = SteamUser()->GetSteamID();
  69. m_nUniverse = SteamUtils()->GetConnectedUniverse();
  70. m_nAppID = SteamUtils()->GetAppID();
  71. }
  72. if ( bPreviousLoggedOn != m_bLoggedOn )
  73. {
  74. // Notify any listeners of the change in logged on state
  75. SteamLoggedOnChange_t loggedOnChange;
  76. loggedOnChange.bPreviousLoggedOn = bPreviousLoggedOn;
  77. loggedOnChange.bLoggedOn = m_bLoggedOn;
  78. InvokeCallbacks( loggedOnChange );
  79. }
  80. }
  81. #if !defined(NO_STEAM)
  82. void CClientSteamContext::OnSteamServersDisconnected( SteamServersDisconnected_t *pDisconnected )
  83. {
  84. UpdateLoggedOnState();
  85. Msg( "CClientSteamContext OnSteamServersDisconnected logged on = %d\n", m_bLoggedOn );
  86. }
  87. void CClientSteamContext::OnSteamServerConnectFailure( SteamServerConnectFailure_t *pConnectFailure )
  88. {
  89. UpdateLoggedOnState();
  90. Msg( "CClientSteamContext OnSteamServerConnectFailure logged on = %d\n", m_bLoggedOn );
  91. }
  92. void CClientSteamContext::OnSteamServersConnected( SteamServersConnected_t *pConnected )
  93. {
  94. UpdateLoggedOnState();
  95. Msg( "CClientSteamContext OnSteamServersConnected logged on = %d\n", m_bLoggedOn );
  96. }
  97. #ifdef TF_CLIENT_DLL
  98. void CClientSteamContext::OnGameJoinRequested( GameRichPresenceJoinRequested_t *pCallback )
  99. {
  100. if ( pCallback && pCallback->m_rgchConnect && ( pCallback->m_rgchConnect[0] == '+' ) )
  101. {
  102. char const *szConCommand = pCallback->m_rgchConnect + 1;
  103. //
  104. // Work around Steam Overlay bug that it doesn't replace %20 characters
  105. //
  106. CFmtStr fmtCommand;
  107. if ( StringHasPrefix( szConCommand, "tf_econ_item_preview%20" ) )
  108. {
  109. fmtCommand.AppendFormat( "%s", szConCommand );
  110. while ( char *pszReplace = strstr( fmtCommand.Access(), "%20" ) )
  111. {
  112. *pszReplace = ' ';
  113. Q_memmove( pszReplace + 1, pszReplace + 3, Q_strlen( pszReplace + 3 ) + 1 );
  114. }
  115. szConCommand = fmtCommand.Access();
  116. }
  117. //
  118. // End of Steam Overlay bug workaround
  119. //
  120. if ( char const *szItemId = StringAfterPrefix( szConCommand, "tf_econ_item_preview " ) )
  121. {
  122. Msg( "CClientSteamContext OnGameJoinRequested tf_econ_item_preview" );
  123. bool bItemIdValid = ( pCallback->m_steamIDFriend.GetAccountID() == ~0u );
  124. while ( *szItemId )
  125. {
  126. if ( ( ( *szItemId >= '0' ) && ( *szItemId <= '9' ) ) ||
  127. ( ( *szItemId >= 'A' ) && ( *szItemId <= 'S' ) ) )
  128. ++szItemId; // support new encoding for owner steamid and assetid
  129. else
  130. {
  131. bItemIdValid = false;
  132. break;
  133. }
  134. }
  135. if ( bItemIdValid )
  136. {
  137. engine->ClientCmd( szConCommand );
  138. }
  139. }
  140. }
  141. }
  142. #endif // TF_CLIENT_DLL
  143. #endif // !defined(NO_STEAM)
  144. void CClientSteamContext::InstallCallback( CUtlDelegate< void ( const SteamLoggedOnChange_t & ) > delegate )
  145. {
  146. m_LoggedOnCallbacks.AddToTail( delegate );
  147. }
  148. void CClientSteamContext::RemoveCallback( CUtlDelegate< void ( const SteamLoggedOnChange_t & ) > delegate )
  149. {
  150. m_LoggedOnCallbacks.FindAndRemove( delegate );
  151. }
  152. void CClientSteamContext::InvokeCallbacks( const SteamLoggedOnChange_t &loggedOnStatus )
  153. {
  154. for ( int i = 0; i < m_LoggedOnCallbacks.Count(); ++i )
  155. {
  156. m_LoggedOnCallbacks[i]( loggedOnStatus );
  157. }
  158. }