Пример #1
0
        /// <summary>
        /// Reads a SPRFrame from a stream.
        /// </summary>
        /// <param name="iff">An Iff instance.</param>
        /// <param name="stream">A Stream object holding a SPRFrame.</param>
        public void Read(uint version, IoBuffer io)
        {
            if (version == 1001)
            {
                var spriteFersion = io.ReadUInt32();
                var size = io.ReadUInt32();
                this.Version = spriteFersion;
            }
            else
            {
                this.Version = version;
            }

            var reserved = io.ReadUInt32();
            var height = io.ReadUInt16();
            var width = io.ReadUInt16();
            this.Init(width, height);
            this.Decode(io);
        }
Пример #2
0
        /// <summary>
        /// Decodes this SPRFrame.
        /// </summary>
        /// <param name="io">IOBuffer used to read a SPRFrame.</param>
        private void Decode(IoBuffer io)
        {
            var palette = Parent.ChunkParent.Get<PALT>(Parent.PaletteID);
            if (palette == null)
            {
                palette = DEFAULT_PALT;
            }

            var y = 0;
            var endmarker = false;

            while (!endmarker){
                var command = io.ReadByte();
                var count = io.ReadByte();

                switch (command){
                    /** Start marker **/
                    case 0x00:
                    case 0x10:
                        break;
                    /** Fill row with pixel data **/
                    case 0x04:
                        var bytes = count - 2;
                        var x = 0;

                        while (bytes > 0){
                            var pxCommand = io.ReadByte();
                            var pxCount = io.ReadByte();
                            bytes -= 2;

                            switch (pxCommand){
                                /** Next {n} pixels are transparent **/
                                case 0x01:
                                    x += pxCount;
                                    break;
                                /** Next {n} pixels are the same palette color **/
                                case 0x02:
                                    var index = io.ReadByte();
                                    var padding = io.ReadByte();
                                    bytes -= 2;

                                    var color = palette.Colors[index];
                                    for (var j=0; j < pxCount; j++){
                                        this.SetPixel(x, y, color);
                                        x++;
                                    }
                                    break;
                                /** Next {n} pixels are specific palette colours **/
                                case 0x03:
                                    for (var j=0; j < pxCount; j++){
                                        var index2 = io.ReadByte();
                                        var color2 = palette.Colors[index2];
                                        this.SetPixel(x, y, color2);
                                        x++;
                                    }
                                    bytes -= pxCount;
                                    if (pxCount % 2 != 0){
                                        //Padding
                                        io.ReadByte();
                                        bytes--;
                                    }
                                    break;
                            }
                        }

                        y++;
                        break;
                    /** End marker **/
                    case 0x05:
                        endmarker = true;
                        break;
                    /** Leave next rows transparent **/
                    case 0x09:
                        y += count;
                        continue;
                }

            }
        }
Пример #3
0
 public TTABFieldEncode(IoBuffer io)
     : base(io)
 {
     curByte = io.ReadByte();
     bitPos = 0;
 }
Пример #4
0
 public TTABNormal(IoBuffer io)
     : base(io)
 {
 }
