Skip to content

Commit ec39f0f

Browse files
committed
Add arbitrary file extension
Allows the user to choose a main file extension for the methods SaveGLB, SaveGLTF instead of always forcing the default ones.
1 parent 7744bac commit ec39f0f

File tree

2 files changed

+42
-28
lines changed

2 files changed

+42
-28
lines changed

src/SharpGLTF.Core/Schema2/Serialization.WriteContext.cs

Lines changed: 40 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -187,22 +187,25 @@ public string WriteImage(string assetName, MemoryImage image)
187187
return callback(this, assetName, image);
188188
}
189189

190-
/// <summary>
191-
/// Writes <paramref name="model"/> to this context using the glTF json container.
192-
/// </summary>
193-
/// <param name="baseName">The base name to use for asset files, without extension.</param>
194-
/// <param name="model">The <see cref="MODEL"/> to write.</param>
195-
/// <remarks>
196-
/// If the model has associated resources like binary assets and textures,<br/>
197-
/// these additional resources will be also written as associated files using the pattern:<br/>
198-
/// <br/>
199-
/// "<paramref name="baseName"/>.{Number}.bin|png|jpg|dds"
200-
/// </remarks>
201-
public void WriteTextSchema2(string baseName, MODEL model)
190+
/// <summary>
191+
/// Writes <paramref name="model"/> to this context using the glTF json container.
192+
/// </summary>
193+
/// <param name="name">The name to use for asset files. It is possible to specify a custom extension for the main file</param>
194+
/// <param name="model">The <see cref="MODEL"/> to write.</param>
195+
/// <remarks>
196+
/// The main file extension will be .gltf if none is provided.<br/>
197+
/// <br/>
198+
/// If the model has associated resources like binary assets and textures,<br/>
199+
/// these additional resources will be also written as associated files using the pattern:<br/>
200+
/// <br/>
201+
/// "<paramref name="name"/>.{Number}.bin|png|jpg|dds"<br/>
202+
/// where <paramref name="name"/> is used without extension.
203+
/// </remarks>
204+
public void WriteTextSchema2(string name, MODEL model)
202205
{
203-
Guard.NotNullOrEmpty(baseName, nameof(baseName));
204-
Guard.FilePathMustBeValid(baseName, nameof(baseName));
205-
if (System.IO.Path.IsPathRooted(baseName)) throw new ArgumentException("path must be relative", nameof(baseName));
206+
Guard.NotNullOrEmpty(name, nameof(name));
207+
Guard.FilePathMustBeValid(name, nameof(name));
208+
if (System.IO.Path.IsPathRooted(name)) throw new ArgumentException("path must be relative", nameof(name));
206209

207210
Guard.NotNull(model, nameof(model));
208211

@@ -212,6 +215,8 @@ public void WriteTextSchema2(string baseName, MODEL model)
212215
model = this._PreprocessSchema2(model, mergeImages, this.MergeBuffers, this.BuffersMaxSize);
213216
Guard.NotNull(model, nameof(model));
214217

218+
var baseName = Path.GetFileNameWithoutExtension(name);
219+
215220
model._PrepareBuffersForSatelliteWriting(this, baseName);
216221

217222
model._PrepareImagesForWriting(this, baseName, false, ResourceWriteMode.SatelliteFile);
@@ -222,22 +227,27 @@ public void WriteTextSchema2(string baseName, MODEL model)
222227
{
223228
model._WriteJSON(m, this.JsonOptions, this.JsonPostprocessor);
224229

225-
WriteAllBytesToEnd($"{baseName}.gltf", m.ToArraySegment());
230+
string finalName = Path.HasExtension(name) ? name : $"{name}.gltf";
231+
232+
WriteAllBytesToEnd(finalName, m.ToArraySegment());
226233
}
227234

228235
model._AfterWriting();
229236
}
230237

231-
/// <summary>
232-
/// Writes <paramref name="model"/> to this context using the GLB binary container.
233-
/// </summary>
234-
/// <param name="baseName">The base name to use for asset files, without extension.</param>
235-
/// <param name="model">The <see cref="MODEL"/> to write.</param>
236-
public void WriteBinarySchema2(string baseName, MODEL model)
238+
/// <summary>
239+
/// Writes <paramref name="model"/> to this context using the GLB binary container.
240+
/// </summary>
241+
/// <param name="name">The name to use for asset files. It is possible to specify a custom extension for the main file</param>
242+
/// <param name="model">The <see cref="MODEL"/> to write.</param>
243+
/// <remarks>
244+
/// The file extension will be .glb if none is provided.
245+
/// </remarks>
246+
public void WriteBinarySchema2(string name, MODEL model)
237247
{
238-
Guard.NotNullOrEmpty(baseName, nameof(baseName));
239-
Guard.FilePathMustBeValid(baseName, nameof(baseName));
240-
if (System.IO.Path.IsPathRooted(baseName)) throw new ArgumentException("path must be relative", nameof(baseName));
248+
Guard.NotNullOrEmpty(name, nameof(name));
249+
Guard.FilePathMustBeValid(name, nameof(name));
250+
if (System.IO.Path.IsPathRooted(name)) throw new ArgumentException("path must be relative", nameof(name));
241251

242252
Guard.NotNull(model, nameof(model));
243253

@@ -254,6 +264,8 @@ public void WriteBinarySchema2(string baseName, MODEL model)
254264

255265
model._PrepareBuffersForInternalWriting();
256266

267+
var baseName = Path.GetFileNameWithoutExtension(name);
268+
257269
model._PrepareImagesForWriting(this, baseName, true, ResourceWriteMode.BufferView);
258270

259271
_ValidateBeforeWriting(model);
@@ -265,7 +277,9 @@ public void WriteBinarySchema2(string baseName, MODEL model)
265277
_BinarySerialization.WriteBinaryModel(w, model);
266278
}
267279

268-
WriteAllBytesToEnd($"{baseName}.glb", m.ToArraySegment());
280+
string finalName = Path.HasExtension(name) ? name : $"{name}.glb";
281+
282+
WriteAllBytesToEnd(finalName, m.ToArraySegment());
269283
}
270284

271285
model._AfterWriting();

src/SharpGLTF.Core/Schema2/Serialization.WriteSettings.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ public void SaveGLB(string filePath, WriteSettings settings = null)
186186
filePath = finfo.Name;
187187
}
188188

189-
var name = Path.GetFileNameWithoutExtension(filePath);
189+
var name = Path.GetFileName(filePath);
190190

191191
context.WithBinarySettings();
192192

@@ -216,7 +216,7 @@ public void SaveGLTF(string filePath, WriteSettings settings = null)
216216
filePath = finfo.Name;
217217
}
218218

219-
var name = Path.GetFileNameWithoutExtension(filePath);
219+
var name = Path.GetFileName(filePath);
220220

221221
context.WithTextSettings();
222222

0 commit comments

Comments
 (0)