Affected parts of model should now inherit properties from their specified class

This commit is contained in:
Daniel Cronqvist 2024-09-05 20:43:12 +02:00
parent ffc529ecb6
commit 99fb910336
8 changed files with 36 additions and 27 deletions

View file

@ -17,7 +17,7 @@ public abstract partial class TmjReaderBase
var offsetY = element.GetOptionalProperty<float>("offsety").GetValueOr(0.0f); var offsetY = element.GetOptionalProperty<float>("offsety").GetValueOr(0.0f);
var parallaxX = element.GetOptionalProperty<float>("parallaxx").GetValueOr(1.0f); var parallaxX = element.GetOptionalProperty<float>("parallaxx").GetValueOr(1.0f);
var parallaxY = element.GetOptionalProperty<float>("parallaxy").GetValueOr(1.0f); var parallaxY = element.GetOptionalProperty<float>("parallaxy").GetValueOr(1.0f);
var properties = element.GetOptionalPropertyCustom("properties", ReadProperties).GetValueOr([]); var properties = ResolveAndMergeProperties(@class, element.GetOptionalPropertyCustom("properties", ReadProperties).GetValueOr([]));
var layers = element.GetOptionalPropertyCustom<List<BaseLayer>>("layers", e => e.GetValueAsList<BaseLayer>(ReadLayer)).GetValueOr([]); var layers = element.GetOptionalPropertyCustom<List<BaseLayer>>("layers", e => e.GetValueAsList<BaseLayer>(ReadLayer)).GetValueOr([]);
return new Group return new Group

View file

@ -16,7 +16,7 @@ public abstract partial class TmjReaderBase
var offsetY = element.GetOptionalProperty<float>("offsety").GetValueOr(0.0f); var offsetY = element.GetOptionalProperty<float>("offsety").GetValueOr(0.0f);
var parallaxX = element.GetOptionalProperty<float>("parallaxx").GetValueOr(1.0f); var parallaxX = element.GetOptionalProperty<float>("parallaxx").GetValueOr(1.0f);
var parallaxY = element.GetOptionalProperty<float>("parallaxy").GetValueOr(1.0f); var parallaxY = element.GetOptionalProperty<float>("parallaxy").GetValueOr(1.0f);
var properties = element.GetOptionalPropertyCustom("properties", ReadProperties).GetValueOr([]); var properties = ResolveAndMergeProperties(@class, element.GetOptionalPropertyCustom("properties", ReadProperties).GetValueOr([]));
var image = element.GetRequiredProperty<string>("image"); var image = element.GetRequiredProperty<string>("image");
var repeatX = element.GetOptionalProperty<bool>("repeatx").GetValueOr(false); var repeatX = element.GetOptionalProperty<bool>("repeatx").GetValueOr(false);

View file

@ -19,7 +19,7 @@ public abstract partial class TmjReaderBase
var offsetY = element.GetOptionalProperty<float>("offsety").GetValueOr(0.0f); var offsetY = element.GetOptionalProperty<float>("offsety").GetValueOr(0.0f);
var parallaxX = element.GetOptionalProperty<float>("parallaxx").GetValueOr(1.0f); var parallaxX = element.GetOptionalProperty<float>("parallaxx").GetValueOr(1.0f);
var parallaxY = element.GetOptionalProperty<float>("parallaxy").GetValueOr(1.0f); var parallaxY = element.GetOptionalProperty<float>("parallaxy").GetValueOr(1.0f);
var properties = element.GetOptionalPropertyCustom("properties", ReadProperties).GetValueOr([]); var properties = ResolveAndMergeProperties(@class, element.GetOptionalPropertyCustom("properties", ReadProperties).GetValueOr([]));
var x = element.GetOptionalProperty<uint>("x").GetValueOr(0); var x = element.GetOptionalProperty<uint>("x").GetValueOr(0);
var y = element.GetOptionalProperty<uint>("y").GetValueOr(0); var y = element.GetOptionalProperty<uint>("y").GetValueOr(0);

View file

