2015-10-22 22:14:53 +08:00
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
2005-03-13 09:47:46 +08:00
*
2015-10-22 22:14:53 +08:00
* This application is open source and may be redistributed and / or modified
2010-11-22 19:22:03 +08:00
* freely and without restriction , both in commercial and non commercial applications ,
2005-03-13 09:47:46 +08:00
* as long as this copyright notice is maintained .
2015-10-22 22:14:53 +08:00
*
2005-03-13 09:47:46 +08:00
* This application is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE .
*/
# include <osg/Timer>
# include <osg/ArgumentParser>
# include <osg/ApplicationUsage>
# include <osgDB/Archive>
# include <osgDB/ReadFile>
# include <osgDB/WriteFile>
# include <osgDB/FileUtils>
# include <iostream>
# include <algorithm>
int main ( int argc , char * * argv )
{
// use an ArgumentParser object to manage the program arguments.
osg : : ArgumentParser arguments ( & argc , argv ) ;
2015-10-22 22:14:53 +08:00
2005-03-13 09:47:46 +08:00
// set up the usage document, in case we need to print out how to use this program.
arguments . getApplicationUsage ( ) - > setApplicationName ( arguments . getApplicationName ( ) ) ;
2011-07-18 00:24:47 +08:00
arguments . getApplicationUsage ( ) - > setDescription ( arguments . getApplicationName ( ) + " is an application for collecting a set of separate files into a single archive file that can be later read in OSG applications.. " ) ;
2005-03-13 09:47:46 +08:00
arguments . getApplicationUsage ( ) - > setCommandLineUsage ( arguments . getApplicationName ( ) + " [options] filename ... " ) ;
2015-10-22 22:14:53 +08:00
2005-03-13 09:47:46 +08:00
// if user request help write it out to cout.
if ( arguments . read ( " -h " ) | | arguments . read ( " --help " ) )
{
arguments . getApplicationUsage ( ) - > write ( std : : cout ) ;
return 1 ;
}
std : : string archiveFilename ;
while ( arguments . read ( " -a " , archiveFilename ) | | arguments . read ( " --archive " , archiveFilename ) )
{
}
bool insert = false ;
while ( arguments . read ( " -i " ) | | arguments . read ( " --insert " ) )
{
insert = true ;
}
2015-10-22 22:14:53 +08:00
2005-03-13 09:47:46 +08:00
bool extract = false ;
while ( arguments . read ( " -e " ) | | arguments . read ( " --extract " ) )
{
extract = true ;
}
2015-10-22 22:14:53 +08:00
bool list = false ;
2005-03-13 09:47:46 +08:00
while ( arguments . read ( " -l " ) | | arguments . read ( " --list " ) )
{
list = true ;
}
typedef std : : vector < std : : string > FileNameList ;
FileNameList files ;
for ( int pos = 1 ; pos < arguments . argc ( ) ; + + pos )
{
if ( ! arguments . isOption ( pos ) )
{
if ( insert )
{
std : : string filePath = osgDB : : findDataFile ( arguments [ pos ] ) ;
osgDB : : FileType fileType = osgDB : : fileType ( filePath ) ;
if ( fileType = = osgDB : : REGULAR_FILE )
{
files . push_back ( arguments [ pos ] ) ;
}
else if ( fileType = = osgDB : : DIRECTORY )
{
osgDB : : DirectoryContents directory = osgDB : : getDirectoryContents ( arguments [ pos ] ) ;
2009-02-21 00:27:57 +08:00
osgDB : : DirectoryContents : : iterator it = directory . begin ( ) ;
while ( it ! = directory . end ( ) )
{
files . push_back ( filePath + " / " + ( * it ) ) ;
+ + it ;
}
2005-03-13 09:47:46 +08:00
}
}
else
{
files . push_back ( arguments [ pos ] ) ;
}
}
}
2015-10-22 22:14:53 +08:00
2005-03-13 09:47:46 +08:00
// any option left unread are converted into errors to write out later.
arguments . reportRemainingOptionsAsUnrecognized ( ) ;
2007-12-11 01:30:18 +08:00
// report any errors if they have occurred when parsing the program arguments.
2005-03-13 09:47:46 +08:00
if ( arguments . errors ( ) )
{
arguments . writeErrorMessages ( std : : cout ) ;
return 1 ;
}
2015-10-22 22:14:53 +08:00
2005-03-13 09:47:46 +08:00
if ( archiveFilename . empty ( ) )
{
std : : cout < < " Please specify an archive name using --archive filename " < < std : : endl ;
return 1 ;
}
if ( ! insert & & ! extract & & ! list )
{
std : : cout < < " Please specify an operation on the archive, either --insert, --extract or --list " < < std : : endl ;
return 1 ;
}
2015-10-22 22:14:53 +08:00
2005-03-13 09:47:46 +08:00
if ( insert & & extract )
{
std : : cout < < " Cannot insert and extract files from the archive at one time, please use either --insert or --extract. " < < std : : endl ;
return 1 ;
}
osg : : ref_ptr < osgDB : : Archive > archive ;
if ( insert )
{
archive = osgDB : : openArchive ( archiveFilename , osgDB : : Archive : : WRITE ) ;
2015-10-22 22:14:53 +08:00
2005-03-13 09:47:46 +08:00
if ( archive . valid ( ) )
{
for ( FileNameList : : iterator itr = files . begin ( ) ;
itr ! = files . end ( ) ;
+ + itr )
{
std : : cout < < " reading " < < * itr < < std : : endl ;
2015-10-22 22:14:53 +08:00
osg : : ref_ptr < osg : : Object > obj = osgDB : : readRefObjectFile ( * itr ) ;
2005-03-13 09:47:46 +08:00
if ( obj . valid ( ) )
{
std : : cout < < " write to archive " < < * itr < < std : : endl ;
2012-11-22 17:49:24 +08:00
osg : : Image * image = dynamic_cast < osg : : Image * > ( obj . get ( ) ) ;
osg : : HeightField * hf = dynamic_cast < osg : : HeightField * > ( obj . get ( ) ) ;
osg : : Node * node = dynamic_cast < osg : : Node * > ( obj . get ( ) ) ;
osg : : Shader * shader = dynamic_cast < osg : : Shader * > ( obj . get ( ) ) ;
if ( image ) archive - > writeImage ( * image , * itr ) ;
else if ( hf ) archive - > writeHeightField ( * hf , * itr ) ;
else if ( node ) archive - > writeNode ( * node , * itr ) ;
else if ( shader ) archive - > writeShader ( * shader , * itr ) ;
else archive - > writeObject ( * obj , * itr ) ;
2005-03-13 09:47:46 +08:00
}
}
}
}
2015-10-22 22:14:53 +08:00
else
2005-03-13 09:47:46 +08:00
{
archive = osgDB : : openArchive ( archiveFilename , osgDB : : Archive : : READ ) ;
2015-10-22 22:14:53 +08:00
2005-03-13 09:47:46 +08:00
if ( extract & & archive . valid ( ) )
{
for ( FileNameList : : iterator itr = files . begin ( ) ;
itr ! = files . end ( ) ;
+ + itr )
{
osg : : Timer_t start = osg : : Timer : : instance ( ) - > tick ( ) ;
osgDB : : ReaderWriter : : ReadResult result = archive - > readObject ( * itr ) ;
osg : : ref_ptr < osg : : Object > obj = result . getObject ( ) ;
std : : cout < < " readObejct time = " < < osg : : Timer : : instance ( ) - > delta_m ( start , osg : : Timer : : instance ( ) - > tick ( ) ) < < std : : endl ;
if ( obj . valid ( ) )
{
2008-01-29 04:27:33 +08:00
osgDB : : writeObjectFile ( * obj , * itr ) ;
2005-03-13 09:47:46 +08:00
}
}
}
}
if ( list & & archive . valid ( ) )
2015-10-22 22:14:53 +08:00
{
2005-03-13 09:47:46 +08:00
std : : cout < < " List of files in archive: " < < std : : endl ;
osgDB : : Archive : : FileNameList fileNames ;
if ( archive - > getFileNames ( fileNames ) )
{
for ( osgDB : : Archive : : FileNameList : : const_iterator itr = fileNames . begin ( ) ;
itr ! = fileNames . end ( ) ;
+ + itr )
{
std : : cout < < " " < < * itr < < std : : endl ;
}
}
2015-10-22 22:14:53 +08:00
2005-03-13 09:47:46 +08:00
std : : cout < < std : : endl ;
std : : cout < < " Master file " < < archive - > getMasterFileName ( ) < < std : : endl ;
}
2015-10-22 22:14:53 +08:00
2005-03-13 09:47:46 +08:00
return 0 ;
}