Пример #5
0
        /// <summary>
        /// Reads a DBPF archive from a stream.
        /// </summary>
        /// <param name="stream">The stream to read from.</param>
        public void Read(Stream stream)
        {
            m_EntryByID = new Dictionary<ulong,DBPFEntry>();
            m_EntriesList = new List<DBPFEntry>();

            var io = IoBuffer.FromStream(stream, ByteOrder.LITTLE_ENDIAN);
            m_Reader = io;
            this.Io = io;

            var magic = io.ReadCString(4);
            if (magic != "DBPF")
            {
                throw new Exception("Not a DBPF file");
            }

            var majorVersion = io.ReadUInt32();
            var minorVersion = io.ReadUInt32();
            var version = majorVersion + (((double)minorVersion)/10.0);

            /** Unknown, set to 0 **/
            io.Skip(12);

            if (version == 1.0)
            {
                this.DateCreated = io.ReadInt32();
                this.DateModified = io.ReadInt32();
            }

            if (version < 2.0)
            {
                IndexMajorVersion = io.ReadUInt32();
            }

            NumEntries = io.ReadUInt32();
            uint indexOffset = 0;
            if (version < 2.0)
            {
                indexOffset = io.ReadUInt32();
            }
            var indexSize = io.ReadUInt32();

            if (version < 2.0)
            {
                var trashEntryCount = io.ReadUInt32();
                var trashIndexOffset = io.ReadUInt32();
                var trashIndexSize = io.ReadUInt32();
                var indexMinor = io.ReadUInt32();
            }
            else if (version == 2.0)
            {
                var indexMinor = io.ReadUInt32();
                indexOffset = io.ReadUInt32();
                io.Skip(4);
            }

            /** Padding **/
            io.Skip(32);

            io.Seek(SeekOrigin.Begin, indexOffset);
            for (int i = 0; i < NumEntries; i++)
            {
                var entry = new DBPFEntry();
                entry.TypeID = (DBPFTypeID)io.ReadUInt32();
                entry.GroupID = (DBPFGroupID)io.ReadUInt32();
                entry.InstanceID = io.ReadUInt32();
                entry.FileOffset = io.ReadUInt32();
                entry.FileSize = io.ReadUInt32();

                m_EntriesList.Add(entry);
                ulong id = (((ulong)entry.InstanceID) << 32) + (ulong)entry.TypeID;
                if (!m_EntryByID.ContainsKey(id))
                    m_EntryByID.Add(id, entry);

                if (!m_EntriesByType.ContainsKey(entry.TypeID))
                    m_EntriesByType.Add(entry.TypeID, new List<DBPFEntry>());

                m_EntriesByType[entry.TypeID].Add(entry);
            }
        }
Пример #6
0
 public IOProxy(IoBuffer io)
 {
     this.io = io;
 }
Пример #7
0
        /// <summary>
        /// Reads a DGRPSprite from a stream.
        /// </summary>
        /// <param name="iff">An Iff instance.</param>
        /// <param name="stream">A Stream object holding a DGRPSprite.</param>
        public void Read(uint version, IoBuffer io)
        {
            if (version < 20003)
            {
                //Unknown ignored "Type" field
                var type = io.ReadUInt16();
                SpriteID = io.ReadUInt16();
                SpriteFrameIndex = io.ReadUInt16();

                var flagsRaw = io.ReadUInt16();
                Flags = (DGRPSpriteFlags)flagsRaw;

                SpriteOffset.X = io.ReadInt16();
                SpriteOffset.Y = io.ReadInt16();

                if(version == 20001)
                {
                    ObjectOffset.Z = io.ReadFloat();
                }
            }
            else
            {
                SpriteID = io.ReadUInt32();
                SpriteFrameIndex = io.ReadUInt32();
                SpriteOffset.X = io.ReadInt32();
                SpriteOffset.Y = io.ReadInt32();
                ObjectOffset.Z = io.ReadFloat();
                Flags = (DGRPSpriteFlags)io.ReadUInt32();
                if (version == 20004)
                {
                    ObjectOffset.X = io.ReadFloat();
                    ObjectOffset.Y = io.ReadFloat();
                }
            }

            this.Flip = (Flags & DGRPSpriteFlags.Flip) == DGRPSpriteFlags.Flip;
        }
