2006-07-18 23:21:48 +08:00
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
2003-01-22 00:45:36 +08:00
*
* This library is open source and may be redistributed and/or modified under
* the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
* (at your option) any later version. The full license is in LICENSE file
* included with this distribution, and on the openscenegraph.org website.
*
* This library 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. See the
* OpenSceneGraph Public License for more details.
*/
2001-10-04 23:12:57 +08:00
2001-09-20 05:08:56 +08:00
#ifndef OSGDB_REGISTRY
#define OSGDB_REGISTRY 1
2008-09-24 01:29:28 +08:00
#include <OpenThreads/ReentrantMutex>
2001-09-20 05:08:56 +08:00
#include <osg/ref_ptr>
2003-02-19 00:36:42 +08:00
#include <osg/ArgumentParser>
2008-07-04 23:57:48 +08:00
#include <osg/KdTree>
2001-09-20 05:08:56 +08:00
#include <osgDB/DynamicLibrary>
#include <osgDB/ReaderWriter>
2009-05-09 16:49:27 +08:00
#include <osgDB/Options>
2001-09-20 05:08:56 +08:00
#include <osgDB/DotOsgWrapper>
2004-01-07 05:18:36 +08:00
#include <osgDB/DatabasePager>
2008-10-21 00:24:57 +08:00
#include <osgDB/FileCache>
2001-09-20 05:08:56 +08:00
2002-06-18 05:50:37 +08:00
#include <vector>
#include <map>
#include <string>
2007-05-28 03:35:50 +08:00
extern "C"
{
typedef void (* CPluginFunction) (void);
}
2002-06-18 05:50:37 +08:00
2001-09-20 05:08:56 +08:00
namespace osgDB {
2003-07-22 02:36:47 +08:00
/** basic structure for custom runtime inheritance checking */
struct basic_type_wrapper {
2005-08-26 01:53:01 +08:00
virtual ~basic_type_wrapper() {}
2004-04-11 00:11:56 +08:00
virtual bool matches(const osg::Object *proto) const = 0;
2003-07-22 02:36:47 +08:00
};
/** a class template that checks inheritance between a given
Object's class and a class defined at compile time through
2004-04-11 00:11:56 +08:00
the template parameter T.
This is used in conjunction with readObjectOfType() to
specify an abstract class as reference type.
2003-07-22 02:36:47 +08:00
**/
template<class T>
struct type_wrapper: basic_type_wrapper {
2004-04-11 00:11:56 +08:00
bool matches(const osg::Object *proto) const
{
return dynamic_cast<const T*>(proto) != 0;
}
2003-07-22 02:36:47 +08:00
};
2001-12-19 08:38:23 +08:00
2002-06-18 05:50:37 +08:00
2001-09-20 05:08:56 +08:00
/**
Registry is a singleton factory which stores
2001-09-28 20:36:40 +08:00
the reader/writers which are linked in
2001-09-20 05:08:56 +08:00
at runtime for reading non-native file formats.
2001-09-28 20:36:40 +08:00
The RegisterDotOsgWrapperProxy can be used to automatically register
DotOsgWrappers, at runtime with the Registry. A DotOsgWrapper encapsulates
the functions that can read and write to the .osg for each osg::Object.
2001-09-20 05:08:56 +08:00
The RegisterReaderWriterProxy can be used to automatically
register at runtime a reader/writer with the Registry.
*/
2003-01-10 17:25:42 +08:00
class OSGDB_EXPORT Registry : public osg::Referenced
2001-09-20 05:08:56 +08:00
{
public:
2004-04-11 00:11:56 +08:00
2003-12-14 00:36:29 +08:00
static Registry* instance(bool erase = false);
2001-09-20 05:08:56 +08:00
2003-02-19 00:36:42 +08:00
/** read the command line arguments.*/
void readCommandLine(osg::ArgumentParser& commandLine);
2001-09-20 05:08:56 +08:00
/** register an .fileextension alias to mapExt toExt, the later
2005-08-26 01:53:01 +08:00
* should the the extension name of the readerwriter plugin library.
* For example to map .tif files to the tiff loader, use
* addExtAlias("tif","tiff") which will enable .tif to be read
* by the libdb_tiff readerwriter plugin.*/
void addFileExtensionAlias(const std::string mapExt, const std::string toExt);
2001-09-20 05:08:56 +08:00
2007-02-05 18:44:10 +08:00
/** Reads a file that configures extension mappings. File is ASCII text
2007-12-11 01:30:18 +08:00
* and each line contains the parameters to the addFileExtensionAlias
2007-02-05 18:44:10 +08:00
* method. Lines can be commented out with an initial '#' character.*/
bool readPluginAliasConfigurationFile( const std::string& file );
2009-04-09 22:00:16 +08:00
/** Registers a mapping of a mime-type to an extension. A process fetching data
* over HTTP can use this facility to determine the proper ReaderWriter to use
* when there is no filename extension to rely upon.
*/
void addMimeTypeExtensionMapping(const std::string fromMimeType, const std::string toExt);
2001-09-20 05:08:56 +08:00
void addDotOsgWrapper(DotOsgWrapper* wrapper);
void removeDotOsgWrapper(DotOsgWrapper* wrapper);
void addReaderWriter(ReaderWriter* rw);
void removeReaderWriter(ReaderWriter* rw);
/** create the platform specific library name associated with file.*/
std::string createLibraryNameForFile(const std::string& fileName);
/** create the platform specific library name associated with file extension.*/
2003-07-10 21:35:19 +08:00
std::string createLibraryNameForExtension(const std::string& ext);
2001-09-20 05:08:56 +08:00
2002-06-07 18:03:49 +08:00
/** create the platform specific library name associated with nodekit library name.*/
std::string createLibraryNameForNodeKit(const std::string& name);
2008-09-26 21:51:18 +08:00
enum LoadStatus {
NOT_LOADED = 0,
PREVIOUSLY_LOADED,
LOADED
};
2006-11-08 01:00:56 +08:00
/** find the library in the OSG_LIBRARY_PATH and load it.*/
2008-09-26 21:51:18 +08:00
LoadStatus loadLibrary(const std::string& fileName);
2006-11-08 01:00:56 +08:00
2001-09-20 05:08:56 +08:00
/** close the attached library with specified name.*/
bool closeLibrary(const std::string& fileName);
2006-11-08 01:00:56 +08:00
2003-12-14 00:36:29 +08:00
/** close all libraries.*/
void closeAllLibraries();
2001-09-20 05:08:56 +08:00
2008-07-15 17:55:33 +08:00
typedef std::vector< osg::ref_ptr<ReaderWriter> > ReaderWriterList;
2003-07-10 21:35:19 +08:00
/** get a reader writer which handles specified extension.*/
ReaderWriter* getReaderWriterForExtension(const std::string& ext);
2009-04-09 22:00:16 +08:00
/** gets a reader/writer that handles the extension mapped to by one of
* the registered mime-types. */
ReaderWriter* getReaderWriterForMimeType(const std::string& mimeType);
2008-07-15 17:55:33 +08:00
/** get list of all registered ReaderWriters.*/
ReaderWriterList& getReaderWriterList() { return _rwList; }
/** get const list of all registered ReaderWriters.*/
const ReaderWriterList& getReaderWriterList() const { return _rwList; }
2003-07-10 21:35:19 +08:00
2003-07-22 02:36:47 +08:00
osg::Object* readObjectOfType(const osg::Object& compObj,Input& fr);
osg::Object* readObjectOfType(const basic_type_wrapper &btw, Input& fr);
2001-09-20 05:08:56 +08:00
osg::Object* readObject(Input& fr);
osg::Image* readImage(Input& fr);
osg::Drawable* readDrawable(Input& fr);
2005-04-18 20:34:28 +08:00
osg::Uniform* readUniform(Input& fr);
2001-09-20 05:08:56 +08:00
osg::StateAttribute* readStateAttribute(Input& fr);
osg::Node* readNode(Input& fr);
2008-03-04 22:04:48 +08:00
osg::Shader* readShader(Input& fr);
2001-09-20 05:08:56 +08:00
bool writeObject(const osg::Object& obj,Output& fw);
2009-05-09 16:49:27 +08:00
typedef class osgDB::FindFileCallback FindFileCallback;
typedef class osgDB::ReadFileCallback ReadFileCallback;
typedef class osgDB::WriteFileCallback WriteFileCallback;
2009-05-11 19:39:12 +08:00
typedef class osgDB::FileLocationCallback FileLocationCallback;
2009-05-09 16:49:27 +08:00
/** Set the Registry callback to use in place of the default findFile calls.*/
void setFindFileCallback( FindFileCallback* cb) { _findFileCallback = cb; }
/** Get the findFile callback.*/
FindFileCallback* getFindFileCallback() { return _findFileCallback.get(); }
/** Get the const findFile callback.*/
const FindFileCallback* getFindFileCallback() const { return _findFileCallback.get(); }
std::string findDataFile(const std::string& fileName, const Options* options, CaseSensitivity caseSensitivity)
2004-04-11 00:11:56 +08:00
{
2009-05-09 16:49:27 +08:00
if (options && options->getFindFileCallback()) return options->getFindFileCallback()->findDataFile(fileName, options, caseSensitivity);
else if (_findFileCallback.valid()) return _findFileCallback->findDataFile(fileName, options, caseSensitivity);
else return findDataFileImplementation(fileName, options, caseSensitivity);
}
std::string findDataFileImplementation(const std::string& fileName, const Options* options, CaseSensitivity caseSensitivity);
std::string findLibraryFile(const std::string& fileName, const Options* options, CaseSensitivity caseSensitivity)
{
if (options && options->getFindFileCallback()) return options->getFindFileCallback()->findLibraryFile(fileName, options, caseSensitivity);
else if (_findFileCallback.valid()) return _findFileCallback->findLibraryFile(fileName, options, caseSensitivity);
else return findLibraryFileImplementation(fileName, options, caseSensitivity);
}
std::string findLibraryFileImplementation(const std::string& fileName, const Options* options, CaseSensitivity caseSensitivity);
2004-04-11 00:11:56 +08:00
2008-03-04 22:04:48 +08:00
2004-04-11 00:11:56 +08:00
/** Set the Registry callback to use in place of the default readFile calls.*/
void setReadFileCallback( ReadFileCallback* cb) { _readFileCallback = cb; }
/** Get the readFile callback.*/
ReadFileCallback* getReadFileCallback() { return _readFileCallback.get(); }
/** Get the const readFile callback.*/
const ReadFileCallback* getReadFileCallback() const { return _readFileCallback.get(); }
2001-09-20 05:08:56 +08:00
2003-10-29 19:11:17 +08:00
2009-05-09 16:49:27 +08:00
ReaderWriter::ReadResult openArchive(const std::string& fileName,ReaderWriter::ArchiveStatus status, unsigned int indexBlockSizeHint, const Options* options)
2004-11-08 05:17:31 +08:00
{
2009-05-09 16:49:27 +08:00
if (options && options->getReadFileCallback()) return options->getReadFileCallback()->openArchive(fileName, status, indexBlockSizeHint, options);
else if (_readFileCallback.valid()) return _readFileCallback->openArchive(fileName, status, indexBlockSizeHint, options);
2004-11-22 22:10:12 +08:00
else return openArchiveImplementation(fileName, status, indexBlockSizeHint, options);
2004-11-08 05:17:31 +08:00
}
2009-05-09 16:49:27 +08:00
ReaderWriter::ReadResult openArchiveImplementation(const std::string& fileName, ReaderWriter::ArchiveStatus status, unsigned int indexBlockSizeHint, const Options* options);
2004-11-08 05:17:31 +08:00
2009-05-09 16:49:27 +08:00
ReaderWriter::ReadResult readObject(const std::string& fileName,const Options* options, bool buildKdTreeIfRequired=true)
2004-04-11 00:11:56 +08:00
{
2009-05-09 16:49:27 +08:00
ReaderWriter::ReadResult result;
if (options && options->getReadFileCallback()) result = options->getReadFileCallback()->readObject(fileName,options);
else if (_readFileCallback.valid()) result = _readFileCallback->readObject(fileName,options);
else result = readObjectImplementation(fileName,options);
2008-10-21 00:24:57 +08:00
if (buildKdTreeIfRequired) _buildKdTreeIfRequired(result, options);
2009-05-09 16:49:27 +08:00
2008-07-04 23:57:48 +08:00
return result;
2004-04-11 00:11:56 +08:00
}
2009-05-09 16:49:27 +08:00
ReaderWriter::ReadResult readObjectImplementation(const std::string& fileName,const Options* options);
ReaderWriter::ReadResult readImage(const std::string& fileName,const Options* options)
2004-04-11 00:11:56 +08:00
{
2009-05-09 16:49:27 +08:00
if (options && options->getReadFileCallback()) return options->getReadFileCallback()->readImage(fileName,options);
else if (_readFileCallback.valid()) return _readFileCallback->readImage(fileName,options);
2004-11-22 22:10:12 +08:00
else return readImageImplementation(fileName,options);
2004-04-11 00:11:56 +08:00
}
2009-05-09 16:49:27 +08:00
ReaderWriter::ReadResult readImageImplementation(const std::string& fileName,const Options* options);
2004-04-11 00:11:56 +08:00
2009-05-09 16:49:27 +08:00
ReaderWriter::ReadResult readHeightField(const std::string& fileName,const Options* options)
2004-04-11 00:11:56 +08:00
{
2009-05-09 16:49:27 +08:00
if (options && options->getReadFileCallback()) return options->getReadFileCallback()->readHeightField(fileName,options);
else if (_readFileCallback.valid()) return _readFileCallback->readHeightField(fileName,options);
2004-11-22 22:10:12 +08:00
else return readHeightFieldImplementation(fileName,options);
2004-04-11 00:11:56 +08:00
}
2009-05-09 16:49:27 +08:00
ReaderWriter::ReadResult readHeightFieldImplementation(const std::string& fileName,const Options* options);
2004-04-11 00:11:56 +08:00
2009-05-09 16:49:27 +08:00
ReaderWriter::ReadResult readNode(const std::string& fileName,const Options* options, bool buildKdTreeIfRequired=true)
2004-04-11 00:11:56 +08:00
{
2009-05-09 16:49:27 +08:00
ReaderWriter::ReadResult result;
if (options && options->getReadFileCallback()) result = options->getReadFileCallback()->readNode(fileName,options);
else if (_readFileCallback.valid()) result = _readFileCallback->readNode(fileName,options);
else result = readNodeImplementation(fileName,options);
2008-10-21 00:24:57 +08:00
if (buildKdTreeIfRequired) _buildKdTreeIfRequired(result, options);
2009-05-09 16:49:27 +08:00
2008-07-04 23:57:48 +08:00
return result;
2004-04-11 00:11:56 +08:00
}
2009-05-09 16:49:27 +08:00
ReaderWriter::ReadResult readNodeImplementation(const std::string& fileName,const Options* options);
2004-04-11 00:11:56 +08:00
2009-05-09 16:49:27 +08:00
ReaderWriter::ReadResult readShader(const std::string& fileName,const Options* options)
2008-03-04 22:04:48 +08:00
{
2009-05-09 16:49:27 +08:00
if (options && options->getReadFileCallback()) return options->getReadFileCallback()->readShader(fileName,options);
2008-03-04 22:04:48 +08:00
if (_readFileCallback.valid()) return _readFileCallback->readShader(fileName,options);
else return readShaderImplementation(fileName,options);
}
2009-05-09 16:49:27 +08:00
ReaderWriter::ReadResult readShaderImplementation(const std::string& fileName,const Options* options);
2004-04-11 00:11:56 +08:00
2004-11-08 05:17:31 +08:00
2004-04-11 00:11:56 +08:00
/** Set the Registry callback to use in place of the default writeFile calls.*/
void setWriteFileCallback( WriteFileCallback* cb) { _writeFileCallback = cb; }
/** Get the writeFile callback.*/
WriteFileCallback* getWriteFileCallback() { return _writeFileCallback.get(); }
/** Get the const writeFile callback.*/
const WriteFileCallback* getWriteFileCallback() const { return _writeFileCallback.get(); }
2009-05-09 16:49:27 +08:00
ReaderWriter::WriteResult writeObject(const osg::Object& obj, const std::string& fileName,const Options* options)
2004-04-11 00:11:56 +08:00
{
2009-05-09 16:49:27 +08:00
if (options && options->getWriteFileCallback()) return options->getWriteFileCallback()->writeObject(obj,fileName,options);
else if (_writeFileCallback.valid()) return _writeFileCallback->writeObject(obj,fileName,options);
2007-10-01 03:53:02 +08:00
else return writeObjectImplementation(obj,fileName,options);
2004-04-11 00:11:56 +08:00
}
2009-05-09 16:49:27 +08:00
ReaderWriter::WriteResult writeObjectImplementation(const osg::Object& obj, const std::string& fileName,const Options* options);
2004-04-11 00:11:56 +08:00
2009-05-09 16:49:27 +08:00
ReaderWriter::WriteResult writeImage(const osg::Image& obj, const std::string& fileName,const Options* options)
2004-04-11 00:11:56 +08:00
{
2009-05-09 16:49:27 +08:00
if (options && options->getWriteFileCallback()) return options->getWriteFileCallback()->writeImage(obj,fileName,options);
else if (_writeFileCallback.valid()) return _writeFileCallback->writeImage(obj,fileName,options);
2007-10-01 03:53:02 +08:00
else return writeImageImplementation(obj,fileName,options);
2004-04-11 00:11:56 +08:00
}
2009-05-09 16:49:27 +08:00
ReaderWriter::WriteResult writeImageImplementation(const osg::Image& obj, const std::string& fileName,const Options* options);
2004-04-11 00:11:56 +08:00
2009-05-09 16:49:27 +08:00
ReaderWriter::WriteResult writeHeightField(const osg::HeightField& obj, const std::string& fileName,const Options* options)
2004-04-11 00:11:56 +08:00
{
2009-05-09 16:49:27 +08:00
if (options && options->getWriteFileCallback()) return options->getWriteFileCallback()->writeHeightField(obj,fileName,options);
else if (_writeFileCallback.valid()) return _writeFileCallback->writeHeightField(obj,fileName,options);
2007-10-01 03:53:02 +08:00
else return writeHeightFieldImplementation(obj,fileName,options);
2004-04-11 00:11:56 +08:00
}
2009-05-09 16:49:27 +08:00
ReaderWriter::WriteResult writeHeightFieldImplementation(const osg::HeightField& obj, const std::string& fileName,const Options* options);
2004-04-11 00:11:56 +08:00
2009-05-09 16:49:27 +08:00
ReaderWriter::WriteResult writeNode(const osg::Node& node, const std::string& fileName,const Options* options)
2004-04-11 00:11:56 +08:00
{
2009-05-09 16:49:27 +08:00
if (options && options->getWriteFileCallback()) return options->getWriteFileCallback()->writeNode(node,fileName,options);
else if (_writeFileCallback.valid()) return _writeFileCallback->writeNode(node,fileName,options);
2007-10-01 03:53:02 +08:00
else return writeNodeImplementation(node,fileName,options);
2004-04-11 00:11:56 +08:00
}
2009-05-09 16:49:27 +08:00
ReaderWriter::WriteResult writeNodeImplementation(const osg::Node& node, const std::string& fileName,const Options* options);
2004-04-11 00:11:56 +08:00
2009-05-09 16:49:27 +08:00
ReaderWriter::WriteResult writeShader(const osg::Shader& obj, const std::string& fileName,const Options* options)
2008-03-04 22:04:48 +08:00
{
2009-05-09 16:49:27 +08:00
if (options && options->getWriteFileCallback()) return options->getWriteFileCallback()->writeShader(obj,fileName,options);
else if (_writeFileCallback.valid()) return _writeFileCallback->writeShader(obj,fileName,options);
2008-03-04 22:04:48 +08:00
else return writeShaderImplementation(obj,fileName,options);
}
2009-05-09 16:49:27 +08:00
ReaderWriter::WriteResult writeShaderImplementation(const osg::Shader& obj, const std::string& fileName,const Options* options);
2001-09-20 05:08:56 +08:00
2004-11-08 05:17:31 +08:00
2009-05-09 16:49:27 +08:00
inline void _buildKdTreeIfRequired(ReaderWriter::ReadResult& result, const Options* options)
2008-07-04 23:57:48 +08:00
{
2009-05-09 16:49:27 +08:00
bool doKdTreeBuilder = (options && options->getBuildKdTreesHint()!=Options::NO_PREFERENCE) ?
options->getBuildKdTreesHint() == Options::BUILD_KDTREES :
_buildKdTreesHint == Options::BUILD_KDTREES;
2008-07-04 23:57:48 +08:00
if (doKdTreeBuilder && _kdTreeBuilder.valid() && result.validNode())
{
2008-07-07 02:27:10 +08:00
osg::ref_ptr<osg::KdTreeBuilder> builder = _kdTreeBuilder->clone();
result.getNode()->accept(*builder);
2008-07-04 23:57:48 +08:00
}
}
2009-05-18 18:28:14 +08:00
/** Set the callback to use inform the DatabasePager whether a file is located on local or remote file system.*/
2009-05-11 19:39:12 +08:00
void setFileLocationCallback( FileLocationCallback* cb) { _fileLocationCallback = cb; }
2009-05-18 18:28:14 +08:00
/** Get the callback to use inform the DatabasePager whether a file is located on local or remote file system.*/
2009-05-11 19:39:12 +08:00
FileLocationCallback* getFileLocationCallback() const { return _fileLocationCallback.get(); }
2008-07-04 23:57:48 +08:00
2008-07-17 19:55:55 +08:00
/** Set whether the KdTrees should be built for geometry in the loader model. */
2009-05-09 16:49:27 +08:00
void setBuildKdTreesHint(Options::BuildKdTreesHint hint) { _buildKdTreesHint = hint; }
2008-07-17 19:55:55 +08:00
/** Get whether the KdTrees should be built for geometry in the loader model. */
2009-05-09 16:49:27 +08:00
Options::BuildKdTreesHint getBuildKdTreesHint() const { return _buildKdTreesHint; }
2008-07-04 23:57:48 +08:00
2008-07-17 19:55:55 +08:00
/** Set the KdTreeBuilder visitor that is used to build KdTree on loaded models.*/
2008-07-04 23:57:48 +08:00
void setKdTreeBuilder(osg::KdTreeBuilder* builder) { _kdTreeBuilder = builder; }
2008-07-17 19:55:55 +08:00
/** Get the KdTreeBuilder visitor that is used to build KdTree on loaded models.*/
2008-07-04 23:57:48 +08:00
osg::KdTreeBuilder* getKdTreeBuilder() { return _kdTreeBuilder.get(); }
2009-05-11 19:39:12 +08:00
2008-10-21 00:24:57 +08:00
/** Set the FileCache that is used to manage local storage of files downloaded from the internet.*/
void setFileCache(FileCache* fileCache) { _fileCache = fileCache; }
/** Get the FileCache that is used to manage local storage of files downloaded from the internet.*/
FileCache* getFileCache() { return _fileCache.get(); }
/** Get the const FileCache that is used to manage local storage of files downloaded from the internet.*/
const FileCache* getFileCache() const { return _fileCache.get(); }
2008-07-04 23:57:48 +08:00
2008-07-17 19:55:55 +08:00
/** Set the password map to be used by plugins when access files from secure locations.*/
void setAuthenticationMap(AuthenticationMap* authenticationMap) { _authenticationMap = authenticationMap; }
2008-07-22 01:27:59 +08:00
/** Get the password map to be used by plugins when access files from secure locations.*/
AuthenticationMap* getAuthenticationMap() { return _authenticationMap.get(); }
2008-07-17 19:55:55 +08:00
/** Get the password map to be used by plugins when access files from secure locations.*/
const AuthenticationMap* getAuthenticationMap() const { return _authenticationMap.get(); }
2001-09-20 05:08:56 +08:00
void setCreateNodeFromImage(bool flag) { _createNodeFromImage = flag; }
bool getCreateNodeFromImage() const { return _createNodeFromImage; }
2008-07-17 19:55:55 +08:00
2004-04-11 00:11:56 +08:00
2009-05-09 16:49:27 +08:00
void setOptions(Options* opt) { _options = opt; }
Options* getOptions() { return _options.get(); }
const Options* getOptions() const { return _options.get(); }
2001-10-15 01:54:25 +08:00
2007-12-11 01:30:18 +08:00
/** initialize both the Data and Library FilePaths, by default called by the
2002-06-18 05:50:37 +08:00
* constructor, so it should only be required if you want to force
* the re-reading of environmental variables.*/
void initFilePathLists() { initDataFilePathList(); initLibraryFilePathList(); }
2007-12-11 01:30:18 +08:00
/** initialize the Data FilePath by reading the OSG_FILE_PATH environmental variable.*/
2002-06-18 05:50:37 +08:00
void initDataFilePathList();
/** Set the data file path using a list of paths stored in a FilePath, which is used when search for data files.*/
void setDataFilePathList(const FilePathList& filepath) { _dataFilePath = filepath; }
2007-12-11 01:30:18 +08:00
/** Set the data file path using a single string delimited either with ';' (Windows) or ':' (All other platforms), which is used when search for data files.*/
2004-08-28 00:14:21 +08:00
void setDataFilePathList(const std::string& paths);
2002-06-18 05:50:37 +08:00
/** get the data file path which is used when search for data files.*/
FilePathList& getDataFilePathList() { return _dataFilePath; }
/** get the const data file path which is used when search for data files.*/
const FilePathList& getDataFilePathList() const { return _dataFilePath; }
2007-12-11 01:30:18 +08:00
/** initialize the Library FilePath by reading the OSG_LIBRARY_PATH
2002-06-18 05:50:37 +08:00
* and the appropriate system environmental variables*/
void initLibraryFilePathList();
/** Set the library file path using a list of paths stored in a FilePath, which is used when search for data files.*/
void setLibraryFilePathList(const FilePathList& filepath) { _libraryFilePath = filepath; }
2007-12-11 01:30:18 +08:00
/** Set the library file path using a single string delimited either with ';' (Windows) or ':' (All other platforms), which is used when search for data files.*/
2004-08-28 00:14:21 +08:00
void setLibraryFilePathList(const std::string& paths);
2002-06-18 05:50:37 +08:00
/** get the library file path which is used when search for library (dso/dll's) files.*/
FilePathList& getLibraryFilePathList() { return _libraryFilePath; }
/** get the const library file path which is used when search for library (dso/dll's) files.*/
const FilePathList& getLibraryFilePathList() const { return _libraryFilePath; }
2003-03-18 06:53:46 +08:00
/** For each object in the cache which has an reference count greater than 1
* (and therefore referenced by elsewhere in the application) set the time stamp
* for that object in the cache to specified time.
* This would typically be called once per frame by applications which are doing database paging,
* and need to prune objects that are no longer required.
2009-08-05 19:06:53 +08:00
* The time used is taken from the FrameStamp::getReferenceTime().*/
void updateTimeStampOfObjectsInCacheWithExternalReferences(const osg::FrameStamp& frameStamp);
2003-03-18 06:53:46 +08:00
/** Removed object in the cache which have a time stamp at or before the specified expiry time.
* This would typically be called once per frame by applications which are doing database paging,
* and need to prune objects that are no longer required, and called after the a called
2009-08-05 19:06:53 +08:00
* after the call to updateTimeStampOfObjectsInCacheWithExternalReferences(frameStamp).*/
void removeExpiredObjectsInCache(const osg::FrameStamp& frameStamp);
/** set hint to viewer code calling removeExpiredObjectsInCache to specify how long it should give before expiring objects in Registry cache,*/
void setExpiryDelay(double expiryDelay) { _expiryDelay = expiryDelay; }
double getExpiryDelay() const { return _expiryDelay; }
2003-03-18 06:53:46 +08:00
/** Remove all objects in the cache regardless of having external references or expiry times.*/
void clearObjectCache();
2007-12-11 01:30:18 +08:00
/** Add a filename,object,timestamp triple to the Registry::ObjectCache.*/
2003-10-01 21:12:25 +08:00
void addEntryToObjectCache(const std::string& filename, osg::Object* object, double timestamp = 0.0);
2005-03-07 22:06:09 +08:00
2005-08-26 01:53:01 +08:00
/** Get an object from the object cache*/
osg::Object* getFromObjectCache(const std::string& fileName);
2003-10-02 21:26:01 +08:00
2004-11-11 00:40:08 +08:00
/** Add archive to archive cache so that future calls reference this archive.*/
void addToArchiveCache(const std::string& fileName, osgDB::Archive* archive);
/** Remove archive from cache.*/
void removeFromArchiveCache(const std::string& fileName);
2004-01-07 05:18:36 +08:00
2004-11-11 00:40:08 +08:00
/** Get an archive from the archive cache*/
osgDB::Archive* getFromArchiveCache(const std::string& fileName);
/** Remove all archives from the archive cache.*/
void clearArchiveCache();
2005-05-08 04:47:09 +08:00
/** If State is non-zero, this function releases OpenGL objects for
* the specified graphics context. Otherwise, releases OpenGL objexts
* for all graphics contexts. */
void releaseGLObjects(osg::State* state=0);
2004-11-11 00:40:08 +08:00
/** get the attached library with specified name.*/
DynamicLibrary* getLibrary(const std::string& fileName);
2003-12-10 23:24:14 +08:00
2004-01-11 01:13:20 +08:00
/** Set the SharedStateManager.*/
void setSharedStateManager(SharedStateManager* SharedStateManager) { _sharedStateManager = SharedStateManager; }
/** Get the SharedStateManager, creating one if one is not already created.*/
SharedStateManager* getOrCreateSharedStateManager();
/** Get the SharedStateManager. Return 0 if no SharedStateManager has been assigned.*/
SharedStateManager* getSharedStateManager() { return _sharedStateManager.get(); }
2007-05-09 17:43:18 +08:00
/** Add an Archive extension.*/
void addArchiveExtension(const std::string ext);
2009-03-10 20:21:13 +08:00
/** registers a protocol */
void registerProtocol(const std::string& protocol);
/** returns true, if named protocol is registered */
bool isProtocolRegistered(const std::string& protocol);
2003-12-14 00:36:29 +08:00
protected:
2003-01-10 17:25:42 +08:00
virtual ~Registry();
2001-09-20 05:08:56 +08:00
2003-03-18 06:53:46 +08:00
typedef std::map< std::string, osg::ref_ptr<DotOsgWrapper> > DotOsgWrapperMap;
typedef std::vector< osg::ref_ptr<DynamicLibrary> > DynamicLibraryList;
typedef std::map< std::string, std::string> ExtensionAliasMap;
2009-04-09 22:00:16 +08:00
typedef std::map< std::string, std::string> MimeTypeExtensionMap;
2007-05-09 17:43:18 +08:00
typedef std::vector< std::string> ArchiveExtensionList;
2003-03-18 06:53:46 +08:00
typedef std::pair<osg::ref_ptr<osg::Object>, double > ObjectTimeStampPair;
typedef std::map<std::string, ObjectTimeStampPair > ObjectCache;
2004-11-11 00:40:08 +08:00
typedef std::map<std::string, osg::ref_ptr<osgDB::Archive> > ArchiveCache;
2009-03-10 20:21:13 +08:00
typedef std::set<std::string> RegisteredProtocolsSet;
2001-09-20 05:08:56 +08:00
/** constructor is private, as its a singleton, preventing
construction other than via the instance() method and
therefore ensuring only one copy is ever constructed*/
Registry();
2009-05-11 19:39:12 +08:00
2001-09-20 05:08:56 +08:00
/** get the attached library with specified name.*/
DynamicLibraryList::iterator getLibraryItr(const std::string& fileName);
2009-05-09 16:49:27 +08:00
Options::BuildKdTreesHint _buildKdTreesHint;
2008-07-04 23:57:48 +08:00
osg::ref_ptr<osg::KdTreeBuilder> _kdTreeBuilder;
2008-10-21 00:24:57 +08:00
osg::ref_ptr<FileCache> _fileCache;
2008-07-17 19:55:55 +08:00
osg::ref_ptr<AuthenticationMap> _authenticationMap;
2008-07-04 23:57:48 +08:00
bool _createNodeFromImage;
2009-03-10 20:21:13 +08:00
RegisteredProtocolsSet _registeredProtocols;
2001-09-20 05:08:56 +08:00
osg::Object* readObject(DotOsgWrapperMap& dowMap,Input& fr);
2002-06-07 18:03:49 +08:00
void eraseWrapper(DotOsgWrapperMap& wrappermap,DotOsgWrapper* wrapper);
2004-11-29 11:05:27 +08:00
public:
2004-11-10 21:47:22 +08:00
/** Functor used in internal implementations.*/
struct ReadFunctor
{
2009-05-09 16:49:27 +08:00
ReadFunctor(const std::string& filename, const Options* options):
2004-11-10 21:47:22 +08:00
_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;
2009-05-09 16:49:27 +08:00
const Options* _options;
2004-11-10 21:47:22 +08:00
};
2004-11-29 11:05:27 +08:00
protected:
2007-06-07 19:19:02 +08:00
void destruct();
2004-11-11 00:40:08 +08:00
// forward declare helper classes
2004-11-10 21:47:22 +08:00
struct ReadObjectFunctor;
struct ReadImageFunctor;
struct ReadHeightFieldFunctor;
struct ReadNodeFunctor;
struct ReadArchiveFunctor;
2008-03-04 22:04:48 +08:00
struct ReadShaderFunctor;
2004-11-16 22:10:30 +08:00
// make helper classes friends to get round VS6.0 "issues"
2004-11-16 23:36:06 +08:00
friend struct ReadFunctor;
2004-11-17 00:04:05 +08:00
friend struct ReadObjectFunctor;
2004-11-16 22:10:30 +08:00
friend struct ReadImageFunctor;
friend struct ReadHeightFieldFunctor;
friend struct ReadNodeFunctor;
friend struct ReadArchiveFunctor;
2008-03-04 22:04:48 +08:00
friend struct ReadShaderFunctor;
2004-11-10 21:47:22 +08:00
2004-11-09 00:11:07 +08:00
ReaderWriter::ReadResult read(const ReadFunctor& readFunctor);
2009-05-09 16:49:27 +08:00
ReaderWriter::ReadResult readImplementation(const ReadFunctor& readFunctor,Options::CacheHintOptions cacheHint);
2004-11-11 00:40:08 +08:00
2007-12-11 01:30:18 +08:00
// forward declare helper class
2004-11-11 00:40:08 +08:00
class AvailableReaderWriterIterator;
2004-11-16 22:10:30 +08:00
friend class AvailableReaderWriterIterator;
2004-11-11 00:40:08 +08:00
2002-06-07 18:03:49 +08:00
2009-05-09 16:49:27 +08:00
osg::ref_ptr<FindFileCallback> _findFileCallback;
2004-04-11 00:11:56 +08:00
osg::ref_ptr<ReadFileCallback> _readFileCallback;
osg::ref_ptr<WriteFileCallback> _writeFileCallback;
2009-05-11 19:39:12 +08:00
osg::ref_ptr<FileLocationCallback> _fileLocationCallback;
2004-04-11 00:11:56 +08:00
2001-09-20 05:08:56 +08:00
DotOsgWrapperMap _objectWrapperMap;
DotOsgWrapperMap _imageWrapperMap;
DotOsgWrapperMap _drawableWrapperMap;
DotOsgWrapperMap _stateAttrWrapperMap;
2005-04-18 20:34:28 +08:00
DotOsgWrapperMap _uniformWrapperMap;
2001-09-20 05:08:56 +08:00
DotOsgWrapperMap _nodeWrapperMap;
2008-03-04 22:04:48 +08:00
DotOsgWrapperMap _shaderWrapperMap;
2001-09-20 05:08:56 +08:00
DotOsgWrapperMap _classNameWrapperMap;
2008-09-24 01:29:28 +08:00
OpenThreads::ReentrantMutex _pluginMutex;
ReaderWriterList _rwList;
DynamicLibraryList _dlList;
2001-09-20 05:08:56 +08:00
bool _openingLibrary;
2004-04-11 00:11:56 +08:00
// map to alias to extensions to plugins.
ExtensionAliasMap _extAliasMap;
2007-02-05 18:44:10 +08:00
2009-04-09 22:00:16 +08:00
// maps mime-types to extensions.
MimeTypeExtensionMap _mimeTypeExtMap;
2007-02-05 18:44:10 +08:00
// Utility: Removes whitespace from both ends of a string.
static std::string trim( const std::string& str );
2001-10-15 01:54:25 +08:00
// options to pass to reader writers.
2009-05-09 16:49:27 +08:00
osg::ref_ptr<Options> _options;
2002-06-18 05:50:37 +08:00
2004-11-22 22:10:12 +08:00
FilePathList _dataFilePath;
FilePathList _libraryFilePath;
2003-10-02 21:26:01 +08:00
2009-08-05 19:06:53 +08:00
double _expiryDelay;
2004-11-22 22:10:12 +08:00
ObjectCache _objectCache;
OpenThreads::Mutex _objectCacheMutex;
2004-01-07 05:18:36 +08:00
2004-11-22 22:10:12 +08:00
ArchiveCache _archiveCache;
OpenThreads::Mutex _archiveCacheMutex;
2007-05-09 17:43:18 +08:00
ArchiveExtensionList _archiveExtList;
2004-11-11 00:40:08 +08:00
2004-11-22 22:10:12 +08:00
osg::ref_ptr<SharedStateManager> _sharedStateManager;
2001-09-20 05:08:56 +08:00
};
2003-02-19 00:36:42 +08:00
/** read the command line arguments.*/
inline void readCommandLine(osg::ArgumentParser& parser)
{
Registry::instance()->readCommandLine(parser);
}
2001-09-20 05:08:56 +08:00
/** Proxy class for automatic registration of DotOsgWrappers with the Registry.*/
class RegisterDotOsgWrapperProxy
{
public:
RegisterDotOsgWrapperProxy(osg::Object* proto,
const std::string& name,
const std::string& associates,
DotOsgWrapper::ReadFunc readFunc,
DotOsgWrapper::WriteFunc writeFunc,
DotOsgWrapper::ReadWriteMode readWriteMode=DotOsgWrapper::READ_AND_WRITE)
{
if (Registry::instance())
{
2002-12-16 21:40:58 +08:00
_wrapper = new DotOsgWrapper(proto,name,associates,readFunc,writeFunc,readWriteMode);
2001-09-20 05:08:56 +08:00
Registry::instance()->addDotOsgWrapper(_wrapper.get());
}
}
~RegisterDotOsgWrapperProxy()
{
if (Registry::instance())
{
Registry::instance()->removeDotOsgWrapper(_wrapper.get());
}
}
protected:
osg::ref_ptr<DotOsgWrapper> _wrapper;
};
2007-09-14 19:01:48 +08:00
template<class T>
class TemplateRegisterDotOsgWrapperProxy : public RegisterDotOsgWrapperProxy, public T
{
public:
TemplateRegisterDotOsgWrapperProxy(osg::Object* proto,
const std::string& name,
const std::string& associates,
DotOsgWrapper::ReadFunc readFunc,
DotOsgWrapper::WriteFunc writeFunc,
DotOsgWrapper::ReadWriteMode readWriteMode=DotOsgWrapper::READ_AND_WRITE):
RegisterDotOsgWrapperProxy(proto, name, associates, readFunc, writeFunc, readWriteMode) {}
};
2001-09-20 05:08:56 +08:00
/** Proxy class for automatic registration of reader/writers with the Registry.*/
template<class T>
class RegisterReaderWriterProxy
{
public:
RegisterReaderWriterProxy()
{
if (Registry::instance())
{
2002-12-16 21:40:58 +08:00
_rw = new T;
2001-09-20 05:08:56 +08:00
Registry::instance()->addReaderWriter(_rw.get());
}
}
~RegisterReaderWriterProxy()
{
if (Registry::instance())
{
Registry::instance()->removeReaderWriter(_rw.get());
}
}
2003-12-09 20:07:41 +08:00
T* get() { return _rw.get(); }
2001-09-20 05:08:56 +08:00
protected:
osg::ref_ptr<T> _rw;
};
2007-05-25 23:27:06 +08:00
2007-06-10 17:51:29 +08:00
struct PluginFunctionProxy
2007-05-25 23:27:06 +08:00
{
2007-06-10 17:51:29 +08:00
PluginFunctionProxy(CPluginFunction function) { (function)(); }
2007-05-25 23:27:06 +08:00
};
#define USE_OSGPLUGIN(ext) \
extern "C" void osgdb_##ext(void); \
static osgDB::PluginFunctionProxy proxy_##ext(osgdb_##ext);
#define REGISTER_OSGPLUGIN(ext, classname) \
extern "C" void osgdb_##ext(void) {} \
2007-06-10 17:51:29 +08:00
static osgDB::RegisterReaderWriterProxy<classname> g_proxy_##classname;
2007-05-25 23:27:06 +08:00
2008-12-11 00:26:02 +08:00
#define USE_DOTOSGWRAPPER(classname) \
extern "C" void dotosgwrapper_##classname(void); \
static osgDB::PluginFunctionProxy proxy_dotosgwrapper_##classname(dotosgwrapper_##classname);
#define REGISTER_DOTOSGWRAPPER(classname) \
extern "C" void dotosgwrapper_##classname(void) {} \
static osgDB::RegisterDotOsgWrapperProxy dotosgwrapper_proxy_##classname
2002-02-03 20:33:41 +08:00
}
2001-09-20 05:08:56 +08:00
#endif