Fix issue by cloning template objects and overriding GID's in tmj parsing

This commit is contained in:
Daniel Cronqvist 2025-04-28 20:12:18 +02:00
parent 81277fdaf1
commit a08d6b0715
11 changed files with 151 additions and 8 deletions

View file

@ -1,7 +1,25 @@
using System.Linq;
namespace DotTiled; namespace DotTiled;
/// <summary> /// <summary>
/// An ellipse object in a map. The existing <see cref="Object.X"/>, <see cref="Object.Y"/>, <see cref="Object.Width"/>, /// An ellipse object in a map. The existing <see cref="Object.X"/>, <see cref="Object.Y"/>, <see cref="Object.Width"/>,
/// and <see cref="Object.Height"/> properties are used to determine the size of the ellipse. /// and <see cref="Object.Height"/> properties are used to determine the size of the ellipse.
/// </summary> /// </summary>
public class EllipseObject : Object { } public class EllipseObject : Object
{
internal override Object Clone() => new EllipseObject
{
ID = ID,
Name = Name,
Type = Type,
X = X,
Y = Y,
Width = Width,
Height = Height,
Rotation = Rotation,
Visible = Visible,
Template = Template,
Properties = Properties.Select(p => p.Clone()).ToList(),
};
}

View file

@ -64,4 +64,10 @@ public abstract class Object : HasPropertiesBase
/// <inheritdoc/> /// <inheritdoc/>
public override IList<IProperty> GetProperties() => Properties; public override IList<IProperty> GetProperties() => Properties;
/// <summary>
/// Creates a deep copy of the object.
/// </summary>
/// <returns></returns>
internal abstract Object Clone();
} }

View file

@ -1,7 +1,25 @@
using System.Linq;
namespace DotTiled; namespace DotTiled;
/// <summary> /// <summary>
/// A point object in a map. The existing <see cref="Object.X"/> and <see cref="Object.Y"/> properties are used to /// A point object in a map. The existing <see cref="Object.X"/> and <see cref="Object.Y"/> properties are used to
/// determine the position of the point. /// determine the position of the point.
/// </summary> /// </summary>
public class PointObject : Object { } public class PointObject : Object
{
internal override Object Clone() => new PointObject
{
ID = ID,
Name = Name,
Type = Type,
X = X,
Y = Y,
Width = Width,
Height = Height,
Rotation = Rotation,
Visible = Visible,
Template = Template,
Properties = Properties.Select(p => p.Clone()).ToList(),
};
}

View file

@ -1,4 +1,5 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using System.Numerics; using System.Numerics;
namespace DotTiled; namespace DotTiled;
@ -14,4 +15,20 @@ public class PolygonObject : Object
/// <see cref="Object.X"/> and <see cref="Object.Y"/> are used as the origin of the polygon. /// <see cref="Object.X"/> and <see cref="Object.Y"/> are used as the origin of the polygon.
/// </summary> /// </summary>
public required List<Vector2> Points { get; set; } public required List<Vector2> Points { get; set; }
internal override Object Clone() => new PolygonObject
{
ID = ID,
Name = Name,
Type = Type,
X = X,
Y = Y,
Width = Width,
Height = Height,
Rotation = Rotation,
Visible = Visible,
Template = Template,
Properties = Properties.Select(p => p.Clone()).ToList(),
Points = Points.ToList(),
};
} }

View file

@ -1,4 +1,5 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using System.Numerics; using System.Numerics;
namespace DotTiled; namespace DotTiled;
@ -13,4 +14,20 @@ public class PolylineObject : Object
/// The points that make up the polyline. <see cref="Object.X"/> and <see cref="Object.Y"/> are used as the origin of the polyline. /// The points that make up the polyline. <see cref="Object.X"/> and <see cref="Object.Y"/> are used as the origin of the polyline.
/// </summary> /// </summary>
public required List<Vector2> Points { get; set; } public required List<Vector2> Points { get; set; }
internal override Object Clone() => new PolylineObject
{
ID = ID,
Name = Name,
Type = Type,
X = X,
Y = Y,
Width = Width,
Height = Height,
Rotation = Rotation,
Visible = Visible,
Template = Template,
Properties = Properties.Select(p => p.Clone()).ToList(),
Points = Points.ToList(),
};
} }

View file

@ -1,7 +1,25 @@
using System.Linq;
namespace DotTiled; namespace DotTiled;
/// <summary> /// <summary>
/// A rectangle object in a map. The existing <see cref="Object.X"/>, <see cref="Object.Y"/>, <see cref="Object.Width"/>, /// A rectangle object in a map. The existing <see cref="Object.X"/>, <see cref="Object.Y"/>, <see cref="Object.Width"/>,
/// and <see cref="Object.Height"/> properties are used to determine the size of the rectangle. /// and <see cref="Object.Height"/> properties are used to determine the size of the rectangle.
/// </summary> /// </summary>
public class RectangleObject : Object { } public class RectangleObject : Object
{
internal override Object Clone() => new RectangleObject
{
ID = ID,
Name = Name,
Type = Type,
X = X,
Y = Y,
Width = Width,
Height = Height,
Rotation = Rotation,
Visible = Visible,
Template = Template,
Properties = Properties.Select(p => p.Clone()).ToList(),
};
}

View file

