Skip to content

Commit 9233c80

Browse files
committed
GetLogicalChildren() can use reflection now, so no need for each class to require an override anymore.
1 parent 4291318 commit 9233c80

17 files changed

+82
-269
lines changed

src/SharpGLTF.Core/Schema2/gltf.AccessorSparse.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,7 @@ internal AccessorSparse() { }
2020
#endregion
2121

2222
#region API
23-
24-
protected override IEnumerable<ExtraProperties> GetLogicalChildren()
25-
{
26-
return base.GetLogicalChildren().ConcatElements(_indices, _values);
27-
}
28-
23+
2924
internal AccessorSparse(BufferView indices, int indicesOffset, IndexEncodingType indicesEncoding, BufferView values, int valuesOffset, int count)
3025
{
3126
Guard.NotNull(indices, nameof(indices));

src/SharpGLTF.Core/Schema2/gltf.Accessors.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -144,11 +144,6 @@ internal MemoryAccessor _GetMemoryAccessor(string name = null)
144144
: this._sparse._CreateMemoryAccessors(this);
145145
}
146146

147-
protected override IEnumerable<ExtraProperties> GetLogicalChildren()
148-
{
149-
return base.GetLogicalChildren().ConcatElements(_sparse);
150-
}
151-
152147
public void UpdateBounds()
153148
{
154149
this._min.Clear();

src/SharpGLTF.Core/Schema2/gltf.AnimationChannel.cs

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -48,16 +48,7 @@ void IChildOfList<Animation>.SetLogicalParent(Animation parent, int index)
4848
LogicalParent = parent;
4949
LogicalIndex = index;
5050
}
51-
52-
protected override IEnumerable<ExtraProperties> GetLogicalChildren()
53-
{
54-
var children = base.GetLogicalChildren();
55-
56-
if (_target != null) children = children.Append(_target);
57-
58-
return children;
59-
}
60-
51+
6152
#endregion
6253

6354
#region properties

src/SharpGLTF.Core/Schema2/gltf.Animations.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,7 @@ internal Animation()
2424
_channels = new ChildrenList<AnimationChannel, Animation>(this);
2525
_samplers = new ChildrenList<AnimationSampler, Animation>(this);
2626
}
27-
28-
protected override IEnumerable<ExtraProperties> GetLogicalChildren()
29-
{
30-
return base.GetLogicalChildren().Concat(_samplers).Concat(_channels);
31-
}
32-
27+
3328
#endregion
3429

3530
#region properties

src/SharpGLTF.Core/Schema2/gltf.Camera.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,7 @@ internal Camera() { }
2424
#endregion
2525

2626
#region API
27-
28-
protected override IEnumerable<ExtraProperties> GetLogicalChildren()
29-
{
30-
return base.GetLogicalChildren().ConcatElements(_orthographic, _perspective);
31-
}
32-
27+
3328
internal ICamera GetCamera()
3429
{
3530
if (this._orthographic != null && this._perspective != null)

src/SharpGLTF.Core/Schema2/gltf.ExtraProperties.cs

Lines changed: 52 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@
88
using SharpGLTF.IO;
99
using SharpGLTF.Reflection;
1010

11-
using JsonToken = System.Text.Json.JsonTokenType;
12-
11+
using JSONTOKEN = System.Text.Json.JsonTokenType;
1312
using JSONEXTRAS = System.Text.Json.Nodes.JsonNode;
1413

1514
namespace SharpGLTF.Schema2
@@ -30,7 +29,7 @@ public interface IExtraProperties
3029
public abstract class ExtraProperties
3130
: JsonSerializable
3231
, IExtraProperties
33-
, Reflection.IReflectionObject
32+
, IReflectionObject
3433
{
3534
#region data
3635

@@ -102,6 +101,53 @@ protected virtual bool TryReflectField(string name, out Reflection.FieldInfo val
102101
}
103102
}
104103

104+
/// <summary>
105+
/// Gets a collection of <see cref="ExtraProperties"/> instances stored by this object.
106+
/// </summary>
107+
/// <returns>A collection of <see cref="ExtraProperties"/> instances.</returns>
108+
/// <remarks>
109+
/// This is used to traverse the whole glTF document tree and gather all the objects<br/>
110+
/// So we can identify which extensions are used anywhere in the document.
111+
/// </remarks>
112+
protected IEnumerable<ExtraProperties> GetLogicalChildren()
113+
{
114+
foreach (var ext in _extensions.OfType<ExtraProperties>())
115+
{
116+
yield return ext;
117+
}
118+
119+
if (!(this is IReflectionObject robj)) yield break;
120+
121+
foreach (var field in robj.GetFields())
122+
{
123+
var value = field.Value;
124+
125+
if (value is IReflectionArray array)
126+
{
127+
for (int i = 0; i < array.Count; ++i)
128+
{
129+
var item = array.GetField(i);
130+
if (item.Value is ExtraProperties itemExtra) yield return itemExtra;
131+
}
132+
}
133+
else if (value is ExtraProperties extra) yield return extra;
134+
}
135+
}
136+
137+
protected static IEnumerable<ExtraProperties> Flatten(ExtraProperties container)
138+
{
139+
if (container == null) yield break;
140+
141+
yield return container;
142+
143+
foreach (var c in container.GetLogicalChildren())
144+
{
145+
var cc = Flatten(c);
146+
147+
foreach (var ccc in cc) yield return ccc;
148+
}
149+
}
150+
105151
#endregion
106152

107153
#region API
@@ -164,34 +210,7 @@ public void RemoveExtensions<T>()
164210
where T : JsonSerializable
165211
{
166212
_extensions.RemoveAll(item => item.GetType() == typeof(T));
167-
}
168-
169-
/// <summary>
170-
/// Gets a collection of <see cref="ExtraProperties"/> instances stored by this object.
171-
/// </summary>
172-
/// <returns>A collection of <see cref="ExtraProperties"/> instances.</returns>
173-
/// <remarks>
174-
/// This is used to traverse the whole glTF document tree and gather all the objects<br/>
175-
/// So we can identify which extensions are used anywhere in the document.
176-
/// </remarks>
177-
protected virtual IEnumerable<ExtraProperties> GetLogicalChildren()
178-
{
179-
return _extensions.OfType<ExtraProperties>();
180-
}
181-
182-
protected static IEnumerable<ExtraProperties> Flatten(ExtraProperties container)
183-
{
184-
if (container == null) yield break;
185-
186-
yield return container;
187-
188-
foreach (var c in container.GetLogicalChildren())
189-
{
190-
var cc = Flatten(c);
191-
192-
foreach (var ccc in cc) yield return ccc;
193-
}
194-
}
213+
}
195214

