internal void WriteTypeMetadata(RSBinaryWriter _binaryWriter, Type _type, UInt32 _typeID)
        {
            if (_type == null)
            {
                throw new NullReferenceException("[RS] Serialize type metadata failed. Type is null.");
            }

            // Get assembly info
            bool     _newAssembly;
            UInt32   _assemblyID;
            Assembly _assembly = _type.Assembly;

            RegisterAssembly(_assembly, out _assemblyID, out _newAssembly);

            // Write assembly properties (if required)
            if (_newAssembly)
            {
                _binaryWriter.WriteBinaryElement(BinaryElement.ASSEMBLY);
                _binaryWriter.Write(_assemblyID);
                _binaryWriter.Write(_assembly.FullName);
            }

            // Write type properties
            _binaryWriter.WriteBinaryElement(BinaryElement.TYPE);
            _binaryWriter.Write(_assemblyID);
            _binaryWriter.Write(_typeID);
            _binaryWriter.Write(_type.FullName);
        }
 private void WriteStringTypeValue(RSBinaryWriter _binaryWriter, object _object, Type _objectType)
 {
     // Write string object data
     _binaryWriter.WriteBinaryElement(BinaryElement.OBJECT_DATA);
     _binaryWriter.WriteTypeTag(eTypeTag.STRING);
     _binaryWriter.Write(_object as string);
 }
        private void WritePrimitiveTypeValue(RSBinaryWriter _binaryWriter, object _object, Type _objectType)
        {
            int _typeID = (int)Type.GetTypeCode(_objectType);

            // Write primitive object data
            _binaryWriter.WriteBinaryElement(BinaryElement.OBJECT_DATA);
            _binaryWriter.WriteTypeTag(eTypeTag.PRIMITIVE);
            _binaryWriter.Write(_typeID);
            _binaryWriter.WritePrimitiveValue(_object, (TypeCode)_typeID);
        }
        private void WriteObjectGraph(RSBinaryWriter _binaryWriter, object _object, Type _objectType)
        {
            // Fetch properties that needs to be serialized
            RuntimeSerializationInfo _serializationInfo = new RuntimeSerializationInfo(_objectType);

            // Get serialization entries for this object
            GetObjectData(_object, ref _serializationInfo);

            // Write initializers, properties
            WriteSerializationContainer(_binaryWriter, _serializationInfo.InitializerValuesContainer);
            WriteSerializationContainer(_binaryWriter, _serializationInfo.SerializedValuesContainer);

            // Trigger serialization finished callback
            if (typeof(IRuntimeSerializationCallback).IsAssignableFrom(_objectType))
            {
                ((IRuntimeSerializationCallback)_object).OnAfterRuntimeSerialize();
            }
        }
