Skip to content

Commit 3a8bc54

Browse files
authored
Merge pull request #1 from vpenades/master
WIP: Refactoring loader API
2 parents c6d3aea + a776c2e commit 3a8bc54

File tree

14 files changed

+321
-167
lines changed

14 files changed

+321
-167
lines changed

src/SharpGLTF.Core/Memory/MemoryAccessor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ public static MemoryAccessInfo[] Slice(MemoryAccessInfo[] attributes, int start,
198198

199199
#endregion
200200

201-
#region types
201+
#region nested types
202202

203203
private static int _GetSortingScore(string attribute)
204204
{

src/SharpGLTF.Core/Schema2/gltf.Accessors.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -456,7 +456,7 @@ internal void ValidateNormals(Validation.ValidationContext result)
456456

457457
for (int i = 0; i < normals.Count; ++i)
458458
{
459-
if (result.TryFixUnitLength(i, normals[i]))
459+
if (result.TryFixUnitLengthOrError(i, normals[i]))
460460
{
461461
normals[i] = normals[i].SanitizeNormal();
462462
}
@@ -476,7 +476,7 @@ internal void ValidateTangents(Validation.ValidationContext result)
476476

477477
for (int i = 0; i < tangents.Count; ++i)
478478
{
479-
if (result.TryFixTangent(i, tangents[i]))
479+
if (result.TryFixTangentOrError(i, tangents[i]))
480480
{
481481
tangents[i] = tangents[i].SanitizeTangent();
482482
}
@@ -523,7 +523,7 @@ internal void ValidateMatrices(Validation.ValidationContext result)
523523
{
524524
result = result.GetContext(this);
525525

526-
SourceBufferView.ValidateBufferUsageData(result);
526+
SourceBufferView.ValidateBufferUsagePlainData(result);
527527
result.CheckLinkMustBeAnyOf(nameof(Dimensions), Dimensions, DimensionType.MAT4);
528528

529529
var matrices = this.AsMatrix4x4Array();
@@ -536,13 +536,13 @@ internal void ValidateMatrices(Validation.ValidationContext result)
536536

537537
internal void ValidateAnimationInput(Validation.ValidationContext result)
538538
{
539-
SourceBufferView.ValidateBufferUsageData(result);
539+
SourceBufferView.ValidateBufferUsagePlainData(result);
540540
result.CheckLinkMustBeAnyOf(nameof(Dimensions), Dimensions, DimensionType.SCALAR);
541541
}
542542

543543
internal void ValidateAnimationOutput(Validation.ValidationContext result)
544544
{
545-
SourceBufferView.ValidateBufferUsageData(result);
545+
SourceBufferView.ValidateBufferUsagePlainData(result);
546546
result.CheckLinkMustBeAnyOf(nameof(Dimensions), Dimensions, DimensionType.SCALAR, DimensionType.VEC3, DimensionType.VEC4);
547547
}
548548

src/SharpGLTF.Core/Schema2/gltf.Buffer.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,18 +44,18 @@ internal Buffer(Byte[] content)
4444
const string EMBEDDEDOCTETSTREAM = "data:application/octet-stream;base64,";
4545
const string EMBEDDEDGLTFBUFFER = "data:application/gltf-buffer;base64,";
4646

47-
internal void _ResolveUri(AssetReader satelliteReferenceSolver)
47+
internal void _ResolveUri(ReadContext context)
4848
{
49-
_Content = _LoadBinaryBufferUnchecked(_uri, satelliteReferenceSolver);
49+
_Content = _LoadBinaryBufferUnchecked(_uri, context);
5050

5151
_uri = null; // When _Data is not empty, clear URI
5252
}
5353

54-
private static Byte[] _LoadBinaryBufferUnchecked(string uri, AssetReader satelliteReferenceSolver)
54+
private static Byte[] _LoadBinaryBufferUnchecked(string uri, ReadContext context)
5555
{
5656
return uri._TryParseBase64Unchecked(EMBEDDEDGLTFBUFFER)
5757
?? uri._TryParseBase64Unchecked(EMBEDDEDOCTETSTREAM)
58-
?? satelliteReferenceSolver.Invoke(uri).ToArray();
58+
?? context.ReadBytes(uri).ToArray();
5959
}
6060

6161
#endregion

src/SharpGLTF.Core/Schema2/gltf.BufferView.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,8 @@ internal static void CheckAccess(Validation.ValidationContext result, BufferView
201201
if (bv.IsIndexBuffer)
202202
{
203203
if (dim != DimensionType.SCALAR) result.AddLinkError(("BufferView", bv.LogicalIndex), $"is an IndexBuffer, but accessor dimensions is: {dim}");
204+
205+
// TODO: these could by fixed by replacing BYTE by UBYTE, SHORT by USHORT, etc
204206
if (enc == EncodingType.BYTE) result.AddLinkError(("BufferView", bv.LogicalIndex), $"is an IndexBuffer, but accessor encoding is (s)byte");
205207
if (enc == EncodingType.SHORT) result.AddLinkError(("BufferView", bv.LogicalIndex), $"is an IndexBuffer, but accessor encoding is (s)short");
206208
if (enc == EncodingType.FLOAT) result.AddLinkError(("BufferView", bv.LogicalIndex), $"is an IndexBuffer, but accessor encoding is float");
@@ -264,11 +266,11 @@ internal void ValidateBufferUsageGPU(Validation.ValidationContext result, Buffer
264266
result.AddLinkError("Device Buffer Target", $"is set as {this._target.Value}. But an accessor wants to use it as '{usingMode}'.");
265267
}
266268

267-
internal void ValidateBufferUsageData(Validation.ValidationContext result)
269+
internal void ValidateBufferUsagePlainData(Validation.ValidationContext result)
268270
{
269271
if (this._byteStride.HasValue)
270272
{
271-
if (result.TryFixLink("BufferView", "Unexpected ByteStride found. Expected null"))
273+
if (result.TryFixLinkOrError("BufferView", "Unexpected ByteStride found. Expected null"))
272274
{
273275
this._byteStride = null;
274276
}
@@ -278,7 +280,7 @@ internal void ValidateBufferUsageData(Validation.ValidationContext result)
278280

279281
if (!this._target.HasValue) return;
280282

281-
if (result.TryFixLink("Device Buffer Target", $"is set as {this._target.Value}. But an accessor wants to use it as a plain data buffer."))
283+
if (result.TryFixLinkOrError("Device Buffer Target", $"is set as {this._target.Value}. But an accessor wants to use it as a plain data buffer."))
282284
{
283285
this._target = null;
284286
}

src/SharpGLTF.Core/Schema2/gltf.Images.cs

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,16 @@ public string FileExtension
101101

102102
internal int _SourceBufferViewIndex => _bufferView.AsValue(-1);
103103

104+
internal bool _HasContent
105+
{
106+
get
107+
{
108+
if (_bufferView != null) return true;
109+
if (_SatelliteImageContent != null) return true;
110+
return false;
111+
}
112+
}
113+
104114
#endregion
105115

106116
#region API
@@ -168,9 +178,8 @@ public void SetSatelliteContent(Byte[] content)
168178

169179
if (imageType == null) throw new ArgumentException($"{nameof(content)} must be a PNG, JPG, DDS or WEBP image", nameof(content));
170180

171-
this._uri = null;
172-
this._mimeType = null;
173-
this._bufferView = null;
181+
_DiscardContent();
182+
174183
this._SatelliteImageContent = content;
175184
}
176185

@@ -196,24 +205,34 @@ public void TransferToInternalBuffer()
196205

197206
#region binary read
198207

199-
internal void _ResolveUri(AssetReader externalReferenceSolver)
208+
internal void _ResolveUri(ReadContext context)
200209
{
201210
if (!String.IsNullOrWhiteSpace(_uri))
202211
{
203-
_SatelliteImageContent = _LoadImageUnchecked(externalReferenceSolver, _uri);
212+
var data = _LoadImageUnchecked(context, _uri);
213+
214+
_SatelliteImageContent = data;
204215
_uri = null;
205216
_mimeType = null;
206217
}
207218
}
208219

209-
private static Byte[] _LoadImageUnchecked(AssetReader externalReferenceSolver, string uri)
220+
private static Byte[] _LoadImageUnchecked(ReadContext context, string uri)
210221
{
211222
return uri._TryParseBase64Unchecked(EMBEDDED_GLTF_BUFFER)
212223
?? uri._TryParseBase64Unchecked(EMBEDDED_OCTET_STREAM)
213224
?? uri._TryParseBase64Unchecked(EMBEDDED_JPEG_BUFFER)
214225
?? uri._TryParseBase64Unchecked(EMBEDDED_PNG_BUFFER)
215226
?? uri._TryParseBase64Unchecked(EMBEDDED_DDS_BUFFER)
216-
?? externalReferenceSolver.Invoke(uri).ToArray();
227+
?? context.ReadBytes(uri).ToArray();
228+
}
229+
230+
internal void _DiscardContent()
231+
{
232+
this._uri = null;
233+
this._mimeType = null;
234+
this._bufferView = null;
235+
this._SatelliteImageContent = null;
217236
}
218237

219238
#endregion

src/SharpGLTF.Core/Schema2/gltf.Root.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,15 @@ internal ModelRoot()
5454
public ModelRoot DeepClone()
5555
{
5656
var dict = new Dictionary<string, ArraySegment<Byte>>();
57-
var settings = WriteSettings.ForDeepClone(dict);
57+
var settings = WriteContext.ForDeepClone(dict);
5858

5959
System.Diagnostics.Debug.Assert(settings._NoCloneWatchdog, "invalid clone settings");
6060

6161
this.Write(settings, "deepclone");
6262

63-
return ModelRoot.ReadFromDictionary(dict, "deepclone.gltf");
63+
var context = ReadContext.CreateFromDictionary(dict);
64+
context.Validation = Validation.ValidationMode.Strict;
65+
return context._ReadFromDictionary("deepclone.gltf");
6466
}
6567

6668
#endregion

0 commit comments

Comments
 (0)