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.

137 lines
2.7 KiB

  1. //========= Copyright � 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #include "cbase.h"
  8. #include "iclassmap.h"
  9. #include "utldict.h"
  10. // memdbgon must be the last include file in a .cpp file!!!
  11. #include "tier0/memdbgon.h"
  12. class classentry_t
  13. {
  14. public:
  15. classentry_t()
  16. {
  17. mapname[ 0 ] = 0;
  18. factory = 0;
  19. size = -1;
  20. }
  21. char const *GetMapName() const
  22. {
  23. return mapname;
  24. }
  25. void SetMapName( char const *newname )
  26. {
  27. Q_strncpy( mapname, newname, sizeof( mapname ) );
  28. }
  29. DISPATCHFUNCTION factory;
  30. int size;
  31. private:
  32. char mapname[ 40 ];
  33. };
  34. class CClassMap : public IClassMap
  35. {
  36. public:
  37. virtual void Add( const char *mapname, const char *classname, int size, DISPATCHFUNCTION factory /*= 0*/ );
  38. virtual const char *Lookup( const char *classname );
  39. virtual C_BaseEntity *CreateEntity( const char *mapname );
  40. virtual int GetClassSize( const char *classname );
  41. private:
  42. CUtlDict< classentry_t, unsigned short > m_ClassDict;
  43. };
  44. IClassMap& GetClassMap( void )
  45. {
  46. static CClassMap g_Classmap;
  47. return g_Classmap;
  48. }
  49. void CClassMap::Add( const char *mapname, const char *classname, int size, DISPATCHFUNCTION factory = 0 )
  50. {
  51. const char *map = Lookup( classname );
  52. if ( map && !Q_strcasecmp( mapname, map ) )
  53. return;
  54. if ( map )
  55. {
  56. int index = m_ClassDict.Find( classname );
  57. Assert( index != m_ClassDict.InvalidIndex() );
  58. m_ClassDict.RemoveAt( index );
  59. }
  60. classentry_t element;
  61. element.SetMapName( mapname );
  62. element.factory = factory;
  63. element.size = size;
  64. m_ClassDict.Insert( classname, element );
  65. }
  66. const char *CClassMap::Lookup( const char *classname )
  67. {
  68. unsigned short index;
  69. static classentry_t lookup;
  70. index = m_ClassDict.Find( classname );
  71. if ( index == m_ClassDict.InvalidIndex() )
  72. return NULL;
  73. lookup = m_ClassDict.Element( index );
  74. return lookup.GetMapName();
  75. }
  76. C_BaseEntity *CClassMap::CreateEntity( const char *mapname )
  77. {
  78. int c = m_ClassDict.Count();
  79. int i;
  80. for ( i = 0; i < c; i++ )
  81. {
  82. classentry_t *lookup = &m_ClassDict[ i ];
  83. if ( !lookup )
  84. continue;
  85. if ( Q_stricmp( lookup->GetMapName(), mapname ) )
  86. continue;
  87. if ( !lookup->factory )
  88. {
  89. #if defined( _DEBUG )
  90. Msg( "No factory for %s/%s\n", lookup->GetMapName(), m_ClassDict.GetElementName( i ) );
  91. #endif
  92. continue;
  93. }
  94. return ( *lookup->factory )();
  95. }
  96. return NULL;
  97. }
  98. int CClassMap::GetClassSize( const char *classname )
  99. {
  100. int c = m_ClassDict.Count();
  101. int i;
  102. for ( i = 0; i < c; i++ )
  103. {
  104. classentry_t *lookup = &m_ClassDict[ i ];
  105. if ( !lookup )
  106. continue;
  107. if ( Q_strcmp( lookup->GetMapName(), classname ) )
  108. continue;
  109. return lookup->size;
  110. }
  111. return -1;
  112. }