diff --git a/src/SharpGLTF.Core/Schema2/Serialization.WriteContext.cs b/src/SharpGLTF.Core/Schema2/Serialization.WriteContext.cs index 1e79607d..e01a02d6 100644 --- a/src/SharpGLTF.Core/Schema2/Serialization.WriteContext.cs +++ b/src/SharpGLTF.Core/Schema2/Serialization.WriteContext.cs @@ -187,22 +187,25 @@ public string WriteImage(string assetName, MemoryImage image) return callback(this, assetName, image); } - /// - /// Writes to this context using the glTF json container. - /// - /// The base name to use for asset files, without extension. - /// The to write. - /// - /// If the model has associated resources like binary assets and textures,
- /// these additional resources will be also written as associated files using the pattern:
- ///
- /// ".{Number}.bin|png|jpg|dds" - ///
- public void WriteTextSchema2(string baseName, MODEL model) + /// + /// Writes to this context using the glTF json container. + /// + /// The name to use for asset files. It is possible to specify a custom extension for the main file + /// The to write. + /// + /// The main file extension will be .gltf if none is provided.
+ ///
+ /// If the model has associated resources like binary assets and textures,
+ /// these additional resources will be also written as associated files using the pattern:
+ ///
+ /// ".{Number}.bin|png|jpg|dds"
+ /// where is used without extension. + ///
+ public void WriteTextSchema2(string name, MODEL model) { - Guard.NotNullOrEmpty(baseName, nameof(baseName)); - Guard.FilePathMustBeValid(baseName, nameof(baseName)); - if (System.IO.Path.IsPathRooted(baseName)) throw new ArgumentException("path must be relative", nameof(baseName)); + Guard.NotNullOrEmpty(name, nameof(name)); + Guard.FilePathMustBeValid(name, nameof(name)); + if (System.IO.Path.IsPathRooted(name)) throw new ArgumentException("path must be relative", nameof(name)); Guard.NotNull(model, nameof(model)); @@ -212,6 +215,8 @@ public void WriteTextSchema2(string baseName, MODEL model) model = this._PreprocessSchema2(model, mergeImages, this.MergeBuffers, this.BuffersMaxSize); Guard.NotNull(model, nameof(model)); + var baseName = Path.GetFileNameWithoutExtension(name); + model._PrepareBuffersForSatelliteWriting(this, baseName); model._PrepareImagesForWriting(this, baseName, false, ResourceWriteMode.SatelliteFile); @@ -222,22 +227,27 @@ public void WriteTextSchema2(string baseName, MODEL model) { model._WriteJSON(m, this.JsonOptions, this.JsonPostprocessor); - WriteAllBytesToEnd($"{baseName}.gltf", m.ToArraySegment()); + string finalName = Path.HasExtension(name) ? name : $"{name}.gltf"; + + WriteAllBytesToEnd(finalName, m.ToArraySegment()); } model._AfterWriting(); } - /// - /// Writes to this context using the GLB binary container. - /// - /// The base name to use for asset files, without extension. - /// The to write. - public void WriteBinarySchema2(string baseName, MODEL model) + /// + /// Writes to this context using the GLB binary container. + /// + /// The name to use for asset files. It is possible to specify a custom extension for the main file + /// The to write. + /// + /// The file extension will be .glb if none is provided. + /// + public void WriteBinarySchema2(string name, MODEL model) { - Guard.NotNullOrEmpty(baseName, nameof(baseName)); - Guard.FilePathMustBeValid(baseName, nameof(baseName)); - if (System.IO.Path.IsPathRooted(baseName)) throw new ArgumentException("path must be relative", nameof(baseName)); + Guard.NotNullOrEmpty(name, nameof(name)); + Guard.FilePathMustBeValid(name, nameof(name)); + if (System.IO.Path.IsPathRooted(name)) throw new ArgumentException("path must be relative", nameof(name)); Guard.NotNull(model, nameof(model)); @@ -254,6 +264,8 @@ public void WriteBinarySchema2(string baseName, MODEL model) model._PrepareBuffersForInternalWriting(); + var baseName = Path.GetFileNameWithoutExtension(name); + model._PrepareImagesForWriting(this, baseName, true, ResourceWriteMode.BufferView); _ValidateBeforeWriting(model); @@ -265,7 +277,9 @@ public void WriteBinarySchema2(string baseName, MODEL model) _BinarySerialization.WriteBinaryModel(w, model); } - WriteAllBytesToEnd($"{baseName}.glb", m.ToArraySegment()); + string finalName = Path.HasExtension(name) ? name : $"{name}.glb"; + + WriteAllBytesToEnd(finalName, m.ToArraySegment()); } model._AfterWriting(); diff --git a/src/SharpGLTF.Core/Schema2/Serialization.WriteSettings.cs b/src/SharpGLTF.Core/Schema2/Serialization.WriteSettings.cs index 5fe89a4b..31a1c1e8 100644 --- a/src/SharpGLTF.Core/Schema2/Serialization.WriteSettings.cs +++ b/src/SharpGLTF.Core/Schema2/Serialization.WriteSettings.cs @@ -186,7 +186,7 @@ public void SaveGLB(string filePath, WriteSettings settings = null) filePath = finfo.Name; } - var name = Path.GetFileNameWithoutExtension(filePath); + var name = Path.GetFileName(filePath); context.WithBinarySettings(); @@ -216,7 +216,7 @@ public void SaveGLTF(string filePath, WriteSettings settings = null) filePath = finfo.Name; } - var name = Path.GetFileNameWithoutExtension(filePath); + var name = Path.GetFileName(filePath); context.WithTextSettings();