Skip to content

Commit

Permalink
Added more attribute decoration to fix trimming issues
Browse files Browse the repository at this point in the history
  • Loading branch information
vpenades committed Dec 18, 2023
1 parent af4cc54 commit 29ab865
Show file tree
Hide file tree
Showing 9 changed files with 108 additions and 5 deletions.
37 changes: 37 additions & 0 deletions src/Shared/Guard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Text;

Expand Down Expand Up @@ -266,6 +267,42 @@ public static void MustShareLogicalParent(Schema2.ModelRoot a, string aName, Sch
}

#endregion

#region reflection

public static void HasDynamicallyAccessedMembers(Type t, bool hasConstructors, bool hasMethods, bool hasProperties, bool hasFields, string parameterName)
{
#if NET6_0_OR_GREATER

System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes flags = default;

if (hasConstructors) flags
|= System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.NonPublicConstructors
| System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicConstructors;

if (hasMethods) flags
|= System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicMethods
;//| System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.NonPublicMethods;

if (hasProperties) flags
|= System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicProperties
;// | System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.NonPublicProperties;

if (hasFields) flags
|= System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicFields
;// | System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.NonPublicFields;

var attr = t.GetCustomAttribute<System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembersAttribute>();

if (attr == null || (attr.MemberTypes & flags) != flags)
{
throw new ArgumentException($"{parameterName} {t.Name} must define #if NET6_0_OR_GREATER [DynamicallyAccessedMembers({flags})] #endif");
}

#endif
}

#endregion
}

