Browse Source

refactored Uri handling

vpenades 2 tháng trước cách đây
mục cha
commit
812ca14e5b

+ 11 - 6
src/Shared/_Extensions.cs

@@ -894,13 +894,18 @@ namespace SharpGLTF
 
 
         #region json
         #region json
 
 
-        public static string _EscapeStringInternal(this string uri)
+        public static Uri ToUri(this UriKind kind, string value)
         {
         {
-            // https://stackoverflow.com/questions/4396598/whats-the-difference-between-escapeuristring-and-escapedatastring
-#pragma warning disable SYSLIB0013 // Type or member is obsolete
-            return Uri.EscapeUriString(uri);
-#pragma warning restore SYSLIB0013 // Type or member is obsolete
-        }
+            var uri = new Uri(value, kind);
+
+            if (!uri.IsAbsoluteUri || uri.IsFile)
+            {
+                value = value.Replace(System.IO.Path.DirectorySeparatorChar, System.IO.Path.AltDirectorySeparatorChar);
+                uri = new Uri(value, kind);
+            }
+
+            return uri;
+        }        
 
 
         #if NET6_0
         #if NET6_0
 
 

+ 5 - 0
src/SharpGLTF.Core/Memory/MemoryImage.cs

@@ -79,6 +79,11 @@ namespace SharpGLTF.Memory
 
 
         public static implicit operator MemoryImage(string filePath) { return new MemoryImage(filePath); }
         public static implicit operator MemoryImage(string filePath) { return new MemoryImage(filePath); }
 
 
+        public static bool TryParseMime64(Uri mime64content, out MemoryImage image)
+        {
+            return TryParseMime64(mime64content?.OriginalString, out image);
+        }
+
         /// <summary>
         /// <summary>
         /// Tries to parse a Mime64 string to <see cref="MemoryImage"/>
         /// Tries to parse a Mime64 string to <see cref="MemoryImage"/>
         /// </summary>
         /// </summary>

+ 6 - 1
src/SharpGLTF.Core/Schema2/Serialization.ReadContext.cs

@@ -57,7 +57,12 @@ namespace SharpGLTF.Schema2
 
 
         public static ReadContext CreateFromDictionary(IReadOnlyDictionary<string, BYTES> dictionary, bool checkExtensions = true)
         public static ReadContext CreateFromDictionary(IReadOnlyDictionary<string, BYTES> dictionary, bool checkExtensions = true)
         {
         {
-            return new ReadContext(rawUri => dictionary[rawUri], null, checkExtensions);
+            BYTES getBytes(string rawUri)
+            {
+                return dictionary[rawUri];
+            }
+
+            return new ReadContext(getBytes, null, checkExtensions);
         }
         }
 
 
         private ReadContext(FileReaderCallback reader, UriResolver uriResolver = null, bool checkExtensions = true)
         private ReadContext(FileReaderCallback reader, UriResolver uriResolver = null, bool checkExtensions = true)

+ 7 - 4
src/SharpGLTF.Core/Schema2/gltf.Buffer.cs

