55using System . Text ;
66
77using Newtonsoft . Json ;
8+
89using BYTES = System . ArraySegment < byte > ;
910
1011namespace SharpGLTF . Schema2
1112{
1213 using MODEL = ModelRoot ;
14+ using VALIDATIONMODE = Validation . ValidationMode ;
1315
1416 /// <summary>
1517 /// Callback used for loading associated files of current model.
@@ -46,7 +48,10 @@ internal ReadSettings(string filePath)
4648 /// </summary>
4749 public AssetReader FileReader { get ; set ; }
4850
49- public Boolean SkipValidation { get ; set ; }
51+ /// <summary>
52+ /// Gets or sets a value indicating the level of validation applied when loading a file.
53+ /// </summary>
54+ public VALIDATIONMODE Validation { get ; set ; } = VALIDATIONMODE . Strict ;
5055 }
5156
5257 partial class ModelRoot
@@ -84,13 +89,16 @@ public static Validation.ValidationResult Validate(string filePath)
8489 /// Reads a <see cref="MODEL"/> instance from a path pointing to a GLB or a GLTF file
8590 /// </summary>
8691 /// <param name="filePath">A valid file path.</param>
92+ /// <param name="vmode">Defines the file validation level.</param>
8793 /// <returns>A <see cref="MODEL"/> instance.</returns>
88- public static MODEL Load ( string filePath )
94+ public static MODEL Load ( string filePath , VALIDATIONMODE vmode = VALIDATIONMODE . Strict )
8995 {
9096 Guard . FilePathMustExist ( filePath , nameof ( filePath ) ) ;
9197
9298 var settings = new ReadSettings ( filePath ) ;
9399
100+ settings . Validation = vmode ;
101+
94102 using ( var s = File . OpenRead ( filePath ) )
95103 {
96104 return Read ( s , settings ) ;
@@ -182,14 +190,16 @@ public static MODEL ParseGLTF(String jsonContent, ReadSettings settings)
182190 return mv . Model ;
183191 }
184192
185- public static MODEL ReadFromDictionary ( Dictionary < string , BYTES > files , string fileName )
193+ public static MODEL ReadFromDictionary ( Dictionary < string , BYTES > files , string fileName , VALIDATIONMODE vmode = VALIDATIONMODE . Strict )
186194 {
187195 Guard . NotNull ( files , nameof ( files ) ) ;
188196
189197 var jsonBytes = files [ fileName ] ;
190198
191199 var settings = new ReadSettings ( fn => files [ fn ] ) ;
192200
201+ settings . Validation = vmode ;
202+
193203 using ( var m = new MemoryStream ( jsonBytes . Array , jsonBytes . Offset , jsonBytes . Count ) )
194204 {
195205 using ( var tr = new StreamReader ( m ) )
@@ -248,11 +258,11 @@ private static (MODEL Model, Validation.ValidationResult Validation) _Read(TextR
248258 Guard . NotNull ( textReader , nameof ( textReader ) ) ;
249259 Guard . NotNull ( settings , nameof ( settings ) ) ;
250260
261+ var root = new MODEL ( ) ;
262+ var vcontext = new Validation . ValidationResult ( root , settings . Validation ) ;
263+
251264 using ( var reader = new JsonTextReader ( textReader ) )
252265 {
253- var root = new MODEL ( ) ;
254- var vcontext = new Validation . ValidationResult ( root ) ;
255-
256266 if ( ! reader . Read ( ) )
257267 {
258268 vcontext . AddError ( new Validation . ModelException ( root , "Json is empty" ) ) ;
@@ -268,36 +278,36 @@ private static (MODEL Model, Validation.ValidationResult Validation) _Read(TextR
268278 vcontext . AddError ( new Validation . SchemaException ( root , rex ) ) ;
269279 return ( null , vcontext ) ;
270280 }
281+ }
271282
272- // schema validation
273-
274- root . ValidateReferences ( vcontext . GetContext ( root ) ) ;
275- var ex = vcontext . Errors . FirstOrDefault ( ) ;
276- if ( ex != null ) return ( null , vcontext ) ;
283+ // schema validation
277284
278- // resolve external references
285+ root . ValidateReferences ( vcontext . GetContext ( root ) ) ;
286+ var ex = vcontext . Errors . FirstOrDefault ( ) ;
287+ if ( ex != null ) return ( null , vcontext ) ;
279288
280- foreach ( var buffer in root . _buffers )
281- {
282- buffer . _ResolveUri ( settings . FileReader ) ;
283- }
289+ // resolve external references
284290
285- foreach ( var image in root . _images )
286- {
287- image . _ResolveUri ( settings . FileReader ) ;
288- }
291+ foreach ( var buffer in root . _buffers )
292+ {
293+ buffer . _ResolveUri ( settings . FileReader ) ;
294+ }
289295
290- // full validation
296+ foreach ( var image in root . _images )
297+ {
298+ image . _ResolveUri ( settings . FileReader ) ;
299+ }
291300
292- if ( ! settings . SkipValidation )
293- {
294- root . Validate ( vcontext . GetContext ( root ) ) ;
295- ex = vcontext . Errors . FirstOrDefault ( ) ;
296- if ( ex != null ) return ( null , vcontext ) ;
297- }
301+ // full validation
298302
299- return ( root , vcontext ) ;
303+ if ( settings . Validation != VALIDATIONMODE . Skip )
304+ {
305+ root . Validate ( vcontext . GetContext ( root ) ) ;
306+ ex = vcontext . Errors . FirstOrDefault ( ) ;
307+ if ( ex != null ) return ( null , vcontext ) ;
300308 }
309+
310+ return ( root , vcontext ) ;
301311 }
302312
303313 #endregion
0 commit comments