| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | #include <osgDB/FileCache> |
|---|
| 15 | #include <osgDB/FileUtils> |
|---|
| 16 | #include <osgDB/FileNameUtils> |
|---|
| 17 | #include <osgDB/ReadFile> |
|---|
| 18 | #include <osgDB/WriteFile> |
|---|
| 19 | |
|---|
| 20 | using namespace osgDB; |
|---|
| 21 | |
|---|
| 22 | FileCache::FileCache(const std::string& path): |
|---|
| 23 | _fileCachePath(path) |
|---|
| 24 | { |
|---|
| 25 | osg::notify(osg::INFO)<<"Constructed FileCache : "<<path<<std::endl; |
|---|
| 26 | } |
|---|
| 27 | |
|---|
| 28 | FileCache::~FileCache() |
|---|
| 29 | { |
|---|
| 30 | osg::notify(osg::INFO)<<"Destructed FileCache "<<std::endl; |
|---|
| 31 | } |
|---|
| 32 | |
|---|
| 33 | bool FileCache::isFileAppropriateForFileCache(const std::string& originalFileName) const |
|---|
| 34 | { |
|---|
| 35 | return osgDB::containsServerAddress(originalFileName); |
|---|
| 36 | } |
|---|
| 37 | |
|---|
| 38 | std::string FileCache::createCacheFileName(const std::string& originalFileName) const |
|---|
| 39 | { |
|---|
| 40 | std::string cacheFileName = _fileCachePath + "/" + |
|---|
| 41 | osgDB::getServerAddress(originalFileName) + "/" + |
|---|
| 42 | osgDB::getServerFileName(originalFileName); |
|---|
| 43 | |
|---|
| 44 | osg::notify(osg::INFO)<<"FileCache::createCacheFileName("<<originalFileName<<") = "<<cacheFileName<<std::endl; |
|---|
| 45 | |
|---|
| 46 | return cacheFileName; |
|---|
| 47 | } |
|---|
| 48 | |
|---|
| 49 | bool FileCache::existsInCache(const std::string& originalFileName) const |
|---|
| 50 | { |
|---|
| 51 | return osgDB::fileExists(createCacheFileName(originalFileName)); |
|---|
| 52 | } |
|---|
| 53 | |
|---|
| 54 | ReaderWriter::ReadResult FileCache::readNode(const std::string& originalFileName, const osgDB::Options* options, bool buildKdTreeIfRequired) const |
|---|
| 55 | { |
|---|
| 56 | std::string cacheFileName = createCacheFileName(originalFileName); |
|---|
| 57 | if (!cacheFileName.empty() && osgDB::fileExists(cacheFileName)) |
|---|
| 58 | { |
|---|
| 59 | osg::notify(osg::INFO)<<"FileCache::readNodeFromCache("<<originalFileName<<") as "<<cacheFileName<<std::endl; |
|---|
| 60 | return osgDB::Registry::instance()->readNode(cacheFileName, options, buildKdTreeIfRequired); |
|---|
| 61 | } |
|---|
| 62 | else |
|---|
| 63 | { |
|---|
| 64 | return 0; |
|---|
| 65 | } |
|---|
| 66 | } |
|---|
| 67 | |
|---|
| 68 | ReaderWriter::WriteResult FileCache::writeNode(const osg::Node& node, const std::string& originalFileName, const osgDB::Options* options) const |
|---|
| 69 | { |
|---|
| 70 | std::string cacheFileName = createCacheFileName(originalFileName); |
|---|
| 71 | if (!cacheFileName.empty()) |
|---|
| 72 | { |
|---|
| 73 | std::string path = osgDB::getFilePath(cacheFileName); |
|---|
| 74 | |
|---|
| 75 | if (!osgDB::fileExists(path) && !osgDB::makeDirectory(path)) |
|---|
| 76 | { |
|---|
| 77 | osg::notify(osg::NOTICE)<<"Could not create cache directory: "<<path<<std::endl; |
|---|
| 78 | return ReaderWriter::WriteResult::ERROR_IN_WRITING_FILE; |
|---|
| 79 | } |
|---|
| 80 | |
|---|
| 81 | osg::notify(osg::INFO)<<"FileCache::writeNodeToCache("<<originalFileName<<") as "<<cacheFileName<<std::endl; |
|---|
| 82 | return osgDB::Registry::instance()->writeNode(node, cacheFileName, options); |
|---|
| 83 | } |
|---|
| 84 | return ReaderWriter::WriteResult::FILE_NOT_HANDLED; |
|---|
| 85 | } |
|---|