From 805613b1fe1ff93d9509bb89edb00bb78ddb5722 Mon Sep 17 00:00:00 2001 From: ousttrue Date: Thu, 16 Dec 2021 18:57:02 +0900 Subject: [PATCH] =?UTF-8?q?extensions=20=E3=81=8C=E7=84=A1=E3=81=84?= =?UTF-8?q?=E5=A0=B4=E5=90=88=E3=82=92=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Runtime/UniGLTF/IO/GltfJsonUtil.cs | 51 +++++++++++++++++-- .../Tests/UniGLTF/GltfJsonUtilTests.cs | 40 ++++++++++++++- 2 files changed, 86 insertions(+), 5 deletions(-) diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/GltfJsonUtil.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/GltfJsonUtil.cs index d3bc1696c..76f5168fe 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/GltfJsonUtil.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/GltfJsonUtil.cs @@ -1,4 +1,3 @@ -using System; using System.Collections.Generic; using System.IO; using System.Linq; @@ -127,8 +126,52 @@ namespace UniGLTF // 無いとき if (parsed.ContainsKey(EXTENSION_USED_KEY)) { - // 消す - throw new NotImplementedException(); + foreach (var kv in parsed.ObjectItems()) + { + if (kv.Key.GetString() == EXTENSION_USED_KEY) + { + // 削除範囲は + // kv.Key の先頭から kv.Value の後ろ + // kv.Value の次の文字は , か } がありえる。 + var begin = kv.Key.Value.Segment.Bytes.Offset; + var end = kv.Value.Value.Segment.Bytes.Offset + kv.Value.Value.Segment.Bytes.Count; + var array = kv.Key.Value.Segment.Bytes.Array; + for (var i = end; i < array.Length; ++i) + { + if (array[i] == ',') + { + end = i + 1; + break; + } + else if (array[i] == '}') + { + // begin 側の , を探す + for (var j = begin - 1; j >= 0; --j) + { + if (array[j] == ',') + { + begin = j; + break; + } + } + end = i; + break; + } + } + + using (var w = new MemoryStream()) + { + // before + w.Write(array, 0, begin); + // after + w.Write(array, end, array.Length - end); + + // BOM 無し encoder + var utf8 = new UTF8Encoding(false); + src = utf8.GetString(w.ToArray()); + } + } + } } else { @@ -150,8 +193,8 @@ namespace UniGLTF // BOM 無し encoder var utf8 = new UTF8Encoding(false); - var bytes = utf8.GetBytes(values); + var bytes = utf8.GetBytes(values); using (var w = new MemoryStream()) { // before diff --git a/Assets/UniGLTF/Tests/UniGLTF/GltfJsonUtilTests.cs b/Assets/UniGLTF/Tests/UniGLTF/GltfJsonUtilTests.cs index 547c32d56..cd806db21 100644 --- a/Assets/UniGLTF/Tests/UniGLTF/GltfJsonUtilTests.cs +++ b/Assets/UniGLTF/Tests/UniGLTF/GltfJsonUtilTests.cs @@ -105,7 +105,7 @@ namespace UniGLTF ] } ], - ""extensionUsed"": [""dummy""], + ""extensionUsed"": [""dummy""] , ""materials"": [ { ""pbrMetallicRoughness"": { @@ -125,5 +125,43 @@ namespace UniGLTF var parsed = dst.ParseAsJson(); Assert.False(parsed.ContainsKey("extensionUsed")); } + + [Test] + public void Empty2_extensionUsed() + { + var dst = GltfJsonUtil.FindUsedExtensionsAndUpdateJson(@"{ + ""asset"": { + ""generator"": ""COLLADA2GLTF"", + ""version"": ""2.0"" + }, + ""scene"": 0, + ""scenes"": [ + { + ""nodes"": [ + 0 + ] + } + ], + ""materials"": [ + { + ""pbrMetallicRoughness"": { + ""baseColorFactor"": [ + 0.800000011920929, + 0.0, + 0.0, + 1.0 + ], + ""metallicFactor"": 0.0 + }, + ""name"": ""Red"" + } + ], + ""extensionUsed"": [""dummy""] +}"); + + var parsed = dst.ParseAsJson(); + Assert.False(parsed.ContainsKey("extensionUsed")); + } + } }