sfxFileStream.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "sfx/sfxFileStream.h"
  23. #include "core/stream/fileStream.h"
  24. #include "console/console.h"
  25. #include "core/util/safeDelete.h"
  26. SFXFileStream::ExtensionsVector SFXFileStream::smExtensions( __FILE__, __LINE__ );
  27. SFXFileStream::CreateFnsVector SFXFileStream::smCreateFns( __FILE__, __LINE__ );
  28. void SFXFileStream::registerExtension( String ext, SFXFILESTREAM_CREATE_FN create_fn )
  29. {
  30. // Register the stream creation first.
  31. smExtensions.push_back( ext );
  32. smCreateFns.push_back( create_fn );
  33. }
  34. void SFXFileStream::unregisterExtension( String ext )
  35. {
  36. for( ExtensionsVector::iterator iter = smExtensions.begin();
  37. iter != smExtensions.end(); ++ iter )
  38. if( ( *iter ).equal( ext, String::NoCase ) )
  39. {
  40. smExtensions.erase( iter );
  41. return;
  42. }
  43. }
  44. SFXFileStream* SFXFileStream::create( String filename )
  45. {
  46. //RDTODO: if original file has an extension, we should try that first
  47. // First strip off our current extension (validating
  48. // against a list of known extensions so that we don't
  49. // strip off the last part of a file name with a dot in it.
  50. String noExtension = Platform::stripExtension( filename, smExtensions );
  51. SFXFileStream *sfxStream = NULL;
  52. for( U32 i = 0; i < smExtensions.size(); i++ )
  53. {
  54. String testName = noExtension + smExtensions[ i ];
  55. Stream *stream = FileStream::createAndOpen( testName, Torque::FS::File::Read );
  56. if ( !stream )
  57. continue;
  58. // Note that the creation function swallows up the
  59. // resource stream and will take care of deleting it.
  60. sfxStream = smCreateFns[i]( stream );
  61. if ( sfxStream )
  62. return sfxStream;
  63. }
  64. return NULL;
  65. }
  66. bool SFXFileStream::exists( String filename )
  67. {
  68. // First strip off our current extension (validating
  69. // against a list of known extensions so that we don't
  70. // strip off the last part of a file name with a dot in it.
  71. String noExtension = Platform::stripExtension( filename, smExtensions );
  72. for( U32 i = 0; i < smExtensions.size(); i++ )
  73. {
  74. String testName = noExtension + smExtensions[ i ];
  75. if( Torque::FS::IsFile( testName ) )
  76. return true;
  77. }
  78. return false;
  79. }
  80. SFXFileStream::SFXFileStream()
  81. : mStream( NULL ),
  82. mOwnStream( false ),
  83. mFormat( 0, 0, 0 ),
  84. mSamples( 0 )
  85. {
  86. }
  87. SFXFileStream::SFXFileStream( const SFXFileStream& cloneFrom )
  88. {
  89. mStream = cloneFrom.mStream->clone();
  90. if( !mStream )
  91. {
  92. Con::errorf( "SFXFileStream::SFXFileStream() - Failed to clone source stream" );
  93. return;
  94. }
  95. mOwnStream = true;
  96. mFormat = cloneFrom.mFormat;
  97. mSamples = cloneFrom.mSamples;
  98. }
  99. SFXFileStream::~SFXFileStream()
  100. {
  101. // If the stream is still open, close it now. _close()
  102. // should usually be called by the destructor of derived classes,
  103. // but it their constructor fails, these won't even run.
  104. if( mStream && mOwnStream )
  105. SAFE_DELETE( mStream );
  106. }
  107. bool SFXFileStream::open( Stream *stream, bool ownStream )
  108. {
  109. AssertFatal( stream, "SFXFileStream::open() - Got null stream!" );
  110. close();
  111. mStream = stream;
  112. mOwnStream = ownStream;
  113. if( _readHeader() )
  114. {
  115. reset(); // Make sure we're set to read sample data.
  116. return true;
  117. }
  118. else
  119. return false;
  120. }
  121. void SFXFileStream::close()
  122. {
  123. if ( !mStream )
  124. return;
  125. // Let the overloaded class cleanup.
  126. _close();
  127. // We only close it if we own it.
  128. if ( mOwnStream )
  129. SAFE_DELETE( mStream );
  130. // Reset these to make it easier to detect bugs.
  131. mFormat.set( 0, 0, 0 );
  132. mSamples = 0;
  133. }
  134. bool SFXFileStream::isEOS() const
  135. {
  136. if ( !mStream )
  137. return true;
  138. return mStream->getStatus() != Stream::Ok;
  139. }