Ejemplo n.º 1
0
        private void ParseData(XElement elem)
        {
            Logger.WriteLine("Parse {0} layer data ...", this.Name);

            string encoding    = TmxHelper.GetAttributeAsString(elem, "encoding", "");
            string compression = TmxHelper.GetAttributeAsString(elem, "compression", "");

            if (elem.Element("tile") != null)
            {
                ParseTileDataAsXml(elem);
            }
            else if (encoding == "csv")
            {
                ParseTileDataAsCsv(elem);
            }
            else if (encoding == "base64" && String.IsNullOrEmpty(compression))
            {
                ParseTileDataAsBase64(elem);
            }
            else if (encoding == "base64" && compression == "gzip")
            {
                ParseTileDataAsBase64GZip(elem);
            }
            else if (encoding == "base64" && compression == "zlib")
            {
                ParseTileDataAsBase64Zlib(elem);
            }
            else
            {
                TmxException.ThrowFormat("Unsupported schema for {0} layer data", this.Name);
            }
        }
Ejemplo n.º 2
0
        public static T GetAttributeAsEnum <T>(XElement elem, string attrName)
        {
            string enumString = elem.Attribute(attrName).Value.Replace("-", "_");

            T value = default(T);

            try
            {
                value = (T)Enum.Parse(typeof(T), enumString, true);
            }
            catch
            {
                StringBuilder msg = new StringBuilder();
                msg.AppendFormat("Could not convert '{0}' to enum of type '{1}'\n", enumString, typeof(T).ToString());
                msg.AppendFormat("Choices are:\n");

                foreach (T t in Enum.GetValues(typeof(T)))
                {
                    msg.AppendFormat("  {0}\n", t.ToString());
                }
                TmxException.ThrowFormat(msg.ToString());
            }

            return(value);
        }
Ejemplo n.º 3
0
        public static void FromAttributeException(Exception inner, XElement element)
        {
            StringBuilder builder = new StringBuilder(inner.Message);

            Array.ForEach(element.Attributes().ToArray(), a => builder.AppendFormat("\n  {0}", a.ToString()));
            TmxException.ThrowFormat("Error parsing {0} attributes\n{1}", element.Name, builder.ToString());
        }
Ejemplo n.º 4
0
        private void ParseMap(XDocument doc)
        {
            Program.WriteLine("Parsing map root ...");
            Program.WriteVerbose(doc.ToString());
            XElement map = doc.Element("map");

            try
            {
                this.Orientation         = TmxHelper.GetAttributeAsEnum <MapOrientation>(map, "orientation");
                this.StaggerAxis         = TmxHelper.GetAttributeAsEnum(map, "staggeraxis", MapStaggerAxis.Y);
                this.StaggerIndex        = TmxHelper.GetAttributeAsEnum(map, "staggerindex", MapStaggerIndex.Odd);
                this.HexSideLength       = TmxHelper.GetAttributeAsInt(map, "hexsidelength", 0);
                this.DrawOrderHorizontal = TmxHelper.GetAttributeAsString(map, "renderorder", "right-down").Contains("right") ? 1 : -1;
                this.DrawOrderVertical   = TmxHelper.GetAttributeAsString(map, "renderorder", "right-down").Contains("down") ? 1 : -1;
                this.Width           = TmxHelper.GetAttributeAsInt(map, "width");
                this.Height          = TmxHelper.GetAttributeAsInt(map, "height");
                this.TileWidth       = TmxHelper.GetAttributeAsInt(map, "tilewidth");
                this.TileHeight      = TmxHelper.GetAttributeAsInt(map, "tileheight");
                this.BackgroundColor = TmxHelper.GetAttributeAsColor(map, "backgroundcolor", Color.FromArgb(128, 128, 128));
            }
            catch (Exception e)
            {
                TmxException.FromAttributeException(e, map);
            }

            // Collect our map properties
            this.Properties = TmxProperties.FromXml(map);

            ParseAllTilesets(doc);
            ParseAllLayers(doc);
            ParseAllObjectGroups(doc);
        }
