// Import TMX
        string ImportTMX(string path)
        {
            try
            {
                XmlDocument doc = new XmlDocument();
                doc.Load(path);
                var mapNode = doc.SelectSingleNode("/map");
                width  = ReadIntAttribute(mapNode, "width");
                height = ReadIntAttribute(mapNode, "height");

                // var tileSetNodes = mapNode.SelectNodes("tileset");
                // if (tileSetNodes.Count > 1) return "Only one tileset supported"; // just ignore this

                var layersNodes = mapNode.SelectNodes("layer");
                foreach (XmlNode layerNode in layersNodes)
                {
                    string name        = layerNode.Attributes["name"].Value;
                    int    layerWidth  = ReadIntAttribute(layerNode, "width");
                    int    layerHeight = ReadIntAttribute(layerNode, "height");
                    if (layerHeight != height || layerWidth != width)
                    {
                        return("Layer \"" + name + "\" has invalid dimensions");
                    }

                    var    dataNode    = layerNode.SelectSingleNode("data");
                    string encoding    = dataNode.Attributes["encoding"].Value;
                    string compression = (encoding == "base64")?dataNode.Attributes["compression"].Value:"";

                    if (encoding == "base64" && compression == "zlib")
                    {
                        uint[] data       = BytesToInts(ZlibInflate(System.Convert.FromBase64String(dataNode.InnerText)));
                        var    layerProxy = new LayerProxy();
                        layerProxy.name  = name;
                        layerProxy.tiles = data;
                        layers.Add(layerProxy);
                    }
                    else
                    {
                        return("Unsupported encoding type");
                    }
                }
            }
            catch (System.Exception e) { return(e.ToString()); }
            return("");
        }
