| 1 | #include <osg/Notify> |
|---|
| 2 | |
|---|
| 3 | #include <osgDB/FileUtils> |
|---|
| 4 | #include <osgDB/FileNameUtils> |
|---|
| 5 | |
|---|
| 6 | #include "OSGA_Archive.h" |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | class ReaderWriterOSGA : public osgDB::ReaderWriter |
|---|
| 10 | { |
|---|
| 11 | public: |
|---|
| 12 | ReaderWriterOSGA() |
|---|
| 13 | { |
|---|
| 14 | supportsExtension("osga","OpenSceneGraph Archive format"); |
|---|
| 15 | } |
|---|
| 16 | |
|---|
| 17 | virtual const char* className() const { return "OpenSceneGraph Archive Reader/Writer"; } |
|---|
| 18 | |
|---|
| 19 | virtual ReadResult openArchive(const std::string& file,ArchiveStatus status, unsigned int indexBlockSize = 4096, const Options* options=NULL) const |
|---|
| 20 | { |
|---|
| 21 | |
|---|
| 22 | std::string ext = osgDB::getLowerCaseFileExtension(file); |
|---|
| 23 | if (!acceptsExtension(ext)) return ReadResult::FILE_NOT_HANDLED; |
|---|
| 24 | |
|---|
| 25 | std::string fileName = osgDB::findDataFile( file, options ); |
|---|
| 26 | if (fileName.empty()) |
|---|
| 27 | { |
|---|
| 28 | if (status==READ) return ReadResult::FILE_NOT_FOUND; |
|---|
| 29 | fileName = file; |
|---|
| 30 | } |
|---|
| 31 | |
|---|
| 32 | osg::ref_ptr<OSGA_Archive> archive = new OSGA_Archive; |
|---|
| 33 | if (!archive->open(fileName, status, indexBlockSize)) |
|---|
| 34 | { |
|---|
| 35 | return ReadResult(ReadResult::FILE_NOT_HANDLED); |
|---|
| 36 | } |
|---|
| 37 | |
|---|
| 38 | return archive.get(); |
|---|
| 39 | } |
|---|
| 40 | |
|---|
| 41 | |
|---|
| 42 | virtual ReadResult openArchive(std::istream& fin,const Options*) const |
|---|
| 43 | { |
|---|
| 44 | osg::ref_ptr<OSGA_Archive> archive = new OSGA_Archive; |
|---|
| 45 | if (!archive->open(fin)) |
|---|
| 46 | { |
|---|
| 47 | return ReadResult(ReadResult::FILE_NOT_HANDLED); |
|---|
| 48 | } |
|---|
| 49 | |
|---|
| 50 | return archive.get(); |
|---|
| 51 | } |
|---|
| 52 | |
|---|
| 53 | virtual ReadResult readImage(const std::string& file,const Options* options) const |
|---|
| 54 | { |
|---|
| 55 | ReadResult result = openArchive(file,osgDB::Archive::READ); |
|---|
| 56 | |
|---|
| 57 | if (!result.validArchive()) return result; |
|---|
| 58 | |
|---|
| 59 | |
|---|
| 60 | |
|---|
| 61 | |
|---|
| 62 | osg::ref_ptr<osgDB::ReaderWriter::Options> local_options = |
|---|
| 63 | options? |
|---|
| 64 | new osgDB::ReaderWriter::Options( *options ) : |
|---|
| 65 | new osgDB::ReaderWriter::Options; |
|---|
| 66 | |
|---|
| 67 | local_options->setDatabasePath(file); |
|---|
| 68 | |
|---|
| 69 | ReadResult result_2 = result.getArchive()->readImage(result.getArchive()->getMasterFileName(),local_options.get()); |
|---|
| 70 | |
|---|
| 71 | |
|---|
| 72 | if (!options || (options->getObjectCacheHint() & osgDB::ReaderWriter::Options::CACHE_ARCHIVES)) |
|---|
| 73 | { |
|---|
| 74 | |
|---|
| 75 | osgDB::Registry::instance()->addToArchiveCache(file, result.getArchive()); |
|---|
| 76 | } |
|---|
| 77 | |
|---|
| 78 | return result_2; |
|---|
| 79 | } |
|---|
| 80 | |
|---|
| 81 | virtual ReadResult readNode(const std::string& file,const Options* options) const |
|---|
| 82 | { |
|---|
| 83 | ReadResult result = openArchive(file,osgDB::Archive::READ); |
|---|
| 84 | |
|---|
| 85 | if (!result.validArchive()) return result; |
|---|
| 86 | |
|---|
| 87 | |
|---|
| 88 | |
|---|
| 89 | |
|---|
| 90 | osg::ref_ptr<osgDB::ReaderWriter::Options> local_options = |
|---|
| 91 | options? |
|---|
| 92 | new osgDB::ReaderWriter::Options( *options ) : |
|---|
| 93 | new osgDB::ReaderWriter::Options; |
|---|
| 94 | |
|---|
| 95 | local_options->setDatabasePath(file); |
|---|
| 96 | |
|---|
| 97 | ReadResult result_2 = result.getArchive()->readNode(result.getArchive()->getMasterFileName(),local_options.get()); |
|---|
| 98 | |
|---|
| 99 | |
|---|
| 100 | if (!options || (options->getObjectCacheHint() & osgDB::ReaderWriter::Options::CACHE_ARCHIVES)) |
|---|
| 101 | { |
|---|
| 102 | |
|---|
| 103 | osgDB::Registry::instance()->addToArchiveCache(file, result.getArchive()); |
|---|
| 104 | } |
|---|
| 105 | |
|---|
| 106 | return result_2; |
|---|
| 107 | } |
|---|
| 108 | |
|---|
| 109 | protected: |
|---|
| 110 | |
|---|
| 111 | |
|---|
| 112 | }; |
|---|
| 113 | |
|---|
| 114 | |
|---|
| 115 | |
|---|
| 116 | REGISTER_OSGPLUGIN(osga, ReaderWriterOSGA) |
|---|