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

Support enum properties in CustomClassDefinition.FromClass #61

Merged
merged 2 commits into from
Nov 21, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -177,4 +177,96 @@ public void LoadMap_MapHasClassAndClassIsDefinedAndFieldIsOverridenFromDefault_R
// Assert
DotTiledAssert.AssertMap(expectedMap, result);
}

private enum TestEnum
{
Value1,
Value2
}

private sealed class TestClassWithEnum
{
public TestEnum Enum { get; set; } = TestEnum.Value1;
}

[Fact]
public void LoadMap_MapHasClassWithEnumAndClassIsDefined_ReturnsCorrectMap()
{
// Arrange
var resourceReader = Substitute.For<IResourceReader>();
resourceReader.Read("map.tmx").Returns(
"""
<?xml version="1.0" encoding="UTF-8"?>
<map version="1.10" tiledversion="1.11.0" class="TestClassWithEnum" orientation="orthogonal" renderorder="right-down" width="5" height="5" tilewidth="32" tileheight="32" infinite="0" nextlayerid="2" nextobjectid="1">
<layer id="1" name="Tile Layer 1" width="5" height="5">
<data encoding="csv">
0,0,0,0,0,
0,0,0,0,0,
0,0,0,0,0,
0,0,0,0,0,
0,0,0,0,0
</data>
</layer>
</map>
""");
var classDefinition = CustomClassDefinition.FromClass<TestClassWithEnum>();
var loader = Loader.DefaultWith(
resourceReader: resourceReader,
customTypeDefinitions: [classDefinition]);
var expectedMap = new Map
{
Class = "TestClassWithEnum",
Orientation = MapOrientation.Orthogonal,
Width = 5,
Height = 5,
TileWidth = 32,
TileHeight = 32,
Infinite = false,
ParallaxOriginX = 0,
ParallaxOriginY = 0,
RenderOrder = RenderOrder.RightDown,
CompressionLevel = -1,
BackgroundColor = new Color { R = 0, G = 0, B = 0, A = 0 },
Version = "1.10",
TiledVersion = "1.11.0",
NextLayerID = 2,
NextObjectID = 1,
Layers = [
new TileLayer
{
ID = 1,
Name = "Tile Layer 1",
Width = 5,
Height = 5,
Data = new Data
{
Encoding = DataEncoding.Csv,
GlobalTileIDs = new Optional<uint[]>([
0, 0, 0, 0, 0,
0, 0, 0, 0, 0,
0, 0, 0, 0, 0,
0, 0, 0, 0, 0,
0, 0, 0, 0, 0
]),
FlippingFlags = new Optional<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
])
}
}
],
Properties = [
new EnumProperty { Name = "Enum", PropertyType = "TestEnum", Value = new HashSet<string> { "Value1" } }
]
};

// Act
var result = loader.LoadMap("map.tmx");

// Assert
DotTiledAssert.AssertMap(expectedMap, result);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,42 @@ private sealed class TestClass3WithOverridenNestedClass
]
};

private enum TestEnum1
{
Value1,
Value2
}

[Flags]
private enum TestFlags1
{
Value1 = 0b001,
Value2 = 0b010,
Value3 = 0b100
}

private sealed class TestClass4WithEnums
{
public TestEnum1 Enum { get; set; } = TestEnum1.Value2;
public TestFlags1 Flags { get; set; } = TestFlags1.Value1 | TestFlags1.Value2;
}

private static CustomClassDefinition ExpectedTestClass4WithEnumsDefinition => new CustomClassDefinition
{
Name = "TestClass4WithEnums",
UseAs = CustomClassUseAs.All,
Members = [
new EnumProperty { Name = "Enum", PropertyType = "TestEnum1", Value = new HashSet<string> { "Value2" } },
new EnumProperty { Name = "Flags", PropertyType = "TestFlags1", Value = new HashSet<string> { "Value1", "Value2" } }
]
};

private static IEnumerable<(Type, CustomClassDefinition)> GetCustomClassDefinitionTestData()
{
yield return (typeof(TestClass1), ExpectedTestClass1Definition);
yield return (typeof(TestClass2WithNestedClass), ExpectedTestClass2WithNestedClassDefinition);
yield return (typeof(TestClass3WithOverridenNestedClass), ExpectedTestClass3WithOverridenNestedClassDefinition);
yield return (typeof(TestClass4WithEnums), ExpectedTestClass4WithEnumsDefinition);
}

public static IEnumerable<object[]> CustomClassDefinitionTestData =>
Expand Down
11 changes: 11 additions & 0 deletions src/DotTiled/Properties/CustomTypes/CustomClassDefinition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,17 @@ private static IProperty ConvertPropertyInfoToIProperty(object instance, Propert
return new IntProperty { Name = propertyInfo.Name, Value = (int)propertyInfo.GetValue(instance) };
case Type t when t.IsClass:
return new ClassProperty { Name = propertyInfo.Name, PropertyType = t.Name, Value = GetNestedProperties(propertyInfo.PropertyType, propertyInfo.GetValue(instance)) };
case Type t when t.IsEnum:
var enumDefinition = CustomEnumDefinition.FromEnum(t);

if (!enumDefinition.ValueAsFlags)
return new EnumProperty { Name = propertyInfo.Name, PropertyType = t.Name, Value = new HashSet<string> { propertyInfo.GetValue(instance).ToString() } };

var flags = (Enum)propertyInfo.GetValue(instance);
var enumValues = Enum.GetValues(t).Cast<Enum>();
var enumNames = enumValues.Where(flags.HasFlag).Select(e => e.ToString());

return new EnumProperty { Name = propertyInfo.Name, PropertyType = t.Name, Value = enumNames.ToHashSet() };
default:
break;
}
Expand Down
Loading