QGearsLGPArchive.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /*
  2. -----------------------------------------------------------------------------
  3. The MIT License (MIT)
  4. Copyright (c) 2013-09-22 Tobias Peters <tobias.peters@kreativeffekt.at>
  5. Permission is hereby granted, free of charge, to any person obtaining a copy
  6. of this software and associated documentation files (the "Software"), to deal
  7. in the Software without restriction, including without limitation the rights
  8. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the Software is
  10. furnished to do so, subject to the following conditions:
  11. The above copyright notice and this permission notice shall be included in
  12. all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. THE SOFTWARE.
  20. -----------------------------------------------------------------------------
  21. */
  22. #include "data/QGearsLGPArchive.h"
  23. //#include <OgreThreadDefines.h>
  24. #include "common/QGearsStringUtil.h"
  25. #include "data/QGearsLGPArchiveSerializer.h"
  26. #include "core/Logger.h"
  27. namespace QGears
  28. {
  29. //-------------------------------------------------------------------------
  30. LGPArchive::LGPArchive( const String &name, const String &archType ) :
  31. Ogre::Archive( name, archType )
  32. {
  33. }
  34. //-------------------------------------------------------------------------
  35. LGPArchive::~LGPArchive()
  36. {
  37. unload();
  38. }
  39. //-------------------------------------------------------------------------
  40. void
  41. LGPArchive::load()
  42. {
  43. //OGRE_LOCK_AUTO_MUTEX
  44. std::ifstream *ifs( OGRE_NEW_T( std::ifstream, Ogre::MEMCATEGORY_GENERAL )( mName.c_str(), std::ifstream::binary ) );
  45. load( OGRE_NEW Ogre::FileStreamDataStream( ifs ) );
  46. }
  47. //-------------------------------------------------------------------------
  48. void
  49. LGPArchive::load( Ogre::DataStream* lgp )
  50. {
  51. m_lgp_file.setNull();
  52. m_lgp_file.bind( lgp );
  53. LGPArchiveSerializer lgp_archive_serializer;
  54. lgp_archive_serializer.importLGPArchive( m_lgp_file, this );
  55. FileList::const_iterator it( m_files.begin() ), it_end( m_files.end() );
  56. while( it != it_end )
  57. {
  58. Ogre::FileInfo file_info;
  59. file_info.archive = this;
  60. file_info.filename = it->file_name;
  61. StringUtil::splitFilename( it->file_name, file_info.basename, file_info.path );
  62. file_info.uncompressedSize = it->data_size;
  63. file_info.compressedSize = file_info.uncompressedSize;
  64. //LOG_DEBUG( "add file:" + file_info.filename );
  65. m_file_infos.push_back( file_info );
  66. ++it;
  67. }
  68. // writing not implemented
  69. mReadOnly = true;
  70. }
  71. //-------------------------------------------------------------------------
  72. void
  73. LGPArchive::unload()
  74. {
  75. //OGRE_LOCK_AUTO_MUTEX
  76. m_files.clear();
  77. m_file_infos.clear();
  78. m_lgp_file.setNull();
  79. }
  80. //-------------------------------------------------------------------------
  81. Ogre::DataStreamPtr
  82. LGPArchive::open( const String& filename, bool readOnly ) const
  83. {
  84. LOG_DEBUG( "file:" + filename + " readOnly: " + Ogre::StringConverter::toString(readOnly) );
  85. Ogre::MemoryDataStream* buffer( NULL );
  86. if( !m_lgp_file.isNull() )
  87. {
  88. FileList::const_iterator it( m_files.begin() );
  89. FileList::const_iterator it_end( m_files.end() );
  90. while( it != it_end )
  91. {
  92. if( it->file_name == filename )
  93. {
  94. m_lgp_file->seek( it->data_offset );
  95. size_t length( it->data_size );
  96. buffer = new Ogre::MemoryDataStream( length );
  97. m_lgp_file->read( buffer->getPtr(), length );
  98. break;
  99. }
  100. ++it;
  101. }
  102. }
  103. return Ogre::DataStreamPtr( buffer );
  104. }
  105. //-------------------------------------------------------------------------
  106. Ogre::DataStreamPtr
  107. LGPArchive::create( const String& filename ) const
  108. {
  109. OGRE_EXCEPT(Ogre::Exception::ERR_NOT_IMPLEMENTED,
  110. "Modification of lgp archives is not supported",
  111. "LGPArchive::create");
  112. }
  113. //-------------------------------------------------------------------------
  114. void
  115. LGPArchive::remove(const String& filename) const
  116. {
  117. OGRE_EXCEPT(Ogre::Exception::ERR_NOT_IMPLEMENTED,
  118. "Modification of lgp archives is not supported",
  119. "LGPArchive::remove");
  120. }
  121. //-------------------------------------------------------------------------
  122. Ogre::StringVectorPtr
  123. LGPArchive::list( bool recursive, bool dirs )
  124. {
  125. //OGRE_LOCK_AUTO_MUTEX
  126. Ogre::StringVector* file_names( OGRE_NEW_T( Ogre::StringVector, Ogre::MEMCATEGORY_GENERAL)() );
  127. FileList::const_iterator it( m_files.begin() );
  128. FileList::const_iterator it_end( m_files.end() );
  129. while( it != it_end )
  130. {
  131. file_names->push_back( it->file_name );
  132. ++it;
  133. }
  134. return Ogre::StringVectorPtr( file_names, Ogre::SPFM_DELETE_T );
  135. }
  136. //-------------------------------------------------------------------------
  137. Ogre::FileInfoListPtr
  138. LGPArchive::listFileInfo( bool recursive, bool dirs )
  139. {
  140. //OGRE_LOCK_AUTO_MUTEX
  141. return Ogre::FileInfoListPtr( OGRE_NEW_T( Ogre::FileInfoList, Ogre::MEMCATEGORY_GENERAL )( m_file_infos ), Ogre::SPFM_DELETE_T );
  142. }
  143. //-------------------------------------------------------------------------
  144. Ogre::StringVectorPtr
  145. LGPArchive::find( const String& pattern, bool recursive, bool dirs )
  146. {
  147. LOG_DEBUG( pattern );
  148. //OGRE_LOCK_AUTO_MUTEX
  149. Ogre::StringVector* file_names( OGRE_NEW_T( Ogre::StringVector, Ogre::MEMCATEGORY_GENERAL)() );
  150. Ogre::FileInfoListPtr found_infos( findFileInfo( pattern, recursive, dirs ) );
  151. Ogre::FileInfoList::const_iterator it( found_infos->begin() ), it_end( found_infos->end() );
  152. while( it != it_end )
  153. {
  154. file_names->push_back( it->filename );
  155. ++it;
  156. }
  157. return Ogre::StringVectorPtr( file_names, Ogre::SPFM_DELETE_T );
  158. }
  159. //-------------------------------------------------------------------------
  160. Ogre::FileInfoListPtr
  161. LGPArchive::findFileInfo( const String& pattern, bool recursive, bool dirs ) const
  162. {
  163. LOG_DEBUG( pattern );
  164. //OGRE_LOCK_AUTO_MUTEX
  165. Ogre::FileInfoList* file_infos( OGRE_NEW_T( Ogre::FileInfoList, Ogre::MEMCATEGORY_GENERAL)() );
  166. // If pattern contains a directory name, do a full match
  167. bool full_match = (pattern.find ('/') != String::npos) ||
  168. (pattern.find ('\\') != String::npos);
  169. Ogre::FileInfoList::const_iterator it( m_file_infos.begin() ), it_end( m_file_infos.end() );
  170. while( it != it_end )
  171. {
  172. const String& file_name( full_match ? it->filename : it->basename );
  173. if ( StringUtil::match( file_name, pattern, isCaseSensitive() ) )
  174. {
  175. file_infos->push_back( *it );
  176. }
  177. ++it;
  178. }
  179. return Ogre::FileInfoListPtr( file_infos, Ogre::SPFM_DELETE_T );
  180. }
  181. //-------------------------------------------------------------------------
  182. bool
  183. LGPArchive::exists( const String& filename )
  184. {
  185. FileList::const_iterator it( m_files.begin() );
  186. FileList::const_iterator it_end( m_files.end() );
  187. while( it != it_end )
  188. {
  189. if( it->file_name == filename )
  190. {
  191. LOG_DEBUG( "Found " + filename + " in " + mName );
  192. return true;
  193. }
  194. ++it;
  195. }
  196. LOG_DEBUG( "Couldn't find " + filename + " in " + mName );
  197. return false;
  198. }
  199. //-------------------------------------------------------------------------
  200. time_t
  201. LGPArchive::getModifiedTime( const String& filename )
  202. {
  203. struct stat tagStat;
  204. bool ret = (stat(mName.c_str(), &tagStat) == 0);
  205. if (ret)
  206. {
  207. return tagStat.st_mtime;
  208. }
  209. else
  210. {
  211. return 0;
  212. }
  213. }
  214. //-------------------------------------------------------------------------
  215. LGPArchive::FileList&
  216. LGPArchive::getFiles( void )
  217. {
  218. return m_files;
  219. }
  220. //-------------------------------------------------------------------------
  221. }