Exemplo n.º 1
0
        private void WriteStruct(object obj, ObjectHeader header)
        {
            ISerializeExplicit  objAsCustom  = obj as ISerializeExplicit;
            ISerializeSurrogate objSurrogate = GetSurrogateFor(header.ObjectType);

            this.writer.Write(objAsCustom != null);
            this.writer.Write(objSurrogate != null);

            if (objSurrogate != null)
            {
                objSurrogate.RealObject = obj;
                objAsCustom             = objSurrogate.SurrogateObject;

                CustomSerialIO customIO = new CustomSerialIO();
                try { objSurrogate.WriteConstructorData(customIO); }
                catch (Exception e) { this.LogCustomSerializationError(header.ObjectId, header.ObjectType, e); }
                customIO.Serialize(this);
            }

            if (objAsCustom != null)
            {
                CustomSerialIO customIO = new CustomSerialIO();
                try { objAsCustom.WriteData(customIO); }
                catch (Exception e) { this.LogCustomSerializationError(header.ObjectId, header.ObjectType, e); }
                customIO.Serialize(this);
            }
            else
            {
                // Assure the type data layout has been written (only once per file)
                this.WriteTypeDataLayout(header.SerializeType);

                // If we're serializing a value type, skip the entire object body if
                // it equals the zero-init struct. This will keep struct-heavy data a lot
                // smaller binary-wise.
                bool skipEntireBody =
                    header.ObjectType.IsValueType &&
                    object.Equals(obj, header.SerializeType.DefaultValue);

                // Write omitted field bitmask
                bool[] fieldOmitted = new bool[header.SerializeType.Fields.Length];
                for (int i = 0; i < fieldOmitted.Length; i++)
                {
                    fieldOmitted[i] = skipEntireBody || this.IsFieldBlocked(header.SerializeType.Fields[i], obj);
                }
                this.WriteArrayData(fieldOmitted);

                // Write the structs fields
                for (int i = 0; i < header.SerializeType.Fields.Length; i++)
                {
                    if (fieldOmitted[i])
                    {
                        continue;
                    }
                    this.WriteObjectData(header.SerializeType.Fields[i].GetValue(obj));
                }
            }
        }
