Exemplo n.º 1
0
        private object ReadStructTypeValue(RSBinaryReader _binaryReader, out Type _objectType, object _object)
        {
            // Get object type
            UInt32 _typeID = _binaryReader.ReadUInt32();

            _objectType = TypeMetadata.GetType(_typeID);

            // Read object refernece
            Dictionary <string, RuntimeSerializationEntry> _initValuesContainer = new Dictionary <string, RuntimeSerializationEntry>();
            Dictionary <string, RuntimeSerializationEntry> _serValuesContainer  = new Dictionary <string, RuntimeSerializationEntry>();
            RuntimeSerializationInfo _serializationInfo = new RuntimeSerializationInfo(_objectType, _initValuesContainer, _serValuesContainer);

            // Read initializers and properties
            ReadSerializationContainer(_binaryReader, ref _initValuesContainer);
            ReadSerializationContainer(_binaryReader, ref _serValuesContainer);

            if (_object == null)
            {
                _object = CreateInstance(_binaryReader, _serializationInfo);
            }

            // Set object value
            _object = SetObjectData(_object, _serializationInfo);

            // Trigger deserialization callback
            if (typeof(IRuntimeSerializationCallback).IsAssignableFrom(_objectType))
            {
                ((IRuntimeSerializationCallback)_object).OnAfterRuntimeDeserialize();
            }

            return(_object);
        }
Exemplo n.º 2
0
        private object ReadStringTypeValue(RSBinaryReader _binaryReader, out Type _objectType)
        {
            // Get object type
            _objectType = typeof(string);

            // Get object value
            return(_binaryReader.ReadString());
        }
Exemplo n.º 3
0
        internal BinaryDeserializer()
        {
            BinaryStreamReader = new RSBinaryReader(512);

            // Load all the supported object readers
            ObjectReaderList    = new ObjectReader[Constants.kSerializationSupportedFormatVersions];
            ObjectReaderList[0] = new ObjectReaderSFV1();
        }
Exemplo n.º 4
0
        private object ReadPrimitiveTypeValue(RSBinaryReader _binaryReader, out Type _objectType)
        {
            // Get object type
            UInt32 _typeID = _binaryReader.ReadUInt32();

            _objectType = TypeMetadata.GetType(_typeID);

            // Get object value
            return(_binaryReader.ReadPrimitiveValue((TypeCode)_typeID));
        }