Ejemplo n.º 5
0
        private void ParseMapXml(XDocument doc)
        {
            Program.WriteLine("Parsing map root ...");
            //Program.WriteVerbose(doc.ToString()); // Some TMX files are far too big (cause out of memory exception) so don't do this
            XElement map = doc.Element("map");

            try
            {
                this.Orientation         = TmxHelper.GetAttributeAsEnum <MapOrientation>(map, "orientation");
                this.StaggerAxis         = TmxHelper.GetAttributeAsEnum(map, "staggeraxis", MapStaggerAxis.Y);
                this.StaggerIndex        = TmxHelper.GetAttributeAsEnum(map, "staggerindex", MapStaggerIndex.Odd);
                this.HexSideLength       = TmxHelper.GetAttributeAsInt(map, "hexsidelength", 0);
                this.DrawOrderHorizontal = TmxHelper.GetAttributeAsString(map, "renderorder", "right-down").Contains("right") ? 1 : -1;
                this.DrawOrderVertical   = TmxHelper.GetAttributeAsString(map, "renderorder", "right-down").Contains("down") ? 1 : -1;
                this.Width           = TmxHelper.GetAttributeAsInt(map, "width");
                this.Height          = TmxHelper.GetAttributeAsInt(map, "height");
                this.TileWidth       = TmxHelper.GetAttributeAsInt(map, "tilewidth");
                this.TileHeight      = TmxHelper.GetAttributeAsInt(map, "tileheight");
                this.BackgroundColor = TmxHelper.GetAttributeAsColor(map, "backgroundcolor", Color.FromArgb(128, 128, 128));
            }
            catch (Exception e)
            {
                TmxException.FromAttributeException(e, map);
            }

            // Collect our map properties
            this.Properties = TmxProperties.FromXml(map);

            ParseAllTilesets(doc);
            ParseAllLayers(doc);
            ParseAllObjectGroups(doc);

            // Once everything is loaded, take a moment to do additional plumbing
            ParseCompleted();
        }
Ejemplo n.º 6
0
 private void ReadTileIds(XElement xml)
 {
     TileIds.Clear();
     if (ParentData.Encoding == DataEncoding.Xml)
     {
         ReadTileIds_Xml(xml);
     }
     else if (ParentData.Encoding == DataEncoding.Csv)
     {
         ReadTiledIds_Csv(xml.Value);
     }
     else if (ParentData.Encoding == DataEncoding.Base64)
     {
         ReadTileIds_Base64(xml.Value);
     }
     else
     {
         TmxException.ThrowFormat("Unsupported encoding for chunk data in layer '{0}'", ParentData.ParentLayer);
     }
     for (int i = 0; i < TileIds.Count; i++)
     {
         uint tileId = TileIds[i];
         tileId = TmxMath.GetTileIdWithoutFlags(tileId);
         if (!ParentData.ParentLayer.ParentMap.Tiles.ContainsKey(tileId))
         {
             TileIds[i] = 0u;
         }
     }
 }
