Browse Source

fix for loading relative paths, added regression test

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

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

@@ -34,6 +34,7 @@ namespace SharpGLTF.Schema2
             return new ReadContext(callback);
             return new ReadContext(callback);
         }
         }
 
 
+        [Obsolete("Use CreateFromDirectory")]
         public static ReadContext CreateFromFile(string filePath)
         public static ReadContext CreateFromFile(string filePath)
         {
         {
             Guard.NotNull(filePath, nameof(filePath));
             Guard.NotNull(filePath, nameof(filePath));

+ 13 - 2
src/SharpGLTF.Core/Schema2/Serialization.ReadSettings.cs

@@ -90,9 +90,13 @@ namespace SharpGLTF.Schema2
 
 
         public static Validation.ValidationResult Validate(string filePath)
         public static Validation.ValidationResult Validate(string filePath)
         {
         {
+            Guard.NotNull(filePath, nameof(filePath));
             Guard.FilePathMustExist(filePath, nameof(filePath));
             Guard.FilePathMustExist(filePath, nameof(filePath));
 
 
-            var context = ReadContext.CreateFromFile(filePath);
+            var dir = Path.GetDirectoryName(filePath);
+            filePath = System.IO.Path.GetFileName(filePath);
+
+            var context = ReadContext.CreateFromDirectory(dir);
 
 
             return context.Validate(filePath);
             return context.Validate(filePath);
         }
         }
@@ -114,10 +118,17 @@ namespace SharpGLTF.Schema2
         /// </remarks>
         /// </remarks>
         public static MODEL Load(string filePath, ReadSettings settings = null)
         public static MODEL Load(string filePath, ReadSettings settings = null)
         {
         {
+            Guard.NotNull(filePath, nameof(filePath));
+
             if (!(settings is ReadContext context))
             if (!(settings is ReadContext context))
             {
             {
+                Guard.FilePathMustExist(filePath, nameof(filePath));
+
+                var dir = Path.GetDirectoryName(filePath);
+                filePath = System.IO.Path.GetFileName(filePath);
+
                 context = ReadContext
                 context = ReadContext
-                    .CreateFromFile(filePath)
+                    .CreateFromDirectory(dir)
                     .WithSettingsFrom(settings);
                     .WithSettingsFrom(settings);
             }
             }
 
 

+ 19 - 0
tests/Assets/SpecialCases/RelativePaths.gltf

@@ -0,0 +1,19 @@
+{
+  "asset": {
+    "version": "2.0"
+  },
+  "images": [
+    {
+      "uri": "RelativePaths.jpg"
+    },
+    {
+      "uri": "Textures/Texture1.jpg"
+    },
+    {
+      "uri": "Textures\\Texture1.jpg"
+    },
+    {
+      "uri": "Textures%5CTexture1.jpg"
+    }
+  ]
+}

BIN
tests/Assets/SpecialCases/RelativePaths.jpg


BIN
tests/Assets/SpecialCases/Textures/Texture1.jpg


+ 44 - 0
tests/SharpGLTF.Core.Tests/Schema2/LoadAndSave/RegressionTests.cs

@@ -12,6 +12,50 @@ namespace SharpGLTF.Schema2.LoadAndSave
 {
 {
     internal class RegressionTests
     internal class RegressionTests
     {
     {
+        [Test]
+        public void LoadWithRelativeAndAbsolutePath()
+        {
+            // store current directory
+
+            var cdir = Environment.CurrentDirectory;
+
+            var modelPath = System.IO.Path.Combine(TestContext.CurrentContext.TestDirectory, "Assets\\SpecialCases\\RelativePaths.gltf");
+
+            // absolute path            
+
+            var model1 = ModelRoot.Load(modelPath, Validation.ValidationMode.TryFix);
+            Assert.NotNull(model1);
+            Assert.AreEqual(4, model1.LogicalImages.Count);
+
+            TestContext.WriteLine(string.Join("   ", ModelRoot.GetSatellitePaths(modelPath)));
+
+            // local path            
+
+            Environment.CurrentDirectory = System.IO.Path.GetDirectoryName(modelPath);
+            var modelFile = System.IO.Path.GetFileName(modelPath);
+
+            var model2 = ModelRoot.Load(modelFile, Validation.ValidationMode.TryFix);
+            Assert.NotNull(model2);
+            Assert.AreEqual(4, model2.LogicalImages.Count);
+
+            TestContext.WriteLine(string.Join("   ", ModelRoot.GetSatellitePaths(modelPath)));
+
+            // relative path:
+
+            modelFile = System.IO.Path.Combine(System.IO.Path.GetFileName(Environment.CurrentDirectory), modelFile);
+            Environment.CurrentDirectory = System.IO.Path.GetDirectoryName(Environment.CurrentDirectory);
+
+            var model3 = ModelRoot.Load(modelFile, Validation.ValidationMode.TryFix);
+            Assert.NotNull(model3);
+            Assert.AreEqual(4, model3.LogicalImages.Count);
+
+            TestContext.WriteLine(string.Join("   ", ModelRoot.GetSatellitePaths(modelPath)));
+
+            // restore current directory
+
+            Environment.CurrentDirectory = cdir;
+        }
+
         [Test]
         [Test]
         public void LoadSuzanneTest()
         public void LoadSuzanneTest()
         {
         {