Browse Source

Fixed infinite recursive loop when loading GLBs with external binary blobs.

Vicente Penades 6 năm trước cách đây
mục cha
commit
cae4f0e859

+ 9 - 1
src/SharpGLTF.Core/Schema2/gltf.Serialization.Read.cs

@@ -135,7 +135,15 @@ namespace SharpGLTF.Schema2
 
 
             if (chunks.ContainsKey(glb.CHUNKBIN))
             if (chunks.ContainsKey(glb.CHUNKBIN))
             {
             {
-                settings.FileReader = key => string.IsNullOrEmpty(key) ? new BYTES(chunks[glb.CHUNKBIN]) : settings.FileReader.Invoke(key);
+                var sourceReader = settings.FileReader;
+
+                settings.FileReader =
+                    key =>
+                    string.IsNullOrEmpty(key)
+                    ?
+                    new BYTES(chunks[glb.CHUNKBIN])
+                    :
+                    sourceReader.Invoke(key);
             }
             }
 
 
             return ParseGLTF(dom, settings);
             return ParseGLTF(dom, settings);

+ 6 - 3
src/SharpGLTF.Core/Schema2/gltf.Serialization.Write.cs

@@ -181,7 +181,7 @@ namespace SharpGLTF.Schema2
         internal Boolean _UpdateSupportedExtensions { get; private set; } = true;
         internal Boolean _UpdateSupportedExtensions { get; private set; } = true;
 
 
         /// <summary>
         /// <summary>
-        /// Gets a value indicating whether cloning the model upon serialization is not allowed.
+        /// Gets a value indicating whether creating a defensive copy before serialization is not allowed.
         /// </summary>
         /// </summary>
         internal bool _NoCloneWatchdog { get; private set; } = false;
         internal bool _NoCloneWatchdog { get; private set; } = false;
 
 
@@ -190,7 +190,7 @@ namespace SharpGLTF.Schema2
         #region API
         #region API
 
 
         /// <summary>
         /// <summary>
-        /// Prepares the model for writing with the appropiate settings, cloning it if neccesary.
+        /// Prepares the model for writing with the appropiate settings, creating a defensive copy if neccesary.
         /// </summary>
         /// </summary>
         /// <param name="model">The source <see cref="MODEL"/> instance.</param>
         /// <param name="model">The source <see cref="MODEL"/> instance.</param>
         /// <returns>The source <see cref="MODEL"/> instance, or a cloned and modified instance if current settings required it.</returns>
         /// <returns>The source <see cref="MODEL"/> instance, or a cloned and modified instance if current settings required it.</returns>
@@ -198,13 +198,16 @@ namespace SharpGLTF.Schema2
         {
         {
             Guard.NotNull(model, nameof(model));
             Guard.NotNull(model, nameof(model));
 
 
+            // check if we need to modify the model before saving it,
+            // in order to create a defensive copy.
+
             var needsMergeBuffers = (this.MergeBuffers | this.BinaryMode) && model.LogicalBuffers.Count > 1;
             var needsMergeBuffers = (this.MergeBuffers | this.BinaryMode) && model.LogicalBuffers.Count > 1;
 
 
             var imagesAsBufferViews = model.LogicalImages.Count > 0 && this.ImageWriting == ImageWriteMode.BufferView;
             var imagesAsBufferViews = model.LogicalImages.Count > 0 && this.ImageWriting == ImageWriteMode.BufferView;
 
 
             if (needsMergeBuffers | imagesAsBufferViews)
             if (needsMergeBuffers | imagesAsBufferViews)
             {
             {
-                if (_NoCloneWatchdog) throw new InvalidOperationException($"Current settings require a model rewrite, but {nameof(MODEL.DeepClone)} is not allowed in the current context");
+                if (_NoCloneWatchdog) throw new InvalidOperationException($"Current settings require creating a densive copy before model modification, but calling {nameof(MODEL.DeepClone)} is not allowed with the current settings.");
                 model = model.DeepClone();
                 model = model.DeepClone();
             }
             }