Exemplo n.º 2
0
        private void WriteStruct(XElement element, object obj, ObjectHeader header)
        {
            ISerializeExplicit  objAsCustom  = obj as ISerializeExplicit;
            ISerializeSurrogate objSurrogate = GetSurrogateFor(header.ObjectType);

            // Write the structs data type
            if (objAsCustom != null)
            {
                element.SetAttributeValue("custom", XmlConvert.ToString(true));
            }
            if (objSurrogate != null)
            {
                element.SetAttributeValue("surrogate", XmlConvert.ToString(true));
            }

            if (objSurrogate != null)
            {
                objSurrogate.RealObject = obj;
                objAsCustom             = objSurrogate.SurrogateObject;

                CustomSerialIO customIO = new CustomSerialIO();
                try { objSurrogate.WriteConstructorData(customIO); }
                catch (Exception e) { this.LogCustomSerializationError(header.ObjectId, header.ObjectType, e); }

                XElement customHeaderElement = new XElement(CustomSerialIO.HeaderElement);
                element.Add(customHeaderElement);
                customIO.Serialize(this, customHeaderElement);
            }

            if (objAsCustom != null)
            {
                CustomSerialIO customIO = new CustomSerialIO();
                try { objAsCustom.WriteData(customIO); }
                catch (Exception e) { this.LogCustomSerializationError(header.ObjectId, header.ObjectType, e); }

                XElement customBodyElement = new XElement(CustomSerialIO.BodyElement);
                element.Add(customBodyElement);
                customIO.Serialize(this, customBodyElement);
            }
            else
            {
                // Write the structs fields
                foreach (FieldInfo field in header.SerializeType.Fields)
                {
                    if (this.IsFieldBlocked(field, obj))
                    {
                        continue;
                    }

                    XElement fieldElement = new XElement(GetXmlElementName(field.Name));
                    element.Add(fieldElement);

                    this.WriteObjectData(fieldElement, field.GetValue(obj));
                }
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Writes the specified structural object, including references objects.
        /// </summary>
        /// <param name="obj">The object to write.</param>
        /// <param name="objSerializeType">The <see cref="Duality.Serialization.SerializeType"/> describing the object.</param>
        /// <param name="id">The objects id.</param>
        protected void WriteStruct(object obj, SerializeType objSerializeType, uint id = 0)
        {
            ISerializable objAsCustom  = obj as ISerializable;
            ISurrogate    objSurrogate = this.GetSurrogateFor(objSerializeType.Type);

            // Write the structs data type
            this.writer.Write(objSerializeType.TypeString);
            this.writer.Write(id);
            this.writer.Write(objAsCustom != null);
            this.writer.Write(objSurrogate != null);

            if (objSurrogate != null)
            {
                objSurrogate.RealObject = obj;
                objAsCustom             = objSurrogate.SurrogateObject;

                CustomSerialIO customIO = new CustomSerialIO();
                try { objSurrogate.WriteConstructorData(customIO); }
                catch (Exception e) { this.LogCustomSerializationError(id, objSerializeType.Type, e); }
                customIO.Serialize(this);
            }

            if (objAsCustom != null)
            {
                CustomSerialIO customIO = new CustomSerialIO();
                try { objAsCustom.WriteData(customIO); }
                catch (Exception e) { this.LogCustomSerializationError(id, objSerializeType.Type, e); }
                customIO.Serialize(this);
            }
            else
            {
                // Assure the type data layout has bee written (only once per file)
                this.WriteTypeDataLayout(objSerializeType);

                // Write omitted field bitmask
                bool[] fieldOmitted = new bool[objSerializeType.Fields.Length];
                for (int i = 0; i < fieldOmitted.Length; i++)
                {
                    fieldOmitted[i] = this.IsFieldBlocked(objSerializeType.Fields[i], obj);
                }
                this.WriteArrayData(fieldOmitted);

                // Write the structs fields
                for (int i = 0; i < objSerializeType.Fields.Length; i++)
                {
                    if (fieldOmitted[i])
                    {
                        continue;
                    }
                    this.WriteObjectData(objSerializeType.Fields[i].GetValue(obj));
                }
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// Writes the specified structural object, including references objects.
        /// </summary>
        /// <param name="obj">The object to write.</param>
        /// <param name="objSerializeType">The <see cref="Duality.Serialization.SerializeType"/> describing the object.</param>
        /// <param name="id">The objects id.</param>
        protected void WriteStruct(object obj, SerializeType objSerializeType, uint id = 0)
        {
            ISerializable objAsCustom  = obj as ISerializable;
            ISurrogate    objSurrogate = this.GetSurrogateFor(objSerializeType.Type);

            // Write the structs data type
            this.writer.WriteAttributeString("type", objSerializeType.TypeString);
            if (id != 0)
            {
                this.writer.WriteAttributeString("id", XmlConvert.ToString(id));
            }
            if (objAsCustom != null)
            {
                this.writer.WriteAttributeString("custom", XmlConvert.ToString(true));
            }
            if (objSurrogate != null)
            {
                this.writer.WriteAttributeString("surrogate", XmlConvert.ToString(true));
            }

            if (objSurrogate != null)
            {
                objSurrogate.RealObject = obj;
                objAsCustom             = objSurrogate.SurrogateObject;

                CustomSerialIO customIO = new CustomSerialIO(CustomSerialIO.HeaderElement);
                try { objSurrogate.WriteConstructorData(customIO); }
                catch (Exception e) { this.LogCustomSerializationError(id, objSerializeType.Type, e); }
                customIO.Serialize(this);
            }

            if (objAsCustom != null)
            {
                CustomSerialIO customIO = new CustomSerialIO(CustomSerialIO.BodyElement);
                try { objAsCustom.WriteData(customIO); }
                catch (Exception e) { this.LogCustomSerializationError(id, objSerializeType.Type, e); }
                customIO.Serialize(this);
            }
            else
            {
                // Write the structs fields
                foreach (FieldInfo field in objSerializeType.Fields)
                {
                    if (this.IsFieldBlocked(field, obj))
                    {
                        continue;
                    }
                    this.WriteObjectData(field.GetValue(obj), field.Name);
                }
            }
        }
Exemplo n.º 5
0
        private void WriteStruct(object obj, ObjectHeader header)
        {
            ISerializeExplicit  objAsCustom  = obj as ISerializeExplicit;
            ISerializeSurrogate objSurrogate = GetSurrogateFor(header.ObjectType);

            this.writer.Write(objAsCustom != null);
            this.writer.Write(objSurrogate != null);

            if (objSurrogate != null)
            {
                objSurrogate.RealObject = obj;
                objAsCustom             = objSurrogate.SurrogateObject;

                CustomSerialIO customIO = new CustomSerialIO();
                try { objSurrogate.WriteConstructorData(customIO); }
                catch (Exception e) { this.LogCustomSerializationError(header.ObjectId, header.ObjectType, e); }
                customIO.Serialize(this);
            }

            if (objAsCustom != null)
            {
                CustomSerialIO customIO = new CustomSerialIO();
                try { objAsCustom.WriteData(customIO); }
                catch (Exception e) { this.LogCustomSerializationError(header.ObjectId, header.ObjectType, e); }
                customIO.Serialize(this);
            }
            else
            {
                // Assure the type data layout has been written (only once per file)
                this.WriteTypeDataLayout(header.SerializeType);

                // Write omitted field bitmask
                bool[] fieldOmitted = new bool[header.SerializeType.Fields.Length];
                for (int i = 0; i < fieldOmitted.Length; i++)
                {
                    fieldOmitted[i] = this.IsFieldBlocked(header.SerializeType.Fields[i], obj);
                }
                this.WriteArrayData(fieldOmitted);

                // Write the structs fields
                for (int i = 0; i < header.SerializeType.Fields.Length; i++)
                {
                    if (fieldOmitted[i])
                    {
                        continue;
                    }
                    this.WriteObjectData(header.SerializeType.Fields[i].GetValue(obj));
                }
            }
        }
Exemplo n.º 6
0
        /// <summary>
        /// Writes the specified <see cref="Duality.Serialization.MetaFormat.StructNode"/>, including possible child nodes.
        /// </summary>
        /// <param name="node"></param>
        protected void WriteStruct(StructNode node)
        {
            // Write the structs data type
            this.writer.Write(node.TypeString);
            this.writer.Write(node.ObjId);
            this.writer.Write(node.CustomSerialization);
            this.writer.Write(node.SurrogateSerialization);

            if (node.SurrogateSerialization)
            {
                CustomSerialIO customIO             = new CustomSerialIO();
                DummyNode      surrogateConstructor = node.SubNodes.FirstOrDefault() as DummyNode;
                if (surrogateConstructor != null)
                {
                    var enumerator = surrogateConstructor.SubNodes.GetEnumerator();
                    while (enumerator.MoveNext())
                    {
                        StringNode key = enumerator.Current as StringNode;
                        if (enumerator.MoveNext() && key != null)
                        {
                            DataNode value = enumerator.Current;
                            customIO.WriteValue(key.StringValue, value);
                        }
                    }
                }
                customIO.Serialize(this);
            }

            if (node.CustomSerialization || node.SurrogateSerialization)
            {
                CustomSerialIO customIO   = new CustomSerialIO();
                var            enumerator = node.SubNodes.GetEnumerator();
                while (enumerator.MoveNext())
                {
                    StringNode key = enumerator.Current as StringNode;
                    if (key != null && enumerator.MoveNext())
                    {
                        DataNode value = enumerator.Current;
                        customIO.WriteValue(key.StringValue, value);
                    }
                }
                customIO.Serialize(this);
            }
            else
            {
                bool           skipLayout = false;
                TypeDataLayout layout     = null;
                if (node.SubNodes.FirstOrDefault() is TypeDataLayoutNode)
                {
                    TypeDataLayoutNode typeDataLayout = node.SubNodes.FirstOrDefault() as TypeDataLayoutNode;
                    this.WriteTypeDataLayout(typeDataLayout.Layout, node.TypeString);
                    layout     = typeDataLayout.Layout;
                    skipLayout = true;
                }
                else
                {
                    this.WriteTypeDataLayout(node.TypeString);
                    layout = this.GetCachedTypeDataLayout(node.TypeString);
                }

                // Write the structs omitted mask
                bool[] fieldOmitted = new bool[layout.Fields.Length];
                for (int i = 0; i < layout.Fields.Length; i++)
                {
                    fieldOmitted[i] = !node.SubNodes.Any(n => !(n is DummyNode) && n.Name == layout.Fields[i].name);
                }
                this.WriteArrayData(fieldOmitted);

                // Write the structs fields
                foreach (DataNode subNode in node.SubNodes)
                {
                    if (skipLayout)
                    {
                        skipLayout = false;
                        continue;
                    }
                    if (subNode is DummyNode)
                    {
                        continue;
                    }
                    this.WriteObject(subNode);
                }
            }
        }
		/// <summary>
		/// Writes the specified <see cref="Duality.Serialization.MetaFormat.StructNode"/>, including possible child nodes.
		/// </summary>
		/// <param name="node"></param>
		protected void WriteStruct(StructNode node)
		{
			// Write the structs data type
			this.writer.WriteAttributeString("type", node.TypeString);
			if (node.ObjId != 0) this.writer.WriteAttributeString("id", XmlConvert.ToString(node.ObjId));
			if (node.CustomSerialization) this.writer.WriteAttributeString("custom", XmlConvert.ToString(true));
			if (node.SurrogateSerialization) this.writer.WriteAttributeString("surrogate", XmlConvert.ToString(true));

			if (node.SurrogateSerialization)
			{
				CustomSerialIO customIO = new CustomSerialIO(CustomSerialIO.HeaderElement);
				DummyNode surrogateConstructor = node.SubNodes.FirstOrDefault() as DummyNode;
				if (surrogateConstructor != null)
				{
					var enumerator = surrogateConstructor.SubNodes.GetEnumerator();
					while (enumerator.MoveNext())
					{
						StringNode key = enumerator.Current as StringNode;
						if (enumerator.MoveNext() && key != null)
						{
							DataNode value = enumerator.Current;
							customIO.WriteValue(key.StringValue, value);
						}
					}
				}
				customIO.Serialize(this);
			}

			if (node.CustomSerialization || node.SurrogateSerialization)
			{
				CustomSerialIO customIO = new CustomSerialIO(CustomSerialIO.BodyElement);
				var enumerator = node.SubNodes.GetEnumerator();
				while (enumerator.MoveNext())
				{
					StringNode key = enumerator.Current as StringNode;
					if (key != null && enumerator.MoveNext())
					{
						DataNode value = enumerator.Current;
						customIO.WriteValue(key.StringValue, value);
					}
				}
				customIO.Serialize(this);
			}
			else
			{
				// Write the structs fields
				foreach (DataNode subNode in node.SubNodes)
				{
					if (subNode is DummyNode) continue;
					if (subNode is TypeDataLayoutNode) continue;
					this.WriteObject(subNode, subNode.Name);
				}
			}
		}
		/// <summary>
		/// Reads a <see cref="Duality.Serialization.MetaFormat.StructNode"/>, including possible child nodes.
		/// </summary>
		/// <param name="node"></param>
		protected StructNode ReadStruct(bool classType)
		{
			// Read struct type
			string	objTypeString	= this.reader.GetAttribute("type");
			string	objIdString		= this.reader.GetAttribute("id");
			string	customString	= this.reader.GetAttribute("custom");
			string	surrogateString	= this.reader.GetAttribute("surrogate");
			uint	objId			= objIdString == null ? 0 : XmlConvert.ToUInt32(objIdString);
			bool	custom			= customString != null && XmlConvert.ToBoolean(customString);
			bool	surrogate		= surrogateString != null && XmlConvert.ToBoolean(surrogateString);

			StructNode result = new StructNode(classType, objTypeString, objId, custom, surrogate);
			
			// Read surrogate constructor data
			if (surrogate)
			{
				custom = true;

				// Set fake object reference for surrogate constructor: No self-references allowed here.
				this.idManager.Inject(null, objId);

				CustomSerialIO customIO = new CustomSerialIO(CustomSerialIO.HeaderElement);
				customIO.Deserialize(this);
				if (customIO.Data.Any())
				{
					DummyNode surrogateConstructor = new DummyNode();
					surrogateConstructor.Parent = result;
					foreach (var pair in customIO.Data)
					{
						StringNode key = new StringNode(pair.Key);
						DataNode value = pair.Value as DataNode;
						key.Parent = surrogateConstructor;
						value.Parent = surrogateConstructor;
					}
				}
			}

			// Prepare object reference
			this.idManager.Inject(result, objId);
			
			// Read custom object data
			if (custom)
			{
				CustomSerialIO customIO = new CustomSerialIO(CustomSerialIO.BodyElement);
				customIO.Deserialize(this);
				foreach (var pair in customIO.Data)
				{
					StringNode key = new StringNode(pair.Key);
					DataNode value = pair.Value as DataNode;
					key.Parent = result;
					value.Parent = result;
				}
			}
			// Red non-custom object data
			else if (!this.reader.IsEmptyElement)
			{
				// Read fields
				bool scopeChanged;
				string fieldName;
				DataNode fieldValue;
				while (true)
				{
					fieldValue = this.ReadObject(out fieldName, out scopeChanged) as DataNode;
					if (scopeChanged) break;
					else
					{
						fieldValue.Name = fieldName;
						fieldValue.Parent = result;
					}
				}
			}

			return result;
		}
Exemplo n.º 9
0
        private object ReadStruct(ObjectHeader header)
        {
            // Read struct type
            bool custom    = this.reader.ReadBoolean();
            bool surrogate = this.reader.ReadBoolean();

            // Retrieve surrogate if requested
            ISerializeSurrogate objSurrogate = null;

            if (surrogate && header.SerializeType != null)
            {
                custom       = true;
                objSurrogate = header.SerializeType.Surrogate;
                if (objSurrogate == null)
                {
                    this.LocalLog.WriteError(
                        "Object type '{0}' was serialized using a surrogate, but no such surrogate was found for deserialization.",
                        LogFormat.Type(header.SerializeType.Type));
                }
            }

            // If the object was serialized as a surrogate, deserialize its header first
            CustomSerialIO surrogateHeader = null;

            if (surrogate)
            {
                // Set fake object reference for surrogate constructor: No self-references allowed here.
                this.idManager.Inject(null, header.ObjectId);

                surrogateHeader = new CustomSerialIO();
                surrogateHeader.Deserialize(this);
            }

            // Construct object
            object obj = null;

            if (header.ObjectType != null)
            {
                if (objSurrogate != null)
                {
                    try { obj = objSurrogate.ConstructObject(surrogateHeader, header.ObjectType); }
                    catch (Exception e) { this.LogCustomDeserializationError(header.ObjectId, header.ObjectType, e); }
                }
                if (obj == null)
                {
                    obj = header.ObjectType.CreateInstanceOf();
                }
            }

            // Prepare object reference
            this.idManager.Inject(obj, header.ObjectId);

            // Read custom object data
            if (custom)
            {
                CustomSerialIO customIO = new CustomSerialIO();
                customIO.Deserialize(this);

                ISerializeExplicit objAsCustom;
                if (objSurrogate != null)
                {
                    objSurrogate.RealObject = obj;
                    objAsCustom             = objSurrogate.SurrogateObject;
                }
                else
                {
                    objAsCustom = obj as ISerializeExplicit;
                }

                if (objAsCustom != null)
                {
                    try { objAsCustom.ReadData(customIO); }
                    catch (Exception e) { this.LogCustomDeserializationError(header.ObjectId, header.ObjectType, e); }
                }
                else if (obj != null && header.ObjectType != null)
                {
                    this.LocalLog.WriteWarning(
                        "Object data (Id {0}) is flagged for custom deserialization, yet the objects Type ('{1}') does not support it. Guessing associated fields...",
                        header.ObjectId,
                        LogFormat.Type(header.ObjectType));
                    this.LocalLog.PushIndent();
                    foreach (var pair in customIO.Data)
                    {
                        this.AssignValueToField(header.SerializeType, obj, pair.Key, pair.Value);
                    }
                    this.LocalLog.PopIndent();
                }
            }
            // Red non-custom object data
            else
            {
                // Determine data layout
                TypeDataLayout layout = this.ReadTypeDataLayout(header.TypeString);

                // Read fields
                bool[] fieldOmitted = new bool[layout.Fields.Length];
                this.ReadArrayData(fieldOmitted);
                for (int i = 0; i < layout.Fields.Length; i++)
                {
                    if (fieldOmitted[i])
                    {
                        continue;
                    }
                    object fieldValue = this.ReadObjectData();
                    this.AssignValueToField(header.SerializeType, obj, layout.Fields[i].name, fieldValue);
                }
            }

            return(obj);
        }
Exemplo n.º 10
0
        /// <summary>
        /// Writes the specified <see cref="Duality.Serialization.MetaFormat.StructNode"/>, including possible child nodes.
        /// </summary>
        /// <param name="node"></param>
        protected void WriteStruct(StructNode node)
        {
            // Write the structs data type
            this.writer.WriteAttributeString("type", node.TypeString);
            if (node.ObjId != 0)
            {
                this.writer.WriteAttributeString("id", XmlConvert.ToString(node.ObjId));
            }
            if (node.CustomSerialization)
            {
                this.writer.WriteAttributeString("custom", XmlConvert.ToString(true));
            }
            if (node.SurrogateSerialization)
            {
                this.writer.WriteAttributeString("surrogate", XmlConvert.ToString(true));
            }

            if (node.SurrogateSerialization)
            {
                CustomSerialIO customIO             = new CustomSerialIO(CustomSerialIO.HeaderElement);
                DummyNode      surrogateConstructor = node.SubNodes.FirstOrDefault() as DummyNode;
                if (surrogateConstructor != null)
                {
                    var enumerator = surrogateConstructor.SubNodes.GetEnumerator();
                    while (enumerator.MoveNext())
                    {
                        StringNode key = enumerator.Current as StringNode;
                        if (enumerator.MoveNext() && key != null)
                        {
                            DataNode value = enumerator.Current;
                            customIO.WriteValue(key.StringValue, value);
                        }
                    }
                }
                customIO.Serialize(this);
            }

            if (node.CustomSerialization || node.SurrogateSerialization)
            {
                CustomSerialIO customIO   = new CustomSerialIO(CustomSerialIO.BodyElement);
                var            enumerator = node.SubNodes.GetEnumerator();
                while (enumerator.MoveNext())
                {
                    StringNode key = enumerator.Current as StringNode;
                    if (key != null && enumerator.MoveNext())
                    {
                        DataNode value = enumerator.Current;
                        customIO.WriteValue(key.StringValue, value);
                    }
                }
                customIO.Serialize(this);
            }
            else
            {
                // Write the structs fields
                foreach (DataNode subNode in node.SubNodes)
                {
                    if (subNode is DummyNode)
                    {
                        continue;
                    }
                    if (subNode is TypeDataLayoutNode)
                    {
                        continue;
                    }
                    this.WriteObjectData(subNode, subNode.Name);
                }
            }
        }
Exemplo n.º 11
0
		/// <summary>
		/// Reads a structural object, including referenced objects.
		/// </summary>
		/// <returns>The object that has been read.</returns>
		protected object ReadStruct()
		{
			// Read struct type
			string	objTypeString	= this.reader.ReadString();
			uint	objId			= this.reader.ReadUInt32();
			bool	custom			= this.reader.ReadBoolean();
			bool	surrogate		= this.reader.ReadBoolean();
			Type	objType			= this.ResolveType(objTypeString, objId);

			SerializeType objSerializeType = null;
			if (objType != null) objSerializeType = objType.GetSerializeType();
			
			// Retrieve surrogate if requested
			ISurrogate objSurrogate = null;
			if (surrogate && objType != null) objSurrogate = this.GetSurrogateFor(objType);

			// Construct object
			object obj = null;
			if (objType != null)
			{
				if (objSurrogate != null)
				{
					custom = true;

					// Set fake object reference for surrogate constructor: No self-references allowed here.
					this.idManager.Inject(null, objId);

					CustomSerialIO customIO = new CustomSerialIO();
					customIO.Deserialize(this);
					try { obj = objSurrogate.ConstructObject(customIO, objType); }
					catch (Exception e) { this.LogCustomDeserializationError(objId, objType, e); }
				}
				if (obj == null) obj = objType.CreateInstanceOf();
				if (obj == null) obj = objType.CreateInstanceOf(true);
			}

			// Prepare object reference
			this.idManager.Inject(obj, objId);

			// Read custom object data
			if (custom)
			{
				CustomSerialIO customIO = new CustomSerialIO();
				customIO.Deserialize(this);

				ISerializable objAsCustom;
				if (objSurrogate != null)
				{
					objSurrogate.RealObject = obj;
					objAsCustom = objSurrogate.SurrogateObject;
				}
				else
					objAsCustom = obj as ISerializable;

				if (objAsCustom != null)
				{
					try { objAsCustom.ReadData(customIO); }
					catch (Exception e) { this.LogCustomDeserializationError(objId, objType, e); }
				}
				else if (obj != null && objType != null)
				{
					this.SerializationLog.WriteWarning(
						"Object data (Id {0}) is flagged for custom deserialization, yet the objects Type ('{1}') does not support it. Guessing associated fields...",
						objId,
						Log.Type(objType));
					this.SerializationLog.PushIndent();
					foreach (var pair in customIO.Data)
					{
						this.AssignValueToField(objSerializeType, obj, pair.Key, pair.Value);
					}
					this.SerializationLog.PopIndent();
				}
			}
			// Red non-custom object data
			else
			{
				// Determine data layout
				TypeDataLayout layout	= this.ReadTypeDataLayout(objTypeString);

				// Read fields
				if (this.dataVersion <= 2)
				{
					for (int i = 0; i < layout.Fields.Length; i++)
					{
						object fieldValue = this.ReadObject();
						this.AssignValueToField(objSerializeType, obj, layout.Fields[i].name, fieldValue);
					}
				}
				else if (this.dataVersion >= 3)
				{
					bool[] fieldOmitted = new bool[layout.Fields.Length];
					this.ReadArrayData(fieldOmitted);
					for (int i = 0; i < layout.Fields.Length; i++)
					{
						if (fieldOmitted[i]) continue;
						object fieldValue = this.ReadObject();
						this.AssignValueToField(objSerializeType, obj, layout.Fields[i].name, fieldValue);
					}
				}
			}

			return obj;
		}
Exemplo n.º 12
0
        private void WriteStruct(XElement element, object obj, ObjectHeader header)
        {
            ISerializeExplicit  objAsCustom  = obj as ISerializeExplicit;
            ISerializeSurrogate objSurrogate = GetSurrogateFor(header.ObjectType);

            // If we're serializing a value type, skip the entire object body if
            // it equals the zero-init struct. This will keep struct-heavy data a lot
            // more concise.
            if (header.ObjectType.IsValueType &&
                object.Equals(obj, header.SerializeType.DefaultValue))
            {
                return;
            }

            // Write information about custom or surrogate serialization
            if (objAsCustom != null)
            {
                element.SetAttributeValue("custom", XmlConvert.ToString(true));
            }
            if (objSurrogate != null)
            {
                element.SetAttributeValue("surrogate", XmlConvert.ToString(true));
            }

            if (objSurrogate != null)
            {
                objSurrogate.RealObject = obj;
                objAsCustom             = objSurrogate.SurrogateObject;

                CustomSerialIO customIO = new CustomSerialIO();
                try { objSurrogate.WriteConstructorData(customIO); }
                catch (Exception e) { this.LogCustomSerializationError(header.ObjectId, header.ObjectType, e); }

                XElement customHeaderElement = new XElement(CustomSerialIO.HeaderElement);
                element.Add(customHeaderElement);
                customIO.Serialize(this, customHeaderElement);
            }

            if (objAsCustom != null)
            {
                CustomSerialIO customIO = new CustomSerialIO();
                try { objAsCustom.WriteData(customIO); }
                catch (Exception e) { this.LogCustomSerializationError(header.ObjectId, header.ObjectType, e); }

                XElement customBodyElement = new XElement(CustomSerialIO.BodyElement);
                element.Add(customBodyElement);
                customIO.Serialize(this, customBodyElement);
            }
            else
            {
                // Write the structs fields
                foreach (FieldInfo field in header.SerializeType.Fields)
                {
                    if (this.IsFieldBlocked(field, obj))
                    {
                        continue;
                    }

                    XElement fieldElement = new XElement(GetXmlElementName(field.Name));
                    element.Add(fieldElement);

                    this.WriteObjectData(fieldElement, field.GetValue(obj));
                }
            }
        }
Exemplo n.º 13
0
		/// <summary>
		/// Writes the specified <see cref="Duality.Serialization.MetaFormat.StructNode"/>, including possible child nodes.
		/// </summary>
		/// <param name="node"></param>
		protected void WriteStruct(StructNode node)
		{
			// Write the structs data type
			this.writer.Write(node.TypeString);
			this.writer.Write(node.ObjId);
			this.writer.Write(node.CustomSerialization);
			this.writer.Write(node.SurrogateSerialization);

			if (node.SurrogateSerialization)
			{
				CustomSerialIO customIO = new CustomSerialIO();
				DummyNode surrogateConstructor = node.SubNodes.FirstOrDefault() as DummyNode;
				if (surrogateConstructor != null)
				{
					var enumerator = surrogateConstructor.SubNodes.GetEnumerator();
					while (enumerator.MoveNext())
					{
						StringNode key = enumerator.Current as StringNode;
						if (enumerator.MoveNext() && key != null)
						{
							DataNode value = enumerator.Current;
							customIO.WriteValue(key.StringValue, value);
						}
					}
				}
				customIO.Serialize(this);
			}

			if (node.CustomSerialization || node.SurrogateSerialization)
			{
				CustomSerialIO customIO = new CustomSerialIO();
				var enumerator = node.SubNodes.GetEnumerator();
				while (enumerator.MoveNext())
				{
					StringNode key = enumerator.Current as StringNode;
					if (key != null && enumerator.MoveNext())
					{
						DataNode value = enumerator.Current;
						customIO.WriteValue(key.StringValue, value);
					}
				}
				customIO.Serialize(this);
			}
			else
			{
				bool skipLayout = false;
				TypeDataLayout layout = null;
				if (node.SubNodes.FirstOrDefault() is TypeDataLayoutNode)
				{
					TypeDataLayoutNode typeDataLayout = node.SubNodes.FirstOrDefault() as TypeDataLayoutNode;
					this.WriteTypeDataLayout(typeDataLayout.Layout, node.TypeString);
					layout = typeDataLayout.Layout;
					skipLayout = true;
				}
				else
				{
					this.WriteTypeDataLayout(node.TypeString);
					layout = this.GetCachedTypeDataLayout(node.TypeString);
				}

				// Write the structs omitted mask
				bool[] fieldOmitted = new bool[layout.Fields.Length];
				for (int i = 0; i < layout.Fields.Length; i++)
				{
					fieldOmitted[i] = !node.SubNodes.Any(n => !(n is DummyNode) && n.Name == layout.Fields[i].name);
				}
				this.WriteArrayData(fieldOmitted);

				// Write the structs fields
				foreach (DataNode subNode in node.SubNodes)
				{
					if (skipLayout)
					{
						skipLayout = false;
						continue;
					}
					if (subNode is DummyNode) continue;
					this.WriteObject(subNode);
				}
			}
		}
Exemplo n.º 14
0
		/// <summary>
		/// Reads a <see cref="Duality.Serialization.MetaFormat.StructNode"/>, including possible child nodes.
		/// </summary>
		/// <param name="node"></param>
		protected StructNode ReadStruct(bool classType)
		{
			// Read struct type
			string	objTypeString	= this.reader.ReadString();
			uint	objId			= this.reader.ReadUInt32();
			bool	custom			= this.reader.ReadBoolean();
			bool	surrogate		= this.reader.ReadBoolean();

			StructNode result = new StructNode(classType, objTypeString, objId, custom, surrogate);
			
			// Read surrogate constructor data
			if (surrogate)
			{
				custom = true;

				// Set fake object reference for surrogate constructor: No self-references allowed here.
				this.idManager.Inject(null, objId);

				CustomSerialIO customIO = new CustomSerialIO();
				customIO.Deserialize(this);
				if (customIO.Data.Any())
				{
					DummyNode surrogateConstructor = new DummyNode();
					surrogateConstructor.Parent = result;
					foreach (var pair in customIO.Data)
					{
						StringNode key = new StringNode(pair.Key);
						DataNode value = pair.Value as DataNode;
						key.Parent = surrogateConstructor;
						value.Parent = surrogateConstructor;
					}
				}
			}

			// Prepare object reference
			this.idManager.Inject(result, objId);

			if (custom)
			{
				CustomSerialIO customIO = new CustomSerialIO();
				customIO.Deserialize(this);
				foreach (var pair in customIO.Data)
				{
					StringNode key = new StringNode(pair.Key);
					DataNode value = pair.Value as DataNode;
					key.Parent = result;
					value.Parent = result;
				}
			}
			else
			{
				// Determine data layout
				bool wasThereBefore = this.GetCachedTypeDataLayout(objTypeString) != null;
				TypeDataLayout layout = this.ReadTypeDataLayout(objTypeString);
				if (!wasThereBefore)
				{
					TypeDataLayoutNode layoutNode = new TypeDataLayoutNode(new TypeDataLayout(layout));
					layoutNode.Parent = result;
				}

				// Read fields
				if (this.dataVersion <= 2)
				{
					for (int i = 0; i < layout.Fields.Length; i++)
					{
						DataNode fieldValue = this.ReadObject() as DataNode;
						fieldValue.Parent = result;
						fieldValue.Name = layout.Fields[i].name;
					}
				}
				else if (this.dataVersion >= 3)
				{
					bool[] fieldOmitted = new bool[layout.Fields.Length];
					this.ReadArrayData(fieldOmitted);
					
					for (int i = 0; i < layout.Fields.Length; i++)
					{
						if (fieldOmitted[i]) continue;
						DataNode fieldValue = this.ReadObject() as DataNode;
						fieldValue.Parent = result;
						fieldValue.Name = layout.Fields[i].name;
					}
				}
			}

			return result;
		}
Exemplo n.º 15
0
        /// <summary>
        /// Writes the specified structural object, including references objects.
        /// </summary>
        /// <param name="obj">The object to write.</param>
        /// <param name="objSerializeType">The <see cref="Duality.Serialization.SerializeType"/> describing the object.</param>
        /// <param name="id">The objects id.</param>
        protected void WriteStruct(object obj, SerializeType objSerializeType, uint id = 0)
        {
            ISerializable objAsCustom = obj as ISerializable;
            ISurrogate objSurrogate = this.GetSurrogateFor(objSerializeType.Type);

            // Write the structs data type
            this.writer.WriteAttributeString("type", objSerializeType.TypeString);
            if (id != 0) this.writer.WriteAttributeString("id", XmlConvert.ToString(id));
            if (objAsCustom != null) this.writer.WriteAttributeString("custom", XmlConvert.ToString(true));
            if (objSurrogate != null) this.writer.WriteAttributeString("surrogate", XmlConvert.ToString(true));

            if (objSurrogate != null)
            {
                objSurrogate.RealObject = obj;
                objAsCustom = objSurrogate.SurrogateObject;

                CustomSerialIO customIO = new CustomSerialIO(CustomSerialIO.HeaderElement);
                try { objSurrogate.WriteConstructorData(customIO); }
                catch (Exception e) { this.LogCustomSerializationError(id, objSerializeType.Type, e); }
                customIO.Serialize(this);
            }

            if (objAsCustom != null)
            {
                CustomSerialIO customIO = new CustomSerialIO(CustomSerialIO.BodyElement);
                try { objAsCustom.WriteData(customIO); }
                catch (Exception e) { this.LogCustomSerializationError(id, objSerializeType.Type, e); }
                customIO.Serialize(this);
            }
            else
            {
                // Write the structs fields
                foreach (FieldInfo field in objSerializeType.Fields)
                {
                    if (this.IsFieldBlocked(field, obj)) continue;
                    this.WriteObjectData(field.GetValue(obj), field.Name);
                }
            }
        }
Exemplo n.º 16
0
        /// <summary>
        /// Reads a structural object, including referenced objects.
        /// </summary>
        /// <returns>The object that has been read.</returns>
        protected object ReadStruct()
        {
            // Read struct type
            string	objTypeString	= this.reader.GetAttribute("type");
            string	objIdString		= this.reader.GetAttribute("id");
            string	customString	= this.reader.GetAttribute("custom");
            string	surrogateString	= this.reader.GetAttribute("surrogate");
            uint	objId			= objIdString == null ? 0 : XmlConvert.ToUInt32(objIdString);
            bool	custom			= customString != null && XmlConvert.ToBoolean(customString);
            bool	surrogate		= surrogateString != null && XmlConvert.ToBoolean(surrogateString);
            Type	objType			= this.ResolveType(objTypeString, objId);

            SerializeType objSerializeType = null;
            if (objType != null) objSerializeType = objType.GetSerializeType();

            // Retrieve surrogate if requested
            ISurrogate objSurrogate = null;
            if (surrogate && objType != null) objSurrogate = this.GetSurrogateFor(objType);

            // Construct object
            object obj = null;
            if (objType != null)
            {
                if (objSurrogate != null)
                {
                    custom = true;

                    // Set fake object reference for surrogate constructor: No self-references allowed here.
                    this.idManager.Inject(null, objId);

                    CustomSerialIO customIO = new CustomSerialIO(CustomSerialIO.HeaderElement);
                    customIO.Deserialize(this);
                    try { obj = objSurrogate.ConstructObject(customIO, objType); }
                    catch (Exception e) { this.LogCustomDeserializationError(objId, objType, e); }
                }
                if (obj == null) obj = objType.CreateInstanceOf();
                if (obj == null) obj = objType.CreateInstanceOf(true);
            }

            // Prepare object reference
            this.idManager.Inject(obj, objId);

            // Read custom object data
            if (custom)
            {
                CustomSerialIO customIO = new CustomSerialIO(CustomSerialIO.BodyElement);
                customIO.Deserialize(this);

                ISerializable objAsCustom;
                if (objSurrogate != null)
                {
                    objSurrogate.RealObject = obj;
                    objAsCustom = objSurrogate.SurrogateObject;
                }
                else
                    objAsCustom = obj as ISerializable;

                if (objAsCustom != null)
                {
                    try { objAsCustom.ReadData(customIO); }
                    catch (Exception e) { this.LogCustomDeserializationError(objId, objType, e); }
                }
                else if (obj != null && objType != null)
                {
                    this.SerializationLog.WriteWarning(
                        "Object data (Id {0}) is flagged for custom deserialization, yet the objects Type ('{1}') does not support it. Guessing associated fields...",
                        objId,
                        Log.Type(objType));
                    this.SerializationLog.PushIndent();
                    foreach (var pair in customIO.Data)
                    {
                        this.AssignValueToField(objSerializeType, obj, pair.Key, pair.Value);
                    }
                    this.SerializationLog.PopIndent();
                }
            }
            // Red non-custom object data
            else if (!this.reader.IsEmptyElement)
            {
                // Read fields
                bool scopeChanged;
                string fieldName;
                object fieldValue;
                while (true)
                {
                    fieldValue = this.ReadObjectData(out fieldName, out scopeChanged);
                    if (scopeChanged) break;
                    this.AssignValueToField(objSerializeType, obj, fieldName, fieldValue);
                }
            }

            return obj;
        }
Exemplo n.º 17
0
        /// <summary>
        /// Reads a structural object, including referenced objects.
        /// </summary>
        /// <returns>The object that has been read.</returns>
        protected object ReadStruct()
        {
            // Read struct type
            string objTypeString   = this.reader.GetAttribute("type");
            string objIdString     = this.reader.GetAttribute("id");
            string customString    = this.reader.GetAttribute("custom");
            string surrogateString = this.reader.GetAttribute("surrogate");
            uint   objId           = objIdString == null ? 0 : XmlConvert.ToUInt32(objIdString);
            bool   custom          = customString != null && XmlConvert.ToBoolean(customString);
            bool   surrogate       = surrogateString != null && XmlConvert.ToBoolean(surrogateString);
            Type   objType         = this.ResolveType(objTypeString, objId);

            SerializeType objSerializeType = null;

            if (objType != null)
            {
                objSerializeType = objType.GetSerializeType();
            }

            // Retrieve surrogate if requested
            ISurrogate objSurrogate = null;

            if (surrogate && objType != null)
            {
                objSurrogate = this.GetSurrogateFor(objType);
            }

            // Construct object
            object obj = null;

            if (objType != null)
            {
                if (objSurrogate != null)
                {
                    custom = true;

                    // Set fake object reference for surrogate constructor: No self-references allowed here.
                    this.idManager.Inject(null, objId);

                    CustomSerialIO customIO = new CustomSerialIO(CustomSerialIO.HeaderElement);
                    customIO.Deserialize(this);
                    try { obj = objSurrogate.ConstructObject(customIO, objType); }
                    catch (Exception e) { this.LogCustomDeserializationError(objId, objType, e); }
                }
                if (obj == null)
                {
                    obj = objType.CreateInstanceOf();
                }
                if (obj == null)
                {
                    obj = objType.CreateInstanceOf(true);
                }
            }

            // Prepare object reference
            this.idManager.Inject(obj, objId);

            // Read custom object data
            if (custom)
            {
                CustomSerialIO customIO = new CustomSerialIO(CustomSerialIO.BodyElement);
                customIO.Deserialize(this);

                ISerializable objAsCustom;
                if (objSurrogate != null)
                {
                    objSurrogate.RealObject = obj;
                    objAsCustom             = objSurrogate.SurrogateObject;
                }
                else
                {
                    objAsCustom = obj as ISerializable;
                }

                if (objAsCustom != null)
                {
                    try { objAsCustom.ReadData(customIO); }
                    catch (Exception e) { this.LogCustomDeserializationError(objId, objType, e); }
                }
                else if (obj != null && objType != null)
                {
                    this.SerializationLog.WriteWarning(
                        "Object data (Id {0}) is flagged for custom deserialization, yet the objects Type ('{1}') does not support it. Guessing associated fields...",
                        objId,
                        Log.Type(objType));
                    this.SerializationLog.PushIndent();
                    foreach (var pair in customIO.Data)
                    {
                        this.AssignValueToField(objSerializeType, obj, pair.Key, pair.Value);
                    }
                    this.SerializationLog.PopIndent();
                }
            }
            // Red non-custom object data
            else if (!this.reader.IsEmptyElement)
            {
                // Read fields
                bool   scopeChanged;
                string fieldName;
                object fieldValue;
                while (true)
                {
                    fieldValue = this.ReadObjectData(out fieldName, out scopeChanged);
                    if (scopeChanged)
                    {
                        break;
                    }
                    this.AssignValueToField(objSerializeType, obj, fieldName, fieldValue);
                }
            }

            return(obj);
        }
Exemplo n.º 18
0
        /// <summary>
        /// Reads a <see cref="Duality.Serialization.MetaFormat.StructNode"/>, including possible child nodes.
        /// </summary>
        /// <param name="node"></param>
        protected StructNode ReadStruct(bool classType)
        {
            // Read struct type
            string objTypeString   = this.reader.GetAttribute("type");
            string objIdString     = this.reader.GetAttribute("id");
            string customString    = this.reader.GetAttribute("custom");
            string surrogateString = this.reader.GetAttribute("surrogate");
            uint   objId           = objIdString == null ? 0 : XmlConvert.ToUInt32(objIdString);
            bool   custom          = customString != null && XmlConvert.ToBoolean(customString);
            bool   surrogate       = surrogateString != null && XmlConvert.ToBoolean(surrogateString);

            StructNode result = new StructNode(classType, objTypeString, objId, custom, surrogate);

            // Read surrogate constructor data
            if (surrogate)
            {
                custom = true;

                // Set fake object reference for surrogate constructor: No self-references allowed here.
                this.idManager.Inject(null, objId);

                CustomSerialIO customIO = new CustomSerialIO(CustomSerialIO.HeaderElement);
                customIO.Deserialize(this);
                if (customIO.Data.Any())
                {
                    DummyNode surrogateConstructor = new DummyNode();
                    surrogateConstructor.Parent = result;
                    foreach (var pair in customIO.Data)
                    {
                        StringNode key   = new StringNode(pair.Key);
                        DataNode   value = pair.Value as DataNode;
                        key.Parent   = surrogateConstructor;
                        value.Parent = surrogateConstructor;
                    }
                }
            }

            // Prepare object reference
            this.idManager.Inject(result, objId);

            // Read custom object data
            if (custom)
            {
                CustomSerialIO customIO = new CustomSerialIO(CustomSerialIO.BodyElement);
                customIO.Deserialize(this);
                foreach (var pair in customIO.Data)
                {
                    StringNode key   = new StringNode(pair.Key);
                    DataNode   value = pair.Value as DataNode;
                    key.Parent   = result;
                    value.Parent = result;
                }
            }
            // Red non-custom object data
            else if (!this.reader.IsEmptyElement)
            {
                // Read fields
                bool     scopeChanged;
                string   fieldName;
                DataNode fieldValue;
                while (true)
                {
                    fieldValue = this.ReadObjectData(out fieldName, out scopeChanged) as DataNode;
                    if (scopeChanged)
                    {
                        break;
                    }
                    else
                    {
                        fieldValue.Name   = fieldName;
                        fieldValue.Parent = result;
                    }
                }
            }

            return(result);
        }
Exemplo n.º 19
0
        /// <summary>
        /// Reads a structural object, including referenced objects.
        /// </summary>
        /// <returns>The object that has been read.</returns>
        protected object ReadStruct()
        {
            // Read struct type
            string objTypeString = this.reader.ReadString();
            uint   objId         = this.reader.ReadUInt32();
            bool   custom        = this.reader.ReadBoolean();
            bool   surrogate     = this.reader.ReadBoolean();
            Type   objType       = this.ResolveType(objTypeString, objId);

            SerializeType objSerializeType = null;

            if (objType != null)
            {
                objSerializeType = objType.GetSerializeType();
            }

            // Retrieve surrogate if requested
            ISurrogate objSurrogate = null;

            if (surrogate && objType != null)
            {
                objSurrogate = this.GetSurrogateFor(objType);
            }

            // Construct object
            object obj = null;

            if (objType != null)
            {
                if (objSurrogate != null)
                {
                    custom = true;

                    // Set fake object reference for surrogate constructor: No self-references allowed here.
                    this.idManager.Inject(null, objId);

                    CustomSerialIO customIO = new CustomSerialIO();
                    customIO.Deserialize(this);
                    try { obj = objSurrogate.ConstructObject(customIO, objType); }
                    catch (Exception e) { this.LogCustomDeserializationError(objId, objType, e); }
                }
                if (obj == null)
                {
                    obj = objType.CreateInstanceOf();
                }
                if (obj == null)
                {
                    obj = objType.CreateInstanceOf(true);
                }
            }

            // Prepare object reference
            this.idManager.Inject(obj, objId);

            // Read custom object data
            if (custom)
            {
                CustomSerialIO customIO = new CustomSerialIO();
                customIO.Deserialize(this);

                ISerializable objAsCustom;
                if (objSurrogate != null)
                {
                    objSurrogate.RealObject = obj;
                    objAsCustom             = objSurrogate.SurrogateObject;
                }
                else
                {
                    objAsCustom = obj as ISerializable;
                }

                if (objAsCustom != null)
                {
                    try { objAsCustom.ReadData(customIO); }
                    catch (Exception e) { this.LogCustomDeserializationError(objId, objType, e); }
                }
                else if (obj != null && objType != null)
                {
                    this.SerializationLog.WriteWarning(
                        "Object data (Id {0}) is flagged for custom deserialization, yet the objects Type ('{1}') does not support it. Guessing associated fields...",
                        objId,
                        Log.Type(objType));
                    this.SerializationLog.PushIndent();
                    foreach (var pair in customIO.Data)
                    {
                        this.AssignValueToField(objSerializeType, obj, pair.Key, pair.Value);
                    }
                    this.SerializationLog.PopIndent();
                }
            }
            // Red non-custom object data
            else
            {
                // Determine data layout
                TypeDataLayout layout = this.ReadTypeDataLayout(objTypeString);

                // Read fields
                if (this.dataVersion <= 2)
                {
                    for (int i = 0; i < layout.Fields.Length; i++)
                    {
                        object fieldValue = this.ReadObjectData();
                        this.AssignValueToField(objSerializeType, obj, layout.Fields[i].name, fieldValue);
                    }
                }
                else if (this.dataVersion >= 3)
                {
                    bool[] fieldOmitted = new bool[layout.Fields.Length];
                    this.ReadArrayData(fieldOmitted);
                    for (int i = 0; i < layout.Fields.Length; i++)
                    {
                        if (fieldOmitted[i])
                        {
                            continue;
                        }
                        object fieldValue = this.ReadObjectData();
                        this.AssignValueToField(objSerializeType, obj, layout.Fields[i].name, fieldValue);
                    }
                }
            }

            return(obj);
        }
Exemplo n.º 20
0
        /// <summary>
        /// Reads a <see cref="Duality.Serialization.MetaFormat.StructNode"/>, including possible child nodes.
        /// </summary>
        /// <param name="node"></param>
        protected StructNode ReadStruct(bool classType)
        {
            // Read struct type
            string objTypeString = this.reader.ReadString();
            uint   objId         = this.reader.ReadUInt32();
            bool   custom        = this.reader.ReadBoolean();
            bool   surrogate     = this.reader.ReadBoolean();

            StructNode result = new StructNode(classType, objTypeString, objId, custom, surrogate);

            // Read surrogate constructor data
            if (surrogate)
            {
                custom = true;

                // Set fake object reference for surrogate constructor: No self-references allowed here.
                this.idManager.Inject(null, objId);

                CustomSerialIO customIO = new CustomSerialIO();
                customIO.Deserialize(this);
                if (customIO.Data.Any())
                {
                    DummyNode surrogateConstructor = new DummyNode();
                    surrogateConstructor.Parent = result;
                    foreach (var pair in customIO.Data)
                    {
                        StringNode key   = new StringNode(pair.Key);
                        DataNode   value = pair.Value as DataNode;
                        key.Parent   = surrogateConstructor;
                        value.Parent = surrogateConstructor;
                    }
                }
            }

            // Prepare object reference
            this.idManager.Inject(result, objId);

            if (custom)
            {
                CustomSerialIO customIO = new CustomSerialIO();
                customIO.Deserialize(this);
                foreach (var pair in customIO.Data)
                {
                    StringNode key   = new StringNode(pair.Key);
                    DataNode   value = pair.Value as DataNode;
                    key.Parent   = result;
                    value.Parent = result;
                }
            }
            else
            {
                // Determine data layout
                bool           wasThereBefore = this.GetCachedTypeDataLayout(objTypeString) != null;
                TypeDataLayout layout         = this.ReadTypeDataLayout(objTypeString);
                if (!wasThereBefore)
                {
                    TypeDataLayoutNode layoutNode = new TypeDataLayoutNode(new TypeDataLayout(layout));
                    layoutNode.Parent = result;
                }

                // Read fields
                if (this.dataVersion <= 2)
                {
                    for (int i = 0; i < layout.Fields.Length; i++)
                    {
                        DataNode fieldValue = this.ReadObject() as DataNode;
                        fieldValue.Parent = result;
                        fieldValue.Name   = layout.Fields[i].name;
                    }
                }
                else if (this.dataVersion >= 3)
                {
                    bool[] fieldOmitted = new bool[layout.Fields.Length];
                    this.ReadArrayData(fieldOmitted);

                    for (int i = 0; i < layout.Fields.Length; i++)
                    {
                        if (fieldOmitted[i])
                        {
                            continue;
                        }
                        DataNode fieldValue = this.ReadObject() as DataNode;
                        fieldValue.Parent = result;
                        fieldValue.Name   = layout.Fields[i].name;
                    }
                }
            }

            return(result);
        }
Exemplo n.º 21
0
        private object ReadStruct(XElement element, ObjectHeader header)
        {
            // Read struct type
            string customString    = element.GetAttributeValue("custom");
            string surrogateString = element.GetAttributeValue("surrogate");
            bool   custom          = customString != null && XmlConvert.ToBoolean(customString);
            bool   surrogate       = surrogateString != null && XmlConvert.ToBoolean(surrogateString);

            // Retrieve surrogate if requested
            ISerializeSurrogate objSurrogate = null;

            if (surrogate && header.ObjectType != null)
            {
                objSurrogate = GetSurrogateFor(header.ObjectType);
            }

            // Construct object
            object obj = null;

            if (header.ObjectType != null)
            {
                if (objSurrogate != null)
                {
                    custom = true;

                    // Set fake object reference for surrogate constructor: No self-references allowed here.
                    this.idManager.Inject(null, header.ObjectId);

                    CustomSerialIO customIO            = new CustomSerialIO();
                    XElement       customHeaderElement = element.Element(CustomSerialIO.HeaderElement) ?? element.Elements().FirstOrDefault();
                    if (customHeaderElement != null)
                    {
                        customIO.Deserialize(this, customHeaderElement);
                    }
                    try { obj = objSurrogate.ConstructObject(customIO, header.ObjectType); }
                    catch (Exception e) { this.LogCustomDeserializationError(header.ObjectId, header.ObjectType, e); }
                }
                if (obj == null)
                {
                    obj = header.ObjectType.CreateInstanceOf();
                }
            }

            // Prepare object reference
            this.idManager.Inject(obj, header.ObjectId);

            // Read custom object data
            if (custom)
            {
                CustomSerialIO customIO          = new CustomSerialIO();
                XElement       customBodyElement = element.Element(CustomSerialIO.BodyElement) ?? element.Elements().ElementAtOrDefault(1);
                if (customBodyElement != null)
                {
                    customIO.Deserialize(this, customBodyElement);
                }

                ISerializeExplicit objAsCustom;
                if (objSurrogate != null)
                {
                    objSurrogate.RealObject = obj;
                    objAsCustom             = objSurrogate.SurrogateObject;
                }
                else
                {
                    objAsCustom = obj as ISerializeExplicit;
                }

                if (objAsCustom != null)
                {
                    try { objAsCustom.ReadData(customIO); }
                    catch (Exception e) { this.LogCustomDeserializationError(header.ObjectId, header.ObjectType, e); }
                }
                else if (obj != null && header.ObjectType != null)
                {
                    this.LocalLog.WriteWarning(
                        "Object data (Id {0}) is flagged for custom deserialization, yet the objects Type ('{1}') does not support it. Guessing associated fields...",
                        header.ObjectId,
                        Log.Type(header.ObjectType));
                    this.LocalLog.PushIndent();
                    foreach (var pair in customIO.Data)
                    {
                        this.AssignValueToField(header.SerializeType, obj, pair.Key, pair.Value);
                    }
                    this.LocalLog.PopIndent();
                }
            }
            // Red non-custom object data
            else if (!element.IsEmpty)
            {
                // Read fields
                object fieldValue;
                foreach (XElement fieldElement in element.Elements())
                {
                    fieldValue = this.ReadObjectData(fieldElement);
                    this.AssignValueToField(header.SerializeType, obj, GetCodeElementName(fieldElement.Name.LocalName), fieldValue);
                }
            }

            return(obj);
        }
Exemplo n.º 22
0
		/// <summary>
		/// Writes the specified structural object, including references objects.
		/// </summary>
		/// <param name="obj">The object to write.</param>
		/// <param name="objSerializeType">The <see cref="Duality.Serialization.SerializeType"/> describing the object.</param>
		/// <param name="id">The objects id.</param>
		protected void WriteStruct(object obj, SerializeType objSerializeType, uint id = 0)
		{
			ISerializable objAsCustom = obj as ISerializable;
			ISurrogate objSurrogate = this.GetSurrogateFor(objSerializeType.Type);

			// Write the structs data type
			this.writer.Write(objSerializeType.TypeString);
			this.writer.Write(id);
			this.writer.Write(objAsCustom != null);
			this.writer.Write(objSurrogate != null);

			if (objSurrogate != null)
			{
				objSurrogate.RealObject = obj;
				objAsCustom = objSurrogate.SurrogateObject;

				CustomSerialIO customIO = new CustomSerialIO();
				try { objSurrogate.WriteConstructorData(customIO); }
				catch (Exception e) { this.LogCustomSerializationError(id, objSerializeType.Type, e); }
				customIO.Serialize(this);
			}

			if (objAsCustom != null)
			{
				CustomSerialIO customIO = new CustomSerialIO();
				try { objAsCustom.WriteData(customIO); }
				catch (Exception e) { this.LogCustomSerializationError(id, objSerializeType.Type, e); }
				customIO.Serialize(this);
			}
			else
			{
				// Assure the type data layout has bee written (only once per file)
				this.WriteTypeDataLayout(objSerializeType);

				// Write the structs fields
				foreach (FieldInfo field in objSerializeType.Fields)
				{
					object val = field.GetValue(obj);

					if (val != null && this.IsFieldBlocked(field))
						val = field.FieldType.GetDefaultInstanceOf();

					this.WriteObject(val);
				}
			}
		}
Exemplo n.º 23
0
		/// <summary>
		/// Writes the specified structural object, including references objects.
		/// </summary>
		/// <param name="obj">The object to write.</param>
		/// <param name="objSerializeType">The <see cref="Duality.Serialization.SerializeType"/> describing the object.</param>
		/// <param name="id">The objects id.</param>
		protected void WriteStruct(object obj, SerializeType objSerializeType, uint id = 0)
		{
			ISerializable objAsCustom = obj as ISerializable;
			ISurrogate objSurrogate = this.GetSurrogateFor(objSerializeType.Type);

			// Write the structs data type
			this.writer.Write(objSerializeType.TypeString);
			this.writer.Write(id);
			this.writer.Write(objAsCustom != null);
			this.writer.Write(objSurrogate != null);

			if (objSurrogate != null)
			{
				objSurrogate.RealObject = obj;
				objAsCustom = objSurrogate.SurrogateObject;

				CustomSerialIO customIO = new CustomSerialIO();
				try { objSurrogate.WriteConstructorData(customIO); }
				catch (Exception e) { this.LogCustomSerializationError(id, objSerializeType.Type, e); }
				customIO.Serialize(this);
			}

			if (objAsCustom != null)
			{
				CustomSerialIO customIO = new CustomSerialIO();
				try { objAsCustom.WriteData(customIO); }
				catch (Exception e) { this.LogCustomSerializationError(id, objSerializeType.Type, e); }
				customIO.Serialize(this);
			}
			else
			{
				// Assure the type data layout has bee written (only once per file)
				this.WriteTypeDataLayout(objSerializeType);

				// Write omitted field bitmask
				bool[] fieldOmitted = new bool[objSerializeType.Fields.Length];
				for (int i = 0; i < fieldOmitted.Length; i++)
				{
					fieldOmitted[i] = this.IsFieldBlocked(objSerializeType.Fields[i], obj);
				}
				this.WriteArrayData(fieldOmitted);

				// Write the structs fields
				for (int i = 0; i < objSerializeType.Fields.Length; i++)
				{
					if (fieldOmitted[i]) continue;
					this.WriteObject(objSerializeType.Fields[i].GetValue(obj));
				}
			}
		}
		/// <summary>
		/// Writes the specified <see cref="Duality.Serialization.MetaFormat.StructNode"/>, including possible child nodes.
		/// </summary>
		/// <param name="node"></param>
		protected void WriteStruct(StructNode node)
		{
			// Write the structs data type
			this.writer.Write(node.TypeString);
			this.writer.Write(node.ObjId);
			this.writer.Write(node.CustomSerialization);
			this.writer.Write(node.SurrogateSerialization);

			if (node.SurrogateSerialization)
			{
				CustomSerialIO customIO = new CustomSerialIO();
				DummyNode surrogateConstructor = node.SubNodes.FirstOrDefault() as DummyNode;
				if (surrogateConstructor != null)
				{
					var enumerator = surrogateConstructor.SubNodes.GetEnumerator();
					while (enumerator.MoveNext())
					{
						StringNode key = enumerator.Current as StringNode;
						if (enumerator.MoveNext() && key != null)
						{
							DataNode value = enumerator.Current;
							customIO.WriteValue(key.StringValue, value);
						}
					}
				}
				customIO.Serialize(this);
			}

			if (node.CustomSerialization || node.SurrogateSerialization)
			{
				CustomSerialIO customIO = new CustomSerialIO();
				var enumerator = node.SubNodes.GetEnumerator();
				while (enumerator.MoveNext())
				{
					StringNode key = enumerator.Current as StringNode;
					if (key != null && enumerator.MoveNext())
					{
						DataNode value = enumerator.Current;
						customIO.WriteValue(key.StringValue, value);
					}
				}
				customIO.Serialize(this);
			}
			else
			{
				bool skipLayout = false;
				if (node.SubNodes.FirstOrDefault() is TypeDataLayoutNode)
				{
					TypeDataLayoutNode typeDataLayout = node.SubNodes.FirstOrDefault() as TypeDataLayoutNode;
					this.WriteTypeDataLayout(typeDataLayout.Layout, node.TypeString);
					skipLayout = true;
				}
				else
				{
					this.WriteTypeDataLayout(node.TypeString);
				}

				// Write the structs fields
				foreach (DataNode subNode in node.SubNodes)
				{
					if (skipLayout)
					{
						skipLayout = false;
						continue;
					}
					if (subNode is DummyNode) continue;
					this.WriteObject(subNode);
				}
			}
		}