Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix parsing, add stream constructor #81

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 21 additions & 9 deletions src/TiledMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,18 @@ public TiledMap(string path)
}
}

/// <summary>
/// Loads a Tiled map in TMX format and parses it
/// </summary>
/// <param name="stream">Stream of opened tmx file</param>
/// <exception cref="TiledException">Thrown when the map could not be loaded</exception>
public TiledMap(Stream stream)
{
var streamReader = new StreamReader(stream);
var content = streamReader.ReadToEnd();
ParseXml(content);
}

/// <summary>
/// Can be used to parse the content of a TMX map manually instead of loading it using the constructor
/// </summary>
Expand Down Expand Up @@ -292,10 +304,10 @@ private TiledLayer ParseTileLayer(XmlNode node)
if (attrLocked != null) tiledLayer.locked = attrLocked.Value == "1";
if (attrTint != null) tiledLayer.tintcolor = attrTint.Value;
if (attrOpacity != null) tiledLayer.opacity = float.Parse(attrOpacity.Value);
if (attrOffsetX != null) tiledLayer.offsetX = float.Parse(attrOffsetX.Value);
if (attrOffsetY != null) tiledLayer.offsetY = float.Parse(attrOffsetY.Value);
if (attrParallaxX != null) tiledLayer.offsetX = float.Parse(attrParallaxX.Value);
if (attrParallaxY != null) tiledLayer.offsetY = float.Parse(attrParallaxY.Value);
if (attrOffsetX != null) tiledLayer.offsetX = float.Parse(attrOffsetX.Value, CultureInfo.InvariantCulture);
if (attrOffsetY != null) tiledLayer.offsetY = float.Parse(attrOffsetY.Value, CultureInfo.InvariantCulture);
if (attrParallaxX != null) tiledLayer.offsetX = float.Parse(attrParallaxX.Value, CultureInfo.InvariantCulture);
if (attrParallaxY != null) tiledLayer.offsetY = float.Parse(attrParallaxY.Value, CultureInfo.InvariantCulture);
if (nodesProperty != null) tiledLayer.properties = ParseProperties(nodesProperty);

ParseTileLayerData(nodeData, ref tiledLayer);
Expand Down Expand Up @@ -471,8 +483,8 @@ private TiledLayer ParseObjectLayer(XmlNode node)
if (attrVisible != null) tiledLayer.visible = attrVisible.Value == "1";
if (attrLocked != null) tiledLayer.locked = attrLocked.Value == "1";
if (attrTint != null) tiledLayer.tintcolor = attrTint.Value;
if (attrOffsetX != null) tiledLayer.offsetX = int.Parse(attrOffsetX.Value);
if (attrOffsetY != null) tiledLayer.offsetY = int.Parse(attrOffsetY.Value);
if (attrOffsetX != null) tiledLayer.offsetX = float.Parse(attrOffsetX.Value, CultureInfo.InvariantCulture);
if (attrOffsetY != null) tiledLayer.offsetY = float.Parse(attrOffsetY.Value, CultureInfo.InvariantCulture);
if (nodesProperty != null) tiledLayer.properties = ParseProperties(nodesProperty);

return tiledLayer;
Expand All @@ -497,8 +509,8 @@ private TiledLayer ParseImageLayer(XmlNode node)
if (attrVisible != null) tiledLayer.visible = attrVisible.Value == "1";
if (attrLocked != null) tiledLayer.locked = attrLocked.Value == "1";
if (attrTint != null) tiledLayer.tintcolor = attrTint.Value;
if (attrOffsetX != null) tiledLayer.offsetX = int.Parse(attrOffsetX.Value);
if (attrOffsetY != null) tiledLayer.offsetY = int.Parse(attrOffsetY.Value);
if (attrOffsetX != null) tiledLayer.offsetX = float.Parse(attrOffsetX.Value, CultureInfo.InvariantCulture);
if (attrOffsetY != null) tiledLayer.offsetY = float.Parse(attrOffsetY.Value, CultureInfo.InvariantCulture);
if (nodesProperty != null) tiledLayer.properties = ParseProperties(nodesProperty);
if (nodeImage != null) tiledLayer.image = ParseImage(nodeImage);

Expand Down Expand Up @@ -817,4 +829,4 @@ public bool IsTileFlippedDiagonal(TiledLayer layer, int dataIndex)
return (layer.dataRotationFlags[dataIndex] & (FLIPPED_DIAGONALLY_FLAG >> SHIFT_FLIP_FLAG_TO_BYTE)) > 0;
}
}
}
}
12 changes: 12 additions & 0 deletions src/TiledTileset.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,18 @@ public TiledTileset(string path)
}
}

/// <summary>
/// Loads a tileset in TSX format and parses it
/// </summary>
/// <param name="stream">The file stream of the TSX file</param>
/// <exception cref="TiledException">Thrown when the file could not be parsed</exception>
public TiledTileset(Stream stream)
{
var streamReader = new StreamReader(stream);
var content = streamReader.ReadToEnd();
ParseXml(content);
}

/// <summary>
/// Can be used to parse the content of a TSX tileset manually instead of loading it using the constructor
/// </summary>
Expand Down