Ejemplo n.º 7
0
        private void ParseMap(XDocument doc)
        {
            Program.WriteLine("Parsing map root ...");
            Program.WriteVerbose(doc.ToString());
            XElement map = doc.Element("map");

            try
            {
                this.Orientation         = TmxHelper.GetAttributeAsString(map, "orientation");
                this.DrawOrderHorizontal = TmxHelper.GetAttributeAsString(map, "renderorder", "right-down").Contains("right") ? 1 : -1;
                this.DrawOrderVertical   = TmxHelper.GetAttributeAsString(map, "renderorder", "right-down").Contains("down") ? 1 : -1;
                this.Width           = TmxHelper.GetAttributeAsInt(map, "width");
                this.Height          = TmxHelper.GetAttributeAsInt(map, "height");
                this.TileWidth       = TmxHelper.GetAttributeAsInt(map, "tilewidth");
                this.TileHeight      = TmxHelper.GetAttributeAsInt(map, "tileheight");
                this.BackgroundColor = TmxHelper.GetAttributeAsColor(map, "backgroundcolor", Color.FromArgb(128, 128, 128));
            }
            catch (Exception e)
            {
                TmxException.FromAttributeException(e, map);
            }

            // We only support orthogonal maps
            if (this.Orientation != "orthogonal")
            {
                TmxException.ThrowFormat("Only orthogonal maps are supported. This map is set to \"{0}\"", this.Orientation);
            }

            // Collect our map properties
            this.Properties = TmxProperties.FromXml(map);

            ParseAllTilesets(doc);
            ParseAllLayers(doc);
            ParseAllObjectGroups(doc);
        }
Ejemplo n.º 8
0
        private void ParseMapXml(XDocument doc)
        {
            Logger.WriteVerbose("Parsing map root ...");
            XElement xElement = doc.Element("map");

            try
            {
                Orientation         = TmxHelper.GetAttributeAsEnum <MapOrientation>(xElement, "orientation");
                StaggerAxis         = TmxHelper.GetAttributeAsEnum(xElement, "staggeraxis", MapStaggerAxis.Y);
                StaggerIndex        = TmxHelper.GetAttributeAsEnum(xElement, "staggerindex", MapStaggerIndex.Odd);
                HexSideLength       = TmxHelper.GetAttributeAsInt(xElement, "hexsidelength", 0);
                DrawOrderHorizontal = (TmxHelper.GetAttributeAsString(xElement, "renderorder", "right-down").Contains("right") ? 1 : (-1));
                DrawOrderVertical   = (TmxHelper.GetAttributeAsString(xElement, "renderorder", "right-down").Contains("down") ? 1 : (-1));
                Width           = TmxHelper.GetAttributeAsInt(xElement, "width");
                Height          = TmxHelper.GetAttributeAsInt(xElement, "height");
                TileWidth       = TmxHelper.GetAttributeAsInt(xElement, "tilewidth");
                TileHeight      = TmxHelper.GetAttributeAsInt(xElement, "tileheight");
                BackgroundColor = TmxHelper.GetAttributeAsColor(xElement, "backgroundcolor", new Color32(128, 128, 128, 255));
            }
            catch (Exception inner)
            {
                TmxException.FromAttributeException(inner, xElement);
            }
            Properties = TmxProperties.FromXml(xElement);
            IsResource = Properties.GetPropertyValueAsBoolean("unity:resource", false);
            IsResource = (IsResource || !string.IsNullOrEmpty(Properties.GetPropertyValueAsString("unity:resourcePath", null)));
            ExplicitSortingLayerName = Properties.GetPropertyValueAsString("unity:sortingLayerName", "");
            ParseAllTilesets(doc);
            ParseAllTemplates(doc);
            LayerNodes      = TmxLayerNode.ListFromXml(xElement, null, this);
            MapSizeInPixels = CalculateMapSizeInPixels();
            TmxDisplayOrderVisitor visitor = new TmxDisplayOrderVisitor();

            Visit(visitor);
        }
