/// <summary> /// Prepares the model for writing with the appropiate settings, creating a defensive copy if neccesary. /// </summary> /// <param name="model">The source <see cref="SCHEMA2"/> instance.</param> /// <param name="imagesAsBufferViews">true if images should be stored as buffer views.</param> /// <param name="mergeBuffers">true if it's required the model must have a single buffer.</param> /// <returns>The source <see cref="SCHEMA2"/> instance, or a cloned and modified instance if current settings required it.</returns> private SCHEMA2 _PreprocessSchema2(SCHEMA2 model, bool imagesAsBufferViews, bool mergeBuffers) { Guard.NotNull(model, nameof(model)); foreach (var img in model.LogicalImages) { if (!img._HasContent) { throw new Validation.DataException(img, "Image Content is missing."); } } // check if we need to modify the model before saving it, // in order to create a defensive copy. if (model.LogicalImages.Count == 0) { imagesAsBufferViews = false; } if (model.LogicalBuffers.Count <= 1 && !imagesAsBufferViews) { mergeBuffers = false; } if (mergeBuffers | imagesAsBufferViews) { // cloning check is done to prevent cloning from entering in an infinite loop where each clone attempt triggers another clone request. if (_NoCloneWatchdog) { throw new InvalidOperationException($"Current settings require creating a densive copy before model modification, but calling {nameof(SCHEMA2.DeepClone)} is not allowed with the current settings."); } model = model.DeepClone(); } if (imagesAsBufferViews) { model.MergeImages(); } if (mergeBuffers) { model.MergeBuffers(); } if (this._UpdateSupportedExtensions) { model.UpdateExtensionsSupport(); } return(model); }