public void SerializeWorld(World world, Stream stream)
        {
            var rootNode = new TWXmlNode(TWXmlNode.CreateXmlDocument(), "World");

            var typesList = new List <WorldObjectType>();

            var worldObjectsNode = rootNode.CreateChildNode("WorldObjects");

            for (int i = 0; i < world.WorldObjectList.Count; i++)
            {
                var wObj = world.WorldObjectList[i];
                if (!typesList.Contains(wObj.ObjectType))
                {
                    typesList.Add(wObj.ObjectType);
                }

                SerializeWorldObject(wObj, worldObjectsNode.CreateChildNode("WorldObject"));
            }

            var typesNode = rootNode.CreateChildNode("WorldObjectTypes");

            for (int i = 0; i < typesList.Count; i++)
            {
                var type = typesList[i];

                SerializeWorldObjectType(type, typesNode.CreateChildNode("Type"));
            }


            rootNode.Document.Save(stream);
        }
Exemplo n.º 2
0
        private void serializeObject(Type type, TWXmlNode node, object obj)
        {
            foreach (var fieldInfo in type.GetFields())
            {
                object value = fieldInfo.GetValue(obj);
                if (value == null)
                {
                    continue;
                }


                serializeElement(node.CreateChildNode(fieldInfo.Name), fieldInfo.FieldType, value);
            }
            foreach (var propertyInfo in type.GetProperties())
            {
                if (propertyInfo.GetGetMethod() == null || propertyInfo.GetSetMethod() == null)
                {
                    continue;
                }
                if (propertyInfo.GetIndexParameters().Length != 0)
                {
                    continue;
                }

                object value = propertyInfo.GetValue(obj, null);
                if (value == null)
                {
                    continue;
                }

                serializeElement(node.CreateChildNode(propertyInfo.Name), propertyInfo.PropertyType, value);
            }
        }
        public void SerializeTileData(TileData data, Stream stream)
        {
            var node  = new TWXmlNode(TWXmlNode.CreateXmlDocument(), "TileData");
            var guide = node.CreateChildNode("Guid");

            XMLSerializer.WriteGuid(guide, data.Guid);
            var mesh = node.CreateChildNode("Mesh");

            XMLSerializer.WriteGuid(mesh, data.Mesh.Guid);

            var dimensions = node.CreateChildNode("Dimensions");

            XMLSerializer.WriteVector3(dimensions, data.Dimensions);

            var facetypes = node.CreateChildNode("Faces");

            for (int i = 0; i < data.FaceTypes.Length; i++)
            {
                if (data.FaceTypes[i] == null)
                {
                    continue;
                }
                var faceNode = facetypes.CreateChildNode("Face");
                var face     = (TileFace)(i + 1);
                faceNode.AddAttribute("Face", face.ToString());

                XMLSerializer.WriteGuid(faceNode.CreateChildNode("Type"), data.FaceTypes[i].GetRoot().Guid);
                XMLSerializer.WriteBoolean(faceNode.CreateChildNode("Winding"), data.GetTotalWinding(face));
            }

            node.XmlDocument.Save(stream);
        }
        public void SerializeWorldObject(WorldObject obj, TWXmlNode node)
        {
            var position = node.CreateChildNode("Position");

            XMLSerializer.WriteVector3(position, obj.Position);

            var rotation = node.CreateChildNode("Rotation");

            XMLSerializer.WriteQuaternion(rotation, obj.Rotation);

            var type = node.CreateChildNode("Type");

            XMLSerializer.WriteGuid(type, obj.ObjectType.Guid);
        }
        public void SerializeWorldObjectType(WorldObjectType type, TWXmlNode node)
        {
            var guid = node.CreateChildNode("Guid");

            XMLSerializer.WriteGuid(guid, type.Guid);

            var mesh = node.CreateChildNode("Mesh");

            XMLSerializer.WriteGuid(mesh, type.Mesh.Guid);

            var tileData = node.CreateChildNode("TileData");

            XMLSerializer.WriteGuid(tileData, type.TileData.Guid);
        }
Exemplo n.º 6
0
        public void Save(DirectoryInfo dir)
        {
            TWXmlNode root = new TWXmlNode(TWXmlNode.CreateXmlDocument(), "World");

            root.AddChildNode("SerializerVersion", "0.1");
            var entitiesNode = root.CreateChildNode("Entities");

            entitiesNode.AddAttributeInt("Count", entities.Count);

            dir.Create();
            for (int i = 0; i < entities.Count; i++)
            {
                var ent = entities[i];


                var relativePath = "Entity" + i.ToString("0000") + ".xml";

                var fi = new FileInfo(dir.FullName + "\\" + relativePath);

                saveEntity(ent, fi);

                var entityNode = entitiesNode.CreateChildNode("Entity");
                entityNode.AddAttribute("Type", ent.GetType().AssemblyQualifiedName);
                entityNode.AddAttribute("RelativePath", relativePath);
            }
            using (var fs = new FileStream(dir.FullName + "\\World.xml", FileMode.Create))
            {
                root.Document.Save(fs);
            }
        }
Exemplo n.º 7
0
        /// <summary>
        /// The node is the node for this element, not for the parent object
        /// </summary>
        private void serializeElement(TWXmlNode node, Type type, object value)
        {
            if (value == null)
            {
                return;
            }
            foreach (var customSerializer in elementSerializers)
            {
                if (!type.IsAssignableFrom(customSerializer.Key))
                {
                    continue;
                }

                customSerializer.Value.Serialize(node, value);
                return;
            }
            for (int i = 0; i < serializers.Count; i++)
            {
                var s = serializers[i];

                if (s.SerializeElement(node, type, value, this))
                {
                    return;
                }
            }

            if (type == typeof(float) ||
                type == typeof(string) ||
                type == typeof(Int16) ||
                type == typeof(Int32) ||
                type == typeof(Int64))
            {
                node.Value = value.ToString();
            }
            else if (type.IsEnum)
            {
                node.Value = value.ToString();
            }

            else if (type.IsArray)
            {
                int  count;
                Type elementType;
                count       = (int)type.GetProperty("Length").GetValue(value, null);
                elementType = type.GetElementType();
                var getMethod = type.GetMethod("Get");
                for (int i = 0; i < count; i++)
                {
                    serializeElement(
                        node.CreateChildNode(elementType.Name),
                        elementType,
                        getMethod.Invoke(value, new object[] { i }));
                }
            }
            else if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(List <>))
            {
                int  count;
                Type elementType;
                elementType = type.GetGenericArguments()[0];
                count       = (int)type.GetProperty("Count").GetValue(value, null);
                var indexerProperty = (PropertyInfo)type.GetDefaultMembers()[0];
                for (int i = 0; i < count; i++)
                {
                    serializeElement(
                        node.CreateChildNode(elementType.Name),
                        elementType,
                        indexerProperty.GetValue(value, new object[] { i }));
                }
            }
            else if (type == typeof(Color))
            {
                XMLSerializer.WriteColor(node, (Color)value);
            }
            else if (type == typeof(Matrix))
            {
                XMLSerializer.WriteMatrix(node, (Matrix)value);
            }


            else
            {
                if (type.IsPrimitive)
                {
                    throw new InvalidOperationException("Can't serialize primitive type");
                }

                serializeObject(type, node, value);
            }
        }