コード例 #1
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);
        }
コード例 #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 fileVersion10 = new Version(1, 0, 0, 0);

            var result = new MeshModelPart(new MeshModelMetadata());

            result.Metadata = MeshModelMetadata.ExtractFrom(zipFile, subFolder);

            // Versionschecks
            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();

            return(result);
        }