From d26c8460dcaee3f5e4515c83c68df1d58aef887e Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Mon, 8 Nov 2004 16:11:07 +0000 Subject: [PATCH] Updates to osgDB::Archive support, and refactoring of implementation of reading files in Registry to faciliate the new archiving support. --- examples/osgarchive/osgarchive.cpp | 21 +- include/osgDB/Registry | 26 +- src/osgDB/Archive.cpp | 44 +- src/osgDB/Registry.cpp | 853 ++++++----------------------- 4 files changed, 238 insertions(+), 706 deletions(-) diff --git a/examples/osgarchive/osgarchive.cpp b/examples/osgarchive/osgarchive.cpp index 9b33c45fa..82f387c91 100644 --- a/examples/osgarchive/osgarchive.cpp +++ b/examples/osgarchive/osgarchive.cpp @@ -16,8 +16,11 @@ #include #include #include +#include #include +#include + int main( int argc, char **argv ) { @@ -94,7 +97,23 @@ int main( int argc, char **argv ) { if (!arguments.isOption(pos)) { - files.push_back(arguments[pos]); + if (insert) + { + osgDB::FileType fileType = osgDB::fileType(arguments[pos]); + if (fileType==osgDB::REGULAR_FILE) + { + files.push_back(arguments[pos]); + } + else if (fileType==osgDB::DIRECTORY) + { + osgDB::DirectoryContents directory = osgDB::getDirectoryContents(arguments[pos]); + files.insert(files.end(),directory.begin(),directory.end()); + } + } + else + { + files.push_back(arguments[pos]); + } } } diff --git a/include/osgDB/Registry b/include/osgDB/Registry index af48b5353..2a5aa3443 100644 --- a/include/osgDB/Registry +++ b/include/osgDB/Registry @@ -228,9 +228,6 @@ class OSGDB_EXPORT Registry : public osg::Referenced - - - class WriteFileCallback : public osg::Referenced { public: @@ -398,6 +395,22 @@ class OSGDB_EXPORT Registry : public osg::Referenced /** Get the SharedStateManager. Return 0 if no SharedStateManager has been assigned.*/ SharedStateManager* getSharedStateManager() { return _sharedStateManager.get(); } + /** Functor used in internal implementations.*/ + struct ReadFunctor + { + ReadFunctor(const std::string& filename, const ReaderWriter::Options* options): + _filename(filename), + _options(options) {} + + virtual ~ReadFunctor() {} + virtual ReaderWriter::ReadResult doRead(ReaderWriter& rw) const = 0; + virtual bool isValid(ReaderWriter::ReadResult& readResult) const = 0; + virtual bool isValid(osg::Object* object) const = 0; + + std::string _filename; + const ReaderWriter::Options* _options; + }; + protected: virtual ~Registry(); @@ -423,11 +436,8 @@ class OSGDB_EXPORT Registry : public osg::Referenced void eraseWrapper(DotOsgWrapperMap& wrappermap,DotOsgWrapper* wrapper); - ReaderWriter::ReadResult openArchive(const std::string& fileName, ReaderWriter::ArchiveStatus status, unsigned int indexBlockSizeHint); - ReaderWriter::ReadResult readObject(const std::string& fileName); - ReaderWriter::ReadResult readImage(const std::string& fileName); - ReaderWriter::ReadResult readHeightField(const std::string& fileName); - ReaderWriter::ReadResult readNode(const std::string& fileName); + ReaderWriter::ReadResult read(const ReadFunctor& readFunctor); + ReaderWriter::ReadResult readImplementation(const ReadFunctor& readFunctor,CacheHintOptions useObjectCache); osg::ref_ptr _readFileCallback; osg::ref_ptr _writeFileCallback; diff --git a/src/osgDB/Archive.cpp b/src/osgDB/Archive.cpp index 7d585b8bc..6bb6fcda9 100644 --- a/src/osgDB/Archive.cpp +++ b/src/osgDB/Archive.cpp @@ -81,11 +81,11 @@ Archive::IndexBlock* Archive::IndexBlock::read(std::istream& in) } else { - osg::notify(osg::NOTICE)<<"Allocation Problem in Archive::IndexBlock::read(std::istream& in)"<(&_blockSize), sizeof(_blockSize)); out.write(reinterpret_cast(&_filePositionNextIndexBlock), sizeof(_filePositionNextIndexBlock)); @@ -141,7 +141,7 @@ void Archive::IndexBlock::write(std::ostream& out) out.write(reinterpret_cast(_data),_blockSize); - osg::notify(osg::NOTICE)<<"Archive::IndexBlock::write()"<getPositionNextIndexBlock()); } - osg::notify(osg::NOTICE)<<"Archive::open("<first)<<" pos="<<(int)((mitr->second).first)<<" size="<<(int)((mitr->second).second)<first)<<" pos="<<(int)((mitr->second).first)<<" size="<<(int)((mitr->second).second)<getReaderWriterForExtension(getLowerCaseFileExtension(fileName)); if (!rw) { - osg::notify(osg::NOTICE)<<"Archive::readObject(obj, "<second.first); diff --git a/src/osgDB/Registry.cpp b/src/osgDB/Registry.cpp index f2d105703..5942bf254 100644 --- a/src/osgDB/Registry.cpp +++ b/src/osgDB/Registry.cpp @@ -1115,15 +1115,110 @@ bool Registry::writeObject(const osg::Object& obj,Output& fw) } -// -// read object from specified file. -// -ReaderWriter::ReadResult Registry::openArchive(const std::string& fileName, ReaderWriter::ArchiveStatus status, unsigned int indexBlockSizeHint) + +struct ReadObjectFunctor : public Registry::ReadFunctor { - if (containsServerAddress(fileName)) + ReadObjectFunctor(const std::string& filename, const ReaderWriter::Options* options):ReadFunctor(filename,options) {} + + virtual ReaderWriter::ReadResult doRead(ReaderWriter& rw) const { return rw.readObject(_filename, _options); } + virtual bool isValid(ReaderWriter::ReadResult& readResult) const { return readResult.validObject(); } + virtual bool isValid(osg::Object* object) const { return object!=0; } +}; + +struct ReadImageFunctor : public Registry::ReadFunctor +{ + ReadImageFunctor(const std::string& filename, const ReaderWriter::Options* options):ReadFunctor(filename,options) {} + + virtual ReaderWriter::ReadResult doRead(ReaderWriter& rw)const { return rw.readImage(_filename, _options); } + virtual bool isValid(ReaderWriter::ReadResult& readResult) const { return readResult.validImage(); } + virtual bool isValid(osg::Object* object) const { return dynamic_cast(object)!=0; } +}; + +struct ReadHeightFieldFunctor : public Registry::ReadFunctor +{ + ReadHeightFieldFunctor(const std::string& filename, const ReaderWriter::Options* options):ReadFunctor(filename,options) {} + + virtual ReaderWriter::ReadResult doRead(ReaderWriter& rw) const { return rw.readHeightField(_filename, _options); } + virtual bool isValid(ReaderWriter::ReadResult& readResult) const { return readResult.validHeightField(); } + virtual bool isValid(osg::Object* object) const { return dynamic_cast(object)!=0; } +}; + +struct ReadNodeFunctor : public Registry::ReadFunctor +{ + ReadNodeFunctor(const std::string& filename, const ReaderWriter::Options* options):ReadFunctor(filename,options) {} + + virtual ReaderWriter::ReadResult doRead(ReaderWriter& rw) const { return rw.readNode(_filename, _options); } + virtual bool isValid(ReaderWriter::ReadResult& readResult) const { return readResult.validNode(); } + virtual bool isValid(osg::Object* object) const { return dynamic_cast(object)!=0; } + +}; + +struct ReadArchiveFunctor : public Registry::ReadFunctor +{ + ReadArchiveFunctor(const std::string& filename, ReaderWriter::ArchiveStatus status, unsigned int indexBlockSizeHint, const ReaderWriter::Options* options): + ReadFunctor(filename,options), + _status(status), + _indexBlockSizeHint(indexBlockSizeHint) {} + + ReaderWriter::ArchiveStatus _status; + unsigned int _indexBlockSizeHint; + + virtual ReaderWriter::ReadResult doRead(ReaderWriter& rw) const { return rw.openArchive(_filename, _status, _indexBlockSizeHint, _options); } + virtual bool isValid(ReaderWriter::ReadResult& readResult) const { return readResult.validArchive(); } + virtual bool isValid(osg::Object* object) const { return dynamic_cast(object)!=0; } + +}; + +ReaderWriter::ReadResult Registry::read(const ReadFunctor& readFunctor) +{ + std::string archiveName(".osga"); + + std::string::size_type positionArchive = readFunctor._filename.find(archiveName+'/'); + if (positionArchive==std::string::npos) positionArchive = readFunctor._filename.find(archiveName+'\\'); + if (positionArchive!=std::string::npos) { - std::string serverName = getServerAddress(fileName); - std::string serverFile = getServerFileName(fileName); + std::string archiveName(readFunctor._filename.substr(0,positionArchive+5)); + std::string fileName(readFunctor._filename.substr(positionArchive+6,std::string::npos)); + osg::notify(osg::INFO)<<"Contains archive : "< s_archive; + if (!s_archive) + { + s_archive = new Archive; + s_archive->open(archiveName,ReaderWriter::READ, 4096); + } + + osg::ref_ptr options = new ReaderWriter::Options; + options->setDatabasePath(archiveName); + + if (s_archive.valid()) + { + osg::notify(osg::INFO)<<" archive loaded"<readObject(fileName,options.get()); + } + else + { + osg::notify(osg::INFO)<<" no archive loaded"<openArchive(serverName+":"+serverFile, status, indexBlockSizeHint, _options.get()); + return readFunctor.doRead(*rw); } else { @@ -1156,8 +1251,8 @@ ReaderWriter::ReadResult Registry::openArchive(const std::string& fileName, Read AvailableReaderWriterIterator itr(_rwList); for(;itr.valid();++itr) { - ReaderWriter::ReadResult rr = itr->openArchive(fileName,status, indexBlockSizeHint, _options.get()); - if (rr.validArchive()) return rr; + ReaderWriter::ReadResult rr = readFunctor.doRead(*itr); + if (readFunctor.isValid(rr)) return rr; else results.push_back(rr); } @@ -1181,247 +1276,102 @@ ReaderWriter::ReadResult Registry::openArchive(const std::string& fileName, Read // we've come across a file not found or error in reading file. if (num_ERROR_IN_READING_FILE) { - osg::notify(osg::NOTICE)<<"Warning: error reading file \""<openArchive(fileName,status, indexBlockSizeHint, _options.get()); - if (rr.validArchive()) return rr; + ReaderWriter::ReadResult rr = readFunctor.doRead(*itr); + if (readFunctor.isValid(rr)) return rr; else results.push_back(rr); } } if (results.empty()) { - return ReaderWriter::ReadResult("Warning: Could not find plugin to read objects from file \""+fileName+"\"."); + return ReaderWriter::ReadResult("Warning: Could not find plugin to read objects from file \""+readFunctor._filename+"\"."); } return results.front(); } +ReaderWriter::ReadResult Registry::readImplementation(const ReadFunctor& readFunctor, CacheHintOptions useObjectCache) +{ + std::string file(readFunctor._filename); + + if (useObjectCache & CACHE_OBJECTS) + { + // search for entry in the object cache. + { + OpenThreads::ScopedLock lock(_objectCacheMutex); + ObjectCache::iterator oitr=_objectCache.find(file); + if (oitr!=_objectCache.end()) + { + notify(INFO)<<"returning cached instanced of "<second.first.get())) return ReaderWriter::ReadResult(oitr->second.first.get(), ReaderWriter::ReadResult::FILE_LOADED_FROM_CACHE); + else return ReaderWriter::ReadResult("Error file does not contain an osg::Object"); + } + } + + PushAndPopDataPath tmpfile(getFilePath(file)); + + ReaderWriter::ReadResult rr = read(readFunctor); + if (rr.validObject()) + { + // update cache with new entry. + notify(INFO)<<"Adding to object cache "< lock(_objectCacheMutex); + tmpObjectCache.swap(_objectCache); + } + + PushAndPopDataPath tmpfile(getFilePath(file)); + + ReaderWriter::ReadResult rr = read(readFunctor); + + { + OpenThreads::ScopedLock lock(_objectCacheMutex); + tmpObjectCache.swap(_objectCache); + } + + return rr; + } +} + + ReaderWriter::ReadResult Registry::openArchiveImplementation(const std::string& fileName, ReaderWriter::ArchiveStatus status, unsigned int indexBlockSizeHint, CacheHintOptions useObjectCache) { - std::string file(fileName); - - if (useObjectCache & CACHE_OBJECTS) - { - // search for entry in the object cache. - { - OpenThreads::ScopedLock lock(_objectCacheMutex); - ObjectCache::iterator oitr=_objectCache.find(file); - if (oitr!=_objectCache.end()) - { - notify(INFO)<<"returning cached instanced of "<(oitr->second.first.get()); - if (archive) return ReaderWriter::ReadResult(archive, ReaderWriter::ReadResult::FILE_LOADED_FROM_CACHE); - else return ReaderWriter::ReadResult("Error file does not contain an osg::Object"); - } - } - - PushAndPopDataPath tmpfile(getFilePath(file)); - - ReaderWriter::ReadResult rr = openArchive(file, status, indexBlockSizeHint); - if (rr.validObject()) - { - // update cache with new entry. - notify(INFO)<<"Adding to cache object "< lock(_objectCacheMutex); - tmpObjectCache.swap(_objectCache); - } - - PushAndPopDataPath tmpfile(getFilePath(file)); - - ReaderWriter::ReadResult rr = openArchive(file, status, indexBlockSizeHint); - - { - OpenThreads::ScopedLock lock(_objectCacheMutex); - tmpObjectCache.swap(_objectCache); - } - - return rr; - - } + return readImplementation(ReadArchiveFunctor(fileName, status, indexBlockSizeHint, _options.get()),useObjectCache); } -// -// read object from specified file. -// -ReaderWriter::ReadResult Registry::readObject(const std::string& fileName) +ReaderWriter::ReadResult Registry::readObjectImplementation(const std::string& fileName,CacheHintOptions useObjectCache) { - if (containsServerAddress(fileName)) - { - std::string serverName = getServerAddress(fileName); - std::string serverFile = getServerFileName(fileName); - osg::notify(osg::INFO)<<"Contains sever address : "<readObject(serverName+":"+serverFile,_options.get()); - } - else - { - return ReaderWriter::ReadResult("Warning: Could not find the .net plugin to read from server."); - } - } - - // record the errors reported by readerwriters. - typedef std::vector Results; - Results results; - - // first attempt to load the file from existing ReaderWriter's - AvailableReaderWriterIterator itr(_rwList); - for(;itr.valid();++itr) - { - ReaderWriter::ReadResult rr = itr->readObject(fileName,_options.get()); - if (rr.validObject()) return rr; - else results.push_back(rr); - } - - if (!results.empty()) - { - unsigned int num_FILE_NOT_HANDLED = 0; - unsigned int num_FILE_NOT_FOUND = 0; - unsigned int num_ERROR_IN_READING_FILE = 0; - - for(Results::iterator ritr=results.begin(); - ritr!=results.end(); - ++ritr) - { - if (ritr->status()==ReaderWriter::ReadResult::FILE_NOT_HANDLED) ++num_FILE_NOT_HANDLED; - else if (ritr->status()==ReaderWriter::ReadResult::FILE_NOT_FOUND) ++num_FILE_NOT_FOUND; - else if (ritr->status()==ReaderWriter::ReadResult::ERROR_IN_READING_FILE) ++num_ERROR_IN_READING_FILE; - } - - if (num_FILE_NOT_HANDLED!=results.size()) - { - // we've come across a file not found or error in reading file. - if (num_ERROR_IN_READING_FILE) - { - osg::notify(osg::NOTICE)<<"Warning: error reading file \""<readObject(fileName,_options.get()); - if (rr.validObject()) return rr; - else results.push_back(rr); - } - } - - if (results.empty()) - { - return ReaderWriter::ReadResult("Warning: Could not find plugin to read objects from file \""+fileName+"\"."); - } - - - return results.front(); -} - -ReaderWriter::ReadResult Registry::readObjectImplementation(const std::string& constFile,CacheHintOptions useObjectCache) -{ - std::string file(constFile); - - if (useObjectCache & CACHE_OBJECTS) - { - // search for entry in the object cache. - { - OpenThreads::ScopedLock lock(_objectCacheMutex); - ObjectCache::iterator oitr=_objectCache.find(file); - if (oitr!=_objectCache.end()) - { - notify(INFO)<<"returning cached instanced of "<second.first.get(); - if (object) return ReaderWriter::ReadResult(object, ReaderWriter::ReadResult::FILE_LOADED_FROM_CACHE); - else return ReaderWriter::ReadResult("Error file does not contain an osg::Object"); - } - } - - PushAndPopDataPath tmpfile(getFilePath(file)); - - ReaderWriter::ReadResult rr = readObject(file); - if (rr.validObject()) - { - // update cache with new entry. - notify(INFO)<<"Adding to cache object "< lock(_objectCacheMutex); - tmpObjectCache.swap(_objectCache); - } - - PushAndPopDataPath tmpfile(getFilePath(file)); - - ReaderWriter::ReadResult rr = readObject(file); - - { - OpenThreads::ScopedLock lock(_objectCacheMutex); - tmpObjectCache.swap(_objectCache); - } - - return rr; - - } + return readImplementation(ReadObjectFunctor(fileName, _options.get()),useObjectCache); } ReaderWriter::WriteResult Registry::writeObjectImplementation(const Object& obj,const std::string& fileName) @@ -1459,159 +1409,13 @@ ReaderWriter::WriteResult Registry::writeObjectImplementation(const Object& obj, return results.front(); } -ReaderWriter::ReadResult Registry::readImage(const std::string& fileName) + + +ReaderWriter::ReadResult Registry::readImageImplementation(const std::string& fileName,CacheHintOptions useObjectCache) { - if (containsServerAddress(fileName)) - { - std::string serverName = getServerAddress(fileName); - std::string serverFile = getServerFileName(fileName); - osg::notify(osg::INFO)<<"Contains sever address : "<readImage(serverName+":"+serverFile,_options.get()); - } - else - { - return ReaderWriter::ReadResult("Warning: Could not find the .net plugin to read from server."); - } - } - - - // record the errors reported by readerwriters. - typedef std::vector Results; - Results results; - - // first attempt to load the file from existing ReaderWriter's - AvailableReaderWriterIterator itr(_rwList); - for(;itr.valid();++itr) - { - ReaderWriter::ReadResult rr = itr->readImage(fileName,_options.get()); - if (rr.validImage()) return rr; - else results.push_back(rr); - } - - if (!results.empty()) - { - unsigned int num_FILE_NOT_HANDLED = 0; - unsigned int num_FILE_NOT_FOUND = 0; - unsigned int num_ERROR_IN_READING_FILE = 0; - - for(Results::iterator ritr=results.begin(); - ritr!=results.end(); - ++ritr) - { - if (ritr->status()==ReaderWriter::ReadResult::FILE_NOT_HANDLED) ++num_FILE_NOT_HANDLED; - else if (ritr->status()==ReaderWriter::ReadResult::FILE_NOT_FOUND) ++num_FILE_NOT_FOUND; - else if (ritr->status()==ReaderWriter::ReadResult::ERROR_IN_READING_FILE) ++num_ERROR_IN_READING_FILE; - } - - if (num_FILE_NOT_HANDLED!=results.size()) - { - // we've come across a file not found or error in reading file. - if (num_ERROR_IN_READING_FILE) - { - osg::notify(osg::NOTICE)<<"Warning: error reading file \""<readImage(fileName,_options.get()); - if (rr.validImage()) return rr; - else results.push_back(rr); - } - } - - if (results.empty()) - { - return ReaderWriter::ReadResult("Warning: Could not find plugin to read image from file \""+fileName+"\"."); - } - - return results.front(); + return readImplementation(ReadImageFunctor(fileName, _options.get()),useObjectCache); } - -ReaderWriter::ReadResult Registry::readImageImplementation(const std::string& file,CacheHintOptions useObjectCache) -{ - if (useObjectCache & CACHE_IMAGES) - { - // search for entry in the object cache. - { - OpenThreads::ScopedLock lock(_objectCacheMutex); - ObjectCache::iterator oitr=_objectCache.find(file); - if (oitr!=_objectCache.end()) - { - notify(INFO)<< "returning cached instanced of "<(oitr->second.first.get()); - if (image) return ReaderWriter::ReadResult(image, ReaderWriter::ReadResult::FILE_LOADED_FROM_CACHE); - else return ReaderWriter::ReadResult("Error file not of type osg::Image"); - } - } - - PushAndPopDataPath tmpfile(getFilePath(file)); - - ReaderWriter::ReadResult rr = readImage(file); - if (rr.validImage()) - { - // update cache with new entry. - notify(INFO)<<"Adding to cache image "< lock(_objectCacheMutex); - tmpObjectCache.swap(_objectCache); - } - - PushAndPopDataPath tmpfile(getFilePath(file)); - - ReaderWriter::ReadResult rr = readImage(file); - - { - OpenThreads::ScopedLock lock(_objectCacheMutex); - tmpObjectCache.swap(_objectCache); - } - - return rr; - - } -} - - - ReaderWriter::WriteResult Registry::writeImageImplementation(const Image& image,const std::string& fileName) { // record the errors reported by readerwriters. @@ -1647,158 +1451,12 @@ ReaderWriter::WriteResult Registry::writeImageImplementation(const Image& image, return results.front(); } -ReaderWriter::ReadResult Registry::readHeightField(const std::string& fileName) + +ReaderWriter::ReadResult Registry::readHeightFieldImplementation(const std::string& fileName,CacheHintOptions useObjectCache) { - if (containsServerAddress(fileName)) - { - std::string serverName = getServerAddress(fileName); - std::string serverFile = getServerFileName(fileName); - osg::notify(osg::INFO)<<"Contains sever address : "<readHeightField(serverName+":"+serverFile,_options.get()); - } - else - { - return ReaderWriter::ReadResult("Warning: Could not find the .net plugin to read from server."); - } - } - - // record the errors reported by readerwriters. - typedef std::vector Results; - Results results; - - // first attempt to load the file from existing ReaderWriter's - AvailableReaderWriterIterator itr(_rwList); - for(;itr.valid();++itr) - { - ReaderWriter::ReadResult rr = itr->readHeightField(fileName,_options.get()); - if (rr.validHeightField()) return rr; - else results.push_back(rr); - } - - if (!results.empty()) - { - unsigned int num_FILE_NOT_HANDLED = 0; - unsigned int num_FILE_NOT_FOUND = 0; - unsigned int num_ERROR_IN_READING_FILE = 0; - - for(Results::iterator ritr=results.begin(); - ritr!=results.end(); - ++ritr) - { - if (ritr->status()==ReaderWriter::ReadResult::FILE_NOT_HANDLED) ++num_FILE_NOT_HANDLED; - else if (ritr->status()==ReaderWriter::ReadResult::FILE_NOT_FOUND) ++num_FILE_NOT_FOUND; - else if (ritr->status()==ReaderWriter::ReadResult::ERROR_IN_READING_FILE) ++num_ERROR_IN_READING_FILE; - } - - if (num_FILE_NOT_HANDLED!=results.size()) - { - // we've come across a file not found or error in reading file. - if (num_ERROR_IN_READING_FILE) - { - osg::notify(osg::NOTICE)<<"Warning: error reading file \""<readHeightField(fileName,_options.get()); - if (rr.validHeightField()) return rr; - else results.push_back(rr); - } - } - - if (results.empty()) - { - return ReaderWriter::ReadResult("Warning: Could not find plugin to read HeightField from file \""+fileName+"\"."); - } - - return results.front(); + return readImplementation(ReadHeightFieldFunctor(fileName, _options.get()),useObjectCache); } - -ReaderWriter::ReadResult Registry::readHeightFieldImplementation(const std::string& file,CacheHintOptions useObjectCache) -{ - if (useObjectCache & CACHE_HEIGHTFIELDS) - { - // search for entry in the object cache. - { - OpenThreads::ScopedLock lock(_objectCacheMutex); - ObjectCache::iterator oitr=_objectCache.find(file); - if (oitr!=_objectCache.end()) - { - notify(INFO)<< "returning cached instanced of "<(oitr->second.first.get()); - if (heightField) return ReaderWriter::ReadResult(heightField, ReaderWriter::ReadResult::FILE_LOADED_FROM_CACHE); - else return ReaderWriter::ReadResult("Error file not of type osg::HeightField"); - } - } - - PushAndPopDataPath tmpfile(getFilePath(file)); - - ReaderWriter::ReadResult rr = readHeightField(file); - if (rr.validHeightField()) - { - // update cache with new entry. - notify(INFO)<<"Adding to cache HeightField "< lock(_objectCacheMutex); - tmpObjectCache.swap(_objectCache); - } - - PushAndPopDataPath tmpfile(getFilePath(file)); - - ReaderWriter::ReadResult rr = readHeightField(file); - - { - OpenThreads::ScopedLock lock(_objectCacheMutex); - tmpObjectCache.swap(_objectCache); - } - - return rr; - - } -} - - - ReaderWriter::WriteResult Registry::writeHeightFieldImplementation(const HeightField& HeightField,const std::string& fileName) { // record the errors reported by readerwriters. @@ -1835,164 +1493,9 @@ ReaderWriter::WriteResult Registry::writeHeightFieldImplementation(const HeightF } - -ReaderWriter::ReadResult Registry::readNode(const std::string& fileName) +ReaderWriter::ReadResult Registry::readNodeImplementation(const std::string& fileName,CacheHintOptions useObjectCache) { - - if (containsServerAddress(fileName)) - { - std::string serverName = getServerAddress(fileName); - std::string serverFile = getServerFileName(fileName); - osg::notify(osg::INFO)<<"Contains sever address : "<readNode(serverName+":"+serverFile,_options.get()); - } - else - { - return ReaderWriter::ReadResult("Warning: Could not find the .net plugin to read from server."); - } - } - - // record the errors reported by readerwriters. - typedef std::vector Results; - Results results; - - AvailableReaderWriterIterator itr(_rwList); - for(;itr.valid();++itr) - { - ReaderWriter::ReadResult rr = itr->readNode(fileName,_options.get()); - if (rr.validNode()) return rr; - else results.push_back(rr); - } - - if (!results.empty()) - { - unsigned int num_FILE_NOT_HANDLED = 0; - unsigned int num_FILE_NOT_FOUND = 0; - unsigned int num_ERROR_IN_READING_FILE = 0; - - for(Results::iterator ritr=results.begin(); - ritr!=results.end(); - ++ritr) - { - if (ritr->status()==ReaderWriter::ReadResult::FILE_NOT_HANDLED) ++num_FILE_NOT_HANDLED; - else if (ritr->status()==ReaderWriter::ReadResult::FILE_NOT_FOUND) ++num_FILE_NOT_FOUND; - else if (ritr->status()==ReaderWriter::ReadResult::ERROR_IN_READING_FILE) ++num_ERROR_IN_READING_FILE; - } - - if (num_FILE_NOT_HANDLED!=results.size()) - { - // we've come across a file not found or error in reading file. - if (num_ERROR_IN_READING_FILE) - { - osg::notify(osg::NOTICE)<<"Warning: error reading file \""<readNode(fileName,_options.get()); - if (rr.validNode()) return rr; - else results.push_back(rr); - } - } - - - // need to sort out. - CacheHintOptions useObjectCache = CACHE_ALL; - - if (_createNodeFromImage) - { - ReaderWriter::ReadResult rr = readImage(fileName,useObjectCache); - if (rr.validImage()) return createGeodeForImage(rr.takeImage()); - //else results.push_back(rr); - } - - if (results.empty()) - { - return ReaderWriter::ReadResult("Warning: Could not find plugin to read nodes from file \""+fileName+"\"."); - } - - return results.front(); -} - -ReaderWriter::ReadResult Registry::readNodeImplementation(const std::string& file,CacheHintOptions useObjectCache) -{ - if (useObjectCache & CACHE_NODES) - { - // search for entry in the object cache. - { - OpenThreads::ScopedLock lock(_objectCacheMutex); - ObjectCache::iterator oitr=_objectCache.find(file); - if (oitr!=_objectCache.end()) - { - notify(INFO)<< "returning cached instanced of "<(oitr->second.first.get()); - if (node) return ReaderWriter::ReadResult(node, ReaderWriter::ReadResult::FILE_LOADED_FROM_CACHE); - else return ReaderWriter::ReadResult("Error file not of type osg::Node"); - } - } - - PushAndPopDataPath tmpfile(getFilePath(file)); - - ReaderWriter::ReadResult rr = readNode(file); - if (rr.validNode()) - { - // update cache with new entry. - notify(INFO)<<"Adding to cache node "< lock(_objectCacheMutex); - tmpObjectCache.swap(_objectCache); - } - - PushAndPopDataPath tmpfile(getFilePath(file)); - - ReaderWriter::ReadResult rr = readNode(file); - - { - OpenThreads::ScopedLock lock(_objectCacheMutex); - tmpObjectCache.swap(_objectCache); - } - - return rr; - - } + return readImplementation(ReadNodeFunctor(fileName, _options.get()),useObjectCache); } ReaderWriter::WriteResult Registry::writeNodeImplementation(const Node& node,const std::string& fileName)