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:
ReaderWriterOSGA()12     ReaderWriterOSGA()
13     {
14         supportsExtension("osga","OpenSceneGraph Archive format");
15     }
16 
className() const17     virtual const char* className() const { return "OpenSceneGraph Archive Reader/Writer"; }
18 
openArchive(const std::string & file,ArchiveStatus status,unsigned int indexBlockSize=4096,const Options * options=NULL) const19     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     /** open an archive for reading.*/
openArchive(std::istream & fin,const Options *) const42     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 
readImage(const std::string & file,const Options * options) const53     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         // copy the incoming options if possible so that plugin options can be applied to files
61         // inside the archive
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             // register the archive so that it is cached for future use.
75             osgDB::Registry::instance()->addToArchiveCache(file, result.getArchive());
76         }
77 
78         return result_2;
79     }
80 
readNode(const std::string & file,const Options * options) const81     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         // copy the incoming options if possible so that plugin options can be applied to files
89         // inside the archive
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             // register the archive so that it is cached for future use.
103             osgDB::Registry::instance()->addToArchiveCache(file, result.getArchive());
104         }
105 
106         return result_2;
107     }
108 
109 protected:
110 
111 
112 };
113 
114 
115 // register with Registry to instantiate the above reader/writer.
116 REGISTER_OSGPLUGIN(osga, ReaderWriterOSGA)
117