Beispiel #1
0
        /// <summary>
        /// Extracts the <see cref="MeshModelMetadata"/> from the specified <paramref name="stream"/>.
        /// </summary>
        /// <remarks>
        /// The stream must contain a meshmodel file which has been created with the <see cref="MeshModel.Serialize(Stream)"/> method.
        /// </remarks>
        public static MeshModelMetadata ExtractFromStream(Stream stream)
        {
            if (stream == null)
            {
                throw new ArgumentNullException(nameof(stream));
            }

            using (var zipFile = new ZipArchive(stream.CanSeek ? stream : new MemoryStream(MeshModelHelper.StreamToArray(stream)), ZipArchiveMode.Read))
                using (var entryStream = zipFile.GetEntry("Metadata.xml").Open())
                {
                    var result = ReadFrom(entryStream);

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

                    return(result);
                }
        }
Beispiel #2
0
        /// <summary>
        /// Reads the data from the specified <paramref name="subFolder"/> in the specified <paramref name="zipFile"/> and deserializes the data found in it.
        /// If no <paramref name="subFolder"/> is specified, the method deserializes the data in the <paramref name="zipFile"/>s root directory.
        /// </summary>
        public static MeshModelPart Deserialize(ZipArchive zipFile, string subFolder = "")
        {
            var result        = new MeshModelPart(new MeshModelMetadata());
            var fileVersion10 = new Version(1, 0, 0, 0);

            // Metadaten lesen
            using (var entryStream = zipFile.GetEntry(Path.Combine(subFolder, "Metadata.xml")).Open())
            {
                result.Metadata = MeshModelMetadata.ReadFrom(entryStream);
            }

            // Versionscheck
            var fileVersion = result.Metadata.FileVersion;

            if (fileVersion < fileVersion10)
            {
                throw new InvalidOperationException(MeshModelHelper.GetResource <MeshModel>("OldFileVersionError_Text"));
            }

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

            // Vorschaubild lesen
            var thumbnailEntry = zipFile.GetEntry(Path.Combine(subFolder, "PreviewImage.png"));

            if (thumbnailEntry != null)
            {
                using (var entryStream = thumbnailEntry.Open())
                {
                    result._Thumbnail = MeshModelHelper.StreamToArray(entryStream);
                }
            }

            // Triangulierungsdaten lesen
            using (var binaryReader = new BinaryReader(zipFile.GetEntry(Path.Combine(subFolder, "Meshes.dat")).Open()))
            {
                var meshCount = binaryReader.ReadInt32();

                var meshes = new List <Mesh>(meshCount);
                for (var i = 0; i < meshCount; i++)
                {
                    meshes.Add(Mesh.Read(binaryReader, i, fileVersion));
                }
                result.Meshes = meshes.ToArray();
            }

            // Edgedaten lesen
            using (var binaryReader = new BinaryReader(zipFile.GetEntry(Path.Combine(subFolder, "Edges.dat")).Open()))
            {
                var edgeCount = binaryReader.ReadInt32();

                var edges = new List <Edge>(edgeCount);
                for (var i = 0; i < edgeCount; i++)
                {
                    edges.Add(Edge.Read(binaryReader, fileVersion));
                }
                // we don't try to create a single-point edge
                result.Edges = edges.Where(e => e.Points?.Length > 3).ToArray();
            }

            // Datenwerte lesen
            var meshValueList = new List <MeshValueList>(result.Metadata.MeshValueEntries?.Length ?? 0);

            foreach (var entry in result.Metadata.MeshValueEntries ?? new MeshValueEntry[0])
            {
                using (var binaryReader = new BinaryReader(zipFile.GetEntry(Path.Combine(subFolder, entry.Filename)).Open()))
                {
                    meshValueList.Add(MeshValueList.Read(binaryReader, entry, fileVersion));
                }
            }
            result.MeshValues = meshValueList.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))
            {
                result.Metadata.Guid = MeshModelGuidGenerator.GenerateGuids(zipFile.Entries.Where(e => string.Equals(Path.GetDirectoryName(e.FullName), subFolder, StringComparison.OrdinalIgnoreCase)));
            }

            return(result);
        }
Beispiel #3
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);
        }