diff --git a/DotTiled.Tests/Serialization/TestData.cs b/DotTiled.Tests/Serialization/TestData.cs index eac8d05..254cba7 100644 --- a/DotTiled.Tests/Serialization/TestData.cs +++ b/DotTiled.Tests/Serialization/TestData.cs @@ -6,9 +6,7 @@ public static partial class TestData { public static XmlReader GetXmlReaderFor(string testDataFile) { - var names = typeof(TestData).Assembly.GetManifestResourceNames(); - - var fullyQualifiedTestDataFile = $"DotTiled.Tests.{testDataFile}"; + var fullyQualifiedTestDataFile = $"DotTiled.Tests.{ConvertPathToAssemblyResourcePath(testDataFile)}"; using var stream = typeof(TestData).Assembly.GetManifestResourceStream(fullyQualifiedTestDataFile) ?? throw new ArgumentException($"Test data file '{fullyQualifiedTestDataFile}' not found"); @@ -20,7 +18,7 @@ public static partial class TestData public static string GetRawStringFor(string testDataFile) { - var fullyQualifiedTestDataFile = $"DotTiled.Tests.{testDataFile}"; + var fullyQualifiedTestDataFile = $"DotTiled.Tests.{ConvertPathToAssemblyResourcePath(testDataFile)}"; using var stream = typeof(TestData).Assembly.GetManifestResourceStream(fullyQualifiedTestDataFile) ?? throw new ArgumentException($"Test data file '{fullyQualifiedTestDataFile}' not found"); @@ -28,12 +26,16 @@ public static partial class TestData return stringReader.ReadToEnd(); } + private static string ConvertPathToAssemblyResourcePath(string path) => + path.Replace("/", ".").Replace("\\", ".").Replace(" ", "_"); + public static IEnumerable MapTests => [ - ["Serialization.TestData.Map.default_map.default-map", TestData.DefaultMap(), Array.Empty()], - ["Serialization.TestData.Map.map_with_common_props.map-with-common-props", TestData.MapWithCommonProps(), Array.Empty()], - ["Serialization.TestData.Map.map_with_custom_type_props.map-with-custom-type-props", TestData.MapWithCustomTypeProps(), TestData.MapWithCustomTypePropsCustomTypeDefinitions()], - ["Serialization.TestData.Map.map_with_embedded_tileset.map-with-embedded-tileset", TestData.MapWithEmbeddedTileset(), Array.Empty()], + ["Serialization/TestData/Map/default_map/default-map", (string f) => TestData.DefaultMap(), Array.Empty()], + ["Serialization/TestData/Map/map_with_common_props/map-with-common-props", (string f) => TestData.MapWithCommonProps(), Array.Empty()], + ["Serialization/TestData/Map/map_with_custom_type_props/map-with-custom-type-props", (string f) => TestData.MapWithCustomTypeProps(), TestData.MapWithCustomTypePropsCustomTypeDefinitions()], + ["Serialization/TestData/Map/map_with_embedded_tileset/map-with-embedded-tileset", (string f) => TestData.MapWithEmbeddedTileset(), Array.Empty()], + ["Serialization/TestData/Map/map_with_external_tileset/map-with-external-tileset", (string f) => TestData.MapWithExternalTileset(f), Array.Empty()], ]; private static CustomTypeDefinition[] typedefs = [ diff --git a/DotTiled.Tests/Serialization/TestData/CustomTypes/large-propertytypes.json b/DotTiled.Tests/Serialization/TestData/CustomTypes/large-propertytypes.json deleted file mode 100644 index e21cf83..0000000 --- a/DotTiled.Tests/Serialization/TestData/CustomTypes/large-propertytypes.json +++ /dev/null @@ -1,103 +0,0 @@ -[ - { - "id": 4, - "name": "Enum0String", - "storageType": "string", - "type": "enum", - "values": [ - "Enum0_1", - "Enum0_2", - "Enum0_3" - ], - "valuesAsFlags": false - }, - { - "id": 5, - "name": "Enum1Num", - "storageType": "int", - "type": "enum", - "values": [ - "Enum1Num_1", - "Enum1Num_2", - "Enum1Num_3", - "Enum1Num_4" - ], - "valuesAsFlags": false - }, - { - "id": 6, - "name": "Enum2StringFlags", - "storageType": "string", - "type": "enum", - "values": [ - "Enum2StringFlags_1", - "Enum2StringFlags_2", - "Enum2StringFlags_3", - "Enum2StringFlags_4" - ], - "valuesAsFlags": true - }, - { - "id": 7, - "name": "Enum3NumFlags", - "storageType": "int", - "type": "enum", - "values": [ - "Enum3NumFlags_1", - "Enum3NumFlags_2", - "Enum3NumFlags_3", - "Enum3NumFlags_4", - "Enum3NumFlags_5" - ], - "valuesAsFlags": true - }, - { - "color": "#ffa0a0a4", - "drawFill": true, - "id": 2, - "members": [ - { - "name": "Yep", - "propertyType": "TestClass", - "type": "class", - "value": { - } - } - ], - "name": "Test", - "type": "class", - "useAs": [ - "property", - "map", - "layer", - "object", - "tile", - "tileset", - "wangcolor", - "wangset", - "project" - ] - }, - { - "color": "#ffa0a0a4", - "drawFill": true, - "id": 1, - "members": [ - { - "name": "Amount", - "type": "float", - "value": 0 - }, - { - "name": "Name", - "type": "string", - "value": "" - } - ], - "name": "TestClass", - "type": "class", - "useAs": [ - "property" - ] - } -] diff --git a/DotTiled.Tests/Serialization/TestData/CustomTypes/map-with-object-template-propertytypes.json b/DotTiled.Tests/Serialization/TestData/CustomTypes/map-with-object-template-propertytypes.json deleted file mode 100644 index 3505dce..0000000 --- a/DotTiled.Tests/Serialization/TestData/CustomTypes/map-with-object-template-propertytypes.json +++ /dev/null @@ -1,24 +0,0 @@ -[ - { - "color": "#ffa0a0a4", - "drawFill": true, - "id": 1, - "members": [ - { - "name": "Amount", - "type": "float", - "value": 0 - }, - { - "name": "Name", - "type": "string", - "value": "" - } - ], - "name": "TestClass", - "type": "class", - "useAs": [ - "property" - ] - } -] diff --git a/DotTiled.Tests/Serialization/TestData/Map/map-with-external-tileset/map-with-external-tileset.cs b/DotTiled.Tests/Serialization/TestData/Map/map-with-external-tileset/map-with-external-tileset.cs new file mode 100644 index 0000000..10c4d67 --- /dev/null +++ b/DotTiled.Tests/Serialization/TestData/Map/map-with-external-tileset/map-with-external-tileset.cs @@ -0,0 +1,79 @@ +using System.Globalization; + +namespace DotTiled.Tests; + +public partial class TestData +{ + public static Map MapWithExternalTileset(string fileExt) => new Map + { + Class = "", + Orientation = MapOrientation.Orthogonal, + Width = 5, + Height = 5, + TileWidth = 32, + TileHeight = 32, + Infinite = false, + HexSideLength = null, + StaggerAxis = null, + StaggerIndex = null, + ParallaxOriginX = 0, + ParallaxOriginY = 0, + RenderOrder = RenderOrder.RightDown, + CompressionLevel = -1, + BackgroundColor = Color.Parse("#00000000", CultureInfo.InvariantCulture), + Version = "1.10", + TiledVersion = "1.11.0", + NextLayerID = 2, + NextObjectID = 1, + Tilesets = [ + new Tileset + { + Version = "1.10", + TiledVersion = "1.11.0", + FirstGID = 1, + Name = "tileset", + TileWidth = 32, + TileHeight = 32, + TileCount = 24, + Columns = 8, + Source = $"tileset.{(fileExt == "tmx" ? "tsx" : "tsj")}", + Image = new Image + { + Format = ImageFormat.Png, + Source = "tileset.png", + Width = 256, + Height = 96, + } + } + ], + Layers = [ + new TileLayer + { + ID = 1, + Name = "Tile Layer 1", + Width = 5, + Height = 5, + Data = new Data + { + Encoding = DataEncoding.Csv, + Chunks = null, + Compression = null, + GlobalTileIDs = [ + 1, 1, 0, 0, 7, + 1, 1, 0, 0, 7, + 0, 0, 1, 0, 7, + 0, 0, 0, 1, 7, + 21, 21, 21, 21, 1 + ], + FlippingFlags = [ + FlippingFlags.None, FlippingFlags.None, FlippingFlags.None, FlippingFlags.None, FlippingFlags.None, + FlippingFlags.None, FlippingFlags.None, FlippingFlags.None, FlippingFlags.None, FlippingFlags.None, + FlippingFlags.None, FlippingFlags.None, FlippingFlags.None, FlippingFlags.None, FlippingFlags.None, + FlippingFlags.None, FlippingFlags.None, FlippingFlags.None, FlippingFlags.None, FlippingFlags.None, + FlippingFlags.None, FlippingFlags.None, FlippingFlags.None, FlippingFlags.None, FlippingFlags.None + ] + } + } + ] + }; +} diff --git a/DotTiled.Tests/Serialization/TestData/Map/map-with-external-tileset/map-with-external-tileset.tmj b/DotTiled.Tests/Serialization/TestData/Map/map-with-external-tileset/map-with-external-tileset.tmj new file mode 100644 index 0000000..89bef93 --- /dev/null +++ b/DotTiled.Tests/Serialization/TestData/Map/map-with-external-tileset/map-with-external-tileset.tmj @@ -0,0 +1,36 @@ +{ "compressionlevel":-1, + "height":5, + "infinite":false, + "layers":[ + { + "data":[1, 1, 0, 0, 7, + 1, 1, 0, 0, 7, + 0, 0, 1, 0, 7, + 0, 0, 0, 1, 7, + 21, 21, 21, 21, 1], + "height":5, + "id":1, + "name":"Tile Layer 1", + "opacity":1, + "type":"tilelayer", + "visible":true, + "width":5, + "x":0, + "y":0 + }], + "nextlayerid":2, + "nextobjectid":1, + "orientation":"orthogonal", + "renderorder":"right-down", + "tiledversion":"1.11.0", + "tileheight":32, + "tilesets":[ + { + "firstgid":1, + "source":"tileset.tsj" + }], + "tilewidth":32, + "type":"map", + "version":"1.10", + "width":5 +} \ No newline at end of file diff --git a/DotTiled.Tests/Serialization/TestData/Map/map-with-external-tileset/map-with-external-tileset.tmx b/DotTiled.Tests/Serialization/TestData/Map/map-with-external-tileset/map-with-external-tileset.tmx new file mode 100644 index 0000000..06114fb --- /dev/null +++ b/DotTiled.Tests/Serialization/TestData/Map/map-with-external-tileset/map-with-external-tileset.tmx @@ -0,0 +1,13 @@ + + + + + +1,1,0,0,7, +1,1,0,0,7, +0,0,1,0,7, +0,0,0,1,7, +21,21,21,21,1 + + + diff --git a/DotTiled.Tests/Serialization/TestData/Map/map-with-external-tileset/tileset.png b/DotTiled.Tests/Serialization/TestData/Map/map-with-external-tileset/tileset.png new file mode 100644 index 0000000..97c1fb3 Binary files /dev/null and b/DotTiled.Tests/Serialization/TestData/Map/map-with-external-tileset/tileset.png differ diff --git a/DotTiled.Tests/Serialization/TestData/Map/map-with-external-tileset/tileset.tsj b/DotTiled.Tests/Serialization/TestData/Map/map-with-external-tileset/tileset.tsj new file mode 100644 index 0000000..820e88f --- /dev/null +++ b/DotTiled.Tests/Serialization/TestData/Map/map-with-external-tileset/tileset.tsj @@ -0,0 +1,14 @@ +{ "columns":8, + "image":"tileset.png", + "imageheight":96, + "imagewidth":256, + "margin":0, + "name":"tileset", + "spacing":0, + "tilecount":24, + "tiledversion":"1.11.0", + "tileheight":32, + "tilewidth":32, + "type":"tileset", + "version":"1.10" +} \ No newline at end of file diff --git a/DotTiled.Tests/Serialization/TestData/Map/map-with-external-tileset/tileset.tsx b/DotTiled.Tests/Serialization/TestData/Map/map-with-external-tileset/tileset.tsx new file mode 100644 index 0000000..d730182 --- /dev/null +++ b/DotTiled.Tests/Serialization/TestData/Map/map-with-external-tileset/tileset.tsx @@ -0,0 +1,4 @@ + + + + diff --git a/DotTiled.Tests/Serialization/Tmj/TmjMapReaderTests.cs b/DotTiled.Tests/Serialization/Tmj/TmjMapReaderTests.cs index 71e5304..670fdf6 100644 --- a/DotTiled.Tests/Serialization/Tmj/TmjMapReaderTests.cs +++ b/DotTiled.Tests/Serialization/Tmj/TmjMapReaderTests.cs @@ -7,21 +7,22 @@ public partial class TmjMapReaderTests [MemberData(nameof(Maps))] public void TmxMapReaderReadMap_ValidTmjExternalTilesetsAndTemplates_ReturnsMapThatEqualsExpected( string testDataFile, - Map expectedMap, + Func expectedMap, IReadOnlyCollection customTypeDefinitions) { // Arrange testDataFile += ".tmj"; + var fileDir = Path.GetDirectoryName(testDataFile); var json = TestData.GetRawStringFor(testDataFile); Template ResolveTemplate(string source) { - var templateJson = TestData.GetRawStringFor($"Serialization.TestData.Template.{source}"); + var templateJson = TestData.GetRawStringFor($"{fileDir}/{source}"); using var templateReader = new TjTemplateReader(templateJson, ResolveTileset, ResolveTemplate, customTypeDefinitions); return templateReader.ReadTemplate(); } Tileset ResolveTileset(string source) { - var tilesetJson = TestData.GetRawStringFor($"Serialization.TestData.Tileset.{source}"); + var tilesetJson = TestData.GetRawStringFor($"{fileDir}/{source}"); using var tilesetReader = new TsjTilesetReader(tilesetJson, ResolveTemplate, customTypeDefinitions); return tilesetReader.ReadTileset(); } @@ -32,6 +33,6 @@ public partial class TmjMapReaderTests // Assert Assert.NotNull(map); - DotTiledAssert.AssertMap(expectedMap, map); + DotTiledAssert.AssertMap(expectedMap("tmj"), map); } } diff --git a/DotTiled.Tests/Serialization/Tmx/TmxMapReaderTests.cs b/DotTiled.Tests/Serialization/Tmx/TmxMapReaderTests.cs index c0dc083..a99ee9a 100644 --- a/DotTiled.Tests/Serialization/Tmx/TmxMapReaderTests.cs +++ b/DotTiled.Tests/Serialization/Tmx/TmxMapReaderTests.cs @@ -9,21 +9,22 @@ public partial class TmxMapReaderTests [MemberData(nameof(Maps))] public void TmxMapReaderReadMap_ValidXmlExternalTilesetsAndTemplates_ReturnsMapThatEqualsExpected( string testDataFile, - Map expectedMap, + Func expectedMap, IReadOnlyCollection customTypeDefinitions) { // Arrange testDataFile += ".tmx"; + var fileDir = Path.GetDirectoryName(testDataFile); using var reader = TestData.GetXmlReaderFor(testDataFile); Template ResolveTemplate(string source) { - using var xmlTemplateReader = TestData.GetXmlReaderFor($"Serialization.TestData.Template.{source}"); + using var xmlTemplateReader = TestData.GetXmlReaderFor($"{fileDir}/{source}"); using var templateReader = new TxTemplateReader(xmlTemplateReader, ResolveTileset, ResolveTemplate, customTypeDefinitions); return templateReader.ReadTemplate(); } Tileset ResolveTileset(string source) { - using var xmlTilesetReader = TestData.GetXmlReaderFor($"Serialization.TestData.Tileset.{source}"); + using var xmlTilesetReader = TestData.GetXmlReaderFor($"{fileDir}/{source}"); using var tilesetReader = new TsxTilesetReader(xmlTilesetReader, ResolveTemplate, customTypeDefinitions); return tilesetReader.ReadTileset(); } @@ -34,6 +35,6 @@ public partial class TmxMapReaderTests // Assert Assert.NotNull(map); - DotTiledAssert.AssertMap(expectedMap, map); + DotTiledAssert.AssertMap(expectedMap("tmx"), map); } } diff --git a/DotTiled/Serialization/Tmx/Tmx.Tileset.cs b/DotTiled/Serialization/Tmx/Tmx.Tileset.cs index 3885dac..9f576bb 100644 --- a/DotTiled/Serialization/Tmx/Tmx.Tileset.cs +++ b/DotTiled/Serialization/Tmx/Tmx.Tileset.cs @@ -83,7 +83,7 @@ internal partial class Tmx var resolvedTileset = externalTilesetResolver(source); resolvedTileset.FirstGID = firstGID; - resolvedTileset.Source = null; + resolvedTileset.Source = source; return resolvedTileset; }