Ejemplo n.º 9
0
        private void ParseMapXml(XDocument doc)
        {
            Logger.WriteVerbose("Parsing map root ...");

            XElement map = doc.Element("map");

            try
            {
                this.Orientation         = TmxHelper.GetAttributeAsEnum <MapOrientation>(map, "orientation");
                this.StaggerAxis         = TmxHelper.GetAttributeAsEnum(map, "staggeraxis", MapStaggerAxis.Y);
                this.StaggerIndex        = TmxHelper.GetAttributeAsEnum(map, "staggerindex", MapStaggerIndex.Odd);
                this.HexSideLength       = TmxHelper.GetAttributeAsInt(map, "hexsidelength", 0);
                this.DrawOrderHorizontal = TmxHelper.GetAttributeAsString(map, "renderorder", "right-down").Contains("right") ? 1 : -1;
                this.DrawOrderVertical   = TmxHelper.GetAttributeAsString(map, "renderorder", "right-down").Contains("down") ? 1 : -1;
                this.Width           = TmxHelper.GetAttributeAsInt(map, "width");
                this.Height          = TmxHelper.GetAttributeAsInt(map, "height");
                this.TileWidth       = TmxHelper.GetAttributeAsInt(map, "tilewidth");
                this.TileHeight      = TmxHelper.GetAttributeAsInt(map, "tileheight");
                this.BackgroundColor = TmxHelper.GetAttributeAsColor(map, "backgroundcolor", Color.FromArgb(128, 128, 128));
            }
            catch (Exception e)
            {
                TmxException.FromAttributeException(e, map);
            }

            // Collect our map properties
            this.Properties = TmxProperties.FromXml(map);

            // Check properties for us being a resource
            this.IsResource = this.Properties.GetPropertyValueAsBoolean("unity:resource", false);
            this.IsResource = this.IsResource || !String.IsNullOrEmpty(this.Properties.GetPropertyValueAsString("unity:resourcePath", null));

            ParseAllTilesets(doc);

            // Get all our child layer nodes
            this.LayerNodes = TmxLayerNode.ListFromXml(map, null, this);

            // Calcuate the size of the map. Isometric and hex maps make this more complicated than a simple width and height thing.
            this.MapSizeInPixels = CalculateMapSizeInPixels();

            // Visit each node in the map to assign display order
            TmxDisplayOrderVisitor visitor = new TmxDisplayOrderVisitor();

            this.Visit(visitor);
        }
Ejemplo n.º 10
0
        private void ParseMapXml(XDocument doc)
        {
            Logger.WriteLine("Parsing map root ...");

            XElement map = doc.Element("map");

            try
            {
                this.Orientation         = TmxHelper.GetAttributeAsEnum <MapOrientation>(map, "orientation");
                this.StaggerAxis         = TmxHelper.GetAttributeAsEnum(map, "staggeraxis", MapStaggerAxis.Y);
                this.StaggerIndex        = TmxHelper.GetAttributeAsEnum(map, "staggerindex", MapStaggerIndex.Odd);
                this.HexSideLength       = TmxHelper.GetAttributeAsInt(map, "hexsidelength", 0);
                this.DrawOrderHorizontal = TmxHelper.GetAttributeAsString(map, "renderorder", "right-down").Contains("right") ? 1 : -1;
                this.DrawOrderVertical   = TmxHelper.GetAttributeAsString(map, "renderorder", "right-down").Contains("down") ? 1 : -1;
                this.Width           = TmxHelper.GetAttributeAsInt(map, "width");
                this.Height          = TmxHelper.GetAttributeAsInt(map, "height");
                this.TileWidth       = TmxHelper.GetAttributeAsInt(map, "tilewidth");
                this.TileHeight      = TmxHelper.GetAttributeAsInt(map, "tileheight");
                this.BackgroundColor = TmxHelper.GetAttributeAsColor(map, "backgroundcolor", Color.FromArgb(128, 128, 128));
            }
            catch (Exception e)
            {
                TmxException.FromAttributeException(e, map);
            }

            // Collect our map properties
            this.Properties = TmxProperties.FromXml(map);

            // Check properties for us being a resource
            this.IsResource = this.Properties.GetPropertyValueAsBoolean("unity:resource", false);
            this.IsResource = this.IsResource || !String.IsNullOrEmpty(this.Properties.GetPropertyValueAsString("unity:resourcePath", null));

            ParseAllTilesets(doc);
            ParseAllLayers(doc);
            ParseAllObjectGroups(doc);

            // Once everything is loaded, take a moment to do additional plumbing
            ParseCompleted();
        }
