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.

133 lines
2.9 KiB

  1. //====== Copyright � 1996-2007, Valve Corporation, All rights reserved. =======
  2. //
  3. // Purpose:
  4. //
  5. //=============================================================================
  6. #include "cbase.h"
  7. #include "SceneCache.h"
  8. #include "choreoscene.h"
  9. #include "choreoevent.h"
  10. // NOTE: This has to be the last file included!
  11. #include "tier0/memdbgon.h"
  12. extern ISoundEmitterSystemBase *soundemitterbase;
  13. CChoreoScene *BlockingLoadScene( const char *filename );
  14. CSceneCache::CSceneCache()
  15. {
  16. msecs = 0;
  17. }
  18. //CSceneCache::CSceneCache( const CSceneCache& src )
  19. //{
  20. // msecs = src.msecs;
  21. // sounds = src.sounds;
  22. //}
  23. int CSceneCache::GetSoundCount() const
  24. {
  25. return sounds.Count();
  26. }
  27. char const *CSceneCache::GetSoundName( int index )
  28. {
  29. return soundemitterbase->GetSoundName( sounds[ index ] );
  30. }
  31. void CSceneCache::Save( CUtlBuffer& buf )
  32. {
  33. buf.PutUnsignedInt( msecs );
  34. unsigned short c = GetSoundCount();
  35. buf.PutShort( c );
  36. Assert( sounds.Count() <= 65536 );
  37. for ( int i = 0; i < c; ++i )
  38. {
  39. buf.PutString( GetSoundName( i ) );
  40. }
  41. }
  42. void CSceneCache::Restore( CUtlBuffer& buf )
  43. {
  44. MEM_ALLOC_CREDIT();
  45. msecs = buf.GetUnsignedInt();
  46. unsigned short c = (unsigned short)buf.GetShort();
  47. for ( int i = 0; i < c; ++i )
  48. {
  49. char soundname[ 512 ];
  50. buf.GetString( soundname, sizeof( soundname ) );
  51. int idx = soundemitterbase->GetSoundIndex( soundname );
  52. if ( soundemitterbase->IsValidIndex( idx ) )
  53. {
  54. if ( sounds.Find( idx ) == sounds.InvalidIndex() )
  55. {
  56. sounds.Insert( idx );
  57. }
  58. }
  59. }
  60. }
  61. //-----------------------------------------------------------------------------
  62. // Purpose: Static method
  63. // Input : *event -
  64. // soundlist -
  65. //-----------------------------------------------------------------------------
  66. void CSceneCache::PrecacheSceneEvent( CChoreoEvent *event, CUtlSortVector< int, CSceneCacheListLess > &soundlist )
  67. {
  68. if ( !event || event->GetType() != CChoreoEvent::SPEAK )
  69. return;
  70. int idx = soundemitterbase->GetSoundIndex( event->GetParameters() );
  71. if ( soundemitterbase->IsValidIndex( idx ) )
  72. {
  73. MEM_ALLOC_CREDIT();
  74. soundlist.Insert( idx );
  75. }
  76. if ( event->GetCloseCaptionType() == CChoreoEvent::CC_MASTER )
  77. {
  78. char tok[ CChoreoEvent::MAX_CCTOKEN_STRING ];
  79. if ( event->GetPlaybackCloseCaptionToken( tok, sizeof( tok ) ) )
  80. {
  81. int idx = soundemitterbase->GetSoundIndex( tok );
  82. if ( soundemitterbase->IsValidIndex( idx ) && soundlist.Find( idx ) == soundlist.InvalidIndex() )
  83. {
  84. MEM_ALLOC_CREDIT();
  85. soundlist.Insert( idx );
  86. }
  87. }
  88. }
  89. }
  90. void CSceneCache::Rebuild( char const *filename )
  91. {
  92. msecs = 0;
  93. sounds.RemoveAll();
  94. CChoreoScene *scene = BlockingLoadScene( filename );
  95. if ( scene )
  96. {
  97. // Walk all events looking for SPEAK events
  98. CChoreoEvent *event;
  99. int c = scene->GetNumEvents();
  100. for ( int i = 0; i < c; ++i )
  101. {
  102. event = scene->GetEvent( i );
  103. PrecacheSceneEvent( event, sounds );
  104. }
  105. // Update scene duration, too
  106. msecs = (int)( scene->FindStopTime() * 1000.0f + 0.5f );
  107. delete scene;
  108. }
  109. }