Exemple #2
0
        private static LayerProxy[] MapToProxy(Layer[] originalArray)
        {
            var result = new LayerProxy[originalArray.Length];

            int i = 0;

            foreach (var original in originalArray)
            {
                result[i++] = new LayerProxy()
                {
                    Id       = original.Id,
                    Name     = original.Name,
                    ParentId = original.ParentId ?? -1,
                    R        = original.Color.R,
                    G        = original.Color.G,
                    B        = original.Color.B,
                    A        = original.Color.A
                };
            }

            return(result);
        }
		// Import TMX
		string ImportTMX(string path)
		{
			try
			{
				XmlDocument doc = new XmlDocument();
				doc.Load(path);
				var mapNode = doc.SelectSingleNode("/map");
				width = ReadIntAttribute(mapNode, "width");
				height = ReadIntAttribute(mapNode, "height");
				string orientation = ReadStringAttribute(mapNode, "orientation", "orthogonal");

				if (orientation != "orthogonal" && orientation != "staggered") {
					throw new System.Exception("ImportTMX only supports orthogonal and staggered tilemaps.\n\n\n");
				}
				staggered = orientation == "staggered";

				// var tileSetNodes = mapNode.SelectNodes("tileset");
				// if (tileSetNodes.Count > 1) return "Only one tileset supported"; // just ignore this
				
				var layersNodes = mapNode.SelectNodes("layer");
				foreach (XmlNode layerNode in layersNodes)
				{
					string name = layerNode.Attributes["name"].Value;
					int layerWidth = ReadIntAttribute(layerNode, "width");
					int layerHeight = ReadIntAttribute(layerNode, "height");
					if (layerHeight != height || layerWidth != width) return "Layer \"" + name + "\" has invalid dimensions";
					
					var dataNode = layerNode.SelectSingleNode("data");
					string encoding = (dataNode.Attributes["encoding"] != null)?dataNode.Attributes["encoding"].Value:"";
					string compression = (dataNode.Attributes["compression"] != null)?dataNode.Attributes["compression"].Value:"";

					uint[] data = null;
					if (encoding == "base64")
					{
						if (compression == "zlib")
						{
							data = BytesToInts(ZlibInflate(System.Convert.FromBase64String(dataNode.InnerText)));
						}
						else if (compression == "")
						{
							data = BytesToInts(System.Convert.FromBase64String(dataNode.InnerText));
						}
						else return FormatErrorString;
					}
					else if (encoding == "")
					{
						List<uint> values = new List<uint>();
						var tileNodes = dataNode.SelectNodes("tile");
						foreach (XmlNode tileNode in tileNodes)
							values.Add( uint.Parse(tileNode.Attributes["gid"].Value, System.Globalization.NumberFormatInfo.InvariantInfo) );
						data = values.ToArray();
					}
					else
					{
						return FormatErrorString;
					}

					if (data != null)
					{
						var layerProxy = new LayerProxy();
						layerProxy.name = name;
						layerProxy.tiles = data;
						layers.Add(layerProxy);
					}
				}
			}
			catch (System.Exception e) { return e.ToString(); }
			return "";
		}
        // Import TMX
        string ImportTMX(string path)
        {
            try
            {
                XmlDocument doc = new XmlDocument();
                doc.Load(path);
                var mapNode = doc.SelectSingleNode("/map");
                width  = ReadIntAttribute(mapNode, "width");
                height = ReadIntAttribute(mapNode, "height");

                // var tileSetNodes = mapNode.SelectNodes("tileset");
                // if (tileSetNodes.Count > 1) return "Only one tileset supported"; // just ignore this

                var layersNodes = mapNode.SelectNodes("layer");
                foreach (XmlNode layerNode in layersNodes)
                {
                    string name        = layerNode.Attributes["name"].Value;
                    int    layerWidth  = ReadIntAttribute(layerNode, "width");
                    int    layerHeight = ReadIntAttribute(layerNode, "height");
                    if (layerHeight != height || layerWidth != width)
                    {
                        return("Layer \"" + name + "\" has invalid dimensions");
                    }

                    var    dataNode    = layerNode.SelectSingleNode("data");
                    string encoding    = (dataNode.Attributes["encoding"] != null)?dataNode.Attributes["encoding"].Value:"";
                    string compression = (dataNode.Attributes["compression"] != null)?dataNode.Attributes["compression"].Value:"";

                    uint[] data = null;
                    if (encoding == "base64")
                    {
                        if (compression == "zlib")
                        {
                            data = BytesToInts(ZlibInflate(System.Convert.FromBase64String(dataNode.InnerText)));
                        }
                        else if (compression == "")
                        {
                            data = BytesToInts(System.Convert.FromBase64String(dataNode.InnerText));
                        }
                        else
                        {
                            return(FormatErrorString);
                        }
                    }
                    else if (encoding == "")
                    {
                        List <uint> values    = new List <uint>();
                        var         tileNodes = dataNode.SelectNodes("tile");
                        foreach (XmlNode tileNode in tileNodes)
                        {
                            values.Add(uint.Parse(tileNode.Attributes["gid"].Value, System.Globalization.NumberFormatInfo.InvariantInfo));
                        }
                        data = values.ToArray();
                    }
                    else
                    {
                        return(FormatErrorString);
                    }

                    if (data != null)
                    {
                        var layerProxy = new LayerProxy();
                        layerProxy.name  = name;
                        layerProxy.tiles = data;
                        layers.Add(layerProxy);
                    }
                }
            }
            catch (System.Exception e) { return(e.ToString()); }
            return("");
        }
        // Import TMX
        string ImportTMX(string path)
        {
            try
            {
                XmlDocument doc = new XmlDocument();
                doc.Load(path);
                var mapNode = doc.SelectSingleNode("/map");
                width = ReadIntAttribute(mapNode, "width");
                height = ReadIntAttribute(mapNode, "height");

                // var tileSetNodes = mapNode.SelectNodes("tileset");
                // if (tileSetNodes.Count > 1) return "Only one tileset supported"; // just ignore this

                var layersNodes = mapNode.SelectNodes("layer");
                foreach (XmlNode layerNode in layersNodes)
                {
                    string name = layerNode.Attributes["name"].Value;
                    int layerWidth = ReadIntAttribute(layerNode, "width");
                    int layerHeight = ReadIntAttribute(layerNode, "height");
                    if (layerHeight != height || layerWidth != width) return "Layer \"" + name + "\" has invalid dimensions";

                    var dataNode = layerNode.SelectSingleNode("data");
                    string encoding = dataNode.Attributes["encoding"].Value;
                    string compression = (encoding == "base64")?dataNode.Attributes["compression"].Value:"";

                    if (encoding == "base64" && compression == "zlib")
                    {
                        uint[] data = BytesToInts(ZlibInflate(System.Convert.FromBase64String(dataNode.InnerText)));
                        var layerProxy = new LayerProxy();
                        layerProxy.name = name;
                        layerProxy.tiles = data;
                        layers.Add(layerProxy);
                    }
                    else return "Unsupported encoding type";
                }
            }
            catch (System.Exception e) { return e.ToString(); }
            return "";
        }