| 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_OPTIONS |
|---|
| 15 | #define OSGDB_OPTIONS 1 |
|---|
| 16 | |
|---|
| 17 | #include <osgDB/AuthenticationMap> |
|---|
| 18 | #include <osgDB/ReaderWriter> |
|---|
| 19 | #include <osgDB/FileCache> |
|---|
| 20 | |
|---|
| 21 | #include <deque> |
|---|
| 22 | #include <list> |
|---|
| 23 | #include <iosfwd> |
|---|
| 24 | |
|---|
| 25 | namespace osgDB { |
|---|
| 26 | |
|---|
| 27 | /** list of directories to search through which searching for files. */ |
|---|
| 28 | typedef std::deque<std::string> FilePathList; |
|---|
| 29 | |
|---|
| 30 | enum CaseSensitivity |
|---|
| 31 | { |
|---|
| 32 | CASE_SENSITIVE, |
|---|
| 33 | CASE_INSENSITIVE |
|---|
| 34 | }; |
|---|
| 35 | |
|---|
| 36 | // forward decare |
|---|
| 37 | class Options; |
|---|
| 38 | |
|---|
| 39 | class OSGDB_EXPORT FindFileCallback : public virtual osg::Referenced |
|---|
| 40 | { |
|---|
| 41 | public: |
|---|
| 42 | |
|---|
| 43 | virtual std::string findDataFile(const std::string& filename, const Options* options, CaseSensitivity caseSensitivity); |
|---|
| 44 | |
|---|
| 45 | virtual std::string findLibraryFile(const std::string& filename, const Options* options, CaseSensitivity caseSensitivity); |
|---|
| 46 | |
|---|
| 47 | protected: |
|---|
| 48 | virtual ~FindFileCallback() {} |
|---|
| 49 | }; |
|---|
| 50 | |
|---|
| 51 | |
|---|
| 52 | class OSGDB_EXPORT ReadFileCallback : public virtual osg::Referenced |
|---|
| 53 | { |
|---|
| 54 | public: |
|---|
| 55 | |
|---|
| 56 | virtual ReaderWriter::ReadResult openArchive(const std::string& filename,ReaderWriter::ArchiveStatus status, unsigned int indexBlockSizeHint, const Options* useObjectCache); |
|---|
| 57 | |
|---|
| 58 | virtual ReaderWriter::ReadResult readObject(const std::string& filename, const Options* options); |
|---|
| 59 | |
|---|
| 60 | virtual ReaderWriter::ReadResult readImage(const std::string& filename, const Options* options); |
|---|
| 61 | |
|---|
| 62 | virtual ReaderWriter::ReadResult readHeightField(const std::string& filename, const Options* options); |
|---|
| 63 | |
|---|
| 64 | virtual ReaderWriter::ReadResult readNode(const std::string& filename, const Options* options); |
|---|
| 65 | |
|---|
| 66 | virtual ReaderWriter::ReadResult readShader(const std::string& filename, const Options* options); |
|---|
| 67 | |
|---|
| 68 | protected: |
|---|
| 69 | virtual ~ReadFileCallback() {} |
|---|
| 70 | }; |
|---|
| 71 | |
|---|
| 72 | class OSGDB_EXPORT WriteFileCallback : public virtual osg::Referenced |
|---|
| 73 | { |
|---|
| 74 | public: |
|---|
| 75 | |
|---|
| 76 | virtual ReaderWriter::WriteResult writeObject(const osg::Object& obj, const std::string& fileName,const Options* options); |
|---|
| 77 | |
|---|
| 78 | virtual ReaderWriter::WriteResult writeImage(const osg::Image& obj, const std::string& fileName,const Options* options); |
|---|
| 79 | |
|---|
| 80 | virtual ReaderWriter::WriteResult writeHeightField(const osg::HeightField& obj, const std::string& fileName,const Options* options); |
|---|
| 81 | |
|---|
| 82 | virtual ReaderWriter::WriteResult writeNode(const osg::Node& obj, const std::string& fileName,const Options* options); |
|---|
| 83 | |
|---|
| 84 | virtual ReaderWriter::WriteResult writeShader(const osg::Shader& obj, const std::string& fileName,const Options* options); |
|---|
| 85 | |
|---|
| 86 | protected: |
|---|
| 87 | virtual ~WriteFileCallback() {} |
|---|
| 88 | }; |
|---|
| 89 | |
|---|
| 90 | class OSGDB_EXPORT FileLocationCallback : public virtual osg::Referenced |
|---|
| 91 | { |
|---|
| 92 | public: |
|---|
| 93 | |
|---|
| 94 | enum Location |
|---|
| 95 | { |
|---|
| 96 | LOCAL_FILE, |
|---|
| 97 | REMOTE_FILE |
|---|
| 98 | }; |
|---|
| 99 | |
|---|
| 100 | virtual Location fileLocation(const std::string& filename, const Options* options) = 0; |
|---|
| 101 | |
|---|
| 102 | virtual bool useFileCache() const = 0; |
|---|
| 103 | |
|---|
| 104 | protected: |
|---|
| 105 | virtual ~FileLocationCallback() {} |
|---|
| 106 | }; |
|---|
| 107 | |
|---|
| 108 | |
|---|
| 109 | /** Options base class used for passing options into plugins to control their operation.*/ |
|---|
| 110 | class OSGDB_EXPORT Options : public osg::Object |
|---|
| 111 | { |
|---|
| 112 | public: |
|---|
| 113 | |
|---|
| 114 | /// bit mask for setting up which object types get cached by readObject/Image/HeightField/Node(filename) calls |
|---|
| 115 | enum CacheHintOptions |
|---|
| 116 | { /// do not cache objects of any type |
|---|
| 117 | CACHE_NONE = 0, |
|---|
| 118 | |
|---|
| 119 | /// cache nodes loaded via readNode(filename) |
|---|
| 120 | CACHE_NODES = 1<<0, |
|---|
| 121 | |
|---|
| 122 | /// cache images loaded via readImage(filename) |
|---|
| 123 | CACHE_IMAGES = 1<<1, |
|---|
| 124 | |
|---|
| 125 | /// cache heightfield loaded via readHeightField(filename) |
|---|
| 126 | CACHE_HEIGHTFIELDS = 1<<2, |
|---|
| 127 | |
|---|
| 128 | /// cache heightfield loaded via readHeightField(filename) |
|---|
| 129 | CACHE_ARCHIVES = 1<<3, |
|---|
| 130 | |
|---|
| 131 | /// cache objects loaded via readObject(filename) |
|---|
| 132 | CACHE_OBJECTS = 1<<4, |
|---|
| 133 | |
|---|
| 134 | /// cache shaders loaded via readShader(filename) |
|---|
| 135 | CACHE_SHADERS = 1<<5, |
|---|
| 136 | |
|---|
| 137 | /// cache on all read*(filename) calls |
|---|
| 138 | CACHE_ALL = CACHE_NODES | |
|---|
| 139 | CACHE_IMAGES | |
|---|
| 140 | CACHE_HEIGHTFIELDS | |
|---|
| 141 | CACHE_ARCHIVES | |
|---|
| 142 | CACHE_OBJECTS | |
|---|
| 143 | CACHE_SHADERS |
|---|
| 144 | }; |
|---|
| 145 | |
|---|
| 146 | /// range of options of whether to build kdtrees automatically on loading |
|---|
| 147 | enum BuildKdTreesHint |
|---|
| 148 | { |
|---|
| 149 | NO_PREFERENCE, |
|---|
| 150 | DO_NOT_BUILD_KDTREES, |
|---|
| 151 | BUILD_KDTREES |
|---|
| 152 | }; |
|---|
| 153 | |
|---|
| 154 | |
|---|
| 155 | Options(): |
|---|
| 156 | osg::Object(true), |
|---|
| 157 | _objectCacheHint(CACHE_ARCHIVES), |
|---|
| 158 | _buildKdTreesHint(NO_PREFERENCE) {} |
|---|
| 159 | |
|---|
| 160 | Options(const std::string& str): |
|---|
| 161 | osg::Object(true), |
|---|
| 162 | _str(str), |
|---|
| 163 | _objectCacheHint(CACHE_ARCHIVES), |
|---|
| 164 | _buildKdTreesHint(NO_PREFERENCE) {} |
|---|
| 165 | |
|---|
| 166 | Options(const Options& options,const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY); |
|---|
| 167 | |
|---|
| 168 | META_Object(osgDB,Options); |
|---|
| 169 | |
|---|
| 170 | /** Set the general Options string.*/ |
|---|
| 171 | void setOptionString(const std::string& str) { _str = str; } |
|---|
| 172 | |
|---|
| 173 | /** Get the general Options string.*/ |
|---|
| 174 | const std::string& getOptionString() const { return _str; } |
|---|
| 175 | |
|---|
| 176 | /** Set the database path to use a hint of where to look when loading models.*/ |
|---|
| 177 | void setDatabasePath(const std::string& str) { _databasePaths.clear(); _databasePaths.push_back(str); } |
|---|
| 178 | |
|---|
| 179 | /** Get the database path which is used a hint of where to look when loading models.*/ |
|---|
| 180 | FilePathList& getDatabasePathList() { return _databasePaths; } |
|---|
| 181 | |
|---|
| 182 | /** Get the const database path which is used a hint of where to look when loading models.*/ |
|---|
| 183 | const FilePathList& getDatabasePathList() const { return _databasePaths; } |
|---|
| 184 | |
|---|
| 185 | |
|---|
| 186 | /** Set whether the Registry::ObjectCache should be used by default.*/ |
|---|
| 187 | void setObjectCacheHint(CacheHintOptions useObjectCache) { _objectCacheHint = useObjectCache; } |
|---|
| 188 | |
|---|
| 189 | /** Get whether the Registry::ObjectCache should be used by default.*/ |
|---|
| 190 | CacheHintOptions getObjectCacheHint() const { return _objectCacheHint; } |
|---|
| 191 | |
|---|
| 192 | |
|---|
| 193 | /** Set whether the KdTrees should be built for geometry in the loader model. */ |
|---|
| 194 | void setBuildKdTreesHint(BuildKdTreesHint hint) { _buildKdTreesHint = hint; } |
|---|
| 195 | |
|---|
| 196 | /** Get whether the KdTrees should be built for geometry in the loader model. */ |
|---|
| 197 | BuildKdTreesHint getBuildKdTreesHint() const { return _buildKdTreesHint; } |
|---|
| 198 | |
|---|
| 199 | |
|---|
| 200 | /** Set the password map to be used by plugins when access files from secure locations.*/ |
|---|
| 201 | void setAuthenticationMap(AuthenticationMap* authenticationMap) { _authenticationMap = authenticationMap; } |
|---|
| 202 | |
|---|
| 203 | /** Get the password map to be used by plugins when access files from secure locations.*/ |
|---|
| 204 | const AuthenticationMap* getAuthenticationMap() const { return _authenticationMap.get(); } |
|---|
| 205 | |
|---|
| 206 | |
|---|
| 207 | /** Sets a plugindata value PluginData with a string */ |
|---|
| 208 | void setPluginData(const std::string& s, void* v) const { _pluginData[s] = v; } |
|---|
| 209 | |
|---|
| 210 | /** Get a value from the PluginData */ |
|---|
| 211 | void* getPluginData(const std::string& s) { return _pluginData[s]; } |
|---|
| 212 | |
|---|
| 213 | /** Get a value from the PluginData */ |
|---|
| 214 | const void* getPluginData(const std::string& s) const |
|---|
| 215 | { |
|---|
| 216 | PluginDataMap::const_iterator itr = _pluginData.find(s); |
|---|
| 217 | return (itr == _pluginData.end()) ? 0 : itr->second; |
|---|
| 218 | } |
|---|
| 219 | |
|---|
| 220 | /** Remove a value from the PluginData */ |
|---|
| 221 | void removePluginData(const std::string& s) const { _pluginData.erase(s); } |
|---|
| 222 | |
|---|
| 223 | |
|---|
| 224 | /** Sets a plugindata value PluginData with a string */ |
|---|
| 225 | void setPluginStringData(const std::string& s, const std::string& v) const { _pluginStringData[s] = v; } |
|---|
| 226 | |
|---|
| 227 | /** Get a string from the PluginStrData */ |
|---|
| 228 | std::string getPluginStringData(const std::string& s) { return _pluginStringData[s]; } |
|---|
| 229 | |
|---|
| 230 | /** Get a value from the PluginData */ |
|---|
| 231 | const std::string getPluginStringData(const std::string& s) const |
|---|
| 232 | { |
|---|
| 233 | PluginStringDataMap::const_iterator itr = _pluginStringData.find(s); |
|---|
| 234 | return (itr == _pluginStringData.end()) ? std::string("") : itr->second; |
|---|
| 235 | } |
|---|
| 236 | |
|---|
| 237 | /** Remove a value from the PluginData */ |
|---|
| 238 | void removePluginStringData(const std::string& s) const { _pluginStringData.erase(s); } |
|---|
| 239 | |
|---|
| 240 | |
|---|
| 241 | |
|---|
| 242 | /** Set the find callback to use in place of the default findFile calls.*/ |
|---|
| 243 | void setFindFileCallback( FindFileCallback* cb) { _findFileCallback = cb; } |
|---|
| 244 | |
|---|
| 245 | /** Get the const findFile callback.*/ |
|---|
| 246 | FindFileCallback* getFindFileCallback() const { return _findFileCallback.get(); } |
|---|
| 247 | |
|---|
| 248 | |
|---|
| 249 | /** Set the read callback to use in place of the default readFile calls.*/ |
|---|
| 250 | void setReadFileCallback( ReadFileCallback* cb) { _readFileCallback = cb; } |
|---|
| 251 | |
|---|
| 252 | /** Get the const readFile callback.*/ |
|---|
| 253 | ReadFileCallback* getReadFileCallback() const { return _readFileCallback.get(); } |
|---|
| 254 | |
|---|
| 255 | |
|---|
| 256 | /** Set the callback to use in place of the default writeFile calls.*/ |
|---|
| 257 | void setWriteFileCallback( WriteFileCallback* cb) { _writeFileCallback = cb; } |
|---|
| 258 | |
|---|
| 259 | /** Get the const writeFile callback.*/ |
|---|
| 260 | WriteFileCallback* getWriteFileCallback() const { return _writeFileCallback.get(); } |
|---|
| 261 | |
|---|
| 262 | |
|---|
| 263 | /** Set the callback to use inform the DatabasePager whether a file is located on local or remote file system..*/ |
|---|
| 264 | void setFileLocationCallback( FileLocationCallback* cb) { _fileLocationCallback = cb; } |
|---|
| 265 | |
|---|
| 266 | /** Get the callback to use inform the DatabasePager whether a file is located on local or remote file system..*/ |
|---|
| 267 | FileLocationCallback* getFileLocationCallback() const { return _fileLocationCallback.get(); } |
|---|
| 268 | |
|---|
| 269 | |
|---|
| 270 | /** Set the FileCache that is used to manage local storage of files downloaded from the internet.*/ |
|---|
| 271 | void setFileCache(FileCache* fileCache) { _fileCache = fileCache; } |
|---|
| 272 | |
|---|
| 273 | /** Get the FileCache that is used to manage local storage of files downloaded from the internet.*/ |
|---|
| 274 | FileCache* getFileCache() const { return _fileCache.get(); } |
|---|
| 275 | |
|---|
| 276 | |
|---|
| 277 | protected: |
|---|
| 278 | |
|---|
| 279 | virtual ~Options() {} |
|---|
| 280 | |
|---|
| 281 | std::string _str; |
|---|
| 282 | FilePathList _databasePaths; |
|---|
| 283 | CacheHintOptions _objectCacheHint; |
|---|
| 284 | BuildKdTreesHint _buildKdTreesHint; |
|---|
| 285 | osg::ref_ptr<AuthenticationMap> _authenticationMap; |
|---|
| 286 | |
|---|
| 287 | typedef std::map<std::string,void*> PluginDataMap; |
|---|
| 288 | mutable PluginDataMap _pluginData; |
|---|
| 289 | typedef std::map<std::string,std::string> PluginStringDataMap; |
|---|
| 290 | mutable PluginStringDataMap _pluginStringData; |
|---|
| 291 | |
|---|
| 292 | osg::ref_ptr<FindFileCallback> _findFileCallback; |
|---|
| 293 | osg::ref_ptr<ReadFileCallback> _readFileCallback; |
|---|
| 294 | osg::ref_ptr<WriteFileCallback> _writeFileCallback; |
|---|
| 295 | osg::ref_ptr<FileLocationCallback> _fileLocationCallback; |
|---|
| 296 | |
|---|
| 297 | osg::ref_ptr<FileCache> _fileCache; |
|---|
| 298 | }; |
|---|
| 299 | |
|---|
| 300 | } |
|---|
| 301 | |
|---|
| 302 | #endif // OSGDB_OPTIONS |
|---|