[DebuggerStepThrough]
Expand Down
5 changes: 2 additions & 3 deletions src/SharpGLTF.Core/Schema2/gltf.ExtensionsFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,9 @@ internal static JsonSerializable Create(JsonSerializable parent, string extensio

var (name, parentType, extType) = _Extensions.FirstOrDefault(item => item.Name == extensionName && item.ParentType.IsAssignableFrom(ptype));

if (name == null) return null;
if (name == null) return null;

// Instance creation on AOT compiled binaries depends on classes defining:
// [System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.NonPublicConstructors | System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicConstructors)]
Guard.HasDynamicallyAccessedMembers(extType, true, false, false, false, nameof(extensionName));

var instance = Activator.CreateInstance
(
Expand Down
8 changes: 6 additions & 2 deletions src/SharpGLTF.Core/Validation/ModelException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,15 @@ private static string _CreateBaseMessage(TARGET target, String message)
{
if (target == null) return message;

var targetTypeInfo = target.GetType().GetTypeInfo();
// TODO: LogicalIndex property should be decorated with DynamicAccess

var targetTypeInfo = target.GetType().GetTypeInfo();

var logicalIndexProp = targetTypeInfo.GetProperty("LogicalIndex");

var logicalIndex = logicalIndexProp != null ? (int)logicalIndexProp.GetValue(target) : -1;
var logicalIndex = logicalIndexProp != null
? (int)logicalIndexProp.GetValue(target)
: -1;

if (logicalIndex >= 0) return $"{targetTypeInfo.Name}[{logicalIndex}] {message}";

Expand Down
4 changes: 4 additions & 0 deletions src/SharpGLTF.Toolkit/Geometry/VertexTypes/VertexEmpty.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Numerics;
using System.Text;
Expand All @@ -12,6 +13,9 @@ namespace SharpGLTF.Geometry.VertexTypes
/// empty <see cref="IVertexMaterial"/> or empty <see cref="IVertexSkinning"/>
/// in a <see cref="VertexBuilder{TvG, TvM, TvS}"/> structure.
/// </summary>
#if NET6_0_OR_GREATER
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.PublicFields)]
#endif
[System.Diagnostics.DebuggerDisplay("Empty")]
public readonly struct VertexEmpty : IVertexMaterial, IVertexSkinning, IEquatable<VertexEmpty>
{
Expand Down
13 changes: 13 additions & 0 deletions src/SharpGLTF.Toolkit/Geometry/VertexTypes/VertexGeometry.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Numerics;
using System.Runtime.Serialization;
using System.Text;
Expand Down Expand Up @@ -86,6 +87,9 @@ public interface IVertexGeometry
/// <summary>
/// Defines a Vertex attribute with a Position.
/// </summary>
#if NET6_0_OR_GREATER
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.PublicFields)]
#endif
[System.Diagnostics.DebuggerDisplay("{_GetDebuggerDisplay(),nq}")]
public struct VertexPosition : IVertexGeometry, IEquatable<VertexPosition>
{
Expand Down Expand Up @@ -183,6 +187,9 @@ public void ApplyTransform(in Matrix4x4 xform)
/// <summary>
/// Defines a Vertex attribute with a Position and a Normal.
/// </summary>
#if NET6_0_OR_GREATER
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.PublicFields)]
#endif
[System.Diagnostics.DebuggerDisplay("{_GetDebuggerDisplay(),nq}")]
public struct VertexPositionNormal : IVertexGeometry, IEquatable<VertexPositionNormal>
{
Expand Down Expand Up @@ -289,6 +296,9 @@ public void ApplyTransform(in Matrix4x4 xform)
/// <summary>
/// Defines a Vertex attribute with a Position, a Normal and a Tangent.
/// </summary>
#if NET6_0_OR_GREATER
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.PublicFields)]
#endif
[System.Diagnostics.DebuggerDisplay("{_GetDebuggerDisplay(),nq}")]
public struct VertexPositionNormalTangent : IVertexGeometry, IEquatable<VertexPositionNormalTangent>
{
Expand Down Expand Up @@ -399,6 +409,9 @@ public void ApplyTransform(in Matrix4x4 xform)
/// <summary>
/// Defines a Vertex attribute with a Position, a Normal and a Tangent.
/// </summary>
#if NET6_0_OR_GREATER
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.PublicFields)]
#endif
[System.Diagnostics.DebuggerDisplay("{_GetDebuggerDisplay(),nq}")]
public struct VertexGeometryDelta : IVertexGeometry, IEquatable<VertexGeometryDelta>
{
Expand Down
28 changes: 28 additions & 0 deletions src/SharpGLTF.Toolkit/Geometry/VertexTypes/VertexMaterial.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Numerics;
using System.Text;

Expand Down Expand Up @@ -85,6 +86,9 @@ public interface IVertexMaterial
/// <summary>
/// Defines a Vertex attribute with a material Color.
/// </summary>
#if NET6_0_OR_GREATER
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.PublicFields)]
#endif
[System.Diagnostics.DebuggerDisplay("{_GetDebuggerDisplay(),nq}")]
public struct VertexColor1 : IVertexMaterial, IEquatable<VertexColor1>
{
Expand Down Expand Up @@ -181,6 +185,9 @@ public readonly Vector2 GetTexCoord(int index)
/// <summary>
/// Defines a Vertex attribute with a two material Colors.
/// </summary>
#if NET6_0_OR_GREATER
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.PublicFields)]
#endif
[System.Diagnostics.DebuggerDisplay("{_GetDebuggerDisplay(),nq}")]
public struct VertexColor2 : IVertexMaterial, IEquatable<VertexColor2>
{
Expand Down Expand Up @@ -284,6 +291,9 @@ public readonly Vector4 GetColor(int index)
/// <summary>
/// Defines a Vertex attribute with a Texture Coordinate.
/// </summary>
#if NET6_0_OR_GREATER
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.PublicFields)]
#endif
[System.Diagnostics.DebuggerDisplay("{_GetDebuggerDisplay(),nq}")]
public struct VertexTexture1 : IVertexMaterial, IEquatable<VertexTexture1>
{
Expand Down Expand Up @@ -379,6 +389,9 @@ public readonly Vector2 GetTexCoord(int index)
/// <summary>
/// Defines a Vertex attribute with two Texture Coordinates.
/// </summary>
#if NET6_0_OR_GREATER
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.PublicFields)]
#endif
[System.Diagnostics.DebuggerDisplay("{_GetDebuggerDisplay(),nq}")]
public struct VertexTexture2 : IVertexMaterial, IEquatable<VertexTexture2>
{
Expand Down Expand Up @@ -485,6 +498,9 @@ public readonly Vector2 GetTexCoord(int index)
/// <summary>
/// Defines a Vertex attribute with a Color material and a Texture Coordinate.
/// </summary>
#if NET6_0_OR_GREATER
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.PublicFields)]
#endif
[System.Diagnostics.DebuggerDisplay("{_GetDebuggerDisplay(),nq}")]
public struct VertexColor1Texture1 : IVertexMaterial, IEquatable<VertexColor1Texture1>
{
Expand Down Expand Up @@ -587,6 +603,9 @@ public readonly Vector2 GetTexCoord(int index)
/// <summary>
/// Defines a Vertex attribute with a material Colors and two Texture Coordinates.
/// </summary>
#if NET6_0_OR_GREATER
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.PublicFields)]
#endif
[System.Diagnostics.DebuggerDisplay("{_GetDebuggerDisplay(),nq}")]
public struct VertexColor1Texture2 : IVertexMaterial, IEquatable<VertexColor1Texture2>
{
Expand Down Expand Up @@ -703,6 +722,9 @@ public readonly Vector2 GetTexCoord(int index)
/// <summary>
/// Defines a Vertex attribute with two material Colors and two Texture Coordinates.
/// </summary>
#if NET6_0_OR_GREATER
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.PublicFields)]
#endif
[System.Diagnostics.DebuggerDisplay("{_GetDebuggerDisplay(),nq}")]
public struct VertexColor2Texture1 : IVertexMaterial, IEquatable<VertexColor2Texture1>
{
Expand Down Expand Up @@ -825,6 +847,9 @@ public readonly Vector2 GetTexCoord(int index)
/// <summary>
/// Defines a Vertex attribute with two material Colors and two Texture Coordinates.
/// </summary>
#if NET6_0_OR_GREATER
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.PublicFields)]
#endif
[System.Diagnostics.DebuggerDisplay("{_GetDebuggerDisplay(),nq}")]
public struct VertexColor2Texture2 : IVertexMaterial, IEquatable<VertexColor2Texture2>
{
Expand Down Expand Up @@ -955,6 +980,9 @@ public readonly Vector2 GetTexCoord(int index)
/// <summary>
/// Defines a Vertex attribute with two material Colors and two Texture Coordinates.
/// </summary>
#if NET6_0_OR_GREATER
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.PublicFields)]
#endif
[System.Diagnostics.DebuggerDisplay("{_GetDebuggerDisplay(),nq}")]
public struct VertexMaterialDelta : IVertexMaterial, IEquatable<VertexMaterialDelta>
{
Expand Down
7 changes: 7 additions & 0 deletions src/SharpGLTF.Toolkit/Geometry/VertexTypes/VertexSkinning.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Numerics;
using System.Text;
using SharpGLTF.Transforms;
Expand Down Expand Up @@ -77,6 +78,9 @@ public interface IVertexSkinning
/// <summary>
/// Defines a Vertex attribute with up to 65535 bone joints and 4 weights.
/// </summary>
#if NET6_0_OR_GREATER
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.PublicFields)]
#endif
[System.Diagnostics.DebuggerDisplay("{_GetDebuggerDisplay(),nq}")]
public struct VertexJoints4 : IVertexSkinning, IEquatable<VertexJoints4>
{
Expand Down Expand Up @@ -205,6 +209,9 @@ public readonly (int Index, float Weight) GetBinding(int index)
/// <summary>
/// Defines a Vertex attribute with up to 65535 bone joints and 8 weights.
/// </summary>
#if NET6_0_OR_GREATER
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.PublicFields)]
#endif
[System.Diagnostics.DebuggerDisplay("{_GetDebuggerDisplay(),nq}")]
public struct VertexJoints8 : IVertexSkinning, IEquatable<VertexJoints8>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,10 @@ public static MemoryAccessInfo[] GetVertexAttributes(this IVertexBuilder firstVe
var tvm = firstVertex.GetMaterial().GetType();
var tvs = firstVertex.GetSkinning().GetType();

Guard.HasDynamicallyAccessedMembers(tvg, false, false, false, true, nameof(firstVertex));
Guard.HasDynamicallyAccessedMembers(tvm, false, false, false, true, nameof(firstVertex));
Guard.HasDynamicallyAccessedMembers(tvs, false, false, false, true, nameof(firstVertex));

var attributes = new List<MemoryAccessInfo>();

foreach (var finfo in tvg.GetFields())
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Numerics;
using System.Text;

namespace SharpGLTF.Geometry.VertexTypes
{
#if NET6_0_OR_GREATER
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields)]
#endif
[System.Diagnostics.DebuggerDisplay("𝐂:{Color} 𝐔𝐕:{TexCoord} {CustomId}")]
public struct VertexColor1Texture1Custom1 : IVertexCustom
{
Expand Down Expand Up @@ -121,6 +125,9 @@ public void SetCustomAttribute(string attributeName, object value)
#endregion
}

#if NET6_0_OR_GREATER
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields)]
#endif
[System.Diagnostics.DebuggerDisplay("{CustomId0} {CustomId1}")]
public struct VertexCustom2 : IVertexCustom
{
Expand Down

0 comments on commit 29ab865

Please sign in to comment.