public static SpriteSetHeader ReadFromFile(string filepath)
        {
            SpriteSetHeader header = new SpriteSetHeader();

            using (FileStream stream = new FileStream(filepath, FileMode.Open))
            {
                using (BinaryReader reader = new BinaryReader(stream, Encoding.Latin1))
                {
                    header.Version = reader.ReadInt16();

                    header.Compression = CompressionType.Unknown;
                    byte compressionType = reader.ReadByte();
                    if (Enum.IsDefined(typeof(CompressionType), (int)compressionType))
                    {
                        header.Compression = (CompressionType)compressionType;
                    }

                    header.FileID       = reader.ReadUInt32();
                    header.SpritesCount = reader.ReadUInt16();

                    if (header.Version < 5)
                    {
                        header.Palette = AGSGraphics.ReadPalette(reader);
                    }
                }
            }

            return(header);
        }
Example #2
0
        private static SpriteSetHeader ReadSpriteSetHeader(BinaryReader reader)
        {
            int version = reader.ReadInt16();
            // FIXME(adm244): should probably use ReadFixedString instead; there are many cases like this
            string signature = reader.ReadFixedCString(SpriteSetSignature.Length);

            //Debug.Assert(signature == SpriteSetSignature);

            if (signature != SpriteSetSignature)
            {
                throw new InvalidDataException("Sprite set file signature mismatch!");
            }

            if (version < 4 || version > 11)
            {
                throw new NotSupportedException(
                          $"Sprite set version is not supported.\n\nGot: {version}\nMin supported: {4}\nMax supported: {11}");
            }

            CompressionType compression = SpriteSetHeader.DefaultCompression;
            UInt32          fileID      = SpriteSetHeader.DefaultFileID;

            if (version == 4)
            {
                compression = CompressionType.Uncompressed;
            }
            else if (version == 5)
            {
                compression = CompressionType.RLE;
            }
            else if (version >= 6)
            {
                compression = CompressionType.Unknown;
                byte compressionType = reader.ReadByte();
                if (Enum.IsDefined(typeof(CompressionType), (int)compressionType))
                {
                    compression = (CompressionType)compressionType;
                }

                fileID = reader.ReadUInt32();
            }

            // TODO(adm244): for correct results we should get palette from a DTA file instead
            Palette palette = SpriteSetHeader.DefaultPalette;

            if (version < 5)
            {
                palette = AGSGraphics.ReadPalette(reader);
                // NOTE(adm244): first 17 colors are locked and cannot be changed in pre 6
                // but for some reason palette in sprite set file differs from game data (2.72)
                // Here we try to restore what we can, but it's not enought to be fully correct
                // AGS. When everything's a stress.
                for (int i = 0; i < 17; ++i)
                {
                    palette.Entries[i] = SpriteSetHeader.DefaultPalette[i];
                }
            }

            int spritesCount;

            if (version < 11)
            {
                spritesCount = reader.ReadUInt16();
            }
            else
            {
                spritesCount = reader.ReadInt32();
            }

            return(new SpriteSetHeader(version, compression, fileID, spritesCount, palette));
        }