diff --git a/Engine/src/ECS/Base/TrimmingAttributes.cs b/Engine/src/ECS/Base/TrimmingAttributes.cs index 30c773682..6d325328b 100644 --- a/Engine/src/ECS/Base/TrimmingAttributes.cs +++ b/Engine/src/ECS/Base/TrimmingAttributes.cs @@ -4,6 +4,8 @@ // File is copy of https://github.com/eerhardt/blog-resources/blob/main/creating-aot-compatible-libraries/TrimmingAttributes.cs // As recommended in [How to make libraries compatible with native AOT](https://devblogs.microsoft.com/dotnet/creating-aot-compatible-libraries/#approach-2-define-the-attributes-internally) +// ReSharper disable once EmptyNamespace +// ReSharper disable once CheckNamespace namespace System.Diagnostics.CodeAnalysis { #if !NET7_0_OR_GREATER diff --git a/Engine/src/Tests/ECS/Base/Test_ComponentSchema.cs b/Engine/src/Tests/ECS/Base/Test_ComponentSchema.cs index 7978e8196..ae7bf433f 100644 --- a/Engine/src/Tests/ECS/Base/Test_ComponentSchema.cs +++ b/Engine/src/Tests/ECS/Base/Test_ComponentSchema.cs @@ -48,12 +48,12 @@ public static void Test_ComponentTypes() var components = schema.Components; var scripts = schema.Scripts; - AreEqual("components: 43 scripts: 10 entity tags: 11", schema.ToString()); - AreEqual(44, components.Length); + AreEqual("components: 44 scripts: 10 entity tags: 11", schema.ToString()); + AreEqual(45, components.Length); AreEqual(11, scripts.Length); - AreEqual(49, schema.SchemaTypeByKey.Count); - AreEqual(43, schema.ComponentTypeByType.Count); + AreEqual(50, schema.SchemaTypeByKey.Count); + AreEqual(44, schema.ComponentTypeByType.Count); AreEqual(10, schema.ScriptTypeByType.Count); IsNull(components[0]); @@ -93,6 +93,7 @@ public static void Test_ComponentTypes() AssertBlittableComponent (schema, true); AssertBlittableComponent (schema, true); AssertBlittableComponent (schema, true); + AssertBlittableComponent (schema, true); // --- Test blittable types diff --git a/Engine/src/Tests/ECS/Components.cs b/Engine/src/Tests/ECS/Components.cs index f907ee78c..ece97832d 100644 --- a/Engine/src/Tests/ECS/Components.cs +++ b/Engine/src/Tests/ECS/Components.cs @@ -55,7 +55,7 @@ public struct NonBlittableCycle2 : IComponent { internal CycleClass1 public struct BlittableDatetime : IComponent { public DateTime dateTime; } public struct BlittableGuid : IComponent { public Guid guid; } public struct BlittableBigInteger : IComponent { public BigInteger bigInteger; } -// public struct BlittableUri : IComponent { public Uri uri; } todo requires fix in Fliox.Mapper +public struct BlittableUri : IComponent { public Uri uri; } // throws exception when serialized [ComponentKey(null)] public struct NonSerializedComponent : IComponent { public int value; } @@ -126,6 +126,22 @@ public static class MyEntityExtensions // test missing [StructComponent()] attribute struct MyInvalidComponent : IComponent { public int b; } +// --------------------- component field types ignored by serializer +public struct GenericStruct { public T value; } +public class GenericClass { public T value; } +public interface IGenericInterface { public T Value { get; set; } } + +// [Generic structs in components is not supported] https://github.com/friflo/Friflo.Json.Fliox/issues/45 +// Fixed by commit: [Mapper - Ignore unsupported fields/properties in custom classes, structs and interfaces.] +// https://github.com/friflo/Friflo.Json.Fliox/commit/12c4f88f26d86cffd014f00f823d152eede29d36 +// Remarks: Unsupported fields/properties in custom classes, structs and interfaces are now ignored by mapper/serialization. +// Fix published in: https://www.nuget.org/packages/Friflo.Json.Fliox/1.0.2 +public struct ComponentWithGenerics : IComponent +{ + public GenericStruct genericStruct; + public GenericClass genericClass; + public IGenericInterface genericInterface; +} // ------------------------------------------------ tags [TagName("test-tag")] diff --git a/Engine/src/Tests/ECS/Entity/Test_StructComponent.cs b/Engine/src/Tests/ECS/Entity/Test_StructComponent.cs index 9bd90ebcb..aa64e9f69 100644 --- a/Engine/src/Tests/ECS/Entity/Test_StructComponent.cs +++ b/Engine/src/Tests/ECS/Entity/Test_StructComponent.cs @@ -513,25 +513,6 @@ public static void Test_StructComponent_Archetype_CreateEntity_default_component AreEqual(0, entity.MyComponent1().a); } - [Test] - // [Generic structs in components is not supported] https://github.com/friflo/Friflo.Json.Fliox/issues/45 - // Fixed by commit: [Mapper - Ignore unsupported fields/properties in custom classes, structs and interfaces.] - // https://github.com/friflo/Friflo.Json.Fliox/commit/12c4f88f26d86cffd014f00f823d152eede29d36 - // Remarks: Unsupported fields/properties in custom classes, structs and interfaces are now ignored by mapper/serialization. - // Fix published in: https://www.nuget.org/packages/Friflo.Json.Fliox/1.0.2 - public static void Test_StructComponent_generic_struct() { - _ = new EntityStore(); - } - - public struct GenericStruct - { - public T value; - } - - public struct ComponentWithGenericStruct : IComponent - { - public GenericStruct gs; - } } } \ No newline at end of file diff --git a/Engine/src/Tests/ECS/Serialize/Test_EntitySerializer.cs b/Engine/src/Tests/ECS/Serialize/Test_EntitySerializer.cs index abfe54eb7..ca7b3c0f8 100644 --- a/Engine/src/Tests/ECS/Serialize/Test_EntitySerializer.cs +++ b/Engine/src/Tests/ECS/Serialize/Test_EntitySerializer.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Diagnostics; using System.IO; +using System.Reflection; using System.Text; using System.Threading.Tasks; using Friflo.Engine.ECS; @@ -456,6 +457,42 @@ public static void Test_Serializer_Non_CLS() var nonClsRead = entityRead.GetComponent(); AreEqual(nonClsWrite, nonClsRead); } + + [Test] + public static void Test_Serializer_ignored_field_types() + { + var store = new EntityStore(); + var serializer = new EntitySerializer(); + + var entity = store.CreateEntity(); + entity.AddComponent(new ComponentWithGenerics()); + var json = serializer.WriteEntity(entity); + var expect = +@"{ + ""id"": 1, + ""components"": { + ""ComponentWithGenerics"": {} + } +}"; + AreEqual(expect, json); + } + + [Test] + public static void Test_Serializer_unsupported_field_types() + { + var store = new EntityStore(); + var serializer = new EntitySerializer(); + + var entity = store.CreateEntity(); + entity.AddComponent(new BlittableUri()); + + var e = Throws(() => { + serializer.WriteEntity(entity); + }); + var inner = e!.InnerException as ArgumentException; + NotNull(inner); + AreEqual("Type 'System.Uri' does not have a default constructor (Parameter 'type')", inner.Message); + } } } \ No newline at end of file