Exemplo n.º 5
0
        internal override object ReadObjectValue(RSBinaryReader _binaryReader, out Type _objectType, object _object = null)
        {
            BinaryElement _curBinaryElement;

            while ((_curBinaryElement = _binaryReader.ReadBinaryElement()) != BinaryElement.OBJECT_DATA)
            {
                TypeMetadata.ReadTypeMetaData(_binaryReader, _curBinaryElement);
            }

            if (_curBinaryElement != BinaryElement.OBJECT_DATA)
            {
                throw new Exception(string.Format("[RS] Parsing error. BinaryElement={0}.", _curBinaryElement));
            }

            // Deserialize based on value
            eTypeTag _typeTag     = _binaryReader.ReadTypeTag();
            object   _objectValue = null;

            switch (_typeTag)
            {
            case eTypeTag.NULL:
            case eTypeTag.UNSUPPORTED:
                _objectType  = null;
                _objectValue = null;
                break;

            case eTypeTag.PRIMITIVE:
                _objectValue = ReadPrimitiveTypeValue(_binaryReader, out _objectType);
                break;

            case eTypeTag.STRUCT:
                _objectValue = ReadStructTypeValue(_binaryReader, out _objectType, _object);
                break;

            case eTypeTag.STRING:
                _objectValue = ReadStringTypeValue(_binaryReader, out _objectType);
                break;

            case eTypeTag.CLASS:
                _objectValue = ReadClassTypeValue(_binaryReader, out _objectType, _object);
                break;

            case eTypeTag.OBJECT_REFERENCE:
                _objectValue = ReadObjectReferenceValue(_binaryReader, out _objectType);
                break;

            default:
                throw new Exception(string.Format("[RS] Unsupported type tag{0}. For more supported types, Purchase full version http://u3d.as/g8b", _typeTag));
            }

            return(_objectValue);
        }
        internal override object ReadObjectValue(RSBinaryReader _binaryReader, out Type _objectType, object _object = null)
        {
            BinaryElement _curBinaryElement;

            while ((_curBinaryElement = _binaryReader.ReadBinaryElement()) != BinaryElement.OBJECT_DATA)
            {
                TypeMetadata.ReadTypeMetaData(_binaryReader, _curBinaryElement);
            }

            if (_curBinaryElement != BinaryElement.OBJECT_DATA)
                throw new Exception(string.Format("[RS] Parsing error. BinaryElement={0}.", _curBinaryElement));

            // Deserialize based on value
            eTypeTag _typeTag	= _binaryReader.ReadTypeTag();
            object _objectValue	= null;

            switch (_typeTag)
            {
            case eTypeTag.NULL:
            case eTypeTag.UNSUPPORTED:
                _objectType		= null;
                _objectValue	= null;
                break;

            case eTypeTag.PRIMITIVE:
                _objectValue	= ReadPrimitiveTypeValue(_binaryReader, out _objectType);
                break;

            case eTypeTag.STRUCT:
                _objectValue	= ReadStructTypeValue(_binaryReader, out _objectType, _object);
                break;

            case eTypeTag.STRING:
                _objectValue	= ReadStringTypeValue(_binaryReader, out _objectType);
                break;

            case eTypeTag.CLASS:
                _objectValue	= ReadClassTypeValue(_binaryReader, out _objectType, _object);
                break;

            case eTypeTag.OBJECT_REFERENCE:
                _objectValue	= ReadObjectReferenceValue(_binaryReader, out _objectType);
                break;

            default:
                throw new Exception(string.Format("[RS] Unsupported type tag{0}. For more supported types, Purchase full version http://u3d.as/g8b", _typeTag));
            }

            return _objectValue;
        }
Exemplo n.º 7
0
        private void ReadSerializationContainer(RSBinaryReader _binaryReader, ref Dictionary <string, RuntimeSerializationEntry> _container)
        {
            int _memberCount = _binaryReader.ReadInt32();
            int _iter        = 0;

            while (_iter < _memberCount)
            {
                // Get property name and type
                string _pName = _binaryReader.ReadString();
                Type   _pType;
                object _pValue = ReadObjectValue(_binaryReader, out _pType);

                // Add new serialization entry
                _container.Add(_pName, new RuntimeSerializationEntry(_pName, _pValue, _pType));

                // Increment
                _iter++;
            }
        }
        private object CreateInstance(RSBinaryReader _binaryReader, RuntimeSerializationInfo _serilizationInfo)
        {
            Type							_objectType				= _serilizationInfo.ObjectType;
            RuntimeSerializableAttribute	_serializableAttr		= SerializationTypeUtil.GetRuntimeSerializableAttribute(_objectType);

            if (_serializableAttr != null)
            {
                if (typeof(IRuntimeSerializableActivator).IsAssignableFrom(_objectType))
                {
                    MethodInfo				_staticInstanceCreator	= _objectType.GetMethod("CreateInstance", BindingFlags.Public | BindingFlags.Static);

                    if (_staticInstanceCreator != null)
                        return _staticInstanceCreator.Invoke(null, new object[] { _serilizationInfo });
                }
            }

            // Fallback condition
            return Activator.CreateInstance(_objectType);
        }
