TiledSharp  0.9.1 r0
A .NET C# library for importing Tiled TMX tile maps
 All Classes Namespaces Files Functions Variables Enumerations Properties Pages
TiledCore.cs
Go to the documentation of this file.
1 // Distributed as part of TiledSharp, Copyright 2012 Marshall Ward
2 // Licensed under the Apache License, Version 2.0
3 // http://www.apache.org/licenses/LICENSE-2.0
4 using System;
5 using System.Collections.Generic;
6 using System.Collections.ObjectModel;
7 using System.Globalization;
8 using System.IO;
9 using System.IO.Compression;
10 using System.Linq;
11 using System.Reflection;
12 using System.Xml.Linq;
13 
14 namespace TiledSharp
15 {
16  public class TmxDocument
17  {
18  public string TmxDirectory {get; private set;}
19 
20  protected XDocument ReadXml(string filepath)
21  {
22  XDocument xDoc;
23 
24  var asm = Assembly.GetExecutingAssembly();
25  var manifest = asm.GetManifestResourceNames();
26 
27  var fileResPath = filepath.Replace(
28  Path.DirectorySeparatorChar.ToString(), ".");
29  var fileRes = Array.Find(manifest, s => s.EndsWith(fileResPath));
30 
31  // If there is a resource in the assembly, load the resource
32  // Otherwise, assume filepath is an explicit path
33  if (fileRes != null)
34  {
35  Stream xmlStream = asm.GetManifestResourceStream(fileRes);
36  xDoc = XDocument.Load(xmlStream);
37  TmxDirectory = "";
38  }
39  else
40  {
41  xDoc = XDocument.Load(filepath);
42  TmxDirectory = Path.GetDirectoryName(filepath);
43  }
44 
45  return xDoc;
46  }
47  }
48 
49  public interface ITmxElement
50  {
51  string Name {get;}
52  }
53 
54  public class TmxList<T> : KeyedCollection<string, T> where T : ITmxElement
55  {
56  public static Dictionary<Tuple<TmxList<T>, string>, int> nameCount
57  = new Dictionary<Tuple<TmxList<T>, string>, int>();
58 
59  public new void Add(T t)
60  {
61  // Rename duplicate entries by appending a number
62  var key = Tuple.Create<TmxList<T>, string> (this, t.Name);
63  if (this.Contains(t.Name))
64  nameCount[key] += 1;
65  else
66  nameCount.Add(key, 0);
67  base.Add(t);
68  }
69 
70  protected override string GetKeyForItem(T t)
71  {
72  var key = Tuple.Create<TmxList<T>, string> (this, t.Name);
73  var count = nameCount[key];
74 
75  var dupes = 0;
76  var itemKey = t.Name;
77 
78  // For duplicate keys, append a counter
79  // For pathological cases, insert underscores to ensure uniqueness
80  while (Contains(itemKey)) {
81  itemKey = t.Name + String.Concat(Enumerable.Repeat("_", dupes))
82  + count;
83  dupes++;
84  }
85 
86  return itemKey;
87  }
88  }
89 
90  public class PropertyDict : Dictionary<string, string>
91  {
92  public PropertyDict(XElement xmlProp)
93  {
94  if (xmlProp == null) return;
95 
96  foreach (var p in xmlProp.Elements("property"))
97  {
98  var pname = p.Attribute("name").Value;
99  var pval = p.Attribute("value").Value;
100  Add(pname, pval);
101  }
102  }
103  }
104 
105  public class TmxImage
106  {
107  public string Format {get; private set;}
108  public string Source {get; private set;}
109  public Stream Data {get; private set;}
110  public TmxColor Trans {get; private set;}
111  public int? Width {get; private set;}
112  public int? Height {get; private set;}
113 
114  public TmxImage(XElement xImage, string tmxDir = "")
115  {
116  if (xImage == null) return;
117 
118  var xSource = xImage.Attribute("source");
119 
120  if (xSource != null)
121  // Append directory if present
122  Source = Path.Combine(tmxDir, (string)xSource);
123  else {
124  Format = (string)xImage.Attribute("format");
125  var xData = xImage.Element("data");
126  var decodedStream = new TmxBase64Data(xData);
127  Data = decodedStream.Data;
128  }
129 
130  Trans = new TmxColor(xImage.Attribute("trans"));
131  Width = (int?)xImage.Attribute("width");
132  Height = (int?)xImage.Attribute("height");
133  }
134  }
135 
136  public class TmxColor
137  {
138  public int R;
139  public int G;
140  public int B;
141 
142  public TmxColor(XAttribute xColor)
143  {
144  if (xColor == null) return;
145 
146  var colorStr = ((string)xColor).TrimStart("#".ToCharArray());
147 
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);
151  }
152  }
153 
154  public class TmxBase64Data
155  {
156  public Stream Data {get; private set;}
157 
158  public TmxBase64Data(XElement xData)
159  {
160  if ((string)xData.Attribute("encoding") != "base64")
161  throw new Exception(
162  "TmxBase64Data: Only Base64-encoded data is supported.");
163 
164  var rawData = Convert.FromBase64String((string)xData.Value);
165  Data = new MemoryStream(rawData, false);
166 
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.");
175  }
176  }
177 }
int G
8-bit green color depth
Definition: TiledCore.cs:139
Base64 data decoder.
Definition: TiledCore.cs:154
int R
8-bit red color depth
Definition: TiledCore.cs:138
RGB color components.
Definition: TiledCore.cs:136
int Height
Image pixel height.
Definition: TiledCore.cs:112
string Format
Embedded image format (currently unused)
Definition: TiledCore.cs:107
override string GetKeyForItem(T t)
Map layer names to list index.
Definition: TiledCore.cs:70
TMX document importer.
Definition: TiledCore.cs:16
string TmxDirectory
Parent directory of TMX file.
Definition: TiledCore.cs:18
Stream Data
Embedded image data.
Definition: TiledCore.cs:109
TMX element interface.
Definition: TiledCore.cs:49
int B
8-bit blue color depth
Definition: TiledCore.cs:140
XDocument ReadXml(string filepath)
Parse XML content of TMX file.
Definition: TiledCore.cs:20
TmxBase64Data(XElement xData)
Base64 data decoding constructor.
Definition: TiledCore.cs:158
string Name
Layer or element name.
Definition: TiledCore.cs:51
string Source
External reference to image data.
Definition: TiledCore.cs:108
TmxImage(XElement xImage, string tmxDir="")
Map image constructor.
Definition: TiledCore.cs:114
TmxColor Trans
Image transparency color.
Definition: TiledCore.cs:110
Stream Data
Stream of decoded data element.
Definition: TiledCore.cs:156
TmxColor(XAttribute xColor)
Color constructor.
Definition: TiledCore.cs:142
new void Add(T t)
Add element to TMX list.
Definition: TiledCore.cs:59
User-defined property list.
Definition: TiledCore.cs:90
PropertyDict(XElement xmlProp)
Property list constructor.
Definition: TiledCore.cs:92
int Width
Image pixel width.
Definition: TiledCore.cs:111
List of map layer elements.
Definition: TiledCore.cs:54