| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256 |
- /*
- -----------------------------------------------------------------------------
- The MIT License (MIT)
- Copyright (c) 2013-09-22 Tobias Peters <tobias.peters@kreativeffekt.at>
- Permission is hereby granted, free of charge, to any person obtaining a copy
- of this software and associated documentation files (the "Software"), to deal
- in the Software without restriction, including without limitation the rights
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- copies of the Software, and to permit persons to whom the Software is
- furnished to do so, subject to the following conditions:
- The above copyright notice and this permission notice shall be included in
- all copies or substantial portions of the Software.
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- THE SOFTWARE.
- -----------------------------------------------------------------------------
- */
- #include <iostream>
- #include <string>
- #include "data/QGearsLGPArchive.h"
- //#include <OgreThreadDefines.h>
- #include "common/QGearsStringUtil.h"
- #include "data/QGearsLGPArchiveSerializer.h"
- #include "core/Logger.h"
- namespace QGears
- {
- //-------------------------------------------------------------------------
- LGPArchive::LGPArchive( const String &name, const String &archType ) :
- Ogre::Archive( name, archType )
- {
- }
- //-------------------------------------------------------------------------
- LGPArchive::~LGPArchive()
- {
- unload();
- }
- //-------------------------------------------------------------------------
- void
- LGPArchive::load()
- {
- //OGRE_LOCK_AUTO_MUTEX
- std::ifstream *ifs( OGRE_NEW_T( std::ifstream, Ogre::MEMCATEGORY_GENERAL )( mName.c_str(), std::ifstream::binary ) );
- load( OGRE_NEW Ogre::FileStreamDataStream( ifs ) );
- }
- //-------------------------------------------------------------------------
- void
- LGPArchive::load( Ogre::DataStream* lgp )
- {
- m_lgp_file.reset();
- m_lgp_file.bind( lgp );
- LGPArchiveSerializer lgp_archive_serializer;
- lgp_archive_serializer.importLGPArchive( m_lgp_file, this );
- FileList::const_iterator it( m_files.begin() ), it_end( m_files.end() );
- while( it != it_end )
- {
- Ogre::FileInfo file_info;
- file_info.archive = this;
- file_info.filename = it->file_name;
- StringUtil::splitFilename( it->file_name, file_info.basename, file_info.path );
- // TODO: This is actually still the compressed size!!
- file_info.uncompressedSize = it->data_size;
- file_info.compressedSize = file_info.uncompressedSize;
- //LOG_DEBUG( "add file:" + file_info.filename );
- m_file_infos.push_back( file_info );
- ++it;
- }
- // writing not implemented
- mReadOnly = true;
- }
- //-------------------------------------------------------------------------
- void
- LGPArchive::unload()
- {
- //OGRE_LOCK_AUTO_MUTEX
- m_files.clear();
- m_file_infos.clear();
- m_lgp_file.reset();
- }
- //-------------------------------------------------------------------------
- Ogre::DataStreamPtr
- LGPArchive::open( const String& filename, bool readOnly ) const
- {
- //LOG_DEBUG( "file:" + filename + " readOnly: " + Ogre::StringConverter::toString(readOnly) );
- Ogre::MemoryDataStream* buffer( nullptr );
- if (m_lgp_file != nullptr)
- {
- FileList::const_iterator it( m_files.begin() );
- FileList::const_iterator it_end( m_files.end() );
- while( it != it_end )
- {
- if( it->file_name == filename )
- {
- m_lgp_file->seek( it->dataoffset_ );
- size_t length( it->data_size );
- buffer = new Ogre::MemoryDataStream( length );
- m_lgp_file->read( buffer->getPtr(), length );
- break;
- }
- ++it;
- }
- }
- return Ogre::DataStreamPtr( buffer );
- }
- //-------------------------------------------------------------------------
- Ogre::DataStreamPtr
- LGPArchive::create( const String& filename ) const
- {
- OGRE_EXCEPT(Ogre::Exception::ERR_NOT_IMPLEMENTED,
- "Modification of lgp archives is not supported",
- "LGPArchive::create");
- }
- //-------------------------------------------------------------------------
- void
- LGPArchive::remove(const String& filename) const
- {
- OGRE_EXCEPT(Ogre::Exception::ERR_NOT_IMPLEMENTED,
- "Modification of lgp archives is not supported",
- "LGPArchive::remove");
- }
- //-------------------------------------------------------------------------
- Ogre::StringVectorPtr
- LGPArchive::list( bool recursive, bool dirs ) const
- {
- //OGRE_LOCK_AUTO_MUTEX
- Ogre::StringVector* file_names( OGRE_NEW_T( Ogre::StringVector, Ogre::MEMCATEGORY_GENERAL)() );
- FileList::const_iterator it( m_files.begin() );
- FileList::const_iterator it_end( m_files.end() );
- while( it != it_end )
- {
- file_names->push_back( it->file_name );
- ++it;
- }
- return Ogre::StringVectorPtr( file_names, Ogre::SPFM_DELETE_T );
- }
- //-------------------------------------------------------------------------
- Ogre::FileInfoListPtr
- LGPArchive::listFileInfo( bool recursive, bool dirs ) const
- {
- //OGRE_LOCK_AUTO_MUTEX
- return Ogre::FileInfoListPtr( OGRE_NEW_T( Ogre::FileInfoList, Ogre::MEMCATEGORY_GENERAL )( m_file_infos ), Ogre::SPFM_DELETE_T );
- }
- //-------------------------------------------------------------------------
- Ogre::StringVectorPtr
- LGPArchive::find( const String& pattern, bool recursive, bool dirs ) const
- {
- LOG_DEBUG( pattern );
- //OGRE_LOCK_AUTO_MUTEX
- Ogre::StringVector* file_names( OGRE_NEW_T( Ogre::StringVector, Ogre::MEMCATEGORY_GENERAL)() );
- Ogre::FileInfoListPtr found_infos( findFileInfo( pattern, recursive, dirs ) );
- Ogre::FileInfoList::const_iterator it( found_infos->begin() ), it_end( found_infos->end() );
- while( it != it_end )
- {
- file_names->push_back( it->filename );
- ++it;
- }
- return Ogre::StringVectorPtr( file_names, Ogre::SPFM_DELETE_T );
- }
- //-------------------------------------------------------------------------
- Ogre::FileInfoListPtr
- LGPArchive::findFileInfo( const String& pattern, bool recursive, bool dirs ) const
- {
- LOG_DEBUG( pattern );
- //OGRE_LOCK_AUTO_MUTEX
- Ogre::FileInfoList* file_infos( OGRE_NEW_T( Ogre::FileInfoList, Ogre::MEMCATEGORY_GENERAL)() );
- // If pattern contains a directory name, do a full match
- bool full_match = (pattern.find ('/') != String::npos) ||
- (pattern.find ('\\') != String::npos);
- Ogre::FileInfoList::const_iterator it( m_file_infos.begin() ), it_end( m_file_infos.end() );
- while( it != it_end )
- {
- const String& file_name( full_match ? it->filename : it->basename );
- if ( StringUtil::match( file_name, pattern, isCaseSensitive() ) )
- {
- file_infos->push_back( *it );
- }
- ++it;
- }
- return Ogre::FileInfoListPtr( file_infos, Ogre::SPFM_DELETE_T );
- }
- //-------------------------------------------------------------------------
- bool
- LGPArchive::exists( const String& filename ) const
- {
- FileList::const_iterator it( m_files.begin() );
- FileList::const_iterator it_end( m_files.end() );
- while( it != it_end )
- {
- if( it->file_name == filename )
- {
- LOG_DEBUG( "Found " + filename + " in " + mName );
- return true;
- }
- ++it;
- }
- LOG_DEBUG( "Couldn't find " + filename + " in " + mName );
- return false;
- }
- //-------------------------------------------------------------------------
- time_t
- LGPArchive::getModifiedTime( const String& filename ) const
- {
- struct stat tagStat;
- bool ret = (stat(mName.c_str(), &tagStat) == 0);
- if (ret)
- {
- return tagStat.st_mtime;
- }
- else
- {
- return 0;
- }
- }
- //-------------------------------------------------------------------------
- LGPArchive::FileList&
- LGPArchive::getFiles( void )
- {
- return m_files;
- }
- //-------------------------------------------------------------------------
- }
|