Added readScript/writeScript methods to ReaderWriter
git-svn-id: http://svn.openscenegraph.org/osg/OpenSceneGraph/trunk@14366 16af8721-9629-0410-8352-f15c8da7e697
This commit is contained in:
@@ -64,6 +64,11 @@ ReaderWriter::ReadResult ReadFileCallback::readShader(const std::string& filenam
|
||||
return osgDB::Registry::instance()->readShaderImplementation(filename,options);
|
||||
}
|
||||
|
||||
ReaderWriter::ReadResult ReadFileCallback::readScript(const std::string& filename, const Options* options)
|
||||
{
|
||||
return osgDB::Registry::instance()->readScriptImplementation(filename,options);
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// WriteFileCallback default implementation
|
||||
@@ -92,3 +97,8 @@ ReaderWriter::WriteResult WriteFileCallback::writeShader(const osg::Shader& obj,
|
||||
{
|
||||
return osgDB::Registry::instance()->writeShaderImplementation(obj,fileName,options);
|
||||
}
|
||||
|
||||
ReaderWriter::WriteResult WriteFileCallback::writeScript(const osg::Script& obj, const std::string& fileName,const Options* options)
|
||||
{
|
||||
return osgDB::Registry::instance()->writeScriptImplementation(obj,fileName,options);
|
||||
}
|
||||
|
||||
@@ -250,6 +250,16 @@ Node* osgDB::readNodeFiles(osg::ArgumentParser& arguments,const Options* options
|
||||
|
||||
}
|
||||
|
||||
|
||||
Script* osgDB::readScriptFile(const std::string& filename,const Options* options)
|
||||
{
|
||||
ReaderWriter::ReadResult rr = Registry::instance()->readScript(filename,options);
|
||||
if (rr.validScript()) return rr.takeScript();
|
||||
if (rr.error()) OSG_WARN << rr.message() << std::endl;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
osg::ref_ptr<osg::Object> osgDB::readRefObjectFile(const std::string& filename,const Options* options)
|
||||
{
|
||||
ReaderWriter::ReadResult rr = Registry::instance()->readObject(filename,options);
|
||||
@@ -289,3 +299,11 @@ osg::ref_ptr<osg::Node> osgDB::readRefNodeFile(const std::string& filename,const
|
||||
if (rr.error()) OSG_WARN << rr.message() << std::endl;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
osg::ref_ptr<osg::Script> osgDB::readRefScriptFile(const std::string& filename,const Options* options)
|
||||
{
|
||||
ReaderWriter::ReadResult rr = Registry::instance()->readScript(filename,options);
|
||||
if (rr.validScript()) return osg::ref_ptr<osg::Script>(rr.getScript());
|
||||
if (rr.error()) OSG_WARN << rr.message() << std::endl;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@@ -25,6 +25,7 @@ osg::HeightField* ReaderWriter::ReadResult::getHeightField() { return dynamic_ca
|
||||
osg::Node* ReaderWriter::ReadResult::getNode() { return dynamic_cast<osg::Node*>(_object.get()); }
|
||||
osgDB::Archive* ReaderWriter::ReadResult::getArchive() { return dynamic_cast<osgDB::Archive*>(_object.get()); }
|
||||
osg::Shader* ReaderWriter::ReadResult::getShader() { return dynamic_cast<osg::Shader*>(_object.get()); }
|
||||
osg::Script* ReaderWriter::ReadResult::getScript() { return dynamic_cast<osg::Script*>(_object.get()); }
|
||||
|
||||
osg::Object* ReaderWriter::ReadResult::takeObject() { osg::Object* obj = _object.get(); if (obj) { obj->ref(); _object=NULL; obj->unref_nodelete(); } return obj; }
|
||||
osg::Image* ReaderWriter::ReadResult::takeImage() { osg::Image* image=dynamic_cast<osg::Image*>(_object.get()); if (image) { image->ref(); _object=NULL; image->unref_nodelete(); } return image; }
|
||||
@@ -32,6 +33,7 @@ osg::HeightField* ReaderWriter::ReadResult::takeHeightField() { osg::HeightField
|
||||
osg::Node* ReaderWriter::ReadResult::takeNode() { osg::Node* node=dynamic_cast<osg::Node*>(_object.get()); if (node) { node->ref(); _object=NULL; node->unref_nodelete(); } return node; }
|
||||
osgDB::Archive* ReaderWriter::ReadResult::takeArchive() { osgDB::Archive* archive=dynamic_cast<osgDB::Archive*>(_object.get()); if (archive) { archive->ref(); _object=NULL; archive->unref_nodelete(); } return archive; }
|
||||
osg::Shader* ReaderWriter::ReadResult::takeShader() { osg::Shader* shader=dynamic_cast<osg::Shader*>(_object.get()); if (shader) { shader->ref(); _object=NULL; shader->unref_nodelete(); } return shader; }
|
||||
osg::Script* ReaderWriter::ReadResult::takeScript() { osg::Script* script=dynamic_cast<osg::Script*>(_object.get()); if (script) { script->ref(); _object=NULL; script->unref_nodelete(); } return script; }
|
||||
|
||||
ReaderWriter::~ReaderWriter()
|
||||
{
|
||||
|
||||
@@ -970,6 +970,18 @@ struct Registry::ReadShaderFunctor : public Registry::ReadFunctor
|
||||
virtual ReadFunctor* cloneType(const std::string& filename, const Options* options) const { return new ReadShaderFunctor(filename, options); }
|
||||
};
|
||||
|
||||
struct Registry::ReadScriptFunctor : public Registry::ReadFunctor
|
||||
{
|
||||
ReadScriptFunctor(const std::string& filename, const Options* options):ReadFunctor(filename,options) {}
|
||||
|
||||
virtual ReaderWriter::ReadResult doRead(ReaderWriter& rw)const { return rw.readScript(_filename, _options); }
|
||||
virtual bool isValid(ReaderWriter::ReadResult& readResult) const { return readResult.validScript(); }
|
||||
virtual bool isValid(osg::Object* object) const { return dynamic_cast<osg::Script*>(object)!=0; }
|
||||
|
||||
virtual ReadFunctor* cloneType(const std::string& filename, const Options* options) const { return new ReadScriptFunctor(filename, options); }
|
||||
};
|
||||
|
||||
|
||||
void Registry::addArchiveExtension(const std::string ext)
|
||||
{
|
||||
for(ArchiveExtensionList::iterator aitr=_archiveExtList.begin();
|
||||
@@ -1590,6 +1602,60 @@ ReaderWriter::WriteResult Registry::writeShaderImplementation(const Shader& shad
|
||||
return result;
|
||||
}
|
||||
|
||||
ReaderWriter::ReadResult Registry::readScriptImplementation(const std::string& fileName,const Options* options)
|
||||
{
|
||||
return readImplementation(ReadScriptFunctor(fileName, options),Options::CACHE_IMAGES);
|
||||
}
|
||||
|
||||
ReaderWriter::WriteResult Registry::writeScriptImplementation(const Script& image,const std::string& fileName,const Options* options)
|
||||
{
|
||||
// record the errors reported by readerwriters.
|
||||
typedef std::vector<ReaderWriter::WriteResult> Results;
|
||||
Results results;
|
||||
|
||||
// first attempt to load the file from existing ReaderWriter's
|
||||
AvailableReaderWriterIterator itr(_rwList, _pluginMutex);
|
||||
for(;itr.valid();++itr)
|
||||
{
|
||||
ReaderWriter::WriteResult rr = itr->writeScript(image,fileName,options);
|
||||
if (rr.success()) return rr;
|
||||
else results.push_back(rr);
|
||||
}
|
||||
|
||||
// now look for a plug-in to save the file.
|
||||
std::string libraryName = createLibraryNameForFile(fileName);
|
||||
if (loadLibrary(libraryName)==LOADED)
|
||||
{
|
||||
for(;itr.valid();++itr)
|
||||
{
|
||||
ReaderWriter::WriteResult rr = itr->writeScript(image,fileName,options);
|
||||
if (rr.success()) return rr;
|
||||
else results.push_back(rr);
|
||||
}
|
||||
}
|
||||
|
||||
if (results.empty())
|
||||
{
|
||||
return ReaderWriter::WriteResult("Warning: Could not find plugin to write image to file \""+fileName+"\".");
|
||||
}
|
||||
|
||||
// sort the results so the most relevant (i.e. ERROR_IN_WRITING_FILE is more relevant than FILE_NOT_FOUND) results get placed at the end of the results list.
|
||||
std::sort(results.begin(), results.end());
|
||||
ReaderWriter::WriteResult result = results.back();
|
||||
|
||||
if (result.message().empty())
|
||||
{
|
||||
switch(result.status())
|
||||
{
|
||||
case(ReaderWriter::WriteResult::FILE_NOT_HANDLED): result.message() = "Warning: Write to \""+fileName+"\" not supported."; break;
|
||||
case(ReaderWriter::WriteResult::ERROR_IN_WRITING_FILE): result.message() = "Warning: Error in writing to \""+fileName+"\"."; break;
|
||||
default: break;
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
void Registry::addEntryToObjectCache(const std::string& filename, osg::Object* object, double timestamp)
|
||||
{
|
||||
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_objectCacheMutex);
|
||||
|
||||
@@ -61,3 +61,10 @@ bool osgDB::writeShaderFile(const Shader& shader,const std::string& filename, co
|
||||
return wr.success();
|
||||
}
|
||||
|
||||
bool osgDB::writeScriptFile(const Script& image,const std::string& filename, const Options* options )
|
||||
{
|
||||
ReaderWriter::WriteResult wr = Registry::instance()->writeScript( image, filename, options );
|
||||
if (wr.error()) OSG_WARN << "Error writing file " << filename << ": " << wr.message() << std::endl;
|
||||
return wr.success();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user