Exemplo n.º 9
0
        private object CreateInstance(RSBinaryReader _binaryReader, RuntimeSerializationInfo _serilizationInfo)
        {
            Type _objectType = _serilizationInfo.ObjectType;
            RuntimeSerializableAttribute _serializableAttr = SerializationTypeUtil.GetRuntimeSerializableAttribute(_objectType);

            if (_serializableAttr != null)
            {
                if (typeof(IRuntimeSerializableActivator).IsAssignableFrom(_objectType))
                {
                    MethodInfo _staticInstanceCreator = _objectType.GetMethod("CreateInstance", BindingFlags.Public | BindingFlags.Static);

                    if (_staticInstanceCreator != null)
                    {
                        return(_staticInstanceCreator.Invoke(null, new object[] { _serilizationInfo }));
                    }
                }
            }

            // Fallback condition
            return(Activator.CreateInstance(_objectType));
        }
        internal void ReadTypeMetaData(RSBinaryReader _binaryReader, BinaryElement _binaryElement)
        {
            // Read assembly info
            if (_binaryElement == BinaryElement.ASSEMBLY)
            {
                UInt32   _assemblyID       = _binaryReader.ReadUInt32();
                string   _assemblyFullName = _binaryReader.ReadString();
                Assembly _assembly         = Assembly.Load(_assemblyFullName);

                if (_assembly == null)
                {
                    throw new Exception(string.Format("[RS] Couldnt load assembly with name={0}.", _assemblyFullName));
                }

                // Add assembly info to cache
                m_cachedAssemblies.Add(_assembly, _assemblyID);

                // Read next element
                _binaryElement = _binaryReader.ReadBinaryElement();
            }

            // Read type info
            if (_binaryElement == BinaryElement.TYPE)
            {
                UInt32   _assemblyID   = _binaryReader.ReadUInt32();
                UInt32   _typeID       = _binaryReader.ReadUInt32();
                string   _typeFullName = _binaryReader.ReadString();
                Assembly _assembly     = GetAssembly(_assemblyID);

                if (_assembly == null)
                {
                    throw new Exception(string.Format("[RS] Assembly with ID={0} couldnt be found.", _assemblyID));
                }

                Type _newType = _assembly.GetType(_typeFullName, true);

                // Add type info to cache
                m_cachedObjectTypes.Add(_newType, _typeID);
            }
        }
Exemplo n.º 11
0
        private object ReadObjectReferenceValue(RSBinaryReader _binaryReader, out Type _objectType)
        {
            UInt32 _objectReferenceID = _binaryReader.ReadUInt32();
            object _object;

            // Find object using reference ID
            Dictionary <object, UInt32> .Enumerator _dictEnumerator = ObjectReferenceCache.GetEnumerator();

            while (_dictEnumerator.MoveNext())
            {
                KeyValuePair <object, UInt32> _keyValuePair = _dictEnumerator.Current;
                UInt32 _currentReferenceID = _keyValuePair.Value;

                if (_currentReferenceID == _objectReferenceID)
                {
                    _object     = _keyValuePair.Key;
                    _objectType = _object.GetType();

                    return(_object);
                }
            }

            throw new Exception(string.Format("[RS] Object Reference not found for ID={0}.", _objectReferenceID));
        }
Exemplo n.º 12
0
 internal abstract object ReadObjectValue(RSBinaryReader _binaryReader, out Type _objectType, object _object = null);
Exemplo n.º 13
0
        private object ReadPrimitiveTypeValue(RSBinaryReader _binaryReader, out Type _objectType)
        {
            // Get object type
            UInt32	_typeID				= _binaryReader.ReadUInt32();
            _objectType					= TypeMetadata.GetType(_typeID);

            // Get object value
            return _binaryReader.ReadPrimitiveValue((TypeCode)_typeID);
        }
Exemplo n.º 14
0
        private object ReadObjectValue(RSBinaryReader _binaryReader)
        {
            Type _objectType;

            return ReadObjectValue(_binaryReader, out _objectType);
        }
Exemplo n.º 15
0
        private object ReadObjectReferenceValue(RSBinaryReader _binaryReader, out Type _objectType)
        {
            UInt32 	_objectReferenceID								= _binaryReader.ReadUInt32();
            object 	_object;

            // Find object using reference ID
            Dictionary<object, UInt32>.Enumerator _dictEnumerator	= ObjectReferenceCache.GetEnumerator();

            while (_dictEnumerator.MoveNext())
            {
                KeyValuePair<object, UInt32> 	_keyValuePair		= _dictEnumerator.Current;
                UInt32 							_currentReferenceID	= _keyValuePair.Value;

                if (_currentReferenceID == _objectReferenceID)
                {
                    _object											= _keyValuePair.Key;
                    _objectType										= _object.GetType();

                    return _object;
                }
            }

            throw new Exception(string.Format("[RS] Object Reference not found for ID={0}.", _objectReferenceID));
        }
