Browse Source

let WriteContext to be more flexible.

Vicente Penades 3 years ago
parent
commit
6700d778c3

+ 13 - 12
src/SharpGLTF.Core/Schema2/Serialization.WriteSettings.cs

@@ -152,8 +152,6 @@ namespace SharpGLTF.Schema2
         /// <param name="settings">Optional settings.</param>
         /// <param name="settings">Optional settings.</param>
         public void Save(string filePath, WriteSettings settings = null)
         public void Save(string filePath, WriteSettings settings = null)
         {
         {
-            Guard.FilePathMustBeValid(filePath, nameof(filePath));
-
             bool isGltfExtension = filePath
             bool isGltfExtension = filePath
                 .ToLower(System.Globalization.CultureInfo.InvariantCulture)
                 .ToLower(System.Globalization.CultureInfo.InvariantCulture)
                 .EndsWith(".gltf", StringComparison.OrdinalIgnoreCase);
                 .EndsWith(".gltf", StringComparison.OrdinalIgnoreCase);
@@ -169,12 +167,14 @@ namespace SharpGLTF.Schema2
         /// <param name="settings">Optional settings.</param>
         /// <param name="settings">Optional settings.</param>
         public void SaveGLB(string filePath, WriteSettings settings = null)
         public void SaveGLB(string filePath, WriteSettings settings = null)
         {
         {
-            Guard.FilePathMustBeValid(filePath, nameof(filePath));
+            if (!(settings is WriteContext context))
+            {
+                context = WriteContext
+                    .CreateFromFile(filePath)
+                    .WithSettingsFrom(settings);
+            }
 
 
-            var context = WriteContext
-                .CreateFromFile(filePath)
-                .WithBinarySettings()
-                .WithSettingsFrom(settings);
+            context.WithBinarySettings();
 
 
             var name = Path.GetFileNameWithoutExtension(filePath);
             var name = Path.GetFileNameWithoutExtension(filePath);
 
 
@@ -191,11 +191,12 @@ namespace SharpGLTF.Schema2
         /// </remarks>
         /// </remarks>
         public void SaveGLTF(string filePath, WriteSettings settings = null)
         public void SaveGLTF(string filePath, WriteSettings settings = null)
         {
         {
-            Guard.FilePathMustBeValid(filePath, nameof(filePath));
-
-            var context = WriteContext
-                .CreateFromFile(filePath)
-                .WithSettingsFrom(settings);
+            if (!(settings is WriteContext context))
+            {
+                context = WriteContext
+                    .CreateFromFile(filePath)
+                    .WithSettingsFrom(settings);
+            }
 
 
             var name = Path.GetFileNameWithoutExtension(filePath);
             var name = Path.GetFileNameWithoutExtension(filePath);
 
 

+ 26 - 0
tests/SharpGLTF.Core.Tests/Schema2/LoadAndSave/LoadSpecialModelsTest.cs

@@ -205,5 +205,31 @@ namespace SharpGLTF.Schema2.LoadAndSave
             model.AttachToCurrentTest("original.glb");
             model.AttachToCurrentTest("original.glb");
             editableScene.ToGltf2().AttachToCurrentTest("WithTangents.glb");
             editableScene.ToGltf2().AttachToCurrentTest("WithTangents.glb");
         }
         }
+
+
+        [Test]
+        public void LoadAndSaveToMemory()
+        {
+            var path = TestFiles.GetSampleModelsPaths().FirstOrDefault(item => item.EndsWith("Avocado.glb"));
+
+            var model = ModelRoot.Load(path);
+            // model.LogicalImages[0].TransferToSatelliteFile(); // TODO
+
+            // we will use this dictionary as our in-memory model container.
+            var dictionary = new Dictionary<string, ArraySegment<Byte>>();
+
+            // write to dictionary
+            var wcontext = WriteContext.CreateFromDictionary(dictionary);
+            model.Save("avocado.gltf", wcontext);
+            Assert.IsTrue(dictionary.ContainsKey("avocado.gltf"));
+            Assert.IsTrue(dictionary.ContainsKey("avocado.bin"));
+
+            // read back from dictionary
+            var rcontext = ReadContext.CreateFromDictionary(dictionary);
+            var model2 = ModelRoot.Load("avocado.gltf", rcontext);
+            
+            // TODO: verify
+            
+        }
     }
     }
 }
 }