2010-01-21 04:13:33 +08:00
|
|
|
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2010 Robert Osfield
|
|
|
|
*
|
|
|
|
* This library is open source and may be redistributed and/or modified under
|
|
|
|
* the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
|
|
|
|
* (at your option) any later version. The full license is in LICENSE file
|
|
|
|
* included with this distribution, and on the openscenegraph.org website.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* OpenSceneGraph Public License for more details.
|
|
|
|
*/
|
|
|
|
// Written by Wang Rui, (C) 2010
|
|
|
|
|
2010-01-25 19:03:21 +08:00
|
|
|
#ifndef OSGDB_INPUTSTREAM
|
|
|
|
#define OSGDB_INPUTSTREAM
|
2010-01-21 04:13:33 +08:00
|
|
|
|
|
|
|
#include <osg/Endian>
|
|
|
|
#include <osg/Vec2>
|
|
|
|
#include <osg/Vec3>
|
|
|
|
#include <osg/Vec4>
|
2013-06-03 21:13:18 +08:00
|
|
|
#include <osg/Vec2i>
|
|
|
|
#include <osg/Vec3i>
|
|
|
|
#include <osg/Vec4i>
|
|
|
|
#include <osg/Vec2ui>
|
|
|
|
#include <osg/Vec3ui>
|
|
|
|
#include <osg/Vec4ui>
|
2010-01-21 04:13:33 +08:00
|
|
|
#include <osg/Quat>
|
|
|
|
#include <osg/Matrix>
|
|
|
|
#include <osg/Array>
|
|
|
|
#include <osg/PrimitiveSet>
|
|
|
|
#include <osgDB/ReaderWriter>
|
2010-01-25 19:03:21 +08:00
|
|
|
#include <osgDB/StreamOperator>
|
2011-01-13 03:29:24 +08:00
|
|
|
#include <osgDB/Options>
|
2010-01-21 04:13:33 +08:00
|
|
|
#include <iostream>
|
|
|
|
#include <sstream>
|
|
|
|
|
|
|
|
namespace osgDB
|
|
|
|
{
|
|
|
|
|
2010-01-28 01:09:05 +08:00
|
|
|
class InputException : public osg::Referenced
|
2010-01-21 04:13:33 +08:00
|
|
|
{
|
|
|
|
public:
|
2010-01-28 01:09:05 +08:00
|
|
|
InputException( const std::vector<std::string>& fields, const std::string& err ) : _error(err)
|
|
|
|
{
|
|
|
|
for ( unsigned int i=0; i<fields.size(); ++i )
|
|
|
|
{
|
|
|
|
_field += fields[i];
|
|
|
|
_field += " ";
|
|
|
|
}
|
|
|
|
}
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2010-01-21 04:13:33 +08:00
|
|
|
const std::string& getField() const { return _field; }
|
|
|
|
const std::string& getError() const { return _error; }
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2010-01-21 04:13:33 +08:00
|
|
|
protected:
|
|
|
|
std::string _field;
|
|
|
|
std::string _error;
|
|
|
|
};
|
|
|
|
|
2010-01-21 17:25:45 +08:00
|
|
|
class OSGDB_EXPORT InputStream
|
2010-01-21 04:13:33 +08:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
typedef std::map< unsigned int, osg::ref_ptr<osg::Array> > ArrayMap;
|
|
|
|
typedef std::map< unsigned int, osg::ref_ptr<osg::Object> > IdentifierMap;
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2010-01-21 04:13:33 +08:00
|
|
|
enum ReadType
|
|
|
|
{
|
|
|
|
READ_UNKNOWN = 0,
|
|
|
|
READ_SCENE,
|
2010-03-10 21:48:41 +08:00
|
|
|
READ_IMAGE,
|
|
|
|
READ_OBJECT
|
2010-01-21 04:13:33 +08:00
|
|
|
};
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2010-01-25 19:03:21 +08:00
|
|
|
InputStream( const osgDB::Options* options );
|
2010-01-21 04:13:33 +08:00
|
|
|
virtual ~InputStream();
|
2012-03-22 01:36:20 +08:00
|
|
|
|
From Wang Rui, "The file attached includes two new features for the serialization IO functionality. First, custom serializer version control should work now, just by defining a new REGISTER_CUSTOM_OBJECT_WRAPPER macro. For example:
// A custom class
namespace CustomDomain {
class MyGroup : public osg::Group
{
public:
META_Node( CustomDomain, MyGroup );
void setMyName( const std::string& n );
const std::string& getMyName() const;
void setMyID( int id );
int getMyID() const;
...
};
}
// The serialization wrapper using a custom domain name
REGISTER_CUSTOM_OBJECT_WRAPPER( MyDomain,
CustomDomain_MyGroup,
new CustomDomain::MyGroup,
CustomDomain::MyGroup,
"osg::Object osg::Node osg::Group CustomDomain::MyGroup" )
{
ADD_STRING_SERIALIZER( MyName, std::string() );
{
UPDATE_TO_VERSION_SCOPED( 1 ); // Updated for a new domain version
ADD_INT_SERIALIZER( MyID, 0 );
}
}
Save the class instance as follows:
osgDB::writeNodeFile( *myGroup, "serializer_test.osgt", new osgDB::Options("CustomDomains=MyDomain:1") );
The output file will include the domain version definition and all the class data, and can be read back. We can also force setting the domain version by the CustomDomains option while reading the saved files. If we save the class instance without any options, MyID will be ignored because the default domain version is 0.
This may help third-party libraries like osgEarth to maintain their own serializers without regarding to the OSG soversion changes.
Another feature added is a more robust binary format, which in fact adds a size-offset at each block's beginning. When there are problems or unsupported data types while reading, we can now directly jump to the block end indicated by the offset value. So a .osgb file will automatically ignore bad data and read remains as normal (at present it will fail at all). This feature will not break the backward compatibility, and can be disabled by setting "RobustBinaryFormat=false" while writing out.
Hope these changes can work smoothly with present and future community projects. Maybe we should also consider have an osgserializer example to test and demonstrate all things we can do now."
2013-06-24 16:48:55 +08:00
|
|
|
void setFileVersion( const std::string& d, int v ) { _domainVersionMap[d] = v; }
|
|
|
|
int getFileVersion( const std::string& d=std::string() ) const;
|
|
|
|
|
2010-01-25 19:03:21 +08:00
|
|
|
bool isBinary() const { return _in->isBinary(); }
|
2010-02-26 01:53:51 +08:00
|
|
|
const osgDB::Options* getOptions() const { return _options.get(); }
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2010-01-21 04:13:33 +08:00
|
|
|
// Serialization related functions
|
2010-01-28 01:09:05 +08:00
|
|
|
InputStream& operator>>( bool& b ) { _in->readBool(b); checkStream(); return *this; }
|
|
|
|
InputStream& operator>>( char& c ) { _in->readChar(c); checkStream(); return *this; }
|
|
|
|
InputStream& operator>>( signed char& c ) { _in->readSChar(c); checkStream(); return *this; }
|
|
|
|
InputStream& operator>>( unsigned char& c ) { _in->readUChar(c); checkStream(); return *this; }
|
|
|
|
InputStream& operator>>( short& s ) { _in->readShort(s); checkStream(); return *this; }
|
|
|
|
InputStream& operator>>( unsigned short& s ) { _in->readUShort(s); checkStream(); return *this; }
|
|
|
|
InputStream& operator>>( int& i ) { _in->readInt(i); checkStream(); return *this; }
|
|
|
|
InputStream& operator>>( unsigned int& i ) { _in->readUInt(i); checkStream(); return *this; }
|
|
|
|
InputStream& operator>>( long& l ) { _in->readLong(l); checkStream(); return *this; }
|
|
|
|
InputStream& operator>>( unsigned long& l ) { _in->readULong(l); checkStream(); return *this; }
|
|
|
|
InputStream& operator>>( float& f ) { _in->readFloat(f); checkStream(); return *this; }
|
|
|
|
InputStream& operator>>( double& d ) { _in->readDouble(d); checkStream(); return *this; }
|
|
|
|
InputStream& operator>>( std::string& s ) { _in->readString(s); checkStream(); return *this; }
|
|
|
|
InputStream& operator>>( std::istream& (*fn)(std::istream&) ) { _in->readStream(fn); checkStream(); return *this; }
|
|
|
|
InputStream& operator>>( std::ios_base& (*fn)(std::ios_base&) ) { _in->readBase(fn); checkStream(); return *this; }
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2010-01-28 01:09:05 +08:00
|
|
|
InputStream& operator>>( ObjectGLenum& value ) { _in->readGLenum(value); checkStream(); return *this; }
|
|
|
|
InputStream& operator>>( ObjectProperty& prop ) { _in->readProperty(prop); checkStream(); return *this; }
|
|
|
|
InputStream& operator>>( ObjectMark& mark ) { _in->readMark(mark); checkStream(); return *this; }
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2010-01-21 04:13:33 +08:00
|
|
|
InputStream& operator>>( osg::Vec2b& v );
|
|
|
|
InputStream& operator>>( osg::Vec3b& v );
|
|
|
|
InputStream& operator>>( osg::Vec4b& v );
|
|
|
|
InputStream& operator>>( osg::Vec4ub& v );
|
|
|
|
InputStream& operator>>( osg::Vec2s& v );
|
|
|
|
InputStream& operator>>( osg::Vec3s& v );
|
|
|
|
InputStream& operator>>( osg::Vec4s& v );
|
2013-06-03 21:13:18 +08:00
|
|
|
InputStream& operator>>( osg::Vec2i& v );
|
|
|
|
InputStream& operator>>( osg::Vec3i& v );
|
|
|
|
InputStream& operator>>( osg::Vec4i& v );
|
|
|
|
InputStream& operator>>( osg::Vec2ui& v );
|
|
|
|
InputStream& operator>>( osg::Vec3ui& v );
|
|
|
|
InputStream& operator>>( osg::Vec4ui& v );
|
2010-01-21 04:13:33 +08:00
|
|
|
InputStream& operator>>( osg::Vec2f& v );
|
|
|
|
InputStream& operator>>( osg::Vec3f& v );
|
|
|
|
InputStream& operator>>( osg::Vec4f& v );
|
|
|
|
InputStream& operator>>( osg::Vec2d& v );
|
|
|
|
InputStream& operator>>( osg::Vec3d& v );
|
|
|
|
InputStream& operator>>( osg::Vec4d& v );
|
|
|
|
InputStream& operator>>( osg::Quat& q );
|
|
|
|
InputStream& operator>>( osg::Plane& p );
|
|
|
|
InputStream& operator>>( osg::Matrixf& mat );
|
|
|
|
InputStream& operator>>( osg::Matrixd& mat );
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2010-01-21 04:13:33 +08:00
|
|
|
InputStream& operator>>( osg::Array*& a ) { a = readArray(); return *this; }
|
|
|
|
InputStream& operator>>( osg::Image*& img ) { img = readImage(); return *this; }
|
|
|
|
InputStream& operator>>( osg::PrimitiveSet*& p ) { p = readPrimitiveSet(); return *this; }
|
|
|
|
InputStream& operator>>( osg::Object*& obj ) { obj = readObject(); return *this; }
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2010-01-21 04:13:33 +08:00
|
|
|
InputStream& operator>>( osg::ref_ptr<osg::Array>& ptr ) { ptr = readArray(); return *this; }
|
|
|
|
InputStream& operator>>( osg::ref_ptr<osg::Image>& ptr ) { ptr = readImage(); return *this; }
|
|
|
|
InputStream& operator>>( osg::ref_ptr<osg::PrimitiveSet>& ptr ) { ptr = readPrimitiveSet(); return *this; }
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2010-01-21 04:13:33 +08:00
|
|
|
template<typename T> InputStream& operator>>( osg::ref_ptr<T>& ptr )
|
|
|
|
{ ptr = static_cast<T*>(readObject()); return *this; }
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2010-01-21 04:13:33 +08:00
|
|
|
// Convenient methods for reading
|
2010-03-10 21:48:41 +08:00
|
|
|
bool matchString( const std::string& str ) { return _in->matchString(str); }
|
|
|
|
void advanceToCurrentEndBracket() { _in->advanceToCurrentEndBracket(); }
|
|
|
|
void readWrappedString( std::string& str ) { _in->readWrappedString(str); checkStream(); }
|
2010-01-25 19:03:21 +08:00
|
|
|
void readCharArray( char* s, unsigned int size ) { _in->readCharArray(s, size); }
|
2012-02-29 18:22:56 +08:00
|
|
|
void readComponentArray( char* s, unsigned int numElements, unsigned int numComponentsPerElements, unsigned int componentSizeInBytes) { _in->readComponentArray( s, numElements, numComponentsPerElements, componentSizeInBytes); }
|
2010-02-11 01:03:09 +08:00
|
|
|
|
|
|
|
// readSize() use unsigned int for all sizes.
|
|
|
|
unsigned int readSize() { unsigned int size; *this>>size; return size; }
|
|
|
|
|
2010-01-21 04:13:33 +08:00
|
|
|
// Global reading functions
|
|
|
|
osg::Array* readArray();
|
|
|
|
osg::PrimitiveSet* readPrimitiveSet();
|
2011-01-14 19:00:11 +08:00
|
|
|
osg::Image* readImage(bool readFromExternal=true);
|
2010-01-21 04:13:33 +08:00
|
|
|
osg::Object* readObject( osg::Object* existingObj=0 );
|
From Miha Ravsel, "While trying to create my custom serializer class, i created some dummy data which accidentally popped-up bug in InputStream readObjectFields function.
Bug description:
Let's say we have class A
namespace Bug
{
class A : public osg::Object
{
public:
//...
typedef std::vector<osg::ref_ptr<A> > AList;
protected:
AList _alist;
//...
}
}
REGISTER_OBJECT_WRAPPER( A,
new Bug::A,
Bug::A,
"osg::Object Bug::A" )
{
ADD_LIST_SERIALIZER(A,Bug::A::AList);
}
Bug:
We create say 3 instances of class A: A1,A2,A3 and then we add A2 and A3 and A1 as child instances of A1 so we get next structure:
A1
|- A2,A3,A1
we call osgDB::writeObjectFile(A1,"/data/a.osgt") -> saved correctly( third element in list is saved as unique id that references parentClass
now we call
A1 = osgDB::readObjectFile("/data/a.osgt");
Everything is deserialized correctely except last element in list which should be same instance as parent A1.
The attached code resolves this issue by passing UniqueID in readObjectFields method and saving object in _identifierMap as soon as we have valid object instance so we can make reference to parent object from any child instance.
"
2012-02-22 18:46:35 +08:00
|
|
|
osg::Object* readObjectFields( const std::string& className, unsigned int id, osg::Object* existingObj=0);
|
2011-05-16 16:50:59 +08:00
|
|
|
|
|
|
|
/// set an input iterator, used directly when not using InputStream with a traditional file releated stream.
|
|
|
|
void setInputIterator( InputIterator* ii ) { _in = ii; }
|
|
|
|
|
|
|
|
/// start reading from InputStream treating it as a traditional file releated stream, handles headers and versioning
|
2010-01-25 19:03:21 +08:00
|
|
|
ReadType start( InputIterator* );
|
2011-05-16 16:50:59 +08:00
|
|
|
|
2010-01-21 04:13:33 +08:00
|
|
|
void decompress();
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2010-01-21 04:13:33 +08:00
|
|
|
// Schema handlers
|
|
|
|
void readSchema( std::istream& fin );
|
|
|
|
void resetSchema();
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2010-01-28 01:09:05 +08:00
|
|
|
// Exception handlers
|
|
|
|
inline void throwException( const std::string& msg );
|
|
|
|
const InputException* getException() const { return _exception.get(); }
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2012-04-05 21:53:47 +08:00
|
|
|
// Property & mask variables
|
|
|
|
ObjectProperty PROPERTY;
|
|
|
|
ObjectMark BEGIN_BRACKET;
|
|
|
|
ObjectMark END_BRACKET;
|
|
|
|
|
2010-01-21 04:13:33 +08:00
|
|
|
protected:
|
2010-01-28 01:09:05 +08:00
|
|
|
inline void checkStream();
|
2010-01-21 04:13:33 +08:00
|
|
|
void setWrapperSchema( const std::string& name, const std::string& properties );
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2010-01-21 04:13:33 +08:00
|
|
|
template<typename T>
|
2012-02-29 18:22:56 +08:00
|
|
|
void readArrayImplementation( T* a, unsigned int numComponentsPerElements, unsigned int componentSizeInBytes );
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2010-01-21 04:13:33 +08:00
|
|
|
ArrayMap _arrayMap;
|
|
|
|
IdentifierMap _identifierMap;
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2013-06-24 17:02:32 +08:00
|
|
|
typedef std::map<std::string, int> VersionMap;
|
|
|
|
VersionMap _domainVersionMap;
|
From Wang Rui, "I'd like to submit my latest modification of the serialization IO
functionalities. It includes two main parts: a version checking macro
for handling backward-compatiblity since 3.0, and enhencement of
current schema mechanism. I also change the option handling process to
use getPluginStringData(), and add new USE_SERIALIZER_WRAPPER macro in
the Registry header to allow for static-link usage as well.
The enhencement of schema machanism just tells the type of each
serializer while outputting them, such as:
osg::Group = Children:1
The meaning of the number can be found in the osgDB/Serializer header,
BaseSerializer::Type enum. It may help 3rdparty utilities understand
the structure of the wrapper and do some reflection work in the
future.
The new macro UPDATE_TO_VERSION can help indicate the InputStream (no
affect on the writer) that a serializer is added/removed since certain
OSG version. An example wrapper file is also attached. The
Geode_modified.cpp is based on the serializers/osg/Geode.cpp file
(hey, don't merge it :-), but assumes that a new user serializer
'Test' is added since version 65 (that is, the OSG_SOVERSION):
REGISTER_OBJECT_WRAPPER( Geode, ... )
{
ADD_USER_SERIALIZER( Drawables ); // origin ones
UPDATE_TO_VERSION( 65 )
{
ADD_USER_SERIALIZER( Test ); // a serializer added from version 65
}
}
All kinds of ADD_... macros following UPDATE_TO_VERSION will
automatically apply the updated version. The braces here are only for
typesetting!
While reading an osgt/osgb/osgx file, OSG will now check if the file
version (recorded as the writer's soversion, instead of previous
meaningless "#Version 2") is equal or greater than Test's version, and
try reading it, or just ignore it if file version is lesser.
And we also have the REMOVE_SERIALIZER macro will mark a named
serializer as removed in some version, with which all files generated
by further versions will just ignore it:
UPDATE_TO_VERSION( 70 )
{
REMOVE_SERIALIZER( Test );
}
This means that from version 70, the serializer Test is removed (but
not actually erased from the list) and should not be read anymore. If
the read file version is less than 70 (and equal or greater than 65),
Test will still be handled when reading; otherwise it will be ignored
to keep compatiblity on different OSG versions.
"
2010-11-09 20:41:55 +08:00
|
|
|
int _fileVersion;
|
2010-05-13 04:02:31 +08:00
|
|
|
bool _useSchemaData;
|
2010-01-28 01:09:05 +08:00
|
|
|
bool _forceReadingImage;
|
|
|
|
std::vector<std::string> _fields;
|
|
|
|
osg::ref_ptr<InputIterator> _in;
|
|
|
|
osg::ref_ptr<InputException> _exception;
|
2010-02-26 01:53:51 +08:00
|
|
|
osg::ref_ptr<const osgDB::Options> _options;
|
2010-06-15 18:02:34 +08:00
|
|
|
|
|
|
|
// store here to avoid a new and a leak in InputStream::decompress
|
|
|
|
std::stringstream* _dataDecompress;
|
2010-01-21 04:13:33 +08:00
|
|
|
};
|
|
|
|
|
2010-01-28 01:09:05 +08:00
|
|
|
void InputStream::throwException( const std::string& msg )
|
2010-01-21 04:13:33 +08:00
|
|
|
{
|
2010-01-28 01:09:05 +08:00
|
|
|
_exception = new InputException(_fields, msg);
|
2010-01-21 04:13:33 +08:00
|
|
|
}
|
|
|
|
|
2010-01-28 01:09:05 +08:00
|
|
|
void InputStream::checkStream()
|
2010-01-21 04:13:33 +08:00
|
|
|
{
|
2010-01-28 01:09:05 +08:00
|
|
|
_in->checkStream();
|
2010-01-25 19:03:21 +08:00
|
|
|
if ( _in->isFailed() )
|
2010-01-28 01:09:05 +08:00
|
|
|
throwException( "InputStream: Failed to read from stream." );
|
2010-01-21 04:13:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|