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); } }