From 8f5a6bed4ec82047ed42a0a457e825d2bd4a42a1 Mon Sep 17 00:00:00 2001 From: ousttrue Date: Thu, 16 Dec 2021 18:27:20 +0900 Subject: [PATCH] =?UTF-8?q?=E3=82=B3=E3=83=BC=E3=83=89=E6=95=B4=E7=90=86?= =?UTF-8?q?=E3=80=82=E3=82=B3=E3=83=A1=E3=83=B3=E3=83=88=E5=A2=97=E9=87=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Runtime/UniGLTF/IO/GltfJsonUtil.cs | 58 ++++++++++++++++--- 1 file changed, 49 insertions(+), 9 deletions(-) diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/GltfJsonUtil.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/GltfJsonUtil.cs index 5492e5490..341a5bab9 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/GltfJsonUtil.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/GltfJsonUtil.cs @@ -9,6 +9,13 @@ namespace UniGLTF { public static class GltfJsonUtil { + /// + /// JsonPath を 再帰的に列挙する + /// object[] の中身は int(array index) or string(object key) + /// + /// + /// + /// public static IEnumerable TraverseJsonPath(JsonNode node, List path) { if (path == null) @@ -50,6 +57,43 @@ namespace UniGLTF return $"\"{src}\""; } + /// + /// jsonPath が + /// + /// [..., "extensions", "EXTENSION_NAME"] + /// + /// で有る場合に EXTENSION_NAME を返す。 + /// + /// + /// + /// + static bool TryGetExtensionName(object[] path, out string extensionName) + { + if (path.Length >= 2) + { + if (path[path.Length - 2] is string x) + { + if (x == "extensions") + { + if (path[path.Length - 1] is string y) + { + extensionName = y; + return true; + } + else + { + // ありえない。はず + var join = string.Join(", ", path); + Debug.LogWarning($"invalid json path: {join}"); + } + } + } + } + + extensionName = default; + return false; + } + /// /// https://github.com/KhronosGroup/glTF/blob/main/specification/2.0/schema/glTF.schema.json /// @@ -63,19 +107,15 @@ namespace UniGLTF /// public static string FindUsedExtensionsAndUpdateJson(string src) { - var used = new HashSet(); var parsed = src.ParseAsJson(); + + // unique な extension 名を収集 + var used = new HashSet(); foreach (var path in TraverseJsonPath(parsed, null)) { - if (path.Length >= 2) + if (TryGetExtensionName(path, out string extensionName)) { - if (path[path.Length - 2] is string x) - { - if (x == "extensions") - { - used.Add(path[path.Length - 1] as string); - } - } + used.Add(extensionName); } }