@ -30,7 +30,7 @@ public abstract partial class TmjReaderBase
var opacity = element.GetOptionalProperty<float>("opacity").GetValueOr(1.0f); var opacity = element.GetOptionalProperty<float>("opacity").GetValueOr(1.0f);
var parallaxx = element.GetOptionalProperty<float>("parallaxx").GetValueOr(1.0f); var parallaxx = element.GetOptionalProperty<float>("parallaxx").GetValueOr(1.0f);
var parallaxy = element.GetOptionalProperty<float>("parallaxy").GetValueOr(1.0f); var parallaxy = element.GetOptionalProperty<float>("parallaxy").GetValueOr(1.0f);
var properties = element.GetOptionalPropertyCustom("properties", ReadProperties).GetValueOr([]); var properties = ResolveAndMergeProperties(@class, element.GetOptionalPropertyCustom("properties", ReadProperties).GetValueOr([]));
var repeatX = element.GetOptionalProperty<bool>("repeatx").GetValueOr(false); var repeatX = element.GetOptionalProperty<bool>("repeatx").GetValueOr(false);
var repeatY = element.GetOptionalProperty<bool>("repeaty").GetValueOr(false); var repeatY = element.GetOptionalProperty<bool>("repeaty").GetValueOr(false);
var startX = element.GetOptionalProperty<int>("startx").GetValueOr(0); var startX = element.GetOptionalProperty<int>("startx").GetValueOr(0);

View file

