private static void WriteHeader(XFile file, XofFileSaveObject saveObject)
        {
            byte[] data;

            using (var ms = new MemoryStream())
                using (var writer = new BinaryWriter(ms))
                {
                    writer.Write((short)file.FileVersion.Major);
                    writer.Write((short)file.FileVersion.Minor);
                    writer.Write(file.FileFlags);

                    data = ms.ToArray();
                }

            using (saveObject.AddData(XofFileDefaultTemplates.HeaderId, null, null, data))
            {
            }
        }
示例#2
0
        public static XFile FromStream(Stream stream)
        {
            if (stream == null)
            {
                throw new ArgumentNullException(nameof(stream));
            }

            byte[] bytes;

            using (var ms = new MemoryStream())
            {
                stream.CopyTo(ms);

                bytes = ms.ToArray();
            }

            return(XFile.FromBytes(bytes));
        }
示例#3
0
        private static void ReadHeader(XFile file, XofFileEnumData enumData)
        {
            byte[] data = enumData.GetData();

            using (var ms = new MemoryStream(data, false))
                using (var reader = new BinaryReader(ms))
                {
                    short majorVersion = reader.ReadInt16();
                    short minorVersion = reader.ReadInt16();
                    file.FileVersion = new Version(majorVersion, minorVersion);

                    file.FileFlags = reader.ReadInt32();

                    if (ms.Position != ms.Length)
                    {
                        throw new InvalidDataException();
                    }
                }

            if (enumData.GetChildrenCount() != 0)
            {
                throw new InvalidDataException();
            }
        }
示例#4
0
        private static XFile ReadFile(XofFileEnumObject enumObject)
        {
            var file = new XFile();

            using (enumObject)
            {
                bool headerRead             = false;
                bool animTicksPerSecondRead = false;

                int childrenCount = enumObject.GetChildrenCount();

                for (int childIndex = 0; childIndex < childrenCount; childIndex++)
                {
#pragma warning disable CA2000 // Supprimer les objets avant la mise hors de portée
                    using (var child = enumObject.GetChild(childIndex))
#pragma warning restore CA2000 // Supprimer les objets avant la mise hors de portée
                    {
                        Guid type = child.GetTemplateType();

                        if (type == XofFileDefaultTemplates.HeaderId)
                        {
                            if (headerRead)
                            {
                                throw new InvalidDataException();
                            }

                            ReadHeader(file, child);
                            headerRead = true;
                        }
                        else if (type == XofFileDefaultTemplates.MeshId)
                        {
                            file.Meshes.Add(ReadMesh(child));
                        }
                        else if (type == XofFileDefaultTemplates.MaterialId)
                        {
                            file.Materials.Add(ReadMaterial(child));
                        }
                        else if (type == XofFileDefaultTemplates.FrameId)
                        {
                            file.Frames.Add(ReadFrame(child));
                        }
                        else if (type == XofFileDefaultTemplates.AnimationSetId)
                        {
                            file.AnimationSets.Add(ReadAnimationSet(child));
                        }
                        else if (type == XofFileDefaultTemplates.AnimTicksPerSecondId)
                        {
                            if (animTicksPerSecondRead)
                            {
                                throw new InvalidDataException();
                            }

                            file.AnimTicksPerSecond = ReadAnimTicksPerSecond(child);
                            animTicksPerSecondRead  = true;
                        }
                        else
                        {
                            throw new InvalidDataException();
                        }
                    }
                }
            }

            return(file);
        }