Beispiel #5
0
        internal BinarySerializer()
        {
            BinaryStreamWriter = new RSBinaryWriter(512);
            int _SFVersion = Constants.kSerializationFormatVersion;

            // Based of serialization format select object writer
            switch (_SFVersion)
            {
            case 1:
                ObjectDataWriter = new ObjectWriterSFV1();
                break;

#pragma warning disable
            default:
                throw new Exception("[RS] Unimplemented serialization format version found!");
#pragma warning restore
            }
        }
        internal BinarySerializer()
        {
            BinaryStreamWriter			= new RSBinaryWriter(512);
            int	_SFVersion				= Constants.kSerializationFormatVersion;

            // Based of serialization format select object writer
            switch (_SFVersion)
            {
            case 1:
                ObjectDataWriter		= new ObjectWriterSFV1();
                break;

            #pragma warning disable
                default:
                    throw new Exception("[RS] Unimplemented serialization format version found!");
            #pragma warning restore
            }
        }
        private void WriteSerializationContainer(RSBinaryWriter _binaryWriter, Dictionary <string, RuntimeSerializationEntry> _container)
        {
            Dictionary <string, RuntimeSerializationEntry> .Enumerator _enumerator = _container.GetEnumerator();

            // Write count
            _binaryWriter.Write(_container.Count);

            // Write entries
            while (_enumerator.MoveNext())
            {
                RuntimeSerializationEntry _curEntry = _enumerator.Current.Value;

                // Write serialization entry
                _binaryWriter.Write(_curEntry.Name);

                // Write child properties
                WriteObjectValue(_binaryWriter, _curEntry.Value);
            }
        }
        private void WriteStructTypeValue(RSBinaryWriter _binaryWriter, object _object, Type _objectType)
        {
            // Register object graph type
            UInt32 _objectTypeID;
            bool   _newType;

            TypeMetadata.RegisterType(_objectType, out _objectTypeID, out _newType);

            if (_newType)
            {
                TypeMetadata.WriteTypeMetadata(_binaryWriter, _objectType, _objectTypeID);
            }

            // Write object properties
            _binaryWriter.WriteBinaryElement(BinaryElement.OBJECT_DATA);
            _binaryWriter.WriteTypeTag(eTypeTag.STRUCT);
            _binaryWriter.Write(_objectTypeID);

            // Write object graph
            WriteObjectGraph(_binaryWriter, _object, _objectType);
        }
        internal override void WriteObjectValue(RSBinaryWriter _binaryWriter, object _object)
        {
            if (_object == null || _object.Equals(null))
            {
                WriteUnsupportedTypeValue(_binaryWriter, eTypeTag.NULL);
                return;
            }

            // Handle serialization based on object type tag
            Type     _objectType = _object.GetType();
            eTypeTag _typeTag    = TypeMetadata.GetTypeTag(_objectType);

            switch (_typeTag)
            {
            case eTypeTag.UNSUPPORTED:
                Debug.LogWarning(string.Format("[RS] Serialization isnt supported for type={0}.", _objectType));
                WriteUnsupportedTypeValue(_binaryWriter, _typeTag);
                break;

            case eTypeTag.PRIMITIVE:
                WritePrimitiveTypeValue(_binaryWriter, _object, _objectType);
                break;

            case eTypeTag.STRUCT:
                WriteStructTypeValue(_binaryWriter, _object, _objectType);
                break;

            case  eTypeTag.STRING:
                WriteStringTypeValue(_binaryWriter, _object, _objectType);
                break;

            case eTypeTag.CLASS:
                WriteObjectReferenceTypeValue(_binaryWriter, _object, _objectType, _typeTag);
                break;

            default:
                Debug.LogWarning(string.Format("[RS] Unknown type={0}.", _typeTag));
                break;
            }
        }
        internal override void WriteObjectValue(RSBinaryWriter _binaryWriter, object _object)
        {
            if (_object == null || _object.Equals(null))
            {
                WriteUnsupportedTypeValue(_binaryWriter, eTypeTag.NULL);
                return;
            }

            // Handle serialization based on object type tag
            Type 		_objectType		= _object.GetType();
            eTypeTag 	_typeTag		= TypeMetadata.GetTypeTag(_objectType);

            switch (_typeTag)
            {
            case eTypeTag.UNSUPPORTED:
                Debug.LogWarning(string.Format("[RS] Serialization isnt supported for type={0}.", _objectType));
                WriteUnsupportedTypeValue(_binaryWriter, _typeTag);
                break;

            case eTypeTag.PRIMITIVE:
                WritePrimitiveTypeValue(_binaryWriter, _object, _objectType);
                break;

            case eTypeTag.STRUCT:
                WriteStructTypeValue(_binaryWriter, _object, _objectType);
                break;

            case  eTypeTag.STRING:
                WriteStringTypeValue(_binaryWriter, _object, _objectType);
                break;

            case eTypeTag.CLASS:
                WriteObjectReferenceTypeValue(_binaryWriter, _object, _objectType, _typeTag);
                break;

            default:
                Debug.LogWarning(string.Format("[RS] Unknown type={0}.", _typeTag));
                break;
            }
        }
        private void WriteObjectReferenceTypeValue(RSBinaryWriter _binaryWriter, object _object, Type _objectType, eTypeTag _typeTag)
        {
            // Check if this object exists in object reference cache
            bool   _firstTime;
            UInt32 _objectReferenceID;

            RegisterObject(_object, out _objectReferenceID, out _firstTime);

            if (_firstTime)
            {
                if (_typeTag == eTypeTag.CLASS)
                {
                    WriteClassTypeValue(_binaryWriter, _object, _objectType, _objectReferenceID);
                    return;
                }
            }
            else
            {
                _binaryWriter.WriteBinaryElement(BinaryElement.OBJECT_DATA);
                _binaryWriter.WriteTypeTag(eTypeTag.OBJECT_REFERENCE);
                _binaryWriter.Write(_objectReferenceID);
                return;
            }
        }
        private void WriteStructTypeValue(RSBinaryWriter _binaryWriter, object _object, Type _objectType)
        {
            // Register object graph type
            UInt32 	_objectTypeID;
            bool	_newType;

            TypeMetadata.RegisterType(_objectType, out _objectTypeID, out _newType);

            if (_newType)
                TypeMetadata.WriteTypeMetadata(_binaryWriter, _objectType, _objectTypeID);

            // Write object properties
            _binaryWriter.WriteBinaryElement(BinaryElement.OBJECT_DATA);
            _binaryWriter.WriteTypeTag(eTypeTag.STRUCT);
            _binaryWriter.Write(_objectTypeID);

            // Write object graph
            WriteObjectGraph(_binaryWriter, _object, _objectType);
        }
 internal abstract void WriteObjectValue(RSBinaryWriter _binaryWriter, object _object);
 private void WriteUnsupportedTypeValue(RSBinaryWriter _binaryWriter, eTypeTag _typeTag)
 {
     _binaryWriter.WriteBinaryElement(BinaryElement.OBJECT_DATA);
     _binaryWriter.WriteTypeTag(_typeTag);
 }
        private void WriteObjectGraph(RSBinaryWriter _binaryWriter, object _object, Type _objectType)
        {
            // Fetch properties that needs to be serialized
            RuntimeSerializationInfo 			_serializationInfo		= new RuntimeSerializationInfo(_objectType);

            // Get serialization entries for this object
            GetObjectData(_object, ref _serializationInfo);

            // Write initializers, properties
            WriteSerializationContainer(_binaryWriter, _serializationInfo.InitializerValuesContainer);
            WriteSerializationContainer(_binaryWriter, _serializationInfo.SerializedValuesContainer);

            // Trigger serialization finished callback
            if (typeof(IRuntimeSerializationCallback).IsAssignableFrom(_objectType))
                ((IRuntimeSerializationCallback)_object).OnAfterRuntimeSerialize();
        }
        private void WriteObjectReferenceTypeValue(RSBinaryWriter _binaryWriter, object _object, Type _objectType, eTypeTag _typeTag)
        {
            // Check if this object exists in object reference cache
            bool 	_firstTime;
            UInt32 	_objectReferenceID;

            RegisterObject(_object, out _objectReferenceID, out _firstTime);

            if (_firstTime)
            {
                if (_typeTag == eTypeTag.CLASS)
                {
                    WriteClassTypeValue(_binaryWriter, _object, _objectType, _objectReferenceID);
                    return;
                }
            }
            else
            {
                _binaryWriter.WriteBinaryElement(BinaryElement.OBJECT_DATA);
                _binaryWriter.WriteTypeTag(eTypeTag.OBJECT_REFERENCE);
                _binaryWriter.Write(_objectReferenceID);
                return;
            }
        }
