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.

171 lines
5.8 KiB

  1. //===== Copyright � 1996-2005, Valve Corporation, All rights reserved. ======//
  2. //
  3. // Purpose:
  4. //
  5. //===========================================================================//
  6. #include "tier1/utlsymbol.h"
  7. #include "tier1/UtlStringMap.h"
  8. #include "tier2/tier2.h"
  9. #include "datacache/iprecachesystem.h"
  10. #include "datacache/iresourceaccesscontrol.h"
  11. // memdbgon must be the last include file in a .cpp file!!!
  12. #include "tier0/memdbgon.h"
  13. //-----------------------------------------------------------------------------
  14. // Implementation class
  15. //-----------------------------------------------------------------------------
  16. class CPrecacheSystem : public CTier2AppSystem< IPrecacheSystem >
  17. {
  18. typedef CTier2AppSystem< IPrecacheSystem > BaseClass;
  19. // Inherited from IAppSystem
  20. public:
  21. // Inherited from IResourceAccessControl
  22. public:
  23. void Register( IResourcePrecacher *pResourcePrecacherFirst, PrecacheSystem_t nSystem );
  24. // Precaches/uncaches all resources used by a particular system
  25. void Cache( IPrecacheHandler *pPrecacheHandler, PrecacheSystem_t nSystem, const char *pName, bool bPrecache, ResourceList_t hResourceList, bool bBuildResourceList );
  26. void UncacheAll( IPrecacheHandler *pPrecacheHandler );
  27. // Limits resource access to only resources used by this particular system
  28. // Use GLOBAL system, and NULL name to disable limited resource access
  29. void LimitResourceAccess( PrecacheSystem_t nSystem, const char *pName );
  30. void EndLimitedResourceAccess();
  31. private:
  32. IResourcePrecacher *m_pFirstPrecacher[PRECACHE_SYSTEM_COUNT];
  33. CUtlStringMap< ResourceList_t > m_ResourceList[ PRECACHE_SYSTEM_COUNT ];
  34. };
  35. //-----------------------------------------------------------------------------
  36. // String names corresponding to resource types
  37. //-----------------------------------------------------------------------------
  38. static const char *s_pResourceSystemName[] =
  39. {
  40. "global client resource",
  41. "global server resource",
  42. "vgui panel",
  43. "dispatch effect",
  44. "shared system",
  45. };
  46. //-----------------------------------------------------------------------------
  47. // Singleton
  48. //-----------------------------------------------------------------------------
  49. CPrecacheSystem g_PrecacheSystem;
  50. EXPOSE_SINGLE_INTERFACE_GLOBALVAR( CPrecacheSystem, IPrecacheSystem,
  51. PRECACHE_SYSTEM_INTERFACE_VERSION, g_PrecacheSystem );
  52. //-----------------------------------------------------------------------------
  53. // Precaches/uncaches all resources used by a particular system
  54. //-----------------------------------------------------------------------------
  55. void CPrecacheSystem::Cache( IPrecacheHandler *pPrecacheHandler, PrecacheSystem_t nSystem, const char *pName, bool bPrecache, ResourceList_t hResourceList, bool bBuildResourceList )
  56. {
  57. COMPILE_TIME_ASSERT( ARRAYSIZE( s_pResourceSystemName ) == PRECACHE_SYSTEM_COUNT );
  58. IResourcePrecacher *pPrecacher = m_pFirstPrecacher[nSystem];
  59. for( ; pPrecacher; pPrecacher = pPrecacher->GetNext() )
  60. {
  61. if ( pName && Q_stricmp( pName, pPrecacher->GetName() ) )
  62. continue;
  63. if ( bBuildResourceList && g_pResourceAccessControl )
  64. {
  65. Assert( hResourceList == RESOURCE_LIST_INVALID );
  66. UtlSymId_t idx = m_ResourceList[ nSystem ].Find( pName );
  67. if ( idx != UTL_INVAL_SYMBOL )
  68. {
  69. hResourceList = m_ResourceList[ nSystem ][ idx ];
  70. }
  71. else
  72. {
  73. char pDebugName[256];
  74. Q_snprintf( pDebugName, sizeof(pDebugName), "%s \"%s\"", s_pResourceSystemName[nSystem], pName );
  75. hResourceList = g_pResourceAccessControl->CreateResourceList( pDebugName );
  76. m_ResourceList[ nSystem ][ pName ] = hResourceList;
  77. }
  78. }
  79. pPrecacher->Cache( pPrecacheHandler, bPrecache, hResourceList, false );
  80. if ( !bPrecache )
  81. {
  82. m_ResourceList[ nSystem ][ pName ] = NULL;
  83. }
  84. }
  85. }
  86. //-----------------------------------------------------------------------------
  87. // Uncaches everything
  88. //-----------------------------------------------------------------------------
  89. void CPrecacheSystem::UncacheAll( IPrecacheHandler *pPrecacheHandler )
  90. {
  91. int nSystem;
  92. for ( nSystem = 0; nSystem < PRECACHE_SYSTEM_COUNT; nSystem ++ )
  93. {
  94. IResourcePrecacher *pPrecacher = m_pFirstPrecacher[nSystem];
  95. for( ; pPrecacher; pPrecacher = pPrecacher->GetNext() )
  96. {
  97. pPrecacher->Cache( pPrecacheHandler, false, RESOURCE_LIST_INVALID, false );
  98. }
  99. m_ResourceList[nSystem].Purge();
  100. }
  101. }
  102. //-----------------------------------------------------------------------------
  103. // Called to register a list of resource precachers for a given system
  104. //-----------------------------------------------------------------------------
  105. void CPrecacheSystem::Register( IResourcePrecacher *pResourcePrecacherFirst, PrecacheSystem_t nSystem )
  106. {
  107. // do we already have any precachers registered for this system?
  108. IResourcePrecacher *pCur = m_pFirstPrecacher[nSystem];
  109. if ( pCur )
  110. {
  111. while ( pCur->GetNext() != NULL )
  112. {
  113. pCur = pCur->GetNext();
  114. }
  115. // add the head of the new list to the tail of the existing list
  116. pCur->SetNext( pResourcePrecacherFirst );
  117. }
  118. else
  119. {
  120. m_pFirstPrecacher[nSystem] = pResourcePrecacherFirst;
  121. }
  122. }
  123. //-----------------------------------------------------------------------------
  124. // Limits resource access to only resources used by this particular system
  125. // Use GLOBAL system, and NULL name to disable limited resource access
  126. //-----------------------------------------------------------------------------
  127. void CPrecacheSystem::LimitResourceAccess( PrecacheSystem_t nSystem, const char *pName )
  128. {
  129. if ( g_pResourceAccessControl )
  130. {
  131. UtlSymId_t nSym = ( pName != NULL ) ? m_ResourceList[nSystem].Find( pName ) : UTL_INVAL_SYMBOL;
  132. if ( nSym != UTL_INVAL_SYMBOL )
  133. {
  134. g_pResourceAccessControl->LimitAccess( m_ResourceList[nSystem][nSym] );
  135. }
  136. else
  137. {
  138. g_pResourceAccessControl->LimitAccess( RESOURCE_LIST_INVALID );
  139. }
  140. }
  141. }
  142. void CPrecacheSystem::EndLimitedResourceAccess()
  143. {
  144. if ( g_pResourceAccessControl )
  145. {
  146. g_pResourceAccessControl->LimitAccess( RESOURCE_LIST_INVALID );
  147. }
  148. }