예제 #1
0
 /// <summary>
 /// Creates a <see cref="MeshModel"/> that contains only this instance as <see cref="MeshModel.Parts"/>
 /// </summary>
 /// <returns></returns>
 public MeshModel ToMeshModel()
 {
     return(MeshModel.FromMeshModelPart(this));
 }
예제 #2
0
        /// <summary>
        /// Deserializes a <see cref="MeshModel"/> from the specified <paramref name="stream"/>
        /// </summary>
        public static MeshModel Deserialize(Stream stream)
        {
            MeshModel result;

            if (!stream.CanSeek)
            {
                stream = new MemoryStream(MeshModelHelper.StreamToArray(stream));
            }

            using (var zipFile = new ZipArchive(stream))
            {
                // Metadaten lesen
                MeshModelMetadata metadata;

                using (var entryStream = zipFile.GetEntry("Metadata.xml").Open())
                {
                    metadata = MeshModelMetadata.ReadFrom(entryStream);
                }

                var partCount = metadata.PartCount;

                // Versionscheck
                var fileVersion = metadata.FileVersion;
                if (fileVersion == null)
                {
                    throw new InvalidOperationException(MeshModelHelper.GetResource <MeshModel>("OldFileVersionError_Text"));
                }

                if (fileVersion.Major > MeshModelFileVersion.Major)
                {
                    throw new InvalidOperationException(MeshModelHelper.FormatResource <MeshModel>("FileVersionError_Text", fileVersion, MeshModelFileVersion));
                }

                // Vorschaubild lesen
                byte[] thumbnail = null;

                if (partCount != 1)
                {
                    var entry = zipFile.GetEntry("PreviewImage.png");
                    if (entry != null)
                    {
                        using (var entryStream = entry.Open())
                        {
                            thumbnail = MeshModelHelper.StreamToArray(entryStream);
                        }
                    }
                }

                // Modelldaten laden
                if (partCount == 1)
                {
                    result = new MeshModel(MeshModelPart.Deserialize(zipFile));
                }
                else
                {
                    var parts = new List <MeshModelPart>(partCount);
                    for (var i = 0; i < partCount; i += 1)
                    {
                        parts.Add(MeshModelPart.Deserialize(zipFile, i.ToString()));
                    }
                    result = new MeshModel(metadata, thumbnail, parts.ToArray());

                    // falls Guid noch nicht Teil der Metadaten war, dann hier stabile (und hoffentlich eindeutige) Guid den Metadaten zuweisen
                    if (fileVersion < new Version(3, 1, 0, 0))
                    {
                        metadata.Guid = MeshModelGuidGenerator.GenerateGuids(zipFile.Entries);
                    }
                }
            }
            return(result);
        }