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); }
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()); }
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); } }
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; } } }
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); }
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); } }
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); } }
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()); * */ }