Пример #8
0
        /// <summary>
        /// Reads a bone from a IOBuffer.
        /// </summary>
        /// <param name="reader">An IOBuffer instance used to read from a stream holding a skeleton.</param>
        /// <returns>A Bone instance.</returns>
        private Bone ReadBone(IoBuffer reader)
        {
            var bone = new Bone();
            bone.Unknown = reader.ReadInt32();
            bone.Name = reader.ReadPascalString();
            bone.ParentName = reader.ReadPascalString();
            bone.HasProps = reader.ReadByte();
            if (bone.HasProps != 0)
            {
                var propertyCount = reader.ReadInt32();
                var property = new PropertyListItem();

                for (var i = 0; i < propertyCount; i++)
                {
                    var pairCount = reader.ReadInt32();
                    for (var x = 0; x < pairCount; x++)
                    {
                        property.KeyPairs.Add(new KeyValuePair<string, string>(
                            reader.ReadPascalString(),
                            reader.ReadPascalString()
                        ));
                    }
                }
                bone.Properties.Add(property);
            }

            var xx = -reader.ReadFloat();
            bone.Translation = new Vector3(
                xx,
                reader.ReadFloat(),
                reader.ReadFloat()
            );
            bone.Rotation = new Quaternion(
                reader.ReadFloat(),
                -reader.ReadFloat(),
                -reader.ReadFloat(),
                -reader.ReadFloat()
            );
            bone.CanTranslate = reader.ReadInt32();
            bone.CanRotate = reader.ReadInt32();
            bone.CanBlend = reader.ReadInt32();
            bone.WiggleValue = reader.ReadFloat();
            bone.WigglePower = reader.ReadFloat();
            return bone;
        }
Пример #9
0
        /// <summary>
        /// Reads a DGRPImage from a stream.
        /// </summary>
        /// <param name="iff">An Iff instance.</param>
        /// <param name="stream">A Stream object holding a DGRPImage.</param>
        public void Read(uint version, IoBuffer io)
        {
            uint spriteCount = 0;
            if (version < 20003){
                spriteCount = io.ReadUInt16();
                Direction = io.ReadByte();
                Zoom = io.ReadByte();
            }else{
                Direction = io.ReadUInt32();
                Zoom = io.ReadUInt32();
                spriteCount = io.ReadUInt32();
            }

            this.Sprites = new DGRPSprite[spriteCount];
            for (var i = 0; i < spriteCount; i++){
                var sprite = new DGRPSprite(Parent);
                sprite.Read(version, io);
                this.Sprites[i] = sprite;
            }
        }
Пример #10
0
        /// <summary>
        /// Reads a property list from a stream.
        /// </summary>
        /// <param name="io">IOBuffer instance used to read an animation.</param>
        /// <returns>A PropertyList instance.</returns>
        private PropertyList ReadPropertyList(IoBuffer io)
        {
            var propsCount = io.ReadUInt32();
            var result = new PropertyListItem[propsCount];

            for (var y = 0; y < propsCount; y++)
            {
                var item = new PropertyListItem();
                var pairsCount = io.ReadUInt32();
                for (var z = 0; z < pairsCount; z++)
                {
                    item.KeyPairs.Add(new KeyValuePair<string, string>(
                        io.ReadPascalString(),
                        io.ReadPascalString()
                    ));
                }
                result[y] = item;
            }

            return new PropertyList {
                Items = result
            };
        }