Exemplo n.º 16
0
        private void ReadSerializationContainer(RSBinaryReader _binaryReader, ref Dictionary<string, RuntimeSerializationEntry> _container)
        {
            int		_memberCount	= _binaryReader.ReadInt32();
            int		_iter			= 0;

            while (_iter < _memberCount)
            {
                // Get property name and type
                string	_pName		= _binaryReader.ReadString();
                Type	_pType;
                object	_pValue		= ReadObjectValue(_binaryReader, out _pType);

                // Add new serialization entry
                _container.Add(_pName, new RuntimeSerializationEntry(_pName, _pValue, _pType));

                // Increment
                _iter++;
            }
        }
Exemplo n.º 17
0
        private object ReadObjectValue(RSBinaryReader _binaryReader)
        {
            Type _objectType;

            return(ReadObjectValue(_binaryReader, out _objectType));
        }
Exemplo n.º 18
0
        private object ReadStringTypeValue(RSBinaryReader _binaryReader, out Type _objectType)
        {
            // Get object type
            _objectType					= typeof(string);

            // Get object value
            return _binaryReader.ReadString();
        }
Exemplo n.º 19
0
 internal abstract object ReadObjectValue(RSBinaryReader _binaryReader, out Type _objectType, object _object = null);
Exemplo n.º 20
0
        private object ReadClassTypeValue(RSBinaryReader _binaryReader, out Type _objectType, object _object)
        {
            // Read properties
            UInt32		_typeID					= _binaryReader.ReadUInt32();
            UInt32		_objectReferenceID		= _binaryReader.ReadUInt32();

            // Get object type
            _objectType							= TypeMetadata.GetType(_typeID);

            // Read object refernece
            Dictionary<string, RuntimeSerializationEntry>	_initValuesContainer	= new Dictionary<string, RuntimeSerializationEntry>();
            Dictionary<string, RuntimeSerializationEntry>	_serValuesContainer		= new Dictionary<string, RuntimeSerializationEntry>();
            RuntimeSerializationInfo						_serializationInfo		= new RuntimeSerializationInfo(_objectType, _initValuesContainer, _serValuesContainer);

            // Read initializers and create instance, if required
            ReadSerializationContainer(_binaryReader, ref _initValuesContainer);

            if (_object == null)
                _object							= CreateInstance(_binaryReader, _serializationInfo);

            ObjectReferenceCache.Add(_object, _objectReferenceID);

            // Read properties
            ReadSerializationContainer(_binaryReader, ref _serValuesContainer);

            // Set object value
            _object								= SetObjectData(_object, _serializationInfo);

            // Trigger deserialization callback
            if (typeof(IRuntimeSerializationCallback).IsAssignableFrom(_objectType))
                ((IRuntimeSerializationCallback)_object).OnAfterRuntimeDeserialize();

            return _object;
        }
        internal void ReadTypeMetaData(RSBinaryReader _binaryReader, BinaryElement _binaryElement)
        {
            // Read assembly info
            if (_binaryElement == BinaryElement.ASSEMBLY)
            {
                UInt32 		_assemblyID			= _binaryReader.ReadUInt32();
                string 		_assemblyFullName	= _binaryReader.ReadString();
                Assembly 	_assembly			= Assembly.Load(_assemblyFullName);

                if (_assembly == null)
                    throw new Exception(string.Format("[RS] Couldnt load assembly with name={0}.", _assemblyFullName));

                // Add assembly info to cache
                m_cachedAssemblies.Add(_assembly, _assemblyID);

                // Read next element
                _binaryElement					= _binaryReader.ReadBinaryElement();
            }

            // Read type info
            if (_binaryElement == BinaryElement.TYPE)
            {
                UInt32 		_assemblyID			= _binaryReader.ReadUInt32();
                UInt32 		_typeID				= _binaryReader.ReadUInt32();
                string 		_typeFullName		= _binaryReader.ReadString();
                Assembly 	_assembly			= GetAssembly(_assemblyID);

                if (_assembly == null)
                    throw new Exception(string.Format("[RS] Assembly with ID={0} couldnt be found.", _assemblyID));

                Type 		_newType			= _assembly.GetType(_typeFullName, true);

                // Add type info to cache
                m_cachedObjectTypes.Add(_newType, _typeID);
            }
        }