Skip to content

Commit 28af2b1

Browse files
committed
Extension classes now inherit from ExtensionBase
1 parent cb9bddc commit 28af2b1

4 files changed

Lines changed: 43 additions & 63 deletions

File tree

src/SharpGLTF.CodeGen.Core/CodeGen/EmitCSharp.cs

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public void SetRuntimeName(SchemaType stype, string runtimeName, string runtimeN
4444

4545
t.RuntimeNamespace = runtimeNamespace;
4646
t.RuntimeName = runtimeName;
47-
}
47+
}
4848

4949
/// <summary>
5050
/// Gets the runtime name associated to the type with the given <paramref name="persistentName"/>
@@ -162,7 +162,7 @@ private _RuntimeType _UseType(SchemaType stype)
162162

163163
rtype = new _RuntimeType()
164164
{
165-
RuntimeName = _SanitizeName(stype.PersistentName)
165+
RuntimeName = _SanitizeName(stype.PersistentName)
166166
};
167167

168168
_Types[key] = rtype;
@@ -451,7 +451,26 @@ private string _GetClassDeclaration(ClassType type)
451451
classDecl += "partial ";
452452
classDecl += "class ";
453453
classDecl += _GetRuntimeName(type);
454-
if (type.BaseClass != null) classDecl += $" : {_GetRuntimeName(type.BaseClass)}";
454+
455+
if (type.BaseClass != null)
456+
{
457+
var baseClass = _GetRuntimeName(type.BaseClass);
458+
459+
// if type is an extension, replace ExtraProperties base class
460+
// with the more meaningful ExtensionBase class
461+
if (baseClass == "ExtraProperties" && type.PersistentName.EndsWith(" EXTENSION", StringComparison.OrdinalIgnoreCase))
462+
{
463+
var parentIdentifier = type.Identifier.Split('.')[0];
464+
// parentIdentifier is glTF, material, node, etc
465+
// var parentClass = type._Owner.FindClass(parentIdentifier);
466+
// var parentName = _GetRuntimeName(parentClass);
467+
468+
baseClass = "ExtensionBase";
469+
}
470+
471+
classDecl += $" : {baseClass}";
472+
}
473+
455474
return classDecl;
456475
}
457476

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

Lines changed: 14 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -80,38 +80,7 @@ static ExtensionsFactory()
8080

8181
public static IEnumerable<string> SupportedExtensions => _Extensions
8282
.Select(item => item.Name)
83-
.Concat(new[] { "KHR_mesh_quantization" }); // special case because it's a "typeless" extension.
84-
85-
/// <summary>
86-
/// Registers a new extensions to be used globally.
87-
/// </summary>
88-
/// <typeparam name="TParent">The parent type to which this extension is attached.</typeparam>
89-
/// <typeparam name="TExtension">The extension type.</typeparam>
90-
/// <param name="persistentName">The extension name.</param>
91-
/// <remarks>
92-
/// The <paramref name="persistentName"/> is the value used for serialization<br/>
93-
/// and it must meet <see href="https://github.com/KhronosGroup/glTF/blob/master/extensions/Prefixes.md">extension naming constraints</see>.
94-
/// </remarks>
95-
[Obsolete("Use RegisterExtension(name, factory) instead.")]
96-
public static void RegisterExtension
97-
<TParent,
98-
#if NET6_0_OR_GREATER
99-
[DYNAMICMEMBERS(DYNAMICCONSTRUCTORS)]
100-
#endif
101-
TExtension>
102-
(string persistentName)
103-
where TParent : JsonSerializable
104-
where TExtension : JsonSerializable
105-
{
106-
Guard.NotNullOrEmpty(persistentName, nameof(persistentName));
107-
Guard.MustBeNull(Identify(typeof(TParent), typeof(TExtension)), $"{nameof(TExtension)} already registered for {nameof(TParent)}");
108-
109-
// TODO: check that persistentName has a valid extension name.
110-
111-
var ext = ExtensionEntry.Create<TParent,TExtension>(persistentName);
112-
113-
_Extensions.Add(ext);
114-
}
83+
.Concat(new[] { "KHR_mesh_quantization" }); // special case because it's a "typeless" extension.
11584