Beispiel #17
0
 internal abstract void WriteObjectValue(RSBinaryWriter _binaryWriter, object _object);
        private void WritePrimitiveTypeValue(RSBinaryWriter _binaryWriter, object _object, Type _objectType)
        {
            int _typeID			= (int)Type.GetTypeCode(_objectType);

            // Write primitive object data
            _binaryWriter.WriteBinaryElement(BinaryElement.OBJECT_DATA);
            _binaryWriter.WriteTypeTag(eTypeTag.PRIMITIVE);
            _binaryWriter.Write(_typeID);
            _binaryWriter.WritePrimitiveValue(_object, (TypeCode)_typeID);
        }
 private void WriteStringTypeValue(RSBinaryWriter _binaryWriter, object _object, Type _objectType)
 {
     // Write string object data
     _binaryWriter.WriteBinaryElement(BinaryElement.OBJECT_DATA);
     _binaryWriter.WriteTypeTag(eTypeTag.STRING);
     _binaryWriter.Write(_object as string);
 }
        private void WriteSerializationContainer(RSBinaryWriter _binaryWriter, Dictionary<string, RuntimeSerializationEntry> _container)
        {
            Dictionary<string, RuntimeSerializationEntry>.Enumerator _enumerator	 = _container.GetEnumerator();

            // Write count
            _binaryWriter.Write(_container.Count);

            // Write entries
            while (_enumerator.MoveNext())
            {
                RuntimeSerializationEntry 	_curEntry	= _enumerator.Current.Value;

                // Write serialization entry
                _binaryWriter.Write(_curEntry.Name);

                // Write child properties
                WriteObjectValue(_binaryWriter, _curEntry.Value);
            }
        }
 private void WriteUnsupportedTypeValue(RSBinaryWriter _binaryWriter, eTypeTag _typeTag)
 {
     _binaryWriter.WriteBinaryElement(BinaryElement.OBJECT_DATA);
     _binaryWriter.WriteTypeTag(_typeTag);
 }
        internal void WriteTypeMetadata(RSBinaryWriter _binaryWriter, Type _type, UInt32 _typeID)
        {
            if (_type == null)
                throw new NullReferenceException("[RS] Serialize type metadata failed. Type is null.");

            // Get assembly info
            bool 		_newAssembly;
            UInt32 		_assemblyID;
            Assembly 	_assembly		= _type.Assembly;

            RegisterAssembly(_assembly, out _assemblyID, out _newAssembly);

            // Write assembly properties (if required)
            if (_newAssembly)
            {
                _binaryWriter.WriteBinaryElement(BinaryElement.ASSEMBLY);
                _binaryWriter.Write(_assemblyID);
                _binaryWriter.Write(_assembly.FullName);
            }

            // Write type properties
            _binaryWriter.WriteBinaryElement(BinaryElement.TYPE);
            _binaryWriter.Write(_assemblyID);
            _binaryWriter.Write(_typeID);
            _binaryWriter.Write(_type.FullName);
        }