87 lines
2.5 KiB
C#
Executable File
87 lines
2.5 KiB
C#
Executable File
using System;
|
|
using System.Runtime.CompilerServices;
|
|
using System.Text.Json;
|
|
|
|
namespace ReFuel.Gltf
|
|
{
|
|
public enum GltfObjectKind
|
|
{
|
|
Document,
|
|
Asset,
|
|
Scene,
|
|
Node,
|
|
Camera,
|
|
Mesh,
|
|
Buffer,
|
|
BufferView,
|
|
Accessor,
|
|
Material,
|
|
Texture,
|
|
Image,
|
|
Sampler,
|
|
Skin,
|
|
Animation,
|
|
MeshPrimitive,
|
|
Misc,
|
|
}
|
|
|
|
public abstract class GltfObject
|
|
{
|
|
public abstract GltfObjectKind Kind { get; }
|
|
public GltfDocument Document { get; protected set; }
|
|
public GltfCollection Extension { get; protected set; } = new GltfCollection();
|
|
public GltfCollection Extras { get; protected set; } = new GltfCollection();
|
|
|
|
protected GltfObject()
|
|
{
|
|
Unsafe.SkipInit(out GltfDocument doc);
|
|
Document = doc;
|
|
}
|
|
|
|
protected GltfObject(GltfDocument document) : this()
|
|
{
|
|
Document = document;
|
|
}
|
|
|
|
protected void DeserializeCommon(JsonElement element)
|
|
{
|
|
if (element.TryGetProperty("extras", out JsonElement extras))
|
|
{
|
|
Extras.Parse(extras);
|
|
}
|
|
if (element.TryGetProperty("extension", out JsonElement extension))
|
|
{
|
|
Extension.Parse(extension);
|
|
}
|
|
}
|
|
|
|
internal abstract void Deserialize(JsonElement element);
|
|
internal abstract void Serialize(Utf8JsonWriter writer);
|
|
|
|
protected void AssertObject(JsonElement element)
|
|
{
|
|
if (element.ValueKind != JsonValueKind.Object)
|
|
throw new Exception($"Gltf document node '{Kind}' expects an object in the JSON.");
|
|
}
|
|
|
|
protected void AssertArray(JsonElement element, string property)
|
|
{
|
|
if (element.ValueKind != JsonValueKind.Array)
|
|
throw new Exception($"Gltf document '{property}' expects an array in the JSON.");
|
|
}
|
|
|
|
protected void RequiredProperty(string name)
|
|
{
|
|
throw new Exception($"Gltf document node '{Kind}' requries property '{name}'.");
|
|
}
|
|
|
|
protected void PropertyFormatFail(string name, string? message)
|
|
{
|
|
if (message != null)
|
|
throw new Exception($"Gltf document node '{Kind}', property '{name}' has invalid format: {message}");
|
|
else
|
|
throw new Exception($"Gltf document node '{Kind}', property '{name}' has invalid format.");
|
|
}
|
|
}
|
|
}
|