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.

183 lines
6.5 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #ifndef SOUNDEMITTERSYSTEMBASE_H
  8. #define SOUNDEMITTERSYSTEMBASE_H
  9. #ifdef _WIN32
  10. #pragma once
  11. #endif
  12. #include "SoundEmitterSystem/isoundemittersystembase.h"
  13. #include "soundflags.h"
  14. #include "interval.h"
  15. #include "UtlSortVector.h"
  16. #include <tier1/utlstring.h>
  17. #include <tier1/utlhashtable.h>
  18. soundlevel_t TextToSoundLevel( const char *key );
  19. struct CSoundEntry
  20. {
  21. CUtlConstString m_Name;
  22. CSoundParametersInternal m_SoundParams;
  23. uint16 m_nScriptFileIndex;
  24. bool m_bRemoved : 1;
  25. bool m_bIsOverride : 1;
  26. bool IsOverride() const
  27. {
  28. return m_bIsOverride;
  29. }
  30. };
  31. struct CSoundEntryHashFunctor : CaselessStringHashFunctor
  32. {
  33. using CaselessStringHashFunctor::operator();
  34. unsigned int operator()( CSoundEntry *e ) const
  35. {
  36. return CaselessStringHashFunctor::operator()( e->m_Name.Get() );
  37. }
  38. };
  39. struct CSoundEntryEqualFunctor : CaselessStringEqualFunctor
  40. {
  41. using CaselessStringEqualFunctor::operator();
  42. bool operator()( CSoundEntry *lhs, CSoundEntry *rhs ) const
  43. {
  44. return CaselessStringEqualFunctor::operator()( lhs->m_Name.Get(), rhs->m_Name.Get() );
  45. }
  46. bool operator()( CSoundEntry *lhs, const char *rhs ) const
  47. {
  48. return CaselessStringEqualFunctor::operator()( lhs->m_Name.Get(), rhs );
  49. }
  50. };
  51. //-----------------------------------------------------------------------------
  52. // Purpose: Base class for sound emitter system handling (can be used by tools)
  53. //-----------------------------------------------------------------------------
  54. class CSoundEmitterSystemBase : public ISoundEmitterSystemBase
  55. {
  56. public:
  57. CSoundEmitterSystemBase();
  58. virtual ~CSoundEmitterSystemBase() { }
  59. // Methods of IAppSystem
  60. virtual bool Connect( CreateInterfaceFn factory );
  61. virtual void Disconnect();
  62. virtual void *QueryInterface( const char *pInterfaceName );
  63. virtual InitReturnVal_t Init();
  64. virtual void Shutdown();
  65. public:
  66. virtual bool ModInit();
  67. virtual void ModShutdown();
  68. virtual int GetSoundIndex( const char *pName ) const;
  69. virtual bool IsValidIndex( int index );
  70. virtual int GetSoundCount( void );
  71. virtual const char *GetSoundName( int index );
  72. virtual bool GetParametersForSound( const char *soundname, CSoundParameters& params, gender_t gender, bool isbeingemitted = false );
  73. virtual const char *GetWaveName( CUtlSymbol& sym );
  74. virtual CUtlSymbol AddWaveName( const char *name );
  75. virtual soundlevel_t LookupSoundLevel( const char *soundname );
  76. virtual const char *GetWavFileForSound( const char *soundname, char const *actormodel );
  77. virtual const char *GetWavFileForSound( const char *soundname, gender_t gender );
  78. virtual int CheckForMissingWavFiles( bool verbose );
  79. virtual const char *GetSourceFileForSound( int index ) const;
  80. // Iteration methods
  81. virtual int First() const;
  82. virtual int Next( int i ) const;
  83. virtual int InvalidIndex() const;
  84. virtual CSoundParametersInternal *InternalGetParametersForSound( int index );
  85. // The host application is responsible for dealing with dirty sound scripts, etc.
  86. virtual bool AddSound( const char *soundname, const char *scriptfile, const CSoundParametersInternal& params );
  87. virtual void RemoveSound( const char *soundname );
  88. virtual void MoveSound( const char *soundname, const char *newscript );
  89. virtual void RenameSound( const char *soundname, const char *newname );
  90. virtual void UpdateSoundParameters( const char *soundname, const CSoundParametersInternal& params );
  91. virtual int GetNumSoundScripts() const;
  92. virtual char const *GetSoundScriptName( int index ) const;
  93. virtual bool IsSoundScriptDirty( int index ) const;
  94. virtual int FindSoundScript( const char *name ) const;
  95. virtual void SaveChangesToSoundScript( int scriptindex );
  96. virtual void ExpandSoundNameMacros( CSoundParametersInternal& params, char const *wavename );
  97. virtual gender_t GetActorGender( char const *actormodel );
  98. virtual void GenderExpandString( char const *actormodel, char const *in, char *out, int maxlen );
  99. virtual void GenderExpandString( gender_t gender, char const *in, char *out, int maxlen );
  100. virtual bool IsUsingGenderToken( char const *soundname );
  101. virtual unsigned int GetManifestFileTimeChecksum();
  102. virtual bool GetParametersForSoundEx( const char *soundname, HSOUNDSCRIPTHANDLE& handle, CSoundParameters& params, gender_t gender, bool isbeingemitted = false );
  103. virtual soundlevel_t LookupSoundLevelByHandle( char const *soundname, HSOUNDSCRIPTHANDLE& handle );
  104. // Called from both client and server (single player) or just one (server only in dedicated server and client only if connected to a remote server)
  105. // Called by LevelInitPreEntity to override sound scripts for the mod with level specific overrides based on custom mapnames, etc.
  106. virtual void AddSoundOverrides( char const *scriptfile, bool bPreload = false );
  107. // Called by either client or server in LevelShutdown to clear out custom overrides
  108. virtual void ClearSoundOverrides();
  109. virtual void ReloadSoundEntriesInList( IFileList *pFilesToReload );
  110. // Called by either client or server to force ModShutdown and ModInit
  111. virtual void Flush();
  112. private:
  113. bool InternalModInit();
  114. void InternalModShutdown();
  115. void AddSoundsFromFile( const char *filename, bool bPreload, bool bIsOverride = false, bool bRefresh = false );
  116. bool InitSoundInternalParameters( const char *soundname, KeyValues *kv, CSoundParametersInternal& params );
  117. void LoadGlobalActors();
  118. float TranslateAttenuation( const char *key );
  119. soundlevel_t TranslateSoundLevel( const char *key );
  120. int TranslateChannel( const char *name );
  121. int FindBestSoundForGender( SoundFile *pSoundnames, int c, gender_t gender );
  122. void EnsureAvailableSlotsForGender( SoundFile *pSoundnames, int c, gender_t gender );
  123. void AddSoundName( CSoundParametersInternal& params, char const *wavename, gender_t gender );
  124. CUtlHashtable< CUtlConstString, gender_t, CaselessStringHashFunctor, UTLConstStringCaselessStringEqualFunctor<char> > m_ActorGenders;
  125. CUtlStableHashtable< CSoundEntry*, empty_t, CSoundEntryHashFunctor, CSoundEntryEqualFunctor, uint16, const char* > m_Sounds;
  126. CUtlVector< CSoundEntry * > m_SavedOverrides;
  127. CUtlVector< FileNameHandle_t > m_OverrideFiles;
  128. struct CSoundScriptFile
  129. {
  130. FileNameHandle_t hFilename;
  131. bool dirty;
  132. };
  133. CUtlVector< CSoundScriptFile > m_SoundKeyValues;
  134. int m_nInitCount;
  135. unsigned int m_uManifestPlusScriptChecksum;
  136. CUtlSymbolTable m_Waves;
  137. };
  138. #endif // SOUNDEMITTERSYSTEMBASE_H