Exemple #1
0
        public void Deserialize(Stream input)
        {
            var baseOffset = input.Position;

            var magic = input.ReadValueU32(Endian.Little);

            if (magic != Signature && magic.Swap() != Signature)
            {
                throw new FormatException("not a peg file");
            }
            var endian = magic == Signature ? Endian.Little : Endian.Big;

            var version = input.ReadValueU16(endian);

            if (version != 10)
            {
                throw new FormatException("unsupported peg version");
            }

            var platform = input.ReadValueU16(endian);

            if (Enum.IsDefined(typeof(Platform), platform) == false)
            {
                throw new FormatException("unsupported peg platform");
            }

            var headerSize = input.ReadValueU32(endian);

            if (baseOffset + headerSize > input.Length)
            {
                throw new EndOfStreamException();
            }

            var dataSize     = input.ReadValueU32(endian);
            var textureCount = input.ReadValueU16(endian);
            var unknown12    = input.ReadValueU16(endian);
            var frameCount   = input.ReadValueU16(endian);
            var unknown16    = input.ReadValueU8();
            var unknown17    = input.ReadValueU8();

            if (unknown12 != 0 ||
                unknown16 != 16 ||
                unknown17 != 0)
            {
                throw new FormatException("unexpected unknown values");
            }

            var textures    = new Peg.Texture <Peg.FrameV10> [textureCount];
            int totalFrames = 0;

            for (int i = 0; i < textures.Length; i++)
            {
                textures[i] = new Peg.Texture <Peg.FrameV10>();

                var frame = new Peg.FrameV10();
                frame.Deserialize(input, endian);
                textures[i].Frames.Add(frame);
                totalFrames++;

                /* some pegs in SR2 seem to have been saved incorrectly?
                 * or there is some sort of flag to mark that data is in
                 * the same file as the header
                 *
                 * see:
                 *   ige_regular.peg_pc
                 *   ige_small.peg_pc
                 *   ige_smallest.peg_pc */
                /*
                 * if (i == 0 &&
                 *  frame.DataOffset != 0)
                 * {
                 *  throw new FormatException("strange data offset");
                 * }
                 */

                if (frame.FrameCount == 0)
                {
                    throw new FormatException("frame count is 0");
                }

                for (int j = 1; j < frame.FrameCount; j++)
                {
                    var extraFrame = new Peg.FrameV10();
                    extraFrame.Deserialize(input, endian);

                    if (extraFrame.FrameCount != 1)
                    {
                        throw new FormatException("frame count is not 1");
                    }

                    textures[i].Frames.Add(extraFrame);
                    totalFrames++;
                }
            }

            if (totalFrames != frameCount)
            {
                throw new FormatException("did not read correct amount of frames");
            }

            foreach (var texture in textures)
            {
                texture.Name = input.ReadStringZ(Encoding.ASCII);
            }

            this._Endian   = endian;
            this._Version  = version;
            this._Platform = (Platform)platform;
            this._DataSize = dataSize;
            this._Textures.Clear();
            this._Textures.AddRange(textures);
        }
        public void Deserialize(Stream input)
        {
            var baseOffset = input.Position;

            var magic = input.ReadValueU32(Endian.Little);

            if (magic != Signature && magic.Swap() != Signature)
            {
                throw new FormatException("not a peg file");
            }
            var endian = magic == Signature ? Endian.Little : Endian.Big;

            var version = input.ReadValueU16(endian);

            if (version != 8)
            {
                throw new FormatException("unsupported peg version");
            }

            var platform = input.ReadValueU16(endian);

            if (Enum.IsDefined(typeof(Platform), platform) == false)
            {
                throw new FormatException("unsupported peg platform");
            }

            var headerSize = input.ReadValueU32(endian);

            if (baseOffset + headerSize > input.Length)
            {
                throw new EndOfStreamException();
            }

            var textureCount = input.ReadValueU16(endian);
            var unknown12    = input.ReadValueU16(endian);
            var frameCount   = input.ReadValueU16(endian);
            var unknown16    = input.ReadValueU8();
            var unknown17    = input.ReadValueU8();

            if (unknown12 != 0 ||
                unknown16 != 0 ||
                unknown17 != 16)
            {
                throw new FormatException("unexpected unknown values");
            }

            var textures    = new Peg.Texture <Peg.FrameV8> [textureCount];
            int totalFrames = 0;

            for (int i = 0; i < textures.Length; i++)
            {
                textures[i] = new Peg.Texture <Peg.FrameV8>();

                var frame = new Peg.FrameV8();
                frame.Deserialize(input, endian);
                textures[i].Frames.Add(frame);
                totalFrames++;

                if (frame.FrameCount == 0)
                {
                    throw new FormatException("frame count is 0");
                }

                for (int j = 1; j < frame.FrameCount; j++)
                {
                    var extraFrame = new Peg.FrameV8();
                    extraFrame.Deserialize(input, endian);

                    if (extraFrame.FrameCount != 1)
                    {
                        throw new FormatException("frame count is not 1");
                    }

                    textures[i].Frames.Add(extraFrame);
                    totalFrames++;
                }
            }

            if (totalFrames != frameCount)
            {
                throw new FormatException("did not read correct amount of frames");
            }

            foreach (var texture in textures)
            {
                texture.Name = input.ReadStringZ(Encoding.ASCII);
            }

            this._Endian   = endian;
            this._Version  = version;
            this._Platform = (Platform)platform;
            this._DataSize = input.ReadValueU32(endian);
            this._Textures.Clear();
            this._Textures.AddRange(textures);
        }