QGearsLGPArchive.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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 <iostream>
  23. #include <string>
  24. #include "data/QGearsLGPArchive.h"
  25. //#include <OgreThreadDefines.h>
  26. #include "common/QGearsStringUtil.h"
  27. #include "data/QGearsLGPArchiveSerializer.h"
  28. #include "core/Logger.h"
  29. namespace QGears
  30. {
  31. //-------------------------------------------------------------------------
  32. LGPArchive::LGPArchive( const String &name, const String &archType ) :
  33. Ogre::Archive( name, archType )
  34. {
  35. }
  36. //-------------------------------------------------------------------------
  37. LGPArchive::~LGPArchive()
  38. {
  39. unload();
  40. }
  41. //-------------------------------------------------------------------------
  42. void
  43. LGPArchive::load()
  44. {
  45. //OGRE_LOCK_AUTO_MUTEX
  46. std::ifstream *ifs( OGRE_NEW_T( std::ifstream, Ogre::MEMCATEGORY_GENERAL )( mName.c_str(), std::ifstream::binary ) );
  47. load( OGRE_NEW Ogre::FileStreamDataStream( ifs ) );
  48. }
  49. //-------------------------------------------------------------------------
  50. void
  51. LGPArchive::load( Ogre::DataStream* lgp )
  52. {
  53. m_lgp_file.reset();
  54. m_lgp_file.bind( lgp );
  55. LGPArchiveSerializer lgp_archive_serializer;
  56. lgp_archive_serializer.importLGPArchive( m_lgp_file, this );
  57. FileList::const_iterator it( m_files.begin() ), it_end( m_files.end() );
  58. while( it != it_end )
  59. {
  60. Ogre::FileInfo file_info;
  61. file_info.archive = this;
  62. file_info.filename = it->file_name;
  63. StringUtil::splitFilename( it->file_name, file_info.basename, file_info.path );
  64. // TODO: This is actually still the compressed size!!
  65. file_info.uncompressedSize = it->data_size;
  66. file_info.compressedSize = file_info.uncompressedSize;
  67. //LOG_DEBUG( "add file:" + file_info.filename );
  68. m_file_infos.push_back( file_info );
  69. ++it;
  70. }
  71. // writing not implemented
  72. mReadOnly = true;
  73. }
  74. //-------------------------------------------------------------------------
  75. void
  76. LGPArchive::unload()
  77. {
  78. //OGRE_LOCK_AUTO_MUTEX
  79. m_files.clear();
  80. m_file_infos.clear();
  81. m_lgp_file.reset();
  82. }
  83. //-------------------------------------------------------------------------
  84. Ogre::DataStreamPtr
  85. LGPArchive::open( const String& filename, bool readOnly ) const
  86. {
  87. //LOG_DEBUG( "file:" + filename + " readOnly: " + Ogre::StringConverter::toString(readOnly) );
  88. Ogre::MemoryDataStream* buffer( nullptr );
  89. if (m_lgp_file != nullptr)
  90. {
  91. FileList::const_iterator it( m_files.begin() );
  92. FileList::const_iterator it_end( m_files.end() );
  93. while( it != it_end )
  94. {
  95. if( it->file_name == filename )
  96. {
  97. m_lgp_file->seek( it->dataoffset_ );
  98. size_t length( it->data_size );
  99. buffer = new Ogre::MemoryDataStream( length );
  100. m_lgp_file->read( buffer->getPtr(), length );
  101. break;
  102. }
  103. ++it;
  104. }
  105. }
  106. return Ogre::DataStreamPtr( buffer );
  107. }
  108. //-------------------------------------------------------------------------
  109. Ogre::DataStreamPtr
  110. LGPArchive::create( const String& filename ) const
  111. {
  112. OGRE_EXCEPT(Ogre::Exception::ERR_NOT_IMPLEMENTED,
  113. "Modification of lgp archives is not supported",
  114. "LGPArchive::create");
  115. }
  116. //-------------------------------------------------------------------------
  117. void
  118. LGPArchive::remove(const String& filename) const
  119. {
  120. OGRE_EXCEPT(Ogre::Exception::ERR_NOT_IMPLEMENTED,
  121. "Modification of lgp archives is not supported",
  122. "LGPArchive::remove");
  123. }
  124. //-------------------------------------------------------------------------
  125. Ogre::StringVectorPtr
  126. LGPArchive::list( bool recursive, bool dirs ) const
  127. {
  128. //OGRE_LOCK_AUTO_MUTEX
  129. Ogre::StringVector* file_names( OGRE_NEW_T( Ogre::StringVector, Ogre::MEMCATEGORY_GENERAL)() );
  130. FileList::const_iterator it( m_files.begin() );
  131. FileList::const_iterator it_end( m_files.end() );
  132. while( it != it_end )
  133. {
  134. file_names->push_back( it->file_name );
  135. ++it;
  136. }
  137. return Ogre::StringVectorPtr( file_names, Ogre::SPFM_DELETE_T );
  138. }
  139. //-------------------------------------------------------------------------
  140. Ogre::FileInfoListPtr
  141. LGPArchive::listFileInfo( bool recursive, bool dirs ) const
  142. {
  143. //OGRE_LOCK_AUTO_MUTEX
  144. return Ogre::FileInfoListPtr( OGRE_NEW_T( Ogre::FileInfoList, Ogre::MEMCATEGORY_GENERAL )( m_file_infos ), Ogre::SPFM_DELETE_T );
  145. }
  146. //-------------------------------------------------------------------------
  147. Ogre::StringVectorPtr
  148. LGPArchive::find( const String& pattern, bool recursive, bool dirs ) const
  149. {
  150. LOG_DEBUG( pattern );
  151. //OGRE_LOCK_AUTO_MUTEX
  152. Ogre::StringVector* file_names( OGRE_NEW_T( Ogre::StringVector, Ogre::MEMCATEGORY_GENERAL)() );
  153. Ogre::FileInfoListPtr found_infos( findFileInfo( pattern, recursive, dirs ) );
  154. Ogre::FileInfoList::const_iterator it( found_infos->begin() ), it_end( found_infos->end() );
  155. while( it != it_end )
  156. {
  157. file_names->push_back( it->filename );
  158. ++it;
  159. }
  160. return Ogre::StringVectorPtr( file_names, Ogre::SPFM_DELETE_T );
  161. }
  162. //-------------------------------------------------------------------------
  163. Ogre::FileInfoListPtr
  164. LGPArchive::findFileInfo( const String& pattern, bool recursive, bool dirs ) const
  165. {
  166. LOG_DEBUG( pattern );
  167. //OGRE_LOCK_AUTO_MUTEX
  168. Ogre::FileInfoList* file_infos( OGRE_NEW_T( Ogre::FileInfoList, Ogre::MEMCATEGORY_GENERAL)() );
  169. // If pattern contains a directory name, do a full match
  170. bool full_match = (pattern.find ('/') != String::npos) ||
  171. (pattern.find ('\\') != String::npos);
  172. Ogre::FileInfoList::const_iterator it( m_file_infos.begin() ), it_end( m_file_infos.end() );
  173. while( it != it_end )
  174. {
  175. const String& file_name( full_match ? it->filename : it->basename );
  176. if ( StringUtil::match( file_name, pattern, isCaseSensitive() ) )
  177. {
  178. file_infos->push_back( *it );
  179. }
  180. ++it;
  181. }
  182. return Ogre::FileInfoListPtr( file_infos, Ogre::SPFM_DELETE_T );
  183. }
  184. //-------------------------------------------------------------------------
  185. bool
  186. LGPArchive::exists( const String& filename ) const
  187. {
  188. FileList::const_iterator it( m_files.begin() );
  189. FileList::const_iterator it_end( m_files.end() );
  190. while( it != it_end )
  191. {
  192. if( it->file_name == filename )
  193. {
  194. LOG_DEBUG( "Found " + filename + " in " + mName );
  195. return true;
  196. }
  197. ++it;
  198. }
  199. LOG_DEBUG( "Couldn't find " + filename + " in " + mName );
  200. return false;
  201. }
  202. //-------------------------------------------------------------------------
  203. time_t
  204. LGPArchive::getModifiedTime( const String& filename ) const
  205. {
  206. struct stat tagStat;
  207. bool ret = (stat(mName.c_str(), &tagStat) == 0);
  208. if (ret)
  209. {
  210. return tagStat.st_mtime;
  211. }
  212. else
  213. {
  214. return 0;
  215. }
  216. }
  217. //-------------------------------------------------------------------------
  218. LGPArchive::FileList&
  219. LGPArchive::getFiles( void )
  220. {
  221. return m_files;
  222. }
  223. //-------------------------------------------------------------------------
  224. }