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.

138 lines
3.8 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #include "cbase.h"
  8. #include "utlhashtable.h"
  9. #ifndef GC
  10. #include "igamesystem.h"
  11. #endif
  12. #include "gamestringpool.h"
  13. // memdbgon must be the last include file in a .cpp file!!!
  14. #include "tier0/memdbgon.h"
  15. //-----------------------------------------------------------------------------
  16. // Purpose: The actual storage for pooled per-level strings
  17. //-----------------------------------------------------------------------------
  18. #ifdef GC
  19. class CGameStringPool
  20. #else
  21. class CGameStringPool : public CBaseGameSystem
  22. #endif
  23. {
  24. virtual char const *Name() { return "CGameStringPool"; }
  25. virtual void LevelShutdownPostEntity() { FreeAll(); }
  26. void FreeAll()
  27. {
  28. #if 0 && _DEBUG
  29. m_Strings.DbgCheckIntegrity();
  30. m_KeyLookupCache.DbgCheckIntegrity();
  31. #endif
  32. m_Strings.Purge();
  33. m_KeyLookupCache.Purge();
  34. }
  35. CUtlHashtable<CUtlConstString> m_Strings;
  36. CUtlHashtable<const void*, const char*> m_KeyLookupCache;
  37. public:
  38. CGameStringPool() : m_Strings(256) { }
  39. ~CGameStringPool() { FreeAll(); }
  40. void Dump( void )
  41. {
  42. CUtlVector<const char*> strings( 0, m_Strings.Count() );
  43. for (UtlHashHandle_t i = m_Strings.FirstHandle(); i != m_Strings.InvalidHandle(); i = m_Strings.NextHandle(i))
  44. {
  45. strings.AddToTail( strings[i] );
  46. }
  47. struct _Local {
  48. static int __cdecl F(const char * const *a, const char * const *b) { return strcmp(*a, *b); }
  49. };
  50. strings.Sort( _Local::F );
  51. for ( int i = 0; i < strings.Count(); ++i )
  52. {
  53. DevMsg( " %d (0x%p) : %s\n", i, strings[i], strings[i] );
  54. }
  55. DevMsg( "\n" );
  56. DevMsg( "Size: %d items\n", strings.Count() );
  57. }
  58. const char *Find(const char *string)
  59. {
  60. UtlHashHandle_t i = m_Strings.Find( string );
  61. return i == m_Strings.InvalidHandle() ? NULL : m_Strings[ i ].Get();
  62. }
  63. const char *Allocate(const char *string)
  64. {
  65. return m_Strings[ m_Strings.Insert( string ) ].Get();
  66. }
  67. const char *AllocateWithKey(const char *string, const void* key)
  68. {
  69. const char * &cached = m_KeyLookupCache[ m_KeyLookupCache.Insert( key, NULL ) ];
  70. if (cached == NULL)
  71. {
  72. cached = Allocate( string );
  73. }
  74. return cached;
  75. }
  76. };
  77. static CGameStringPool g_GameStringPool;
  78. #ifndef GC
  79. //-----------------------------------------------------------------------------
  80. // String system accessor
  81. //-----------------------------------------------------------------------------
  82. IGameSystem *GameStringSystem()
  83. {
  84. return &g_GameStringPool;
  85. }
  86. #endif
  87. //-----------------------------------------------------------------------------
  88. // Purpose: The public accessor for the level-global pooled strings
  89. //-----------------------------------------------------------------------------
  90. string_t AllocPooledString( const char * pszValue )
  91. {
  92. if (pszValue && *pszValue)
  93. return MAKE_STRING( g_GameStringPool.Allocate( pszValue ) );
  94. return NULL_STRING;
  95. }
  96. string_t AllocPooledString_StaticConstantStringPointer( const char * pszGlobalConstValue )
  97. {
  98. Assert(pszGlobalConstValue && *pszGlobalConstValue);
  99. return MAKE_STRING( g_GameStringPool.AllocateWithKey( pszGlobalConstValue, pszGlobalConstValue ) );
  100. }
  101. string_t FindPooledString( const char *pszValue )
  102. {
  103. return MAKE_STRING( g_GameStringPool.Find( pszValue ) );
  104. }
  105. #if !defined(CLIENT_DLL) && !defined( GC )
  106. //------------------------------------------------------------------------------
  107. // Purpose:
  108. //------------------------------------------------------------------------------
  109. void CC_DumpGameStringTable( void )
  110. {
  111. if ( !UTIL_IsCommandIssuedByServerAdmin() )
  112. return;
  113. g_GameStringPool.Dump();
  114. }
  115. static ConCommand dumpgamestringtable("dumpgamestringtable", CC_DumpGameStringTable, "Dump the contents of the game string table to the console.", FCVAR_CHEAT);
  116. #endif