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.

191 lines
5.2 KiB

  1. //========= Copyright 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. void RegisterUserMessages( void );
  12. //-----------------------------------------------------------------------------
  13. // Purpose: Force registration on .dll load
  14. // FIXME: Should this be a client/server system?
  15. //-----------------------------------------------------------------------------
  16. CUserMessages::CUserMessages()
  17. {
  18. // Game specific registration function;
  19. RegisterUserMessages();
  20. }
  21. CUserMessages::~CUserMessages()
  22. {
  23. int c = m_UserMessages.Count();
  24. for ( int i = 0; i < c; ++i )
  25. {
  26. delete m_UserMessages[ i ];
  27. }
  28. m_UserMessages.RemoveAll();
  29. }
  30. //-----------------------------------------------------------------------------
  31. // Purpose:
  32. // Input : *name -
  33. // Output : int
  34. //-----------------------------------------------------------------------------
  35. int CUserMessages::LookupUserMessage( const char *name )
  36. {
  37. int idx = m_UserMessages.Find( name );
  38. if ( idx == m_UserMessages.InvalidIndex() )
  39. {
  40. return -1;
  41. }
  42. return idx;
  43. }
  44. //-----------------------------------------------------------------------------
  45. // Purpose:
  46. // Input : index -
  47. // Output : int
  48. //-----------------------------------------------------------------------------
  49. int CUserMessages::GetUserMessageSize( int index )
  50. {
  51. if ( index < 0 || index >= (int)m_UserMessages.Count() )
  52. {
  53. Error( "CUserMessages::GetUserMessageSize( %i ) out of range!!!\n", index );
  54. }
  55. CUserMessage *e = m_UserMessages[ index ];
  56. return e->size;
  57. }
  58. //-----------------------------------------------------------------------------
  59. // Purpose:
  60. // Input : index -
  61. // Output : char const
  62. //-----------------------------------------------------------------------------
  63. const char *CUserMessages::GetUserMessageName( int index )
  64. {
  65. if ( index < 0 || index >= (int)m_UserMessages.Count() )
  66. {
  67. Error( "CUserMessages::GetUserMessageSize( %i ) out of range!!!\n", index );
  68. }
  69. return m_UserMessages.GetElementName( index );
  70. }
  71. //-----------------------------------------------------------------------------
  72. // Purpose:
  73. // Input : index -
  74. // Output : Returns true on success, false on failure.
  75. //-----------------------------------------------------------------------------
  76. bool CUserMessages::IsValidIndex( int index )
  77. {
  78. return m_UserMessages.IsValidIndex( index );
  79. }
  80. //-----------------------------------------------------------------------------
  81. // Purpose:
  82. // Input : *name -
  83. // size - -1 for variable size
  84. //-----------------------------------------------------------------------------
  85. void CUserMessages::Register( const char *name, int size )
  86. {
  87. Assert( name );
  88. int idx = m_UserMessages.Find( name );
  89. if ( idx != m_UserMessages.InvalidIndex() )
  90. {
  91. Error( "CUserMessages::Register '%s' already registered\n", name );
  92. }
  93. CUserMessage * entry = new CUserMessage;
  94. entry->size = size;
  95. entry->name = name;
  96. m_UserMessages.Insert( name, entry );
  97. }
  98. //-----------------------------------------------------------------------------
  99. // Purpose:
  100. // Input : *name -
  101. // hook -
  102. //-----------------------------------------------------------------------------
  103. void CUserMessages::HookMessage( const char *name, pfnUserMsgHook hook )
  104. {
  105. #if defined( CLIENT_DLL )
  106. Assert( name );
  107. Assert( hook );
  108. int idx = m_UserMessages.Find( name );
  109. if ( idx == m_UserMessages.InvalidIndex() )
  110. {
  111. DevMsg( "CUserMessages::HookMessage: no such message %s\n", name );
  112. Assert( 0 );
  113. return;
  114. }
  115. int i = m_UserMessages[ idx ]->clienthooks.AddToTail();
  116. m_UserMessages[ idx ]->clienthooks[i] = hook;
  117. #else
  118. Error( "CUserMessages::HookMessage called from server code!!!\n" );
  119. #endif
  120. }
  121. //-----------------------------------------------------------------------------
  122. // Purpose:
  123. // Input : *pszName -
  124. // iSize -
  125. // *pbuf -
  126. // Output : Returns true on success, false on failure.
  127. //-----------------------------------------------------------------------------
  128. bool CUserMessages::DispatchUserMessage( int msg_type, bf_read &msg_data )
  129. {
  130. #if defined( CLIENT_DLL )
  131. if ( msg_type < 0 || msg_type >= (int)m_UserMessages.Count() )
  132. {
  133. DevMsg( "CUserMessages::DispatchUserMessage: Bogus msg type %i (max == %i)\n", msg_type, m_UserMessages.Count() );
  134. Assert( 0 );
  135. return false;
  136. }
  137. CUserMessage *entry = m_UserMessages[ msg_type ];
  138. if ( !entry )
  139. {
  140. DevMsg( "CUserMessages::DispatchUserMessage: Missing client entry for msg type %i\n", msg_type );
  141. Assert( 0 );
  142. return false;
  143. }
  144. if ( entry->clienthooks.Count() == 0 )
  145. {
  146. DevMsg( "CUserMessages::DispatchUserMessage: missing client hook for %s\n", GetUserMessageName(msg_type) );
  147. Assert( 0 );
  148. return false;
  149. }
  150. for (int i = 0; i < entry->clienthooks.Count(); i++ )
  151. {
  152. bf_read msg_copy = msg_data;
  153. pfnUserMsgHook hook = entry->clienthooks[i];
  154. (*hook)( msg_copy );
  155. }
  156. return true;
  157. #else
  158. Error( "CUserMessages::DispatchUserMessage called from server code!!!\n" );
  159. return false;
  160. #endif
  161. }
  162. // Singleton
  163. static CUserMessages g_UserMessages;
  164. // Expose to rest of .dll
  165. CUserMessages *usermessages = &g_UserMessages;