From 3426ac1d30a66ab9a2976930249f1ea923f5db17 Mon Sep 17 00:00:00 2001 From: 7H3LaughingMan <7H3LaughingMan@proton.me> Date: Tue, 22 Apr 2025 11:27:53 -0500 Subject: [PATCH] AOT Notation --- src/DotTiled/DotTiled.csproj | 1 + .../CustomTypes/CustomClassDefinition.cs | 11 +++++++++++ .../CustomTypes/CustomEnumDefinition.cs | 3 +++ src/DotTiled/Properties/IHasProperties.cs | 19 +++++++++++++++++++ .../Tmx/TmxReaderBase.ObjectLayer.cs | 15 ++++++++++----- 5 files changed, 44 insertions(+), 5 deletions(-) diff --git a/src/DotTiled/DotTiled.csproj b/src/DotTiled/DotTiled.csproj index 6e8de2a..819309b 100644 --- a/src/DotTiled/DotTiled.csproj +++ b/src/DotTiled/DotTiled.csproj @@ -3,6 +3,7 @@ net8.0 true + true diff --git a/src/DotTiled/Properties/CustomTypes/CustomClassDefinition.cs b/src/DotTiled/Properties/CustomTypes/CustomClassDefinition.cs index 37b8c6e..8a05b90 100644 --- a/src/DotTiled/Properties/CustomTypes/CustomClassDefinition.cs +++ b/src/DotTiled/Properties/CustomTypes/CustomClassDefinition.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; using System.Linq; using System.Reflection; @@ -104,6 +105,8 @@ public class CustomClassDefinition : HasPropertiesBase, ICustomTypeDefinition /// The type of the class to create a custom class definition from. /// A new instance. /// Thrown when the specified type is not a class. + [RequiresUnreferencedCode("Use manually defined class properties.", Url = "https://dcronqvist.github.io/DotTiled/docs/essentials/custom-properties.html#class-properties")] + [RequiresDynamicCode("Use manually defined class properties.", Url = "https://dcronqvist.github.io/DotTiled/docs/essentials/custom-properties.html#class-properties")] public static CustomClassDefinition FromClass(Type type) { ArgumentNullException.ThrowIfNull(type, nameof(type)); @@ -127,6 +130,8 @@ public class CustomClassDefinition : HasPropertiesBase, ICustomTypeDefinition /// /// The type of the class to create a custom class definition from. /// A new instance. + [RequiresUnreferencedCode("Use manually defined class properties.", Url = "https://dcronqvist.github.io/DotTiled/docs/essentials/custom-properties.html#class-properties")] + [RequiresDynamicCode("Use manually defined class properties.", Url = "https://dcronqvist.github.io/DotTiled/docs/essentials/custom-properties.html#class-properties")] public static CustomClassDefinition FromClass() where T : class, new() => FromClass(() => new T()); /// @@ -135,6 +140,8 @@ public class CustomClassDefinition : HasPropertiesBase, ICustomTypeDefinition /// The type of the class to create a custom class definition from. /// The factory function that creates an instance of the class. /// A new instance. + [RequiresUnreferencedCode("Use manually defined class properties.", Url = "https://dcronqvist.github.io/DotTiled/docs/essentials/custom-properties.html#class-properties")] + [RequiresDynamicCode("Use manually defined class properties.", Url = "https://dcronqvist.github.io/DotTiled/docs/essentials/custom-properties.html#class-properties")] public static CustomClassDefinition FromClass(Func factory) where T : class { var instance = factory(); @@ -149,6 +156,8 @@ public class CustomClassDefinition : HasPropertiesBase, ICustomTypeDefinition }; } + [RequiresUnreferencedCode("Use manually defined class properties.", Url = "https://dcronqvist.github.io/DotTiled/docs/essentials/custom-properties.html#class-properties")] + [RequiresDynamicCode("Use manually defined class properties.", Url = "https://dcronqvist.github.io/DotTiled/docs/essentials/custom-properties.html#class-properties")] private static IProperty ConvertPropertyInfoToIProperty(object instance, PropertyInfo propertyInfo) { switch (propertyInfo.PropertyType) @@ -183,6 +192,8 @@ public class CustomClassDefinition : HasPropertiesBase, ICustomTypeDefinition throw new NotSupportedException($"Type '{propertyInfo.PropertyType.Name}' is not supported in custom classes."); } + [RequiresUnreferencedCode("Use manually defined class properties.", Url = "https://dcronqvist.github.io/DotTiled/docs/essentials/custom-properties.html#class-properties")] + [RequiresDynamicCode("Use manually defined class properties.", Url = "https://dcronqvist.github.io/DotTiled/docs/essentials/custom-properties.html#class-properties")] private static List GetNestedProperties(Type type, object instance) { var defaultInstance = Activator.CreateInstance(type); diff --git a/src/DotTiled/Properties/CustomTypes/CustomEnumDefinition.cs b/src/DotTiled/Properties/CustomTypes/CustomEnumDefinition.cs index b0d6e88..b2cba2e 100644 --- a/src/DotTiled/Properties/CustomTypes/CustomEnumDefinition.cs +++ b/src/DotTiled/Properties/CustomTypes/CustomEnumDefinition.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; using System.Linq; namespace DotTiled; @@ -53,6 +54,7 @@ public class CustomEnumDefinition : ICustomTypeDefinition /// /// The storage type of the custom enum. Defaults to to be consistent with Tiled. /// + [RequiresUnreferencedCode("Use manually defined enum properties.", Url = "https://dcronqvist.github.io/DotTiled/docs/essentials/custom-properties.html#enum-properties")] public static CustomEnumDefinition FromEnum(CustomEnumStorageType storageType = CustomEnumStorageType.String) where T : Enum { var type = typeof(T); @@ -73,6 +75,7 @@ public class CustomEnumDefinition : ICustomTypeDefinition /// The enum type to create a custom enum definition from. /// The storage type of the custom enum. Defaults to to be consistent with Tiled. /// + [RequiresUnreferencedCode("Use manually defined enum properties.", Url = "https://dcronqvist.github.io/DotTiled/docs/essentials/custom-properties.html#enum-properties")] public static CustomEnumDefinition FromEnum(Type type, CustomEnumStorageType storageType = CustomEnumStorageType.String) { if (!type.IsEnum) diff --git a/src/DotTiled/Properties/IHasProperties.cs b/src/DotTiled/Properties/IHasProperties.cs index 6dd1798..668a91a 100644 --- a/src/DotTiled/Properties/IHasProperties.cs +++ b/src/DotTiled/Properties/IHasProperties.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; using System.Linq; namespace DotTiled; @@ -36,6 +37,7 @@ public interface IHasProperties /// /// /// + [RequiresUnreferencedCode("Use 'MapPropertiesTo' with a custom mapper instead.")] T MapPropertiesTo() where T : new(); /// @@ -44,7 +46,16 @@ public interface IHasProperties /// /// /// + [RequiresUnreferencedCode("Use 'MapPropertiesTo' with a custom mapper instead.")] T MapPropertiesTo(Func initializer); + + /// + /// Maps all properties in this object to a new instance of the specified type using the provided mapper. + /// + /// + /// + /// + T MapPropertiesTo(Func, T> mapper); } /// @@ -85,11 +96,17 @@ public abstract class HasPropertiesBase : IHasProperties } /// + [RequiresUnreferencedCode("Use 'MapPropertiesTo' with a custom mapper instead.")] public T MapPropertiesTo() where T : new() => CreateMappedInstance(GetProperties()); /// + [RequiresUnreferencedCode("Use 'MapPropertiesTo' with a custom mapper instead.")] public T MapPropertiesTo(Func initializer) => CreateMappedInstance(GetProperties(), initializer); + /// + public T MapPropertiesTo(Func, T> mapper) => mapper(GetProperties()); + + [RequiresUnreferencedCode("Use 'MapPropertiesTo' with a custom mapper instead.")] private static object CreatedMappedInstance(object instance, IList properties) { var type = instance.GetType(); @@ -141,8 +158,10 @@ public abstract class HasPropertiesBase : IHasProperties return instance; } + [RequiresUnreferencedCode("Use 'MapPropertiesTo' with a custom mapper instead.")] private static T CreateMappedInstance(IList properties) where T : new() => (T)CreatedMappedInstance(Activator.CreateInstance() ?? throw new InvalidOperationException($"Failed to create instance of '{typeof(T).Name}'."), properties); + [RequiresUnreferencedCode("Use 'MapPropertiesTo' with a custom mapper instead.")] private static T CreateMappedInstance(IList properties, Func initializer) => (T)CreatedMappedInstance(initializer(), properties); } diff --git a/src/DotTiled/Serialization/Tmx/TmxReaderBase.ObjectLayer.cs b/src/DotTiled/Serialization/Tmx/TmxReaderBase.ObjectLayer.cs index b2e0c56..9cdd7f1 100644 --- a/src/DotTiled/Serialization/Tmx/TmxReaderBase.ObjectLayer.cs +++ b/src/DotTiled/Serialization/Tmx/TmxReaderBase.ObjectLayer.cs @@ -160,12 +160,17 @@ public abstract partial class TmxReaderBase obj.Properties = Helpers.MergeProperties(obj.Properties, foundObject.Properties).ToList(); obj.Template = foundObject.Template; - if (obj.GetType() != foundObject.GetType()) + return (obj, foundObject) switch { - return obj; - } - - return OverrideObject((dynamic)obj, (dynamic)foundObject); + (TileObject tile, TileObject foundTile) => OverrideObject(tile, foundTile), + (RectangleObject rectangle, RectangleObject foundRectangle) => OverrideObject(rectangle, foundRectangle), + (PolygonObject polygon, PolygonObject foundPolygon) => OverrideObject(polygon, foundPolygon), + (PolylineObject polyline, PolylineObject foundPolyline) => OverrideObject(polyline, foundPolyline), + (EllipseObject ellipse, EllipseObject foundEllipse) => OverrideObject(ellipse, foundEllipse), + (TextObject text, TextObject foundText) => OverrideObject(text, foundText), + (PointObject point, PointObject foundPoint) => OverrideObject(point, foundPoint), + _ => obj + }; } internal EllipseObject ReadEllipseObject()