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.

182 lines
5.2 KiB

  1. //========= Copyright � 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. //=============================================================================//
  6. #include "cbase.h"
  7. #include "usermessages.h"
  8. #include <bitbuf.h>
  9. // memdbgon must be the last include file in a .cpp file!!!
  10. #include "tier0/memdbgon.h"
  11. #include "tier0/microprofiler.h"
  12. static ConVar cl_show_usermessage( "cl_show_usermessage", "0", 0, "Shows the incoming user messages for this client and dumps them out the type and size of the messages to the console. Setting this to 2 will display message contents as well" ); // filter incoming voice data
  13. //-----------------------------------------------------------------------------
  14. IUserMessageBinder::~IUserMessageBinder()
  15. {
  16. }
  17. //-----------------------------------------------------------------------------
  18. CUserMessages::CUserMessages()
  19. {
  20. SetDefLessFunc( m_UserMessageBinderMap );
  21. }
  22. //-----------------------------------------------------------------------------
  23. CUserMessages::~CUserMessages()
  24. {
  25. m_UserMessageBinderMap.Purge();
  26. }
  27. // we might wanna move this definition here::
  28. // template< int msgType, typename PB_OBJECT_TYPE, int32 nExpectedPassthroughInReplay >
  29. // virtual ::google::protobuf::Message * CUserMessageBinder::BindParams<msgType, PB_OBJECT_TYPE, nExpectedPassthroughInReplay >::Parse( int32 nPassthroughFlags, const void *msg, int size )
  30. //-----------------------------------------------------------------------------
  31. bool CUserMessages::DispatchUserMessage( int msg_type, int32 nPassthroughFlags, int size, const void *msg )
  32. {
  33. UserMessageHandlerMap_t::IndexType_t index = m_UserMessageBinderMap.Find( msg_type );
  34. if ( index == UserMessageHandlerMap_t::InvalidIndex() )
  35. {
  36. DevMsg( "CUserMessages::DispatchUserMessage: Unknown msg type %i\n", msg_type );
  37. return true;
  38. }
  39. int nSlot = GET_ACTIVE_SPLITSCREEN_SLOT();
  40. if ( m_UserMessageBinderMap.Element( index )[ nSlot ].Count() == 0 )
  41. {
  42. // not hooking a usermessage is acceptable, pretend we parsed it
  43. return true;
  44. }
  45. IUserMessageBinder *pHandler = m_UserMessageBinderMap.Element( index )[ nSlot ][0];
  46. if ( !pHandler )
  47. {
  48. // not hooking a usermessage is acceptable, pretend we parsed it
  49. return true;
  50. }
  51. bool bSilentIgnore = false;
  52. ::google::protobuf::Message *pMsg = pHandler->Parse( nPassthroughFlags, msg, size, bSilentIgnore );
  53. if ( bSilentIgnore )
  54. {
  55. if ( pMsg )
  56. delete pMsg;
  57. //DevMsg( "CUserMessages::DispatchUserMessage: Silently ignoring alt-timeline msg type %i\n", msg_type );
  58. return true;
  59. }
  60. if ( !pMsg )
  61. {
  62. DevMsg( "CUserMessages::DispatchUserMessage: Parse error msg type=%i\n", msg_type );
  63. Assert( 0 );
  64. return false;
  65. }
  66. //handle logging to the console if this is enabled
  67. if ( cl_show_usermessage.GetBool() )
  68. {
  69. Msg("DispatchUserMessage - %s(%d) bytes: %d\n", pMsg->GetTypeName().c_str(), msg_type, pMsg->ByteSize() );
  70. //handle message content display if they have it set to a value > 1
  71. if( cl_show_usermessage.GetInt() > 1 )
  72. Msg("%s", pMsg->DebugString().c_str() );
  73. }
  74. bool result = true;
  75. FOR_EACH_VEC( m_UserMessageBinderMap.Element( index )[ nSlot ], i )
  76. {
  77. IUserMessageBinder *h = m_UserMessageBinderMap.Element( index )[ nSlot ][i];
  78. if ( h )
  79. {
  80. result = h->Invoke( pMsg );
  81. }
  82. if ( !result )
  83. {
  84. break;
  85. }
  86. }
  87. delete pMsg;
  88. return result;
  89. }
  90. //-----------------------------------------------------------------------------
  91. void CUserMessages::BindMessage( IUserMessageBinder *pMessageBinder )
  92. {
  93. if ( !pMessageBinder )
  94. {
  95. return;
  96. }
  97. int message = pMessageBinder->GetType();
  98. UserMessageHandlerMap_t::IndexType_t index = m_UserMessageBinderMap.Find( message );
  99. if ( index == UserMessageHandlerMap_t::InvalidIndex() )
  100. {
  101. index = m_UserMessageBinderMap.Insert( message );
  102. m_UserMessageBinderMap.Element( index ).SetCount( MAX_SPLITSCREEN_PLAYERS );
  103. }
  104. ASSERT_LOCAL_PLAYER_RESOLVABLE();
  105. #ifdef DEBUG
  106. FOR_EACH_VEC( m_UserMessageBinderMap.Element( index )[ GET_ACTIVE_SPLITSCREEN_SLOT() ], j )
  107. {
  108. if( m_UserMessageBinderMap.Element( index )[ GET_ACTIVE_SPLITSCREEN_SLOT() ][ j ]->GetAbstractDelegate().IsEqual( pMessageBinder->GetAbstractDelegate() ) )
  109. {
  110. DevMsg( "CUserMessages::BindMessage called duplicate %d!!!\n", pMessageBinder->GetType() );
  111. }
  112. }
  113. #endif
  114. m_UserMessageBinderMap.Element( index )[ GET_ACTIVE_SPLITSCREEN_SLOT() ].AddToTail( pMessageBinder );
  115. }
  116. //-----------------------------------------------------------------------------
  117. bool CUserMessages::UnbindMessage( IUserMessageBinder *pMessageBinder )
  118. {
  119. if ( !pMessageBinder )
  120. {
  121. return true;
  122. }
  123. int message = pMessageBinder->GetType();
  124. UserMessageHandlerMap_t::IndexType_t index = m_UserMessageBinderMap.Find( message );
  125. if ( index == UserMessageHandlerMap_t::InvalidIndex() )
  126. {
  127. return false;
  128. }
  129. for ( int split = 0; split < MAX_SPLITSCREEN_PLAYERS; ++split )
  130. {
  131. FOR_EACH_VEC( m_UserMessageBinderMap.Element( index )[ split ], i )
  132. {
  133. if ( m_UserMessageBinderMap.Element( index )[ split ][i] == pMessageBinder )
  134. {
  135. m_UserMessageBinderMap.Element( index )[ split ].Remove(i);
  136. return true;
  137. }
  138. }
  139. }
  140. return false;
  141. }
  142. //-----------------------------------------------------------------------------
  143. // Singleton
  144. CUserMessages *UserMessages()
  145. {
  146. static CUserMessages g_UserMessages;
  147. return &g_UserMessages;
  148. }