Пример #1
0
        public void SerializeGrid()
        {
            // Creates a list of serializable gridtiles, as normal gridtiles contain a lot of extra information.
            // We're sure it's a sexier way of doing this, but we're out of time.
            GridTileSerializable[] serializableList = new GridTileSerializable[GridTileContainer.Children.Count];
            GridTileHandler gth = new GridTileHandler();;

            // Goes through the whole grid and converts all grid tiles to serializable grid tiles.
            int counter = 0;
            foreach (GridTile child in GridTileContainer.Children.Cast<GridTile>())
            {
                serializableList[counter] = gth.SerializeGridTile(child);
                counter++;
            }

            // Creates a serializer and a stream, and fills the stream with the serializable list, and writes it to a XML file.
            System.Xml.Serialization.XmlSerializer x = new System.Xml.Serialization.XmlSerializer(serializableList.GetType());
            StreamWriter file = new StreamWriter(@"c:\temp\saveFile.xml");
            x.Serialize(file, serializableList);
        }
Пример #2
0
        public void DeserializeGrid()
        {
            // Finds the XML document, and creates a handler for converting them back to normal grid tiles.
            XDocument xdoc = XDocument.Load(@"c:\temp\saveFile.xml");
            GridTileHandler gth = new GridTileHandler();

            // Queries that only gets entries with an image tag present, as all other is superflous.
            // Could have just avoided writing them to the file in the first place, but oh well.
            var allValidData =
                from element in xdoc.Descendants("GridTileSerializable")
                where element.Elements("Image").Any()
                select element;

            Console.WriteLine(allValidData.Count());

            // Goes through each entry it finds in the XML file and converts it back to a grid tile.
            foreach (var entry in allValidData)
            {
                byte[] temp = new Byte[entry.Element("Image").Value.Length * sizeof(char)];

                // Firstly, recreate the serializable grid tile.
                GridTileSerializable gts = new GridTileSerializable
                {
                    Rotation = int.Parse(entry.Element("Rotation").Value),
                    Id = int.Parse(entry.Element("Rotation").Value),
                    Row = int.Parse(entry.Element("Row").Value),
                    Column = int.Parse(entry.Element("Column").Value),
                };

                Console.WriteLine(entry.Element("Column").Value);
                gts.Image = new Byte[entry.Element("Image").Value.Length*sizeof (char)];
                // Apparently byte arrays are saved as a base 64 string, so we convert that this way.
                gts.Image = Convert.FromBase64String(entry.Element("Image").Value);

                // Copies the collision map size.
                gts.CollisionMap = new int[3][];
                for (int i = 0; i < gts.CollisionMap.Length; i++)
                {
                    gts.CollisionMap[i] = new int[3];
                }

                // Copies the content of the collision map.
                for (int i = 0; i < 3; i++)
                {
                    for (int j = 0; j < 3; j++)
                    {
                        if (entry.Element("CollisionMap").IsEmpty)
                            gts.CollisionMap[i][j] = 0;
                        else
                            gts.CollisionMap[i][j] = (int)Char.GetNumericValue(entry.Element("CollisionMap").Value.ElementAt(i));
                    }
                }

                // Converts all info of the serializable grid tile back to a normal grid tile.
                GridTile gridInfo = gth.DeserializeGridTile(gts);
                // Creates a brush based on the image in the grid tile.
                ImageBrush brush = new ImageBrush { ImageSource = gridInfo.Image.Source };

                // Finds the grid tile in the specific spot in the grid it was before, and sets all appropriate information.
                var tileToChange = GridTileContainer.Children.Cast<GridTile>().First(ele => Grid.GetRow(ele) == gridInfo.Row && Grid.GetColumn(ele) == gridInfo.Column);
                tileToChange.Background = brush;
                tileToChange.Image = gridInfo.Image;
                tileToChange.Name = gridInfo.Name;
                tileToChange.CollisionMap = gridInfo.CollisionMap;
            }
        }