Registry 35 KB

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