Counter Strike : Global Offensive Source Code
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.

132 lines
3.8 KiB

  1. #include "cbase.h"
  2. #include "clientsteamcontext.h"
  3. // memdbgon must be the last include file in a .cpp file!!!
  4. #include "tier0/memdbgon.h"
  5. static CClientSteamContext g_ClientSteamContext;
  6. CClientSteamContext &ClientSteamContext()
  7. {
  8. return g_ClientSteamContext;
  9. }
  10. CSteamAPIContext *steamapicontext = &g_ClientSteamContext;
  11. //-----------------------------------------------------------------------------
  12. CClientSteamContext::CClientSteamContext()
  13. #if !defined(NO_STEAM)
  14. :
  15. m_CallbackSteamServersDisconnected( this, &CClientSteamContext::OnSteamServersDisconnected ),
  16. m_CallbackSteamServerConnectFailure( this, &CClientSteamContext::OnSteamServerConnectFailure ),
  17. m_CallbackSteamServersConnected( this, &CClientSteamContext::OnSteamServersConnected )
  18. #endif
  19. {
  20. m_bActive = false;
  21. m_bLoggedOn = false;
  22. m_nAppID = 0;
  23. }
  24. //-----------------------------------------------------------------------------
  25. CClientSteamContext::~CClientSteamContext()
  26. {
  27. }
  28. //-----------------------------------------------------------------------------
  29. // Purpose: Unload the steam3 engine
  30. //-----------------------------------------------------------------------------
  31. void CClientSteamContext::Shutdown()
  32. {
  33. if ( !m_bActive )
  34. return;
  35. m_bActive = false;
  36. m_bLoggedOn = false;
  37. #if !defined( NO_STEAM )
  38. Clear(); // Steam API context shutdown
  39. #endif
  40. }
  41. //-----------------------------------------------------------------------------
  42. // Purpose: Initialize the steam3 connection
  43. //-----------------------------------------------------------------------------
  44. void CClientSteamContext::Activate()
  45. {
  46. if ( m_bActive )
  47. return;
  48. m_bActive = true;
  49. #if !defined( NO_STEAM )
  50. #ifndef _PS3
  51. SteamAPI_InitSafe(); // ignore failure, that will fall out later when they don't get a valid logon cookie
  52. SteamAPI_SetTryCatchCallbacks( false ); // We don't use exceptions, so tell steam not to use try/catch in callback handlers
  53. #endif
  54. Init(); // Steam API context init
  55. UpdateLoggedOnState();
  56. Msg( "CClientSteamContext logged on = %d\n", m_bLoggedOn );
  57. #endif
  58. }
  59. void CClientSteamContext::UpdateLoggedOnState()
  60. {
  61. bool bPreviousLoggedOn = m_bLoggedOn;
  62. m_bLoggedOn = ( SteamUser() && SteamUtils() && SteamUser()->BLoggedOn() );
  63. if ( !bPreviousLoggedOn && m_bLoggedOn )
  64. {
  65. // update Steam info
  66. m_SteamIDLocalPlayer = SteamUser()->GetSteamID();
  67. m_nUniverse = SteamUtils()->GetConnectedUniverse();
  68. m_nAppID = SteamUtils()->GetAppID();
  69. }
  70. if ( bPreviousLoggedOn != m_bLoggedOn )
  71. {
  72. // Notify any listeners of the change in logged on state
  73. SteamLoggedOnChange_t loggedOnChange;
  74. loggedOnChange.bPreviousLoggedOn = bPreviousLoggedOn;
  75. loggedOnChange.bLoggedOn = m_bLoggedOn;
  76. InvokeCallbacks( loggedOnChange );
  77. }
  78. }
  79. #if !defined(NO_STEAM)
  80. void CClientSteamContext::OnSteamServersDisconnected( SteamServersDisconnected_t *pDisconnected )
  81. {
  82. UpdateLoggedOnState();
  83. Msg( "CClientSteamContext OnSteamServersDisconnected logged on = %d\n", m_bLoggedOn );
  84. }
  85. void CClientSteamContext::OnSteamServerConnectFailure( SteamServerConnectFailure_t *pConnectFailure )
  86. {
  87. UpdateLoggedOnState();
  88. Msg( "CClientSteamContext OnSteamServerConnectFailure logged on = %d\n", m_bLoggedOn );
  89. }
  90. void CClientSteamContext::OnSteamServersConnected( SteamServersConnected_t *pConnected )
  91. {
  92. UpdateLoggedOnState();
  93. Msg( "CClientSteamContext OnSteamServersConnected logged on = %d\n", m_bLoggedOn );
  94. }
  95. #endif // !defined(NO_STEAM)
  96. void CClientSteamContext::InstallCallback( CUtlDelegate< void ( const SteamLoggedOnChange_t & ) > delegate )
  97. {
  98. m_LoggedOnCallbacks.AddToTail( delegate );
  99. }
  100. void CClientSteamContext::RemoveCallback( CUtlDelegate< void ( const SteamLoggedOnChange_t & ) > delegate )
  101. {
  102. m_LoggedOnCallbacks.FindAndRemove( delegate );
  103. }
  104. void CClientSteamContext::InvokeCallbacks( const SteamLoggedOnChange_t &loggedOnStatus )
  105. {
  106. for ( int i = 0; i < m_LoggedOnCallbacks.Count(); ++i )
  107. {
  108. m_LoggedOnCallbacks[i]( loggedOnStatus );
  109. }
  110. }