Пример #11
0
        /// <summary>
        /// Reads a Handgroup from a supplied Stream.
        /// </summary>
        /// <param name="Str">The Stream to read from.</param>
        public void Read(Stream Str)
        {
            using(IoBuffer IOBuf = new IoBuffer(Str))
            {
                m_Version = IOBuf.ReadUInt32();

                LightSkin.RightHand.Idle.FileID = IOBuf.ReadUInt32();
                LightSkin.RightHand.Idle.TypeID = IOBuf.ReadUInt32();
                LightSkin.RightHand.Fist.FileID = IOBuf.ReadUInt32();
                LightSkin.RightHand.Fist.TypeID = IOBuf.ReadUInt32();
                LightSkin.RightHand.Pointing.FileID = IOBuf.ReadUInt32();
                LightSkin.RightHand.Pointing.TypeID = IOBuf.ReadUInt32();

                LightSkin.LeftHand.Idle.FileID = IOBuf.ReadUInt32();
                LightSkin.LeftHand.Idle.TypeID = IOBuf.ReadUInt32();
                LightSkin.LeftHand.Fist.FileID = IOBuf.ReadUInt32();
                LightSkin.LeftHand.Fist.TypeID = IOBuf.ReadUInt32();
                LightSkin.LeftHand.Pointing.FileID = IOBuf.ReadUInt32();
                LightSkin.LeftHand.Pointing.TypeID = IOBuf.ReadUInt32();

                MediumSkin.RightHand.Idle.FileID = IOBuf.ReadUInt32();
                MediumSkin.RightHand.Idle.TypeID = IOBuf.ReadUInt32();
                MediumSkin.RightHand.Fist.FileID = IOBuf.ReadUInt32();
                MediumSkin.RightHand.Fist.TypeID = IOBuf.ReadUInt32();
                MediumSkin.RightHand.Pointing.FileID = IOBuf.ReadUInt32();
                MediumSkin.RightHand.Pointing.TypeID = IOBuf.ReadUInt32();

                MediumSkin.LeftHand.Idle.FileID = IOBuf.ReadUInt32();
                MediumSkin.LeftHand.Idle.TypeID = IOBuf.ReadUInt32();
                MediumSkin.LeftHand.Fist.FileID = IOBuf.ReadUInt32();
                MediumSkin.LeftHand.Fist.TypeID = IOBuf.ReadUInt32();
                MediumSkin.LeftHand.Pointing.FileID = IOBuf.ReadUInt32();
                MediumSkin.LeftHand.Pointing.TypeID = IOBuf.ReadUInt32();

                DarkSkin.RightHand.Idle.FileID = IOBuf.ReadUInt32();
                DarkSkin.RightHand.Idle.TypeID = IOBuf.ReadUInt32();
                DarkSkin.RightHand.Fist.FileID = IOBuf.ReadUInt32();
                DarkSkin.RightHand.Fist.TypeID = IOBuf.ReadUInt32();
                DarkSkin.RightHand.Pointing.FileID = IOBuf.ReadUInt32();
                DarkSkin.RightHand.Pointing.TypeID = IOBuf.ReadUInt32();

                DarkSkin.LeftHand.Idle.FileID = IOBuf.ReadUInt32();
                DarkSkin.LeftHand.Idle.TypeID = IOBuf.ReadUInt32();
                DarkSkin.LeftHand.Fist.FileID = IOBuf.ReadUInt32();
                DarkSkin.LeftHand.Fist.TypeID = IOBuf.ReadUInt32();
                DarkSkin.LeftHand.Pointing.FileID = IOBuf.ReadUInt32();
                DarkSkin.LeftHand.Pointing.TypeID = IOBuf.ReadUInt32();

            }
        }
