Browse Source

Added Icloneable interfaces

Vicente Penades 3 years ago
parent
commit
b128341314

+ 3 - 1
src/SharpGLTF.Core/IO/JsonContent.cs

@@ -18,7 +18,7 @@ namespace SharpGLTF.IO
     /// </remarks>
     /// </remarks>
     [System.ComponentModel.ImmutableObject(true)]
     [System.ComponentModel.ImmutableObject(true)]
     [System.Diagnostics.DebuggerDisplay("{ToDebuggerDisplay(),nq}")]
     [System.Diagnostics.DebuggerDisplay("{ToDebuggerDisplay(),nq}")]
-    public readonly struct JsonContent
+    public readonly struct JsonContent : ICloneable
     {
     {
         #region debug
         #region debug
 
 
@@ -60,6 +60,8 @@ namespace SharpGLTF.IO
                 _Content = null;
                 _Content = null;
         }
         }
 
 
+        Object ICloneable.Clone() { return new JsonContent(_Content); }
+
         public JsonContent DeepClone() { return new JsonContent(_Content); }
         public JsonContent DeepClone() { return new JsonContent(_Content); }
 
 
         #endregion
         #endregion

+ 23 - 22
src/SharpGLTF.Core/Memory/MemoryImage.cs

@@ -78,6 +78,29 @@ 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); }
 
 
+        /// <summary>
+        /// Tries to parse a Mime64 string to <see cref="MemoryImage"/>
+        /// </summary>
+        /// <param name="mime64content">The Mime64 string source.</param>
+        /// <param name="image">if decoding succeeds, it will contain the image file.</param>
+        /// <returns>true if decoding succeeded.</returns>
+        public static bool TryParseMime64(string mime64content, out MemoryImage image)
+        {
+            if (mime64content == null) { image = default; return false; }
+
+            var data = mime64content.TryParseBase64Unchecked(_EmbeddedHeaders);
+            if (data == null) { image = default; return false; }
+
+            if (mime64content.StartsWith(EMBEDDED_PNG_BUFFER, StringComparison.Ordinal) && !_IsPngImage(data)) throw new ArgumentException("Invalid PNG Content", nameof(mime64content));
+            if (mime64content.StartsWith(EMBEDDED_JPEG_BUFFER, StringComparison.Ordinal) && !_IsJpgImage(data)) throw new ArgumentException("Invalid JPG Content", nameof(mime64content));
+            if (mime64content.StartsWith(EMBEDDED_DDS_BUFFER, StringComparison.Ordinal) && !_IsDdsImage(data)) throw new ArgumentException("Invalid DDS Content", nameof(mime64content));
+            if (mime64content.StartsWith(EMBEDDED_WEBP_BUFFER, StringComparison.Ordinal) && !_IsWebpImage(data)) throw new ArgumentException("Invalid WEBP Content", nameof(mime64content));
+            if (mime64content.StartsWith(EMBEDDED_KTX2_BUFFER, StringComparison.Ordinal) && !_IsKtx2Image(data)) throw new ArgumentException("Invalid KTX2 Content", nameof(mime64content));
+
+            image = data;
+            return true;
+        }
+
         public MemoryImage(BYTES image)
         public MemoryImage(BYTES image)
         {
         {
             Guard.IsTrue(_IsImage(image), nameof(image), GuardError_MustBeValidImage);
             Guard.IsTrue(_IsImage(image), nameof(image), GuardError_MustBeValidImage);
@@ -336,28 +359,6 @@ namespace SharpGLTF.Memory
             return mimeContent + Convert.ToBase64String(_Image.Array, _Image.Offset, _Image.Count, Base64FormattingOptions.None);
             return mimeContent + Convert.ToBase64String(_Image.Array, _Image.Offset, _Image.Count, Base64FormattingOptions.None);
         }
         }
 
 