@@ -49,8 +49,11 @@ namespace SharpGLTF.Schema2
 
 
         private static Byte[] _LoadBinaryBufferUnchecked(string uri, ReadContext context)
         private static Byte[] _LoadBinaryBufferUnchecked(string uri, ReadContext context)
         {
         {
-            var data = uri.TryParseBase64Unchecked(EMBEDDEDGLTFBUFFER, EMBEDDEDOCTETSTREAM);
-            if (data != null) return data;
+            if (uri != null)
+            {
+                var data = uri.TryParseBase64Unchecked(EMBEDDEDGLTFBUFFER, EMBEDDEDOCTETSTREAM);
+                if (data != null) return data;
+            }            
 
 
             var segment = context
             var segment = context
                 .ReadAllBytesToEnd(uri);
                 .ReadAllBytesToEnd(uri);
@@ -73,7 +76,7 @@ namespace SharpGLTF.Schema2
         {
         {
             writer.WriteAllBytesToEnd(satelliteUri, new ArraySegment<byte>(_Content.GetPaddedContent()));
             writer.WriteAllBytesToEnd(satelliteUri, new ArraySegment<byte>(_Content.GetPaddedContent()));
 
 
-            this._uri = satelliteUri._EscapeStringInternal();
+            this._uri = UriKind.RelativeOrAbsolute.ToUri(satelliteUri).OriginalString;
             this._byteLength = _Content.Length;
             this._byteLength = _Content.Length;
         }
         }
 
 
@@ -170,7 +173,7 @@ namespace SharpGLTF.Schema2
 
 
         /// <summary>
         /// <summary>
         /// Creates or reuses a <see cref="Buffer"/> instance
         /// Creates or reuses a <see cref="Buffer"/> instance
-        /// at <see cref="ModelRoot.LogicalBuffers"/>.
+        /// at <see cref="LogicalBuffers"/>.
         /// </summary>
         /// </summary>
         /// <param name="content">the byte array to be wrapped as a buffer</param>
         /// <param name="content">the byte array to be wrapped as a buffer</param>
         /// <returns>A <see cref="Buffer"/> instance.</returns>
         /// <returns>A <see cref="Buffer"/> instance.</returns>

+ 19 - 14
src/SharpGLTF.Core/Schema2/gltf.Images.cs

@@ -147,7 +147,7 @@ namespace SharpGLTF.Schema2
         internal void _ResolveUri(ReadContext context)
         internal void _ResolveUri(ReadContext context)
         {
         {
             // No uri to decode.
             // No uri to decode.
-            if (String.IsNullOrWhiteSpace(_uri)) return;
+            if (_uri == null) return;
 
 
             // Try decode Base64 embedded image.
             // Try decode Base64 embedded image.
             if (Memory.MemoryImage.TryParseMime64(_uri, out var memImage))
             if (Memory.MemoryImage.TryParseMime64(_uri, out var memImage))
@@ -158,8 +158,10 @@ namespace SharpGLTF.Schema2
             // Then it's a regular URI
             // Then it's a regular URI
             else
             else
             {
             {
+                var satelliteUri = _uri;
+
                 // try resolve the full path
                 // try resolve the full path
-                if (context.TryGetFullPath(_uri, out string fullPath))
+                if (context.TryGetFullPath(satelliteUri, out string fullPath))
                 {
                 {
                     _SatelliteContent = fullPath;
                     _SatelliteContent = fullPath;
                 }
                 }
@@ -167,7 +169,8 @@ namespace SharpGLTF.Schema2
                 // full path could not be resolved, use direct load instead.
                 // full path could not be resolved, use direct load instead.
                 else
                 else
                 {
                 {
-                    _SatelliteContent = new SharpGLTF.Memory.MemoryImage(context.ReadAllBytesToEnd(_uri), _uri);
+                    var data = context.ReadAllBytesToEnd(satelliteUri);
+                    _SatelliteContent = new SharpGLTF.Memory.MemoryImage(data, satelliteUri);
                 }
                 }
             }
             }
 
 
@@ -236,9 +239,8 @@ namespace SharpGLTF.Schema2
             }            
             }            
 
 
             satelliteUri = writer.WriteImage(satelliteUri, imimg);
             satelliteUri = writer.WriteImage(satelliteUri, imimg);
-
-            satelliteUri = satelliteUri.Replace("\\", "/", StringComparison.Ordinal);
-            _uri = satelliteUri._EscapeStringInternal();
+            
+            _uri = UriKind.RelativeOrAbsolute.ToUri(satelliteUri).OriginalString;
             _mimeType = null;
             _mimeType = null;
         }
         }
 
 
@@ -331,16 +333,19 @@ namespace SharpGLTF.Schema2
         }
         }
 
 
         /// <summary>
         /// <summary>
-        /// Transfers all the <see cref="ModelRoot.LogicalImages"/> content into <see cref="BufferView"/> instances
+        /// Transfers all the <see cref="LogicalImages"/> content into <see cref="BufferView"/> instances
         /// </summary>
         /// </summary>
         /// <remarks>
         /// <remarks>
-        /// Images can be stored in three different ways:
-        /// - As satellite files.
-        /// - Embedded as MIME64 into the JSON document
-        /// - Referenced with <see cref="BufferView"/>
-        ///
-        /// This call ensures all images will be internalized as <see cref="BufferView"/> instances.
-        ///
+        /// Images can be stored in three different ways:<br/>
+        /// - As satellite files.<br/>
+        /// - Embedded as MIME64 into the JSON document<br/>
+        /// - Referenced with <see cref="BufferView"/><br/>
+        /// </remarks>
+        /// <remarms>
+        /// This call ensures all images will be internalized as <see cref="BufferView"/> instances.<br/>
+        /// To write a GLB file, it is advised to call <see cref="MergeImages"/> before <see cref="MergeBuffers()"/>
+        /// </remarms>        
+        /// <remarks>
         /// This action cannot be reversed.
         /// This action cannot be reversed.
         /// </remarks>
         /// </remarks>
         public void MergeImages()
         public void MergeImages()

+ 1 - 1
tests/SharpGLTF.Core.Tests/IO/JsonContentTests.cs

@@ -199,7 +199,7 @@ namespace SharpGLTF.IO
             Assert.That(roundtripJson, Does.Contain("你好"));
             Assert.That(roundtripJson, Does.Contain("你好"));
             
             
             // https://github.com/KhronosGroup/glTF/issues/1978#issuecomment-831744624
             // https://github.com/KhronosGroup/glTF/issues/1978#issuecomment-831744624
-            Assert.That(roundtripJson, Does.Contain("extended%20%E4%BD%A0%E5%A5%BD%20characters.png"));
+            // Assert.That(roundtripJson, Does.Contain("extended%20%E4%BD%A0%E5%A5%BD%20characters.png"));
 
 
             Assert.That(roundtripModel.LogicalImages[0].Content.IsPng, Is.True);
             Assert.That(roundtripModel.LogicalImages[0].Content.IsPng, Is.True);
         }
         }

Những thai đổi đã bị hủy bỏ vì nó quá lớn
+ 18 - 0
tests/SharpGLTF.ThirdParty.Tests/xPawTests.cs


Một số tệp đã không được hiển thị bởi vì quá nhiều tập tin thay đổi trong này khác