196215
#endregion
197216

@@ -297,9 +316,9 @@ private static void _DeserializeExtensions(JsonSerializable parent, ref Utf8Json
297316
{
298317
reader.Read();
299318

300-
if (reader.TokenType == JsonToken.StartObject)
319+
if (reader.TokenType == JSONTOKEN.StartObject)
301320
{
302-
while (reader.Read() && reader.TokenType != JsonToken.EndObject)
321+
while (reader.Read() && reader.TokenType != JSONTOKEN.EndObject)
303322
{
304323
var key = reader.GetString();
305324

src/SharpGLTF.Core/Schema2/gltf.Material.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -92,12 +92,7 @@ public float IndexOfRefraction
9292
#endregion
9393

9494
#region API
95-
96-
protected override IEnumerable<ExtraProperties> GetLogicalChildren()
97-
{
98-
return base.GetLogicalChildren().ConcatElements(_normalTexture, _emissiveTexture, _occlusionTexture, _pbrMetallicRoughness);
99-
}
100-
95+
10196
/// <summary>
10297
/// Finds an instance of <see cref="MaterialChannel"/>
10398
/// </summary>

src/SharpGLTF.Core/Schema2/gltf.MaterialsFactory.cs

Lines changed: 10 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -206,12 +206,7 @@ public float RoughnessFactor
206206
#endregion
207207

208208
#region API
209-
210-
protected override IEnumerable<ExtraProperties> GetLogicalChildren()
211-
{
212-
return base.GetLogicalChildren().ConcatElements(_baseColorTexture, _metallicRoughnessTexture);
213-
}
214-
209+
215210
protected override void OnValidateContent(ValidationContext validate)
216211
{
217212
base.OnValidateContent(validate);
@@ -253,12 +248,7 @@ internal sealed partial class MaterialPBRSpecularGlossiness
253248
#pragma warning disable CA1801 // Review unused parameters
254249
internal MaterialPBRSpecularGlossiness(Material material) { }
255250
#pragma warning restore CA1801 // Review unused parameters
256-
257-
protected override IEnumerable<ExtraProperties> GetLogicalChildren()
258-
{
259-
return base.GetLogicalChildren().ConcatElements(_diffuseTexture, _specularGlossinessTexture);
260-
}
261-
251+
262252
protected override void OnValidateContent(ValidationContext validate)
263253
{
264254
base.OnValidateContent(validate);
@@ -319,12 +309,7 @@ internal sealed partial class MaterialClearCoat
319309
#pragma warning disable CA1801 // Review unused parameters
320310
internal MaterialClearCoat(Material material) { }
321311
#pragma warning restore CA1801 // Review unused parameters
322-
323-
protected override IEnumerable<ExtraProperties> GetLogicalChildren()
324-
{
325-
return base.GetLogicalChildren().ConcatElements(_clearcoatTexture, _clearcoatRoughnessTexture, _clearcoatNormalTexture);
326-
}
327-
312+
328313
protected override void OnValidateContent(ValidationContext validate)
329314
{
330315
base.OnValidateContent(validate);
@@ -393,12 +378,7 @@ internal sealed partial class MaterialTransmission
393378
#pragma warning disable CA1801 // Review unused parameters
394379
internal MaterialTransmission(Material material) { }
395380
#pragma warning restore CA1801 // Review unused parameters
396-
397-
protected override IEnumerable<ExtraProperties> GetLogicalChildren()
398-
{
399-
return base.GetLogicalChildren().ConcatElements(_transmissionTexture);
400-
}
401-
381+
402382
protected override void OnValidateContent(ValidationContext validate)
403383
{
404384
base.OnValidateContent(validate);
@@ -429,12 +409,7 @@ internal sealed partial class MaterialSheen
429409
#pragma warning disable CA1801 // Review unused parameters
430410
internal MaterialSheen(Material material) { }
431411
#pragma warning restore CA1801 // Review unused parameters
432-
433-
protected override IEnumerable<ExtraProperties> GetLogicalChildren()
434-
{
435-
return base.GetLogicalChildren().ConcatElements(_sheenColorTexture, _sheenRoughnessTexture);
436-
}
437-
412+
438413
protected override void OnValidateContent(ValidationContext validate)
439414
{
440415
base.OnValidateContent(validate);
@@ -507,12 +482,7 @@ internal sealed partial class MaterialSpecular
507482
#pragma warning disable CA1801 // Review unused parameters
508483
internal MaterialSpecular(Material material) { }
509484
#pragma warning restore CA1801 // Review unused parameters
510-
511-
protected override IEnumerable<ExtraProperties> GetLogicalChildren()
512-
{
513-
return base.GetLogicalChildren().ConcatElements(_specularColorTexture, _specularTexture);
514-
}
515-
485+
516486
protected override void OnValidateContent(ValidationContext validate)
517487
{
518488
base.OnValidateContent(validate);
@@ -559,12 +529,7 @@ internal sealed partial class MaterialVolume
559529
#pragma warning disable CA1801 // Review unused parameters
560530
internal MaterialVolume(Material material) { }
561531
#pragma warning restore CA1801 // Review unused parameters
562-
563-
protected override IEnumerable<ExtraProperties> GetLogicalChildren()
564-
{
565-
return base.GetLogicalChildren().ConcatElements(_thicknessTexture);
566-
}
567-
532+
568533
protected override void OnValidateContent(ValidationContext validate)
569534
{
570535
base.OnValidateContent(validate);
@@ -657,12 +622,7 @@ internal sealed partial class MaterialIridescence
657622
#pragma warning disable CA1801 // Review unused parameters
658623
internal MaterialIridescence(Material material) { }
659624
#pragma warning restore CA1801 // Review unused parameters
660-
661-
protected override IEnumerable<ExtraProperties> GetLogicalChildren()
662-
{
663-
return base.GetLogicalChildren().ConcatElements(_iridescenceTexture, _iridescenceThicknessTexture);
664-
}
665-
625+
666626
protected override void OnValidateContent(ValidationContext validate)
667627
{
668628
base.OnValidateContent(validate);
@@ -727,12 +687,7 @@ internal sealed partial class MaterialAnisotropy
727687
#pragma warning disable CA1801 // Review unused parameters
728688
internal MaterialAnisotropy(Material material) { }
729689
#pragma warning restore CA1801 // Review unused parameters
730-
731-
protected override IEnumerable<ExtraProperties> GetLogicalChildren()
732-
{
733-
return base.GetLogicalChildren().ConcatElements(_anisotropyTexture);
734-
}
735-
690+
736691
protected override void OnValidateContent(ValidationContext validate)
737692
{
738693
base.OnValidateContent(validate);
@@ -769,12 +724,7 @@ internal sealed partial class MaterialDiffuseTransmission
769724
#pragma warning disable CA1801 // Review unused parameters
770725
internal MaterialDiffuseTransmission(Material material) { }
771726
#pragma warning restore CA1801 // Review unused parameters
772-
773-
protected override IEnumerable<ExtraProperties> GetLogicalChildren()
774-
{
775-
return base.GetLogicalChildren().ConcatElements(_diffuseTransmissionTexture, _diffuseTransmissionColorTexture);
776-
}
777-
727+
778728
protected override void OnValidateContent(ValidationContext validate)
779729
{
780730
base.OnValidateContent(validate);

src/SharpGLTF.Core/Schema2/gltf.Mesh.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,7 @@ public void SetMorphWeights(Transforms.SparseWeight8 weights)
6767

6868
_weights.SetMorphWeights(count, weights);
6969
}
70-
71-
protected override IEnumerable<ExtraProperties> GetLogicalChildren()
72-
{
73-
return base.GetLogicalChildren().Concat(_primitives);
74-
}
75-
70+
7671
/// <summary>
7772
/// Creates a new <see cref="MeshPrimitive"/> instance
7873
/// and adds it to the current <see cref="Mesh"/>.

src/SharpGLTF.Core/Schema2/gltf.PunctualLight.cs

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -151,20 +151,7 @@ public Single Range
151151
}
152152
}
153153

154-
#endregion
155-
156-
#region API
157-
158-
protected override IEnumerable<ExtraProperties> GetLogicalChildren()
159-
{
160-
var children = base.GetLogicalChildren();
161-
162-
if (_spot != null) children = children.Concat(new[] { _spot });
163-
164-
return children;
165-
}
166-
167-
#endregion
154+
#endregion
168155

169156
#region Validation
170157

0 commit comments

Comments
 (0)