Browse Source

GetLogicalChildren() can use reflection now, so no need for each class to require an override anymore.

vpenades 8 months ago
parent
commit
9233c805e0

+ 1 - 6
src/SharpGLTF.Core/Schema2/gltf.AccessorSparse.cs

@@ -20,12 +20,7 @@ namespace SharpGLTF.Schema2
         #endregion
 
         #region API
-
-        protected override IEnumerable<ExtraProperties> GetLogicalChildren()
-        {
-            return base.GetLogicalChildren().ConcatElements(_indices, _values);
-        }
-
+        
         internal AccessorSparse(BufferView indices, int indicesOffset, IndexEncodingType indicesEncoding, BufferView values, int valuesOffset, int count)
         {
             Guard.NotNull(indices, nameof(indices));

+ 0 - 5
src/SharpGLTF.Core/Schema2/gltf.Accessors.cs

@@ -144,11 +144,6 @@ namespace SharpGLTF.Schema2
                 : this._sparse._CreateMemoryAccessors(this);
         }
 
-        protected override IEnumerable<ExtraProperties> GetLogicalChildren()
-        {
-            return base.GetLogicalChildren().ConcatElements(_sparse);
-        }
-
         public void UpdateBounds()
         {
             this._min.Clear();

+ 1 - 10
src/SharpGLTF.Core/Schema2/gltf.AnimationChannel.cs

@@ -48,16 +48,7 @@ namespace SharpGLTF.Schema2
             LogicalParent = parent;
             LogicalIndex = index;
         }
-
-        protected override IEnumerable<ExtraProperties> GetLogicalChildren()
-        {
-            var children = base.GetLogicalChildren();
-
-            if (_target != null) children = children.Append(_target);
-
-            return children;
-        }
-
+        
         #endregion
 
         #region properties

+ 1 - 6
src/SharpGLTF.Core/Schema2/gltf.Animations.cs

@@ -24,12 +24,7 @@ namespace SharpGLTF.Schema2
             _channels = new ChildrenList<AnimationChannel, Animation>(this);
             _samplers = new ChildrenList<AnimationSampler, Animation>(this);
         }
-
-        protected override IEnumerable<ExtraProperties> GetLogicalChildren()
-        {
-            return base.GetLogicalChildren().Concat(_samplers).Concat(_channels);
-        }
-
+        
         #endregion
 
         #region properties

+ 1 - 6
src/SharpGLTF.Core/Schema2/gltf.Camera.cs

@@ -24,12 +24,7 @@ namespace SharpGLTF.Schema2
         #endregion
 
         #region API
-
-        protected override IEnumerable<ExtraProperties> GetLogicalChildren()
-        {
-            return base.GetLogicalChildren().ConcatElements(_orthographic, _perspective);
-        }
-
+        
         internal ICamera GetCamera()
         {
             if (this._orthographic != null && this._perspective != null)

+ 52 - 33
src/SharpGLTF.Core/Schema2/gltf.ExtraProperties.cs

@@ -8,8 +8,7 @@ using System.Text.Json;
 using SharpGLTF.IO;
 using SharpGLTF.Reflection;
 
-using JsonToken = System.Text.Json.JsonTokenType;
-
+using JSONTOKEN = System.Text.Json.JsonTokenType;
 using JSONEXTRAS = System.Text.Json.Nodes.JsonNode;
 
 namespace SharpGLTF.Schema2
@@ -30,7 +29,7 @@ namespace SharpGLTF.Schema2
     public abstract class ExtraProperties
         : JsonSerializable
         , IExtraProperties
-        , Reflection.IReflectionObject
+        , IReflectionObject
     {
         #region data
 
@@ -102,6 +101,53 @@ namespace SharpGLTF.Schema2
             }
         }
 
+        /// <summary>
+        /// Gets a collection of <see cref="ExtraProperties"/> instances stored by this object.
+        /// </summary>
+        /// <returns>A collection of <see cref="ExtraProperties"/> instances.</returns>
+        /// <remarks>
+        /// This is used to traverse the whole glTF document tree and gather all the objects<br/>
+        /// So we can identify which extensions are used anywhere in the document.
+        /// </remarks>
+        protected IEnumerable<ExtraProperties> GetLogicalChildren()
+        {
+            foreach (var ext in _extensions.OfType<ExtraProperties>())
+            {
+                yield return ext;
+            }
+
+            if (!(this is IReflectionObject robj)) yield break;
+
+            foreach (var field in robj.GetFields())
+            {
+                var value = field.Value;
+
+                if (value is IReflectionArray array)
+                {
+                    for (int i = 0; i < array.Count; ++i)
+                    {
+                        var item = array.GetField(i);
+                        if (item.Value is ExtraProperties itemExtra) yield return itemExtra;
+                    }
+                }
+                else if (value is ExtraProperties extra) yield return extra;
+            }
+        }
+
+        protected static IEnumerable<ExtraProperties> Flatten(ExtraProperties container)
+        {
+            if (container == null) yield break;
+
+            yield return container;
+
+            foreach (var c in container.GetLogicalChildren())
+            {
+                var cc = Flatten(c);
+
+                foreach (var ccc in cc) yield return ccc;
+            }
+        }
+
         #endregion
 
         #region API        
