Skip to content

Commit

Permalink
Merge pull request #81 from ebatianoSoftware/fixParsing-addStreamCons…
Browse files Browse the repository at this point in the history
…tructor

Fix parsing, add stream constructor
  • Loading branch information
TheBoneJarmer committed Aug 5, 2022
2 parents 6a6b7b4 + f2bb80f commit ef08815
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 9 deletions.
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

0 comments on commit ef08815

Please sign in to comment.