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.

148 lines
4.3 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //
  7. //=============================================================================//
  8. //
  9. // text_message.cpp
  10. //
  11. // implementation of CHudTextMessage class
  12. //
  13. // this class routes messages through titles.txt for localisation
  14. //
  15. #include "cbase.h"
  16. #include "text_message.h"
  17. #include "client_textmessage.h"
  18. #include "vgui_controls/Controls.h"
  19. #include "vgui/ILocalize.h"
  20. // memdbgon must be the last include file in a .cpp file!!!
  21. #include "tier0/memdbgon.h"
  22. //-----------------------------------------------------------------------------
  23. // Purpose: Implements IHudTextMessage
  24. //-----------------------------------------------------------------------------
  25. class CHudTextMessage : public IHudTextMessage
  26. {
  27. public:
  28. virtual char *LocaliseTextString( const char *msg, char *dst_buffer, int buffer_size );
  29. virtual char *BufferedLocaliseTextString( const char *msg );
  30. virtual char *LookupString( const char *msg_name, int *msg_dest = NULL );
  31. };
  32. // Singleton
  33. static CHudTextMessage g_HudTextMessage;
  34. IHudTextMessage *hudtextmessage = &g_HudTextMessage;
  35. //-----------------------------------------------------------------------------
  36. // Purpose: Searches through the string for any msg names (indicated by a '#')
  37. // any found are looked up in titles.txt and the new message substituted
  38. // the new value is pushed into dst_buffer
  39. // Input : *msg -
  40. // *dst_buffer -
  41. // buffer_size -
  42. // Output : char
  43. //-----------------------------------------------------------------------------
  44. char *CHudTextMessage::LocaliseTextString( const char *msg, char *dst_buffer, int buffer_size )
  45. {
  46. char *dst = dst_buffer;
  47. for ( char *src = (char*)msg; *src != 0 && buffer_size > 0; buffer_size-- )
  48. {
  49. if ( *src == '#' )
  50. {
  51. // cut msg name out of string
  52. static char word_buf[255];
  53. char *wdst = word_buf, *word_start = src;
  54. for ( ++src ; *src >= 'A' && *src <= 'z'; wdst++, src++ )
  55. {
  56. *wdst = *src;
  57. }
  58. *wdst = 0;
  59. // lookup msg name in titles.txt
  60. client_textmessage_t *clmsg = TextMessageGet( word_buf );
  61. if ( !clmsg || !(clmsg->pMessage) )
  62. {
  63. src = word_start;
  64. *dst = *src;
  65. dst++, src++;
  66. continue;
  67. }
  68. // Does titles.txt want to lookup into cstrike_<language>.txt?
  69. wchar_t *pLocalizedStr;
  70. if ( clmsg->pMessage[0] == '#' && ((pLocalizedStr = g_pVGuiLocalize->Find( clmsg->pMessage )) != NULL ) )
  71. {
  72. g_pVGuiLocalize->ConvertUnicodeToANSI( pLocalizedStr, dst, buffer_size );
  73. }
  74. else
  75. {
  76. // copy string into message over the msg name
  77. for ( char *wsrc = (char*)clmsg->pMessage; *wsrc != 0; wsrc++, dst++ )
  78. {
  79. *dst = *wsrc;
  80. }
  81. *dst = 0;
  82. }
  83. }
  84. else
  85. {
  86. *dst = *src;
  87. dst++, src++;
  88. *dst = 0;
  89. }
  90. }
  91. //
  92. // ensure null termination
  93. dst_buffer[buffer_size-1] = 0;
  94. return dst_buffer;
  95. }
  96. //-----------------------------------------------------------------------------
  97. // Purpose: As above, but with a local static buffer
  98. // Input : *msg -
  99. // Output : char
  100. //-----------------------------------------------------------------------------
  101. char *CHudTextMessage::BufferedLocaliseTextString( const char *msg )
  102. {
  103. static char dst_buffer[1024];
  104. return LocaliseTextString( msg, dst_buffer, 1024 );
  105. }
  106. //-----------------------------------------------------------------------------
  107. // Purpose: Simplified version of LocaliseTextString; assumes string is only one word
  108. // Input : *msg -
  109. // *msg_dest -
  110. // Output : char
  111. //-----------------------------------------------------------------------------
  112. char *CHudTextMessage::LookupString( const char *msg, int *msg_dest )
  113. {
  114. if ( !msg )
  115. return "";
  116. // '#' character indicates this is a reference to a string in titles.txt, and not the string itself
  117. if ( msg[0] == '#' )
  118. {
  119. // this is a message name, so look up the real message
  120. client_textmessage_t *clmsg = TextMessageGet( msg+1 );
  121. if ( !clmsg || !(clmsg->pMessage) )
  122. return (char*)msg; // lookup failed, so return the original string
  123. if ( msg_dest )
  124. {
  125. // check to see if titles.txt info overrides msg destination
  126. // if clmsg->effect is less than 0, then clmsg->effect holds -1 * message_destination
  127. if ( clmsg->effect < 0 ) //
  128. *msg_dest = -clmsg->effect;
  129. }
  130. return (char*)clmsg->pMessage;
  131. }
  132. else
  133. { // nothing special about this message, so just return the same string
  134. return (char*)msg;
  135. }
  136. }