| 1 | /* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield |
|---|
| 2 | * |
|---|
| 3 | * This library is open source and may be redistributed and/or modified under |
|---|
| 4 | * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or |
|---|
| 5 | * (at your option) any later version. The full license is in LICENSE file |
|---|
| 6 | * included with this distribution, and on the openscenegraph.org website. |
|---|
| 7 | * |
|---|
| 8 | * This library is distributed in the hope that it will be useful, |
|---|
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 11 | * OpenSceneGraph Public License for more details. |
|---|
| 12 | */ |
|---|
| 13 | |
|---|
| 14 | #ifndef OSGDB_REGISTRY |
|---|
| 15 | #define OSGDB_REGISTRY 1 |
|---|
| 16 | |
|---|
| 17 | #include <OpenThreads/ReentrantMutex> |
|---|
| 18 | |
|---|
| 19 | #include <osg/ref_ptr> |
|---|
| 20 | #include <osg/ArgumentParser> |
|---|
| 21 | #include <osg/KdTree> |
|---|
| 22 | |
|---|
| 23 | #include <osgDB/DynamicLibrary> |
|---|
| 24 | #include <osgDB/ReaderWriter> |
|---|
| 25 | #include <osgDB/Options> |
|---|
| 26 | #include <osgDB/DotOsgWrapper> |
|---|
| 27 | #include <osgDB/ObjectWrapper> |
|---|
| 28 | #include <osgDB/FileCache> |
|---|
| 29 | #include <osgDB/SharedStateManager> |
|---|
| 30 | #include <osgDB/ImageProcessor> |
|---|
| 31 | |
|---|
| 32 | #include <vector> |
|---|
| 33 | #include <map> |
|---|
| 34 | #include <string> |
|---|
| 35 | |
|---|
| 36 | extern "C" |
|---|
| 37 | { |
|---|
| 38 | typedef void (* CPluginFunction) (void); |
|---|
| 39 | } |
|---|
| 40 | |
|---|
| 41 | namespace osgDB { |
|---|
| 42 | |
|---|
| 43 | /** basic structure for custom runtime inheritance checking */ |
|---|
| 44 | struct basic_type_wrapper { |
|---|
| 45 | virtual ~basic_type_wrapper() {} |
|---|
| 46 | virtual bool matches(const osg::Object *proto) const = 0; |
|---|
| 47 | }; |
|---|
| 48 | |
|---|
| 49 | /** a class template that checks inheritance between a given |
|---|
| 50 | Object's class and a class defined at compile time through |
|---|
| 51 | the template parameter T. |
|---|
| 52 | This is used in conjunction with readObjectOfType() to |
|---|
| 53 | specify an abstract class as reference type. |
|---|
| 54 | **/ |
|---|
| 55 | template<class T> |
|---|
| 56 | struct type_wrapper: basic_type_wrapper { |
|---|
| 57 | bool matches(const osg::Object *proto) const |
|---|
| 58 | { |
|---|
| 59 | return dynamic_cast<const T*>(proto) != 0; |
|---|
| 60 | } |
|---|
| 61 | }; |
|---|
| 62 | |
|---|
| 63 | /** |
|---|
| 64 | Registry is a singleton factory which stores |
|---|
| 65 | the reader/writers which are linked in |
|---|
| 66 | at runtime for reading non-native file formats. |
|---|
| 67 | |
|---|
| 68 | The RegisterReaderWriterProxy can be used to automatically |
|---|
| 69 | register at runtime a reader/writer with the Registry. |
|---|
| 70 | */ |
|---|
| 71 | class OSGDB_EXPORT Registry : public osg::Referenced |
|---|
| 72 | { |
|---|
| 73 | public: |
|---|
| 74 | |
|---|
| 75 | |
|---|
| 76 | |
|---|
| 77 | |
|---|
| 78 | static Registry* instance(bool erase = false); |
|---|
| 79 | |
|---|
| 80 | /** read the command line arguments.*/ |
|---|
| 81 | void readCommandLine(osg::ArgumentParser& commandLine); |
|---|
| 82 | |
|---|
| 83 | /** register an .fileextension alias to mapExt toExt, the later |
|---|
| 84 | * should the the extension name of the readerwriter plugin library. |
|---|
| 85 | * For example to map .tif files to the tiff loader, use |
|---|
| 86 | * addExtAlias("tif","tiff") which will enable .tif to be read |
|---|
| 87 | * by the libdb_tiff readerwriter plugin.*/ |
|---|
| 88 | void addFileExtensionAlias(const std::string mapExt, const std::string toExt); |
|---|
| 89 | |
|---|
| 90 | /** Reads a file that configures extension mappings. File is ASCII text |
|---|
| 91 | * and each line contains the parameters to the addFileExtensionAlias |
|---|
| 92 | * method. Lines can be commented out with an initial '#' character.*/ |
|---|
| 93 | bool readPluginAliasConfigurationFile( const std::string& file ); |
|---|
| 94 | |
|---|
| 95 | typedef std::map< std::string, std::string> MimeTypeExtensionMap; |
|---|
| 96 | |
|---|
| 97 | /** Registers a mapping of a mime-type to an extension. A process fetching data |
|---|
| 98 | * over HTTP can use this facility to determine the proper ReaderWriter to use |
|---|
| 99 | * when there is no filename extension to rely upon. |
|---|
| 100 | */ |
|---|
| 101 | void addMimeTypeExtensionMapping(const std::string fromMimeType, const std::string toExt); |
|---|
| 102 | MimeTypeExtensionMap& getMimeTypeExtensionMap() { return _mimeTypeExtMap; } |
|---|
| 103 | const MimeTypeExtensionMap& getMimeTypeExtensionMap() const { return _mimeTypeExtMap; } |
|---|
| 104 | |
|---|
| 105 | void addReaderWriter(ReaderWriter* rw); |
|---|
| 106 | void removeReaderWriter(ReaderWriter* rw); |
|---|
| 107 | |
|---|
| 108 | void addImageProcessor(ImageProcessor* ip); |
|---|
| 109 | void removeImageProcessor(ImageProcessor* ip); |
|---|
| 110 | |
|---|
| 111 | /** create the platform specific library name associated with file.*/ |
|---|
| 112 | std::string createLibraryNameForFile(const std::string& fileName); |
|---|
| 113 | |
|---|
| 114 | /** create the platform specific library name associated with file extension.*/ |
|---|
| 115 | std::string createLibraryNameForExtension(const std::string& ext); |
|---|
| 116 | |
|---|
| 117 | /** create the platform specific library name associated with nodekit library name.*/ |
|---|
| 118 | std::string createLibraryNameForNodeKit(const std::string& name); |
|---|
| 119 | |
|---|
| 120 | |
|---|
| 121 | enum LoadStatus { |
|---|
| 122 | NOT_LOADED = 0, |
|---|
| 123 | PREVIOUSLY_LOADED, |
|---|
| 124 | LOADED |
|---|
| 125 | }; |
|---|
| 126 | |
|---|
| 127 | /** find the library in the OSG_LIBRARY_PATH and load it.*/ |
|---|
| 128 | LoadStatus loadLibrary(const std::string& fileName); |
|---|
| 129 | |
|---|
| 130 | /** close the attached library with specified name.*/ |
|---|
| 131 | bool closeLibrary(const std::string& fileName); |
|---|
| 132 | |
|---|
| 133 | /** close all libraries.*/ |
|---|
| 134 | void closeAllLibraries(); |
|---|
| 135 | |
|---|
| 136 | typedef std::vector< osg::ref_ptr<ReaderWriter> > ReaderWriterList; |
|---|
| 137 | |
|---|
| 138 | /** get a reader writer which handles specified extension.*/ |
|---|
| 139 | ReaderWriter* getReaderWriterForExtension(const std::string& ext); |
|---|
| 140 | |
|---|
| 141 | /** gets a reader/writer that handles the extension mapped to by one of |
|---|
| 142 | * the registered mime-types. */ |
|---|
| 143 | ReaderWriter* getReaderWriterForMimeType(const std::string& mimeType); |
|---|
| 144 | |
|---|
| 145 | /** get list of all registered ReaderWriters.*/ |
|---|
| 146 | ReaderWriterList& getReaderWriterList() { return _rwList; } |
|---|
| 147 | |
|---|
| 148 | /** get const list of all registered ReaderWriters.*/ |
|---|
| 149 | const ReaderWriterList& getReaderWriterList() const { return _rwList; } |
|---|
| 150 | |
|---|
| 151 | |
|---|
| 152 | typedef std::vector< osg::ref_ptr<ImageProcessor> > ImageProcessorList; |
|---|
| 153 | |
|---|
| 154 | /** get a image processor if available.*/ |
|---|
| 155 | ImageProcessor* getImageProcessor(); |
|---|
| 156 | |
|---|
| 157 | /** get a image processor which is associated specified extension.*/ |
|---|
| 158 | ImageProcessor* getImageProcessorForExtension(const std::string& ext); |
|---|
| 159 | |
|---|
| 160 | /** get list of all registered ImageProcessors.*/ |
|---|
| 161 | ImageProcessorList& getImageProcessorList() { return _ipList; } |
|---|
| 162 | |
|---|
| 163 | /** get const list of all registered ImageProcessors.*/ |
|---|
| 164 | const ImageProcessorList& getImageProcessorList() const { return _ipList; } |
|---|
| 165 | |
|---|
| 166 | |
|---|
| 167 | typedef class osgDB::FindFileCallback FindFileCallback; |
|---|
| 168 | typedef class osgDB::ReadFileCallback ReadFileCallback; |
|---|
| 169 | typedef class osgDB::WriteFileCallback WriteFileCallback; |
|---|
| 170 | typedef class osgDB::FileLocationCallback FileLocationCallback; |
|---|
| 171 | |
|---|
| 172 | /** Set the Registry callback to use in place of the default findFile calls.*/ |
|---|
| 173 | void setFindFileCallback( FindFileCallback* cb) { _findFileCallback = cb; } |
|---|
| 174 | |
|---|
| 175 | /** Get the findFile callback.*/ |
|---|
| 176 | FindFileCallback* getFindFileCallback() { return _findFileCallback.get(); } |
|---|
| 177 | |
|---|
| 178 | /** Get the const findFile callback.*/ |
|---|
| 179 | const FindFileCallback* getFindFileCallback() const { return _findFileCallback.get(); } |
|---|
| 180 | |
|---|
| 181 | |
|---|
| 182 | std::string findDataFile(const std::string& fileName, const Options* options, CaseSensitivity caseSensitivity) |
|---|
| 183 | { |
|---|
| 184 | if (options && options->getFindFileCallback()) return options->getFindFileCallback()->findDataFile(fileName, options, caseSensitivity); |
|---|
| 185 | else if (_findFileCallback.valid()) return _findFileCallback->findDataFile(fileName, options, caseSensitivity); |
|---|
| 186 | else return findDataFileImplementation(fileName, options, caseSensitivity); |
|---|
| 187 | } |
|---|
| 188 | std::string findDataFileImplementation(const std::string& fileName, const Options* options, CaseSensitivity caseSensitivity); |
|---|
| 189 | |
|---|
| 190 | std::string findLibraryFile(const std::string& fileName, const Options* options, CaseSensitivity caseSensitivity) |
|---|
| 191 | { |
|---|
| 192 | if (options && options->getFindFileCallback()) return options->getFindFileCallback()->findLibraryFile(fileName, options, caseSensitivity); |
|---|
| 193 | else if (_findFileCallback.valid()) return _findFileCallback->findLibraryFile(fileName, options, caseSensitivity); |
|---|
| 194 | else return findLibraryFileImplementation(fileName, options, caseSensitivity); |
|---|
| 195 | } |
|---|
| 196 | std::string findLibraryFileImplementation(const std::string& fileName, const Options* options, CaseSensitivity caseSensitivity); |
|---|
| 197 | |
|---|
| 198 | |
|---|
| 199 | |
|---|
| 200 | /** Set the Registry callback to use in place of the default readFile calls.*/ |
|---|
| 201 | void setReadFileCallback( ReadFileCallback* cb) { _readFileCallback = cb; } |
|---|
| 202 | |
|---|
| 203 | /** Get the readFile callback.*/ |
|---|
| 204 | ReadFileCallback* getReadFileCallback() { return _readFileCallback.get(); } |
|---|
| 205 | |
|---|
| 206 | /** Get the const readFile callback.*/ |
|---|
| 207 | const ReadFileCallback* getReadFileCallback() const { return _readFileCallback.get(); } |
|---|
| 208 | |
|---|
| 209 | |
|---|
| 210 | ReaderWriter::ReadResult openArchive(const std::string& fileName,ReaderWriter::ArchiveStatus status, unsigned int indexBlockSizeHint, const Options* options) |
|---|
| 211 | { |
|---|
| 212 | if (options && options->getReadFileCallback()) return options->getReadFileCallback()->openArchive(fileName, status, indexBlockSizeHint, options); |
|---|
| 213 | else if (_readFileCallback.valid()) return _readFileCallback->openArchive(fileName, status, indexBlockSizeHint, options); |
|---|
| 214 | else return openArchiveImplementation(fileName, status, indexBlockSizeHint, options); |
|---|
| 215 | } |
|---|
| 216 | ReaderWriter::ReadResult openArchiveImplementation(const std::string& fileName, ReaderWriter::ArchiveStatus status, unsigned int indexBlockSizeHint, const Options* options); |
|---|
| 217 | |
|---|
| 218 | ReaderWriter::ReadResult readObject(const std::string& fileName,const Options* options, bool buildKdTreeIfRequired=true) |
|---|
| 219 | { |
|---|
| 220 | ReaderWriter::ReadResult result; |
|---|
| 221 | if (options && options->getReadFileCallback()) result = options->getReadFileCallback()->readObject(fileName,options); |
|---|
| 222 | else if (_readFileCallback.valid()) result = _readFileCallback->readObject(fileName,options); |
|---|
| 223 | else result = readObjectImplementation(fileName,options); |
|---|
| 224 | |
|---|
| 225 | if (buildKdTreeIfRequired) _buildKdTreeIfRequired(result, options); |
|---|
| 226 | |
|---|
| 227 | return result; |
|---|
| 228 | } |
|---|
| 229 | ReaderWriter::ReadResult readObjectImplementation(const std::string& fileName,const Options* options); |
|---|
| 230 | |
|---|
| 231 | ReaderWriter::ReadResult readImage(const std::string& fileName,const Options* options) |
|---|
| 232 | { |
|---|
| 233 | if (options && options->getReadFileCallback()) return options->getReadFileCallback()->readImage(fileName,options); |
|---|
| 234 | else if (_readFileCallback.valid()) return _readFileCallback->readImage(fileName,options); |
|---|
| 235 | else return readImageImplementation(fileName,options); |
|---|
| 236 | } |
|---|
| 237 | ReaderWriter::ReadResult readImageImplementation(const std::string& fileName,const Options* options); |
|---|
| 238 | |
|---|
| 239 | ReaderWriter::ReadResult readHeightField(const std::string& fileName,const Options* options) |
|---|
| 240 | { |
|---|
| 241 | if (options && options->getReadFileCallback()) return options->getReadFileCallback()->readHeightField(fileName,options); |
|---|
| 242 | else if (_readFileCallback.valid()) return _readFileCallback->readHeightField(fileName,options); |
|---|
| 243 | else return readHeightFieldImplementation(fileName,options); |
|---|
| 244 | } |
|---|
| 245 | ReaderWriter::ReadResult readHeightFieldImplementation(const std::string& fileName,const Options* options); |
|---|
| 246 | |
|---|
| 247 | ReaderWriter::ReadResult readNode(const std::string& fileName,const Options* options, bool buildKdTreeIfRequired=true) |
|---|
| 248 | { |
|---|
| 249 | ReaderWriter::ReadResult result; |
|---|
| 250 | if (options && options->getReadFileCallback()) result = options->getReadFileCallback()->readNode(fileName,options); |
|---|
| 251 | else if (_readFileCallback.valid()) result = _readFileCallback->readNode(fileName,options); |
|---|
| 252 | else result = readNodeImplementation(fileName,options); |
|---|
| 253 | |
|---|
| 254 | if (buildKdTreeIfRequired) _buildKdTreeIfRequired(result, options); |
|---|
| 255 | |
|---|
| 256 | return result; |
|---|
| 257 | } |
|---|
| 258 | ReaderWriter::ReadResult readNodeImplementation(const std::string& fileName,const Options* options); |
|---|
| 259 | |
|---|
| 260 | ReaderWriter::ReadResult readShader(const std::string& fileName,const Options* options) |
|---|
| 261 | { |
|---|
| 262 | if (options && options->getReadFileCallback()) return options->getReadFileCallback()->readShader(fileName,options); |
|---|
| 263 | if (_readFileCallback.valid()) return _readFileCallback->readShader(fileName,options); |
|---|
| 264 | else return readShaderImplementation(fileName,options); |
|---|
| 265 | } |
|---|
| 266 | ReaderWriter::ReadResult readShaderImplementation(const std::string& fileName,const Options* options); |
|---|
| 267 | |
|---|
| 268 | |
|---|
| 269 | /** Set the Registry callback to use in place of the default writeFile calls.*/ |
|---|
| 270 | void setWriteFileCallback( WriteFileCallback* cb) { _writeFileCallback = cb; } |
|---|
| 271 | |
|---|
| 272 | /** Get the writeFile callback.*/ |
|---|
| 273 | WriteFileCallback* getWriteFileCallback() { return _writeFileCallback.get(); } |
|---|
| 274 | |
|---|
| 275 | /** Get the const writeFile callback.*/ |
|---|
| 276 | const WriteFileCallback* getWriteFileCallback() const { return _writeFileCallback.get(); } |
|---|
| 277 | |
|---|
| 278 | |
|---|
| 279 | ReaderWriter::WriteResult writeObject(const osg::Object& obj, const std::string& fileName,const Options* options) |
|---|
| 280 | { |
|---|
| 281 | if (options && options->getWriteFileCallback()) return options->getWriteFileCallback()->writeObject(obj,fileName,options); |
|---|
| 282 | else if (_writeFileCallback.valid()) return _writeFileCallback->writeObject(obj,fileName,options); |
|---|
| 283 | else return writeObjectImplementation(obj,fileName,options); |
|---|
| 284 | } |
|---|
| 285 | ReaderWriter::WriteResult writeObjectImplementation(const osg::Object& obj, const std::string& fileName,const Options* options); |
|---|
| 286 | |
|---|
| 287 | ReaderWriter::WriteResult writeImage(const osg::Image& obj, const std::string& fileName,const Options* options) |
|---|
| 288 | { |
|---|
| 289 | if (options && options->getWriteFileCallback()) return options->getWriteFileCallback()->writeImage(obj,fileName,options); |
|---|
| 290 | else if (_writeFileCallback.valid()) return _writeFileCallback->writeImage(obj,fileName,options); |
|---|
| 291 | else return writeImageImplementation(obj,fileName,options); |
|---|
| 292 | } |
|---|
| 293 | ReaderWriter::WriteResult writeImageImplementation(const osg::Image& obj, const std::string& fileName,const Options* options); |
|---|
| 294 | |
|---|
| 295 | ReaderWriter::WriteResult writeHeightField(const osg::HeightField& obj, const std::string& fileName,const Options* options) |
|---|
| 296 | { |
|---|
| 297 | if (options && options->getWriteFileCallback()) return options->getWriteFileCallback()->writeHeightField(obj,fileName,options); |
|---|
| 298 | else if (_writeFileCallback.valid()) return _writeFileCallback->writeHeightField(obj,fileName,options); |
|---|
| 299 | else return writeHeightFieldImplementation(obj,fileName,options); |
|---|
| 300 | } |
|---|
| 301 | ReaderWriter::WriteResult writeHeightFieldImplementation(const osg::HeightField& obj, const std::string& fileName,const Options* options); |
|---|
| 302 | |
|---|
| 303 | ReaderWriter::WriteResult writeNode(const osg::Node& node, const std::string& fileName,const Options* options) |
|---|
| 304 | { |
|---|
| 305 | if (options && options->getWriteFileCallback()) return options->getWriteFileCallback()->writeNode(node,fileName,options); |
|---|
| 306 | else if (_writeFileCallback.valid()) return _writeFileCallback->writeNode(node,fileName,options); |
|---|
| 307 | else return writeNodeImplementation(node,fileName,options); |
|---|
| 308 | } |
|---|
| 309 | ReaderWriter::WriteResult writeNodeImplementation(const osg::Node& node, const std::string& fileName,const Options* options); |
|---|
| 310 | |
|---|
| 311 | ReaderWriter::WriteResult writeShader(const osg::Shader& obj, const std::string& fileName,const Options* options) |
|---|
| 312 | { |
|---|
| 313 | if (options && options->getWriteFileCallback()) return options->getWriteFileCallback()->writeShader(obj,fileName,options); |
|---|
| 314 | else if (_writeFileCallback.valid()) return _writeFileCallback->writeShader(obj,fileName,options); |
|---|
| 315 | else return writeShaderImplementation(obj,fileName,options); |
|---|
| 316 | } |
|---|
| 317 | ReaderWriter::WriteResult writeShaderImplementation(const osg::Shader& obj, const std::string& fileName,const Options* options); |
|---|
| 318 | |
|---|
| 319 | |
|---|
| 320 | inline void _buildKdTreeIfRequired(ReaderWriter::ReadResult& result, const Options* options) |
|---|
| 321 | { |
|---|
| 322 | bool doKdTreeBuilder = (options && options->getBuildKdTreesHint()!=Options::NO_PREFERENCE) ? |
|---|
| 323 | options->getBuildKdTreesHint() == Options::BUILD_KDTREES : |
|---|
| 324 | _buildKdTreesHint == Options::BUILD_KDTREES; |
|---|
| 325 | |
|---|
| 326 | if (doKdTreeBuilder && _kdTreeBuilder.valid() && result.validNode()) |
|---|
| 327 | { |
|---|
| 328 | osg::ref_ptr<osg::KdTreeBuilder> builder = _kdTreeBuilder->clone(); |
|---|
| 329 | result.getNode()->accept(*builder); |
|---|
| 330 | } |
|---|
| 331 | } |
|---|
| 332 | |
|---|
| 333 | /** Set the callback to use inform to the DatabasePager whether a file is located on local or remote file system.*/ |
|---|
| 334 | void setFileLocationCallback( FileLocationCallback* cb) { _fileLocationCallback = cb; } |
|---|
| 335 | |
|---|
| 336 | /** Get the callback to use inform to the DatabasePager whether a file is located on local or remote file system.*/ |
|---|
| 337 | FileLocationCallback* getFileLocationCallback() const { return _fileLocationCallback.get(); } |
|---|
| 338 | |
|---|
| 339 | |
|---|
| 340 | |
|---|
| 341 | /** Set whether the KdTrees should be built for geometry in the loader model. */ |
|---|
| 342 | void setBuildKdTreesHint(Options::BuildKdTreesHint hint) { _buildKdTreesHint = hint; } |
|---|
| 343 | |
|---|
| 344 | /** Get whether the KdTrees should be built for geometry in the loader model. */ |
|---|
| 345 | Options::BuildKdTreesHint getBuildKdTreesHint() const { return _buildKdTreesHint; } |
|---|
| 346 | |
|---|
| 347 | /** Set the KdTreeBuilder visitor that is used to build KdTree on loaded models.*/ |
|---|
| 348 | void setKdTreeBuilder(osg::KdTreeBuilder* builder) { _kdTreeBuilder = builder; } |
|---|
| 349 | |
|---|
| 350 | /** Get the KdTreeBuilder visitor that is used to build KdTree on loaded models.*/ |
|---|
| 351 | osg::KdTreeBuilder* getKdTreeBuilder() { return _kdTreeBuilder.get(); } |
|---|
| 352 | |
|---|
| 353 | |
|---|
| 354 | /** Set the FileCache that is used to manage local storage of files downloaded from the internet.*/ |
|---|
| 355 | void setFileCache(FileCache* fileCache) { _fileCache = fileCache; } |
|---|
| 356 | |
|---|
| 357 | /** Get the FileCache that is used to manage local storage of files downloaded from the internet.*/ |
|---|
| 358 | FileCache* getFileCache() { return _fileCache.get(); } |
|---|
| 359 | |
|---|
| 360 | /** Get the const FileCache that is used to manage local storage of files downloaded from the internet.*/ |
|---|
| 361 | const FileCache* getFileCache() const { return _fileCache.get(); } |
|---|
| 362 | |
|---|
| 363 | |
|---|
| 364 | /** Set the password map to be used by plugins when access files from secure locations.*/ |
|---|
| 365 | void setAuthenticationMap(AuthenticationMap* authenticationMap) { _authenticationMap = authenticationMap; } |
|---|
| 366 | |
|---|
| 367 | /** Get the password map to be used by plugins when access files from secure locations.*/ |
|---|
| 368 | AuthenticationMap* getAuthenticationMap() { return _authenticationMap.get(); } |
|---|
| 369 | |
|---|
| 370 | /** Get the password map to be used by plugins when access files from secure locations.*/ |
|---|
| 371 | const AuthenticationMap* getAuthenticationMap() const { return _authenticationMap.get(); } |
|---|
| 372 | |
|---|
| 373 | |
|---|
| 374 | void setCreateNodeFromImage(bool flag) { _createNodeFromImage = flag; } |
|---|
| 375 | bool getCreateNodeFromImage() const { return _createNodeFromImage; } |
|---|
| 376 | |
|---|
| 377 | |
|---|
| 378 | void setOptions(Options* opt) { _options = opt; } |
|---|
| 379 | Options* getOptions() { return _options.get(); } |
|---|
| 380 | const Options* getOptions() const { return _options.get(); } |
|---|
| 381 | |
|---|
| 382 | |
|---|
| 383 | /** initialize both the Data and Library FilePaths, by default called by the |
|---|
| 384 | * constructor, so it should only be required if you want to force |
|---|
| 385 | * the re-reading of environmental variables.*/ |
|---|
| 386 | void initFilePathLists() { initDataFilePathList(); initLibraryFilePathList(); } |
|---|
| 387 | |
|---|
| 388 | /** initialize the Data FilePath by reading the OSG_FILE_PATH environmental variable.*/ |
|---|
| 389 | void initDataFilePathList(); |
|---|
| 390 | |
|---|
| 391 | /** Set the data file path using a list of paths stored in a FilePath, which is used when search for data files.*/ |
|---|
| 392 | void setDataFilePathList(const FilePathList& filepath) { _dataFilePath = filepath; } |
|---|
| 393 | |
|---|
| 394 | /** 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.*/ |
|---|
| 395 | void setDataFilePathList(const std::string& paths); |
|---|
| 396 | |
|---|
| 397 | /** get the data file path which is used when search for data files.*/ |
|---|
| 398 | FilePathList& getDataFilePathList() { return _dataFilePath; } |
|---|
| 399 | |
|---|
| 400 | /** get the const data file path which is used when search for data files.*/ |
|---|
| 401 | const FilePathList& getDataFilePathList() const { return _dataFilePath; } |
|---|
| 402 | |
|---|
| 403 | /** initialize the Library FilePath by reading the OSG_LIBRARY_PATH |
|---|
| 404 | * and the appropriate system environmental variables*/ |
|---|
| 405 | void initLibraryFilePathList(); |
|---|
| 406 | |
|---|
| 407 | /** Set the library file path using a list of paths stored in a FilePath, which is used when search for data files.*/ |
|---|
| 408 | void setLibraryFilePathList(const FilePathList& filepath) { _libraryFilePath = filepath; } |
|---|
| 409 | |
|---|
| 410 | /** 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.*/ |
|---|
| 411 | void setLibraryFilePathList(const std::string& paths); |
|---|
| 412 | |
|---|
| 413 | /** get the library file path which is used when search for library (dso/dll's) files.*/ |
|---|
| 414 | FilePathList& getLibraryFilePathList() { return _libraryFilePath; } |
|---|
| 415 | |
|---|
| 416 | /** get the const library file path which is used when search for library (dso/dll's) files.*/ |
|---|
| 417 | const FilePathList& getLibraryFilePathList() const { return _libraryFilePath; } |
|---|
| 418 | |
|---|
| 419 | /** For each object in the cache which has an reference count greater than 1 |
|---|
| 420 | * (and therefore referenced by elsewhere in the application) set the time stamp |
|---|
| 421 | * for that object in the cache to specified time. |
|---|
| 422 | * This would typically be called once per frame by applications which are doing database paging, |
|---|
| 423 | * and need to prune objects that are no longer required. |
|---|
| 424 | * The time used is taken from the FrameStamp::getReferenceTime().*/ |
|---|
| 425 | void updateTimeStampOfObjectsInCacheWithExternalReferences(const osg::FrameStamp& frameStamp); |
|---|
| 426 | |
|---|
| 427 | /** Removed object in the cache which have a time stamp at or before the specified expiry time. |
|---|
| 428 | * This would typically be called once per frame by applications which are doing database paging, |
|---|
| 429 | * and need to prune objects that are no longer required, and called after the a called |
|---|
| 430 | * after the call to updateTimeStampOfObjectsInCacheWithExternalReferences(frameStamp).*/ |
|---|
| 431 | void removeExpiredObjectsInCache(const osg::FrameStamp& frameStamp); |
|---|
| 432 | |
|---|
| 433 | /** set hint to viewer code calling removeExpiredObjectsInCache to specify how long it should give before expiring objects in Registry cache,*/ |
|---|
| 434 | void setExpiryDelay(double expiryDelay) { _expiryDelay = expiryDelay; } |
|---|
| 435 | |
|---|
| 436 | double getExpiryDelay() const { return _expiryDelay; } |
|---|
| 437 | |
|---|
| 438 | |
|---|
| 439 | /** Remove all objects in the cache regardless of having external references or expiry times.*/ |
|---|
| 440 | void clearObjectCache(); |
|---|
| 441 | |
|---|
| 442 | /** Add a filename,object,timestamp triple to the Registry::ObjectCache.*/ |
|---|
| 443 | void addEntryToObjectCache(const std::string& filename, osg::Object* object, double timestamp = 0.0); |
|---|
| 444 | |
|---|
| 445 | /** Remove Object from cache.*/ |
|---|
| 446 | void removeFromObjectCache(const std::string& fileName); |
|---|
| 447 | |
|---|
| 448 | /** Get an Object from the object cache*/ |
|---|
| 449 | osg::Object* getFromObjectCache(const std::string& fileName); |
|---|
| 450 | |
|---|
| 451 | /** Get an ref_ptr<Object> from the object cache*/ |
|---|
| 452 | osg::ref_ptr<osg::Object> getRefFromObjectCache(const std::string& fileName); |
|---|
| 453 | |
|---|
| 454 | |
|---|
| 455 | |
|---|
| 456 | /** Add archive to archive cache so that future calls reference this archive.*/ |
|---|
| 457 | void addToArchiveCache(const std::string& fileName, osgDB::Archive* archive); |
|---|
| 458 | |
|---|
| 459 | /** Remove Archive from cache.*/ |
|---|
| 460 | void removeFromArchiveCache(const std::string& fileName); |
|---|
| 461 | |
|---|
| 462 | /** Get an Archive from the archive cache.*/ |
|---|
| 463 | osgDB::Archive* getFromArchiveCache(const std::string& fileName); |
|---|
| 464 | |
|---|
| 465 | /** Get an ref_ptr<Archive> from the archive cache.*/ |
|---|
| 466 | osg::ref_ptr<osgDB::Archive> getRefFromArchiveCache(const std::string& fileName); |
|---|
| 467 | |
|---|
| 468 | /** Remove all archives from the archive cache.*/ |
|---|
| 469 | void clearArchiveCache(); |
|---|
| 470 | |
|---|
| 471 | /** If State is non-zero, this function releases OpenGL objects for |
|---|
| 472 | * the specified graphics context. Otherwise, releases OpenGL objexts |
|---|
| 473 | * for all graphics contexts. */ |
|---|
| 474 | void releaseGLObjects(osg::State* state=0); |
|---|
| 475 | |
|---|
| 476 | /** get the attached library with specified name.*/ |
|---|
| 477 | DynamicLibrary* getLibrary(const std::string& fileName); |
|---|
| 478 | |
|---|
| 479 | /** Set the SharedStateManager.*/ |
|---|
| 480 | void setSharedStateManager(SharedStateManager* SharedStateManager) { _sharedStateManager = SharedStateManager; } |
|---|
| 481 | |
|---|
| 482 | /** Get the SharedStateManager, creating one if one is not already created.*/ |
|---|
| 483 | SharedStateManager* getOrCreateSharedStateManager(); |
|---|
| 484 | |
|---|
| 485 | /** Get the SharedStateManager. Return 0 if no SharedStateManager has been assigned.*/ |
|---|
| 486 | SharedStateManager* getSharedStateManager() { return _sharedStateManager.get(); } |
|---|
| 487 | |
|---|
| 488 | /** Add an Archive extension.*/ |
|---|
| 489 | void addArchiveExtension(const std::string ext); |
|---|
| 490 | |
|---|
| 491 | /** registers a protocol */ |
|---|
| 492 | void registerProtocol(const std::string& protocol); |
|---|
| 493 | |
|---|
| 494 | /** returns true, if named protocol is registered */ |
|---|
| 495 | bool isProtocolRegistered(const std::string& protocol); |
|---|
| 496 | |
|---|
| 497 | /** Get the ObjectWrapperManager that is used to store all the ObjectWrappers. */ |
|---|
| 498 | ObjectWrapperManager* getObjectWrapperManager() { return _objectWrapperManager.get(); } |
|---|
| 499 | |
|---|
| 500 | /** Get the ObjectWrapperManager that is used to store all the ObjectWrappers. */ |
|---|
| 501 | DeprecatedDotOsgWrapperManager* getDeprecatedDotOsgObjectWrapperManager() { return _deprecatedDotOsgWrapperManager.get(); } |
|---|
| 502 | |
|---|
| 503 | typedef std::vector< std::string> ArchiveExtensionList; |
|---|
| 504 | const ArchiveExtensionList& getArchiveExtensions() const { return _archiveExtList; } |
|---|
| 505 | |
|---|
| 506 | protected: |
|---|
| 507 | |
|---|
| 508 | virtual ~Registry(); |
|---|
| 509 | |
|---|
| 510 | typedef std::vector< osg::ref_ptr<DynamicLibrary> > DynamicLibraryList; |
|---|
| 511 | typedef std::map< std::string, std::string> ExtensionAliasMap; |
|---|
| 512 | |
|---|
| 513 | typedef std::pair<osg::ref_ptr<osg::Object>, double > ObjectTimeStampPair; |
|---|
| 514 | typedef std::map<std::string, ObjectTimeStampPair > ObjectCache; |
|---|
| 515 | typedef std::map<std::string, osg::ref_ptr<osgDB::Archive> > ArchiveCache; |
|---|
| 516 | |
|---|
| 517 | typedef std::set<std::string> RegisteredProtocolsSet; |
|---|
| 518 | |
|---|
| 519 | /** constructor is private, as its a singleton, preventing |
|---|
| 520 | construction other than via the instance() method and |
|---|
| 521 | therefore ensuring only one copy is ever constructed*/ |
|---|
| 522 | Registry(); |
|---|
| 523 | |
|---|
| 524 | /** get the attached library with specified name.*/ |
|---|
| 525 | DynamicLibraryList::iterator getLibraryItr(const std::string& fileName); |
|---|
| 526 | |
|---|
| 527 | Options::BuildKdTreesHint _buildKdTreesHint; |
|---|
| 528 | osg::ref_ptr<osg::KdTreeBuilder> _kdTreeBuilder; |
|---|
| 529 | |
|---|
| 530 | osg::ref_ptr<FileCache> _fileCache; |
|---|
| 531 | |
|---|
| 532 | osg::ref_ptr<AuthenticationMap> _authenticationMap; |
|---|
| 533 | |
|---|
| 534 | bool _createNodeFromImage; |
|---|
| 535 | |
|---|
| 536 | RegisteredProtocolsSet _registeredProtocols; |
|---|
| 537 | |
|---|
| 538 | public: |
|---|
| 539 | /** Functor used in internal implementations.*/ |
|---|
| 540 | struct ReadFunctor |
|---|
| 541 | { |
|---|
| 542 | ReadFunctor(const std::string& filename, const Options* options): |
|---|
| 543 | _filename(filename), |
|---|
| 544 | _options(options) {} |
|---|
| 545 | |
|---|
| 546 | virtual ~ReadFunctor() {} |
|---|
| 547 | virtual ReaderWriter::ReadResult doRead(ReaderWriter& rw) const = 0; |
|---|
| 548 | virtual bool isValid(ReaderWriter::ReadResult& readResult) const = 0; |
|---|
| 549 | virtual bool isValid(osg::Object* object) const = 0; |
|---|
| 550 | |
|---|
| 551 | virtual ReadFunctor* cloneType(const std::string& filename, const Options* options) const = 0; |
|---|
| 552 | |
|---|
| 553 | std::string _filename; |
|---|
| 554 | const Options* _options; |
|---|
| 555 | }; |
|---|
| 556 | |
|---|
| 557 | protected: |
|---|
| 558 | |
|---|
| 559 | void destruct(); |
|---|
| 560 | |
|---|
| 561 | // forward declare helper classes |
|---|
| 562 | struct ReadObjectFunctor; |
|---|
| 563 | struct ReadImageFunctor; |
|---|
| 564 | struct ReadHeightFieldFunctor; |
|---|
| 565 | struct ReadNodeFunctor; |
|---|
| 566 | struct ReadArchiveFunctor; |
|---|
| 567 | struct ReadShaderFunctor; |
|---|
| 568 | |
|---|
| 569 | // make helper classes friends to get round VS6.0 "issues" |
|---|
| 570 | friend struct ReadFunctor; |
|---|
| 571 | friend struct ReadObjectFunctor; |
|---|
| 572 | friend struct ReadImageFunctor; |
|---|
| 573 | friend struct ReadHeightFieldFunctor; |
|---|
| 574 | friend struct ReadNodeFunctor; |
|---|
| 575 | friend struct ReadArchiveFunctor; |
|---|
| 576 | friend struct ReadShaderFunctor; |
|---|
| 577 | |
|---|
| 578 | ReaderWriter::ReadResult read(const ReadFunctor& readFunctor); |
|---|
| 579 | ReaderWriter::ReadResult readImplementation(const ReadFunctor& readFunctor,Options::CacheHintOptions cacheHint); |
|---|
| 580 | |
|---|
| 581 | |
|---|
| 582 | // forward declare helper class |
|---|
| 583 | class AvailableReaderWriterIterator; |
|---|
| 584 | friend class AvailableReaderWriterIterator; |
|---|
| 585 | class AvailableArchiveIterator; |
|---|
| 586 | friend class AvailableArchiveIterator; |
|---|
| 587 | |
|---|
| 588 | |
|---|
| 589 | osg::ref_ptr<FindFileCallback> _findFileCallback; |
|---|
| 590 | osg::ref_ptr<ReadFileCallback> _readFileCallback; |
|---|
| 591 | osg::ref_ptr<WriteFileCallback> _writeFileCallback; |
|---|
| 592 | osg::ref_ptr<FileLocationCallback> _fileLocationCallback; |
|---|
| 593 | |
|---|
| 594 | OpenThreads::ReentrantMutex _pluginMutex; |
|---|
| 595 | ReaderWriterList _rwList; |
|---|
| 596 | ImageProcessorList _ipList; |
|---|
| 597 | DynamicLibraryList _dlList; |
|---|
| 598 | |
|---|
| 599 | OpenThreads::ReentrantMutex _archiveCacheMutex; |
|---|
| 600 | ArchiveCache _archiveCache; |
|---|
| 601 | |
|---|
| 602 | bool _openingLibrary; |
|---|
| 603 | |
|---|
| 604 | // map to alias to extensions to plugins. |
|---|
| 605 | ExtensionAliasMap _extAliasMap; |
|---|
| 606 | |
|---|
| 607 | // maps mime-types to extensions. |
|---|
| 608 | MimeTypeExtensionMap _mimeTypeExtMap; |
|---|
| 609 | |
|---|
| 610 | // Utility: Removes whitespace from both ends of a string. |
|---|
| 611 | static std::string trim( const std::string& str ); |
|---|
| 612 | |
|---|
| 613 | // options to pass to reader writers. |
|---|
| 614 | osg::ref_ptr<Options> _options; |
|---|
| 615 | |
|---|
| 616 | FilePathList _dataFilePath; |
|---|
| 617 | FilePathList _libraryFilePath; |
|---|
| 618 | |
|---|
| 619 | double _expiryDelay; |
|---|
| 620 | ObjectCache _objectCache; |
|---|
| 621 | OpenThreads::Mutex _objectCacheMutex; |
|---|
| 622 | |
|---|
| 623 | |
|---|
| 624 | ArchiveExtensionList _archiveExtList; |
|---|
| 625 | |
|---|
| 626 | osg::ref_ptr<SharedStateManager> _sharedStateManager; |
|---|
| 627 | |
|---|
| 628 | osg::ref_ptr<ObjectWrapperManager> _objectWrapperManager; |
|---|
| 629 | osg::ref_ptr<DeprecatedDotOsgWrapperManager> _deprecatedDotOsgWrapperManager; |
|---|
| 630 | }; |
|---|
| 631 | |
|---|
| 632 | /** read the command line arguments.*/ |
|---|
| 633 | inline void readCommandLine(osg::ArgumentParser& parser) |
|---|
| 634 | { |
|---|
| 635 | Registry::instance()->readCommandLine(parser); |
|---|
| 636 | } |
|---|
| 637 | |
|---|
| 638 | /** Proxy class for automatic registration of reader/writers with the Registry.*/ |
|---|
| 639 | template<class T> |
|---|
| 640 | class RegisterReaderWriterProxy |
|---|
| 641 | { |
|---|
| 642 | public: |
|---|
| 643 | RegisterReaderWriterProxy() |
|---|
| 644 | { |
|---|
| 645 | if (Registry::instance()) |
|---|
| 646 | { |
|---|
| 647 | _rw = new T; |
|---|
| 648 | Registry::instance()->addReaderWriter(_rw.get()); |
|---|
| 649 | } |
|---|
| 650 | } |
|---|
| 651 | |
|---|
| 652 | ~RegisterReaderWriterProxy() |
|---|
| 653 | { |
|---|
| 654 | if (Registry::instance()) |
|---|
| 655 | { |
|---|
| 656 | Registry::instance()->removeReaderWriter(_rw.get()); |
|---|
| 657 | } |
|---|
| 658 | } |
|---|
| 659 | |
|---|
| 660 | T* get() { return _rw.get(); } |
|---|
| 661 | |
|---|
| 662 | protected: |
|---|
| 663 | osg::ref_ptr<T> _rw; |
|---|
| 664 | }; |
|---|
| 665 | |
|---|
| 666 | |
|---|
| 667 | /** Proxy class for automatic registration of reader/writers with the Registry.*/ |
|---|
| 668 | template<class T> |
|---|
| 669 | class RegisterImageProcessorProxy |
|---|
| 670 | { |
|---|
| 671 | public: |
|---|
| 672 | RegisterImageProcessorProxy() |
|---|
| 673 | { |
|---|
| 674 | if (Registry::instance()) |
|---|
| 675 | { |
|---|
| 676 | _rw = new T; |
|---|
| 677 | Registry::instance()->addImageProcessor(_rw.get()); |
|---|
| 678 | } |
|---|
| 679 | } |
|---|
| 680 | |
|---|
| 681 | ~RegisterImageProcessorProxy() |
|---|
| 682 | { |
|---|
| 683 | if (Registry::instance()) |
|---|
| 684 | { |
|---|
| 685 | Registry::instance()->removeImageProcessor(_rw.get()); |
|---|
| 686 | } |
|---|
| 687 | } |
|---|
| 688 | |
|---|
| 689 | T* get() { return _rw.get(); } |
|---|
| 690 | |
|---|
| 691 | protected: |
|---|
| 692 | osg::ref_ptr<T> _rw; |
|---|
| 693 | }; |
|---|
| 694 | |
|---|
| 695 | struct PluginFunctionProxy |
|---|
| 696 | { |
|---|
| 697 | PluginFunctionProxy(CPluginFunction function) { (function)(); } |
|---|
| 698 | }; |
|---|
| 699 | |
|---|
| 700 | #define USE_OSGPLUGIN(ext) \ |
|---|
| 701 | extern "C" void osgdb_##ext(void); \ |
|---|
| 702 | static osgDB::PluginFunctionProxy proxy_##ext(osgdb_##ext); |
|---|
| 703 | |
|---|
| 704 | #define USE_DOTOSGWRAPPER(classname) \ |
|---|
| 705 | extern "C" void dotosgwrapper_##classname(void); \ |
|---|
| 706 | static osgDB::PluginFunctionProxy proxy_dotosgwrapper_##classname(dotosgwrapper_##classname); |
|---|
| 707 | |
|---|
| 708 | #define USE_DOTOSGWRAPPER_LIBRARY(libname) \ |
|---|
| 709 | extern "C" void dotosgwrapper_library_##libname(void); \ |
|---|
| 710 | static osgDB::PluginFunctionProxy proxy_dotosgwrapper_library_##libname(dotosgwrapper_library_##libname); |
|---|
| 711 | |
|---|
| 712 | #define USE_SERIALIZER_WRAPPER(classname) \ |
|---|
| 713 | extern "C" void wrapper_serializer_##classname(void); \ |
|---|
| 714 | static osgDB::PluginFunctionProxy proxy_serializer_##classname(wrapper_serializer_##classname); |
|---|
| 715 | |
|---|
| 716 | #define USE_SERIALIZER_WRAPPER_LIBRARY(libname) \ |
|---|
| 717 | extern "C" void wrapper_serializer_library_##libname(void); \ |
|---|
| 718 | static osgDB::PluginFunctionProxy proxy_serializer_library_##libname(wrapper_serializer_library_##libname); |
|---|
| 719 | |
|---|
| 720 | #define USE_COMPRESSOR_WRAPPER(classname) \ |
|---|
| 721 | extern "C" void wrapper_serializer_##classname(void); \ |
|---|
| 722 | static osgDB::PluginFunctionProxy proxy_compressor_##classname(wrapper_compressor_##classname); |
|---|
| 723 | |
|---|
| 724 | #define REGISTER_OSGPLUGIN(ext, classname) \ |
|---|
| 725 | extern "C" void osgdb_##ext(void) {} \ |
|---|
| 726 | static osgDB::RegisterReaderWriterProxy<classname> g_proxy_##classname; |
|---|
| 727 | |
|---|
| 728 | #define REGISTER_OSGIMAGEPROCESSOR(ext, classname) \ |
|---|
| 729 | extern "C" void osgdb_##ext(void) {} \ |
|---|
| 730 | static osgDB::RegisterImageProcessorProxy<classname> g_proxy_##classname; |
|---|
| 731 | |
|---|
| 732 | } |
|---|
| 733 | |
|---|
| 734 | #endif |
|---|