@ -1,4 +1,5 @@
using System.Globalization; using System.Globalization;
using System.Linq;
namespace DotTiled; namespace DotTiled;
@ -113,4 +114,32 @@ public class TextObject : Object
/// The text to be displayed. /// The text to be displayed.
/// </summary> /// </summary>
public string Text { get; set; } = ""; public string Text { get; set; } = "";
internal override Object Clone() => new TextObject
{
ID = ID,
Name = Name,
Type = Type,
X = X,
Y = Y,
Width = Width,
Height = Height,
Rotation = Rotation,
Visible = Visible,
Template = Template,
Properties = Properties.Select(p => p.Clone()).ToList(),
FontFamily = FontFamily,
PixelSize = PixelSize,
Wrap = Wrap,
Color = Color,
Bold = Bold,
Italic = Italic,
Underline = Underline,
Strikeout = Strikeout,
Kerning = Kerning,
HorizontalAlignment = HorizontalAlignment,
VerticalAlignment = VerticalAlignment,
Text = Text,
};
} }

View file

@ -1,3 +1,5 @@
using System.Linq;
namespace DotTiled; namespace DotTiled;
/// <summary> /// <summary>
@ -14,4 +16,21 @@ public class TileObject : Object
/// The flipping flags for the tile. /// The flipping flags for the tile.
/// </summary> /// </summary>
public FlippingFlags FlippingFlags { get; set; } public FlippingFlags FlippingFlags { get; set; }
internal override Object Clone() => new TileObject
{
ID = ID,
Name = Name,
Type = Type,
X = X,
Y = Y,
Width = Width,
Height = Height,
Rotation = Rotation,
Visible = Visible,
Template = Template,
Properties = Properties.Select(p => p.Clone()).ToList(),
GID = GID,
FlippingFlags = FlippingFlags,
};
} }

View file

@ -19,8 +19,7 @@ public abstract partial class TmjReaderBase
internal static Chunk ReadChunk(JsonElement element, Optional<DataCompression> compression, DataEncoding encoding) internal static Chunk ReadChunk(JsonElement element, Optional<DataCompression> compression, DataEncoding encoding)
{ {
var data = ReadDataWithoutChunks(element, compression, encoding); var data = element.GetRequiredPropertyCustom<Data>("data", e => ReadDataWithoutChunks(e, compression, encoding));
var x = element.GetRequiredProperty<int>("x"); var x = element.GetRequiredProperty<int>("x");
var y = element.GetRequiredProperty<int>("y"); var y = element.GetRequiredProperty<int>("y");
var width = element.GetRequiredProperty<int>("width"); var width = element.GetRequiredProperty<int>("width");

View file

@ -76,12 +76,13 @@ public abstract partial class TmjReaderBase
List<Vector2> polygonDefault = null; List<Vector2> polygonDefault = null;
List<Vector2> polylineDefault = null; List<Vector2> polylineDefault = null;
List<IProperty> propertiesDefault = []; List<IProperty> propertiesDefault = [];
Optional<uint> gidDefault = Optional.Empty;
var template = element.GetOptionalProperty<string>("template"); var template = element.GetOptionalProperty<string>("template");
if (template.HasValue) if (template.HasValue)
{ {
var resolvedTemplate = _externalTemplateResolver(template.Value); var resolvedTemplate = _externalTemplateResolver(template.Value);
var templObj = resolvedTemplate.Object; var templObj = resolvedTemplate.Object.Clone();
idDefault = templObj.ID; idDefault = templObj.ID;
nameDefault = templObj.Name; nameDefault = templObj.Name;
@ -97,10 +98,11 @@ public abstract partial class TmjReaderBase
pointDefault = templObj is PointObject; pointDefault = templObj is PointObject;
polygonDefault = (templObj is PolygonObject polygonObj) ? polygonObj.Points : null; polygonDefault = (templObj is PolygonObject polygonObj) ? polygonObj.Points : null;
polylineDefault = (templObj is PolylineObject polylineObj) ? polylineObj.Points : null; polylineDefault = (templObj is PolylineObject polylineObj) ? polylineObj.Points : null;
gidDefault = (templObj is TileObject tileObj) ? tileObj.GID : Optional.Empty;
} }
var ellipse = element.GetOptionalProperty<bool>("ellipse").GetValueOr(ellipseDefault); var ellipse = element.GetOptionalProperty<bool>("ellipse").GetValueOr(ellipseDefault);
var gid = element.GetOptionalProperty<uint>("gid"); var gid = element.GetOptionalProperty<uint>("gid").GetValueOrOptional(gidDefault);
var height = element.GetOptionalProperty<float>("height").GetValueOr(heightDefault); var height = element.GetOptionalProperty<float>("height").GetValueOr(heightDefault);
var id = element.GetOptionalProperty<uint>("id").GetValueOrOptional(idDefault); var id = element.GetOptionalProperty<uint>("id").GetValueOrOptional(idDefault);
var name = element.GetOptionalProperty<string>("name").GetValueOr(nameDefault); var name = element.GetOptionalProperty<string>("name").GetValueOr(nameDefault);

View file

@ -74,7 +74,7 @@ public abstract partial class TmxReaderBase
var template = _reader.GetOptionalAttribute("template"); var template = _reader.GetOptionalAttribute("template");
DotTiled.Object obj = null; DotTiled.Object obj = null;
if (template.HasValue) if (template.HasValue)
obj = _externalTemplateResolver(template.Value).Object; obj = _externalTemplateResolver(template.Value).Object.Clone();
uint idDefault = obj?.ID.GetValueOr(0) ?? 0; uint idDefault = obj?.ID.GetValueOr(0) ?? 0;
string nameDefault = obj?.Name ?? ""; string nameDefault = obj?.Name ?? "";