Ejemplo n.º 11
0
        public static T GetStringAsEnum <T>(string enumString)
        {
            enumString = enumString.Replace("-", "_");
            T result = default(T);

            try
            {
                result = (T)Enum.Parse(typeof(T), enumString, true);
                return(result);
            }
            catch
            {
                StringBuilder stringBuilder = new StringBuilder();
                stringBuilder.AppendFormat("Could not convert '{0}' to enum of type '{1}'\n", enumString, typeof(T).ToString());
                stringBuilder.AppendFormat("Choices are:\n");
                foreach (T value in Enum.GetValues(typeof(T)))
                {
                    stringBuilder.AppendFormat("  {0}\n", value.ToString());
                }
                TmxException.ThrowFormat(stringBuilder.ToString());
                return(result);
            }
        }
Ejemplo n.º 12
0
 private void ReadTileIds_Base64(string data)
 {
     if (ParentData.Compression == DataCompression.None)
     {
         Logger.WriteVerbose("Parsing layer chunk data as base64 string ...");
         TileIds = data.Base64ToBytes().ToUInts().ToList();
     }
     else if (ParentData.Compression == DataCompression.Gzip)
     {
         Logger.WriteVerbose("Parsing layer chunk data as gzip compressed string ...");
         TileIds = data.Base64ToBytes().GzipDecompress().ToUInts()
                   .ToList();
     }
     else if (ParentData.Compression == DataCompression.Zlib)
     {
         Logger.WriteVerbose("Parsing layer chunk data as zlib string ...");
         TileIds = data.Base64ToBytes().ZlibDeflate().ToUInts()
                   .ToList();
     }
     else
     {
         TmxException.ThrowFormat("Unsupported compression for chunk data in layer '{0}'", ParentData.ParentLayer);
     }
 }
Ejemplo n.º 13
0
        private void ParseData(XElement elem)
        {
            Program.WriteLine("Parse {0} layer data ...", this.UniqueName);
            Program.WriteVerbose(elem.ToString());

            string encoding    = TmxHelper.GetAttributeAsString(elem, "encoding", "");
            string compression = TmxHelper.GetAttributeAsString(elem, "compression", "");

            if (elem.Element("tile") != null)
            {
                ParseTileDataAsXml(elem);
            }
            else if (encoding == "csv")
            {
                ParseTileDataAsCsv(elem);
            }
            else if (encoding == "base64" && String.IsNullOrEmpty(compression))
            {
                ParseTileDataAsBase64(elem);
            }
            else if (encoding == "base64" && compression == "gzip")
            {
                ParseTileDataAsBase64GZip(elem);
            }
            else if (encoding == "base64" && compression == "zlib")
            {
                ParseTileDataAsBase64Zlib(elem);
            }
            else
            {
                TmxException.ThrowFormat("Unsupported schema for {0} layer data", this.UniqueName);
            }

            // Note: This is too noisy and slow and doesn't really add anything.
            // Pretty-print the tileIds

            /*
             * //Program.WriteLine("TileIds for {0} layer:", this.Name);
             *
             * //uint largest = this.TileIds.Max();
             * //largest = TmxMath.GetTileIdWithoutFlags(largest);
             *
             * //int padding = largest.ToString().Length + 2;
             *
             * //StringBuilder builder = new StringBuilder();
             * //for (int t = 0; t < this.TileIds.Count(); ++t)
             * //{
             * //    if (t % this.Width == 0)
             * //    {
             * //        Program.WriteLine(builder.ToString());
             * //        builder.Clear();
             * //    }
             *
             * //    uint tileId = this.TileIds[t];
             * //    tileId = TmxMath.GetTileIdWithoutFlags(tileId);
             * //    builder.AppendFormat("{0}", tileId.ToString().PadLeft(padding));
             * //}
             *
             * //// Write the last row
             * //Program.WriteLine(builder.ToString());
             * */
        }