mirror of
https://github.com/vrm-c/UniVRM.git
synced 2026-05-12 13:34:39 -05:00
86 lines
2.2 KiB
C#
86 lines
2.2 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Text;
|
|
using UniJSON;
|
|
|
|
namespace UniGLTF
|
|
{
|
|
/// <summary>
|
|
/// Extension または Extras に使う
|
|
/// </summary>
|
|
public class glTFExtension
|
|
{
|
|
// NO BOM
|
|
static Encoding Utf8 = new UTF8Encoding(false);
|
|
|
|
#region for Export
|
|
public readonly Dictionary<string, ArraySegment<byte>> Serialized;
|
|
public glTFExtension()
|
|
{
|
|
Serialized = new Dictionary<string, ArraySegment<byte>>();
|
|
}
|
|
public static glTFExtension Create(string key, string serialized)
|
|
{
|
|
var e = new glTFExtension();
|
|
e.Serialized.Add(key, new ArraySegment<byte>(Utf8.GetBytes(serialized)));
|
|
return e;
|
|
}
|
|
#endregion
|
|
|
|
#region for Import
|
|
readonly ListTreeNode<JsonValue> m_json;
|
|
public glTFExtension(ListTreeNode<JsonValue> json)
|
|
{
|
|
m_json = json;
|
|
}
|
|
|
|
public IEnumerable<KeyValuePair<ListTreeNode<JsonValue>, ListTreeNode<JsonValue>>> ObjectItems()
|
|
{
|
|
if (m_json.Value.ValueType == ValueNodeType.Object)
|
|
{
|
|
foreach (var kv in m_json.ObjectItems())
|
|
{
|
|
yield return kv;
|
|
}
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
/// <summary>
|
|
/// for unit test
|
|
///
|
|
/// parse exported value
|
|
/// </summary>
|
|
public glTFExtension Parse()
|
|
{
|
|
var f = new JsonFormatter();
|
|
f.BeginMap();
|
|
foreach (var kv in Serialized)
|
|
{
|
|
f.Key(kv.Key);
|
|
f.Raw(kv.Value);
|
|
}
|
|
f.EndMap();
|
|
|
|
var b = f.GetStoreBytes();
|
|
var json = Encoding.UTF8.GetString(b.Array, b.Offset, b.Count);
|
|
return new glTFExtension(json.ParseAsJson());
|
|
}
|
|
}
|
|
|
|
public static class GltfExtensionFormatterExtensions
|
|
{
|
|
public static void GenSerialize(this JsonFormatter f, glTFExtension v)
|
|
{
|
|
//CommaCheck();
|
|
f.BeginMap();
|
|
foreach (var kv in v.Serialized)
|
|
{
|
|
f.Key(kv.Key);
|
|
f.Raw(kv.Value);
|
|
}
|
|
f.EndMap();
|
|
}
|
|
}
|
|
}
|