/// <summary>
        /// This needs to be called immediately before writing to json,
        /// but immediately after preprocessing and buffer setup, so the model can be correctly validated.
        /// </summary>
        /// <param name="model">The model to validate.</param>
        private void _ValidateBeforeWriting(MODEL model)
        {
            if (_NoCloneWatchdog)
            {
                return;
            }

            if (this.Validation == SharpGLTF.Validation.ValidationMode.Skip)
            {
                return;
            }

            var vcontext = new Validation.ValidationResult(model, this.Validation);

            model.ValidateReferences(vcontext.GetContext());
            var ex = vcontext.Errors.FirstOrDefault();

            if (ex != null)
            {
                throw ex;
            }

            model.ValidateContent(vcontext.GetContext());
            ex = vcontext.Errors.FirstOrDefault();
            if (ex != null)
            {
                throw ex;
            }
        }
示例#2
0
        private (MODEL Model, Validation.ValidationResult Validation) _Read(ReadOnlyMemory <Byte> jsonUtf8Bytes)
        {
            var root     = new MODEL();
            var vcontext = new Validation.ValidationResult(root, this.Validation);

            #if !SUPRESSTRYCATCH
            try {
            #endif

            if (jsonUtf8Bytes.IsEmpty)
            {
                throw new System.Text.Json.JsonException("JSon is empty.");
            }

            var reader = new Utf8JsonReader(jsonUtf8Bytes.Span);

            if (!reader.Read())
            {
                vcontext.SetError(new Validation.SchemaException(root, "Json is empty"));
                return(null, vcontext);
            }

            root.Deserialize(ref reader);
            root.OnDeserializationCompleted();

            // binary chunk check

            foreach (var b in root.LogicalBuffers)
            {
                b.OnValidateBinaryChunk(vcontext.GetContext(), this._BinaryChunk);
            }

            // schema validation

            if (this._CheckSupportedExtensions)
            {
                root._ValidateExtensions(vcontext.GetContext());
                var ex = vcontext.Errors.FirstOrDefault();
                if (ex != null)
                {
                    return(null, vcontext);
                }
            }

            // we must do a basic validation before resolving external dependencies

            if (true)
            {
                root.ValidateReferences(vcontext.GetContext());
                var ex = vcontext.Errors.FirstOrDefault();
                if (ex != null)
                {
                    return(null, vcontext);
                }
            }

            // resolve external dependencies

            root._ResolveSatelliteDependencies(this);

            // full validation

            if (this.Validation != VALIDATIONMODE.Skip)
            {
                root.ValidateContent(vcontext.GetContext());
                var ex = vcontext.Errors.FirstOrDefault();
                if (ex != null)
                {
                    return(null, vcontext);
                }
            }

            #if !SUPRESSTRYCATCH
        }

        catch (JsonException rex)
        {
            vcontext.SetError(new Validation.SchemaException(root, rex));
            return(null, vcontext);
        }
        catch (System.FormatException fex)
        {
            vcontext.SetError(new Validation.ModelException(null, fex));
            return(null, vcontext);
        }
        catch (ArgumentException aex)
        {
            vcontext.SetError(new Validation.ModelException(root, aex));
            return(null, vcontext);
        }
        catch (Validation.ModelException mex)
        {
            vcontext.SetError(mex);
            return(null, vcontext);
        }
            #endif

            return(root, vcontext);
        }