@ -40,7 +40,7 @@ public abstract partial class TmjReaderBase
"bottomright" => ObjectAlignment.BottomRight, "bottomright" => ObjectAlignment.BottomRight,
_ => throw new JsonException($"Unknown object alignment '{s}'") _ => throw new JsonException($"Unknown object alignment '{s}'")
}).GetValueOr(ObjectAlignment.Unspecified); }).GetValueOr(ObjectAlignment.Unspecified);
var properties = element.GetOptionalPropertyCustom("properties", ReadProperties).GetValueOr([]); var properties = ResolveAndMergeProperties(@class, element.GetOptionalPropertyCustom("properties", ReadProperties).GetValueOr([]));
var source = element.GetOptionalProperty<string>("source"); var source = element.GetOptionalProperty<string>("source");
var spacing = element.GetOptionalProperty<uint>("spacing"); var spacing = element.GetOptionalProperty<uint>("spacing");
var tileCount = element.GetOptionalProperty<uint>("tilecount"); var tileCount = element.GetOptionalProperty<uint>("tilecount");
@ -166,8 +166,8 @@ public abstract partial class TmjReaderBase
var height = e.GetOptionalProperty<uint>("height").GetValueOr(imageHeight.GetValueOr(0)); var height = e.GetOptionalProperty<uint>("height").GetValueOr(imageHeight.GetValueOr(0));
var objectGroup = e.GetOptionalPropertyCustom<ObjectLayer>("objectgroup", e => ReadObjectLayer(e)); var objectGroup = e.GetOptionalPropertyCustom<ObjectLayer>("objectgroup", e => ReadObjectLayer(e));
var probability = e.GetOptionalProperty<float>("probability").GetValueOr(0.0f); var probability = e.GetOptionalProperty<float>("probability").GetValueOr(0.0f);
var properties = e.GetOptionalPropertyCustom("properties", ReadProperties).GetValueOr([]);
var type = e.GetOptionalProperty<string>("type").GetValueOr(""); var type = e.GetOptionalProperty<string>("type").GetValueOr("");
var properties = ResolveAndMergeProperties(type, e.GetOptionalPropertyCustom("properties", ReadProperties).GetValueOr([]));
Optional<Image> imageModel = image.HasValue ? new Image Optional<Image> imageModel = image.HasValue ? new Image
{ {
@ -207,17 +207,17 @@ public abstract partial class TmjReaderBase
internal Wangset ReadWangset(JsonElement element) internal Wangset ReadWangset(JsonElement element)
{ {
var @clalss = element.GetOptionalProperty<string>("class").GetValueOr(""); var @class = element.GetOptionalProperty<string>("class").GetValueOr("");
var colors = element.GetOptionalPropertyCustom<List<WangColor>>("colors", e => e.GetValueAsList<WangColor>(el => ReadWangColor(el))).GetValueOr([]); var colors = element.GetOptionalPropertyCustom<List<WangColor>>("colors", e => e.GetValueAsList<WangColor>(el => ReadWangColor(el))).GetValueOr([]);
var name = element.GetRequiredProperty<string>("name"); var name = element.GetRequiredProperty<string>("name");
var properties = element.GetOptionalPropertyCustom("properties", ReadProperties).GetValueOr([]); var properties = ResolveAndMergeProperties(@class, element.GetOptionalPropertyCustom("properties", ReadProperties).GetValueOr([]));
var tile = element.GetOptionalProperty<int>("tile").GetValueOr(0); var tile = element.GetOptionalProperty<int>("tile").GetValueOr(0);
var type = element.GetOptionalProperty<string>("type").GetValueOr(""); var type = element.GetOptionalProperty<string>("type").GetValueOr("");
var wangTiles = element.GetOptionalPropertyCustom<List<WangTile>>("wangtiles", e => e.GetValueAsList<WangTile>(ReadWangTile)).GetValueOr([]); var wangTiles = element.GetOptionalPropertyCustom<List<WangTile>>("wangtiles", e => e.GetValueAsList<WangTile>(ReadWangTile)).GetValueOr([]);
return new Wangset return new Wangset
{ {
Class = @clalss, Class = @class,
WangColors = colors, WangColors = colors,
Name = name, Name = name,
Properties = properties, Properties = properties,
@ -232,7 +232,7 @@ public abstract partial class TmjReaderBase
var color = element.GetRequiredPropertyParseable<Color>("color"); var color = element.GetRequiredPropertyParseable<Color>("color");
var name = element.GetRequiredProperty<string>("name"); var name = element.GetRequiredProperty<string>("name");
var probability = element.GetOptionalProperty<float>("probability").GetValueOr(1.0f); var probability = element.GetOptionalProperty<float>("probability").GetValueOr(1.0f);
var properties = element.GetOptionalPropertyCustom("properties", ReadProperties).GetValueOr([]); var properties = ResolveAndMergeProperties(@class, element.GetOptionalPropertyCustom("properties", ReadProperties).GetValueOr([]));
var tile = element.GetOptionalProperty<int>("tile").GetValueOr(0); var tile = element.GetOptionalProperty<int>("tile").GetValueOr(0);
return new WangColor return new WangColor

View file

@ -34,12 +34,13 @@ public abstract partial class TmxReaderBase
}).GetValueOr(DrawOrder.TopDown); }).GetValueOr(DrawOrder.TopDown);
// Elements // Elements
List<IProperty> properties = null; var propertiesCounter = 0;
List<IProperty> properties = Helpers.ResolveClassProperties(@class, _customTypeResolver);
List<DotTiled.Object> objects = []; List<DotTiled.Object> objects = [];
_reader.ProcessChildren("objectgroup", (r, elementName) => elementName switch _reader.ProcessChildren("objectgroup", (r, elementName) => elementName switch
{ {
"properties" => () => Helpers.SetAtMostOnce(ref properties, ReadProperties(), "Properties"), "properties" => () => Helpers.SetAtMostOnceUsingCounter(ref properties, Helpers.MergeProperties(properties, ReadProperties()).ToList(), "Properties", ref propertiesCounter),
"object" => () => objects.Add(ReadObject()), "object" => () => objects.Add(ReadObject()),
_ => r.Skip _ => r.Skip
}); });
@ -101,7 +102,7 @@ public abstract partial class TmxReaderBase
// Elements // Elements
DotTiled.Object foundObject = null; DotTiled.Object foundObject = null;
int propertiesCounter = 0; int propertiesCounter = 0;
List<IProperty> properties = propertiesDefault; List<IProperty> properties = Helpers.ResolveClassProperties(type, _customTypeResolver) ?? propertiesDefault;
_reader.ProcessChildren("object", (r, elementName) => elementName switch _reader.ProcessChildren("object", (r, elementName) => elementName switch
{ {

View file

@ -1,4 +1,5 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
namespace DotTiled.Serialization.Tmx; namespace DotTiled.Serialization.Tmx;
@ -21,13 +22,14 @@ public abstract partial class TmxReaderBase
var parallaxX = _reader.GetOptionalAttributeParseable<float>("parallaxx").GetValueOr(1.0f); var parallaxX = _reader.GetOptionalAttributeParseable<float>("parallaxx").GetValueOr(1.0f);
var parallaxY = _reader.GetOptionalAttributeParseable<float>("parallaxy").GetValueOr(1.0f); var parallaxY = _reader.GetOptionalAttributeParseable<float>("parallaxy").GetValueOr(1.0f);
List<IProperty> properties = null; var propertiesCounter = 0;
List<IProperty> properties = Helpers.ResolveClassProperties(@class, _customTypeResolver);
Data data = null; Data data = null;
_reader.ProcessChildren("layer", (r, elementName) => elementName switch _reader.ProcessChildren("layer", (r, elementName) => elementName switch
{ {
"data" => () => Helpers.SetAtMostOnce(ref data, ReadData(dataUsesChunks), "Data"), "data" => () => Helpers.SetAtMostOnce(ref data, ReadData(dataUsesChunks), "Data"),
"properties" => () => Helpers.SetAtMostOnce(ref properties, ReadProperties(), "Properties"), "properties" => () => Helpers.SetAtMostOnceUsingCounter(ref properties, Helpers.MergeProperties(properties, ReadProperties()).ToList(), "Properties", ref propertiesCounter),
_ => r.Skip _ => r.Skip
}); });
@ -69,13 +71,14 @@ public abstract partial class TmxReaderBase
var repeatX = _reader.GetOptionalAttributeParseable<uint>("repeatx").GetValueOr(0) == 1; var repeatX = _reader.GetOptionalAttributeParseable<uint>("repeatx").GetValueOr(0) == 1;
var repeatY = _reader.GetOptionalAttributeParseable<uint>("repeaty").GetValueOr(0) == 1; var repeatY = _reader.GetOptionalAttributeParseable<uint>("repeaty").GetValueOr(0) == 1;
List<IProperty> properties = null; var propertiesCounter = 0;
List<IProperty> properties = Helpers.ResolveClassProperties(@class, _customTypeResolver);
Image image = null; Image image = null;
_reader.ProcessChildren("imagelayer", (r, elementName) => elementName switch _reader.ProcessChildren("imagelayer", (r, elementName) => elementName switch
{ {
"image" => () => Helpers.SetAtMostOnce(ref image, ReadImage(), "Image"), "image" => () => Helpers.SetAtMostOnce(ref image, ReadImage(), "Image"),
"properties" => () => Helpers.SetAtMostOnce(ref properties, ReadProperties(), "Properties"), "properties" => () => Helpers.SetAtMostOnceUsingCounter(ref properties, Helpers.MergeProperties(properties, ReadProperties()).ToList(), "Properties", ref propertiesCounter),
_ => r.Skip _ => r.Skip
}); });
@ -113,12 +116,13 @@ public abstract partial class TmxReaderBase
var parallaxX = _reader.GetOptionalAttributeParseable<float>("parallaxx").GetValueOr(1f); var parallaxX = _reader.GetOptionalAttributeParseable<float>("parallaxx").GetValueOr(1f);
var parallaxY = _reader.GetOptionalAttributeParseable<float>("parallaxy").GetValueOr(1f); var parallaxY = _reader.GetOptionalAttributeParseable<float>("parallaxy").GetValueOr(1f);
List<IProperty> properties = null; var propertiesCounter = 0;
List<IProperty> properties = Helpers.ResolveClassProperties(@class, _customTypeResolver);
List<BaseLayer> layers = []; List<BaseLayer> layers = [];
_reader.ProcessChildren("group", (r, elementName) => elementName switch _reader.ProcessChildren("group", (r, elementName) => elementName switch
{ {
"properties" => () => Helpers.SetAtMostOnce(ref properties, ReadProperties(), "Properties"), "properties" => () => Helpers.SetAtMostOnceUsingCounter(ref properties, Helpers.MergeProperties(properties, ReadProperties()).ToList(), "Properties", ref propertiesCounter),
"layer" => () => layers.Add(ReadTileLayer(false)), "layer" => () => layers.Add(ReadTileLayer(false)),
"objectgroup" => () => layers.Add(ReadObjectLayer()), "objectgroup" => () => layers.Add(ReadObjectLayer()),
"imagelayer" => () => layers.Add(ReadImageLayer()), "imagelayer" => () => layers.Add(ReadImageLayer()),

View file

@ -68,7 +68,8 @@ public abstract partial class TmxReaderBase
Image image = null; Image image = null;
TileOffset tileOffset = null; TileOffset tileOffset = null;
Grid grid = null; Grid grid = null;
List<IProperty> properties = null; var propertiesCounter = 0;
List<IProperty> properties = Helpers.ResolveClassProperties(@class, _customTypeResolver);
List<Wangset> wangsets = null; List<Wangset> wangsets = null;
Transformations transformations = null; Transformations transformations = null;
List<Tile> tiles = []; List<Tile> tiles = [];
@ -78,7 +79,7 @@ public abstract partial class TmxReaderBase
"image" => () => Helpers.SetAtMostOnce(ref image, ReadImage(), "Image"), "image" => () => Helpers.SetAtMostOnce(ref image, ReadImage(), "Image"),
"tileoffset" => () => Helpers.SetAtMostOnce(ref tileOffset, ReadTileOffset(), "TileOffset"), "tileoffset" => () => Helpers.SetAtMostOnce(ref tileOffset, ReadTileOffset(), "TileOffset"),
"grid" => () => Helpers.SetAtMostOnce(ref grid, ReadGrid(), "Grid"), "grid" => () => Helpers.SetAtMostOnce(ref grid, ReadGrid(), "Grid"),
"properties" => () => Helpers.SetAtMostOnce(ref properties, ReadProperties(), "Properties"), "properties" => () => Helpers.SetAtMostOnceUsingCounter(ref properties, Helpers.MergeProperties(properties, ReadProperties()).ToList(), "Properties", ref propertiesCounter),
"wangsets" => () => Helpers.SetAtMostOnce(ref wangsets, ReadWangsets(), "Wangsets"), "wangsets" => () => Helpers.SetAtMostOnce(ref wangsets, ReadWangsets(), "Wangsets"),
"transformations" => () => Helpers.SetAtMostOnce(ref transformations, ReadTransformations(), "Transformations"), "transformations" => () => Helpers.SetAtMostOnce(ref transformations, ReadTransformations(), "Transformations"),
"tile" => () => tiles.Add(ReadTile()), "tile" => () => tiles.Add(ReadTile()),
@ -197,14 +198,15 @@ public abstract partial class TmxReaderBase
var height = _reader.GetOptionalAttributeParseable<uint>("height"); var height = _reader.GetOptionalAttributeParseable<uint>("height");
// Elements // Elements
List<IProperty> properties = null; var propertiesCounter = 0;
List<IProperty> properties = Helpers.ResolveClassProperties(type, _customTypeResolver);
Image image = null; Image image = null;
ObjectLayer objectLayer = null; ObjectLayer objectLayer = null;
List<Frame> animation = null; List<Frame> animation = null;
_reader.ProcessChildren("tile", (r, elementName) => elementName switch _reader.ProcessChildren("tile", (r, elementName) => elementName switch
{ {
"properties" => () => Helpers.SetAtMostOnce(ref properties, ReadProperties(), "Properties"), "properties" => () => Helpers.SetAtMostOnceUsingCounter(ref properties, Helpers.MergeProperties(properties, ReadProperties()).ToList(), "Properties", ref propertiesCounter),
"image" => () => Helpers.SetAtMostOnce(ref image, ReadImage(), "Image"), "image" => () => Helpers.SetAtMostOnce(ref image, ReadImage(), "Image"),
"objectgroup" => () => Helpers.SetAtMostOnce(ref objectLayer, ReadObjectLayer(), "ObjectLayer"), "objectgroup" => () => Helpers.SetAtMostOnce(ref objectLayer, ReadObjectLayer(), "ObjectLayer"),
"animation" => () => Helpers.SetAtMostOnce(ref animation, r.ReadList<Frame>("animation", "frame", (ar) => "animation" => () => Helpers.SetAtMostOnce(ref animation, r.ReadList<Frame>("animation", "frame", (ar) =>
@ -243,13 +245,14 @@ public abstract partial class TmxReaderBase
var tile = _reader.GetRequiredAttributeParseable<int>("tile"); var tile = _reader.GetRequiredAttributeParseable<int>("tile");
// Elements // Elements
List<IProperty> properties = null; var propertiesCounter = 0;
List<IProperty> properties = Helpers.ResolveClassProperties(@class, _customTypeResolver);
List<WangColor> wangColors = []; List<WangColor> wangColors = [];
List<WangTile> wangTiles = []; List<WangTile> wangTiles = [];
_reader.ProcessChildren("wangset", (r, elementName) => elementName switch _reader.ProcessChildren("wangset", (r, elementName) => elementName switch
{ {
"properties" => () => Helpers.SetAtMostOnce(ref properties, ReadProperties(), "Properties"), "properties" => () => Helpers.SetAtMostOnceUsingCounter(ref properties, Helpers.MergeProperties(properties, ReadProperties()).ToList(), "Properties", ref propertiesCounter),
"wangcolor" => () => wangColors.Add(ReadWangColor()), "wangcolor" => () => wangColors.Add(ReadWangColor()),
"wangtile" => () => wangTiles.Add(ReadWangTile()), "wangtile" => () => wangTiles.Add(ReadWangTile()),
_ => r.Skip _ => r.Skip
@ -279,11 +282,12 @@ public abstract partial class TmxReaderBase
var probability = _reader.GetOptionalAttributeParseable<float>("probability").GetValueOr(0f); var probability = _reader.GetOptionalAttributeParseable<float>("probability").GetValueOr(0f);
// Elements // Elements
List<IProperty> properties = null; var propertiesCounter = 0;
List<IProperty> properties = Helpers.ResolveClassProperties(@class, _customTypeResolver);
_reader.ProcessChildren("wangcolor", (r, elementName) => elementName switch _reader.ProcessChildren("wangcolor", (r, elementName) => elementName switch
{ {
"properties" => () => Helpers.SetAtMostOnce(ref properties, ReadProperties(), "Properties"), "properties" => () => Helpers.SetAtMostOnceUsingCounter(ref properties, Helpers.MergeProperties(properties, ReadProperties()).ToList(), "Properties", ref propertiesCounter),
_ => r.Skip _ => r.Skip
}); });