| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | #include <osg/Notify> |
|---|
| 15 | #include <osgDB/Archive> |
|---|
| 16 | #include <osgDB/FileNameUtils> |
|---|
| 17 | |
|---|
| 18 | #include <OpenThreads/ScopedLock> |
|---|
| 19 | #include <OpenThreads/ReentrantMutex> |
|---|
| 20 | |
|---|
| 21 | #define SERIALIZER() OpenThreads::ScopedLock<OpenThreads::ReentrantMutex> lock(_serializerMutex) |
|---|
| 22 | |
|---|
| 23 | class OSGA_Archive : public osgDB::Archive |
|---|
| 24 | { |
|---|
| 25 | public: |
|---|
| 26 | OSGA_Archive(); |
|---|
| 27 | virtual ~OSGA_Archive(); |
|---|
| 28 | |
|---|
| 29 | virtual const char* libraryName() const { return "osga"; } |
|---|
| 30 | |
|---|
| 31 | virtual const char* className() const { return "Archive"; } |
|---|
| 32 | |
|---|
| 33 | virtual bool acceptsExtension(const std::string& extension) const |
|---|
| 34 | { |
|---|
| 35 | return osgDB::equalCaseInsensitive(extension,"osga"); |
|---|
| 36 | } |
|---|
| 37 | |
|---|
| 38 | |
|---|
| 39 | virtual bool open(const std::string& filename, ArchiveStatus status, unsigned int indexBlockSizeHint=4096); |
|---|
| 40 | |
|---|
| 41 | |
|---|
| 42 | virtual bool open(std::istream& fin); |
|---|
| 43 | |
|---|
| 44 | |
|---|
| 45 | virtual void close(); |
|---|
| 46 | |
|---|
| 47 | |
|---|
| 48 | virtual std::string getArchiveFileName() const { return _archiveFileName; } |
|---|
| 49 | |
|---|
| 50 | |
|---|
| 51 | virtual std::string getMasterFileName() const; |
|---|
| 52 | |
|---|
| 53 | |
|---|
| 54 | virtual bool fileExists(const std::string& filename) const; |
|---|
| 55 | |
|---|
| 56 | |
|---|
| 57 | virtual osgDB::FileType getFileType(const std::string& filename) const; |
|---|
| 58 | |
|---|
| 59 | |
|---|
| 60 | virtual bool getFileNames(FileNameList& fileNameList) const; |
|---|
| 61 | |
|---|
| 62 | |
|---|
| 63 | |
|---|
| 64 | virtual ReadResult readObject(const std::string& fileName,const Options* options=NULL) const; |
|---|
| 65 | |
|---|
| 66 | |
|---|
| 67 | virtual ReadResult readImage(const std::string& fileName,const Options* options=NULL) const; |
|---|
| 68 | |
|---|
| 69 | |
|---|
| 70 | virtual ReadResult readHeightField(const std::string& fileName,const Options* options=NULL) const; |
|---|
| 71 | |
|---|
| 72 | |
|---|
| 73 | virtual ReadResult readNode(const std::string& fileName,const Options* options=NULL) const; |
|---|
| 74 | |
|---|
| 75 | |
|---|
| 76 | virtual ReadResult readShader(const std::string& fileName,const Options* options=NULL) const; |
|---|
| 77 | |
|---|
| 78 | |
|---|
| 79 | virtual WriteResult writeObject(const osg::Object& obj,const std::string& fileName,const Options* options=NULL) const; |
|---|
| 80 | |
|---|
| 81 | |
|---|
| 82 | virtual WriteResult writeImage(const osg::Image& image,const std::string& fileName,const Options* options=NULL) const; |
|---|
| 83 | |
|---|
| 84 | |
|---|
| 85 | virtual WriteResult writeHeightField(const osg::HeightField& heightField,const std::string& fileName,const Options* options=NULL) const; |
|---|
| 86 | |
|---|
| 87 | |
|---|
| 88 | virtual WriteResult writeNode(const osg::Node& node,const std::string& fileName,const Options* options=NULL) const; |
|---|
| 89 | |
|---|
| 90 | |
|---|
| 91 | virtual WriteResult writeShader(const osg::Shader& shader,const std::string& fileName,const Options* options=NULL) const; |
|---|
| 92 | |
|---|
| 93 | #if defined(_MSC_VER) |
|---|
| 94 | typedef __int64 pos_type; |
|---|
| 95 | typedef __int64 size_type; |
|---|
| 96 | #else |
|---|
| 97 | typedef unsigned long long pos_type; |
|---|
| 98 | typedef unsigned long long size_type; |
|---|
| 99 | #endif |
|---|
| 100 | |
|---|
| 101 | typedef std::pair<pos_type, size_type> PositionSizePair; |
|---|
| 102 | typedef std::map<std::string, PositionSizePair> FileNamePositionMap; |
|---|
| 103 | |
|---|
| 104 | protected: |
|---|
| 105 | |
|---|
| 106 | mutable OpenThreads::ReentrantMutex _serializerMutex; |
|---|
| 107 | |
|---|
| 108 | class IndexBlock; |
|---|
| 109 | friend class IndexBlock; |
|---|
| 110 | |
|---|
| 111 | class IndexBlock : public osg::Referenced |
|---|
| 112 | { |
|---|
| 113 | public: |
|---|
| 114 | IndexBlock(unsigned int blockSize=0); |
|---|
| 115 | |
|---|
| 116 | inline pos_type getPosition() const { return _filePosition; } |
|---|
| 117 | |
|---|
| 118 | inline unsigned int getBlockSize() const { return _blockSize; } |
|---|
| 119 | |
|---|
| 120 | |
|---|
| 121 | void setPositionNextIndexBlock(pos_type position); |
|---|
| 122 | |
|---|
| 123 | inline pos_type getPositionNextIndexBlock() const { return _filePositionNextIndexBlock; } |
|---|
| 124 | |
|---|
| 125 | |
|---|
| 126 | static IndexBlock* read(std::istream& in, bool doEndianSwap); |
|---|
| 127 | |
|---|
| 128 | std::string getFirstFileName() const; |
|---|
| 129 | |
|---|
| 130 | bool getFileReferences(FileNamePositionMap& indexMap) const; |
|---|
| 131 | |
|---|
| 132 | |
|---|
| 133 | inline bool requiresWrite() const { return _requiresWrite; } |
|---|
| 134 | |
|---|
| 135 | void write(std::ostream& out); |
|---|
| 136 | |
|---|
| 137 | inline bool spaceAvailable(pos_type, size_type, const std::string& filename) const |
|---|
| 138 | { |
|---|
| 139 | unsigned requiredSize = sizeof(pos_type)+sizeof(size_type)+sizeof(unsigned int)+filename.size(); |
|---|
| 140 | return (_offsetOfNextAvailableSpace + requiredSize)<_blockSize; |
|---|
| 141 | } |
|---|
| 142 | |
|---|
| 143 | bool addFileReference(pos_type position, size_type size, const std::string& filename); |
|---|
| 144 | |
|---|
| 145 | |
|---|
| 146 | |
|---|
| 147 | protected: |
|---|
| 148 | |
|---|
| 149 | void allocateData(unsigned int blockSize); |
|---|
| 150 | |
|---|
| 151 | virtual ~IndexBlock(); |
|---|
| 152 | bool _requiresWrite; |
|---|
| 153 | pos_type _filePosition; |
|---|
| 154 | |
|---|
| 155 | unsigned int _blockSize; |
|---|
| 156 | pos_type _filePositionNextIndexBlock; |
|---|
| 157 | unsigned int _offsetOfNextAvailableSpace; |
|---|
| 158 | char* _data; |
|---|
| 159 | }; |
|---|
| 160 | |
|---|
| 161 | public: |
|---|
| 162 | |
|---|
| 163 | struct ReadFunctor |
|---|
| 164 | { |
|---|
| 165 | ReadFunctor(const std::string& filename, const osgDB::ReaderWriter::Options* options): |
|---|
| 166 | _filename(filename), |
|---|
| 167 | _options(options) {} |
|---|
| 168 | |
|---|
| 169 | virtual ~ReadFunctor() {} |
|---|
| 170 | virtual osgDB::ReaderWriter::ReadResult doRead(osgDB::ReaderWriter& rw, std::istream& input) const = 0; |
|---|
| 171 | |
|---|
| 172 | std::string _filename; |
|---|
| 173 | const osgDB::ReaderWriter::Options* _options; |
|---|
| 174 | }; |
|---|
| 175 | |
|---|
| 176 | |
|---|
| 177 | struct WriteFunctor |
|---|
| 178 | { |
|---|
| 179 | WriteFunctor(const std::string& filename, const osgDB::ReaderWriter::Options* options): |
|---|
| 180 | _filename(filename), |
|---|
| 181 | _options(options) {} |
|---|
| 182 | |
|---|
| 183 | virtual ~WriteFunctor() {} |
|---|
| 184 | virtual osgDB::ReaderWriter::WriteResult doWrite(osgDB::ReaderWriter& rw, std::ostream& output) const = 0; |
|---|
| 185 | |
|---|
| 186 | std::string _filename; |
|---|
| 187 | const osgDB::ReaderWriter::Options* _options; |
|---|
| 188 | }; |
|---|
| 189 | |
|---|
| 190 | protected: |
|---|
| 191 | struct ReadObjectFunctor; |
|---|
| 192 | struct ReadImageFunctor; |
|---|
| 193 | struct ReadHeightFieldFunctor; |
|---|
| 194 | struct ReadNodeFunctor; |
|---|
| 195 | struct ReadShaderFunctor; |
|---|
| 196 | |
|---|
| 197 | |
|---|
| 198 | struct WriteObjectFunctor; |
|---|
| 199 | struct WriteImageFunctor; |
|---|
| 200 | struct WriteHeightFieldFunctor; |
|---|
| 201 | struct WriteNodeFunctor; |
|---|
| 202 | struct WriteShaderFunctor; |
|---|
| 203 | |
|---|
| 204 | |
|---|
| 205 | osgDB::ReaderWriter::ReadResult read(const ReadFunctor& readFunctor); |
|---|
| 206 | osgDB::ReaderWriter::WriteResult write(const WriteFunctor& writeFunctor); |
|---|
| 207 | |
|---|
| 208 | typedef std::list< osg::ref_ptr<IndexBlock> > IndexBlockList; |
|---|
| 209 | |
|---|
| 210 | bool _open(std::istream& fin); |
|---|
| 211 | |
|---|
| 212 | void writeIndexBlocks(); |
|---|
| 213 | |
|---|
| 214 | bool addFileReference(pos_type position, size_type size, const std::string& fileName); |
|---|
| 215 | |
|---|
| 216 | static float s_currentSupportedVersion; |
|---|
| 217 | float _version; |
|---|
| 218 | ArchiveStatus _status; |
|---|
| 219 | osgDB::ifstream _input; |
|---|
| 220 | osgDB::fstream _output; |
|---|
| 221 | |
|---|
| 222 | std::string _archiveFileName; |
|---|
| 223 | std::string _masterFileName; |
|---|
| 224 | IndexBlockList _indexBlockList; |
|---|
| 225 | FileNamePositionMap _indexMap; |
|---|
| 226 | |
|---|
| 227 | |
|---|
| 228 | template <typename T> |
|---|
| 229 | static inline void _write(char* ptr, const T& value) |
|---|
| 230 | { |
|---|
| 231 | std::copy(reinterpret_cast<const char*>(&value),reinterpret_cast<const char*>(&value)+sizeof(value),ptr); |
|---|
| 232 | } |
|---|
| 233 | |
|---|
| 234 | template <typename T> |
|---|
| 235 | static inline void _read(char* ptr, T& value) |
|---|
| 236 | { |
|---|
| 237 | std::copy(ptr,ptr+sizeof(value),reinterpret_cast<char*>(&value)); |
|---|
| 238 | } |
|---|
| 239 | |
|---|
| 240 | }; |
|---|