Skip to content

Commit

Permalink
Add arbitrary file extension
Browse files Browse the repository at this point in the history
Allows the user to choose a main file extension for the methods SaveGLB, SaveGLTF instead of always forcing the default ones.
  • Loading branch information
gentilinigm committed Jan 17, 2024
1 parent 7744bac commit ec39f0f
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 28 deletions.
66 changes: 40 additions & 26 deletions src/SharpGLTF.Core/Schema2/Serialization.WriteContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -187,22 +187,25 @@ public string WriteImage(string assetName, MemoryImage image)
return callback(this, assetName, image);
}

/// <summary>
/// Writes <paramref name="model"/> to this context using the glTF json container.
/// </summary>
/// <param name="baseName">The base name to use for asset files, without extension.</param>
/// <param name="model">The <see cref="MODEL"/> to write.</param>
/// <remarks>
/// If the model has associated resources like binary assets and textures,<br/>
/// these additional resources will be also written as associated files using the pattern:<br/>
/// <br/>
/// "<paramref name="baseName"/>.{Number}.bin|png|jpg|dds"
/// </remarks>
public void WriteTextSchema2(string baseName, MODEL model)
/// <summary>
/// Writes <paramref name="model"/> to this context using the glTF json container.
/// </summary>
/// <param name="name">The name to use for asset files. It is possible to specify a custom extension for the main file</param>
/// <param name="model">The <see cref="MODEL"/> to write.</param>
/// <remarks>
/// The main file extension will be .gltf if none is provided.<br/>
/// <br/>
/// If the model has associated resources like binary assets and textures,<br/>
/// these additional resources will be also written as associated files using the pattern:<br/>
/// <br/>
/// "<paramref name="name"/>.{Number}.bin|png|jpg|dds"<br/>
/// where <paramref name="name"/> is used without extension.
/// </remarks>
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));

Expand All @@ -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);
Expand All @@ -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();
}

/// <summary>
/// Writes <paramref name="model"/> to this context using the GLB binary container.
/// </summary>
/// <param name="baseName">The base name to use for asset files, without extension.</param>
/// <param name="model">The <see cref="MODEL"/> to write.</param>
public void WriteBinarySchema2(string baseName, MODEL model)
/// <summary>
/// Writes <paramref name="model"/> to this context using the GLB binary container.
/// </summary>
/// <param name="name">The name to use for asset files. It is possible to specify a custom extension for the main file</param>
/// <param name="model">The <see cref="MODEL"/> to write.</param>
/// <remarks>
/// The file extension will be .glb if none is provided.
/// </remarks>
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));

Expand All @@ -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);
Expand All @@ -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();
Expand Down
4 changes: 2 additions & 2 deletions src/SharpGLTF.Core/Schema2/Serialization.WriteSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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();

Expand Down

0 comments on commit ec39f0f

Please sign in to comment.