Example #1
0
        protected override object ReadObjectBody(DataType dataType)
        {
            DataNode result = null;

            if (dataType.IsPrimitiveType())
            {
                result = new PrimitiveNode(dataType, this.ReadPrimitive(dataType));
            }
            else if (dataType == DataType.String)
            {
                result = new StringNode(this.ReadString());
            }
            else if (dataType == DataType.Enum)
            {
                result = this.ReadEnum();
            }
            else if (dataType == DataType.Struct)
            {
                result = this.ReadStruct(false);
            }
            else if (dataType == DataType.ObjectRef)
            {
                result = this.ReadObjectRef();
            }
            else if (dataType == DataType.Array)
            {
                result = this.ReadArray();
            }
            else if (dataType == DataType.Class)
            {
                result = this.ReadStruct(true);
            }
            else if (dataType == DataType.Delegate)
            {
                result = this.ReadDelegate();
            }
            else if (dataType.IsMemberInfoType())
            {
                result = this.ReadMemberInfo(dataType);
            }

            return(result);
        }
Example #2
0
			public StringTreeNode(StringNode data) : base(data)
			{
				this.stringData = data;
			}
		/// <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;
		}
		protected override object ReadObjectBody(DataType dataType)
		{
			DataNode result = null;

			if (dataType.IsPrimitiveType())				result = new PrimitiveNode(dataType, this.ReadPrimitive(dataType));
			else if (dataType == DataType.String)		result = new StringNode(this.reader.ReadString());
			else if (dataType == DataType.Enum)			result = this.ReadEnum();
			else if (dataType == DataType.Struct)		result = this.ReadStruct(false);
			else if (dataType == DataType.ObjectRef)	result = this.ReadObjectRef();
			else if (dataType == DataType.Array)		result = this.ReadArray();
			else if (dataType == DataType.Class)		result = this.ReadStruct(true);
			else if (dataType == DataType.Delegate)		result = this.ReadDelegate();
			else if (dataType.IsMemberInfoType())		result = this.ReadMemberInfo(dataType);

			return result;
		}
Example #5
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);
        }
Example #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>
		/// 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;
		}
Example #8
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);
        }
Example #9
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);
                }
            }
        }