@@ -164,34 +210,7 @@ namespace SharpGLTF.Schema2
             where T : JsonSerializable
         {
             _extensions.RemoveAll(item => item.GetType() == typeof(T));
-        }
-
-        /// <summary>
-        /// Gets a collection of <see cref="ExtraProperties"/> instances stored by this object.
-        /// </summary>
-        /// <returns>A collection of <see cref="ExtraProperties"/> instances.</returns>
-        /// <remarks>
-        /// This is used to traverse the whole glTF document tree and gather all the objects<br/>
-        /// So we can identify which extensions are used anywhere in the document.
-        /// </remarks>
-        protected virtual IEnumerable<ExtraProperties> GetLogicalChildren()
-        {
-            return _extensions.OfType<ExtraProperties>();
-        }
-
-        protected static IEnumerable<ExtraProperties> Flatten(ExtraProperties container)
-        {
-            if (container == null) yield break;
-
-            yield return container;
-
-            foreach (var c in container.GetLogicalChildren())
-            {
-                var cc = Flatten(c);
-
-                foreach (var ccc in cc) yield return ccc;
-            }
-        }
+        }        
 
         #endregion
 
@@ -297,9 +316,9 @@ namespace SharpGLTF.Schema2
         {
             reader.Read();
 
-            if (reader.TokenType == JsonToken.StartObject)
+            if (reader.TokenType == JSONTOKEN.StartObject)
             {
-                while (reader.Read() && reader.TokenType != JsonToken.EndObject)
+                while (reader.Read() && reader.TokenType != JSONTOKEN.EndObject)
                 {
                     var key = reader.GetString();
 

+ 1 - 6
src/SharpGLTF.Core/Schema2/gltf.Material.cs

@@ -92,12 +92,7 @@ namespace SharpGLTF.Schema2
         #endregion
 
         #region API
-
-        protected override IEnumerable<ExtraProperties> GetLogicalChildren()
-        {
-            return base.GetLogicalChildren().ConcatElements(_normalTexture, _emissiveTexture, _occlusionTexture, _pbrMetallicRoughness);
-        }
-
+        
         /// <summary>
         /// Finds an instance of <see cref="MaterialChannel"/>
         /// </summary>

+ 10 - 60
src/SharpGLTF.Core/Schema2/gltf.MaterialsFactory.cs

@@ -206,12 +206,7 @@ namespace SharpGLTF.Schema2
         #endregion
 
         #region API
-
-        protected override IEnumerable<ExtraProperties> GetLogicalChildren()
-        {
-            return base.GetLogicalChildren().ConcatElements(_baseColorTexture, _metallicRoughnessTexture);
-        }
-
+        
         protected override void OnValidateContent(ValidationContext validate)
         {
             base.OnValidateContent(validate);
@@ -253,12 +248,7 @@ namespace SharpGLTF.Schema2
         #pragma warning disable CA1801 // Review unused parameters
         internal MaterialPBRSpecularGlossiness(Material material) { }
         #pragma warning restore CA1801 // Review unused parameters
-
-        protected override IEnumerable<ExtraProperties> GetLogicalChildren()
-        {
-            return base.GetLogicalChildren().ConcatElements(_diffuseTexture, _specularGlossinessTexture);
-        }
-
+        
         protected override void OnValidateContent(ValidationContext validate)
         {
             base.OnValidateContent(validate);
@@ -319,12 +309,7 @@ namespace SharpGLTF.Schema2
         #pragma warning disable CA1801 // Review unused parameters
         internal MaterialClearCoat(Material material) { }
         #pragma warning restore CA1801 // Review unused parameters
-
-        protected override IEnumerable<ExtraProperties> GetLogicalChildren()
-        {
-            return base.GetLogicalChildren().ConcatElements(_clearcoatTexture, _clearcoatRoughnessTexture, _clearcoatNormalTexture);
-        }
-
+        
         protected override void OnValidateContent(ValidationContext validate)
         {
             base.OnValidateContent(validate);
@@ -393,12 +378,7 @@ namespace SharpGLTF.Schema2
         #pragma warning disable CA1801 // Review unused parameters
         internal MaterialTransmission(Material material) { }
         #pragma warning restore CA1801 // Review unused parameters
-
-        protected override IEnumerable<ExtraProperties> GetLogicalChildren()
-        {
-            return base.GetLogicalChildren().ConcatElements(_transmissionTexture);
-        }
-
+        
         protected override void OnValidateContent(ValidationContext validate)
         {
             base.OnValidateContent(validate);
@@ -429,12 +409,7 @@ namespace SharpGLTF.Schema2
         #pragma warning disable CA1801 // Review unused parameters
         internal MaterialSheen(Material material) { }
         #pragma warning restore CA1801 // Review unused parameters
-
-        protected override IEnumerable<ExtraProperties> GetLogicalChildren()
-        {
-            return base.GetLogicalChildren().ConcatElements(_sheenColorTexture, _sheenRoughnessTexture);
-        }
-
+        
         protected override void OnValidateContent(ValidationContext validate)
         {
             base.OnValidateContent(validate);
@@ -507,12 +482,7 @@ namespace SharpGLTF.Schema2
         #pragma warning disable CA1801 // Review unused parameters
         internal MaterialSpecular(Material material) { }
         #pragma warning restore CA1801 // Review unused parameters
-
-        protected override IEnumerable<ExtraProperties> GetLogicalChildren()
-        {
-            return base.GetLogicalChildren().ConcatElements(_specularColorTexture, _specularTexture);
-        }
-
+        
         protected override void OnValidateContent(ValidationContext validate)
         {
             base.OnValidateContent(validate);
@@ -559,12 +529,7 @@ namespace SharpGLTF.Schema2
         #pragma warning disable CA1801 // Review unused parameters
         internal MaterialVolume(Material material) { }
         #pragma warning restore CA1801 // Review unused parameters
-
-        protected override IEnumerable<ExtraProperties> GetLogicalChildren()
-        {
-            return base.GetLogicalChildren().ConcatElements(_thicknessTexture);
-        }
-
+        
         protected override void OnValidateContent(ValidationContext validate)
         {
             base.OnValidateContent(validate);
@@ -657,12 +622,7 @@ namespace SharpGLTF.Schema2
         #pragma warning disable CA1801 // Review unused parameters
         internal MaterialIridescence(Material material) { }
         #pragma warning restore CA1801 // Review unused parameters
-
-        protected override IEnumerable<ExtraProperties> GetLogicalChildren()
-        {
-            return base.GetLogicalChildren().ConcatElements(_iridescenceTexture, _iridescenceThicknessTexture);
-        }
-
+        
         protected override void OnValidateContent(ValidationContext validate)
         {
             base.OnValidateContent(validate);            
@@ -727,12 +687,7 @@ namespace SharpGLTF.Schema2
         #pragma warning disable CA1801 // Review unused parameters
         internal MaterialAnisotropy(Material material) { }
         #pragma warning restore CA1801 // Review unused parameters
-
-        protected override IEnumerable<ExtraProperties> GetLogicalChildren()
-        {
-            return base.GetLogicalChildren().ConcatElements(_anisotropyTexture);
-        }
-
+        
         protected override void OnValidateContent(ValidationContext validate)
         {
             base.OnValidateContent(validate);
@@ -769,12 +724,7 @@ namespace SharpGLTF.Schema2
         #pragma warning disable CA1801 // Review unused parameters
         internal MaterialDiffuseTransmission(Material material) { }
         #pragma warning restore CA1801 // Review unused parameters
-
-        protected override IEnumerable<ExtraProperties> GetLogicalChildren()
-        {
-            return base.GetLogicalChildren().ConcatElements(_diffuseTransmissionTexture, _diffuseTransmissionColorTexture);
-        }
-
+        
         protected override void OnValidateContent(ValidationContext validate)
         {
             base.OnValidateContent(validate);

+ 1 - 6
src/SharpGLTF.Core/Schema2/gltf.Mesh.cs

@@ -67,12 +67,7 @@ namespace SharpGLTF.Schema2
 
             _weights.SetMorphWeights(count, weights);
         }
-
-        protected override IEnumerable<ExtraProperties> GetLogicalChildren()
-        {
-            return base.GetLogicalChildren().Concat(_primitives);
-        }
-
+        
         /// <summary>
         /// Creates a new <see cref="MeshPrimitive"/> instance
         /// and adds it to the current <see cref="Mesh"/>.

+ 1 - 14
src/SharpGLTF.Core/Schema2/gltf.PunctualLight.cs

@@ -151,20 +151,7 @@ namespace SharpGLTF.Schema2
             }
         }
 
-        #endregion
-
-        #region API
-
-        protected override IEnumerable<ExtraProperties> GetLogicalChildren()
-        {
-            var children = base.GetLogicalChildren();
-
-            if (_spot != null) children = children.Concat(new[] { _spot });
-
-            return children;
-        }
-
-        #endregion
+        #endregion        
 
         #region Validation
 

+ 0 - 5
src/SharpGLTF.Core/Schema2/gltf.Root.PunctualLights.cs

@@ -14,11 +14,6 @@ namespace SharpGLTF.Schema2
             _lights = new ChildrenList<PunctualLight, ModelRoot>(root);
         }
 
-        protected override IEnumerable<ExtraProperties> GetLogicalChildren()
-        {
-            return base.GetLogicalChildren().Concat(_lights);
-        }
-
         public IReadOnlyList<PunctualLight> Lights => _lights;
 
         public PunctualLight CreateLight(string name, PunctualLightType ltype)

+ 1 - 23
src/SharpGLTF.Core/Schema2/gltf.Root.cs

@@ -129,29 +129,7 @@ namespace SharpGLTF.Schema2
         public IReadOnlyList<Node>              LogicalNodes            => _nodes;
         public IReadOnlyList<Scene>             LogicalScenes           => _scenes;
         public IReadOnlyList<Animation>         LogicalAnimations       => _animations;
-
-        protected override IEnumerable<ExtraProperties> GetLogicalChildren()
-        {
-            var containers = base.GetLogicalChildren();
-
-            containers = containers.ConcatElements(this.Asset);
-            containers = containers.Concat(this.LogicalAccessors);
-            containers = containers.Concat(this.LogicalAnimations);
-            containers = containers.Concat(this.LogicalBuffers);
-            containers = containers.Concat(this.LogicalBufferViews);
-            containers = containers.Concat(this.LogicalCameras);
-            containers = containers.Concat(this.LogicalImages);
-            containers = containers.Concat(this.LogicalMaterials);
-            containers = containers.Concat(this.LogicalMeshes);
-            containers = containers.Concat(this.LogicalNodes);
-            containers = containers.Concat(this.LogicalTextureSamplers);
-            containers = containers.Concat(this.LogicalScenes);
-            containers = containers.Concat(this.LogicalSkins);
-            containers = containers.Concat(this.LogicalTextures);
-
-            return containers;
-        }
-
+        
         internal IEnumerable<ExtraProperties> GetLogicalChildrenFlattened()
         {
             return GetLogicalChildren().SelectMany(item => Flatten(item));

+ 1 - 10
src/SharpGLTF.Ext.3DTiles/Schema2/Ext.FeatureID.cs

@@ -90,16 +90,7 @@ namespace SharpGLTF.Schema2.Tiles3D
         #region lifecycle
 
         internal MeshExtMeshFeatureID() { }
-
-        protected override IEnumerable<ExtraProperties> GetLogicalChildren()
-        {
-            var items = base.GetLogicalChildren();
-
-            if (_texture != null) items = items.Append(_texture);
-
-            return items;
-        }
-
+        
         #endregion
 
         #region child properties

+ 2 - 12
src/SharpGLTF.Ext.3DTiles/Schema2/Ext.Features.cs

@@ -112,12 +112,7 @@ namespace SharpGLTF.Schema2
                 _node = node;
                 _featureIds = new ChildrenList<MeshExtInstanceFeatureID, MeshExtInstanceFeatures>(this);
             }
-
-            protected override IEnumerable<ExtraProperties> GetLogicalChildren()
-            {
-                return base.GetLogicalChildren().Concat(_featureIds);
-            }
-
+            
             #endregion
 
             #region data
@@ -211,12 +206,7 @@ namespace SharpGLTF.Schema2
                 _meshPrimitive = meshPrimitive;
                 _featureIds = new ChildrenList<MeshExtMeshFeatureID, MeshExtMeshFeatures>(this);
             }
-
-            protected override IEnumerable<ExtraProperties> GetLogicalChildren()
-            {
-                return base.GetLogicalChildren().Concat(_featureIds);
-            }
-
+            
             #endregion
 
             #region data

+ 6 - 49
src/SharpGLTF.Ext.3DTiles/Schema2/Ext.StructuralMetadataRoot.cs

@@ -39,19 +39,7 @@ namespace SharpGLTF.Schema2
                 _propertyAttributes = new ChildrenList<PropertyAttribute, EXTStructuralMetadataRoot>(this);
                 _propertyTextures = new ChildrenList<PropertyTexture, EXTStructuralMetadataRoot>(this);
             }
-
-            protected override IEnumerable<ExtraProperties> GetLogicalChildren()
-            {
-                var items = base.GetLogicalChildren()
-                    .Concat(_propertyTables)
-                    .Concat(_propertyAttributes)
-                    .Concat(_propertyTextures);
-
-                if (Schema != null) items = items.Append(Schema);
-
-                return items;
-            }
-
+            
             #endregion
 
             #region data
@@ -300,13 +288,7 @@ namespace SharpGLTF.Schema2
             {
                 _properties = new ChildrenDictionary<PropertyTextureProperty, PropertyTexture>(this);
             }
-
-            protected override IEnumerable<ExtraProperties> GetLogicalChildren()
-            {
-                return base.GetLogicalChildren()
-                    .Concat(_properties.Values);
-            }
-
+            
             #endregion
 
             #region child properties
@@ -444,13 +426,7 @@ namespace SharpGLTF.Schema2
             {
                 _properties = new ChildrenDictionary<PropertyAttributeProperty, PropertyAttribute>(this);
             }
-
-            protected override IEnumerable<ExtraProperties> GetLogicalChildren()
-            {
-                return base.GetLogicalChildren()
-                    .Concat(_properties.Values);
-            }
-
+            
             #endregion
 
             #region child properties
@@ -587,13 +563,7 @@ namespace SharpGLTF.Schema2
 
                 _count = _countMinimum;
             }
-
-            protected override IEnumerable<ExtraProperties> GetLogicalChildren()
-            {
-                return base.GetLogicalChildren()
-                    .Concat(_properties.Values);
-            }
-
+            
             #endregion
 
             #region child properties
@@ -928,14 +898,7 @@ namespace SharpGLTF.Schema2
                 _classes = new ChildrenDictionary<StructuralMetadataClass, StructuralMetadataSchema>(this);
                 _enums = new ChildrenDictionary<StructuralMetadataEnum, StructuralMetadataSchema>(this);
             }
-
-            protected override IEnumerable<ExtraProperties> GetLogicalChildren()
-            {
-                return base.GetLogicalChildren()
-                    .Concat(_classes.Values)
-                    .Concat(_enums.Values);
-            }
-
+            
             #endregion
 
             #region child properties           
@@ -1134,13 +1097,7 @@ namespace SharpGLTF.Schema2
             {
                 _properties = new ChildrenDictionary<StructuralMetadataClassProperty, StructuralMetadataClass>(this);
             }
-
-            protected override IEnumerable<ExtraProperties> GetLogicalChildren()
-            {
-                return base.GetLogicalChildren()
-                    .Concat(_properties.Values);
-            }
-
+            
             #endregion
 
             #region child properties

+ 2 - 12
src/SharpGLTF.Ext.Agi/Schema2/agi.Articulations.cs

@@ -13,12 +13,7 @@ namespace SharpGLTF.Schema2.AGI
         {
             _articulations = new ChildrenList<AgiArticulation, AgiRootArticulations>(this);
         }
-
-        protected override IEnumerable<ExtraProperties> GetLogicalChildren()
-        {
-            return base.GetLogicalChildren().Concat(_articulations);
-        }
-
+        
         public IReadOnlyList<AgiArticulation> Articulations => _articulations;
 
         public AgiArticulation CreateArticulation(string name)
@@ -65,12 +60,7 @@ namespace SharpGLTF.Schema2.AGI
         {
             _stages = new ChildrenList<AgiArticulationStage, AgiArticulation>(this);
         }
-
-        protected override IEnumerable<ExtraProperties> GetLogicalChildren()
-        {
-            return base.GetLogicalChildren().Concat(_stages);
-        }
-
+        
         public IReadOnlyList<AgiArticulationStage> Stages => _stages;
 
         public AgiArticulationStage CreateArticulationStage(string name, AgiArticulationTransformType transformType)

+ 1 - 6
src/SharpGLTF.Ext.Agi/Schema2/agi.StkMetadata.cs

@@ -11,12 +11,7 @@ namespace SharpGLTF.Schema2.AGI
         {
             _solarPanelGroups = new ChildrenList<AgiStkSolarPanelGroup, AgiRootStkMetadata>(this);
         }
-
-        protected override IEnumerable<ExtraProperties> GetLogicalChildren()
-        {
-            return base.GetLogicalChildren().Concat(_solarPanelGroups);
-        }
-
+        
         public IReadOnlyList<AgiStkSolarPanelGroup> SolarPanelGroups => _solarPanelGroups;
 
         public AgiStkSolarPanelGroup CreateSolarPanelGroup(string name)