Exemple #1
0
		private string ConvertLayerDataToString(LayerData layer, bool compressed)
		{
			MemoryStream ms = new MemoryStream();
			BinaryWriter bw = new BinaryWriter(ms);

			for (int y = 0; y < layer.height; y++)
			{
				for (int x = 0; x < layer.width; x++)
				{
					bw.Write(layer.data[x, y]);
				}
			}

			// gzip decoding
			byte[] layerdataCompressed;

			if (compressed)
			{
				layerdataCompressed = gzip.Compress(ms.ToArray());
			}
			else
			{
				layerdataCompressed = ms.ToArray();
			}

			//Base64 Encodierung
			string layerdataEncoded = Base64.Encode(layerdataCompressed);
			return layerdataEncoded;
		}
Exemple #2
0
		void Open(string filename, bool loadTilesets)
		{
			Tilesets = new List<TilesetData>();
			Layers = new List<LayerData>();
			ObjectLayers = new List<Objectgroup>();
			Properties = new List<Property>();

			FileData = new XmlDocument();
			FileData.Load(filename);

			#region Get map info
			XmlNodeList xnl = FileData.SelectNodes("/map");

			MapVersion = xnl[0].Attributes["version"].Value;
			Orientation = xnl[0].Attributes["orientation"].Value;

			Width = Convert.ToInt32(xnl[0].Attributes["width"].Value);
			Height = Convert.ToInt32(xnl[0].Attributes["height"].Value);

			TileWidth = Convert.ToInt32(xnl[0].Attributes["tilewidth"].Value);
			TileHeight = Convert.ToInt32(xnl[0].Attributes["tileheight"].Value);
			#endregion

			#region Read properties
			xnl = FileData.SelectNodes("/map/properties");

			foreach (XmlNode j in xnl)
			{
				XmlNodeList subnodes = j.SelectNodes("child::property");

				foreach (XmlNode pNode in subnodes)
				{
					string name = pNode.Attributes[0].Name;
					string value = pNode.Attributes[0].Value;

					Properties.Add(new Property(pNode));
				}
			}
			#endregion

			#region Search tilesets
			xnl = FileData.SelectNodes("/map/tileset");

			foreach (XmlNode j in xnl)
			{
				// Tilesets
				TilesetData ts = new TilesetData();

				ts.imgsource = j.SelectNodes("child::image")[0].Attributes[0].Value; //Image Source für den Layer
				string imgsourceComplete = Paths.GetPath(filename) + ts.imgsource;

				// Load Tileset
				XmlNodeList nodelist = j.SelectNodes("child::tile");

				foreach (XmlNode tileXml in nodelist)
				{
					Tile tile = new Tile();
					tile.ID = tileXml.Attributes["id"].Value.ToString();

					xnl = tileXml.SelectNodes("child::properties");

					foreach (XmlNode jProp in xnl)
					{
						XmlNodeList subnodes = jProp.SelectNodes("child::property");

						foreach (XmlNode pNode in subnodes)
						{
							tile.Properties.Add(new Property(pNode));
						}
					}

					ts.Tiles.Add(tile);
				}

				// Load tiles
				if (loadTilesets)
				{
					try
					{
						ts.img = (Image8i)pooledLoader.FromFile(imgsourceComplete);
					}
					catch (FileNotFoundException ex)
					{
						throw new TilesetNotExistsException(ex.Message);
					}
				}

				// Attributes
				ts.name = j.Attributes["name"].Value; 
				ts.firstgid = Convert.ToInt32(j.Attributes["firstgid"].Value);
				ts.tilewidth = Convert.ToInt32(j.Attributes["tilewidth"].Value);
				ts.tileheight = Convert.ToInt32(j.Attributes["tileheight"].Value);

				Tilesets.Add(ts);
			}
			#endregion

			#region Layers ermitteln
			xnl = FileData.SelectNodes("/map/layer");

			foreach (XmlNode j in xnl) //pro layer
			{
				// Layers
				LayerData lr = new LayerData();

				// Attributes
				lr.name = j.Attributes["name"].Value;
				lr.width = Convert.ToInt32(j.Attributes["width"].Value);
				lr.height = Convert.ToInt32(j.Attributes["height"].Value);

				// Layer data
				// Attributes should be "<data encoding="base64" compression="gzip">"
				string encoding = j["data"].Attributes["encoding"].Value;

				string compression = "uncompressed";

				if (j["data"].Attributes["compression"] != null)
				{
					compression = j["data"].Attributes["compression"].Value;
				}

				if (encoding != "base64")
				{
					throw (new NotImplementedException("Weitere Codierungsarten sind noch nicht implementiert!"));
				}

				if (compression != "uncompressed" && compression != "gzip")
				{
					throw (new NotSupportedCompressionException("Weitere Kompressionsverfahren sind noch nicht implementiert!"));
				}

				// Base64 encoding
				string layerdataBase64Compressed = j.SelectNodes("child::data")[0].InnerText;
				layerdataBase64Compressed = layerdataBase64Compressed.TrimStart('\n');
				layerdataBase64Compressed = layerdataBase64Compressed.Trim();
				byte[] layerdataCompressed = Base64.Decode(layerdataBase64Compressed); 

				// gzip decode, if nessecary
				byte[] layerdataDecompressed;
				if (compression == "uncompressed")
				{
					layerdataDecompressed = layerdataCompressed;
				}
				else
				{
					layerdataDecompressed = gzip.Decompress(layerdataCompressed);
				}

				// Interpret coded data
				lr.data = new int[lr.width, lr.height];

				BinaryReader br = new BinaryReader(new MemoryStream(layerdataDecompressed));

				for (int y = 0; y < lr.height; y++)
				{
					for (int x = 0; x < lr.width; x++)
					{
						lr.data[x, y] = br.ReadInt32();
					}
				}
				
				Layers.Add(lr);
			}
			#endregion

			#region Objektlayer ermitteln
			xnl = FileData.SelectNodes("/map/objectgroup");

			foreach (XmlNode j in xnl) //pro layer
			{
				ObjectLayers.Add(new Objectgroup(j));
			}
			#endregion
		}