Пример #12
0
        /// <summary>
        /// Decodes this SPR2Frame.
        /// </summary>
        /// <param name="io">An IOBuffer instance used to read a SPR2Frame.</param>
        private void Decode(IoBuffer io)
        {
            var y = 0;
            var endmarker = false;

            var hasPixels = (this.Flags & 0x01) == 0x01;
            var hasZBuffer = (this.Flags & 0x02) == 0x02;
            var hasAlpha = (this.Flags & 0x04) == 0x04;

            var numPixels = this.Width * this.Height;
            if (hasPixels){
                this.PixelData = new Color[numPixels];
            }
            if (hasZBuffer){
                this.ZBufferData = new byte[numPixels];
            }
            if (hasAlpha){
                this.AlphaData = new byte[numPixels];
            }

            var palette = Parent.ChunkParent.Get<PALT>(this.PaletteID);
            var transparentPixel = palette.Colors[TransparentColorIndex];

            while (!endmarker)
            {
                var marker = io.ReadUInt16();
                var command = marker >> 13;
                var count = marker & 0x1FFF;

                switch (command)
                {
                    /** Fill with pixel data **/
                    case 0x00:
                        var bytes = count;
                        bytes -= 2;

                        var x = 0;

                        while (bytes > 0)
                        {
                            var pxMarker = io.ReadUInt16();
                            var pxCommand = pxMarker >> 13;
                            var pxCount = pxMarker & 0x1FFF;
                            bytes -= 2;

                            switch (pxCommand)
                            {
                                case 0x01:
                                case 0x02:
                                    var pxWithAlpha = pxCommand == 0x02;
                                    for (var col = 0; col < pxCount; col++)
                                    {
                                        var zValue = io.ReadByte();
                                        var pxValue = io.ReadByte();
                                        bytes -= 2;

                                        var pxColor = palette.Colors[pxValue];
                                        if (pxWithAlpha)
                                        {
                                            pxColor.A = (byte)(io.ReadByte() * 8.2258064516129032258064516129032);
                                            bytes--;
                                        }
                                        else
                                        {
                                            if (pxColor.PackedValue == transparentPixel.PackedValue)
                                            {
                                                pxColor.A = 0;
                                            }
                                        }
                                        var offset = (y * Width) + x;
                                        this.PixelData[offset] = pxColor;
                                        this.ZBufferData[offset] = zValue;
                                        x++;
                                    }
                                    if (pxWithAlpha)
                                    {
                                        /** Padding? **/
                                        if ((pxCount * 3) % 2 != 0){
                                            bytes--;
                                            io.ReadByte();
                                        }
                                    }
                                    break;
                                case 0x03:
                                    for (var col = 0; col < pxCount; col++)
                                    {
                                        var offset = (y * Width) + x;
                                        this.PixelData[offset] = transparentPixel;
                                        this.PixelData[offset].A = 0;
                                        if (hasZBuffer){
                                            this.ZBufferData[offset] = 255;
                                        }
                                        x++;
                                    }
                                    break;
                                case 0x06:
                                    for (var col = 0; col < pxCount; col++)
                                    {
                                        var pxIndex = io.ReadByte();
                                        bytes--;
                                        var offset = (y * Width) + x;
                                        var pxColor = palette.Colors[pxIndex];
                                        byte z = 0;
                                        if (pxColor.PackedValue == transparentPixel.PackedValue)
                                        {
                                            pxColor.A = 0;
                                            z = 255;
                                        }
                                        this.PixelData[offset] = pxColor;
                                        if (hasZBuffer)
                                        {
                                            this.ZBufferData[offset] = z;
                                        }
                                        x++;
                                    }
                                    if (pxCount % 2 != 0)
                                    {
                                        bytes--;
                                        io.ReadByte();
                                    }
                                    break;
                            }
                        }

                        /** If row isnt filled in, the rest is transparent **/
                        while (x < Width)
                        {
                            var offset = (y * Width) + x;
                            if (hasZBuffer)
                            {
                                this.ZBufferData[offset] = 255;
                            }
                            x++;
                        }
                        break;
                    /**  Leave the next count rows in the color channel filled with the transparent color,
                     * in the z-buffer channel filled with 255, and in the alpha channel filled with 0. **/
                    case 0x04:
                        for (var row = 0; row < count; row++)
                        {
                            for (var col = 0; col < Width; col++)
                            {
                                var offset = ((y+row) * Width) + col;
                                if (hasPixels)
                                {
                                    this.PixelData[offset] = transparentPixel;
                                }
                                if (hasAlpha)
                                {
                                    this.PixelData[offset].A = 0;
                                }
                                if (hasZBuffer)
                                {
                                    ZBufferData[offset] = 255;
                                }
                            }
                        }
                        y += count - 1;
                        break;
                    case 0x05:
                        endmarker = true;
                        break;
                }
                y++;
            }
        }
Пример #13
0
        /// <summary>
        /// Reads a BMP chunk from a stream.
        /// </summary>
        /// <param name="version">Version of the SPR2 that this frame belongs to.</param>
        /// <param name="stream">A IOBuffer object used to read a SPR2 chunk.</param>
        public void Read(uint version, IoBuffer io)
        {
            if (version == 1001)
            {
                var spriteVersion = io.ReadUInt32();
                var spriteSize = io.ReadUInt32();
            }

            this.Width = io.ReadUInt16();
            this.Height = io.ReadUInt16();
            this.Flags = io.ReadUInt32();
            io.ReadUInt16();

            if (this.PaletteID == 0 || this.PaletteID == 0xA3A3)
            {
                this.PaletteID = (ushort)Parent.DefaultPaletteID;
            }

            TransparentColorIndex = io.ReadUInt16();

            var y = io.ReadInt16();
            var x = io.ReadInt16();
            this.Position = new Vector2(x, y);

            this.Decode(io);
        }