5 using System.Collections.Generic;
6 using System.Collections.ObjectModel;
7 using System.Globalization;
9 using System.IO.Compression;
11 using System.Reflection;
12 using System.Xml.Linq;
20 protected XDocument
ReadXml(
string filepath)
24 var
asm = Assembly.GetExecutingAssembly();
25 var manifest = asm.GetManifestResourceNames();
27 var fileResPath = filepath.Replace(
28 Path.DirectorySeparatorChar.ToString(),
".");
29 var fileRes = Array.Find(manifest, s => s.EndsWith(fileResPath));
35 Stream xmlStream = asm.GetManifestResourceStream(fileRes);
36 xDoc = XDocument.Load(xmlStream);
41 xDoc = XDocument.Load(filepath);
54 public class TmxList<T> : KeyedCollection<string, T> where T :
ITmxElement
56 public static Dictionary<Tuple<TmxList<T>,
string>,
int> nameCount
57 =
new Dictionary<Tuple<TmxList<T>,
string>,
int>();
59 public new void Add(T t)
62 var key = Tuple.Create<
TmxList<T>,
string> (
this, t.Name);
63 if (this.Contains(t.Name))
66 nameCount.Add(key, 0);
72 var key = Tuple.Create<
TmxList<T>,
string> (
this, t.Name);
73 var count = nameCount[key];
80 while (Contains(itemKey)) {
81 itemKey = t.Name + String.Concat(Enumerable.Repeat(
"_", dupes))
94 if (xmlProp == null)
return;
96 foreach (var p
in xmlProp.Elements(
"property"))
98 var pname = p.Attribute(
"name").Value;
99 var pval = p.Attribute(
"value").Value;
109 public Stream
Data {
get;
private set;}
111 public int?
Width {
get;
private set;}
114 public TmxImage(XElement xImage,
string tmxDir =
"")
116 if (xImage == null)
return;
118 var xSource = xImage.Attribute(
"source");
122 Source = Path.Combine(tmxDir, (string)xSource);
124 Format = (string)xImage.Attribute(
"format");
125 var xData = xImage.Element(
"data");
127 Data = decodedStream.Data;
131 Width = (
int?)xImage.Attribute(
"width");
132 Height = (
int?)xImage.Attribute(
"height");
144 if (xColor == null)
return;
146 var colorStr = ((string)xColor).TrimStart(
"#".ToCharArray());
148 R = int.Parse(colorStr.Substring(0, 2), NumberStyles.HexNumber);
149 G = int.Parse(colorStr.Substring(2, 2), NumberStyles.HexNumber);
150 B = int.Parse(colorStr.Substring(4, 2), NumberStyles.HexNumber);
156 public Stream
Data {
get;
private set;}
160 if ((
string)xData.Attribute(
"encoding") !=
"base64")
162 "TmxBase64Data: Only Base64-encoded data is supported.");
164 var rawData = Convert.FromBase64String((string)xData.Value);
165 Data =
new MemoryStream(rawData,
false);
167 var compression = (string)xData.Attribute(
"compression");
168 if (compression ==
"gzip")
169 Data =
new GZipStream(
Data, CompressionMode.Decompress,
false);
170 else if (compression ==
"zlib")
171 Data =
new Ionic.Zlib.ZlibStream(
Data,
172 Ionic.Zlib.CompressionMode.Decompress,
false);
173 else if (compression != null)
174 throw new Exception(
"TmxBase64Data: Unknown compression.");
int G
8-bit green color depth
int R
8-bit red color depth
int Height
Image pixel height.
string Format
Embedded image format (currently unused)
override string GetKeyForItem(T t)
Map layer names to list index.
string TmxDirectory
Parent directory of TMX file.
Stream Data
Embedded image data.
int B
8-bit blue color depth
XDocument ReadXml(string filepath)
Parse XML content of TMX file.
TmxBase64Data(XElement xData)
Base64 data decoding constructor.
string Name
Layer or element name.
string Source
External reference to image data.
TmxImage(XElement xImage, string tmxDir="")
Map image constructor.
TmxColor Trans
Image transparency color.
Stream Data
Stream of decoded data element.
TmxColor(XAttribute xColor)
Color constructor.
new void Add(T t)
Add element to TMX list.
User-defined property list.
PropertyDict(XElement xmlProp)
Property list constructor.
int Width
Image pixel width.
List of map layer elements.