11685
/// <summary>
11786
/// Registers a new extensions to be used globally.
@@ -126,7 +95,7 @@ public static void RegisterExtension
12695
/// </remarks>
12796
public static void RegisterExtension<TParent,TExtension>(string persistentName, Func<TParent, JsonSerializable> factory)
12897
where TParent : JsonSerializable
129-
where TExtension : JsonSerializable
98+
where TExtension : ExtensionBase
13099
{
131100
Guard.NotNullOrEmpty(persistentName, nameof(persistentName));
132101
Guard.MustBeNull(Identify(typeof(TParent), typeof(TExtension)), $"{nameof(TExtension)} already registered for {nameof(TParent)}");
@@ -299,13 +268,11 @@ internal void UpdateExtensionsSupport()
299268
if (string.IsNullOrWhiteSpace(id)) continue;
300269

301270
bool isRequired = false;
302-
303-
#pragma warning disable GLTF1001 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
304-
if (ext is IExtensionTypeInfo extInfo)
271+
272+
if (ext is ExtensionBase extInfo)
305273
{
306274
isRequired = extInfo.CheckIsRequiredExtension(c);
307-
}
308-
#pragma warning restore GLTF1001 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
275+
}
309276

310277
if (exts.TryGetValue(id, out var stored)) { isRequired |= stored; }
311278

@@ -344,21 +311,15 @@ internal void _ValidateExtensions(Validation.ValidationContext validate)
344311
}
345312

346313
/// <summary>
347-
/// Implemented by extensions
314+
/// Base class for extensions
348315
/// </summary>
349-
#if NET8_0_OR_GREATER
350-
[Experimental("GLTF1001")] // I might move this functionality to an "ExtensionBase" class
351-
#endif
352-
public interface IExtensionTypeInfo
316+
public abstract class ExtensionBase : ExtraProperties
353317
{
354-
/// <summary>
355-
/// Checks whether the extension instance implementing this interface should be tagged as required.
356-
/// </summary>
357-
/// <param name="extensionOwner">the owner of the extension</param>
358-
/// <returns>true if the extension should be tagged as required</returns>
359-
/// <remarks>
360-
/// This is called just before saving the model.
361-
/// </remarks>
362-
public bool CheckIsRequiredExtension(ExtraProperties extensionOwner);
363-
}
318+
protected ExtensionBase() { }
319+
320+
public virtual bool CheckIsRequiredExtension(ExtraProperties extensionOwner)
321+
{
322+
return false;
323+
}
324+
}
364325
}

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ protected override void OnValidateReferences(ValidationContext validate)
158158

159159
#pragma warning disable GLTF1001 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
160160

161-
partial class TextureDDS : IExtensionTypeInfo
161+
partial class TextureDDS
162162
{
163163
internal TextureDDS(Texture parent)
164164
{
@@ -182,15 +182,15 @@ public Image Image
182182
}
183183
}
184184

185-
public bool CheckIsRequiredExtension(ExtraProperties extensionOwner)
185+
public override bool CheckIsRequiredExtension(ExtraProperties extensionOwner)
186186
{
187187
if (extensionOwner is not Texture tex) return false;
188188

189189
return tex.FallbackImage == null;
190190
}
191191
}
192192

193-
partial class TextureWEBP : IExtensionTypeInfo
193+
partial class TextureWEBP
194194
{
195195
internal TextureWEBP(Texture parent)
196196
{
@@ -214,15 +214,15 @@ public Image Image
214214
}
215215
}
216216

217-
public bool CheckIsRequiredExtension(ExtraProperties extensionOwner)
217+
public override bool CheckIsRequiredExtension(ExtraProperties extensionOwner)
218218
{
219219
if (extensionOwner is not Texture tex) return false;
220220

221221
return tex.FallbackImage == null;
222222
}
223223
}
224224

225-
partial class TextureKTX2 : IExtensionTypeInfo
225+
partial class TextureKTX2
226226
{
227227
internal TextureKTX2(Texture parent)
228228
{
@@ -246,7 +246,7 @@ public Image Image
246246
}
247247
}
248248

249-
public bool CheckIsRequiredExtension(ExtraProperties extensionOwner)
249+
public override bool CheckIsRequiredExtension(ExtraProperties extensionOwner)
250250
{
251251
if (extensionOwner is not Texture tex) return false;
252252

tests/SharpGLTF.ThirdParty.Tests/GaussianSplatExtension.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public int BufferViewIndex
3737
[System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.NonPublicConstructors | System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicConstructors)]
3838
#endif
3939
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("SharpGLTF.CodeGen", "1.0.0.0")]
40-
partial class SpzGaussianSplatsCompression : ExtraProperties
40+
partial class SpzGaussianSplatsCompression : ExtensionBase
4141
{
4242

4343
#region reflection

0 commit comments

Comments
 (0)