-        /// <summary>
-        /// Tries to parse a Mime64 string to a Byte array.
-        /// </summary>
-        /// <param name="mime64content">The Mime64 string source.</param>
-        /// <param name="data">if decoding succeeds, it will contain the decoded data</param>
-        /// <returns>true if decoding succeeded.</returns>
-        internal static bool TryParseMime64(string mime64content, out Byte[] data)
-        {
-            if (mime64content == null) { data = null; return false; }
-
-            data = mime64content.TryParseBase64Unchecked(_EmbeddedHeaders);
-            if (data == null) return false;
-
-            if (mime64content.StartsWith(EMBEDDED_PNG_BUFFER, StringComparison.Ordinal) && !_IsPngImage(data)) throw new ArgumentException("Invalid PNG Content", nameof(mime64content));
-            if (mime64content.StartsWith(EMBEDDED_JPEG_BUFFER, StringComparison.Ordinal) && !_IsJpgImage(data)) throw new ArgumentException("Invalid JPG Content", nameof(mime64content));
-            if (mime64content.StartsWith(EMBEDDED_DDS_BUFFER, StringComparison.Ordinal) && !_IsDdsImage(data)) throw new ArgumentException("Invalid DDS Content", nameof(mime64content));
-            if (mime64content.StartsWith(EMBEDDED_WEBP_BUFFER, StringComparison.Ordinal) && !_IsWebpImage(data)) throw new ArgumentException("Invalid WEBP Content", nameof(mime64content));
-            if (mime64content.StartsWith(EMBEDDED_KTX2_BUFFER, StringComparison.Ordinal) && !_IsKtx2Image(data)) throw new ArgumentException("Invalid KTX2 Content", nameof(mime64content));
-
-            return true;
-        }
-
         /// <summary>
         /// <summary>
         /// identifies an image of a specific type.
         /// identifies an image of a specific type.
         /// </summary>
         /// </summary>

+ 2 - 2
src/SharpGLTF.Core/Schema2/gltf.Images.cs

@@ -132,9 +132,9 @@ namespace SharpGLTF.Schema2
             if (String.IsNullOrWhiteSpace(_uri)) return;
             if (String.IsNullOrWhiteSpace(_uri)) return;
 
 
             // Try decode Base64 embedded image.
             // Try decode Base64 embedded image.
-            if (Memory.MemoryImage.TryParseMime64(_uri, out byte[] data))
+            if (Memory.MemoryImage.TryParseMime64(_uri, out var memImage))
             {
             {
-                _SatelliteContent = data;
+                _SatelliteContent = memImage;
             }
             }
 
 
             // Then it's a regular URI
             // Then it's a regular URI

+ 3 - 1
src/SharpGLTF.Toolkit/Materials/MaterialBuilder.cs

@@ -14,7 +14,7 @@ namespace SharpGLTF.Materials
     /// Represents the root object of a material instance structure.
     /// Represents the root object of a material instance structure.
     /// </summary>
     /// </summary>
     [System.Diagnostics.DebuggerDisplay("{_DebuggerDisplay(),nq}")]
     [System.Diagnostics.DebuggerDisplay("{_DebuggerDisplay(),nq}")]
-    public partial class MaterialBuilder : BaseBuilder
+    public partial class MaterialBuilder : BaseBuilder, ICloneable
     {
     {
         #region debug
         #region debug
 
 
@@ -52,6 +52,8 @@ namespace SharpGLTF.Materials
         public MaterialBuilder(string name = null)
         public MaterialBuilder(string name = null)
             : base(name) { }
             : base(name) { }
 
 
+        Object ICloneable.Clone() { return new MaterialBuilder(this); }
+
         public MaterialBuilder Clone() { return new MaterialBuilder(this); }
         public MaterialBuilder Clone() { return new MaterialBuilder(this); }
 
 
         public MaterialBuilder(MaterialBuilder other)
         public MaterialBuilder(MaterialBuilder other)