diff --git a/ArcFormats/AZSys/ArcEncrypted.cs b/ArcFormats/AZSys/ArcEncrypted.cs index bd02620b..1ad23e8e 100644 --- a/ArcFormats/AZSys/ArcEncrypted.cs +++ b/ArcFormats/AZSys/ArcEncrypted.cs @@ -248,7 +248,7 @@ namespace GameRes.Formats.AZSys Buffer.BlockCopy (data, 0, header, 0, 0x10); return new PrefixStream (header, asb); } - return new MemoryStream (data); + return new BinMemoryStream (data, entry.Name); } uint ReadSysenvSeed (ArcView file, IEnumerable dir, uint key) diff --git a/ArcFormats/AZSys/ImageCPB.cs b/ArcFormats/AZSys/ImageCPB.cs index 03b3a33c..1c5055e9 100644 --- a/ArcFormats/AZSys/ImageCPB.cs +++ b/ArcFormats/AZSys/ImageCPB.cs @@ -54,51 +54,48 @@ namespace GameRes.Formats.AZSys throw new System.NotImplementedException ("CpbFormat.Write not implemented"); } - public override ImageMetaData ReadMetaData (Stream stream) + public override ImageMetaData ReadMetaData (IBinaryStream file) { - stream.Seek (4, SeekOrigin.Current); - int type = stream.ReadByte(); - int bpp = stream.ReadByte(); + file.Position = 4; + int type = file.ReadByte(); + int bpp = file.ReadByte(); if (24 != bpp && 32 != bpp) throw new NotSupportedException ("Not supported CPB image format"); - using (var input = new ArcView.Reader (stream)) + int version = file.ReadInt16(); + if (1 != version && 0 != version) + throw new NotSupportedException ("Not supported CPB image version"); + var info = new CpbMetaData { + Type = type, + Version = version, + BPP = bpp, + }; + if (1 == version) { - int version = input.ReadInt16 (); - if (1 != version && 0 != version) - throw new NotSupportedException ("Not supported CPB image version"); - var info = new CpbMetaData { - Type = type, - Version = version, - BPP = bpp, - }; - if (1 == version) - { - input.ReadUInt32(); - info.Width = input.ReadUInt16(); - info.Height = input.ReadUInt16(); - info.Channel[0] = input.ReadUInt32(); - info.Channel[1] = input.ReadUInt32(); - info.Channel[2] = input.ReadUInt32(); - info.Channel[3] = input.ReadUInt32(); - } - else - { - info.Width = input.ReadUInt16(); - info.Height = input.ReadUInt16(); - input.ReadUInt32(); - info.Channel[0] = input.ReadUInt32(); - info.Channel[1] = input.ReadUInt32(); - info.Channel[2] = input.ReadUInt32(); - info.Channel[3] = input.ReadUInt32(); - } - info.DataOffset = (uint)stream.Position; - return info; + file.ReadUInt32(); + info.Width = file.ReadUInt16(); + info.Height = file.ReadUInt16(); + info.Channel[0] = file.ReadUInt32(); + info.Channel[1] = file.ReadUInt32(); + info.Channel[2] = file.ReadUInt32(); + info.Channel[3] = file.ReadUInt32(); } + else + { + info.Width = file.ReadUInt16(); + info.Height = file.ReadUInt16(); + file.ReadUInt32(); + info.Channel[0] = file.ReadUInt32(); + info.Channel[1] = file.ReadUInt32(); + info.Channel[2] = file.ReadUInt32(); + info.Channel[3] = file.ReadUInt32(); + } + info.DataOffset = (uint)file.Position; + return info; } - public override ImageData Read (Stream stream, ImageMetaData info) + public override ImageData Read (IBinaryStream stream, ImageMetaData info) { - var reader = new Reader (stream, (CpbMetaData)info); + var reader = new Reader (stream.AsStream, (CpbMetaData)info); reader.Unpack(); return ImageData.Create (info, reader.Format, reader.Palette, reader.Data); } diff --git a/ArcFormats/AZSys/ImageTYP1.cs b/ArcFormats/AZSys/ImageTYP1.cs index 39e7a7b7..aad4243f 100644 --- a/ArcFormats/AZSys/ImageTYP1.cs +++ b/ArcFormats/AZSys/ImageTYP1.cs @@ -45,46 +45,38 @@ namespace GameRes.Formats.AZSys public override string Description { get { return "AZ system image format"; } } public override uint Signature { get { return 0x31505954; } } // 'TYP1' - public Typ1Format () - { - Extensions = new string[] { "cpb" }; - } - - public override ImageMetaData ReadMetaData (Stream stream) + public override ImageMetaData ReadMetaData (IBinaryStream stream) { stream.Position = 4; int bpp = stream.ReadByte(); bool has_palette = stream.ReadByte() != 0; - using (var input = new ArcView.Reader (stream)) + var info = new Typ1MetaData { BPP = bpp }; + info.Width = stream.ReadUInt16(); + info.Height = stream.ReadUInt16(); + uint packed_size = stream.ReadUInt32(); + uint palette_size = 8 == bpp ? 0x400u : 0u; + if (packed_size+palette_size+0xE == stream.Length) { - var info = new Typ1MetaData { BPP = bpp }; - info.Width = input.ReadUInt16(); - info.Height = input.ReadUInt16(); - uint packed_size = input.ReadUInt32(); - uint palette_size = 8 == bpp ? 0x400u : 0u; - if (packed_size+palette_size+0xE == stream.Length) - { - info.SeparateChannels = false; - info.HasPalette = palette_size > 0; - info.PackedSize = packed_size; - } - else - { - info.SeparateChannels = true; - info.HasPalette = has_palette; - info.Channel[0] = input.ReadUInt32(); - info.Channel[1] = input.ReadUInt32(); - info.Channel[2] = input.ReadUInt32(); - info.Channel[3] = input.ReadUInt32(); - } - return info; + info.SeparateChannels = false; + info.HasPalette = palette_size > 0; + info.PackedSize = packed_size; } + else + { + info.SeparateChannels = true; + info.HasPalette = has_palette; + info.Channel[0] = stream.ReadUInt32(); + info.Channel[1] = stream.ReadUInt32(); + info.Channel[2] = stream.ReadUInt32(); + info.Channel[3] = stream.ReadUInt32(); + } + return info; } - public override ImageData Read (Stream stream, ImageMetaData info) + public override ImageData Read (IBinaryStream stream, ImageMetaData info) { var meta = (Typ1MetaData)info; - var reader = new Reader (stream, meta); + var reader = new Reader (stream.AsStream, meta); reader.Unpack(); return ImageData.Create (meta, reader.Format, reader.Palette, reader.Data); } diff --git a/ArcFormats/Abel/ArcARC.cs b/ArcFormats/Abel/ArcARC.cs index b3273a9e..941edd7e 100644 --- a/ArcFormats/Abel/ArcARC.cs +++ b/ArcFormats/Abel/ArcARC.cs @@ -102,7 +102,7 @@ namespace GameRes.Formats.Abel { data[i] = (byte)(0xFF - data[i]); } - return new MemoryStream (data); + return new BinMemoryStream (data, entry.Name); } Stream OpenCmpEntry (ArcFile arc, Entry entry) diff --git a/ArcFormats/Abel/ImageGPS.cs b/ArcFormats/Abel/ImageGPS.cs index 39a1649d..229d21ce 100644 --- a/ArcFormats/Abel/ImageGPS.cs +++ b/ArcFormats/Abel/ImageGPS.cs @@ -52,9 +52,9 @@ namespace GameRes.Formats.Abel Extensions = new string[] { "gps", "cmp" }; } - public override ImageMetaData ReadMetaData (IBinaryStream stream) + public override ImageMetaData ReadMetaData (IBinaryStream file) { - var header = stream.ReadHeader (0x29); + var header = file.ReadHeader (0x29); if (header.Length != 0x29) return null; var gps = new GpsMetaData @@ -66,7 +66,8 @@ namespace GameRes.Formats.Abel PackedSize = header.ToInt32 (0x15), }; // read BMP header - using (var input = OpenGpsStream (stream, gps.Compression, 0x54)) + using (var stream = OpenGpsStream (file, gps.Compression, 0x54)) + using (var input = BinaryStream.FromStream (stream, file.Name)) { var bmp_info = base.ReadMetaData (input); if (null == bmp_info) @@ -76,11 +77,12 @@ namespace GameRes.Formats.Abel } } - public override ImageData Read (IBinaryStream stream, ImageMetaData info) + public override ImageData Read (IBinaryStream file, ImageMetaData info) { var gps = (GpsMetaData)info; - stream.Position = 0x29; - using (var input = OpenGpsStream (stream, gps.Compression, gps.UnpackedSize)) + file.Position = 0x29; + using (var stream = OpenGpsStream (file, gps.Compression, gps.UnpackedSize)) + using (var input = BinaryStream.FromStream (stream, file.Name)) return base.Read (input, info); } @@ -89,30 +91,28 @@ namespace GameRes.Formats.Abel throw new System.NotImplementedException ("GpsFormat.Write not implemented"); } - IBinaryStream OpenGpsStream (IBinaryStream input, byte compression, int unpacked_size) + Stream OpenGpsStream (IBinaryStream input, byte compression, int unpacked_size) { - Stream gps = null; if (0 == compression) - gps = new StreamRegion (input.AsStream, 0x29, true); + return new StreamRegion (input.AsStream, 0x29, true); else if (1 == compression) - gps = OpenRLEStream (input.AsStream, unpacked_size); + return OpenRLEStream (input.AsStream, unpacked_size); else if (2 == compression) - gps = new LzssStream (input.AsStream, LzssMode.Decompress, true); + return new LzssStream (input.AsStream, LzssMode.Decompress, true); else if (3 == compression) { using (var lzss = new LzssStream (input.AsStream, LzssMode.Decompress, true)) - gps = OpenRLEStream (lzss, unpacked_size); + return OpenRLEStream (lzss, unpacked_size); } else throw new InvalidFormatException(); - return new BinaryStream (gps); } Stream OpenRLEStream (Stream input, int output_size) { var output = new byte[output_size]; UnpackRLE (input, output); - return new MemoryStream (output); + return new BinMemoryStream (output, ""); } void UnpackRLE (Stream input, byte[] output) diff --git a/ArcFormats/Actgs/ArcDAT.cs b/ArcFormats/Actgs/ArcDAT.cs index 080775ee..07460b1a 100644 --- a/ArcFormats/Actgs/ArcDAT.cs +++ b/ArcFormats/Actgs/ArcDAT.cs @@ -123,7 +123,7 @@ namespace GameRes.Formats.Actgs arc.File.View.Read (entry.Offset, data, 0, entry.Size); Decrypt (data, 1, data.Length-1, actarc.Key); data[0] = (byte)'N'; - return new MemoryStream (data); + return new BinMemoryStream (data, entry.Name); } if (arc.File.View.AsciiEqual (entry.Offset, "PAK ")) { @@ -136,7 +136,7 @@ namespace GameRes.Formats.Actgs arc.File.View.Read (entry.Offset, header, 0, length); Decrypt (header, 0, (int)length, actarc.Key); if (entry.Size <= 0x20) - return new MemoryStream (header); + return new BinMemoryStream (header, entry.Name); var rest = arc.File.CreateStream (entry.Offset+0x20, entry.Size-0x20); return new PrefixStream (header, rest); } diff --git a/ArcFormats/ActiveSoft/ImageEDT.cs b/ArcFormats/ActiveSoft/ImageEDT.cs index b8e6a71d..36aebb57 100644 --- a/ArcFormats/ActiveSoft/ImageEDT.cs +++ b/ArcFormats/ActiveSoft/ImageEDT.cs @@ -107,17 +107,15 @@ namespace GameRes.Formats.AdPack throw new NotImplementedException ("EdtFormat.Write not implemented"); } - public override ImageMetaData ReadMetaData (Stream stream) + public override ImageMetaData ReadMetaData (IBinaryStream stream) { - var header = new byte[0x22]; - if (header.Length != stream.Read (header, 0, header.Length)) + var header = stream.ReadHeader (0x22); + if (!header.AsciiEqual (".TRUE\x8d\x5d\x8c\xcb\x00")) return null; - if (!Binary.AsciiEqual (header, ".TRUE\x8d\x5d\x8c\xcb\x00")) - return null; - uint width = LittleEndian.ToUInt16 (header, 0x0e); - uint height = LittleEndian.ToUInt16 (header, 0x10); - uint comp_size = LittleEndian.ToUInt32 (header, 0x1a); - uint extra_size = LittleEndian.ToUInt32 (header, 0x1e); + uint width = header.ToUInt16 (0x0e); + uint height = header.ToUInt16 (0x10); + uint comp_size = header.ToUInt32 (0x1a); + uint extra_size = header.ToUInt32 (0x1e); if (extra_size % 3 != 0 || 0 == extra_size) return null; @@ -131,21 +129,18 @@ namespace GameRes.Formats.AdPack }; } - public override ImageData Read (Stream stream, ImageMetaData info) + public override ImageData Read (IBinaryStream stream, ImageMetaData info) { - var meta = info as EdtMetaData; - if (null == meta) - throw new ArgumentException ("EdtFormat.Read should be supplied with EdtMetaData", "info"); - + var meta = (EdtMetaData)info; stream.Position = 0x22; - using (var reader = new Reader (stream, meta)) + using (var reader = new Reader (stream.AsStream, meta)) { reader.Unpack(); return ImageData.Create (meta, PixelFormats.Bgr24, null, reader.Data, (int)meta.Width*3); } } - internal class Reader : BitReader, IDisposable + internal sealed class Reader : BitReader, IDisposable { MemoryStream m_packed; MemoryStream m_extra; @@ -244,24 +239,15 @@ namespace GameRes.Formats.AdPack #region IDisposable Members bool disposed = false; - public void Dispose () - { - Dispose (true); - GC.SuppressFinalize (this); - } - - protected virtual void Dispose (bool disposing) { if (!disposed) { - if (disposing) - { - m_packed.Dispose(); - m_extra.Dispose(); - } + m_packed.Dispose(); + m_extra.Dispose(); disposed = true; } + GC.SuppressFinalize (this); } #endregion } @@ -284,17 +270,15 @@ namespace GameRes.Formats.AdPack throw new NotImplementedException ("Ed8Format.Write not implemented"); } - public override ImageMetaData ReadMetaData (Stream stream) + public override ImageMetaData ReadMetaData (IBinaryStream stream) { - var header = new byte[0x1a]; - if (header.Length != stream.Read (header, 0, header.Length)) + var header = stream.ReadHeader (0x1A); + if (!header.AsciiEqual (".8Bit\x8d\x5d\x8c\xcb\x00")) return null; - if (!Binary.AsciiEqual (header, ".8Bit\x8d\x5d\x8c\xcb\x00")) - return null; - uint width = LittleEndian.ToUInt16 (header, 0x0e); - uint height = LittleEndian.ToUInt16 (header, 0x10); - uint palette_size = LittleEndian.ToUInt32 (header, 0x12); - uint comp_size = LittleEndian.ToUInt32 (header, 0x16); + uint width = header.ToUInt16 (0x0e); + uint height = header.ToUInt16 (0x10); + uint palette_size = header.ToUInt32 (0x12); + uint comp_size = header.ToUInt32 (0x16); if (palette_size > 0x100) return null; @@ -308,14 +292,11 @@ namespace GameRes.Formats.AdPack }; } - public override ImageData Read (Stream stream, ImageMetaData info) + public override ImageData Read (IBinaryStream stream, ImageMetaData info) { - var meta = info as Ed8MetaData; - if (null == meta) - throw new ArgumentException ("Ed8Format.Read should be supplied with Ed8MetaData", "info"); - + var meta = (Ed8MetaData)info; stream.Position = 0x1a; - var reader = new Reader (stream, meta); + var reader = new Reader (stream.AsStream, meta); reader.Unpack(); var palette = new BitmapPalette (reader.Palette); return ImageData.Create (info, PixelFormats.Indexed8, palette, reader.Data, (int)info.Width); diff --git a/ArcFormats/AdvSys/ImageGR2.cs b/ArcFormats/AdvSys/ImageGR2.cs index 40ee74b7..af39c86a 100644 --- a/ArcFormats/AdvSys/ImageGR2.cs +++ b/ArcFormats/AdvSys/ImageGR2.cs @@ -38,20 +38,18 @@ namespace GameRes.Formats.AdvSys public override string Description { get { return "AdvSys engine image format"; } } public override uint Signature { get { return 0x5F325247; } } // 'GR2_' - public override ImageMetaData ReadMetaData (Stream stream) + public override ImageMetaData ReadMetaData (IBinaryStream stream) { - var header = new byte[16]; - if (16 != stream.Read (header, 0, 16)) - return null; + var header = stream.ReadHeader (0x10); return new ImageMetaData { - Width = LittleEndian.ToUInt16 (header, 4), - Height = LittleEndian.ToUInt16 (header, 6), - BPP = LittleEndian.ToInt16 (header, 12) * 8 + Width = header.ToUInt16 (4), + Height = header.ToUInt16 (6), + BPP = header.ToInt16 (12) * 8 }; } - public override ImageData Read (Stream stream, ImageMetaData info) + public override ImageData Read (IBinaryStream stream, ImageMetaData info) { stream.Position = 0x10; int stride = ((int)info.Width * info.BPP/8 + 3) & ~3; @@ -92,18 +90,16 @@ namespace GameRes.Formats.AdvSys Extensions = new string[] { "gr2" }; } - public override ImageMetaData ReadMetaData (Stream stream) + public override ImageMetaData ReadMetaData (IBinaryStream stream) { - var header = new byte[20]; - if (20 != stream.Read (header, 0, 20)) + var header = stream.ReadHeader (20); + if (!header.AsciiEqual ("*Pola* ")) return null; - if (!Binary.AsciiEqual (header, "*Pola* ")) - return null; - int unpacked_size = LittleEndian.ToInt32 (header, 8); + int unpacked_size = header.ToInt32 (8); using (var reader = new PolaReader (stream, 64)) { reader.Unpack(); - using (var temp = new MemoryStream (reader.Data)) + using (var temp = BinaryStream.FromArray (reader.Data, stream.Name)) { var info = base.ReadMetaData (temp); if (null == info) @@ -119,17 +115,14 @@ namespace GameRes.Formats.AdvSys } } - public override ImageData Read (Stream stream, ImageMetaData info) + public override ImageData Read (IBinaryStream stream, ImageMetaData info) { - var meta = info as PolaMetaData; - if (null == meta) - throw new ArgumentException ("PolaFormat.Read should be supplied with PolaMetaData", "info"); - + var meta = (PolaMetaData)info; stream.Position = 0x14; using (var reader = new PolaReader (stream, meta.UnpackedSize)) { reader.Unpack(); - using (var temp = new MemoryStream (reader.Data)) + using (var temp = BinaryStream.FromArray (reader.Data, stream.Name)) return base.Read (temp, info); } } @@ -142,14 +135,14 @@ namespace GameRes.Formats.AdvSys internal sealed class PolaReader : IDisposable { - BinaryReader m_input; + IBinaryStream m_input; byte[] m_output; public byte[] Data { get { return m_output; } } - public PolaReader (Stream input, int unpacked_size) + public PolaReader (IBinaryStream input, int unpacked_size) { - m_input = new ArcView.Reader (input); + m_input = input; m_output = new byte[unpacked_size+2]; } @@ -161,14 +154,14 @@ namespace GameRes.Formats.AdvSys { if (0 != NextBit()) { - m_output[dst++] = m_input.ReadByte(); + m_output[dst++] = m_input.ReadUInt8(); continue; } int offset, count = 0; if (0 != NextBit()) { - offset = m_input.ReadByte() - 256; + offset = m_input.ReadUInt8() - 256; if (0 == NextBit()) { @@ -228,7 +221,7 @@ namespace GameRes.Formats.AdvSys } else { - count = m_input.ReadByte() + 17; + count = m_input.ReadUInt8() + 17; } if (dst + count > m_output.Length) count = m_output.Length - dst; @@ -237,7 +230,7 @@ namespace GameRes.Formats.AdvSys } else { - offset = m_input.ReadByte() - 256; + offset = m_input.ReadUInt8() - 256; if (0 == NextBit()) { if (offset != -1) @@ -280,14 +273,8 @@ namespace GameRes.Formats.AdvSys } #region IDisposable Members - bool m_disposed = false; public void Dispose () { - if (!m_disposed) - { - m_input.Dispose(); - m_disposed = true; - } } #endregion } diff --git a/ArcFormats/AdvSys/ImageGWD.cs b/ArcFormats/AdvSys/ImageGWD.cs index 61548b04..29358d02 100644 --- a/ArcFormats/AdvSys/ImageGWD.cs +++ b/ArcFormats/AdvSys/ImageGWD.cs @@ -48,7 +48,7 @@ namespace GameRes.Formats.AdvSys var header = stream.ReadHeader (12); if (header.Length != 12) return null; - if (!Binary.AsciiEqual (header, 4, "GWD")) + if (!header.AsciiEqual (4, "GWD")) return null; return new GwdMetaData { @@ -72,7 +72,7 @@ namespace GameRes.Formats.AdvSys if (24 == info.BPP && 1 == stream.ReadByte()) { using (var part = new StreamRegion (stream.AsStream, stream.Position, true)) - using (var alpha_stream = new BinaryStream (part)) + using (var alpha_stream = new BinaryStream (part, stream.Name)) { var alpha_info = ReadMetaData (alpha_stream) as GwdMetaData; if (null != alpha_info && 8 == alpha_info.BPP diff --git a/ArcFormats/AliceSoft/ArcAFA.cs b/ArcFormats/AliceSoft/ArcAFA.cs index fc9e15af..b8d4f843 100644 --- a/ArcFormats/AliceSoft/ArcAFA.cs +++ b/ArcFormats/AliceSoft/ArcAFA.cs @@ -101,7 +101,7 @@ namespace GameRes.Formats.AliceSoft for (int i = 0; i < prefix.Length; ++i) prefix[i] ^= AffKey[i & 0xF]; if (data_size <= 0x40) - return new MemoryStream (prefix); + return new BinMemoryStream (prefix, entry.Name); var rest = arc.File.CreateStream (entry.Offset+0x10+encrypted_length, data_size-encrypted_length); return new PrefixStream (prefix, rest); } diff --git a/ArcFormats/AliceSoft/ImageDCF.cs b/ArcFormats/AliceSoft/ImageDCF.cs index 6c07b512..f570b8c0 100644 --- a/ArcFormats/AliceSoft/ImageDCF.cs +++ b/ArcFormats/AliceSoft/ImageDCF.cs @@ -137,13 +137,15 @@ namespace GameRes.Formats.AliceSoft long qnt_pos = m_input.Position; if (m_input.ReadUInt32() != Qnt.Signature) throw new InvalidFormatException(); - m_input.Seek (-4, SeekOrigin.Current); - var qnt_info = Qnt.ReadMetaData (m_input) as QntMetaData; + QntMetaData qnt_info; + using (var reg = new StreamRegion (m_input.AsStream, qnt_pos, true)) + using (var qnt = new BinaryStream (reg, m_input.Name)) + qnt_info = Qnt.ReadMetaData (qnt) as QntMetaData; if (null == qnt_info) throw new InvalidFormatException(); m_input.Position = qnt_pos + 0x44; - var overlay = new QntFormat.Reader (m_input, qnt_info); + var overlay = new QntFormat.Reader (m_input.AsStream, qnt_info); overlay.Unpack(); m_overlay_bpp = overlay.BPP; if (m_mask != null) @@ -213,13 +215,13 @@ namespace GameRes.Formats.AliceSoft string dir_name = VFS.GetDirectoryName (m_info.FileName); string base_name = Path.ChangeExtension (m_info.BaseName, "qnt"); base_name = VFS.CombinePath (dir_name, base_name); - using (var base_file = VFS.OpenSeekableStream (base_name)) + using (var base_file = VFS.OpenBinaryStream (base_name)) { var base_info = Qnt.ReadMetaData (base_file) as QntMetaData; if (null != base_info && m_info.Width == base_info.Width && m_info.Height == base_info.Height) { base_info.FileName = base_name; - var reader = new QntFormat.Reader (base_file, base_info); + var reader = new QntFormat.Reader (base_file.AsStream, base_info); reader.Unpack(); m_base_bpp = reader.BPP; m_base = reader.Data; diff --git a/ArcFormats/AliceSoft/ImageQNT.cs b/ArcFormats/AliceSoft/ImageQNT.cs index e696604d..61cb0b83 100644 --- a/ArcFormats/AliceSoft/ImageQNT.cs +++ b/ArcFormats/AliceSoft/ImageQNT.cs @@ -49,36 +49,34 @@ namespace GameRes.Formats.AliceSoft throw new System.NotImplementedException ("QntFormat.Write not implemented"); } - public override ImageMetaData ReadMetaData (Stream stream) + public override ImageMetaData ReadMetaData (IBinaryStream stream) { - var header = new byte[0x44]; - if (header.Length != stream.Read (header, 0, header.Length)) - return null; - int version = LittleEndian.ToInt32 (header, 4); + var header = stream.ReadHeader (0x44); + int version = header.ToInt32 (4); if (version <= 0 || version > 2) return null; - if (0x44 != LittleEndian.ToUInt32 (header, 8)) + if (0x44 != header.ToUInt32 (8)) return null; - uint width = LittleEndian.ToUInt32 (header, 0x14); - uint height = LittleEndian.ToUInt32 (header, 0x18); + uint width = header.ToUInt32 (0x14); + uint height = header.ToUInt32 (0x18); if (0 == width || 0 == height) return null; return new QntMetaData { Width = width, Height = height, - OffsetX = LittleEndian.ToInt32 (header, 0x0c), - OffsetY = LittleEndian.ToInt32 (header, 0x10), - BPP = LittleEndian.ToInt32 (header, 0x1c), - RGBSize = LittleEndian.ToUInt32 (header, 0x24), - AlphaSize = LittleEndian.ToUInt32 (header, 0x28), + OffsetX = header.ToInt32 (0x0c), + OffsetY = header.ToInt32 (0x10), + BPP = header.ToInt32 (0x1c), + RGBSize = header.ToUInt32 (0x24), + AlphaSize = header.ToUInt32 (0x28), }; } - public override ImageData Read (Stream stream, ImageMetaData info) + public override ImageData Read (IBinaryStream stream, ImageMetaData info) { stream.Position = 0x44; - var reader = new Reader (stream, (QntMetaData)info); + var reader = new Reader (stream.AsStream, (QntMetaData)info); reader.Unpack(); int stride = (int)info.Width * (reader.BPP / 8); PixelFormat format = 24 == reader.BPP ? PixelFormats.Bgr24 : PixelFormats.Bgra32; diff --git a/ArcFormats/Amaterasu/ArcAMI.cs b/ArcFormats/Amaterasu/ArcAMI.cs index 0faaab43..5748a3ee 100644 --- a/ArcFormats/Amaterasu/ArcAMI.cs +++ b/ArcFormats/Amaterasu/ArcAMI.cs @@ -272,7 +272,7 @@ namespace GameRes.Formats.Amaterasu uint WriteAmiEntry (PackedEntry entry, Stream output) { uint packed_size = 0; - using (var input = File.OpenRead (entry.Name)) + using (var input = VFS.OpenBinaryStream (entry)) { long file_size = input.Length; if (file_size > uint.MaxValue) @@ -284,7 +284,7 @@ namespace GameRes.Formats.Amaterasu } else { - input.CopyTo (output); + input.AsStream.CopyTo (output); } } return packed_size; @@ -293,19 +293,19 @@ namespace GameRes.Formats.Amaterasu static Lazy s_grp_format = new Lazy (() => FormatCatalog.Instance.ImageFormats.OfType().FirstOrDefault()); - uint WriteImageEntry (PackedEntry entry, Stream input, Stream output) + uint WriteImageEntry (PackedEntry entry, IBinaryStream input, Stream output) { var grp = s_grp_format.Value; if (null == grp) // probably never happens throw new FileFormatException ("GRP image encoder not available"); - bool is_grp = grp.Signature == FormatCatalog.ReadSignature (input); + bool is_grp = grp.Signature == input.Signature; input.Position = 0; var start = output.Position; using (var zstream = new ZLibStream (output, CompressionMode.Compress, CompressionLevel.Level9, true)) { if (is_grp) { - input.CopyTo (zstream); + input.AsStream.CopyTo (zstream); } else { diff --git a/ArcFormats/Amaterasu/ImageGRP.cs b/ArcFormats/Amaterasu/ImageGRP.cs index 784e10d8..8106b54f 100644 --- a/ArcFormats/Amaterasu/ImageGRP.cs +++ b/ArcFormats/Amaterasu/ImageGRP.cs @@ -40,23 +40,19 @@ namespace GameRes.Formats.Amaterasu public override uint Signature { get { return 0x00505247; } } // 'GRP' public override bool CanWrite { get { return true; } } - public override ImageMetaData ReadMetaData (Stream stream) + public override ImageMetaData ReadMetaData (IBinaryStream file) { - using (var file = new BinaryReader (stream, Encoding.ASCII, true)) - { - if (file.ReadUInt32() != Signature) - return null; - var meta = new ImageMetaData(); - meta.OffsetX = file.ReadInt16(); - meta.OffsetY = file.ReadInt16(); - meta.Width = file.ReadUInt16(); - meta.Height = file.ReadUInt16(); - meta.BPP = 32; - return meta; - } + file.Position = 4; + var meta = new ImageMetaData(); + meta.OffsetX = file.ReadInt16(); + meta.OffsetY = file.ReadInt16(); + meta.Width = file.ReadUInt16(); + meta.Height = file.ReadUInt16(); + meta.BPP = 32; + return meta; } - public override ImageData Read (Stream file, ImageMetaData info) + public override ImageData Read (IBinaryStream file, ImageMetaData info) { int width = (int)info.Width; int height = (int)info.Height; diff --git a/ArcFormats/AnimeGameSystem/ArcDAT.cs b/ArcFormats/AnimeGameSystem/ArcDAT.cs index 287113f6..75eae8c0 100644 --- a/ArcFormats/AnimeGameSystem/ArcDAT.cs +++ b/ArcFormats/AnimeGameSystem/ArcDAT.cs @@ -92,7 +92,7 @@ namespace GameRes.Formats.Ags data[i] ^= key; key += earc.Key.Increment; } - return new MemoryStream (data); + return new BinMemoryStream (data, entry.Name); } public static readonly EncryptionScheme DefaultScheme = new EncryptionScheme { diff --git a/ArcFormats/AnimeGameSystem/AudioPCM.cs b/ArcFormats/AnimeGameSystem/AudioPCM.cs index 6f655b6e..c2e0c020 100644 --- a/ArcFormats/AnimeGameSystem/AudioPCM.cs +++ b/ArcFormats/AnimeGameSystem/AudioPCM.cs @@ -41,12 +41,12 @@ namespace GameRes.Formats.Ags Extensions = new string[] { "pcm" }; } - public override SoundInput TryOpen (Stream file) + public override SoundInput TryOpen (IBinaryStream file) { - uint signature = FormatCatalog.ReadSignature (file) & 0xF0FFFFFF; + uint signature = file.Signature & 0xF0FFFFFF; if (0x564157 != signature) // 'WAV' return null; - return new PcmInput (file); + return new PcmInput (file.AsStream); } } diff --git a/ArcFormats/AnimeGameSystem/ImageAinos.cs b/ArcFormats/AnimeGameSystem/ImageAinos.cs index 8f8ff3aa..3cc9cd25 100644 --- a/ArcFormats/AnimeGameSystem/ImageAinos.cs +++ b/ArcFormats/AnimeGameSystem/ImageAinos.cs @@ -28,10 +28,7 @@ using System.Collections.Generic; using System.ComponentModel.Composition; using System.IO; using System.Linq; -using System.Text; -using System.Windows; using System.Windows.Media; -using System.Windows.Media.Imaging; using GameRes.Utility; namespace GameRes.Formats.Ags @@ -196,40 +193,37 @@ namespace GameRes.Formats.Ags throw new System.NotImplementedException ("CgFormat.Write not implemented"); } - public override ImageMetaData ReadMetaData (Stream stream) + public override ImageMetaData ReadMetaData (IBinaryStream file) { - int sig = stream.ReadByte(); + int sig = file.ReadByte(); if (sig >= 0x20) return null; - using (var input = new ArcView.Reader (stream)) + int width = file.ReadInt16(); + int height = file.ReadInt16(); + if (width <= 0 || height <= 0 || width > 4096 || height > 4096) + return null; + var meta = new CgMetaData { - int width = input.ReadInt16(); - int height = input.ReadInt16(); - if (width <= 0 || height <= 0 || width > 4096 || height > 4096) + Width = (uint)width, + Height = (uint)height, + BPP = 24, + Type = sig, + }; + if (0 != (sig & 7)) + { + meta.OffsetX = file.ReadInt16(); + meta.OffsetY = file.ReadInt16(); + meta.Right = file.ReadInt16(); + meta.Bottom = file.ReadInt16(); + if (meta.OffsetX > meta.Right || meta.OffsetY > meta.Bottom || + meta.Right > width || meta.Bottom > height || + meta.OffsetX < 0 || meta.OffsetY < 0) return null; - var meta = new CgMetaData - { - Width = (uint)width, - Height = (uint)height, - BPP = 24, - Type = sig, - }; - if (0 != (sig & 7)) - { - meta.OffsetX = input.ReadInt16(); - meta.OffsetY = input.ReadInt16(); - meta.Right = input.ReadInt16(); - meta.Bottom = input.ReadInt16(); - if (meta.OffsetX > meta.Right || meta.OffsetY > meta.Bottom || - meta.Right > width || meta.Bottom > height || - meta.OffsetX < 0 || meta.OffsetY < 0) - return null; - } - return meta; } + return meta; } - public override ImageData Read (Stream stream, ImageMetaData info) + public override ImageData Read (IBinaryStream stream, ImageMetaData info) { var meta = (CgMetaData)info; using (var reader = new Reader (stream, meta)) @@ -239,9 +233,9 @@ namespace GameRes.Formats.Ags } } - internal class Reader : IDisposable + internal sealed class Reader : IDisposable { - BinaryReader m_input; + IBinaryStream m_input; byte[] m_output; int m_type; int m_width; @@ -253,7 +247,7 @@ namespace GameRes.Formats.Ags public byte[] Data { get { return m_output; } } - public Reader (Stream file, CgMetaData info, byte[] base_image = null) + public Reader (IBinaryStream file, CgMetaData info, byte[] base_image = null) { m_type = info.Type; m_width = (int)info.Width; @@ -263,13 +257,13 @@ namespace GameRes.Formats.Ags m_right = info.Right == 0 ? m_width : info.Right; m_bottom = info.Bottom == 0 ? m_height : info.Bottom; m_output = base_image ?? new byte[3*m_width*m_height]; - m_input = new BinaryReader (file, Encoding.ASCII, true); + m_input = file; ShiftTable = InitShiftTable(); if (0 != (info.Type & 7)) - file.Position = 13; + m_input.Position = 13; else - file.Position = 5; + m_input.Position = 5; } static readonly short[] ShiftX = new short[] { // 409b6c @@ -307,7 +301,7 @@ namespace GameRes.Formats.Ags int dst = left; while (dst != right) { - byte v9 = m_input.ReadByte(); + byte v9 = m_input.ReadUInt8(); if (0 != (v9 & 0x80)) { if (0 != (v9 & 0x40)) @@ -318,10 +312,10 @@ namespace GameRes.Formats.Ags } else { - byte v15 = m_input.ReadByte(); + byte v15 = m_input.ReadUInt8(); m_output[dst] = (byte)(((v9 << 1) + (v15 & 1)) << 1); m_output[dst + 1] = (byte)(v15 & 0xfe); - m_output[dst + 2] = m_input.ReadByte(); + m_output[dst + 2] = m_input.ReadUInt8(); } dst += 3; continue; @@ -330,13 +324,13 @@ namespace GameRes.Formats.Ags int count = v9 & 0xF; if (0 == count) { - count = (int)m_input.ReadByte() + 15; + count = (int)m_input.ReadUInt8() + 15; if (270 == count) { int v12; do { - v12 = m_input.ReadByte(); + v12 = m_input.ReadUInt8(); count += v12; } while (v12 == 0xff); @@ -364,7 +358,7 @@ namespace GameRes.Formats.Ags int dst = left; while (dst != right) { - byte v13 = m_input.ReadByte(); + byte v13 = m_input.ReadUInt8(); if (0 != (v13 & 0x80)) { int color = 3 * (v13 & 0x7F); @@ -378,13 +372,13 @@ namespace GameRes.Formats.Ags int count = v13 & 0xF; if (0 == count) { - count = m_input.ReadByte() + 15; + count = m_input.ReadUInt8() + 15; if (270 == count) { int v16; do { - v16 = m_input.ReadByte(); + v16 = m_input.ReadUInt8(); count += v16; } while (v16 == 0xff); @@ -403,25 +397,10 @@ namespace GameRes.Formats.Ags } #region IDisposable Members - bool disposed = false; - public void Dispose () { - Dispose (true); GC.SuppressFinalize (this); } - - protected virtual void Dispose (bool disposing) - { - if (!disposed) - { - if (disposing) - { - m_input.Dispose(); - } - disposed = true; - } - } #endregion } } diff --git a/ArcFormats/Ankh/ArcGRP.cs b/ArcFormats/Ankh/ArcGRP.cs index 334d2a45..00f66030 100644 --- a/ArcFormats/Ankh/ArcGRP.cs +++ b/ArcFormats/Ankh/ArcGRP.cs @@ -178,7 +178,7 @@ namespace GameRes.Formats.Ankh { var unpacked = new byte[entry.UnpackedSize]; reader.UnpackHDJ (unpacked, 0); - return new MemoryStream (unpacked); + return new BinMemoryStream (unpacked, entry.Name); } } @@ -201,7 +201,7 @@ namespace GameRes.Formats.Ankh reader.UnpackA (unpacked, header_size, channels); else reader.UnpackS (unpacked, header_size, channels); - return new MemoryStream (unpacked); + return new BinMemoryStream (unpacked, entry.Name); } } @@ -276,7 +276,7 @@ namespace GameRes.Formats.Ankh dst += count; } } - return new MemoryStream (output); + return new BinMemoryStream (output, entry.Name); } } } diff --git a/ArcFormats/Ankh/AudioPCM.cs b/ArcFormats/Ankh/AudioPCM.cs index a503e939..b38341ab 100644 --- a/ArcFormats/Ankh/AudioPCM.cs +++ b/ArcFormats/Ankh/AudioPCM.cs @@ -65,7 +65,7 @@ namespace GameRes.Formats.Ice if (0 == format.AverageBytesPerSecond || format.SamplesPerSecond * format.BlockAlign != format.AverageBytesPerSecond) return null; - var pcm = new StreamRegion (file, 0x16, pcm_size); + var pcm = new StreamRegion (file.AsStream, 0x16, pcm_size); return new RawPcmInput (pcm, format); } } diff --git a/ArcFormats/Aoi/ArcBOX.cs b/ArcFormats/Aoi/ArcBOX.cs index 1c6b4e3b..1267a04d 100644 --- a/ArcFormats/Aoi/ArcBOX.cs +++ b/ArcFormats/Aoi/ArcBOX.cs @@ -188,7 +188,7 @@ namespace GameRes.Formats.Aoi { data[i] ^= KeyFromOffset (offset++); } - return new MemoryStream (data); + return new BinMemoryStream (data, entry.Name); } static byte KeyFromOffset (uint offset) diff --git a/ArcFormats/Aoi/AudioAOG.cs b/ArcFormats/Aoi/AudioAOG.cs index 77de9396..20a788ee 100644 --- a/ArcFormats/Aoi/AudioAOG.cs +++ b/ArcFormats/Aoi/AudioAOG.cs @@ -36,14 +36,12 @@ namespace GameRes.Formats.Aoi public override string Description { get { return "Aoi engine audio format"; } } public override uint Signature { get { return 0x4F696F41; } } // 'AoiO' - public override SoundInput TryOpen (Stream file) + public override SoundInput TryOpen (IBinaryStream file) { - var header = new byte[0x30]; - if (header.Length != file.Read (header, 0, header.Length)) + var header = file.ReadHeader (0x30); + if (!header.AsciiEqual (0, "AoiOgg") || !header.AsciiEqual (0x2C, "OggS")) return null; - if (!Binary.AsciiEqual (header, 0, "AoiOgg") || !Binary.AsciiEqual (header, 0x2C, "OggS")) - return null; - var ogg = new StreamRegion (file, 0x2C); + var ogg = new StreamRegion (file.AsStream, 0x2C); return new OggInput (ogg); } } diff --git a/ArcFormats/Aoi/ImageAGF.cs b/ArcFormats/Aoi/ImageAGF.cs index 582a8cd0..4ae15269 100644 --- a/ArcFormats/Aoi/ImageAGF.cs +++ b/ArcFormats/Aoi/ImageAGF.cs @@ -118,7 +118,7 @@ namespace GameRes.Formats.Aoi case 5: count = (count >> 8) & 0xFF; - input.BaseStream.Seek ((count - count / 4) * 4, SeekOrigin.Current); + input.Seek ((count - count / 4) * 4, SeekOrigin.Current); count *= 4; break; @@ -134,9 +134,9 @@ namespace GameRes.Formats.Aoi var base_name = ReadBaseName (input, meta); if (VFS.FileExists (base_name)) { - using (var base_file = VFS.OpenSeekableStream (base_name)) + using (var base_file = VFS.OpenBinaryStream (base_name)) { - var base_image = Read (base_name, base_file); + var base_image = Read (base_file); BlendImage (meta, pixels, base_image.Bitmap); } } @@ -152,13 +152,13 @@ namespace GameRes.Formats.Aoi string ReadBaseName (IBinaryStream input, AgfMetaData info) { - input.BaseStream.Position = info.DataOffset + info.BaseNameOffset; + input.Position = info.DataOffset + info.BaseNameOffset; using (var reader = new BinaryReader (input.AsStream, Encoding.Unicode, true)) { var name = new StringBuilder(); for (;;) { - char c = input.ReadChar(); + char c = reader.ReadChar(); if (0 == c) break; name.Append (c); diff --git a/ArcFormats/ArcAST.cs b/ArcFormats/ArcAST.cs index bcb25c1f..48f03f2c 100644 --- a/ArcFormats/ArcAST.cs +++ b/ArcFormats/ArcAST.cs @@ -125,14 +125,14 @@ namespace GameRes.Formats.AST var data = arc.File.View.ReadBytes (entry.Offset, entry.Size); for (int i = 0; i < data.Length; ++i) data[i] ^= 0xff; - return new MemoryStream (data); + return new BinMemoryStream (data, entry.Name); } return arc.File.CreateStream (entry.Offset, entry.Size); } using (var input = arc.File.CreateStream (entry.Offset, entry.Size)) { var data = UnpackLzss (input, pent.Size, pent.UnpackedSize); - return new MemoryStream (data); + return new BinMemoryStream (data, entry.Name); } } diff --git a/ArcFormats/ArcAVC.cs b/ArcFormats/ArcAVC.cs index 51fdbc03..4718feff 100644 --- a/ArcFormats/ArcAVC.cs +++ b/ArcFormats/ArcAVC.cs @@ -91,7 +91,7 @@ namespace GameRes.Formats.AVC int base_offset = (int)(entry.Offset-arcf.HeaderOffset); for (int i = 0; i < data.Length; ++i) data[i] ^= arcf.Key[((base_offset+i)&7)]; - return new MemoryStream (data, false); + return new BinMemoryStream (data, entry.Name); } internal class AdvReader diff --git a/ArcFormats/ArcAil.cs b/ArcFormats/ArcAil.cs index dfd08143..eb06175c 100644 --- a/ArcFormats/ArcAil.cs +++ b/ArcFormats/ArcAil.cs @@ -122,7 +122,7 @@ namespace GameRes.Formats.Ail { byte[] data = new byte[pentry.UnpackedSize]; LzssUnpack (input, data); - return new MemoryStream (data); + return new BinMemoryStream (data, entry.Name); } } diff --git a/ArcFormats/ArcKogado.cs b/ArcFormats/ArcKogado.cs index 6a4c2aa4..214c1988 100644 --- a/ArcFormats/ArcKogado.cs +++ b/ArcFormats/ArcKogado.cs @@ -159,7 +159,7 @@ namespace GameRes.Formats.Kogado var unpacked = new byte[packed_entry.UnpackedSize]; var mariel = new MarielEncoder(); mariel.Unpack (input, unpacked, unpacked.Length); - return new MemoryStream (unpacked); + return new BinMemoryStream (unpacked, entry.Name); } finally { diff --git a/ArcFormats/ArcLST.cs b/ArcFormats/ArcLST.cs index 52dbef53..814fab66 100644 --- a/ArcFormats/ArcLST.cs +++ b/ArcFormats/ArcLST.cs @@ -150,7 +150,7 @@ namespace GameRes.Formats.Nexton var data = arc.File.View.ReadBytes (entry.Offset, entry.Size); for (int i = 0; i != data.Length; ++i) data[i] ^= nxent.Key; - return new MemoryStream (data); + return new BinMemoryStream (data, entry.Name); } private static string ReadName (ArcView view, long offset, uint size, uint key, Encoding enc) diff --git a/ArcFormats/ArcNEKO.cs b/ArcFormats/ArcNEKO.cs index f61c9eaa..34ce53f0 100644 --- a/ArcFormats/ArcNEKO.cs +++ b/ArcFormats/ArcNEKO.cs @@ -50,7 +50,7 @@ namespace GameRes.Formats.Neko /// /// Read a directory record from archive index. /// - DirRecord ReadDir (BinaryReader input); + DirRecord ReadDir (IBinaryStream input); /// /// Returns offset of an entry that immediately follows specified one. /// @@ -65,15 +65,14 @@ namespace GameRes.Formats.Neko internal sealed class IndexReader : IDisposable { - BinaryReader m_input; + IBinaryStream m_input; int m_index_size; long m_max_offset; INekoFormat m_format; public IndexReader (ArcView file, INekoFormat enc, byte[] index, int index_size) { - var input = new MemoryStream (index, 0, index_size); - m_input = new BinaryReader (input); + m_input = new BinMemoryStream (index, 0, index_size, file.Name); m_index_size = index_size; m_max_offset = file.MaxOffset; m_format = enc; @@ -85,7 +84,7 @@ namespace GameRes.Formats.Neko var files_map = GetNamesMap (KnownFileNames); var dir = new List(); - while (m_input.BaseStream.Position < m_index_size) + while (m_input.Position < m_index_size) { var dir_info = m_format.ReadDir (m_input); string dir_name; @@ -273,7 +272,7 @@ namespace GameRes.Formats.Neko return arc.File.CreateStream (entry.Offset, entry.Size); int length; var data = ReadBlock (arc.File.View, pak.Decoder, entry.Offset, out length); - return new MemoryStream (data, 0, length); + return new BinMemoryStream (data, 0, length, entry.Name); } static uint HashFromString (uint seed, byte[] str, int offset, int length) @@ -358,7 +357,7 @@ namespace GameRes.Formats.Neko return hash; } - public DirRecord ReadDir (BinaryReader input) + public DirRecord ReadDir (IBinaryStream input) { return new DirRecord { Hash = input.ReadUInt32(), @@ -468,7 +467,7 @@ namespace GameRes.Formats.Neko data = new byte[aligned_size]; arc.File.View.Read (entry.Offset+12, data, 0, (uint)size); narc.Decoder.Decrypt (key, data, 0, aligned_size); - return new MemoryStream (data, 0, size); + return new BinMemoryStream (data, 0, size, entry.Name); } } @@ -506,7 +505,7 @@ namespace GameRes.Formats.Neko return hash; } - public DirRecord ReadDir (BinaryReader input) + public DirRecord ReadDir (IBinaryStream input) { uint hash = input.ReadUInt32(); int count = input.ReadInt32(); diff --git a/ArcFormats/ArcPACKDAT.cs b/ArcFormats/ArcPACKDAT.cs index 92002c28..f9feddfe 100644 --- a/ArcFormats/ArcPACKDAT.cs +++ b/ArcFormats/ArcPACKDAT.cs @@ -102,7 +102,7 @@ namespace GameRes.Formats.SystemEpsylon } } } - return new MemoryStream (input); + return new BinMemoryStream (input, entry.Name); } } } diff --git a/ArcFormats/ArcPBX.cs b/ArcFormats/ArcPBX.cs index 97c75968..a3976b99 100644 --- a/ArcFormats/ArcPBX.cs +++ b/ArcFormats/ArcPBX.cs @@ -76,7 +76,7 @@ namespace GameRes.Formats.Terios using (var reader = new PandoraCompression (input, unpacked_size)) { var data = reader.Unpack(); - return new MemoryStream (data); + return new BinMemoryStream (data, entry.Name); } } catch diff --git a/ArcFormats/ArcSPack.cs b/ArcFormats/ArcSPack.cs index 95dd812b..185b97f7 100644 --- a/ArcFormats/ArcSPack.cs +++ b/ArcFormats/ArcSPack.cs @@ -108,7 +108,7 @@ namespace GameRes.Formats.SPack using (var reader = new PackedReader (packed_entry, input)) { reader.Unpack(); - return new MemoryStream (reader.Data); + return new BinMemoryStream (reader.Data, entry.Name); } } return input; diff --git a/ArcFormats/Astronauts/ArcGXP.cs b/ArcFormats/Astronauts/ArcGXP.cs index 95524999..0baba6f8 100644 --- a/ArcFormats/Astronauts/ArcGXP.cs +++ b/ArcFormats/Astronauts/ArcGXP.cs @@ -86,7 +86,7 @@ namespace GameRes.Formats.Astronauts { var data = arc.File.View.ReadBytes (entry.Offset, entry.Size); Decrypt (data, entry.Size); - return new MemoryStream (data); + return new BinMemoryStream (data, entry.Name); } static void Decrypt (byte[] data, uint length) diff --git a/ArcFormats/AudioMP3.cs b/ArcFormats/AudioMP3.cs index 584c02b6..0c208e95 100644 --- a/ArcFormats/AudioMP3.cs +++ b/ArcFormats/AudioMP3.cs @@ -94,11 +94,9 @@ namespace GameRes.Formats public override uint Signature { get { return 0; } } public override bool CanWrite { get { return false; } } - public override SoundInput TryOpen (Stream file) + public override SoundInput TryOpen (IBinaryStream file) { - byte[] header = new byte[10]; - if (header.Length != file.Read (header, 0, header.Length)) - return null; + var header = file.ReadHeader (10).ToArray(); long start_offset = SkipId3Tag (header); if (0 != start_offset) { @@ -109,7 +107,7 @@ namespace GameRes.Formats if (0xff != header[0] || 0xe2 != (header[1] & 0xe6) || 0xf0 == (header[2] & 0xf0)) return null; file.Position = 0; - return new Mp3Input (file); + return new Mp3Input (file.AsStream); } long SkipId3Tag (byte[] buffer) diff --git a/ArcFormats/AudioOGG.cs b/ArcFormats/AudioOGG.cs index 9ea80a1b..63bbe1db 100644 --- a/ArcFormats/AudioOGG.cs +++ b/ArcFormats/AudioOGG.cs @@ -131,9 +131,9 @@ namespace GameRes.Formats public override uint Signature { get { return 0x5367674f; } } // 'OggS' public override bool CanWrite { get { return false; } } - public override SoundInput TryOpen (Stream file) + public override SoundInput TryOpen (IBinaryStream file) { - return new OggInput (file); + return new OggInput (file.AsStream); } public static AudioFormat Instance { get { return s_OggFormat.Value; } } diff --git a/ArcFormats/AudioVOC.cs b/ArcFormats/AudioVOC.cs index a2a97199..9267a3a7 100644 --- a/ArcFormats/AudioVOC.cs +++ b/ArcFormats/AudioVOC.cs @@ -67,8 +67,8 @@ namespace GameRes.Formats.Creative switch (block_type) { case 1: - freq = m_input.ReadByte(); - codec = m_input.ReadByte(); + freq = m_input.ReadUInt8(); + codec = m_input.ReadUInt8(); Copy (block_size-2, pcm); m_format.Channels = 1; m_format.SamplesPerSecond = 1000000u / (256u - freq); @@ -80,16 +80,16 @@ namespace GameRes.Formats.Creative break; case 8: freq = m_input.ReadUInt16(); - codec = m_input.ReadByte(); - m_format.Channels = (ushort)(m_input.ReadByte()+1); + codec = m_input.ReadUInt8(); + m_format.Channels = (ushort)(m_input.ReadUInt8()+1); m_format.SamplesPerSecond = 256000000u / (Format.Channels * (65536u - freq)); m_format.BitsPerSample = 8; format_read = true; break; case 9: m_format.SamplesPerSecond = m_input.ReadUInt32(); - m_format.BitsPerSample = m_input.ReadByte(); - m_format.Channels = (ushort)(m_input.ReadByte()+1); + m_format.BitsPerSample = m_input.ReadUInt8(); + m_format.Channels = (ushort)(m_input.ReadUInt8()+1); codec = m_input.ReadUInt16(); format_read = true; m_input.ReadInt32(); diff --git a/ArcFormats/Banana/ImageMAG.cs b/ArcFormats/Banana/ImageMAG.cs index 01f7433c..92ceb708 100644 --- a/ArcFormats/Banana/ImageMAG.cs +++ b/ArcFormats/Banana/ImageMAG.cs @@ -45,20 +45,18 @@ namespace GameRes.Formats.Banana public override string Description { get { return "BANANA Shu-Shu image format"; } } public override uint Signature { get { return 0; } } - public override ImageMetaData ReadMetaData (Stream stream) + public override ImageMetaData ReadMetaData (IBinaryStream stream) { - var header = new byte[0x24]; - if (header.Length != stream.Read (header, 0, header.Length)) + var header = stream.ReadHeader (0x24); + if (0 != header.ToInt32 (0x10) || 0 != header.ToInt32 (0x14)) return null; - if (0 != LittleEndian.ToInt32 (header, 0x10) || 0 != LittleEndian.ToInt32 (header, 0x14)) - return null; - int left = LittleEndian.ToInt32 (header, 0); - int top = LittleEndian.ToInt32 (header, 4); - int right = LittleEndian.ToInt32 (header, 8); - int bottom = LittleEndian.ToInt32 (header, 0xC); - int back_width = LittleEndian.ToInt32 (header, 0x18); - int back_height = LittleEndian.ToInt32 (header, 0x1C); - uint alpha_channel = LittleEndian.ToUInt32 (header, 0x20); + int left = header.ToInt32 (0); + int top = header.ToInt32 (4); + int right = header.ToInt32 (8); + int bottom = header.ToInt32 (0xC); + int back_width = header.ToInt32 (0x18); + int back_height = header.ToInt32 (0x1C); + uint alpha_channel = header.ToUInt32 (0x20); int width = right - left; int height = bottom - top; if (left >= back_width || top >= back_height @@ -78,12 +76,12 @@ namespace GameRes.Formats.Banana }; } - public override ImageData Read (Stream stream, ImageMetaData info) + public override ImageData Read (IBinaryStream stream, ImageMetaData info) { int stride = (int)info.Width * 3; var pixels = new byte[stride * (int)info.Height]; stream.Position = 0x24; - using (var lz = new LzssStream (stream, LzssMode.Decompress, true)) + using (var lz = new LzssStream (stream.AsStream, LzssMode.Decompress, true)) { if (pixels.Length != lz.Read (pixels, 0, pixels.Length)) throw new InvalidFormatException(); @@ -104,7 +102,7 @@ namespace GameRes.Formats.Banana stream.Position = 0x24 + meta.AlphaOffset; var alpha = new byte[meta.BackWidth*meta.BackHeight]; - using (var lz = new LzssStream (stream, LzssMode.Decompress, true)) + using (var lz = new LzssStream (stream.AsStream, LzssMode.Decompress, true)) { if (alpha.Length != lz.Read (alpha, 0, alpha.Length)) throw new InvalidFormatException(); diff --git a/ArcFormats/Bishop/ImageBSG.cs b/ArcFormats/Bishop/ImageBSG.cs index acc40d1d..827bb623 100644 --- a/ArcFormats/Bishop/ImageBSG.cs +++ b/ArcFormats/Bishop/ImageBSG.cs @@ -49,36 +49,34 @@ namespace GameRes.Formats.Bishop public override string Description { get { return "Bishop image format"; } } public override uint Signature { get { return 0x2D535342; } } // 'BSS-' - public override ImageMetaData ReadMetaData (Stream stream) + public override ImageMetaData ReadMetaData (IBinaryStream stream) { - var header = new byte[0x60]; - if (header.Length != stream.Read (header, 0, header.Length)) - return null; + var header = stream.ReadHeader (0x60); int base_offset = 0; - if (Binary.AsciiEqual (header, 0, "BSS-Composition\0")) + if (header.AsciiEqual ("BSS-Composition\0")) base_offset = 0x20; - if (!Binary.AsciiEqual (header, base_offset, "BSS-Graphics\0")) + if (!header.AsciiEqual (base_offset, "BSS-Graphics\0")) return null; int type = header[base_offset+0x30]; if (type > 2) return null; return new BsgMetaData { - Width = LittleEndian.ToUInt16 (header, base_offset+0x16), - Height = LittleEndian.ToUInt16 (header, base_offset+0x18), - OffsetX = LittleEndian.ToInt16 (header, base_offset+0x20), - OffsetY = LittleEndian.ToInt16 (header, base_offset+0x22), - UnpackedSize = LittleEndian.ToInt32 (header, base_offset+0x12), + Width = header.ToUInt16 (base_offset+0x16), + Height = header.ToUInt16 (base_offset+0x18), + OffsetX = header.ToInt16 (base_offset+0x20), + OffsetY = header.ToInt16 (base_offset+0x22), + UnpackedSize = header.ToInt32 (base_offset+0x12), BPP = 2 == type ? 8 : 32, ColorMode = type, CompressionMode = header[base_offset+0x31], - DataOffset = LittleEndian.ToInt32 (header, base_offset+0x32)+base_offset, - DataSize = LittleEndian.ToInt32 (header, base_offset+0x36), - PaletteOffset = LittleEndian.ToInt32 (header, base_offset+0x3A)+base_offset, + DataOffset = header.ToInt32 (base_offset+0x32)+base_offset, + DataSize = header.ToInt32 (base_offset+0x36), + PaletteOffset = header.ToInt32 (base_offset+0x3A)+base_offset, }; } - public override ImageData Read (Stream stream, ImageMetaData info) + public override ImageData Read (IBinaryStream stream, ImageMetaData info) { var meta = (BsgMetaData)info; using (var reader = new BsgReader (stream, meta)) @@ -96,7 +94,7 @@ namespace GameRes.Formats.Bishop internal sealed class BsgReader : IDisposable { - BinaryReader m_input; + IBinaryStream m_input; BsgMetaData m_info; byte[] m_output; @@ -105,13 +103,13 @@ namespace GameRes.Formats.Bishop public BitmapPalette Palette { get; private set; } public int Stride { get; private set; } - public BsgReader (Stream input, BsgMetaData info) + public BsgReader (IBinaryStream input, BsgMetaData info) { m_info = info; if (m_info.CompressionMode > 2) throw new NotSupportedException ("Not supported BSS Graphics compression"); - m_input = new ArcView.Reader (input); + m_input = input; m_output = new byte[m_info.UnpackedSize]; switch (m_info.ColorMode) { @@ -133,7 +131,7 @@ namespace GameRes.Formats.Bishop public void Unpack () { - m_input.BaseStream.Position = m_info.DataOffset; + m_input.Position = m_info.DataOffset; if (0 == m_info.CompressionMode) { if (1 == m_info.ColorMode) @@ -179,13 +177,13 @@ namespace GameRes.Formats.Bishop int remaining = m_input.ReadInt32(); while (remaining > 0) { - int count = m_input.ReadSByte(); + int count = m_input.ReadInt8(); --remaining; if (count >= 0) { for (int i = 0; i <= count; ++i) { - m_output[dst] = m_input.ReadByte(); + m_output[dst] = m_input.ReadUInt8(); --remaining; dst += pixel_size; } @@ -193,7 +191,7 @@ namespace GameRes.Formats.Bishop else { count = 1 - count; - byte repeat = m_input.ReadByte(); + byte repeat = m_input.ReadUInt8(); --remaining; for (int i = 0; i < count; ++i) { @@ -207,20 +205,20 @@ namespace GameRes.Formats.Bishop void UnpackLz (int plane, int pixel_size) { int dst = plane; - byte control = m_input.ReadByte(); + byte control = m_input.ReadUInt8(); int remaining = m_input.ReadInt32() - 5; while (remaining > 0) { - byte c = m_input.ReadByte(); + byte c = m_input.ReadUInt8(); --remaining; if (c == control) { - int offset = m_input.ReadByte(); + int offset = m_input.ReadUInt8(); --remaining; if (offset != control) { - int count = m_input.ReadByte(); + int count = m_input.ReadUInt8(); --remaining; if (offset > control) @@ -245,7 +243,7 @@ namespace GameRes.Formats.Bishop BitmapPalette ReadPalette () { - m_input.BaseStream.Position = m_info.PaletteOffset; + m_input.Position = m_info.PaletteOffset; var palette_data = new byte[0x400]; if (palette_data.Length != m_input.Read (palette_data, 0, palette_data.Length)) throw new InvalidFormatException(); @@ -259,14 +257,8 @@ namespace GameRes.Formats.Bishop } #region IDisposable Members - bool _disposed = false; public void Dispose () { - if (!_disposed) - { - m_input.Dispose(); - _disposed = true; - } } #endregion } diff --git a/ArcFormats/BlackCyc/AudioVAW.cs b/ArcFormats/BlackCyc/AudioVAW.cs index a0df616c..4840c162 100644 --- a/ArcFormats/BlackCyc/AudioVAW.cs +++ b/ArcFormats/BlackCyc/AudioVAW.cs @@ -44,7 +44,7 @@ namespace GameRes.Formats.BlackCyc Extensions = new string[] { "vaw", "wgq" }; } - public override SoundInput TryOpen (Stream file) + public override SoundInput TryOpen (IBinaryStream file) { var header = ResourceHeader.Read (file); if (null == header) @@ -76,8 +76,8 @@ namespace GameRes.Formats.BlackCyc } else return null; - var input = new StreamRegion (file, offset, file.Length-offset); - return format.TryOpen (input); + var input = new StreamRegion (file.AsStream, offset, file.Length-offset); + return format.TryOpen (new BinaryStream (input, file.Name)); } public override void Write (SoundInput source, Stream output) @@ -85,7 +85,7 @@ namespace GameRes.Formats.BlackCyc throw new System.NotImplementedException ("EdimFormat.Write not implemenented"); } - SoundInput Unpack (Stream input) + SoundInput Unpack (IBinaryStream input) { input.Position = 0x40; var header = new byte[0x24]; @@ -106,7 +106,7 @@ namespace GameRes.Formats.BlackCyc { pcm.Write (header, 0, header_size); using (var output = new BinaryWriter (pcm, Encoding.Default, true)) - using (var bits = new LsbBitStream (input, true)) + using (var bits = new LsbBitStream (input.AsStream, true)) { int written = 0; short sample = 0; @@ -128,7 +128,7 @@ namespace GameRes.Formats.BlackCyc } } pcm.Position = 0; - var sound = Wav.TryOpen (pcm); + var sound = Wav.TryOpen (new BinMemoryStream (pcm, input.Name)); if (sound != null) input.Dispose(); else diff --git a/ArcFormats/BlackCyc/ImageDWQ.cs b/ArcFormats/BlackCyc/ImageDWQ.cs index 0de67ae3..5932f0e1 100644 --- a/ArcFormats/BlackCyc/ImageDWQ.cs +++ b/ArcFormats/BlackCyc/ImageDWQ.cs @@ -43,11 +43,9 @@ namespace GameRes.Formats.BlackCyc public int PackType { get; private set; } public bool AType { get; private set; } - public static ResourceHeader Read (Stream file) + public static ResourceHeader Read (IBinaryStream file) { - var header = new byte[0x40]; - if (0x40 != file.Read (header, 0, 0x40)) - return null; + var header = file.ReadHeader (0x40).ToArray(); var header_string = Encoding.ASCII.GetString (header, 0x30, 0x10); var match = PackTypeRe.Match (header_string); @@ -88,9 +86,9 @@ namespace GameRes.Formats.BlackCyc }; } - public override ImageMetaData ReadMetaData (Stream stream) + public override ImageMetaData ReadMetaData (IBinaryStream file) { - var header = ResourceHeader.Read (stream); + var header = ResourceHeader.Read (file); if (null == header) return null; if (Binary.AsciiEqual (header.Bytes, "IF PACKTYPE==")) @@ -99,7 +97,8 @@ namespace GameRes.Formats.BlackCyc !Binary.AsciiEqual (header.Bytes, 0x2C, "BMP ") || header.PackType != 0 && header.PackType != 1) return null; - using (var bmp = new StreamRegion (stream, 0x40, true)) + using (var reg = new StreamRegion (file.AsStream, 0x40, true)) + using (var bmp = new BinaryStream (reg, file.Name)) { var info = Bmp.ReadMetaData (bmp); if (null == info) @@ -110,7 +109,7 @@ namespace GameRes.Formats.BlackCyc Height = info.Height, BPP = info.BPP, BaseType = "BMP", - PackedSize = (int)(stream.Length-0x40), + PackedSize = (int)(file.Length-0x40), PackType = header.PackType, HasAlpha = header.AType, }; @@ -122,7 +121,7 @@ namespace GameRes.Formats.BlackCyc case 0: // BMP case 5: // JPEG case 8: // PNG - packed_size = (int)(stream.Length-0x40); + packed_size = (int)(file.Length-0x40); break; case 2: // BMP+MASK @@ -146,12 +145,13 @@ namespace GameRes.Formats.BlackCyc }; } - public override ImageData Read (Stream stream, ImageMetaData info) + public override ImageData Read (IBinaryStream stream, ImageMetaData info) { var meta = (DwqMetaData)info; BitmapSource bitmap = null; - using (var input = new StreamRegion (stream, 0x40, meta.PackedSize, true)) + using (var sreg = new StreamRegion (stream.AsStream, 0x40, meta.PackedSize, true)) + using (var input = new BinaryStream (sreg, stream.Name)) { switch (meta.PackType) { @@ -192,7 +192,7 @@ namespace GameRes.Formats.BlackCyc int mask_offset = 0x40+meta.PackedSize; if (mask_offset != stream.Length) { - using (var mask = new StreamRegion (stream, mask_offset, true)) + using (var mask = new StreamRegion (stream.AsStream, mask_offset, true)) { var reader = new DwqBmpReader (mask, meta); if (8 == reader.Format.BitsPerPixel) // mask should be represented as 8bpp bitmap @@ -238,17 +238,15 @@ namespace GameRes.Formats.BlackCyc PixelFormats.Bgra32, null, pixels, stride); } - private BitmapSource ReadFuckedUpBmpImage (Stream file, ImageMetaData info) + private BitmapSource ReadFuckedUpBmpImage (IBinaryStream file, ImageMetaData info) { - var header = new byte[0x36]; - if (header.Length != file.Read (header, 0, header.Length)) - throw new InvalidFormatException(); - int w = LittleEndian.ToInt32 (header, 0x12); - int h = LittleEndian.ToInt32 (header, 0x16); + var header = file.ReadHeader (0x36); + int w = header.ToInt32 (0x12); + int h = header.ToInt32 (0x16); if (w != info.Width || h != info.Height) throw new InvalidFormatException(); - int bpp = LittleEndian.ToUInt16 (header, 0x1c); + int bpp = header.ToUInt16 (0x1c); PixelFormat format; switch (bpp) { @@ -261,8 +259,8 @@ namespace GameRes.Formats.BlackCyc BitmapPalette palette = null; if (8 == bpp) { - int colors = Math.Min (LittleEndian.ToInt32 (header, 0x2E), 0x100); - palette = DwqBmpReader.ReadPalette (file, colors); + int colors = Math.Min (header.ToInt32 (0x2E), 0x100); + palette = DwqBmpReader.ReadPalette (file.AsStream, colors); } int pixel_size = bpp / 8; int stride = ((int)info.Width * pixel_size + 3) & ~3; diff --git a/ArcFormats/BlackRainbow/ImageBMD.cs b/ArcFormats/BlackRainbow/ImageBMD.cs index 8d1830d4..e6b6e447 100644 --- a/ArcFormats/BlackRainbow/ImageBMD.cs +++ b/ArcFormats/BlackRainbow/ImageBMD.cs @@ -51,31 +51,25 @@ namespace GameRes.Formats.BlackRainbow public override uint Signature { get { return 0x444d425fu; } } // '_BMD' public override bool CanWrite { get { return true; } } - public override ImageMetaData ReadMetaData (Stream stream) + public override ImageMetaData ReadMetaData (IBinaryStream stream) { - var header = new byte[0x14]; - if (header.Length != stream.Read (header, 0, header.Length)) - return null; - + var header = stream.ReadHeader (0x14); return new BmdMetaData { - Width = LittleEndian.ToUInt32 (header, 8), - Height = LittleEndian.ToUInt32 (header, 12), + Width = header.ToUInt32 (8), + Height = header.ToUInt32 (12), BPP = 32, - PackedSize = LittleEndian.ToUInt32 (header, 4), - Flag = LittleEndian.ToInt32 (header, 0x10), + PackedSize = header.ToUInt32 (4), + Flag = header.ToInt32 (0x10), }; } - public override ImageData Read (Stream stream, ImageMetaData info) + public override ImageData Read (IBinaryStream stream, ImageMetaData info) { - var meta = info as BmdMetaData; - if (null == meta) - throw new ArgumentException ("BmdFormat.Read should be supplied with BmdMetaData", "info"); - + var meta = (BmdMetaData)info; stream.Position = 0x14; int image_size = (int)(meta.Width*meta.Height*4); - using (var reader = new LzssReader (stream, (int)meta.PackedSize, image_size)) + using (var reader = new LzssReader (stream.AsStream, (int)meta.PackedSize, image_size)) { PixelFormat format = meta.Flag != 0 ? PixelFormats.Bgra32 : PixelFormats.Bgr32; reader.Unpack(); diff --git a/ArcFormats/BlackRainbow/ImageBMZ.cs b/ArcFormats/BlackRainbow/ImageBMZ.cs index 5cde5ebf..20e231c0 100644 --- a/ArcFormats/BlackRainbow/ImageBMZ.cs +++ b/ArcFormats/BlackRainbow/ImageBMZ.cs @@ -55,21 +55,21 @@ namespace GameRes.Formats.BlackRainbow } } - public override ImageMetaData ReadMetaData (Stream stream) + public override ImageMetaData ReadMetaData (IBinaryStream file) { - var header = new byte[8]; - if (header.Length != stream.Read (header, 0, header.Length)) - return null; - using (var zstream = new ZLibStream (stream, CompressionMode.Decompress, true)) - return base.ReadMetaData (zstream); + var header = file.ReadHeader (8); + using (var zstream = new ZLibStream (file.AsStream, CompressionMode.Decompress, true)) + using (var bmp = new BinaryStream (zstream, file.Name)) + return base.ReadMetaData (bmp); } - public override ImageData Read (Stream stream, ImageMetaData info) + public override ImageData Read (IBinaryStream file, ImageMetaData info) { - stream.Seek (8, SeekOrigin.Current); - using (var zstream = new ZLibStream (stream, CompressionMode.Decompress, true)) + file.Seek (8, SeekOrigin.Current); + using (var zstream = new ZLibStream (file.AsStream, CompressionMode.Decompress, true)) using (var input = new SeekableStream (zstream)) - return base.Read (input, info); + using (var bmp = new BinaryStream (input, file.Name)) + return base.Read (bmp, info); } } } diff --git a/ArcFormats/BlueGale/ImageZBM.cs b/ArcFormats/BlueGale/ImageZBM.cs index dd588f48..f9824320 100644 --- a/ArcFormats/BlueGale/ImageZBM.cs +++ b/ArcFormats/BlueGale/ImageZBM.cs @@ -43,48 +43,40 @@ namespace GameRes.Formats.BlueGale public override string Description { get { return "BlueGale compressed image format"; } } public override uint Signature { get { return 0x5F706D61; } } // 'amp_' - public ZbmFormat () - { - Extensions = new string[] { "zbm" }; - } - - public override ImageMetaData ReadMetaData (Stream stream) + public override ImageMetaData ReadMetaData (IBinaryStream stream) { stream.Position = 4; - using (var reader = new ArcView.Reader (stream)) + int version = stream.ReadInt16(); + if (version != 1) + return null; + int unpacked_size = stream.ReadInt32(); + int data_offset = stream.ReadInt32(); + if (unpacked_size < 0x36 || data_offset < stream.Position) + return null; + var header = new byte[0x20]; + stream.Position = data_offset; + Unpack (stream.AsStream, header); + Decrypt (header); + if ('B' != header[0] || 'M' != header[1]) + return null; + return new ZbmMetaData { - int version = reader.ReadInt16(); - if (version != 1) - return null; - int unpacked_size = reader.ReadInt32(); - int data_offset = reader.ReadInt32(); - if (unpacked_size < 0x36 || data_offset < stream.Position) - return null; - var header = new byte[0x20]; - stream.Position = data_offset; - Unpack (stream, header); - Decrypt (header); - if ('B' != header[0] || 'M' != header[1]) - return null; - return new ZbmMetaData - { - Width = LittleEndian.ToUInt32 (header, 0x12), - Height = LittleEndian.ToUInt32 (header, 0x16), - BPP = LittleEndian.ToInt16 (header, 0x1C), - UnpackedSize = unpacked_size, - DataOffset = data_offset, - }; - } + Width = LittleEndian.ToUInt32 (header, 0x12), + Height = LittleEndian.ToUInt32 (header, 0x16), + BPP = LittleEndian.ToInt16 (header, 0x1C), + UnpackedSize = unpacked_size, + DataOffset = data_offset, + }; } - public override ImageData Read (Stream stream, ImageMetaData info) + public override ImageData Read (IBinaryStream stream, ImageMetaData info) { var meta = (ZbmMetaData)info; var data = new byte[meta.UnpackedSize]; stream.Position = meta.DataOffset; - Unpack (stream, data); + Unpack (stream.AsStream, data); Decrypt (data); - using (var bmp = new MemoryStream (data)) + using (var bmp = new BinMemoryStream (data, stream.Name)) return Bmp.Read (bmp, info); } diff --git a/ArcFormats/BlueGale/VideoAMV.cs b/ArcFormats/BlueGale/VideoAMV.cs index c87594d9..f30f6fbd 100644 --- a/ArcFormats/BlueGale/VideoAMV.cs +++ b/ArcFormats/BlueGale/VideoAMV.cs @@ -90,7 +90,7 @@ namespace GameRes.Formats.BlueGale LittleEndian.Pack (pent.UnpackedSize, output, 2); int header_size = LittleEndian.ToInt32 (output, 0xE); LittleEndian.Pack (header_size+0xE, output, 0xA); - return new MemoryStream (output); + return new BinMemoryStream (output, entry.Name); } } } diff --git a/ArcFormats/Bruns/ImageEENC.cs b/ArcFormats/Bruns/ImageEENC.cs index bf048a24..5e274aac 100644 --- a/ArcFormats/Bruns/ImageEENC.cs +++ b/ArcFormats/Bruns/ImageEENC.cs @@ -102,7 +102,7 @@ namespace GameRes.Formats.Bruns if (meta.Compressed) input = new ZLibStream (input, CompressionMode.Decompress); using (var bin = new BinaryStream (input, stream.Name, true)) - return meta.Format.Read (input, meta.Info); + return meta.Format.Read (bin, meta.Info); } finally { diff --git a/ArcFormats/CaramelBox/ArcARC3.cs b/ArcFormats/CaramelBox/ArcARC3.cs index 1a17c2a9..f62309d4 100644 --- a/ArcFormats/CaramelBox/ArcARC3.cs +++ b/ArcFormats/CaramelBox/ArcARC3.cs @@ -200,7 +200,7 @@ namespace GameRes.Formats.CaramelBox using (input) { var data = UnpackLze (input, a3ent.UnpackedSize); - return new MemoryStream (data); + return new BinMemoryStream (data, entry.Name); } } diff --git a/ArcFormats/CaramelBox/ArcARC4.cs b/ArcFormats/CaramelBox/ArcARC4.cs index a51f0c47..d97f257d 100644 --- a/ArcFormats/CaramelBox/ArcARC4.cs +++ b/ArcFormats/CaramelBox/ArcARC4.cs @@ -130,7 +130,7 @@ namespace GameRes.Formats.CaramelBox return input; using (input) using (var tz = new TzCompression (input)) - return new MemoryStream (tz.Unpack()); + return new BinMemoryStream (tz.Unpack(), entry.Name); } static int ReadInt24 (byte[] data, int pos) diff --git a/ArcFormats/CatSystem/ArcINT.cs b/ArcFormats/CatSystem/ArcINT.cs index 558559dd..8718747e 100644 --- a/ArcFormats/CatSystem/ArcINT.cs +++ b/ArcFormats/CatSystem/ArcINT.cs @@ -208,7 +208,7 @@ namespace GameRes.Formats.CatSystem { byte[] data = arc.File.View.ReadBytes (entry.Offset, entry.Size); arc.Encryption.Decipher (data, data.Length/8*8); - return new MemoryStream (data); + return new BinMemoryStream (data, entry.Name); } public override Stream OpenEntry (ArcFile arc, Entry entry) diff --git a/ArcFormats/CatSystem/ImageHG2.cs b/ArcFormats/CatSystem/ImageHG2.cs index ddd49041..d0b3ebe2 100644 --- a/ArcFormats/CatSystem/ImageHG2.cs +++ b/ArcFormats/CatSystem/ImageHG2.cs @@ -44,37 +44,34 @@ namespace GameRes.Formats.CatSystem public override string Description { get { return "CatSystem engine image format"; } } public override uint Signature { get { return 0x322D4748; } } // 'HG-2' - public override ImageMetaData ReadMetaData (Stream stream) + public override ImageMetaData ReadMetaData (IBinaryStream stream) { stream.Position = 8; - using (var header = new ArcView.Reader (stream)) - { - var info = new Hg2MetaData(); - int type = header.ReadInt32(); - if (0x25 == type) - info.HeaderSize = 0x58; - else if (0x20 == type) - info.HeaderSize = 0x50; - else - return null; - info.Width = header.ReadUInt32(); - info.Height = header.ReadUInt32(); - info.BPP = header.ReadInt32(); - header.BaseStream.Seek (8, SeekOrigin.Current); - info.DataPacked = header.ReadInt32(); - info.DataUnpacked = header.ReadInt32(); - info.CtlPacked = header.ReadInt32(); - info.CtlUnpacked = header.ReadInt32(); - header.BaseStream.Seek (8, SeekOrigin.Current); - info.CanvasWidth = header.ReadUInt32(); - info.CanvasHeight = header.ReadUInt32(); - info.OffsetX = header.ReadInt32(); - info.OffsetY = header.ReadInt32(); - return info; - } + var info = new Hg2MetaData(); + int type = stream.ReadInt32(); + if (0x25 == type) + info.HeaderSize = 0x58; + else if (0x20 == type) + info.HeaderSize = 0x50; + else + return null; + info.Width = stream.ReadUInt32(); + info.Height = stream.ReadUInt32(); + info.BPP = stream.ReadInt32(); + stream.Seek (8, SeekOrigin.Current); + info.DataPacked = stream.ReadInt32(); + info.DataUnpacked = stream.ReadInt32(); + info.CtlPacked = stream.ReadInt32(); + info.CtlUnpacked = stream.ReadInt32(); + stream.Seek (8, SeekOrigin.Current); + info.CanvasWidth = stream.ReadUInt32(); + info.CanvasHeight = stream.ReadUInt32(); + info.OffsetX = stream.ReadInt32(); + info.OffsetY = stream.ReadInt32(); + return info; } - public override ImageData Read (Stream stream, ImageMetaData info) + public override ImageData Read (IBinaryStream stream, ImageMetaData info) { using (var reader = new Hg2Reader (stream, (Hg2MetaData)info)) { @@ -94,7 +91,7 @@ namespace GameRes.Formats.CatSystem { Hg2MetaData m_hg2; - public Hg2Reader (Stream input, Hg2MetaData info) : base (input, info) + public Hg2Reader (IBinaryStream input, Hg2MetaData info) : base (input, info) { m_hg2 = info; } diff --git a/ArcFormats/CatSystem/ImageHG3.cs b/ArcFormats/CatSystem/ImageHG3.cs index a3131e41..9a6baf2d 100644 --- a/ArcFormats/CatSystem/ImageHG3.cs +++ b/ArcFormats/CatSystem/ImageHG3.cs @@ -25,7 +25,6 @@ using System; using System.IO; -using System.Linq; using System.ComponentModel.Composition; using System.Windows.Media.Imaging; using System.Windows.Media; @@ -48,35 +47,34 @@ namespace GameRes.Formats.CatSystem public override string Description { get { return "CatSystem engine image format"; } } public override uint Signature { get { return 0x332d4748; } } // 'HG-3' - public override ImageMetaData ReadMetaData (Stream stream) + public override ImageMetaData ReadMetaData (IBinaryStream stream) { - var header = new byte[0x4c]; - if (0x4c != stream.Read (header, 0, header.Length)) + var header = stream.ReadHeader (0x4c); + if (header.ToUInt32 (4) != 0x0c) return null; - if (LittleEndian.ToUInt32 (header, 4) != 0x0c) - return null; - if (!Binary.AsciiEqual (header, 0x14, "stdinfo\0")) + if (!header.AsciiEqual (0x14, "stdinfo\0")) return null; return new HgMetaData { - HeaderSize = LittleEndian.ToUInt32 (header, 0x1C), - Width = LittleEndian.ToUInt32 (header, 0x24), - Height = LittleEndian.ToUInt32 (header, 0x28), - OffsetX = LittleEndian.ToInt32 (header, 0x30), - OffsetY = LittleEndian.ToInt32 (header, 0x34), - BPP = LittleEndian.ToInt32 (header, 0x2C), - CanvasWidth = LittleEndian.ToUInt32 (header, 0x44), - CanvasHeight = LittleEndian.ToUInt32 (header, 0x48), + HeaderSize = header.ToUInt32 (0x1C), + Width = header.ToUInt32 (0x24), + Height = header.ToUInt32 (0x28), + OffsetX = header.ToInt32 (0x30), + OffsetY = header.ToInt32 (0x34), + BPP = header.ToInt32 (0x2C), + CanvasWidth = header.ToUInt32 (0x44), + CanvasHeight = header.ToUInt32 (0x48), }; } - public override ImageData Read (Stream stream, ImageMetaData info) + public override ImageData Read (IBinaryStream stream, ImageMetaData info) { var meta = (HgMetaData)info; if (0x20 != meta.BPP) throw new NotSupportedException ("Not supported HG-3 color depth"); - using (var input = new StreamRegion (stream, 0x14, true)) + using (var reg = new StreamRegion (stream.AsStream, 0x14, true)) + using (var input = new BinaryStream (reg, stream.Name)) using (var reader = new Hg3Reader (input, meta)) { var pixels = reader.Unpack(); @@ -95,17 +93,17 @@ namespace GameRes.Formats.CatSystem internal class HgReader : IDisposable { - private BinaryReader m_input; + private IBinaryStream m_input; protected HgMetaData m_info; protected int m_pixel_size; - protected BinaryReader Input { get { return m_input; } } - protected Stream InputStream { get { return m_input.BaseStream; } } + protected IBinaryStream Input { get { return m_input; } } + protected Stream InputStream { get { return m_input.AsStream; } } public int Stride { get; protected set; } - protected HgReader (Stream input, HgMetaData info) + protected HgReader (IBinaryStream input, HgMetaData info) { - m_input = new ArcView.Reader (input); + m_input = input; m_info = info; m_pixel_size = m_info.BPP / 8; Stride = (int)m_info.Width * m_pixel_size; @@ -228,25 +226,10 @@ namespace GameRes.Formats.CatSystem } #region IDisposable Members - bool _disposed = false; - public void Dispose () { - Dispose (true); GC.SuppressFinalize (this); } - - protected virtual void Dispose (bool disposing) - { - if (!_disposed) - { - if (disposing) - { - m_input.Dispose(); - } - _disposed = true; - } - } #endregion } @@ -254,17 +237,17 @@ namespace GameRes.Formats.CatSystem { public bool Flipped { get; private set; } - public Hg3Reader (Stream input, HgMetaData info) : base (input, info) + public Hg3Reader (IBinaryStream input, HgMetaData info) : base (input, info) { } public byte[] Unpack () { InputStream.Position = m_info.HeaderSize; - var img_type = Input.ReadChars (8); - if (img_type.SequenceEqual ("img0000\0")) + var img_type = Input.ReadBytes (8); + if (Binary.AsciiEqual (img_type, "img0000\0")) return UnpackImg0000(); - else if (img_type.SequenceEqual ("img_jpg\0")) + else if (Binary.AsciiEqual (img_type, "img_jpg\0")) return UnpackJpeg(); else throw new NotSupportedException ("Not supported HG-3 image"); @@ -309,11 +292,11 @@ namespace GameRes.Formats.CatSystem src += src_pixel_size; } - InputStream.Position = next_section; - var section_header = Input.ReadChars (8); - if (!section_header.SequenceEqual ("img_al\0\0")) + Input.Position = next_section; + var section_header = Input.ReadBytes (8); + if (!Binary.AsciiEqual (section_header, "img_al\0\0")) return output; - InputStream.Seek (8, SeekOrigin.Current); + Input.Seek (8, SeekOrigin.Current); int alpha_size = Input.ReadInt32(); using (var alpha_in = new StreamRegion (InputStream, InputStream.Position+4, alpha_size, true)) using (var alpha = new ZLibStream (alpha_in, CompressionMode.Decompress)) diff --git a/ArcFormats/Cherry/ArcCherry.cs b/ArcFormats/Cherry/ArcCherry.cs index 27f31117..2f3c6f8e 100644 --- a/ArcFormats/Cherry/ArcCherry.cs +++ b/ArcFormats/Cherry/ArcCherry.cs @@ -110,7 +110,7 @@ namespace GameRes.Formats.Cherry { data[text_offset+i] ^= (byte)i; } - return new MemoryStream (data); + return new BinMemoryStream (data, entry.Name); } } @@ -209,7 +209,7 @@ namespace GameRes.Formats.Cherry } Decrypt (data, 0x18, (int)(data.Length - 0x18)); } - return new MemoryStream (data); + return new BinMemoryStream (data, entry.Name); } } } diff --git a/ArcFormats/Cherry/ImageGRP.cs b/ArcFormats/Cherry/ImageGRP.cs index 897448df..2afc0c11 100644 --- a/ArcFormats/Cherry/ImageGRP.cs +++ b/ArcFormats/Cherry/ImageGRP.cs @@ -54,16 +54,14 @@ namespace GameRes.Formats.Cherry Extensions = new string[] { "grp" }; } - public override ImageMetaData ReadMetaData (Stream stream) + public override ImageMetaData ReadMetaData (IBinaryStream stream) { - var header = new byte[0x18]; - if (header.Length != stream.Read (header, 0, header.Length)) - return null; - uint width = LittleEndian.ToUInt32 (header, 0); - uint height = LittleEndian.ToUInt32 (header, 4); - int bpp = LittleEndian.ToInt32 (header, 8); - int packed_size = LittleEndian.ToInt32 (header, 0x0C); - int unpacked_size = LittleEndian.ToInt32 (header, 0x10); + var header = stream.ReadHeader (0x18); + uint width = header.ToUInt32 (0); + uint height = header.ToUInt32 (4); + int bpp = header.ToInt32 (8); + int packed_size = header.ToInt32 (0x0C); + int unpacked_size = header.ToInt32 (0x10); if (0 == width || 0 == height || width > 0x7fff || height > 0x7fff || (bpp != 24 && bpp != 8) || unpacked_size <= 0 || packed_size < 0) @@ -75,16 +73,16 @@ namespace GameRes.Formats.Cherry BPP = bpp, PackedSize = packed_size, UnpackedSize = unpacked_size, - Offset = LittleEndian.ToInt32 (header, 0x14), + Offset = header.ToInt32 (0x14), HeaderSize = 0x18, AlphaChannel = false, }; } - public override ImageData Read (Stream stream, ImageMetaData info) + public override ImageData Read (IBinaryStream file, ImageMetaData info) { var meta = (GrpMetaData)info; - var reader = new GrpReader (stream, meta); + var reader = new GrpReader (file.AsStream, meta); return reader.CreateImage(); } @@ -101,18 +99,16 @@ namespace GameRes.Formats.Cherry public override string Description { get { return "Cherry Soft compressed image format"; } } public override uint Signature { get { return 0; } } - public override ImageMetaData ReadMetaData (Stream stream) + public override ImageMetaData ReadMetaData (IBinaryStream stream) { - var header = new byte[0x28]; - if (header.Length != stream.Read (header, 0, header.Length)) + var header = stream.ReadHeader (0x28); + if (0xFFFF != header.ToInt32 (8)) return null; - if (0xFFFF != LittleEndian.ToInt32 (header, 8)) - return null; - int packed_size = LittleEndian.ToInt32 (header, 0); - int unpacked_size = LittleEndian.ToInt32 (header, 4); - uint width = LittleEndian.ToUInt32 (header, 0x10); - uint height = LittleEndian.ToUInt32 (header, 0x14); - int bpp = LittleEndian.ToInt32 (header, 0x18); + int packed_size = header.ToInt32 (0); + int unpacked_size = header.ToInt32 (4); + uint width = header.ToUInt32 (0x10); + uint height = header.ToUInt32 (0x14); + int bpp = header.ToInt32 (0x18); if (0 == width || 0 == height || width > 0x7fff || height > 0x7fff || (bpp != 32 && bpp != 24 && bpp != 8) || unpacked_size <= 0 || packed_size < 0) @@ -126,7 +122,7 @@ namespace GameRes.Formats.Cherry UnpackedSize = unpacked_size, Offset = 0xFFFF, HeaderSize = 0x28, - AlphaChannel = LittleEndian.ToInt32 (header, 0x24) != 0, + AlphaChannel = header.ToInt32 (0x24) != 0, }; } } diff --git a/ArcFormats/Circus/ArcValkyrieComplex.cs b/ArcFormats/Circus/ArcValkyrieComplex.cs index c6d3ce9d..789552d8 100644 --- a/ArcFormats/Circus/ArcValkyrieComplex.cs +++ b/ArcFormats/Circus/ArcValkyrieComplex.cs @@ -117,7 +117,7 @@ namespace GameRes.Formats.Circus { data = UnpackCps (data); } - return new MemoryStream (data); + return new BinMemoryStream (data, entry.Name); } byte[] UnpackCps (byte[] input) diff --git a/ArcFormats/Circus/AudioPCM.cs b/ArcFormats/Circus/AudioPCM.cs index a467bb29..121b3d93 100644 --- a/ArcFormats/Circus/AudioPCM.cs +++ b/ArcFormats/Circus/AudioPCM.cs @@ -38,46 +38,43 @@ namespace GameRes.Formats.Circus public override string Description { get { return "Circus PCM audio"; } } public override uint Signature { get { return 0x4d435058; } } // 'XPCM' - public override SoundInput TryOpen (Stream file) + public override SoundInput TryOpen (IBinaryStream file) { file.Position = 4; - using (var input = new ArcView.Reader (file)) + int src_size = file.ReadInt32(); + if (src_size <= 0) + throw new InvalidFormatException(); + int mode = file.ReadInt32(); + int extra = (mode >> 8) & 0xff; + mode &= 0xff; + if (5 == mode) { - int src_size = input.ReadInt32(); - if (src_size <= 0) - throw new InvalidFormatException(); - int mode = input.ReadInt32(); - int extra = (mode >> 8) & 0xff; - mode &= 0xff; - if (5 == mode) - { - uint ogg_size = input.ReadUInt32(); - var ogg = new StreamRegion (file, 0x10, ogg_size); - return new OggInput (ogg); - } - var format = new WaveFormat(); - format.FormatTag = input.ReadUInt16(); - format.Channels = input.ReadUInt16(); - format.SamplesPerSecond = input.ReadUInt32(); - format.AverageBytesPerSecond = input.ReadUInt32(); - format.BlockAlign = input.ReadUInt16(); - format.BitsPerSample = input.ReadUInt16(); - Stream pcm; - if (0 == mode) - { - pcm = new StreamRegion (file, file.Position, src_size); - } - else if (1 == mode || 3 == mode) - { - var decoder = new PcmDecoder (input, src_size, extra, (XpcmCompression)mode); - pcm = new MemoryStream (decoder.Unpack(), 0, src_size); - file.Dispose(); - } - else - throw new NotSupportedException ("Not supported Circus PCM audio compression"); - - return new RawPcmInput (pcm, format); + uint ogg_size = file.ReadUInt32(); + var ogg = new StreamRegion (file.AsStream, 0x10, ogg_size); + return new OggInput (ogg); } + var format = new WaveFormat(); + format.FormatTag = file.ReadUInt16(); + format.Channels = file.ReadUInt16(); + format.SamplesPerSecond = file.ReadUInt32(); + format.AverageBytesPerSecond = file.ReadUInt32(); + format.BlockAlign = file.ReadUInt16(); + format.BitsPerSample = file.ReadUInt16(); + Stream pcm; + if (0 == mode) + { + pcm = new StreamRegion (file.AsStream, file.Position, src_size); + } + else if (1 == mode || 3 == mode) + { + var decoder = new PcmDecoder (file, src_size, extra, (XpcmCompression)mode); + pcm = new MemoryStream (decoder.Unpack(), 0, src_size); + file.Dispose(); + } + else + throw new NotSupportedException ("Not supported Circus PCM audio compression"); + + return new RawPcmInput (pcm, format); } } @@ -97,7 +94,7 @@ namespace GameRes.Formats.Circus public byte[] Data { get { return m_pcm_data; } } - public PcmDecoder (BinaryReader input, int pcm_size, int extra, XpcmCompression mode) + public PcmDecoder (IBinaryStream input, int pcm_size, int extra, XpcmCompression mode) { if (extra < 0 || extra > 3) throw new InvalidFormatException(); @@ -114,7 +111,7 @@ namespace GameRes.Formats.Circus } else if (XpcmCompression.Zlib == mode) { - using (var z = new ZLibStream (input.BaseStream, CompressionMode.Decompress, true)) + using (var z = new ZLibStream (input.AsStream, CompressionMode.Decompress, true)) z.Read (m_encoded, 0, m_encoded.Length); } else diff --git a/ArcFormats/Circus/ImageCRX.cs b/ArcFormats/Circus/ImageCRX.cs index 4961b7e0..74302913 100644 --- a/ArcFormats/Circus/ImageCRX.cs +++ b/ArcFormats/Circus/ImageCRX.cs @@ -266,23 +266,23 @@ namespace GameRes.Formats.Circus { flag >>= 1; if (0 == (flag & 0x100)) - flag = m_input.ReadByte() | 0xff00; + flag = m_input.ReadUInt8() | 0xff00; if (0 != (flag & 1)) { - byte dat = m_input.ReadByte(); + byte dat = m_input.ReadUInt8(); window[win_pos++] = dat; win_pos &= 0xffff; m_output[dst++] = dat; } else { - byte control = m_input.ReadByte(); + byte control = m_input.ReadUInt8(); int count, offset; if (control >= 0xc0) { - offset = ((control & 3) << 8) | m_input.ReadByte(); + offset = ((control & 3) << 8) | m_input.ReadUInt8(); count = 4 + ((control >> 2) & 0xf); } else if (0 != (control & 0x80)) @@ -290,7 +290,7 @@ namespace GameRes.Formats.Circus offset = control & 0x1f; count = 2 + ((control >> 5) & 3); if (0 == offset) - offset = m_input.ReadByte(); + offset = m_input.ReadUInt8(); } else if (0x7f == control) { diff --git a/ArcFormats/Circus/ImageCRXD.cs b/ArcFormats/Circus/ImageCRXD.cs index a0ddb374..3dc09cea 100644 --- a/ArcFormats/Circus/ImageCRXD.cs +++ b/ArcFormats/Circus/ImageCRXD.cs @@ -70,7 +70,7 @@ namespace GameRes.Formats.Circus else if (header.AsciiEqual (0x20, "CRXG")) { using (var crx_input = new StreamRegion (stream.AsStream, 0x20, true)) - using (var crx = new BinaryStream (crx_input)) + using (var crx = new BinaryStream (crx_input, stream.Name)) { var diff_info = base.ReadMetaData (crx) as CrxMetaData; if (null == diff_info) @@ -90,12 +90,12 @@ namespace GameRes.Formats.Circus if (info != null) { info.BaseOffset = header.ToUInt32 (8); - info.BaseFileName = Binary.GetCString (header, 0xC, 0x14); + info.BaseFileName = header.GetCString (0xC, 0x14); } return info; } - Stream OpenByOffset (uint offset) + IBinaryStream OpenByOffset (uint offset) { var vfs = VFS.Top as ArchiveFileSystem; if (null == vfs) @@ -118,9 +118,9 @@ namespace GameRes.Formats.Circus diff = OpenByOffset (info.DiffOffset); if (null == diff) throw new FileNotFoundException ("Referenced diff image not found"); - input = new StreamRegion (diff, 0x20); + input = new StreamRegion (diff.AsStream, 0x20); } - return new BinaryStream (input); + return new BinaryStream (input, diff.Name); } public override ImageData Read (IBinaryStream stream, ImageMetaData info) diff --git a/ArcFormats/Cmvs/ArcCPZ.cs b/ArcFormats/Cmvs/ArcCPZ.cs index aa909b91..c71b9cb4 100644 --- a/ArcFormats/Cmvs/ArcCPZ.cs +++ b/ArcFormats/Cmvs/ArcCPZ.cs @@ -279,7 +279,7 @@ namespace GameRes.Formats.Purple data = UnpackPs2 (data); else if (data.Length > 0x40 && Binary.AsciiEqual (data, 0, "PB3B")) DecryptPb3 (data); - return new MemoryStream (data); + return new BinMemoryStream (data, entry.Name); } void DecryptIndexStage1 (byte[] data, uint key, CmvsScheme scheme) diff --git a/ArcFormats/Cmvs/ArcPBZ.cs b/ArcFormats/Cmvs/ArcPBZ.cs index 7223fdd6..69179417 100644 --- a/ArcFormats/Cmvs/ArcPBZ.cs +++ b/ArcFormats/Cmvs/ArcPBZ.cs @@ -122,7 +122,7 @@ namespace GameRes.Formats.Pvns { data = DecryptScript (data, parc.ScriptKey); } - return new MemoryStream (data); + return new BinMemoryStream (data, entry.Name); } static void Decrypt (byte[] data, byte[] key) diff --git a/ArcFormats/Cmvs/ImagePB3.cs b/ArcFormats/Cmvs/ImagePB3.cs index 9854f9dc..28f0d0fe 100644 --- a/ArcFormats/Cmvs/ImagePB3.cs +++ b/ArcFormats/Cmvs/ImagePB3.cs @@ -47,33 +47,30 @@ namespace GameRes.Formats.Purple public override string Description { get { return "Purple Software image format"; } } public override uint Signature { get { return 0x42334250; } } // 'PB3B' - public override ImageMetaData ReadMetaData (Stream stream) + public override ImageMetaData ReadMetaData (IBinaryStream stream) { stream.Position = 4; - using (var reader = new ArcView.Reader (stream)) + int input_size = stream.ReadInt32(); + stream.Position = 0x18; + int t2 = stream.ReadInt32(); + int t1 = stream.ReadUInt16(); + uint width = stream.ReadUInt16(); + uint height = stream.ReadUInt16(); + int bpp = stream.ReadUInt16(); + return new Pb3MetaData { - int input_size = reader.ReadInt32(); - stream.Position = 0x18; - int t2 = reader.ReadInt32(); - int t1 = reader.ReadUInt16(); - uint width = reader.ReadUInt16(); - uint height = reader.ReadUInt16(); - int bpp = reader.ReadUInt16(); - return new Pb3MetaData - { - Width = width, - Height = height, - BPP = bpp, - Type = t1, - SubType = t2, - InputSize = input_size, - }; - } + Width = width, + Height = height, + BPP = bpp, + Type = t1, + SubType = t2, + InputSize = input_size, + }; } - public override ImageData Read (Stream stream, ImageMetaData info) + public override ImageData Read (IBinaryStream stream, ImageMetaData info) { - var reader = new Pb3Reader (stream, (Pb3MetaData)info); + var reader = new Pb3Reader (stream.AsStream, (Pb3MetaData)info); reader.Unpack(); return ImageData.Create (info, reader.Format, null, reader.Data); } @@ -350,9 +347,9 @@ namespace GameRes.Formats.Purple if (name.Equals (m_info.FileName, StringComparison.InvariantCultureIgnoreCase)) throw new InvalidFormatException(); // two files referencing each other still could create infinite recursion - using (var base_file = VFS.OpenSeekableStream (name)) + using (var base_file = VFS.OpenBinaryStream (name)) { - var image_data = ImageFormat.Read (name, base_file); + var image_data = ImageFormat.Read (base_file); int stride = image_data.Bitmap.PixelWidth * 4; var pixels = new byte[stride * image_data.Bitmap.PixelHeight]; image_data.Bitmap.CopyPixels (pixels, stride, 0); diff --git a/ArcFormats/Cri/ArcCPK.cs b/ArcFormats/Cri/ArcCPK.cs index 9c5c9968..a1d1b079 100644 --- a/ArcFormats/Cri/ArcCPK.cs +++ b/ArcFormats/Cri/ArcCPK.cs @@ -104,7 +104,7 @@ namespace GameRes.Formats.Cri } Array.Reverse (output, (int)prefix_size, unpacked_size); arc.File.View.Read (entry.Offset+0x10+packed_size, output, 0, prefix_size); - return new MemoryStream (output); + return new BinMemoryStream (output, entry.Name); } void DetectFileTypes (ArcView file, List dir) diff --git a/ArcFormats/Cri/AudioADX.cs b/ArcFormats/Cri/AudioADX.cs index dddacba8..83f63ed8 100644 --- a/ArcFormats/Cri/AudioADX.cs +++ b/ArcFormats/Cri/AudioADX.cs @@ -43,7 +43,7 @@ namespace GameRes.Formats.Cri uint signature = file.Signature; if (0x80 != (signature & 0xFFFF)) return null; - uint header_size = Binary.BigEndian (signature & 0xFFFF0000); + int header_size = (int)Binary.BigEndian (signature & 0xFFFF0000); if (header_size < 0x10 || header_size >= file.Length) return null; var header = file.ReadBytes (header_size); diff --git a/ArcFormats/Cri/ImageBIP.cs b/ArcFormats/Cri/ImageBIP.cs index 10f63267..58654079 100644 --- a/ArcFormats/Cri/ImageBIP.cs +++ b/ArcFormats/Cri/ImageBIP.cs @@ -61,57 +61,52 @@ namespace GameRes.Formats.Cri throw new NotImplementedException ("BipFormat.Write not implemented"); } - public override ImageMetaData ReadMetaData (Stream stream) + public override ImageMetaData ReadMetaData (IBinaryStream input) { - using (var input = new BinaryReader (stream, Encoding.ASCII, true)) - { - int sig = input.ReadInt32(); - if (sig != 5 && sig != 10) - return null; - uint header_end = (uint)sig*4; - uint index_offset = input.ReadUInt32(); + int sig = input.ReadInt32(); + if (sig != 5 && sig != 10) + return null; + uint header_end = (uint)sig*4; + uint index_offset = input.ReadUInt32(); - input.BaseStream.Position = header_end-4; - uint data_offset = input.ReadUInt32() + 8; - if (index_offset >= data_offset || index_offset < header_end) - return null; - input.BaseStream.Position = index_offset; - int tile_count = input.ReadInt16(); - int flag = input.ReadInt16(); - if (tile_count <= 0 || 0 != flag) - return null; - input.ReadInt32(); - uint w = input.ReadUInt16(); - uint h = input.ReadUInt16(); - if (0 == w || 0 == h) - return null; - var meta = new BipMetaData { Width = w, Height = h, BPP = 32 }; - meta.Tiles.Capacity = tile_count; - for (int i = 0; i < tile_count; ++i) - { - input.ReadInt64(); - var tile = new BipTile(); - tile.Left = input.ReadUInt16(); - tile.Top = input.ReadUInt16(); - tile.Width = input.ReadUInt16(); - tile.Height = input.ReadUInt16(); - if (tile.Left + tile.Width > meta.Width) - meta.Width = (uint)(tile.Left + tile.Width); - if (tile.Top + tile.Height > meta.Height) - meta.Height = (uint)(tile.Top + tile.Height); - input.ReadInt64(); - tile.Offset = input.ReadUInt32() + data_offset; - meta.Tiles.Add (tile); - } - return meta; + input.Position = header_end-4; + uint data_offset = input.ReadUInt32() + 8; + if (index_offset >= data_offset || index_offset < header_end) + return null; + input.Position = index_offset; + int tile_count = input.ReadInt16(); + int flag = input.ReadInt16(); + if (tile_count <= 0 || 0 != flag) + return null; + input.ReadInt32(); + uint w = input.ReadUInt16(); + uint h = input.ReadUInt16(); + if (0 == w || 0 == h) + return null; + var meta = new BipMetaData { Width = w, Height = h, BPP = 32 }; + meta.Tiles.Capacity = tile_count; + for (int i = 0; i < tile_count; ++i) + { + input.ReadInt64(); + var tile = new BipTile(); + tile.Left = input.ReadUInt16(); + tile.Top = input.ReadUInt16(); + tile.Width = input.ReadUInt16(); + tile.Height = input.ReadUInt16(); + if (tile.Left + tile.Width > meta.Width) + meta.Width = (uint)(tile.Left + tile.Width); + if (tile.Top + tile.Height > meta.Height) + meta.Height = (uint)(tile.Top + tile.Height); + input.ReadInt64(); + tile.Offset = input.ReadUInt32() + data_offset; + meta.Tiles.Add (tile); } + return meta; } - public override ImageData Read (Stream stream, ImageMetaData info) + public override ImageData Read (IBinaryStream stream, ImageMetaData info) { - var meta = info as BipMetaData; - if (null == meta) - throw new ArgumentException ("BipFormat.Read should be supplied with BipMetaData", "info"); + var meta = (BipMetaData)info; var header = new byte[0x7c]; var bitmap = new WriteableBitmap ((int)meta.Width, (int)meta.Height, @@ -127,7 +122,7 @@ namespace GameRes.Formats.Cri int alpha = LittleEndian.ToInt32 (header, 0x68); int x = LittleEndian.ToInt32 (header, 0x6c); int y = LittleEndian.ToInt32 (header, 0x70); - using (var png = new StreamRegion (stream, stream.Position, data_size, true)) + using (var png = new StreamRegion (stream.AsStream, stream.Position, data_size, true)) { var decoder = new PngBitmapDecoder (png, BitmapCreateOptions.None, BitmapCacheOption.OnLoad); diff --git a/ArcFormats/Cri/ImageSPC.cs b/ArcFormats/Cri/ImageSPC.cs index 7f65e5dc..36c74963 100644 --- a/ArcFormats/Cri/ImageSPC.cs +++ b/ArcFormats/Cri/ImageSPC.cs @@ -49,7 +49,7 @@ namespace GameRes.Formats.Cri return null; using (var lzss = new LzssStream (stream.AsStream, LzssMode.Decompress, true)) using (var input = new SeekableStream (lzss)) - using (var xtx = new BinaryStream (input)) + using (var xtx = new BinaryStream (input, stream.Name)) return base.ReadMetaData (xtx); } @@ -58,7 +58,7 @@ namespace GameRes.Formats.Cri stream.Position = 4; using (var lzss = new LzssStream (stream.AsStream, LzssMode.Decompress, true)) using (var input = new SeekableStream (lzss)) - using (var xtx = new BinaryStream (input)) + using (var xtx = new BinaryStream (input, stream.Name)) return base.Read (xtx, info); } diff --git a/ArcFormats/Crowd/AudioEOG.cs b/ArcFormats/Crowd/AudioEOG.cs index 14fccce1..f09fc468 100644 --- a/ArcFormats/Crowd/AudioEOG.cs +++ b/ArcFormats/Crowd/AudioEOG.cs @@ -36,9 +36,9 @@ namespace GameRes.Formats.Crowd public override string Description { get { return "Crowd engine audio format (Ogg/Vorbis)"; } } public override uint Signature { get { return 0x004D5243; } } // 'CRM' - public override SoundInput TryOpen (Stream file) + public override SoundInput TryOpen (IBinaryStream file) { - var ogg = new StreamRegion (file, 8); + var ogg = new StreamRegion (file.AsStream, 8); return new OggInput (ogg); // in case of exception ogg stream is left undisposed } diff --git a/ArcFormats/Crowd/ImageCWL.cs b/ArcFormats/Crowd/ImageCWL.cs index 071d5887..6ac73990 100644 --- a/ArcFormats/Crowd/ImageCWL.cs +++ b/ArcFormats/Crowd/ImageCWL.cs @@ -43,23 +43,21 @@ namespace GameRes.Formats.Crowd static readonly byte[] SignatureText = Encoding.ASCII.GetBytes ("cwd format - version 1.00 -"); - public override ImageMetaData ReadMetaData (Stream stream) + public override ImageMetaData ReadMetaData (IBinaryStream stream) { - var header = new byte[0x38]; - if (header.Length != stream.Read (header, 0, header.Length)) - return null; + var header = stream.ReadHeader (0x38); if (!header.Take (SignatureText.Length).SequenceEqual (SignatureText)) return null; uint key = header[0x34] + 0x259Au; return new ImageMetaData { - Width = LittleEndian.ToUInt32 (header, 0x2c) + key, - Height = LittleEndian.ToUInt32 (header, 0x30) + key, + Width = header.ToUInt32 (0x2c) + key, + Height = header.ToUInt32 (0x30) + key, BPP = 15, }; } - public override ImageData Read (Stream stream, ImageMetaData info) + public override ImageData Read (IBinaryStream stream, ImageMetaData info) { stream.Position = 0x38; int size = (int)info.Width * (int)info.Height * 2; @@ -82,36 +80,34 @@ namespace GameRes.Formats.Crowd public override string Description { get { return "LZ-compressed Crowd bitmap"; } } public override uint Signature { get { return 0x44445A53u; } } // 'SZDD' - public override ImageMetaData ReadMetaData (Stream stream) + public override ImageMetaData ReadMetaData (IBinaryStream stream) { stream.Position = 0x0e; - using (var lz = new LzssReader (stream, 100, 0x38)) // extract CWD header + using (var lz = new LzssReader (stream.AsStream, 100, 0x38)) // extract CWD header { lz.FrameSize = 0x1000; lz.FrameFill = 0x20; lz.FrameInitPos = 0x1000 - 0x10; lz.Unpack(); - using (var cwd = new MemoryStream (lz.Data)) + using (var cwd = new BinMemoryStream (lz.Data, stream.Name)) return base.ReadMetaData (cwd); } } - public override ImageData Read (Stream stream, ImageMetaData info) + public override ImageData Read (IBinaryStream file, ImageMetaData info) { - if (stream.Length > int.MaxValue) + if (file.Length > int.MaxValue) throw new FileSizeException(); - var header = new byte[14]; - if (header.Length != stream.Read (header, 0, header.Length)) - throw new InvalidFormatException(); - int data_length = LittleEndian.ToInt32 (header, 10); - int input_length = (int)(stream.Length-stream.Position); - using (var lz = new LzssReader (stream, input_length, data_length)) + var header = file.ReadHeader (14); + int data_length = header.ToInt32 (10); + int input_length = (int)(file.Length-file.Position); + using (var lz = new LzssReader (file.AsStream, input_length, data_length)) { lz.FrameSize = 0x1000; lz.FrameFill = 0x20; lz.FrameInitPos = 0x1000 - 0x10; lz.Unpack(); - using (var cwd = new MemoryStream (lz.Data)) + using (var cwd = new BinMemoryStream (lz.Data, file.Name)) return base.Read (cwd, info); } } diff --git a/ArcFormats/Crowd/ImageCWP.cs b/ArcFormats/Crowd/ImageCWP.cs index 061aeedf..bcc54d21 100644 --- a/ArcFormats/Crowd/ImageCWP.cs +++ b/ArcFormats/Crowd/ImageCWP.cs @@ -40,50 +40,47 @@ namespace GameRes.Formats.Crowd public override uint Signature { get { return 0x50445743; } } // 'CWDP' public override bool CanWrite { get { return true; } } - public override ImageMetaData ReadMetaData (Stream stream) + public override ImageMetaData ReadMetaData (IBinaryStream input) { - using (var input = new ArcView.Reader (stream)) + input.Position = 4; + uint width = Binary.BigEndian (input.ReadUInt32()); + uint height = Binary.BigEndian (input.ReadUInt32()); + if (0 == width || 0 == height) + return null; + int bpp = input.ReadByte(); + int color_type = input.ReadByte(); + switch (color_type) { - input.ReadInt32(); - uint width = Binary.BigEndian (input.ReadUInt32()); - uint height = Binary.BigEndian (input.ReadUInt32()); - if (0 == width || 0 == height) - return null; - int bpp = input.ReadByte(); - int color_type = input.ReadByte(); - switch (color_type) - { - case 2: bpp *= 3; break; - case 4: bpp *= 2; break; - case 6: bpp *= 4; break; - case 3: - case 0: break; - default: return null; - } - return new ImageMetaData - { - Width = width, - Height = height, - BPP = bpp, - }; + case 2: bpp *= 3; break; + case 4: bpp *= 2; break; + case 6: bpp *= 4; break; + case 3: + case 0: break; + default: return null; } + return new ImageMetaData + { + Width = width, + Height = height, + BPP = bpp, + }; } - public override ImageData Read (Stream stream, ImageMetaData info) + public override ImageData Read (IBinaryStream file, ImageMetaData info) { var header = new byte[0x15]; - using (var mem = new MemoryStream((int)(0x14 + stream.Length + 12))) + using (var mem = new MemoryStream((int)(0x14 + file.Length + 12))) using (var png = new BinaryWriter (mem)) { png.Write (0x474E5089u); // png header png.Write (0x0A1A0A0Du); png.Write (0x0D000000u); png.Write (0x52444849u); // 'IHDR' - stream.Position = 4; - stream.Read (header, 0, header.Length); + file.Position = 4; + file.Read (header, 0, header.Length); png.Write (header, 0, header.Length); png.Write (0x54414449u); // 'IDAT' - stream.CopyTo (mem); + file.AsStream.CopyTo (mem); header[1] = 0; header[2] = 0; header[3] = 0; @@ -149,7 +146,9 @@ namespace GameRes.Formats.Crowd png.Read (header, 0, header.Length); cwp.Write (0x50445743u); // 'CWDP' cwp.Write (header, 0, header.Length); - var idat = PngFormat.FindChunk (png, "IDAT"); + long idat; + using (var bin = new BinMemoryStream (png, "")) + idat = PngFormat.FindChunk (bin, "IDAT"); if (-1 == idat) throw new InvalidFormatException ("CWP conversion failed"); png.Position = idat; diff --git a/ArcFormats/Crowd/ImageZBM.cs b/ArcFormats/Crowd/ImageZBM.cs index a2e33669..d1dbc2f2 100644 --- a/ArcFormats/Crowd/ImageZBM.cs +++ b/ArcFormats/Crowd/ImageZBM.cs @@ -39,10 +39,10 @@ namespace GameRes.Formats.Crowd public override uint Signature { get { return 0x44445A53u; } } // 'SZDD' public override bool CanWrite { get { return false; } } - public override ImageMetaData ReadMetaData (Stream stream) + public override ImageMetaData ReadMetaData (IBinaryStream stream) { stream.Position = 0x0e; - using (var lz = new LzssReader (stream, 100, 54)) // extract BMP header + using (var lz = new LzssReader (stream.AsStream, 100, 54)) // extract BMP header { lz.FrameSize = 0x1000; lz.FrameFill = 0x20; @@ -51,21 +51,19 @@ namespace GameRes.Formats.Crowd var header = lz.Data; for (int i = 0; i < 54; ++i) header[i] ^= 0xff; - using (var bmp = new MemoryStream (header)) + using (var bmp = new BinMemoryStream (header, stream.Name)) return base.ReadMetaData (bmp); } } - public override ImageData Read (Stream stream, ImageMetaData info) + public override ImageData Read (IBinaryStream stream, ImageMetaData info) { if (stream.Length > int.MaxValue) throw new FileSizeException(); - var header = new byte[14]; - if (header.Length != stream.Read (header, 0, header.Length)) - throw new InvalidFormatException(); - int data_length = LittleEndian.ToInt32 (header, 10); + var header = stream.ReadHeader (14); + int data_length = header.ToInt32 (10); int input_length = (int)(stream.Length-stream.Position); - using (var lz = new LzssReader (stream, input_length, data_length)) + using (var lz = new LzssReader (stream.AsStream, input_length, data_length)) { lz.FrameSize = 0x1000; lz.FrameFill = 0x20; @@ -75,7 +73,7 @@ namespace GameRes.Formats.Crowd int count = Math.Min (100, data.Length); for (int i = 0; i < count; ++i) data[i] ^= 0xff; - using (var bmp = new MemoryStream (data)) + using (var bmp = new BinMemoryStream (data, stream.Name)) return base.Read (bmp, info); } } diff --git a/ArcFormats/CsWare/ArcPCS.cs b/ArcFormats/CsWare/ArcPCS.cs index 82d08317..36c2491a 100644 --- a/ArcFormats/CsWare/ArcPCS.cs +++ b/ArcFormats/CsWare/ArcPCS.cs @@ -149,7 +149,7 @@ namespace GameRes.Formats.CsWare header[i] = (byte)(pent.Key - header[i] - 1); } if (header_size == entry.Size) - return new MemoryStream (header); + return new BinMemoryStream (header, entry.Name); var rest = arc.File.CreateStream (entry.Offset+512, entry.Size-512); return new PrefixStream (header, rest); } diff --git a/ArcFormats/Cyberworks/AudioTINK.cs b/ArcFormats/Cyberworks/AudioTINK.cs index 6ea8ab2f..a30823f7 100644 --- a/ArcFormats/Cyberworks/AudioTINK.cs +++ b/ArcFormats/Cyberworks/AudioTINK.cs @@ -90,9 +90,8 @@ namespace GameRes.Formats.Cyberworks input = new MemoryStream (header); else input = new PrefixStream (header, new StreamRegion (file.AsStream, file.Position)); - var ogg = new BinaryStream (input, file.Name); - var sound = OggAudio.Instance.TryOpen (ogg); - if (sound != null && header.Length >= file.Length) + var sound = new OggInput (input); + if (header.Length >= file.Length) file.Dispose(); return sound; } diff --git a/ArcFormats/DDSystem/ArcDDP.cs b/ArcFormats/DDSystem/ArcDDP.cs index bb36b70d..3922a8a8 100644 --- a/ArcFormats/DDSystem/ArcDDP.cs +++ b/ArcFormats/DDSystem/ArcDDP.cs @@ -130,7 +130,7 @@ namespace GameRes.Formats.DDSystem reader.Unpack (data); if (data.Length > 16 && Binary.AsciiEqual (data, 0, "DDSxHXB")) DecryptHxb (data); - return new MemoryStream (data); + return new BinMemoryStream (data, entry.Name); } } diff --git a/ArcFormats/Dac/ArcDPK.cs b/ArcFormats/Dac/ArcDPK.cs index 152604c3..9742e776 100644 --- a/ArcFormats/Dac/ArcDPK.cs +++ b/ArcFormats/Dac/ArcDPK.cs @@ -157,7 +157,7 @@ namespace GameRes.Formats.Dac var data = new byte[entry.Size]; arc.File.View.Read (entry.Offset, data, 0, entry.Size); DecryptEntry (data, parc.Key1, parc.Key2, pentry); - return new MemoryStream (data); + return new BinMemoryStream (data, entry.Name); } private void DecryptIndex (byte[] buf, int base_offset, int length, byte last) diff --git a/ArcFormats/Dac/ImageDGC.cs b/ArcFormats/Dac/ImageDGC.cs index d9f4e48b..fcbcfc08 100644 --- a/ArcFormats/Dac/ImageDGC.cs +++ b/ArcFormats/Dac/ImageDGC.cs @@ -43,28 +43,22 @@ namespace GameRes.Formats.Dac public override string Description { get { return "DAC engine image format"; } } public override uint Signature { get { return 0x00434744; } } // 'DGC' - public override ImageMetaData ReadMetaData (Stream stream) + public override ImageMetaData ReadMetaData (IBinaryStream file) { - using (var input = new ArcView.Reader (stream)) - { - input.ReadInt32(); - var info = new DgcMetaData(); - info.Flags = input.ReadUInt32(); - info.Width = input.ReadUInt16(); - info.Height = input.ReadUInt16(); - if (info.Width > 0x7fff || info.Height > 0x7fff) - return null; - info.BPP = 0 == (info.Flags & Reader.FlagAlphaChannel) ? 24 : 32; - return info; - } + file.Position = 4; + var info = new DgcMetaData(); + info.Flags = file.ReadUInt32(); + info.Width = file.ReadUInt16(); + info.Height = file.ReadUInt16(); + if (info.Width > 0x7fff || info.Height > 0x7fff) + return null; + info.BPP = 0 == (info.Flags & Reader.FlagAlphaChannel) ? 24 : 32; + return info; } - public override ImageData Read (Stream stream, ImageMetaData info) + public override ImageData Read (IBinaryStream stream, ImageMetaData info) { - var meta = info as DgcMetaData; - if (null == meta) - throw new ArgumentException ("DgcFormat.Read should be supplied with DgcMetaData", "info"); - + var meta = (DgcMetaData)info; stream.Position = 12; using (var reader = new Reader (stream, meta)) { @@ -80,7 +74,7 @@ namespace GameRes.Formats.Dac internal class Reader : IDataUnpacker, IDisposable { - BinaryReader m_input; + IBinaryStream m_input; byte[] m_output; readonly int m_width; readonly int m_height; @@ -96,11 +90,11 @@ namespace GameRes.Formats.Dac public byte[] Data { get { return m_output; } } public PixelFormat Format { get; private set; } - public Reader (Stream input, DgcMetaData info) + public Reader (IBinaryStream input, DgcMetaData info) { m_width = (int)info.Width; m_height = (int)info.Height; - m_input = new ArcView.Reader (input); + m_input = input; m_use_dict = 0 != (info.Flags & FlagUseDictionary); m_has_alpha = 0 != (info.Flags & FlagAlphaChannel); m_max_dict_size = (int)(info.Flags & 0xffffff); @@ -162,7 +156,7 @@ namespace GameRes.Formats.Dac if (dict_len > 256) i = m_input.ReadUInt16(); else - i = m_input.ReadByte(); + i = m_input.ReadUInt8(); i *= 3; m_output[dst] = dict[i]; m_output[dst+1] = dict[i+1]; @@ -178,7 +172,7 @@ namespace GameRes.Formats.Dac { var dict = new byte[m_max_dict_size * 3]; - int dict_len = m_input.ReadByte() + 1; + int dict_len = m_input.ReadUInt8() + 1; m_input.Read (dict, 0, dict_len * 3); for (int y = 0; y < m_height; y++) @@ -198,7 +192,7 @@ namespace GameRes.Formats.Dac { for (int x = 0; x < m_width; x++) { - int i = 3 * m_input.ReadByte(); + int i = 3 * m_input.ReadUInt8(); m_output[dst] = dict[i]; m_output[dst+1] = dict[i+1]; m_output[dst+2] = dict[i+2]; @@ -260,7 +254,7 @@ namespace GameRes.Formats.Dac { for (int x = 0; x < m_width; x++) { - m_output[dst] = m_input.ReadByte(); + m_output[dst] = m_input.ReadUInt8(); dst += m_pixel_size; } } @@ -290,7 +284,7 @@ namespace GameRes.Formats.Dac int index = 0; if (0 != (ctl & 0x2000)) { - index = m_input.ReadByte(); + index = m_input.ReadUInt8(); --length; } else @@ -314,7 +308,7 @@ namespace GameRes.Formats.Dac int index = 0; if (0 != (ctl & 0x2000)) { - index = m_input.ReadByte(); + index = m_input.ReadUInt8(); --length; } else @@ -337,11 +331,11 @@ namespace GameRes.Formats.Dac { while (length > 0) { - byte ctl = m_input.ReadByte(); + byte ctl = m_input.ReadUInt8(); --length; if (0 != ctl) { - int index = 3 * m_input.ReadByte(); + int index = 3 * m_input.ReadUInt8(); --length; while (0 != ctl--) { @@ -353,13 +347,13 @@ namespace GameRes.Formats.Dac } else { - ctl = m_input.ReadByte(); + ctl = m_input.ReadUInt8(); --length; if (0 == (ctl & 0x80)) { for (int count = ctl + 2; 0 != count; --count) { - int src = 3 * m_input.ReadByte(); + int src = 3 * m_input.ReadUInt8(); --length; m_output[dst] = dict[src]; m_output[dst+1] = dict[src+1]; @@ -369,7 +363,7 @@ namespace GameRes.Formats.Dac } else { - int offset = (short)((ctl << 8) | m_input.ReadByte()); + int offset = (short)((ctl << 8) | m_input.ReadUInt8()); --length; int count = (offset & 0x3F) + 4; offset >>= 6; @@ -386,11 +380,11 @@ namespace GameRes.Formats.Dac { while (length > 0) { - byte ctl = m_input.ReadByte(); + byte ctl = m_input.ReadUInt8(); --length; if (0 != ctl) { - byte alpha = m_input.ReadByte(); + byte alpha = m_input.ReadUInt8(); --length; while (0 != ctl--) { @@ -400,7 +394,7 @@ namespace GameRes.Formats.Dac } else { - ctl = m_input.ReadByte(); + ctl = m_input.ReadUInt8(); --length; if (0 == (ctl & 0x80)) { @@ -408,13 +402,13 @@ namespace GameRes.Formats.Dac length -= count; while (0 != count--) { - m_output[dst] = m_input.ReadByte(); + m_output[dst] = m_input.ReadUInt8(); dst += m_pixel_size; } } else { - int offset = (short)((ctl << 8) | m_input.ReadByte()); + int offset = (short)((ctl << 8) | m_input.ReadUInt8()); --length; int count = (offset & 0x3F) + 4; offset >>= 6; @@ -479,25 +473,10 @@ namespace GameRes.Formats.Dac } #region IDisposable Members - bool disposed = false; - public void Dispose () { - Dispose (true); GC.SuppressFinalize (this); } - - protected virtual void Dispose (bool disposing) - { - if (!disposed) - { - if (disposing) - { - m_input.Dispose(); - } - disposed = true; - } - } #endregion } } diff --git a/ArcFormats/Dogenzaka/ImageRSA.cs b/ArcFormats/Dogenzaka/ImageRSA.cs index 20caf11b..69e114b1 100644 --- a/ArcFormats/Dogenzaka/ImageRSA.cs +++ b/ArcFormats/Dogenzaka/ImageRSA.cs @@ -53,13 +53,14 @@ namespace GameRes.Formats.Dogenzaka public static readonly byte[] KnownKey = Encoding.ASCII.GetBytes ("Hlk9D28p"); - public override ImageMetaData ReadMetaData (Stream stream) + public override ImageMetaData ReadMetaData (IBinaryStream stream) { using (var sha = SHA1.Create()) { var key = sha.ComputeHash (KnownKey).Take (16).ToArray(); - using (var proxy = new InputProxyStream (stream, true)) - using (var input = new CryptoStream (proxy, new Rc4Transform (key), CryptoStreamMode.Read)) + using (var proxy = new InputProxyStream (stream.AsStream, true)) + using (var crypto = new CryptoStream (proxy, new Rc4Transform (key), CryptoStreamMode.Read)) + using (var input = new BinaryStream (crypto, stream.Name)) { var info = base.ReadMetaData (input); if (null == info) @@ -77,12 +78,13 @@ namespace GameRes.Formats.Dogenzaka } } - public override ImageData Read (Stream stream, ImageMetaData info) + public override ImageData Read (IBinaryStream stream, ImageMetaData info) { var rc4 = (Rc4PngMetaData)info; using (var sha = SHA1.Create()) - using (var proxy = new InputProxyStream (stream, true)) - using (var input = new CryptoStream (proxy, new Rc4Transform (rc4.Key), CryptoStreamMode.Read)) + using (var proxy = new InputProxyStream (stream.AsStream, true)) + using (var crypto = new CryptoStream (proxy, new Rc4Transform (rc4.Key), CryptoStreamMode.Read)) + using (var input = new BinaryStream (crypto, stream.Name)) return base.Read (input, info); } diff --git a/ArcFormats/DraftImage.cs b/ArcFormats/DraftImage.cs index de6b481d..ca9b236f 100644 --- a/ArcFormats/DraftImage.cs +++ b/ArcFormats/DraftImage.cs @@ -31,11 +31,11 @@ namespace GameRes.Formats.?????? public override string Description { get { return "?????? image format"; } } public override uint Signature { get { return 0; } } - public override ImageMetaData ReadMetaData (Stream stream) + public override ImageMetaData ReadMetaData (IBinaryStream file) { } - public override ImageData Read (Stream stream, ImageMetaData info) + public override ImageData Read (IBinaryStream file, ImageMetaData info) { var meta = (xxxMetaData)info; diff --git a/ArcFormats/DxLib/ArcDX.cs b/ArcFormats/DxLib/ArcDX.cs index 39e3be96..fdb3fcd1 100644 --- a/ArcFormats/DxLib/ArcDX.cs +++ b/ArcFormats/DxLib/ArcDX.cs @@ -145,7 +145,7 @@ namespace GameRes.Formats.DxLib using (input) { var data = Unpack (input); - return new MemoryStream (data); + return new BinMemoryStream (data, entry.Name); } } diff --git a/ArcFormats/DxLib/ArcMED.cs b/ArcFormats/DxLib/ArcMED.cs index eaeb94fa..d375a432 100644 --- a/ArcFormats/DxLib/ArcMED.cs +++ b/ArcFormats/DxLib/ArcMED.cs @@ -162,7 +162,7 @@ namespace GameRes.Formats.DxLib var offset = scr_arc.Encryption.StartOffset; scr_arc.Encryption.Decrypt (data, offset, data.Length-offset); } - return new MemoryStream (data); + return new BinMemoryStream (data, entry.Name); } public override ResourceOptions GetDefaultOptions () diff --git a/ArcFormats/Eagls/ArcEAGLS.cs b/ArcFormats/Eagls/ArcEAGLS.cs index 010c8518..406b3288 100644 --- a/ArcFormats/Eagls/ArcEAGLS.cs +++ b/ArcFormats/Eagls/ArcEAGLS.cs @@ -333,7 +333,7 @@ namespace GameRes.Formats.Eagls { byte[] input = File.View.ReadBytes (entry.Offset, entry.Size); Encryption.Decrypt (input); - return new MemoryStream (input); + return new BinMemoryStream (input, entry.Name); } } } diff --git a/ArcFormats/Eagls/ImageGR.cs b/ArcFormats/Eagls/ImageGR.cs index cf95d0de..e8a80541 100644 --- a/ArcFormats/Eagls/ImageGR.cs +++ b/ArcFormats/Eagls/ImageGR.cs @@ -45,9 +45,9 @@ namespace GameRes.Formats.Eagls public override uint Signature { get { return 0; } } public override bool CanWrite { get { return false; } } - public override ImageMetaData ReadMetaData (Stream stream) + public override ImageMetaData ReadMetaData (IBinaryStream file) { - using (var lzs = new LzssStream (stream, LzssMode.Decompress, true)) + using (var lzs = new LzssStream (file.AsStream, LzssMode.Decompress, true)) { if (lzs.ReadByte() != 'B' || lzs.ReadByte() != 'M') return null; @@ -71,10 +71,11 @@ namespace GameRes.Formats.Eagls } } - public override ImageData Read (Stream stream, ImageMetaData info) + public override ImageData Read (IBinaryStream file, ImageMetaData info) { var meta = (GrMetaData)info; - using (var bmp = new LzssStream (stream, LzssMode.Decompress, true)) + using (var lzs = new LzssStream (file.AsStream, LzssMode.Decompress, true)) + using (var bmp = new BinaryStream (lzs, file.Name)) { if (32 != info.BPP) return base.Read (bmp, info); diff --git a/ArcFormats/Emic/ImageMWP.cs b/ArcFormats/Emic/ImageMWP.cs index 7681d25e..53a542c1 100644 --- a/ArcFormats/Emic/ImageMWP.cs +++ b/ArcFormats/Emic/ImageMWP.cs @@ -43,24 +43,22 @@ namespace GameRes.Formats.Emic Signatures = new uint[] { 0x1050574D, 0x4C594554 }; // 'TEYL' } - public override ImageMetaData ReadMetaData (Stream stream) + public override ImageMetaData ReadMetaData (IBinaryStream file) { - var header = new byte[12]; - if (header.Length != stream.Read (header, 0, header.Length)) - return null; + file.Position = 4; return new ImageMetaData { - Width = LittleEndian.ToUInt32 (header, 4), - Height = LittleEndian.ToUInt32 (header, 8), + Width = file.ReadUInt32(), + Height = file.ReadUInt32(), BPP = 32, }; } - public override ImageData Read (Stream stream, ImageMetaData info) + public override ImageData Read (IBinaryStream file, ImageMetaData info) { var pixels = new byte[info.Width*info.Height*4]; - stream.Position = 12; - if (pixels.Length != stream.Read (pixels, 0, pixels.Length)) + file.Position = 12; + if (pixels.Length != file.Read (pixels, 0, pixels.Length)) throw new EndOfStreamException(); return ImageData.Create (info, PixelFormats.Bgra32, null, pixels); } diff --git a/ArcFormats/EmonEngine/ArcEME.cs b/ArcFormats/EmonEngine/ArcEME.cs index c23272f8..e41ed383 100644 --- a/ArcFormats/EmonEngine/ArcEME.cs +++ b/ArcFormats/EmonEngine/ArcEME.cs @@ -128,7 +128,7 @@ namespace GameRes.Formats.EmonEngine lzss.Config.FrameInitPos = entry.LzssInitPos; lzss.Read (data, part1_size, unpacked_size); } - return new MemoryStream (data); + return new BinMemoryStream (data, entry.Name); } else { diff --git a/ArcFormats/Emote/ArcPSB.cs b/ArcFormats/Emote/ArcPSB.cs index d5ee40e5..1243e09d 100644 --- a/ArcFormats/Emote/ArcPSB.cs +++ b/ArcFormats/Emote/ArcPSB.cs @@ -583,10 +583,10 @@ namespace GameRes.Formats.Emote Extensions = new string[0]; } - public override ImageMetaData ReadMetaData (Stream stream) + public override ImageMetaData ReadMetaData (IBinaryStream stream) { stream.Position = 4; - using (var reader = new BinaryReader (stream, Encoding.UTF8, true)) + using (var reader = new BinaryReader (stream.AsStream, Encoding.UTF8, true)) { var info = new PsbTexMetaData { BPP = 32 }; info.DataOffset = reader.ReadInt32(); @@ -599,14 +599,14 @@ namespace GameRes.Formats.Emote } } - public override ImageData Read (Stream stream, ImageMetaData info) + public override ImageData Read (IBinaryStream stream, ImageMetaData info) { var meta = (PsbTexMetaData)info; var pixels = new byte[meta.Width * meta.Height * 4]; if ("RGBA8" == meta.TexType) - ReadRgba8 (stream, meta, pixels); + ReadRgba8 (stream.AsStream, meta, pixels); else if ("RGBA4444" == meta.TexType) - ReadRgba4444 (stream, meta, pixels); + ReadRgba4444 (stream.AsStream, meta, pixels); else throw new NotImplementedException (string.Format ("PSB texture format '{0}' not implemented", meta.TexType)); return ImageData.Create (info, PixelFormats.Bgra32, null, pixels); diff --git a/ArcFormats/Entis/AudioMIO.cs b/ArcFormats/Entis/AudioMIO.cs index 9c787f7d..890de297 100644 --- a/ArcFormats/Entis/AudioMIO.cs +++ b/ArcFormats/Entis/AudioMIO.cs @@ -42,17 +42,15 @@ namespace GameRes.Formats.Entis public override string Description { get { return "Entis engine compressed audio format"; } } public override uint Signature { get { return 0x69746e45u; } } // 'Enti' - public override SoundInput TryOpen (Stream file) + public override SoundInput TryOpen (IBinaryStream file) { - byte[] header = new byte[0x40]; - if (header.Length != file.Read (header, 0, header.Length)) + var header = file.ReadHeader (0x40); + if (0x03000100 != header.ToUInt32 (8)) return null; - if (0x03000100 != LittleEndian.ToUInt32 (header, 8)) - return null; - if (!Binary.AsciiEqual (header, 0x10, "Music Interleaved")) + if (!header.AsciiEqual (0x10, "Music Interleaved")) return null; - return new MioInput (file); + return new MioInput (file.AsStream); } } diff --git a/ArcFormats/Entis/ImageERI.cs b/ArcFormats/Entis/ImageERI.cs index 7dbde246..38e39a9f 100644 --- a/ArcFormats/Entis/ImageERI.cs +++ b/ArcFormats/Entis/ImageERI.cs @@ -149,17 +149,15 @@ namespace GameRes.Formats.Entis public override string Description { get { return "Entis rasterized image format"; } } public override uint Signature { get { return 0x69746e45u; } } // 'Enti' - public override ImageMetaData ReadMetaData (Stream stream) + public override ImageMetaData ReadMetaData (IBinaryStream stream) { - byte[] header = new byte[0x40]; - if (header.Length != stream.Read (header, 0, header.Length)) + var header = stream.ReadHeader (0x40); + if (0x03000100 != header.ToUInt32 (8)) return null; - if (0x03000100 != LittleEndian.ToUInt32 (header, 8)) + if (!header.AsciiEqual (0x10, "Entis Rasterized Image") + && !header.AsciiEqual (0x10, "Moving Entis Image")) return null; - if (!Binary.AsciiEqual (header, 0x10, "Entis Rasterized Image") - && !Binary.AsciiEqual (header, 0x10, "Moving Entis Image")) - return null; - using (var reader = new EriFile (stream)) + using (var reader = new EriFile (stream.AsStream)) { var section = reader.ReadSection(); if (section.Id != "Header " || section.Length <= 0) @@ -240,7 +238,7 @@ namespace GameRes.Formats.Entis } } - public override ImageData Read (Stream stream, ImageMetaData info) + public override ImageData Read (IBinaryStream stream, ImageMetaData info) { var reader = ReadImageData (stream, (EriMetaData)info); return ImageData.Create (info, reader.Format, reader.Palette, reader.Data, reader.Stride); @@ -261,11 +259,11 @@ namespace GameRes.Formats.Entis return colors; } - internal EriReader ReadImageData (Stream stream, EriMetaData meta) + internal EriReader ReadImageData (IBinaryStream stream, EriMetaData meta) { stream.Position = meta.StreamPos; Color[] palette = null; - using (var input = new EriFile (stream)) + using (var input = new EriFile (stream.AsStream)) { for (;;) // ReadSection throws an exception in case of EOF { @@ -276,13 +274,13 @@ namespace GameRes.Formats.Entis break; if ("Palette " == section.Id && meta.BPP <= 8 && section.Length <= 0x400) { - palette = ReadPalette (stream, (int)section.Length); + palette = ReadPalette (stream.AsStream, (int)section.Length); continue; } input.BaseStream.Seek (section.Length, SeekOrigin.Current); } } - var reader = new EriReader (stream, meta, palette); + var reader = new EriReader (stream.AsStream, meta, palette); reader.DecodeImage(); if (!string.IsNullOrEmpty (meta.Description)) @@ -298,7 +296,7 @@ namespace GameRes.Formats.Entis throw new InvalidFormatException(); ref_file = VFS.CombinePath (VFS.GetDirectoryName (meta.FileName), ref_file); - using (var ref_src = VFS.OpenSeekableStream (ref_file)) + using (var ref_src = VFS.OpenBinaryStream (ref_file)) { var ref_info = ReadMetaData (ref_src) as EriMetaData; if (null == ref_info) diff --git a/ArcFormats/Ethornell/ArcBGI.cs b/ArcFormats/Ethornell/ArcBGI.cs index 9a99cc28..951e6ec2 100644 --- a/ArcFormats/Ethornell/ArcBGI.cs +++ b/ArcFormats/Ethornell/ArcBGI.cs @@ -83,7 +83,7 @@ namespace GameRes.Formats.BGI using (var decoder = new DscDecoder (input)) { decoder.Unpack(); - return new MemoryStream (decoder.Output); + return new BinMemoryStream (decoder.Output, entry.Name); } } return new ArcView.ArcStream (input, entry_offset, entry.Size); diff --git a/ArcFormats/Ethornell/AudioBGI.cs b/ArcFormats/Ethornell/AudioBGI.cs index c03c3153..8e72f6c1 100644 --- a/ArcFormats/Ethornell/AudioBGI.cs +++ b/ArcFormats/Ethornell/AudioBGI.cs @@ -41,18 +41,16 @@ namespace GameRes.Formats.BGI Extensions = new string[] { "" }; } - public override SoundInput TryOpen (Stream file) + public override SoundInput TryOpen (IBinaryStream file) { - var header = new byte[8]; - if (8 != file.Read (header, 0, 8)) + var header = file.ReadHeader (8); + if (!header.AsciiEqual (4, "bw ")) return null; - if (!Binary.AsciiEqual (header, 4, "bw ")) - return null; - uint offset = LittleEndian.ToUInt32 (header, 0); + uint offset = header.ToUInt32 (0); if (offset >= file.Length) return null; - var input = new StreamRegion (file, offset); + var input = new StreamRegion (file.AsStream, offset); return new OggInput (input); // input is left undisposed in case of exception. } diff --git a/ArcFormats/Ethornell/ImageBGI.cs b/ArcFormats/Ethornell/ImageBGI.cs index 995216d6..35e89dab 100644 --- a/ArcFormats/Ethornell/ImageBGI.cs +++ b/ArcFormats/Ethornell/ImageBGI.cs @@ -51,29 +51,26 @@ namespace GameRes.Formats.BGI throw new System.NotImplementedException ("BgiFormat.Write not implemented"); } - public override ImageMetaData ReadMetaData (Stream stream) + public override ImageMetaData ReadMetaData (IBinaryStream stream) { - using (var input = new ArcView.Reader (stream)) + int width = stream.ReadInt16(); + int height = stream.ReadInt16(); + if (width <= 0 || height <= 0) + return null; + int bpp = stream.ReadInt32(); + if (24 != bpp && 32 != bpp && 8 != bpp) + return null; + if (0 != stream.ReadInt64()) + return null; + return new ImageMetaData { - int width = input.ReadInt16(); - int height = input.ReadInt16(); - if (width <= 0 || height <= 0) - return null; - int bpp = input.ReadInt32(); - if (24 != bpp && 32 != bpp && 8 != bpp) - return null; - if (0 != input.ReadInt64()) - return null; - return new ImageMetaData - { - Width = (uint)width, - Height = (uint)height, - BPP = bpp, - }; - } + Width = (uint)width, + Height = (uint)height, + BPP = bpp, + }; } - public override ImageData Read (Stream stream, ImageMetaData info) + public override ImageData Read (IBinaryStream stream, ImageMetaData info) { PixelFormat format; if (24 == info.BPP) @@ -119,33 +116,29 @@ namespace GameRes.Formats.BGI throw new System.NotImplementedException ("BgiFormat.Write not implemented"); } - public override ImageMetaData ReadMetaData (Stream stream) + public override ImageMetaData ReadMetaData (IBinaryStream stream) { - var header = new byte[0x30]; - if (header.Length != stream.Read (header, 0, header.Length)) - return null; - if (!Binary.AsciiEqual (header, "CompressedBG___")) + var header = stream.ReadHeader (0x30); + if (!header.AsciiEqual ("CompressedBG___")) return null; return new CbgMetaData { - Width = LittleEndian.ToUInt16 (header, 0x10), - Height = LittleEndian.ToUInt16 (header, 0x12), - BPP = LittleEndian.ToInt32 (header, 0x14), - IntermediateLength = LittleEndian.ToInt32 (header, 0x20), - Key = LittleEndian.ToUInt32 (header, 0x24), - EncLength = LittleEndian.ToInt32 (header, 0x28), + Width = header.ToUInt16 (0x10), + Height = header.ToUInt16 (0x12), + BPP = header.ToInt32 (0x14), + IntermediateLength = header.ToInt32 (0x20), + Key = header.ToUInt32 (0x24), + EncLength = header.ToInt32 (0x28), CheckSum = header[0x2C], CheckXor = header[0x2D], - Version = LittleEndian.ToUInt16 (header, 0x2E), + Version = header.ToUInt16 (0x2E), }; } - public override ImageData Read (Stream stream, ImageMetaData info) + public override ImageData Read (IBinaryStream stream, ImageMetaData info) { - var meta = info as CbgMetaData; - if (null == meta) - throw new ArgumentException ("CompressedBGFormat.Read should be supplied with CbgMetaData", "info"); - using (var reader = new CbgReader (stream, meta)) + var meta = (CbgMetaData)info as CbgMetaData; + using (var reader = new CbgReader (stream.AsStream, meta)) { reader.Unpack(); return ImageData.Create (meta, reader.Format, null, reader.Data, reader.Stride); diff --git a/ArcFormats/Eushully/ImageAGF.cs b/ArcFormats/Eushully/ImageAGF.cs index 68b41ce3..cbc6c4e6 100644 --- a/ArcFormats/Eushully/ImageAGF.cs +++ b/ArcFormats/Eushully/ImageAGF.cs @@ -51,7 +51,7 @@ namespace GameRes.Formats.Eushully Signatures = new uint[] { 0x46474341, 0 }; } - public override ImageMetaData ReadMetaData (Stream stream) + public override ImageMetaData ReadMetaData (IBinaryStream stream) { var header = new byte[0x20]; if (0x18 != stream.Read (header, 0, 0x18)) @@ -64,7 +64,7 @@ namespace GameRes.Formats.Eushully return null; int unpacked_size = LittleEndian.ToInt32 (header, 0x10); int packed_size = LittleEndian.ToInt32 (header, 0x14); - using (var unpacked = AgfReader.OpenSection (stream, unpacked_size, packed_size)) + using (var unpacked = AgfReader.OpenSection (stream.AsStream, unpacked_size, packed_size)) using (var reader = new BinaryReader (unpacked)) { if (0x20 != reader.Read (header, 0, 0x20)) @@ -101,7 +101,7 @@ namespace GameRes.Formats.Eushully return palette; } - public override ImageData Read (Stream stream, ImageMetaData info) + public override ImageData Read (IBinaryStream stream, ImageMetaData info) { using (var reader = new AgfReader (stream, (AgfMetaData)info)) { @@ -118,7 +118,7 @@ namespace GameRes.Formats.Eushully internal sealed class AgfReader : IDisposable { - BinaryReader m_input; + IBinaryStream m_input; byte[] m_output; int m_width; int m_height; @@ -129,9 +129,9 @@ namespace GameRes.Formats.Eushully public PixelFormat Format { get; private set; } public byte[] Data { get { return m_output; } } - public AgfReader (Stream input, AgfMetaData info) + public AgfReader (IBinaryStream input, AgfMetaData info) { - m_input = new ArcView.Reader (input); + m_input = input; m_bpp = info.BPP; m_source_bpp = info.SourceBPP; input.Position = info.DataOffset; @@ -155,15 +155,15 @@ namespace GameRes.Formats.Eushully m_input.ReadInt32(); int data_size = m_input.ReadInt32(); int packed_size = m_input.ReadInt32(); - var data_pos = m_input.BaseStream.Position; + var data_pos = m_input.Position; var bmp_data = new byte[data_size]; - using (var unpacked = OpenSection (m_input.BaseStream, data_size, packed_size)) + using (var unpacked = OpenSection (m_input.AsStream, data_size, packed_size)) if (data_size != unpacked.Read (bmp_data, 0, data_size)) throw new EndOfStreamException(); byte[] alpha = null; if (32 == m_bpp) { - m_input.BaseStream.Position = data_pos + packed_size; + m_input.Position = data_pos + packed_size; alpha = ReadAlphaChannel(); if (null == alpha) m_bpp = 24; @@ -231,21 +231,15 @@ namespace GameRes.Formats.Eushully if (m_width*m_height != unpacked_size) return null; var alpha = new byte[unpacked_size]; - using (var unpacked = OpenSection (m_input.BaseStream, unpacked_size, packed_size)) + using (var unpacked = OpenSection (m_input.AsStream, unpacked_size, packed_size)) if (unpacked_size != unpacked.Read (alpha, 0, unpacked_size)) return null; return alpha; } #region IDisposable methods - bool _disposed = false; public void Dispose () { - if (!_disposed) - { - m_input.Dispose(); - _disposed = true; - } } #endregion } diff --git a/ArcFormats/Eushully/ImageGP.cs b/ArcFormats/Eushully/ImageGP.cs index 7dc45e16..ab7e1ece 100644 --- a/ArcFormats/Eushully/ImageGP.cs +++ b/ArcFormats/Eushully/ImageGP.cs @@ -53,7 +53,7 @@ namespace GameRes.Formats.Eushully Extensions = new string[] { "gpcf" }; // made-up, real files have no extension } - public override ImageMetaData ReadMetaData (Stream stream) + public override ImageMetaData ReadMetaData (IBinaryStream stream) { int alpha_channel = stream.ReadByte(); int method = stream.ReadByte(); @@ -64,30 +64,28 @@ namespace GameRes.Formats.Eushully || align1 < 0 || align1 > 4 || align2 < 0 || align2 > 4 || bpp <= 0 || !(bpp <= 16 || 24 == bpp || 32 == bpp)) return null; - using (var reader = new ArcView.Reader (stream)) + + int palette_size = stream.ReadInt32(); + uint width = stream.ReadUInt16(); + uint height = stream.ReadUInt16(); + if (palette_size <= 0 || 0 == width || 0 == height || palette_size >= stream.Length) + return null; + if (bpp <= 8 && palette_size > 0x100) + return null; + return new GpMetaData { - int palette_size = reader.ReadInt32(); - uint width = reader.ReadUInt16(); - uint height = reader.ReadUInt16(); - if (palette_size <= 0 || 0 == width || 0 == height || palette_size >= stream.Length) - return null; - if (bpp <= 8 && palette_size > 0x100) - return null; - return new GpMetaData - { - Width = width, - Height = height, - BPP = bpp, - HasAlpha = alpha_channel != 0, - Method = method, - ElementSize = align1, - PixelsPerElement = align2, - PaletteSize = palette_size, - }; - } + Width = width, + Height = height, + BPP = bpp, + HasAlpha = alpha_channel != 0, + Method = method, + ElementSize = align1, + PixelsPerElement = align2, + PaletteSize = palette_size, + }; } - public override ImageData Read (Stream stream, ImageMetaData info) + public override ImageData Read (IBinaryStream stream, ImageMetaData info) { var meta = (GpMetaData)info; using (var reader = new GpReader (stream, meta)) @@ -105,7 +103,7 @@ namespace GameRes.Formats.Eushully internal sealed class GpReader : IDisposable { - BinaryReader m_input; + IBinaryStream m_input; GpMetaData m_info; int m_width; int m_height; @@ -115,17 +113,17 @@ namespace GameRes.Formats.Eushully public byte[] Data { get; private set; } public int Stride { get; private set; } - public GpReader (Stream input, GpMetaData info) + public GpReader (IBinaryStream input, GpMetaData info) { m_info = info; m_width = (int)m_info.Width; m_height = (int)m_info.Height; - m_input = new ArcView.Reader (input); + m_input = input; } public void Unpack () { - m_input.BaseStream.Position = 0xD; + m_input.Position = 0xD; switch (m_info.Method) { case 0: UnpackV0(); break; @@ -293,8 +291,8 @@ namespace GameRes.Formats.Eushully int i = 3; while (i < Data.Length) { - byte alpha = m_input.ReadByte(); - int count = m_input.ReadByte(); + byte alpha = m_input.ReadUInt8(); + int count = m_input.ReadUInt8(); for (int j = 0; j < count && i < Data.Length; ++j) { Data[i] = alpha; @@ -305,14 +303,8 @@ namespace GameRes.Formats.Eushully } #region IDisposable Members - bool _disposed = false; public void Dispose () { - if (!_disposed) - { - m_input.Dispose(); - _disposed = true; - } } #endregion } diff --git a/ArcFormats/ExHibit/ImageGYU.cs b/ArcFormats/ExHibit/ImageGYU.cs index b96f131d..e0ad42d0 100644 --- a/ArcFormats/ExHibit/ImageGYU.cs +++ b/ArcFormats/ExHibit/ImageGYU.cs @@ -69,29 +69,26 @@ namespace GameRes.Formats.ExHibit set { KnownKeys = ((GyuMap)value).KnownKeys; } } - public override ImageMetaData ReadMetaData (Stream stream) + public override ImageMetaData ReadMetaData (IBinaryStream stream) { - using (var reader = new ArcView.Reader (stream)) + stream.Position = 4; + return new GyuMetaData { - reader.ReadInt32(); - return new GyuMetaData - { - Flags = reader.ReadUInt16(), - CompressionMode = reader.ReadUInt16(), - Key = reader.ReadUInt32(), - BPP = reader.ReadInt32(), - Width = reader.ReadUInt32(), - Height = reader.ReadUInt32(), - DataSize = reader.ReadInt32(), - AlphaSize = reader.ReadInt32(), - PaletteSize = reader.ReadInt32(), - }; - } + Flags = stream.ReadUInt16(), + CompressionMode = stream.ReadUInt16(), + Key = stream.ReadUInt32(), + BPP = stream.ReadInt32(), + Width = stream.ReadUInt32(), + Height = stream.ReadUInt32(), + DataSize = stream.ReadInt32(), + AlphaSize = stream.ReadInt32(), + PaletteSize = stream.ReadInt32(), + }; } IDictionary CurrentMap = null; - public override ImageData Read (Stream stream, ImageMetaData info) + public override ImageData Read (IBinaryStream stream, ImageMetaData info) { var meta = (GyuMetaData)info; if (0 == meta.Key) @@ -111,7 +108,7 @@ namespace GameRes.Formats.ExHibit throw new UnknownEncryptionScheme ("Unknown image encryption key"); } } - var reader = new GyuReader (stream, meta); + var reader = new GyuReader (stream.AsStream, meta); reader.Unpack(); return ImageData.CreateFlipped (meta, reader.Format, reader.Palette, reader.Data, reader.Stride); } diff --git a/ArcFormats/FC01/ArcMRG.cs b/ArcFormats/FC01/ArcMRG.cs index 631b1c92..f2752e7c 100644 --- a/ArcFormats/FC01/ArcMRG.cs +++ b/ArcFormats/FC01/ArcMRG.cs @@ -114,7 +114,7 @@ namespace GameRes.Formats.FC01 var data = arc.File.View.ReadBytes (entry.Offset, entry.Size); var reader = new MrgDecoder (data); reader.Unpack(); - input = new MemoryStream (reader.Data); + input = new BinMemoryStream (reader.Data, entry.Name); } else input = arc.File.CreateStream (entry.Offset, entry.Size); @@ -124,7 +124,7 @@ namespace GameRes.Formats.FC01 using (var reader = new MrgLzssReader (input, (int)input.Length, (int)packed_entry.UnpackedSize)) { reader.Unpack(); - return new MemoryStream (reader.Data); + return new BinMemoryStream (reader.Data, entry.Name); } } return input; @@ -240,14 +240,14 @@ namespace GameRes.Formats.FC01 { var data = arc.File.View.ReadBytes (entry.Offset, entry.Size); Decrypt (data, 0, data.Length, mrg_entry.Key, mrg_entry.ArcKey); - input = new MemoryStream (data); + input = new BinMemoryStream (data, entry.Name); } else if (mrg_entry.Method >= 2) { var data = arc.File.View.ReadBytes (entry.Offset, entry.Size); var reader = new MrgDecoder (data); reader.Unpack(); - input = new MemoryStream (reader.Data); + input = new BinMemoryStream (reader.Data, entry.Name); } else input = arc.File.CreateStream (entry.Offset, entry.Size); @@ -257,7 +257,7 @@ namespace GameRes.Formats.FC01 using (var reader = new MrgLzssReader (input, (int)input.Length, (int)mrg_entry.UnpackedSize)) { reader.Unpack(); - return new MemoryStream (reader.Data); + return new BinMemoryStream (reader.Data, entry.Name); } } return input; diff --git a/ArcFormats/FC01/ImageACD.cs b/ArcFormats/FC01/ImageACD.cs index ade96d95..95b1ea9f 100644 --- a/ArcFormats/FC01/ImageACD.cs +++ b/ArcFormats/FC01/ImageACD.cs @@ -45,20 +45,18 @@ namespace GameRes.Formats.FC01 public override string Description { get { return "F&C Co. image format"; } } public override uint Signature { get { return 0x20444341; } } // 'ACD' - public override ImageMetaData ReadMetaData (Stream stream) + public override ImageMetaData ReadMetaData (IBinaryStream stream) { - var header = new byte[0x1c]; - if (header.Length != stream.Read (header, 0, header.Length)) - return null; - int header_size = LittleEndian.ToInt32 (header, 8); - if (!Binary.AsciiEqual (header, 4, "1.00") || header_size < 0x1c) + var header = stream.ReadHeader (0x1c); + int header_size = header.ToInt32 (8); + if (!header.AsciiEqual (4, "1.00") || header_size < 0x1c) throw new NotSupportedException ("Not supported ACD image version"); - int packed_size = LittleEndian.ToInt32 (header, 0x0C); - int unpacked_size = LittleEndian.ToInt32 (header, 0x10); + int packed_size = header.ToInt32 (0x0C); + int unpacked_size = header.ToInt32 (0x10); return new AcdMetaData { - Width = LittleEndian.ToUInt32 (header, 0x14), - Height = LittleEndian.ToUInt32 (header, 0x18), + Width = header.ToUInt32 (0x14), + Height = header.ToUInt32 (0x18), BPP = 24, DataOffset = header_size, PackedSize = packed_size, @@ -66,14 +64,12 @@ namespace GameRes.Formats.FC01 }; } - public override ImageData Read (Stream stream, ImageMetaData info) + public override ImageData Read (IBinaryStream stream, ImageMetaData info) { - var meta = info as AcdMetaData; - if (null == meta) - throw new ArgumentException ("AcdFormat.Read should be supplied with AcdMetaData", "info"); + var meta = (AcdMetaData)info; stream.Position = meta.DataOffset; - using (var reader = new MrgLzssReader (stream, meta.PackedSize, meta.UnpackedSize)) + using (var reader = new MrgLzssReader (stream.AsStream, meta.PackedSize, meta.UnpackedSize)) { reader.Unpack(); var decoder = new AcdDecoder (reader.Data, meta); diff --git a/ArcFormats/FC01/ImageCLM.cs b/ArcFormats/FC01/ImageCLM.cs index 7196ed78..10cd3e6b 100644 --- a/ArcFormats/FC01/ImageCLM.cs +++ b/ArcFormats/FC01/ImageCLM.cs @@ -45,20 +45,18 @@ namespace GameRes.Formats.FC01 public override string Description { get { return "F&C Co. image format"; } } public override uint Signature { get { return 0x204D4C43; } } // 'CLM' - public override ImageMetaData ReadMetaData (Stream stream) + public override ImageMetaData ReadMetaData (IBinaryStream stream) { - var header = new byte[0x40]; - if (header.Length != stream.Read (header, 0, header.Length)) + var header = stream.ReadHeader (0x40); + if (!header.AsciiEqual (4, "1.00")) return null; - if (!Binary.AsciiEqual (header, 4, "1.00")) - return null; - uint data_offset = LittleEndian.ToUInt32 (header, 0x10); + uint data_offset = header.ToUInt32 (0x10); if (data_offset < 0x40) return null; - uint width = LittleEndian.ToUInt32 (header, 0x1C); - uint height = LittleEndian.ToUInt32 (header, 0x20); - int bpp = LittleEndian.ToInt32 (header, 0x24); - int unpacked_size = LittleEndian.ToInt32 (header, 0x28); + uint width = header.ToUInt32 (0x1C); + uint height = header.ToUInt32 (0x20); + int bpp = header.ToInt32 (0x24); + int unpacked_size = header.ToInt32 (0x28); return new ClmMetaData { Width = width, @@ -69,7 +67,7 @@ namespace GameRes.Formats.FC01 }; } - public override ImageData Read (Stream stream, ImageMetaData info) + public override ImageData Read (IBinaryStream stream, ImageMetaData info) { var meta = (ClmMetaData)info; stream.Position = meta.DataOffset; @@ -78,7 +76,7 @@ namespace GameRes.Formats.FC01 if (8 == meta.BPP) { format = PixelFormats.Indexed8; - palette = ReadPalette (stream); + palette = ReadPalette (stream.AsStream); } else if (24 == meta.BPP) format = PixelFormats.Bgr24; @@ -87,7 +85,7 @@ namespace GameRes.Formats.FC01 else throw new NotSupportedException ("Not supported CLM color depth"); int packed_size = (int)(stream.Length - stream.Position); - using (var reader = new MrgLzssReader (stream, packed_size, meta.UnpackedSize)) + using (var reader = new MrgLzssReader (stream.AsStream, packed_size, meta.UnpackedSize)) { reader.Unpack(); return ImageData.Create (info, format, palette, reader.Data); diff --git a/ArcFormats/FC01/ImageMCG.cs b/ArcFormats/FC01/ImageMCG.cs index 8edb2bb0..e1e38b2f 100644 --- a/ArcFormats/FC01/ImageMCG.cs +++ b/ArcFormats/FC01/ImageMCG.cs @@ -67,31 +67,29 @@ namespace GameRes.Formats.FC01 set { KnownKeys = ((McgScheme)value).KnownKeys; } } - public override ImageMetaData ReadMetaData (Stream stream) + public override ImageMetaData ReadMetaData (IBinaryStream stream) { - byte[] header = new byte[0x40]; - if (header.Length != stream.Read (header, 0, header.Length)) - return null; + var header = stream.ReadHeader (0x40); if (header[5] != '.') return null; int version = header[4] * 100 + header[6] * 10 + header[7] - 0x14D0; if (version != 200 && version != 101) throw new NotSupportedException ("Not supported MCG format version"); - int header_size = LittleEndian.ToInt32 (header, 0x10); + int header_size = header.ToInt32 (0x10); if (header_size < 0x40) return null; - int bpp = LittleEndian.ToInt32 (header, 0x24); + int bpp = header.ToInt32 (0x24); if (24 != bpp) throw new NotSupportedException ("Not supported MCG image bitdepth"); return new McgMetaData { - Width = LittleEndian.ToUInt32 (header, 0x1c), - Height = LittleEndian.ToUInt32 (header, 0x20), - OffsetX = LittleEndian.ToInt32 (header, 0x14), - OffsetY = LittleEndian.ToInt32 (header, 0x18), + Width = header.ToUInt32 (0x1c), + Height = header.ToUInt32 (0x20), + OffsetX = header.ToInt32 (0x14), + OffsetY = header.ToInt32 (0x18), BPP = bpp, DataOffset = header_size, - PackedSize = LittleEndian.ToInt32 (header, 0x38) - header_size, + PackedSize = header.ToInt32 (0x38) - header_size, Version = version, }; } @@ -99,7 +97,7 @@ namespace GameRes.Formats.FC01 // cache key value so that dialog does not pop up on every file accessed. byte? LastKey = null; - public override ImageData Read (Stream stream, ImageMetaData info) + public override ImageData Read (IBinaryStream stream, ImageMetaData info) { var meta = (McgMetaData)info; byte key = Settings.Default.MCGLastKey; @@ -113,7 +111,7 @@ namespace GameRes.Formats.FC01 else key = LastKey.Value; } - var reader = new McgDecoder (stream, meta, key); + var reader = new McgDecoder (stream.AsStream, meta, key); reader.Unpack(); if (reader.Key != 0) LastKey = reader.Key; diff --git a/ArcFormats/Favorite/ArcFVP.cs b/ArcFormats/Favorite/ArcFVP.cs index d62efd9b..e8d4b48d 100644 --- a/ArcFormats/Favorite/ArcFVP.cs +++ b/ArcFormats/Favorite/ArcFVP.cs @@ -80,7 +80,7 @@ namespace GameRes.Formats.FVP using (var decoder = new LzwDecoder (input, unpacked_size)) { decoder.Unpack(); - return new MemoryStream (decoder.Output); + return new BinMemoryStream (decoder.Output, entry.Name); } } } diff --git a/ArcFormats/Favorite/ImageHZC.cs b/ArcFormats/Favorite/ImageHZC.cs index 2d911654..a0a71166 100644 --- a/ArcFormats/Favorite/ImageHZC.cs +++ b/ArcFormats/Favorite/ImageHZC.cs @@ -47,28 +47,26 @@ namespace GameRes.Formats.FVP public override string Description { get { return "Favorite View Point image format"; } } public override uint Signature { get { return 0x31637A68; } } // 'HZC1' - public override ImageMetaData ReadMetaData (Stream stream) + public override ImageMetaData ReadMetaData (IBinaryStream stream) { - var header = new byte[0x2C]; - if (header.Length != stream.Read (header, 0, header.Length)) + var header = stream.ReadHeader (0x2C); + if (!header.AsciiEqual (0xC, "NVSG")) return null; - if (!Binary.AsciiEqual (header, 0xC, "NVSG")) - return null; - int type = LittleEndian.ToUInt16 (header, 0x12); + int type = header.ToUInt16 (0x12); return new HzcMetaData { - Width = LittleEndian.ToUInt16 (header, 0x14), - Height = LittleEndian.ToUInt16 (header, 0x16), - OffsetX = LittleEndian.ToInt16 (header, 0x18), - OffsetY = LittleEndian.ToInt16 (header, 0x1A), + Width = header.ToUInt16 (0x14), + Height = header.ToUInt16 (0x16), + OffsetX = header.ToInt16 (0x18), + OffsetY = header.ToInt16 (0x1A), BPP = 0 == type ? 24 : type > 2 ? 8 : 32, Type = type, - UnpackedSize = LittleEndian.ToInt32 (header, 4), - HeaderSize = LittleEndian.ToInt32 (header, 8), + UnpackedSize = header.ToInt32 (4), + HeaderSize = header.ToInt32 (8), }; } - public override ImageData Read (Stream stream, ImageMetaData info) + public override ImageData Read (IBinaryStream stream, ImageMetaData info) { var meta = (HzcMetaData)info; BitmapPalette palette = null; @@ -90,7 +88,7 @@ namespace GameRes.Formats.FVP } } stream.Position = 12 + meta.HeaderSize; - using (var z = new ZLibStream (stream, CompressionMode.Decompress, true)) + using (var z = new ZLibStream (stream.AsStream, CompressionMode.Decompress, true)) { var pixels = new byte[stride * (int)meta.Height]; if (pixels.Length != z.Read (pixels, 0, pixels.Length)) diff --git a/ArcFormats/Ffa/ArcBlackPackage.cs b/ArcFormats/Ffa/ArcBlackPackage.cs index ab2f7d6d..d65e98c9 100644 --- a/ArcFormats/Ffa/ArcBlackPackage.cs +++ b/ArcFormats/Ffa/ArcBlackPackage.cs @@ -92,7 +92,7 @@ namespace GameRes.Formats.Ffa using (var reader = new LzssReader (input, packed, unpacked)) { reader.Unpack(); - return new MemoryStream (reader.Data); + return new BinMemoryStream (reader.Data, entry.Name); } } } diff --git a/ArcFormats/Ffa/AudioWA1.cs b/ArcFormats/Ffa/AudioWA1.cs index 3e10175f..cacb797a 100644 --- a/ArcFormats/Ffa/AudioWA1.cs +++ b/ArcFormats/Ffa/AudioWA1.cs @@ -39,18 +39,9 @@ namespace GameRes.Formats.Ffa public override string Description { get { return "FFA System wave audio format"; } } public override uint Signature { get { return 0; } } - private static int ReadInt32 (Stream file) + public override SoundInput TryOpen (IBinaryStream file) { - int dword = file.ReadByte(); - dword |= file.ReadByte() << 8; - dword |= file.ReadByte() << 16; - dword |= file.ReadByte() << 24; - return dword; - } - - public override SoundInput TryOpen (Stream file) - { - int packed = ReadInt32 (file); + int packed = file.ReadInt32(); if (packed < 0) return null; byte[] input; @@ -58,10 +49,10 @@ namespace GameRes.Formats.Ffa { if ((packed + 8) != file.Length) return null; - int unpacked = ReadInt32 (file); + int unpacked = file.ReadInt32(); if (unpacked <= 0) return null; - using (var reader = new LzssReader (file, packed, unpacked)) + using (var reader = new LzssReader (file.AsStream, packed, unpacked)) { reader.Unpack(); if (Binary.AsciiEqual (reader.Data, 0, "RIFF")) @@ -75,7 +66,7 @@ namespace GameRes.Formats.Ffa } else { - if (0x46464952 != ReadInt32 (file)) // 'RIFF' + if (0x46464952 != file.ReadInt32()) // 'RIFF' return null; file.Position = 0; input = new byte[file.Length]; diff --git a/ArcFormats/Ffa/AudioWA2.cs b/ArcFormats/Ffa/AudioWA2.cs index 9a5e2eb2..1ffb3763 100644 --- a/ArcFormats/Ffa/AudioWA2.cs +++ b/ArcFormats/Ffa/AudioWA2.cs @@ -36,7 +36,7 @@ namespace GameRes.Formats.Ffa public override string Description { get { return "FFA System PCM audio format"; } } public override uint Signature { get { return 0x4D435041; } } // 'APCM' - public override SoundInput TryOpen (Stream file) + public override SoundInput TryOpen (IBinaryStream file) { return new Wa2Input (file); } @@ -51,27 +51,25 @@ namespace GameRes.Formats.Ffa get { return (int)Format.AverageBytesPerSecond * 8; } } - public Wa2Input (Stream file) : base (null) + public Wa2Input (IBinaryStream file) : base (null) { - var header = new byte[0x2C]; - if (header.Length != file.Read (header, 0, header.Length)) - throw new EndOfStreamException(); - if (!Binary.AsciiEqual (header, 8, "WAVEfmt ")) + var header = file.ReadHeader (0x2C); + if (!header.AsciiEqual (8, "WAVEfmt ")) throw new InvalidFormatException(); var format = new WaveFormat(); - format.FormatTag = LittleEndian.ToUInt16 (header, 0x14); - format.Channels = LittleEndian.ToUInt16 (header, 0x16); - format.SamplesPerSecond = LittleEndian.ToUInt32 (header, 0x18); - format.AverageBytesPerSecond = LittleEndian.ToUInt32 (header, 0x1C); - format.BlockAlign = LittleEndian.ToUInt16 (header, 0x20); - format.BitsPerSample = LittleEndian.ToUInt16 (header, 0x22); + format.FormatTag = header.ToUInt16 (0x14); + format.Channels = header.ToUInt16 (0x16); + format.SamplesPerSecond = header.ToUInt32 (0x18); + format.AverageBytesPerSecond = header.ToUInt32 (0x1C); + format.BlockAlign = header.ToUInt16 (0x20); + format.BitsPerSample = header.ToUInt16 (0x22); format.ExtraSize = 0; this.Format = format; - uint pcm_size = LittleEndian.ToUInt32 (header, 0x28); + uint pcm_size = header.ToUInt32 (0x28); var pcm = new byte[pcm_size]; - Decode (file, pcm); + Decode (file.AsStream, pcm); Source = new MemoryStream (pcm); this.PcmSize = pcm_size; file.Dispose(); diff --git a/ArcFormats/Ffa/ImagePT1.cs b/ArcFormats/Ffa/ImagePT1.cs index 6fa53187..9095fdf7 100644 --- a/ArcFormats/Ffa/ImagePT1.cs +++ b/ArcFormats/Ffa/ImagePT1.cs @@ -59,40 +59,37 @@ namespace GameRes.Formats.Ffa throw new NotImplementedException ("Pt1Format.Write not implemented"); } - public override ImageMetaData ReadMetaData (Stream stream) + public override ImageMetaData ReadMetaData (IBinaryStream file) { - using (var input = new ArcView.Reader (stream)) - { - int type = input.ReadInt32(); - if (type < 0 || type > 3) - return null; - if (-1 != input.ReadInt32()) - return null; - int x = input.ReadInt32(); - int y = input.ReadInt32(); - uint width = input.ReadUInt32(); - uint height = input.ReadUInt32(); - uint comp_size = input.ReadUInt32(); - uint uncomp_size = input.ReadUInt32(); - if (uncomp_size != width*height*3u) - return null; - return new Pt1MetaData { - Width = width, - Height = height, - OffsetX = x, - OffsetY = y, - BPP = 3 == type ? 32 : 24, - Type = type, - PackedSize = comp_size, - UnpackedSize = uncomp_size - }; - } + int type = file.ReadInt32(); + if (type < 0 || type > 3) + return null; + if (-1 != file.ReadInt32()) + return null; + int x = file.ReadInt32(); + int y = file.ReadInt32(); + uint width = file.ReadUInt32(); + uint height = file.ReadUInt32(); + uint comp_size = file.ReadUInt32(); + uint uncomp_size = file.ReadUInt32(); + if (uncomp_size != width*height*3u) + return null; + return new Pt1MetaData { + Width = width, + Height = height, + OffsetX = x, + OffsetY = y, + BPP = 3 == type ? 32 : 24, + Type = type, + PackedSize = comp_size, + UnpackedSize = uncomp_size + }; } - public override ImageData Read (Stream stream, ImageMetaData info) + public override ImageData Read (IBinaryStream stream, ImageMetaData info) { var meta = (Pt1MetaData)info; - var reader = new Reader (stream, meta); + var reader = new Reader (stream.AsStream, meta); reader.Unpack(); return ImageData.Create (meta, reader.Format, null, reader.Data); } diff --git a/ArcFormats/G2/ArcGCEX.cs b/ArcFormats/G2/ArcGCEX.cs index 13a2c705..dba94f48 100644 --- a/ArcFormats/G2/ArcGCEX.cs +++ b/ArcFormats/G2/ArcGCEX.cs @@ -125,7 +125,7 @@ namespace GameRes.Formats.G2 using (input) using (var reader = new GceReader (input, (int)pentry.UnpackedSize)) { - return new MemoryStream (reader.Data); + return new BinMemoryStream (reader.Data, entry.Name); } } } diff --git a/ArcFormats/G2/ImageBGRA.cs b/ArcFormats/G2/ImageBGRA.cs index a00094d8..50d179a2 100644 --- a/ArcFormats/G2/ImageBGRA.cs +++ b/ArcFormats/G2/ImageBGRA.cs @@ -42,25 +42,23 @@ namespace GameRes.Formats.G2 Extensions = new string[] { "argb", "arg" }; } - public override ImageMetaData ReadMetaData (Stream stream) + public override ImageMetaData ReadMetaData (IBinaryStream stream) { - var header = new byte[0x10]; - if (header.Length != stream.Read (header, 0, header.Length)) - return null; - uint bitmap = LittleEndian.ToUInt32 (header, 4); + stream.Position = 4; + uint bitmap = stream.ReadUInt32(); if (0x08080808 != bitmap) return null; return new ImageMetaData { - Width = LittleEndian.ToUInt32 (header, 8), - Height = LittleEndian.ToUInt32 (header, 12), + Width = stream.ReadUInt32(), + Height = stream.ReadUInt32(), BPP = 32, }; } - public override ImageData Read (Stream stream, ImageMetaData info) + public override ImageData Read (IBinaryStream stream, ImageMetaData info) { - stream.Seek (0x10, SeekOrigin.Current); + stream.Position = 0x10; var pixels = new byte[info.Width*info.Height*4]; if (pixels.Length != stream.Read (pixels, 0, pixels.Length)) throw new EndOfStreamException(); diff --git a/ArcFormats/Glib2/ArcG2.cs b/ArcFormats/Glib2/ArcG2.cs index e04b338f..19b87f13 100644 --- a/ArcFormats/Glib2/ArcG2.cs +++ b/ArcFormats/Glib2/ArcG2.cs @@ -193,7 +193,7 @@ namespace GameRes.Formats.Glib2 current_decoder = (current_decoder + 1) & 3; offset += current_chunk_size; } - return new MemoryStream (output); + return new BinMemoryStream (output, entry.Name); } } diff --git a/ArcFormats/Glib2/ImagePGX.cs b/ArcFormats/Glib2/ImagePGX.cs index 2e8d53bf..e7c1d03f 100644 --- a/ArcFormats/Glib2/ImagePGX.cs +++ b/ArcFormats/Glib2/ImagePGX.cs @@ -58,34 +58,32 @@ namespace GameRes.Formats.Glib2 static readonly InfoReader InfoCache = new InfoReader(); - public override ImageMetaData ReadMetaData (Stream stream) + public override ImageMetaData ReadMetaData (IBinaryStream stream) { - var header = new byte[0x18]; - if (header.Length != stream.Read (header, 0, header.Length)) - return null; + var header = stream.ReadHeader (0x18); return new PgxMetaData { - Width = LittleEndian.ToUInt32 (header, 8), - Height = LittleEndian.ToUInt32 (header, 12), - BPP = (LittleEndian.ToInt16 (header, 0x10) & 1) == 0 ? 24 : 32, - PackedSize = LittleEndian.ToInt32 (header, 0x14), - Flags = LittleEndian.ToUInt16 (header, 0x12), + Width = header.ToUInt32 (8), + Height = header.ToUInt32 (12), + BPP = (header.ToInt16 (0x10) & 1) == 0 ? 24 : 32, + PackedSize = header.ToInt32 (0x14), + Flags = header.ToUInt16 (0x12), }; } - public override ImageData Read (Stream stream, ImageMetaData info) + public override ImageData Read (IBinaryStream stream, ImageMetaData info) { var meta = (PgxMetaData)info; stream.Position = 0x20; if (0 != (meta.Flags & 0x1000)) { - ReadGms (stream); + ReadGms (stream.AsStream); } PixelFormat format = 32 == meta.BPP ? PixelFormats.Bgra32 : PixelFormats.Bgr32; int stride = (int)meta.Width * 4; var pixels = new byte[stride * (int)meta.Height]; // stream.Seek (-meta.PackedSize, SeekOrigin.End); - LzssUnpack (stream, pixels); + LzssUnpack (stream.AsStream, pixels); var layer = InfoCache.GetInfo (info.FileName); if (null != layer && null != layer.Rect) { diff --git a/ArcFormats/Gpk2/ImageGFB.cs b/ArcFormats/Gpk2/ImageGFB.cs index ed18ca7f..4d0d1c9e 100644 --- a/ArcFormats/Gpk2/ImageGFB.cs +++ b/ArcFormats/Gpk2/ImageGFB.cs @@ -29,7 +29,6 @@ using System.IO; using System.Windows.Media; using System.Windows.Media.Imaging; using GameRes.Compression; -using GameRes.Utility; namespace GameRes.Formats.Gpk2 { @@ -47,37 +46,35 @@ namespace GameRes.Formats.Gpk2 public override string Description { get { return "GPK2 image format"; } } public override uint Signature { get { return 0x20424647; } } // 'GFB ' - public override ImageMetaData ReadMetaData (Stream stream) + public override ImageMetaData ReadMetaData (IBinaryStream stream) { - var header = new byte[0x40]; - if (header.Length != stream.Read (header, 0, header.Length)) - return null; + var header = stream.ReadHeader (0x40); return new GfbMetaData { - Width = LittleEndian.ToUInt32 (header, 0x1C), - Height = LittleEndian.ToUInt32 (header, 0x20), - BPP = LittleEndian.ToUInt16 (header, 0x26), - PackedSize = LittleEndian.ToInt32 (header,0x0C), - UnpackedSize = LittleEndian.ToInt32 (header,0x10), - DataOffset = LittleEndian.ToInt32 (header,0x14), + Width = header.ToUInt32 (0x1C), + Height = header.ToUInt32 (0x20), + BPP = header.ToUInt16 (0x26), + PackedSize = header.ToInt32 (0x0C), + UnpackedSize = header.ToInt32 (0x10), + DataOffset = header.ToInt32 (0x14), }; } - public override ImageData Read (Stream stream, ImageMetaData info) + public override ImageData Read (IBinaryStream stream, ImageMetaData info) { var meta = (GfbMetaData)info; BitmapPalette palette = null; if (8 == meta.BPP && meta.DataOffset != 0x40) { stream.Position = 0x40; - palette = ReadPalette (stream, meta.DataOffset - 0x40); + palette = ReadPalette (stream.AsStream, meta.DataOffset - 0x40); } stream.Position = meta.DataOffset; byte[] pixels = new byte[meta.UnpackedSize]; if (0 != meta.PackedSize) { - using (var lzss = new LzssStream (stream, LzssMode.Decompress, true)) + using (var lzss = new LzssStream (stream.AsStream, LzssMode.Decompress, true)) lzss.Read (pixels, 0, pixels.Length); } else diff --git a/ArcFormats/GsPack/ImageGS.cs b/ArcFormats/GsPack/ImageGS.cs index 328290bd..c515c99c 100644 --- a/ArcFormats/GsPack/ImageGS.cs +++ b/ArcFormats/GsPack/ImageGS.cs @@ -54,36 +54,33 @@ namespace GameRes.Formats.Gs Extensions = new string[] { "pic" }; // made-up } - public override ImageMetaData ReadMetaData (Stream stream) + public override ImageMetaData ReadMetaData (IBinaryStream file) { - using (var input = new ArcView.Reader (stream)) + file.Position = 4; + var info = new PicMetaData(); + info.PackedSize = file.ReadUInt32(); + info.UnpackedSize = file.ReadUInt32(); + info.HeaderSize = file.ReadUInt32(); + if (info.HeaderSize >= file.Length || info.PackedSize + info.HeaderSize > file.Length) + return null; + file.ReadUInt32(); + info.Width = file.ReadUInt32(); + info.Height = file.ReadUInt32(); + info.BPP = file.ReadInt32(); + if (info.HeaderSize >= 0x2C) { - var info = new PicMetaData(); - input.ReadUInt32(); - info.PackedSize = input.ReadUInt32(); - info.UnpackedSize = input.ReadUInt32(); - info.HeaderSize = input.ReadUInt32(); - if (info.HeaderSize >= stream.Length || info.PackedSize + info.HeaderSize > stream.Length) - return null; - input.ReadUInt32(); - info.Width = input.ReadUInt32(); - info.Height = input.ReadUInt32(); - info.BPP = input.ReadInt32(); - if (info.HeaderSize >= 0x2C) - { - input.ReadInt32(); - info.OffsetX = input.ReadInt32(); - info.OffsetY = input.ReadInt32(); - } - return info; + file.ReadInt32(); + info.OffsetX = file.ReadInt32(); + info.OffsetY = file.ReadInt32(); } + return info; } - public override ImageData Read (Stream stream, ImageMetaData info) + public override ImageData Read (IBinaryStream file, ImageMetaData info) { var meta = (PicMetaData)info; - stream.Position = meta.HeaderSize; - using (var input = new LzssStream (stream, LzssMode.Decompress, true)) + file.Position = meta.HeaderSize; + using (var input = new LzssStream (file.AsStream, LzssMode.Decompress, true)) { BitmapPalette palette = null; PixelFormat format; diff --git a/ArcFormats/Hexenhaus/ArcWAG.cs b/ArcFormats/Hexenhaus/ArcWAG.cs index f477306b..cb04dd08 100644 --- a/ArcFormats/Hexenhaus/ArcWAG.cs +++ b/ArcFormats/Hexenhaus/ArcWAG.cs @@ -157,16 +157,16 @@ namespace GameRes.Formats.Hexenhaus Extensions = new string[] { "png" }; } - public override ImageMetaData ReadMetaData (Stream stream) + public override ImageMetaData ReadMetaData (IBinaryStream stream) { ImageMetaData info; - using (var png = new StreamRegion (stream, 0x10, true)) + using (var input = new StreamRegion (stream.AsStream, 0x10, true)) + using (var png = new BinaryStream (input, stream.Name)) info = base.ReadMetaData (png); if (null == info) return null; stream.Seek (-14, SeekOrigin.End); - var cntr = new byte[12]; - stream.Read (cntr, 0, 12); + var cntr = stream.ReadBytes (12); if (Binary.AsciiEqual (cntr, "CNTR")) { info.OffsetX = LittleEndian.ToInt32 (cntr, 4); @@ -175,9 +175,10 @@ namespace GameRes.Formats.Hexenhaus return info; } - public override ImageData Read (Stream stream, ImageMetaData info) + public override ImageData Read (IBinaryStream stream, ImageMetaData info) { - using (var png = new StreamRegion (stream, 0x10, true)) + using (var input = new StreamRegion (stream.AsStream, 0x10, true)) + using (var png = new BinaryStream (input, stream.Name)) return base.Read (png, info); } diff --git a/ArcFormats/Ikura/ArcDRS.cs b/ArcFormats/Ikura/ArcDRS.cs index bd33b2fc..25cedd97 100644 --- a/ArcFormats/Ikura/ArcDRS.cs +++ b/ArcFormats/Ikura/ArcDRS.cs @@ -213,7 +213,7 @@ namespace GameRes.Formats.Ikura byte key = data[6]; ApplyTransformation (data, 8, x => x ^ key); } - return new MemoryStream (data); + return new BinMemoryStream (data, entry.Name); } public override ResourceOptions GetDefaultOptions () diff --git a/ArcFormats/Ikura/ImageDRG.cs b/ArcFormats/Ikura/ImageDRG.cs index 0ae21c29..b33a8868 100644 --- a/ArcFormats/Ikura/ImageDRG.cs +++ b/ArcFormats/Ikura/ImageDRG.cs @@ -58,9 +58,9 @@ namespace GameRes.Formats.Ikura } } - public override ImageMetaData ReadMetaData (Stream stream) + public override ImageMetaData ReadMetaData (IBinaryStream stream) { - uint signature = ~FormatCatalog.ReadSignature (stream); + uint signature = ~stream.Signature; int bpp; switch (signature) { @@ -69,19 +69,16 @@ namespace GameRes.Formats.Ikura case 0x48474948: bpp = 16; break; default: return null; } - using (var input = new BinaryReader (stream, Encoding.ASCII, true)) - { - uint width = input.ReadUInt16(); - uint height = input.ReadUInt16(); - return new ImageMetaData { - Width = width, - Height = height, - BPP = bpp, - }; - } + uint width = stream.ReadUInt16(); + uint height = stream.ReadUInt16(); + return new ImageMetaData { + Width = width, + Height = height, + BPP = bpp, + }; } - public override ImageData Read (Stream file, ImageMetaData info) + public override ImageData Read (IBinaryStream file, ImageMetaData info) { file.Position = 8; PixelFormat format; @@ -91,7 +88,7 @@ namespace GameRes.Formats.Ikura format = PixelFormats.Bgr565; int stride = ((int)info.Width * info.BPP / 8 + 3) & ~3; - var pixel_data = DecodeStream (file, stride*(int)info.Height); + var pixel_data = DecodeStream (file.AsStream, stride*(int)info.Height); if (null == pixel_data) throw new InvalidFormatException(); return ImageData.Create (info, format, null, pixel_data, stride); @@ -414,28 +411,25 @@ namespace GameRes.Formats.Ikura public override string Description { get { return "Digital Romance System indexed image format"; } } public override uint Signature { get { return ~0x47363532u; } } // '256G' - public override ImageMetaData ReadMetaData (Stream stream) + public override ImageMetaData ReadMetaData (IBinaryStream file) { - using (var input = new ArcView.Reader (stream)) + file.Position = 4; + var info = new GgdMetaData { BPP = 8 }; + info.HeaderSize = file.ReadUInt32(); + info.Width = file.ReadUInt32(); + int height = file.ReadInt32(); + if (height < 0) { - input.ReadUInt32(); - var info = new GgdMetaData { BPP = 8 }; - info.HeaderSize = input.ReadUInt32(); - info.Width = input.ReadUInt32(); - int height = input.ReadInt32(); - if (height < 0) - { - height = -height; - info.Flipped = true; - } - info.Height = (uint)height; - input.ReadInt64(); - info.BitmapSize = input.ReadUInt32(); - return info; + height = -height; + info.Flipped = true; } + info.Height = (uint)height; + file.ReadInt64(); + info.BitmapSize = file.ReadUInt32(); + return info; } - public override ImageData Read (Stream file, ImageMetaData info) + public override ImageData Read (IBinaryStream file, ImageMetaData info) { var meta = (GgdMetaData)info; file.Position = meta.HeaderSize + 4; @@ -449,7 +443,7 @@ namespace GameRes.Formats.Ikura } file.Seek (4, SeekOrigin.Current); int input_size = (int)(file.Length - file.Position); - using (var reader = new LzssReader (file, input_size, (int)meta.BitmapSize)) + using (var reader = new LzssReader (file.AsStream, input_size, (int)meta.BitmapSize)) { reader.Unpack(); var palette = new BitmapPalette (colors); @@ -488,153 +482,146 @@ namespace GameRes.Formats.Ikura throw new NotImplementedException ("GgaFormat.Write not implemented"); } - public override ImageMetaData ReadMetaData (Stream stream) + public override ImageMetaData ReadMetaData (IBinaryStream stream) { - var header = new byte[24]; - if (header.Length != stream.Read (header, 0, header.Length)) - return null; - if (!Binary.AsciiEqual (header, "GGA00000")) + var header = stream.ReadHeader (24); + if (!header.AsciiEqual ("GGA00000")) return null; return new GgaMetaData { - Width = LittleEndian.ToUInt16 (header, 8), - Height = LittleEndian.ToUInt16 (header, 10), + Width = header.ToUInt16 (8), + Height = header.ToUInt16 (10), BPP = header[14], Flags = header[15], - HeaderSize = LittleEndian.ToUInt32 (header, 16), - CompSize = LittleEndian.ToUInt32 (header, 20) + HeaderSize = header.ToUInt32 (16), + CompSize = header.ToUInt32 (20) }; } - public override ImageData Read (Stream file, ImageMetaData info) + public override ImageData Read (IBinaryStream file, ImageMetaData info) { - var meta = info as GgaMetaData; - if (null == meta) - throw new ArgumentException ("GgaFormat.Read should be supplied with GgaMetaData", "info"); + var meta = (GgaMetaData)info; file.Position = meta.HeaderSize; var pixel_data = DecodeStream (file, meta); PixelFormat format = 0 == (meta.Flags & 1) ? PixelFormats.Bgr32 : PixelFormats.Bgra32; return ImageData.Create (info, format, null, pixel_data); } - byte[] DecodeStream (Stream file, GgaMetaData meta) + byte[] DecodeStream (IBinaryStream input, GgaMetaData meta) { int dst = 0; var output = new byte[meta.Width*meta.Height*4]; - using (var input = new BinaryReader (file, Encoding.ASCII, true)) + var end_pos = input.Position + meta.CompSize; + while (input.Position < end_pos) { - var end_pos = input.BaseStream.Position + meta.CompSize; - while (input.BaseStream.Position < end_pos) + int code = input.ReadByte(); + switch (code) { - int code = input.ReadByte(); - switch (code) + case 0: { - case 0: - { - int src = dst - 4; - int count = input.ReadByte() * 4; - Binary.CopyOverlapped (output, src, dst, count); - dst += count; - break; - } - case 1: - { - int src = dst - 4; - int count = input.ReadUInt16() * 4; - Binary.CopyOverlapped (output, src, dst, count); - dst += count; - break; - } - case 2: - { - int l = input.ReadByte(); - int src = dst - (l << 2); - System.Buffer.BlockCopy (output, src, output, dst, 4); - dst += 4; - break; - } - case 3: - { - int l = input.ReadUInt16(); - int src = dst - (l << 2); - System.Buffer.BlockCopy (output, src, output, dst, 4); - dst += 4; - break; - } - case 4: - { - int l = input.ReadByte(); - int src = dst - (l << 2); - int count = input.ReadByte() * 4; - Binary.CopyOverlapped (output, src, dst, count); - dst += count; - break; - } - case 5: - { - int l = input.ReadByte(); - int src = dst - (l << 2); - int count = input.ReadUInt16() * 4; - Binary.CopyOverlapped (output, src, dst, count); - dst += count; - break; - } - case 6: - { - int l = input.ReadUInt16(); - int src = dst - (l << 2); - int count = input.ReadByte() * 4; - Binary.CopyOverlapped (output, src, dst, count); - dst += count; - break; - } - case 7: - { - int l = input.ReadUInt16(); - int src = dst - (l << 2); - int count = input.ReadUInt16() * 4; - Binary.CopyOverlapped (output, src, dst, count); - dst += count; - break; - } - case 8: - { - System.Buffer.BlockCopy (output, dst-4, output, dst, 4); - dst += 4; - break; - } - case 9: - { - int src = dst - (int)meta.Width * 4; - System.Buffer.BlockCopy (output, src, output, dst, 4); - dst += 4; - break; - } - case 0x0a: - { - int src = dst - ((int)meta.Width * 4 + 4); - System.Buffer.BlockCopy (output, src, output, dst, 4); - dst += 4; - break; - } - case 0x0b: - { - int src = dst - ((int)meta.Width * 4 - 4); - System.Buffer.BlockCopy (output, src, output, dst, 4); - dst += 4; - break; - } - default: - { - int count = (code-11)*4; - if (count != input.Read (output, dst, count)) - throw new InvalidFormatException ("Unexpected end of input"); - dst += count; - break; - } + int src = dst - 4; + int count = input.ReadByte() * 4; + Binary.CopyOverlapped (output, src, dst, count); + dst += count; + break; + } + case 1: + { + int src = dst - 4; + int count = input.ReadUInt16() * 4; + Binary.CopyOverlapped (output, src, dst, count); + dst += count; + break; + } + case 2: + { + int l = input.ReadByte(); + int src = dst - (l << 2); + System.Buffer.BlockCopy (output, src, output, dst, 4); + dst += 4; + break; + } + case 3: + { + int l = input.ReadUInt16(); + int src = dst - (l << 2); + System.Buffer.BlockCopy (output, src, output, dst, 4); + dst += 4; + break; + } + case 4: + { + int l = input.ReadByte(); + int src = dst - (l << 2); + int count = input.ReadByte() * 4; + Binary.CopyOverlapped (output, src, dst, count); + dst += count; + break; + } + case 5: + { + int l = input.ReadByte(); + int src = dst - (l << 2); + int count = input.ReadUInt16() * 4; + Binary.CopyOverlapped (output, src, dst, count); + dst += count; + break; + } + case 6: + { + int l = input.ReadUInt16(); + int src = dst - (l << 2); + int count = input.ReadByte() * 4; + Binary.CopyOverlapped (output, src, dst, count); + dst += count; + break; + } + case 7: + { + int l = input.ReadUInt16(); + int src = dst - (l << 2); + int count = input.ReadUInt16() * 4; + Binary.CopyOverlapped (output, src, dst, count); + dst += count; + break; + } + case 8: + { + System.Buffer.BlockCopy (output, dst-4, output, dst, 4); + dst += 4; + break; + } + case 9: + { + int src = dst - (int)meta.Width * 4; + System.Buffer.BlockCopy (output, src, output, dst, 4); + dst += 4; + break; + } + case 0x0a: + { + int src = dst - ((int)meta.Width * 4 + 4); + System.Buffer.BlockCopy (output, src, output, dst, 4); + dst += 4; + break; + } + case 0x0b: + { + int src = dst - ((int)meta.Width * 4 - 4); + System.Buffer.BlockCopy (output, src, output, dst, 4); + dst += 4; + break; + } + default: + { + int count = (code-11)*4; + if (count != input.Read (output, dst, count)) + throw new InvalidFormatException ("Unexpected end of input"); + dst += count; + break; } } - return output; } + return output; } } } diff --git a/ArcFormats/Ikura/ImageGGP.cs b/ArcFormats/Ikura/ImageGGP.cs index f437dcac..9ba72803 100644 --- a/ArcFormats/Ikura/ImageGGP.cs +++ b/ArcFormats/Ikura/ImageGGP.cs @@ -45,22 +45,21 @@ namespace GameRes.Formats.Ikura public override uint Signature { get { return 0x46504747u; } } // 'GGPF' public override bool CanWrite { get { return false; } } - public override ImageMetaData ReadMetaData (Stream stream) + public override ImageMetaData ReadMetaData (IBinaryStream stream) { - var header = new byte[0x24]; - if (header.Length != stream.Read (header, 0, header.Length)) - return null; - if (!Binary.AsciiEqual (header, 0, "GGPFAIKE")) + var header = stream.ReadHeader (0x24); + if (!header.AsciiEqual ("GGPFAIKE")) return null; var info = new GgpMetaData { - Offset = LittleEndian.ToUInt32 (header, 0x14), - Length = LittleEndian.ToUInt32 (header, 0x18), + Offset = header.ToUInt32 (0x14), + Length = header.ToUInt32 (0x18), }; for (int i = 0; i < 8; ++i) info.Key[i] = (byte)(header[i] ^ header[i+0xC]); stream.Position = info.Offset; - using (var png = new EncryptedStream (stream, info.Key, true)) + using (var enc = new EncryptedStream (stream.AsStream, info.Key, true)) + using (var png = new BinaryStream (enc, stream.Name)) { var png_info = base.ReadMetaData (png); info.Width = png_info.Width; @@ -72,13 +71,12 @@ namespace GameRes.Formats.Ikura } } - public override ImageData Read (Stream file, ImageMetaData info) + public override ImageData Read (IBinaryStream file, ImageMetaData info) { - var meta = info as GgpMetaData; - if (null == meta) - throw new ArgumentException ("GgpFormat.Read should be supplied with GgpMetaData", "info"); - using (var input = new StreamRegion (file, meta.Offset, meta.Length, true)) - using (var png = new EncryptedStream (input, meta.Key, true)) + var meta = (GgpMetaData)info; + using (var input = new StreamRegion (file.AsStream, meta.Offset, meta.Length, true)) + using (var enc = new EncryptedStream (input, meta.Key)) + using (var png = new BinaryStream (enc, file.Name)) return base.Read (png, info); } diff --git a/ArcFormats/Ikura/ImageYGP.cs b/ArcFormats/Ikura/ImageYGP.cs index 2ba18d6b..f721c444 100644 --- a/ArcFormats/Ikura/ImageYGP.cs +++ b/ArcFormats/Ikura/ImageYGP.cs @@ -45,50 +45,44 @@ namespace GameRes.Formats.Ikura public override string Description { get { return "Ikura GDL image format"; } } public override uint Signature { get { return 0x504759; } } // 'YGP' - public override ImageMetaData ReadMetaData (Stream stream) + public override ImageMetaData ReadMetaData (IBinaryStream stream) { stream.Position = 4; - using (var reader = new ArcView.Reader (stream)) + int mask_pos = stream.ReadUInt16(); // 04 + byte type = stream.ReadUInt8(); // 06 + if (type != 1 && type != 2) + return null; + var info = new YgpMetaData { Type = type, BPP = 32 }; + info.Flags = stream.ReadUInt8(); // 07 + int header_size = stream.ReadInt32(); // 08 + stream.Position = header_size; + info.DataSize = stream.ReadInt32(); // XX+00 + info.Width = stream.ReadUInt16(); // XX+04 + info.Height = stream.ReadUInt16(); // XX+06 + info.DataOffset = header_size+8; + if (0 != (info.Flags & 4)) { - int mask_pos = reader.ReadUInt16(); // 04 - byte type = reader.ReadByte(); // 06 - if (type != 1 && type != 2) - return null; - var info = new YgpMetaData { Type = type, BPP = 32 }; - info.Flags = reader.ReadByte(); // 07 - int header_size = reader.ReadInt32(); // 08 - stream.Position = header_size; - info.DataSize = reader.ReadInt32(); // XX+00 - info.Width = reader.ReadUInt16(); // XX+04 - info.Height = reader.ReadUInt16(); // XX+06 - info.DataOffset = header_size+8; - if (0 != (info.Flags & 4)) - { - stream.Position = 0x14; - info.OffsetX = reader.ReadInt16(); - info.OffsetY = reader.ReadInt16(); - } - return info; + stream.Position = 0x14; + info.OffsetX = stream.ReadInt16(); + info.OffsetY = stream.ReadInt16(); } + return info; } - public override ImageData Read (Stream stream, ImageMetaData info) + public override ImageData Read (IBinaryStream stream, ImageMetaData info) { var meta = (YgpMetaData)info; stream.Position = meta.DataOffset; int stride = (int)meta.Width * 4; var pixels = new byte[stride * (int)meta.Height]; if (0 != (meta.Flags & 1)) - { - using (var reader = new ArcView.Reader (stream)) - Unpack (reader, meta.DataSize, pixels); - } + Unpack (stream, meta.DataSize, pixels); else stream.Read (pixels, 0, pixels.Length); return ImageData.Create (info, PixelFormats.Bgra32, null, pixels); } - void Unpack (BinaryReader input, int input_size, byte[] output) + void Unpack (IBinaryStream input, int input_size, byte[] output) { int dst = 0; while (input_size > 0) diff --git a/ArcFormats/ImageDDS.cs b/ArcFormats/ImageDDS.cs index 174b134c..85299076 100644 --- a/ArcFormats/ImageDDS.cs +++ b/ArcFormats/ImageDDS.cs @@ -61,34 +61,32 @@ namespace GameRes.Formats.Microsoft public override string Description { get { return "Direct Draw Surface format"; } } public override uint Signature { get { return 0x20534444; } } // 'DDS' - public override ImageMetaData ReadMetaData (Stream stream) + public override ImageMetaData ReadMetaData (IBinaryStream stream) { - var header = new byte[0x6C]; - if (header.Length != stream.Read (header, 0, header.Length)) - return null; - int dwSize = LittleEndian.ToInt32 (header, 4); + var header = stream.ReadHeader (0x6C); + int dwSize = header.ToInt32 (4); if (dwSize < 0x7C) return null; - var bitflags = (DdsPF)LittleEndian.ToUInt32 (header, 0x50); + var bitflags = (DdsPF)header.ToUInt32 (0x50); string four_cc = null; if (bitflags.HasFlag (DdsPF.FourCC)) - four_cc = Binary.GetCString (header, 0x54, 4, Encoding.ASCII); + four_cc = Binary.GetCString (header.ToArray(), 0x54, 4, Encoding.ASCII); return new DdsMetaData { - Width = LittleEndian.ToUInt32 (header, 0x10), - Height = LittleEndian.ToUInt32 (header, 0xC), - BPP = LittleEndian.ToInt32 (header, 0x58), + Width = header.ToUInt32 (0x10), + Height = header.ToUInt32 (0xC), + BPP = header.ToInt32 (0x58), PixelFlags = bitflags, FourCC = four_cc, - RBitMask = LittleEndian.ToUInt32 (header, 0x5C), - GBitMask = LittleEndian.ToUInt32 (header, 0x60), - BBitMask = LittleEndian.ToUInt32 (header, 0x64), - ABitMask = LittleEndian.ToUInt32 (header, 0x68), + RBitMask = header.ToUInt32 (0x5C), + GBitMask = header.ToUInt32 (0x60), + BBitMask = header.ToUInt32 (0x64), + ABitMask = header.ToUInt32 (0x68), DataOffset = 4 + dwSize, }; } - public override ImageData Read (Stream stream, ImageMetaData info) + public override ImageData Read (IBinaryStream stream, ImageMetaData info) { var meta = (DdsMetaData)info; if (meta.PixelFlags.HasFlag (DdsPF.Yuv | DdsPF.Luminance)) @@ -98,7 +96,7 @@ namespace GameRes.Formats.Microsoft if (meta.PixelFlags.HasFlag (DdsPF.Rgb) && (0 == meta.RBitMask || 0 == meta.GBitMask || 0 == meta.BBitMask)) throw new InvalidFormatException(); - var pixels = ReadPixelData (stream, meta); + var pixels = ReadPixelData (stream.AsStream, meta); PixelFormat format; if (meta.PixelFlags.HasFlag (DdsPF.AlphaPixels) && meta.ABitMask != 0) format = PixelFormats.Bgra32; diff --git a/ArcFormats/ImageEGN.cs b/ArcFormats/ImageEGN.cs index 94aaeec6..9079493d 100644 --- a/ArcFormats/ImageEGN.cs +++ b/ArcFormats/ImageEGN.cs @@ -49,74 +49,66 @@ namespace GameRes.Formats.Unknown public override uint Signature { get { return 0; } } public override bool CanWrite { get { return false; } } - public override ImageMetaData ReadMetaData (Stream stream) + public override ImageMetaData ReadMetaData (IBinaryStream stream) { - using (var input = new ArcView.Reader (stream)) + int signature = (int)~stream.Signature; + int mode = (signature & 0x70) >> 4; // v6 + if (0 != (mode & 4)) + return null; + int flag = signature & 0xF; // v7 + int data_size, data_offset; + if (0 != (signature & 0x80)) { - int signature = ~input.ReadInt32(); - int mode = (signature & 0x70) >> 4; // v6 - if (0 != (mode & 4)) + data_offset = 4; + data_size = Binary.BigEndian (signature) & 0xFFFFFF; + } + else + { + data_offset = 8; + data_size = Binary.BigEndian (stream.ReadInt32()); + } + if (data_size <= 0 || data_size > 0xFFFFFF) // arbitrary max BMP size + return null; + var reader = new Reader (stream, 0x36, mode, flag); // size of BMP header + reader.Unpack(); + using (var bmp = new BinMemoryStream (reader.Data, stream.Name)) + { + var info = base.ReadMetaData (bmp); + if (null == info) return null; - int flag = signature & 0xF; // v7 - int data_size, data_offset; - if (0 != (signature & 0x80)) + return new EgnMetaData { - data_offset = 4; - data_size = Binary.BigEndian (signature) & 0xFFFFFF; - } - else - { - data_offset = 8; - data_size = Binary.BigEndian (input.ReadInt32()); - } - if (data_size <= 0 || data_size > 0xFFFFFF) // arbitrary max BMP size - return null; - var reader = new Reader (input, 0x36, mode, flag); // size of BMP header - reader.Unpack(); - using (var bmp = new MemoryStream (reader.Data)) - { - var info = base.ReadMetaData (bmp); - if (null == info) - return null; - return new EgnMetaData - { - Width = info.Width, - Height = info.Height, - BPP = info.BPP, - Mode = mode, - Flag = flag, - DataOffset = data_offset, - UnpackedSize = data_size, - }; - } + Width = info.Width, + Height = info.Height, + BPP = info.BPP, + Mode = mode, + Flag = flag, + DataOffset = data_offset, + UnpackedSize = data_size, + }; } } - public override ImageData Read (Stream stream, ImageMetaData info) + public override ImageData Read (IBinaryStream stream, ImageMetaData info) { - var meta = info as EgnMetaData; - if (null == meta) - throw new ArgumentException ("EgnFormat.Read should be supplied with EgnMetaData", "info"); + var meta = (EgnMetaData)info; stream.Position = meta.DataOffset; - using (var input = new ArcView.Reader (stream)) - { - var reader = new Reader (input, meta.UnpackedSize, meta.Mode, meta.Flag); - reader.Unpack(); - using (var bmp = new MemoryStream (reader.Data)) - return base.Read (bmp, info); - } + var reader = new Reader (stream, meta.UnpackedSize, meta.Mode, meta.Flag); + reader.Unpack(); + using (var bmp = new BinMemoryStream (reader.Data, stream.Name)) + return base.Read (bmp, info); } internal class Reader { - BinaryReader m_input; + IBinaryStream m_input; int m_mode; int m_flag; byte[] m_output; public byte[] Data { get { return m_output; } } - public Reader (BinaryReader input, int output_size, int mode, int flag) + public Reader (IBinaryStream input, int output_size, int mode, int flag) { m_input = input; m_mode = mode; @@ -153,12 +145,12 @@ namespace GameRes.Formats.Unknown v8 >>= 1; if (0 == v8) { - v12 = m_input.ReadByte(); + v12 = m_input.ReadUInt8(); v8 = 0x80; } if (0 != (v8 & v12)) { - m_output[dst++] = m_input.ReadByte(); + m_output[dst++] = m_input.ReadUInt8(); } else { diff --git a/ArcFormats/ImageISG.cs b/ArcFormats/ImageISG.cs index c7371923..1fef6d27 100644 --- a/ArcFormats/ImageISG.cs +++ b/ArcFormats/ImageISG.cs @@ -55,33 +55,29 @@ namespace GameRes.Formats.ISM throw new NotImplementedException ("IsgFormat.Write not implemented"); } - public override ImageMetaData ReadMetaData (Stream stream) + public override ImageMetaData ReadMetaData (IBinaryStream stream) { - var header = new byte[0x24]; - if (header.Length != stream.Read (header, 0, header.Length)) - return null; - if (!Binary.AsciiEqual (header, "ISM IMAGEFILE\x00")) + var header = stream.ReadHeader (0x24); + if (!header.AsciiEqual ("ISM IMAGEFILE\x00")) return null; int colors = header[0x23]; if (0 == colors) colors = 256; return new IsgMetaData { - Width = LittleEndian.ToUInt16 (header, 0x1d), - Height = LittleEndian.ToUInt16 (header, 0x1f), + Width = header.ToUInt16 (0x1d), + Height = header.ToUInt16 (0x1f), BPP = 8, Type = header[0x10], Colors = colors, - Packed = LittleEndian.ToUInt32 (header, 0x11), - Unpacked = LittleEndian.ToUInt32 (header, 0x15), + Packed = header.ToUInt32 (0x11), + Unpacked = header.ToUInt32 (0x15), }; } - public override ImageData Read (Stream stream, ImageMetaData info) + public override ImageData Read (IBinaryStream stream, ImageMetaData info) { - var meta = info as IsgMetaData; - if (null == meta) - throw new ArgumentException ("IsgFormat.Read should be supplied with IsgMetaData", "info"); + var meta = (IsgMetaData)info; if (0x21 != meta.Type && 0x10 != meta.Type) throw new InvalidFormatException ("Unsupported ISM image type"); @@ -99,7 +95,7 @@ namespace GameRes.Formats.ISM internal class Reader : IDisposable { - BinaryReader m_input; + IBinaryStream m_input; byte[] m_data; Color[] m_palette; int m_input_size; @@ -107,7 +103,7 @@ namespace GameRes.Formats.ISM public Color[] Palette { get { return m_palette; } } public byte[] Data { get { return m_data; } } - public Reader (Stream file, IsgMetaData info) + public Reader (IBinaryStream file, IsgMetaData info) { int palette_size = (int)info.Colors*4; var palette_data = new byte[Math.Max (0x400, palette_size)]; @@ -118,7 +114,7 @@ namespace GameRes.Formats.ISM { m_palette[i] = Color.FromRgb (palette_data[i*4+2], palette_data[i*4+1], palette_data[i*4]); } - m_input = new BinaryReader (file, Encoding.ASCII, true); + m_input = file; m_input_size = (int)info.Packed; m_data = new byte[info.Width * info.Height]; } @@ -129,15 +125,15 @@ namespace GameRes.Formats.ISM var frame = new byte[2048]; int frame_pos = 2039; int remaining = m_input_size; - byte ctl = m_input.ReadByte(); + byte ctl = m_input.ReadUInt8(); --remaining; int bit = 0x80; while (remaining > 0) { if (0 != (ctl & bit)) { - byte hi = m_input.ReadByte(); - byte lo = m_input.ReadByte(); + byte hi = m_input.ReadUInt8(); + byte lo = m_input.ReadUInt8(); remaining -= 2; int offset = (hi & 7) << 8 | lo; for (int count = (hi >> 3) + 3; count > 0; --count) @@ -151,7 +147,7 @@ namespace GameRes.Formats.ISM } else { - byte p = m_input.ReadByte(); + byte p = m_input.ReadUInt8(); --remaining; m_data[dst++] = p; frame[frame_pos] = p; @@ -159,7 +155,7 @@ namespace GameRes.Formats.ISM } if (0 == (bit >>= 1)) { - ctl = m_input.ReadByte(); + ctl = m_input.ReadUInt8(); --remaining; bit = 0x80; } @@ -170,16 +166,16 @@ namespace GameRes.Formats.ISM { int dst = 0; int remaining = m_input_size; - byte ctl = m_input.ReadByte(); + byte ctl = m_input.ReadUInt8(); --remaining; int bit = 1; while (remaining > 0) { - byte p = m_input.ReadByte(); + byte p = m_input.ReadUInt8(); --remaining; if (0 != (ctl & bit)) { - for (int count = 2 + m_input.ReadByte(); count > 0; --count) + for (int count = 2 + m_input.ReadUInt8(); count > 0; --count) m_data[dst++] = p; --remaining; } @@ -189,7 +185,7 @@ namespace GameRes.Formats.ISM } if (0x100 == (bit <<= 1)) { - ctl = m_input.ReadByte(); + ctl = m_input.ReadUInt8(); --remaining; bit = 1; } @@ -199,11 +195,6 @@ namespace GameRes.Formats.ISM #region IDisposable Members public void Dispose () { - if (null != m_input) - { - m_input.Dispose(); - m_input = null; - } GC.SuppressFinalize (this); } #endregion diff --git a/ArcFormats/ImageLZ.cs b/ArcFormats/ImageLZ.cs index a76b12ff..d42d8396 100644 --- a/ArcFormats/ImageLZ.cs +++ b/ArcFormats/ImageLZ.cs @@ -38,27 +38,29 @@ namespace GameRes.Formats public override uint Signature { get { return 0x44445A53u; } } // 'SZDD' public override bool CanWrite { get { return false; } } - public override ImageMetaData ReadMetaData (Stream stream) + public override ImageMetaData ReadMetaData (IBinaryStream stream) { stream.Position = 0x0e; - using (var lz = new LzssStream (stream, LzssMode.Decompress, true)) + using (var lz = new LzssStream (stream.AsStream, LzssMode.Decompress, true)) { lz.Config.FrameSize = 0x1000; lz.Config.FrameFill = 0x20; lz.Config.FrameInitPos = 0x1000 - 0x10; - return base.ReadMetaData (lz); + using (var bmp = new BinaryStream (lz, stream.Name)) + return base.ReadMetaData (bmp); } } - public override ImageData Read (Stream stream, ImageMetaData info) + public override ImageData Read (IBinaryStream stream, ImageMetaData info) { stream.Position = 0x0e; - using (var lz = new LzssStream (stream, LzssMode.Decompress, true)) + using (var lz = new LzssStream (stream.AsStream, LzssMode.Decompress, true)) { lz.Config.FrameSize = 0x1000; lz.Config.FrameFill = 0x20; lz.Config.FrameInitPos = 0x1000 - 0x10; - return base.Read (lz, info); + using (var bmp = new BinaryStream (lz, stream.Name)) + return base.Read (bmp, info); } } diff --git a/ArcFormats/ImageMB.cs b/ArcFormats/ImageMB.cs index d1afc649..6f5871ae 100644 --- a/ArcFormats/ImageMB.cs +++ b/ArcFormats/ImageMB.cs @@ -56,7 +56,7 @@ namespace GameRes.Formats { var header = new byte[2] { (byte)'B', (byte)'M' }; Stream stream = new StreamRegion (input.AsStream, 2, true); - stream = new PrefixStream (header, input); + stream = new PrefixStream (header, stream); return new BinaryStream (stream, input.Name); } diff --git a/ArcFormats/ImagePSD.cs b/ArcFormats/ImagePSD.cs index c1399cb2..9d58d0d8 100644 --- a/ArcFormats/ImagePSD.cs +++ b/ArcFormats/ImagePSD.cs @@ -46,11 +46,9 @@ namespace GameRes.Formats.Adobe public override string Description { get { return "Adobe Photoshop image format"; } } public override uint Signature { get { return 0x53504238; } } // '8BPS' - public override ImageMetaData ReadMetaData (Stream stream) + public override ImageMetaData ReadMetaData (IBinaryStream stream) { - var header = new byte[26]; - if (header.Length != stream.Read (header, 0, header.Length)) - return null; + var header = stream.ReadHeader (26).ToArray(); int version = BigEndian.ToInt16 (header, 4); if (1 != version) return null; @@ -72,7 +70,7 @@ namespace GameRes.Formats.Adobe }; } - public override ImageData Read (Stream stream, ImageMetaData info) + public override ImageData Read (IBinaryStream stream, ImageMetaData info) { var meta = (PsdMetaData)info; using (var reader = new PsdReader (stream, meta)) @@ -90,7 +88,7 @@ namespace GameRes.Formats.Adobe internal class PsdReader : IDisposable { - BinaryReader m_input; + IBinaryStream m_input; PsdMetaData m_info; byte[] m_output; int m_channel_size; @@ -99,10 +97,10 @@ namespace GameRes.Formats.Adobe public BitmapPalette Palette { get; private set; } public byte[] Data { get { return m_output; } } - public PsdReader (Stream input, PsdMetaData info) + public PsdReader (IBinaryStream input, PsdMetaData info) { m_info = info; - m_input = new BinaryReader (input, Encoding.Unicode, true); + m_input = input; int bpc = m_info.BPP / m_info.Channels; switch (m_info.Mode) { @@ -134,22 +132,22 @@ namespace GameRes.Formats.Adobe public void Unpack () { - m_input.BaseStream.Position = 0x1A; + m_input.Position = 0x1A; int color_data_length = Binary.BigEndian (m_input.ReadInt32()); - long next_pos = m_input.BaseStream.Position + color_data_length; + long next_pos = m_input.Position + color_data_length; if (0 != color_data_length) { if (8 == m_info.BPP) ReadPalette (color_data_length); - m_input.BaseStream.Position = next_pos; + m_input.Position = next_pos; } next_pos += 4 + Binary.BigEndian (m_input.ReadInt32()); - m_input.BaseStream.Position = next_pos; // skip Image Resources + m_input.Position = next_pos; // skip Image Resources next_pos += 4 + Binary.BigEndian (m_input.ReadInt32()); - m_input.BaseStream.Position = next_pos; // skip Layer and Mask Information + m_input.Position = next_pos; // skip Layer and Mask Information int compression = Binary.BigEndian (m_input.ReadInt16()); - int remaining = checked((int)(m_input.BaseStream.Length - m_input.BaseStream.Position)); + int remaining = checked((int)(m_input.Length - m_input.Position)); byte[] pixels; if (0 == compression) pixels = m_input.ReadBytes (remaining); @@ -212,7 +210,7 @@ namespace GameRes.Formats.Adobe int n = 0; while (n < line_count) { - int count = m_input.ReadSByte(); + int count = m_input.ReadInt8(); ++n; if (count >= 0) { @@ -224,7 +222,7 @@ namespace GameRes.Formats.Adobe else if (count > -128) { count = 1 - count; - byte color = m_input.ReadByte(); + byte color = m_input.ReadUInt8(); ++n; for (int i = 0; i < count; ++i) pixels[dst++] = color; @@ -236,14 +234,8 @@ namespace GameRes.Formats.Adobe } #region IDisposable Members - bool _disposed = false; public void Dispose () { - if (!_disposed) - { - m_input.Dispose(); - _disposed = true; - } } #endregion } diff --git a/ArcFormats/ImagePTI.cs b/ArcFormats/ImagePTI.cs index 632c7d03..10bf23d8 100644 --- a/ArcFormats/ImagePTI.cs +++ b/ArcFormats/ImagePTI.cs @@ -37,16 +37,16 @@ namespace GameRes.Formats.Misc public override uint Signature { get { return 0; } } public override bool CanWrite { get { return false; } } - public override ImageMetaData ReadMetaData (Stream stream) + public override ImageMetaData ReadMetaData (IBinaryStream stream) { var header = ReadHeader (stream); if (null == header) return null; - using (var bmp = new MemoryStream (header)) + using (var bmp = new BinMemoryStream (header, stream.Name)) return base.ReadMetaData (bmp); } - byte[] ReadHeader (Stream stream) + byte[] ReadHeader (IBinaryStream stream) { var header = new byte[0x36]; if (0x10 != stream.Read (header, 0, 0x10) @@ -58,7 +58,7 @@ namespace GameRes.Formats.Misc return header; } - public override ImageData Read (Stream stream, ImageMetaData info) + public override ImageData Read (IBinaryStream stream, ImageMetaData info) { uint length = (uint)(stream.Length - 0x38); var image = new byte[length+0x38]; @@ -71,7 +71,7 @@ namespace GameRes.Formats.Misc length += 2; } LittleEndian.Pack (length+0x36, image, 2); - using (var bmp = new MemoryStream (image)) + using (var bmp = new BinMemoryStream (image, stream.Name)) return base.Read (bmp, info); } diff --git a/ArcFormats/ImageSeraph.cs b/ArcFormats/ImageSeraph.cs index 78ca5d18..59454e35 100644 --- a/ArcFormats/ImageSeraph.cs +++ b/ArcFormats/ImageSeraph.cs @@ -45,22 +45,20 @@ namespace GameRes.Formats.Seraphim public override string Description { get { return "Seraphim engine image format"; } } public override uint Signature { get { return 0x4643; } } - public override ImageMetaData ReadMetaData (Stream stream) + public override ImageMetaData ReadMetaData (IBinaryStream stream) { - var header = new byte[0x10]; - if (0x10 != stream.Read (header, 0, 0x10)) - return null; - int packed_size = LittleEndian.ToInt32 (header, 12); + var header = stream.ReadHeader (0x10); + int packed_size = header.ToInt32 (12); if (packed_size <= 0 || packed_size > stream.Length-0x10) return null; - uint width = LittleEndian.ToUInt16 (header, 8); - uint height = LittleEndian.ToUInt16 (header, 10); + uint width = header.ToUInt16 (8); + uint height = header.ToUInt16 (10); if (0 == width || 0 == height) return null; return new SeraphMetaData { - OffsetX = LittleEndian.ToInt16 (header, 4), - OffsetY = LittleEndian.ToInt16 (header, 6), + OffsetX = header.ToInt16 (4), + OffsetY = header.ToInt16 (6), Width = width, Height = height, BPP = 24, @@ -68,13 +66,10 @@ namespace GameRes.Formats.Seraphim }; } - public override ImageData Read (Stream stream, ImageMetaData info) + public override ImageData Read (IBinaryStream stream, ImageMetaData info) { - var meta = info as SeraphMetaData; - if (null == meta) - throw new ArgumentException ("SeraphCfImage.Read should be supplied with SeraphMetaData", "info"); - - var reader = new SeraphReader (stream, meta); + var meta = (SeraphMetaData)info; + var reader = new SeraphReader (stream.AsStream, meta); reader.UnpackCf(); return ImageData.Create (info, PixelFormats.Bgr24, null, reader.Data); } @@ -91,20 +86,17 @@ namespace GameRes.Formats.Seraphim public override string Tag { get { return "CT"; } } public override uint Signature { get { return 0x5443; } } - public override ImageMetaData ReadMetaData (Stream stream) + public override ImageMetaData ReadMetaData (IBinaryStream stream) { var info = base.ReadMetaData (stream); info.BPP = 32; return info; } - public override ImageData Read (Stream stream, ImageMetaData info) + public override ImageData Read (IBinaryStream stream, ImageMetaData info) { - var meta = info as SeraphMetaData; - if (null == meta) - throw new ArgumentException ("SeraphCtImage.Read should be supplied with SeraphMetaData", "info"); - - var reader = new SeraphReader (stream, meta); + var meta = (SeraphMetaData)info; + var reader = new SeraphReader (stream.AsStream, meta); reader.UnpackCt(); return ImageData.Create (info, reader.Format, null, reader.Data); } @@ -122,25 +114,23 @@ namespace GameRes.Formats.Seraphim public override string Description { get { return "Seraphim engine image format"; } } public override uint Signature { get { return 0; } } - public override ImageMetaData ReadMetaData (Stream stream) + public override ImageMetaData ReadMetaData (IBinaryStream stream) { - if ('C' != stream.ReadByte() || 'B' != stream.ReadByte()) + var header = stream.ReadHeader (0x10); + if ('C' != header[0] || 'B' != header[1]) return null; - var header = new byte[0x10]; - if (0xE != stream.Read (header, 2, 0xE)) - return null; - int colors = LittleEndian.ToUInt16 (header, 2); - int packed_size = LittleEndian.ToInt32 (header, 12); + int colors = header.ToUInt16 (2); + int packed_size = header.ToInt32 (12); if (packed_size <= 0 || packed_size > stream.Length-0x10) return null; - uint width = LittleEndian.ToUInt16 (header, 8); - uint height = LittleEndian.ToUInt16 (header, 10); + uint width = header.ToUInt16 (8); + uint height = header.ToUInt16 (10); if (0 == width || 0 == height) return null; return new SeraphMetaData { - OffsetX = LittleEndian.ToInt16 (header, 4), - OffsetY = LittleEndian.ToInt16 (header, 6), + OffsetX = header.ToInt16 (4), + OffsetY = header.ToInt16 (6), Width = width, Height = height, BPP = 8, @@ -149,13 +139,10 @@ namespace GameRes.Formats.Seraphim }; } - public override ImageData Read (Stream stream, ImageMetaData info) + public override ImageData Read (IBinaryStream stream, ImageMetaData info) { - var meta = info as SeraphMetaData; - if (null == meta) - throw new ArgumentException ("SeraphCbImage.Read should be supplied with SeraphMetaData", "info"); - - var reader = new SeraphReader (stream, meta, 1); + var meta = (SeraphMetaData)info; + var reader = new SeraphReader (stream.AsStream, meta, 1); reader.UnpackCb(); return ImageData.Create (info, reader.Format, reader.Palette, reader.Data); } diff --git a/ArcFormats/Interheart/ArcFPK.cs b/ArcFormats/Interheart/ArcFPK.cs index 650e1df4..c8e8c905 100644 --- a/ArcFormats/Interheart/ArcFPK.cs +++ b/ArcFormats/Interheart/ArcFPK.cs @@ -141,7 +141,7 @@ namespace GameRes.Formats.CandySoft using (var reader = new Zlc2Reader (input, (int)entry.Size)) { reader.Unpack(); - return new MemoryStream (reader.Data); + return new BinMemoryStream (reader.Data, entry.Name); } } } diff --git a/ArcFormats/Interheart/ImageKG.cs b/ArcFormats/Interheart/ImageKG.cs index 49000084..43677246 100644 --- a/ArcFormats/Interheart/ImageKG.cs +++ b/ArcFormats/Interheart/ImageKG.cs @@ -38,14 +38,12 @@ namespace GameRes.Formats.Interheart public override string Description { get { return "Interheart image format"; } } public override uint Signature { get { return 0x4B474347; } } // 'GCGK' - public override ImageMetaData ReadMetaData (Stream stream) + public override ImageMetaData ReadMetaData (IBinaryStream stream) { - byte[] header = new byte[12]; - if (12 != stream.Read (header, 0, 12)) - return null; - uint width = LittleEndian.ToUInt16 (header, 4); - uint height = LittleEndian.ToUInt16 (header, 6); - int packed_size = LittleEndian.ToInt32 (header, 8); + stream.Position = 4; + uint width = stream.ReadUInt16(); + uint height = stream.ReadUInt16(); + int packed_size = stream.ReadInt32(); if (packed_size <= 0) return null; return new ImageMetaData @@ -56,47 +54,44 @@ namespace GameRes.Formats.Interheart }; } - public override ImageData Read (Stream stream, ImageMetaData info) + public override ImageData Read (IBinaryStream input, ImageMetaData info) { byte[] pixels = new byte[info.Width*info.Height*4]; - using (var input = new ArcView.Reader (stream)) - { - input.BaseStream.Position = 12; - uint[] offset_table = new uint[info.Height]; - for (uint i = 0; i < info.Height; ++i) - offset_table[i] = input.ReadUInt32(); + input.Position = 12; + uint[] offset_table = new uint[info.Height]; + for (uint i = 0; i < info.Height; ++i) + offset_table[i] = input.ReadUInt32(); - long base_offset = input.BaseStream.Position; - int dst = 0; - foreach (var offset in offset_table) + long base_offset = input.Position; + int dst = 0; + foreach (var offset in offset_table) + { + input.Position = base_offset + offset; + for (int x = 0; x < info.Width; ) { - input.BaseStream.Position = base_offset + offset; - for (int x = 0; x < info.Width; ) + byte alpha = input.ReadUInt8(); + int count = input.ReadUInt8(); + if (0 == count) + count = 0x100; + if (0 == alpha) { - byte alpha = input.ReadByte(); - int count = input.ReadByte(); - if (0 == count) - count = 0x100; - if (0 == alpha) - { - dst += count * 4; - } - else - { - for (int n = 0; n < count; ++n) - { - pixels[dst+3] = alpha; - pixels[dst+2] = input.ReadByte(); - pixels[dst+1] = input.ReadByte(); - pixels[dst] = input.ReadByte(); - dst += 4; - } - } - x += count; + dst += count * 4; } + else + { + for (int n = 0; n < count; ++n) + { + pixels[dst+3] = alpha; + pixels[dst+2] = input.ReadUInt8(); + pixels[dst+1] = input.ReadUInt8(); + pixels[dst] = input.ReadUInt8(); + dst += 4; + } + } + x += count; } - return ImageData.Create (info, PixelFormats.Bgra32, null, pixels); } + return ImageData.Create (info, PixelFormats.Bgra32, null, pixels); } public override void Write (Stream file, ImageData image) diff --git a/ArcFormats/Ipac/AudioWST.cs b/ArcFormats/Ipac/AudioWST.cs index 6c52a55d..4dfeb5df 100644 --- a/ArcFormats/Ipac/AudioWST.cs +++ b/ArcFormats/Ipac/AudioWST.cs @@ -68,7 +68,7 @@ namespace GameRes.Formats.BaseUnit wav.Write (adpcm_data, 0, adpcm_data.Length); wav.Write (0x61746164); // 'data' wav.Write (data_length); - file.CopyTo (wav_file); + file.AsStream.CopyTo (wav_file); } wav_file.Position = 0; var sound = new WaveInput (wav_file); diff --git a/ArcFormats/Ivory/AudioCTRK.cs b/ArcFormats/Ivory/AudioCTRK.cs index fb8a2acd..b9c2d71e 100644 --- a/ArcFormats/Ivory/AudioCTRK.cs +++ b/ArcFormats/Ivory/AudioCTRK.cs @@ -90,7 +90,7 @@ namespace GameRes.Formats.Ivory } else if (3 == type) { - var input = new StreamRegion (file, start_offset, data_length); + var input = new StreamRegion (file.AsStream, start_offset, data_length); return new OggInput (input); } else diff --git a/ArcFormats/Ivory/ImageSG.cs b/ArcFormats/Ivory/ImageSG.cs index a24a1d5b..59003ef6 100644 --- a/ArcFormats/Ivory/ImageSG.cs +++ b/ArcFormats/Ivory/ImageSG.cs @@ -144,7 +144,7 @@ namespace GameRes.Formats.Ivory public byte[] Data { get { return m_output; } } public int Stride { get { return m_stride; } } - public SgRgbReader (Stream input, SgMetaData info) + public SgRgbReader (IBinaryStream input, SgMetaData info) { if (info.Type != SgType.cRGB || !(0x18 == info.BPP || 0x20 == info.BPP)) throw new InvalidFormatException(); @@ -189,16 +189,16 @@ namespace GameRes.Formats.Ivory { for (int x = 0; x < m_width; ) { - byte ctl = m_input.ReadByte(); + byte ctl = m_input.ReadUInt8(); int count = ctl & 0x3F; if (0 != (ctl & 0x40)) { count <<= 8; - count |= m_input.ReadByte(); + count |= m_input.ReadUInt8(); } if (0 != (ctl & 0x80)) { - byte v = m_input.ReadByte(); + byte v = m_input.ReadUInt8(); for (int i = 0; i < count; ++i) line[line_pos++] = v; } @@ -250,17 +250,17 @@ namespace GameRes.Formats.Ivory m_input.Position = data_pos + index[y]; for (int x = 0; x < m_width; ) { - int ctl = m_input.ReadByte(); + int ctl = m_input.ReadUInt8(); int count = ctl >> 2; if (0 != (ctl & 2)) { - count |= m_input.ReadByte() << 6; + count |= m_input.ReadUInt8() << 6; } count = Math.Min (count, m_width - x); x += count; if (0 != (ctl & 1)) { - byte c = m_input.ReadByte(); + byte c = m_input.ReadUInt8(); for (int i = 0; i < count; ++i) m_output[dst++] = c; } diff --git a/ArcFormats/Kaas/ImageKAAS.cs b/ArcFormats/Kaas/ImageKAAS.cs index d1a50553..b86a0a0f 100644 --- a/ArcFormats/Kaas/ImageKAAS.cs +++ b/ArcFormats/Kaas/ImageKAAS.cs @@ -55,7 +55,7 @@ namespace GameRes.Formats.KAAS throw new NotImplementedException ("PicFormat.Write not implemented"); } - public override ImageMetaData ReadMetaData (Stream stream) + public override ImageMetaData ReadMetaData (IBinaryStream stream) { int mode = stream.ReadByte(); switch (mode) @@ -66,8 +66,8 @@ namespace GameRes.Formats.KAAS return null; } int key = stream.ReadByte(); - var header = new byte[0x10]; - if (header.Length != stream.Read (header, 0, header.Length)) + var header = stream.ReadBytes (0x10); + if (header.Length != 0x10) return null; uint width = LittleEndian.ToUInt16 (header, 0); uint height = LittleEndian.ToUInt16 (header, 2); @@ -92,17 +92,13 @@ namespace GameRes.Formats.KAAS }; } - public override ImageData Read (Stream stream, ImageMetaData info) + public override ImageData Read (IBinaryStream stream, ImageMetaData info) { - var meta = info as PicMetaData; - if (null == meta) - throw new ArgumentException ("PicFormat.Read should be supplied with PicMetaData", "info"); - stream.Position = 0x12; - using (var reader = new Reader (stream, meta)) + using (var reader = new Reader (stream.AsStream, (PicMetaData)info)) { reader.Unpack(); - return ImageData.Create (meta, PixelFormats.Bgr24, null, reader.Data, (int)meta.Width*3); + return ImageData.Create (info, PixelFormats.Bgr24, null, reader.Data, (int)info.Width*3); } } diff --git a/ArcFormats/Kaguya/ArcKaguya.cs b/ArcFormats/Kaguya/ArcKaguya.cs index ac7c2b5c..1d3c6b00 100644 --- a/ArcFormats/Kaguya/ArcKaguya.cs +++ b/ArcFormats/Kaguya/ArcKaguya.cs @@ -70,7 +70,7 @@ namespace GameRes.Formats.Kaguya using (var reader = new LzReader (input, entry.Size, packed_entry.UnpackedSize)) { reader.Unpack(); - return new MemoryStream (reader.Data); + return new BinMemoryStream (reader.Data, entry.Name); } } } diff --git a/ArcFormats/Kaguya/ArcLINK.cs b/ArcFormats/Kaguya/ArcLINK.cs index ce90af54..37652bbb 100644 --- a/ArcFormats/Kaguya/ArcLINK.cs +++ b/ArcFormats/Kaguya/ArcLINK.cs @@ -82,7 +82,7 @@ namespace GameRes.Formats.Kaguya using (var bmr = new BmrDecoder (input)) { bmr.Unpack(); - return new MemoryStream (bmr.Data); + return new BinMemoryStream (bmr.Data, entry.Name); } } } diff --git a/ArcFormats/Kaguya/ImageAO.cs b/ArcFormats/Kaguya/ImageAO.cs index 82ab342b..f1c30c03 100644 --- a/ArcFormats/Kaguya/ImageAO.cs +++ b/ArcFormats/Kaguya/ImageAO.cs @@ -45,27 +45,24 @@ namespace GameRes.Formats.Kaguya Extensions = new string[] { "sp_" }; } - public override ImageMetaData ReadMetaData (Stream stream) + public override ImageMetaData ReadMetaData (IBinaryStream stream) { int A = stream.ReadByte(); int O = stream.ReadByte(); if ('A' != A || 'O' != O) return null; - using (var file = new ArcView.Reader (stream)) - { - var info = new ImageMetaData(); - info.Width = file.ReadUInt32(); - info.Height = file.ReadUInt32(); - info.BPP = file.ReadInt16(); - info.OffsetX = file.ReadInt32(); - info.OffsetY = file.ReadInt32(); - if (info.Width > 0x8000 || info.Height > 0x8000 || !(32 == info.BPP || 24 == info.BPP)) - return null; - return info; - } + var info = new ImageMetaData(); + info.Width = stream.ReadUInt32(); + info.Height = stream.ReadUInt32(); + info.BPP = stream.ReadInt16(); + info.OffsetX = stream.ReadInt32(); + info.OffsetY = stream.ReadInt32(); + if (info.Width > 0x8000 || info.Height > 0x8000 || !(32 == info.BPP || 24 == info.BPP)) + return null; + return info; } - public override ImageData Read (Stream stream, ImageMetaData info) + public override ImageData Read (IBinaryStream stream, ImageMetaData info) { stream.Position = 0x14; return ReadBitmapData (stream, info); diff --git a/ArcFormats/Kaguya/ImageAP.cs b/ArcFormats/Kaguya/ImageAP.cs index 975d27c6..4a2866c4 100644 --- a/ArcFormats/Kaguya/ImageAP.cs +++ b/ArcFormats/Kaguya/ImageAP.cs @@ -45,31 +45,28 @@ namespace GameRes.Formats.Kaguya Extensions = new string[] { "bg_", "cg_", "cgw", "sp_", "aps", "alp", "prs" }; } - public override ImageMetaData ReadMetaData (Stream stream) + public override ImageMetaData ReadMetaData (IBinaryStream stream) { int A = stream.ReadByte(); int P = stream.ReadByte(); if ('A' != A || 'P' != P) return null; - using (var file = new ArcView.Reader (stream)) - { - var info = new ImageMetaData(); - info.Width = file.ReadUInt32(); - info.Height = file.ReadUInt32(); - info.BPP = file.ReadInt16(); - if (info.Width > 0x8000 || info.Height > 0x8000 || !(32 == info.BPP || 24 == info.BPP)) - return null; - return info; - } + var info = new ImageMetaData(); + info.Width = stream.ReadUInt32(); + info.Height = stream.ReadUInt32(); + info.BPP = stream.ReadInt16(); + if (info.Width > 0x8000 || info.Height > 0x8000 || !(32 == info.BPP || 24 == info.BPP)) + return null; + return info; } - public override ImageData Read (Stream stream, ImageMetaData info) + public override ImageData Read (IBinaryStream stream, ImageMetaData info) { stream.Position = 12; return ReadBitmapData (stream, info); } - protected ImageData ReadBitmapData (Stream stream, ImageMetaData info) + protected ImageData ReadBitmapData (IBinaryStream stream, ImageMetaData info) { int stride = (int)info.Width*4; var pixels = new byte[stride*info.Height]; @@ -126,24 +123,21 @@ namespace GameRes.Formats.Kaguya Extensions = new string[] { "alp" }; } - public override ImageMetaData ReadMetaData (Stream stream) + public override ImageMetaData ReadMetaData (IBinaryStream stream) { stream.Position = 4; - using (var file = new ArcView.Reader (stream)) - { - var info = new ImageMetaData(); - info.OffsetX = file.ReadInt32(); - info.OffsetY = file.ReadInt32(); - info.Width = file.ReadUInt32(); - info.Height = file.ReadUInt32(); - info.BPP = 32; - if (info.Width > 0x8000 || info.Height > 0x8000) - return null; - return info; - } + var info = new ImageMetaData(); + info.OffsetX = stream.ReadInt32(); + info.OffsetY = stream.ReadInt32(); + info.Width = stream.ReadUInt32(); + info.Height = stream.ReadUInt32(); + info.BPP = 32; + if (info.Width > 0x8000 || info.Height > 0x8000) + return null; + return info; } - public override ImageData Read (Stream stream, ImageMetaData info) + public override ImageData Read (IBinaryStream stream, ImageMetaData info) { stream.Position = 0x18; var pixels = new byte[4*info.Width*info.Height]; diff --git a/ArcFormats/Kaguya/ImageAPS.cs b/ArcFormats/Kaguya/ImageAPS.cs index 0835767a..23b0434c 100644 --- a/ArcFormats/Kaguya/ImageAPS.cs +++ b/ArcFormats/Kaguya/ImageAPS.cs @@ -49,39 +49,36 @@ namespace GameRes.Formats.Kaguya Extensions = new string[] { "aps", "parts" }; } - public override ImageMetaData ReadMetaData (Stream stream) + public override ImageMetaData ReadMetaData (IBinaryStream stream) { stream.Position = 4; if (stream.ReadByte() != '3') return null; var rect = new Rectangle (0, 0, 0, 0); - using (var reader = new ArcView.Reader (stream)) + int count = stream.ReadInt32(); + for (int i = 0; i < count; ++i) { - int count = reader.ReadInt32(); - for (int i = 0; i < count; ++i) + stream.ReadInt32(); + int name_length = stream.ReadByte(); + stream.Seek (name_length, SeekOrigin.Current); + int x = stream.ReadInt32(); + int y = stream.ReadInt32(); + int w = stream.ReadInt32() - x; + int h = stream.ReadInt32() - y; + if (name_length > 0) { - reader.ReadInt32(); - int name_length = reader.ReadByte(); - reader.BaseStream.Seek (name_length, SeekOrigin.Current); - int x = reader.ReadInt32(); - int y = reader.ReadInt32(); - int w = reader.ReadInt32() - x; - int h = reader.ReadInt32() - y; - if (name_length > 0) - { - var part_rect = new Rectangle (x, y, w, h); - rect = Rectangle.Union (rect, part_rect); - } - reader.BaseStream.Seek (12, SeekOrigin.Current); + var part_rect = new Rectangle (x, y, w, h); + rect = Rectangle.Union (rect, part_rect); } - uint data_size = reader.ReadUInt32(); - if (data_size > stream.Length-stream.Position) - return null; - return ReadCompressionMetaData (reader, rect); + stream.Seek (12, SeekOrigin.Current); } + uint data_size = stream.ReadUInt32(); + if (data_size > stream.Length-stream.Position) + return null; + return ReadCompressionMetaData (stream, rect); } - internal ApsMetaData ReadCompressionMetaData (BinaryReader reader, Rectangle rect) + internal ApsMetaData ReadCompressionMetaData (IBinaryStream reader, Rectangle rect) { int compression = reader.ReadInt16(); var info = new ApsMetaData @@ -102,18 +99,18 @@ namespace GameRes.Formats.Kaguya } else return null; - info.DataOffset = (uint)reader.BaseStream.Position; + info.DataOffset = (uint)reader.Position; return info; } - public override ImageData Read (Stream stream, ImageMetaData info) + public override ImageData Read (IBinaryStream stream, ImageMetaData info) { var meta = (ApsMetaData)info; stream.Position = meta.DataOffset; byte[] image_data; if (meta.IsPacked) { - using (var reader = new LzReader (stream, meta.PackedSize, meta.UnpackedSize)) + using (var reader = new LzReader (stream.AsStream, meta.PackedSize, meta.UnpackedSize)) { reader.Unpack(); image_data = reader.Data; @@ -121,10 +118,9 @@ namespace GameRes.Formats.Kaguya } else { - using (var reader = new ArcView.Reader (stream)) - image_data = reader.ReadBytes ((int)meta.UnpackedSize); + image_data = stream.ReadBytes ((int)meta.UnpackedSize); } - using (var unpacked = new MemoryStream (image_data)) + using (var unpacked = BinaryStream.FromArray (image_data, stream.Name)) { var ap_info = base.ReadMetaData (unpacked); if (null == ap_info) @@ -151,40 +147,37 @@ namespace GameRes.Formats.Kaguya Extensions = new string[] { "aps", "parts" }; } - public override ImageMetaData ReadMetaData (Stream stream) + public override ImageMetaData ReadMetaData (IBinaryStream stream) { - using (var reader = new ArcView.Reader (stream)) + int name_count = stream.ReadInt16(); + if (name_count <= 0 || name_count > 1000) + return null; + for (int i = 0; i < name_count; ++i) { - int name_count = reader.ReadInt16(); - if (name_count <= 0 || name_count > 1000) + int name_length = stream.ReadInt32(); + if (name_length <= 0 || name_length > 260) return null; - for (int i = 0; i < name_count; ++i) - { - int name_length = reader.ReadInt32(); - if (name_length <= 0 || name_length > 260) - return null; - stream.Seek (name_length, SeekOrigin.Current); - } - int tile_count = reader.ReadInt16(); - if (tile_count <= 0 || tile_count > 1000) - return null; - var rect = new Rectangle (0, 0, 0, 0); - for (int i = 0; i < tile_count; ++i) - { - int name_length = reader.ReadInt32(); - if (name_length <= 0 || name_length > 260) - return null; - stream.Seek (name_length+0xC, SeekOrigin.Current); - int x = reader.ReadInt32(); - int y = reader.ReadInt32(); - int w = reader.ReadInt32() - x; - int h = reader.ReadInt32() - y; - var part_rect = new Rectangle (x, y, w, h); - rect = Rectangle.Union (rect, part_rect); - stream.Seek (0x28, SeekOrigin.Current); - } - return ReadCompressionMetaData (reader, rect); + stream.Seek (name_length, SeekOrigin.Current); } + int tile_count = stream.ReadInt16(); + if (tile_count <= 0 || tile_count > 1000) + return null; + var rect = new Rectangle (0, 0, 0, 0); + for (int i = 0; i < tile_count; ++i) + { + int name_length = stream.ReadInt32(); + if (name_length <= 0 || name_length > 260) + return null; + stream.Seek (name_length+0xC, SeekOrigin.Current); + int x = stream.ReadInt32(); + int y = stream.ReadInt32(); + int w = stream.ReadInt32() - x; + int h = stream.ReadInt32() - y; + var part_rect = new Rectangle (x, y, w, h); + rect = Rectangle.Union (rect, part_rect); + stream.Seek (0x28, SeekOrigin.Current); + } + return ReadCompressionMetaData (stream, rect); } public override void Write (Stream file, ImageData image) diff --git a/ArcFormats/KiriKiri/ImageTLG.cs b/ArcFormats/KiriKiri/ImageTLG.cs index af6a06a0..4bcc343f 100644 --- a/ArcFormats/KiriKiri/ImageTLG.cs +++ b/ArcFormats/KiriKiri/ImageTLG.cs @@ -38,30 +38,28 @@ namespace GameRes.Formats.KiriKiri Signatures = new uint[] { 0x30474c54, 0x35474c54, 0x36474c54, 0x35474cAB }; } - public override ImageMetaData ReadMetaData (Stream stream) + public override ImageMetaData ReadMetaData (IBinaryStream stream) { - var header = new byte[0x26]; - if (header.Length != stream.Read (header, 0, header.Length)) - return null; + var header = stream.ReadHeader (0x26); int offset = 0xf; - if (!Binary.AsciiEqual (header, "TLG0.0\x00sds\x1a")) + if (!header.AsciiEqual ("TLG0.0\x00sds\x1a")) offset = 0; int version; - if (!Binary.AsciiEqual (header, offset+6, "\x00raw\x1a")) + if (!header.AsciiEqual (offset+6, "\x00raw\x1a")) return null; if (0xAB == header[offset]) header[offset] = (byte)'T'; - if (Binary.AsciiEqual (header, offset, "TLG6.0")) + if (header.AsciiEqual (offset, "TLG6.0")) version = 6; - else if (Binary.AsciiEqual (header, offset, "TLG5.0")) + else if (header.AsciiEqual (offset, "TLG5.0")) version = 5; - else if (Binary.AsciiEqual (header, offset, "XXXYYY")) + else if (header.AsciiEqual (offset, "XXXYYY")) { version = 5; header[offset+0x0C] ^= 0xAB; header[offset+0x10] ^= 0xAC; } - else if (Binary.AsciiEqual (header, offset, "XXXZZZ")) + else if (header.AsciiEqual (offset, "XXXZZZ")) { version = 6; header[offset+0x0F] ^= 0xAB; @@ -84,44 +82,40 @@ namespace GameRes.Formats.KiriKiri return null; offset += 12; } - uint width = LittleEndian.ToUInt32 (header, offset); - uint height = LittleEndian.ToUInt32 (header, offset+4); - return new TlgMetaData { - Width = width, - Height = height, + return new TlgMetaData + { + Width = header.ToUInt32 (offset), + Height = header.ToUInt32 (offset+4), BPP = colors*8, Version = version, DataOffset = offset+8, }; } - public override ImageData Read (Stream file, ImageMetaData info) + public override ImageData Read (IBinaryStream file, ImageMetaData info) { var meta = (TlgMetaData)info; file.Position = meta.DataOffset; - using (var src = new ArcView.Reader (file)) - { - var image = ReadTlg (src, meta); + var image = ReadTlg (file, meta); - int tail_size = (int)Math.Min (file.Length - file.Position, 512); - if (tail_size > 8) + int tail_size = (int)Math.Min (file.Length - file.Position, 512); + if (tail_size > 8) + { + var tail = file.ReadBytes (tail_size); + try { - var tail = src.ReadBytes (tail_size); - try - { - var blended_image = ApplyTags (image, meta, tail); - if (null != blended_image) - return blended_image; - } - catch (Exception X) - { - Trace.WriteLine (X.Message, "[TlgFormat.Read]"); - } + var blended_image = ApplyTags (image, meta, tail); + if (null != blended_image) + return blended_image; + } + catch (Exception X) + { + Trace.WriteLine (X.Message, "[TlgFormat.Read]"); } - PixelFormat format = 32 == meta.BPP ? PixelFormats.Bgra32 : PixelFormats.Bgr32; - return ImageData.Create (meta, format, null, image, (int)meta.Width * 4); } + PixelFormat format = 32 == meta.BPP ? PixelFormats.Bgra32 : PixelFormats.Bgr32; + return ImageData.Create (meta, format, null, image, (int)meta.Width * 4); } public override void Write (Stream file, ImageData image) @@ -129,7 +123,7 @@ namespace GameRes.Formats.KiriKiri throw new NotImplementedException ("TlgFormat.Write not implemented"); } - byte[] ReadTlg (BinaryReader src, TlgMetaData info) + byte[] ReadTlg (IBinaryStream src, TlgMetaData info) { if (6 == info.Version) return ReadV6 (src, info); @@ -163,15 +157,14 @@ namespace GameRes.Formats.KiriKiri TlgMetaData base_info; byte[] base_image; - using (var base_file = VFS.OpenSeekableStream (base_name)) - using (var base_src = new BinaryReader (base_file)) + using (var base_file = VFS.OpenBinaryStream (base_name)) { base_info = ReadMetaData (base_file) as TlgMetaData; if (null == base_info) return null; base_info.FileName = base_name; base_file.Position = base_info.DataOffset; - base_image = ReadTlg (base_src, base_info); + base_image = ReadTlg (base_file, base_info); } var pixels = BlendImage (base_image, base_info, image, meta); PixelFormat format = 32 == base_info.BPP ? PixelFormats.Bgra32 : PixelFormats.Bgr32; @@ -226,7 +219,7 @@ namespace GameRes.Formats.KiriKiri const int TVP_TLG6_LeadingZeroTable_BITS = 12; const int TVP_TLG6_LeadingZeroTable_SIZE = (1< 0xFF) return null; - frame_offset = LittleEndian.ToUInt32 (header, 0x42); + frame_offset = header.ToUInt32 (0x42); } else if (32 == type || 24 == type) { - uint unpacked_size = LittleEndian.ToUInt32 (header, 2); + uint unpacked_size = header.ToUInt32 (2); if (0 == unpacked_size || unpacked_size == stream.Length) // probably an ordinary bmp file return null; - frame_offset = LittleEndian.ToUInt32 (header, 0xA); + frame_offset = header.ToUInt32 (0xA); } else return null; @@ -67,15 +65,15 @@ namespace GameRes.Formats.Lilim return null; return new AbmImageData { - Width = LittleEndian.ToUInt32 (header, 0x12), - Height = LittleEndian.ToUInt32 (header, 0x16), + Width = header.ToUInt32 (0x12), + Height = header.ToUInt32 (0x16), BPP = 24, Mode = type, BaseOffset = frame_offset, }; } - public override ImageData Read (Stream stream, ImageMetaData info) + public override ImageData Read (IBinaryStream stream, ImageMetaData info) { using (var reader = new AbmReader (stream, (AbmImageData)info)) { diff --git a/ArcFormats/LiveMaker/ImageGAL.cs b/ArcFormats/LiveMaker/ImageGAL.cs index 2d8c2b5b..9bc66e16 100644 --- a/ArcFormats/LiveMaker/ImageGAL.cs +++ b/ArcFormats/LiveMaker/ImageGAL.cs @@ -75,7 +75,7 @@ namespace GameRes.Formats.LiveMaker set { KnownKeys = ((GalScheme)value).KnownKeys; } } - public override ImageMetaData ReadMetaData (Stream stream) + public override ImageMetaData ReadMetaData (IBinaryStream stream) { var header = new byte[0x30]; if (11 != stream.Read (header, 0, 11)) @@ -110,7 +110,7 @@ namespace GameRes.Formats.LiveMaker uint? LastKey = null; - public override ImageData Read (Stream stream, ImageMetaData info) + public override ImageData Read (IBinaryStream stream, ImageMetaData info) { var meta = (GalMetaData)info; uint key = 0; @@ -171,7 +171,7 @@ namespace GameRes.Formats.LiveMaker internal sealed class GalReader : IDisposable { - BinaryReader m_input; + IBinaryStream m_input; GalMetaData m_info; byte[] m_output; List m_frames; @@ -182,14 +182,14 @@ namespace GameRes.Formats.LiveMaker public BitmapPalette Palette { get; private set; } public int Stride { get; private set; } - public GalReader (Stream input, GalMetaData info, uint key) + public GalReader (IBinaryStream input, GalMetaData info, uint key) { m_info = info; if (m_info.Compression < 0 || m_info.Compression > 2) throw new InvalidFormatException(); m_frames = new List (m_info.FrameCount); m_key = key; - m_input = new ArcView.Reader (input); + m_input = input; } internal class Frame @@ -216,11 +216,11 @@ namespace GameRes.Formats.LiveMaker public void Unpack () { - m_input.BaseStream.Position = m_info.DataOffset; + m_input.Position = m_info.DataOffset; uint name_length = m_input.ReadUInt32(); - m_input.BaseStream.Seek (name_length, SeekOrigin.Current); + m_input.Seek (name_length, SeekOrigin.Current); uint mask = m_input.ReadUInt32(); - m_input.BaseStream.Seek (9, SeekOrigin.Current); + m_input.Seek (9, SeekOrigin.Current); int layer_count = m_input.ReadInt32(); if (layer_count < 1) throw new InvalidFormatException(); @@ -251,14 +251,14 @@ namespace GameRes.Formats.LiveMaker m_input.ReadInt32(); // 0xFF m_input.ReadByte(); name_length = m_input.ReadUInt32(); - m_input.BaseStream.Seek (name_length, SeekOrigin.Current); + m_input.Seek (name_length, SeekOrigin.Current); if (m_info.Version >= 107) m_input.ReadByte(); var layer = new Layer(); int layer_size = m_input.ReadInt32(); - long layer_end = m_input.BaseStream.Position + layer_size; + long layer_end = m_input.Position + layer_size; layer.Pixels = UnpackLayer (frame, layer_size); - m_input.BaseStream.Position = layer_end; + m_input.Position = layer_end; int alpha_size = m_input.ReadInt32(); if (alpha_size != 0) { @@ -271,7 +271,7 @@ namespace GameRes.Formats.LiveMaker byte[] UnpackLayer (Frame frame, int length, bool is_alpha = false) { - using (var packed = new StreamRegion (m_input.BaseStream, m_input.BaseStream.Position, length, true)) + using (var packed = new StreamRegion (m_input.AsStream, m_input.Position, length, true)) { if (0 == m_info.Compression || 2 == m_info.Compression && is_alpha) return ReadZlib (frame, packed, is_alpha); @@ -563,14 +563,8 @@ namespace GameRes.Formats.LiveMaker } #region IDisposable Members - bool _disposed = false; public void Dispose () { - if (!_disposed) - { - m_input.Dispose(); - _disposed = true; - } } #endregion } diff --git a/ArcFormats/Lucifen/ArcLPK.cs b/ArcFormats/Lucifen/ArcLPK.cs index 8c1d3932..2293156f 100644 --- a/ArcFormats/Lucifen/ArcLPK.cs +++ b/ArcFormats/Lucifen/ArcLPK.cs @@ -182,7 +182,7 @@ namespace GameRes.Formats.Lucifen if (count != 0) DecryptEntry (data, count, larc.Info.Key, larc.Scheme.RotatePattern); } - input = new MemoryStream (data); + input = new BinMemoryStream (data, entry.Name); if (null != larc.Info.Prefix) return new PrefixStream (larc.Info.Prefix, input); else diff --git a/ArcFormats/Lucifen/ImageELG.cs b/ArcFormats/Lucifen/ImageELG.cs index d2bcdbff..9cae8650 100644 --- a/ArcFormats/Lucifen/ImageELG.cs +++ b/ArcFormats/Lucifen/ImageELG.cs @@ -56,53 +56,50 @@ namespace GameRes.Formats.Lucifen throw new NotImplementedException ("ElgFormat.Write not implemented"); } - public override ImageMetaData ReadMetaData (Stream stream) + public override ImageMetaData ReadMetaData (IBinaryStream file) { - stream.Seek (3, SeekOrigin.Current); - using (var input = new ArcView.Reader (stream)) + file.Position = 3; + int bpp = file.ReadByte(); + int x = 0; + int y = 0; + int type = bpp; + int header_size = 8; + if (2 == type) { - int bpp = input.ReadByte(); - int x = 0; - int y = 0; - int type = bpp; - int header_size = 8; - if (2 == type) - { - bpp = input.ReadByte(); - header_size = 13; - } - else if (1 == type) - { - bpp = input.ReadByte(); - x = input.ReadInt16(); - y = input.ReadInt16(); - header_size = 13; - } - else - type = 0; - if (8 != bpp && 24 != bpp && 32 != bpp) - return null; - uint w = input.ReadUInt16(); - uint h = input.ReadUInt16(); - if (2 == type) - { - x = input.ReadInt16(); - y = input.ReadInt16(); - } - return new ElgMetaData - { - Width = w, - Height = h, - OffsetX = x, - OffsetY = y, - BPP = bpp, - Type = type, - HeaderSize = header_size, - }; + bpp = file.ReadByte(); + header_size = 13; } + else if (1 == type) + { + bpp = file.ReadByte(); + x = file.ReadInt16(); + y = file.ReadInt16(); + header_size = 13; + } + else + type = 0; + if (8 != bpp && 24 != bpp && 32 != bpp) + return null; + uint w = file.ReadUInt16(); + uint h = file.ReadUInt16(); + if (2 == type) + { + x = file.ReadInt16(); + y = file.ReadInt16(); + } + return new ElgMetaData + { + Width = w, + Height = h, + OffsetX = x, + OffsetY = y, + BPP = bpp, + Type = type, + HeaderSize = header_size, + }; } - public override ImageData Read (Stream file, ImageMetaData info) + public override ImageData Read (IBinaryStream file, ImageMetaData info) { var meta = (ElgMetaData)info; file.Position = meta.HeaderSize; @@ -119,33 +116,33 @@ namespace GameRes.Formats.Lucifen int m_height; int m_bpp; int m_type; - BinaryReader m_input; + IBinaryStream m_input; byte[] m_output; public PixelFormat Format { get; private set; } public BitmapPalette Palette { get; private set; } public byte[] Data { get { return m_output; } } - public Reader (Stream stream, ElgMetaData info) + public Reader (IBinaryStream stream, ElgMetaData info) { m_width = (int)info.Width; m_height = (int)info.Height; m_bpp = info.BPP; m_type = info.Type; m_output = new byte[m_width*m_height*m_bpp/8]; - m_input = new ArcView.Reader (stream); + m_input = stream; } public void Unpack () { if (2 == m_type) { - while (0 != m_input.ReadByte()) + while (0 != m_input.ReadUInt8()) { int size = m_input.ReadInt32(); if (size < 4) throw new InvalidFormatException(); - m_input.BaseStream.Seek (size-4, SeekOrigin.Current); + m_input.Seek (size-4, SeekOrigin.Current); } } if (8 == m_bpp) @@ -182,7 +179,7 @@ namespace GameRes.Formats.Lucifen int dst = 0; for (;;) { - byte flags = m_input.ReadByte(); + byte flags = m_input.ReadUInt8(); if (0xff == flags || dst >= output.Length) break; int count, pos; @@ -190,7 +187,7 @@ namespace GameRes.Formats.Lucifen if (0 == (flags & 0xc0)) { if (0 != (flags & 0x20)) - count = ((flags & 0x1f) << 8) + m_input.ReadByte() + 33; + count = ((flags & 0x1f) << 8) + m_input.ReadUInt8() + 33; else count = (flags & 0x1f) + 1; @@ -200,11 +197,11 @@ namespace GameRes.Formats.Lucifen else if ((flags & 0xc0) == 0x40) { if (0 != (flags & 0x20)) - count = ((flags & 0x1f) << 8) + m_input.ReadByte() + 35; + count = ((flags & 0x1f) << 8) + m_input.ReadUInt8() + 35; else count = (flags & 0x1f) + 3; - byte v = m_input.ReadByte(); + byte v = m_input.ReadUInt8(); for (int i = 0; i < count; ++i) output[dst++] = v; } @@ -215,21 +212,21 @@ namespace GameRes.Formats.Lucifen if (0 == (flags & 0x30)) { count = (flags & 0xf) + 2; - pos = m_input.ReadByte() + 2; + pos = m_input.ReadUInt8() + 2; } else if ((flags & 0x30) == 0x10) { - pos = ((flags & 0xf) << 8) + m_input.ReadByte() + 3; - count = m_input.ReadByte() + 4; + pos = ((flags & 0xf) << 8) + m_input.ReadUInt8() + 3; + count = m_input.ReadUInt8() + 4; } else if ((flags & 0x30) == 0x20) { - pos = ((flags & 0xf) << 8) + m_input.ReadByte() + 3; + pos = ((flags & 0xf) << 8) + m_input.ReadUInt8() + 3; count = 3; } else { - pos = ((flags & 0xf) << 8) + m_input.ReadByte() + 3; + pos = ((flags & 0xf) << 8) + m_input.ReadUInt8() + 3; count = 4; } } @@ -255,7 +252,7 @@ namespace GameRes.Formats.Lucifen int dst = 0; for (;;) { - byte flags = m_input.ReadByte(); + byte flags = m_input.ReadUInt8(); if (0xff == flags || dst >= m_output.Length) break; int count, pos, src; @@ -263,28 +260,28 @@ namespace GameRes.Formats.Lucifen if (0 == (flags & 0xc0)) { if (0 != (flags & 0x20)) - count = ((flags & 0x1f) << 8) + m_input.ReadByte() + 33; + count = ((flags & 0x1f) << 8) + m_input.ReadUInt8() + 33; else count = (flags & 0x1f) + 1; for (int i = 0; i < count; ++i) { - m_output[dst++] = m_input.ReadByte(); - m_output[dst++] = m_input.ReadByte(); - m_output[dst++] = m_input.ReadByte(); + m_output[dst++] = m_input.ReadUInt8(); + m_output[dst++] = m_input.ReadUInt8(); + m_output[dst++] = m_input.ReadUInt8(); m_output[dst++] = 0xff; } } else if ((flags & 0xc0) == 0x40) { if (0 != (flags & 0x20)) - count = ((flags & 0x1f) << 8) + m_input.ReadByte() + 34; + count = ((flags & 0x1f) << 8) + m_input.ReadUInt8() + 34; else count = (flags & 0x1f) + 2; - byte b = m_input.ReadByte(); - byte g = m_input.ReadByte(); - byte r = m_input.ReadByte(); + byte b = m_input.ReadUInt8(); + byte g = m_input.ReadUInt8(); + byte r = m_input.ReadUInt8(); for (int i = 0; i < count; ++i) { m_output[dst++] = b; @@ -298,23 +295,23 @@ namespace GameRes.Formats.Lucifen if (0 == (flags & 0x30)) { count = (flags & 0xf) + 1; - pos = m_input.ReadByte() + 2; + pos = m_input.ReadUInt8() + 2; } else if ((flags & 0x30) == 0x10) { - pos = ((flags & 0xf) << 8) + m_input.ReadByte() + 2; - count = m_input.ReadByte() + 1; + pos = ((flags & 0xf) << 8) + m_input.ReadUInt8() + 2; + count = m_input.ReadUInt8() + 1; } else if ((flags & 0x30) == 0x20) { - byte tmp = m_input.ReadByte(); - pos = ((((flags & 0xf) << 8) + tmp) << 8) + m_input.ReadByte() + 4098; - count = m_input.ReadByte() + 1; + byte tmp = m_input.ReadUInt8(); + pos = ((((flags & 0xf) << 8) + tmp) << 8) + m_input.ReadUInt8() + 4098; + count = m_input.ReadUInt8() + 1; } else { if (0 != (flags & 8)) - pos = ((flags & 0x7) << 8) + m_input.ReadByte() + 10; + pos = ((flags & 0x7) << 8) + m_input.ReadUInt8() + 10; else pos = (flags & 0x7) + 2; count = 1; @@ -332,22 +329,22 @@ namespace GameRes.Formats.Lucifen { if (0 == (flags & 0xc)) { - y = ((flags & 0x3) << 8) + m_input.ReadByte() + 16; + y = ((flags & 0x3) << 8) + m_input.ReadUInt8() + 16; x = 0; } else if ((flags & 0xc) == 0x4) { - y = ((flags & 0x3) << 8) + m_input.ReadByte() + 16; + y = ((flags & 0x3) << 8) + m_input.ReadUInt8() + 16; x = -1; } else if ((flags & 0xc) == 0x8) { - y = ((flags & 0x3) << 8) + m_input.ReadByte() + 16; + y = ((flags & 0x3) << 8) + m_input.ReadUInt8() + 16; x = 1; } else { - pos = ((flags & 0x3) << 8) + m_input.ReadByte() + 2058; + pos = ((flags & 0x3) << 8) + m_input.ReadUInt8() + 2058; src = dst - 4 * pos; Buffer.BlockCopy (m_output, src, m_output, dst, 4); dst += 4; @@ -381,7 +378,7 @@ namespace GameRes.Formats.Lucifen int dst = 3; for (;;) { - byte flags = m_input.ReadByte(); + byte flags = m_input.ReadUInt8(); if (0xff == flags || dst >= m_output.Length) break; @@ -390,24 +387,24 @@ namespace GameRes.Formats.Lucifen if (0 == (flags & 0xc0)) { if (0 != (flags & 0x20)) - count = ((flags & 0x1f) << 8) + m_input.ReadByte() + 33; + count = ((flags & 0x1f) << 8) + m_input.ReadUInt8() + 33; else count = (flags & 0x1f) + 1; for (int i = 0; i < count; ++i) { - m_output[dst] = m_input.ReadByte(); + m_output[dst] = m_input.ReadUInt8(); dst += 4; } } else if (0x40 == (flags & 0xc0)) { if (0 != (flags & 0x20)) - count = ((flags & 0x1f) << 8) + m_input.ReadByte() + 35; + count = ((flags & 0x1f) << 8) + m_input.ReadUInt8() + 35; else count = (flags & 0x1f) + 3; - byte a = m_input.ReadByte(); + byte a = m_input.ReadUInt8(); for (int i = 0; i < count; ++i) { m_output[dst] = a; @@ -421,21 +418,21 @@ namespace GameRes.Formats.Lucifen if (0 == (flags & 0x30)) { count = (flags & 0xf) + 2; - pos = m_input.ReadByte() + 2; + pos = m_input.ReadUInt8() + 2; } else if (0x10 == (flags & 0x30)) { - pos = ((flags & 0xf) << 8) + m_input.ReadByte() + 3; - count = m_input.ReadByte() + 4; + pos = ((flags & 0xf) << 8) + m_input.ReadUInt8() + 3; + count = m_input.ReadUInt8() + 4; } else if ((flags & 0x30) == 0x20) { - pos = ((flags & 0xf) << 8) + m_input.ReadByte() + 3; + pos = ((flags & 0xf) << 8) + m_input.ReadUInt8() + 3; count = 3; } else { - pos = ((flags & 0xf) << 8) + m_input.ReadByte() + 3; + pos = ((flags & 0xf) << 8) + m_input.ReadUInt8() + 3; count = 4; } } @@ -466,34 +463,34 @@ namespace GameRes.Formats.Lucifen int dst = 0; for (;;) { - byte flags = m_input.ReadByte(); + byte flags = m_input.ReadUInt8(); if (0xff == flags || dst >= m_output.Length) break; int count, pos, src; if (0 == (flags & 0xc0)) { if (0 != (flags & 0x20)) - count = ((flags & 0x1f) << 8) + m_input.ReadByte() + 33; + count = ((flags & 0x1f) << 8) + m_input.ReadUInt8() + 33; else count = (flags & 0x1f) + 1; for (int i = 0; i < count; ++i) { - m_output[dst++] = m_input.ReadByte(); - m_output[dst++] = m_input.ReadByte(); - m_output[dst++] = m_input.ReadByte(); + m_output[dst++] = m_input.ReadUInt8(); + m_output[dst++] = m_input.ReadUInt8(); + m_output[dst++] = m_input.ReadUInt8(); } } else if ((flags & 0xc0) == 0x40) { if (0 != (flags & 0x20)) - count = ((flags & 0x1f) << 8) + m_input.ReadByte() + 34; + count = ((flags & 0x1f) << 8) + m_input.ReadUInt8() + 34; else count = (flags & 0x1f) + 2; - byte b = m_input.ReadByte(); - byte g = m_input.ReadByte(); - byte r = m_input.ReadByte(); + byte b = m_input.ReadUInt8(); + byte g = m_input.ReadUInt8(); + byte r = m_input.ReadUInt8(); for (int i = 0; i < count; ++i) { m_output[dst++] = b; @@ -506,23 +503,23 @@ namespace GameRes.Formats.Lucifen if (0 == (flags & 0x30)) { count = (flags & 0xf) + 1; - pos = m_input.ReadByte() + 2; + pos = m_input.ReadUInt8() + 2; } else if ((flags & 0x30) == 0x10) { - pos = ((flags & 0xf) << 8) + m_input.ReadByte() + 2; - count = m_input.ReadByte() + 1; + pos = ((flags & 0xf) << 8) + m_input.ReadUInt8() + 2; + count = m_input.ReadUInt8() + 1; } else if ((flags & 0x30) == 0x20) { - byte tmp = m_input.ReadByte(); - pos = ((((flags & 0xf) << 8) + tmp) << 8) + m_input.ReadByte() + 4098; - count = m_input.ReadByte() + 1; + byte tmp = m_input.ReadUInt8(); + pos = ((((flags & 0xf) << 8) + tmp) << 8) + m_input.ReadUInt8() + 4098; + count = m_input.ReadUInt8() + 1; } else { if (0 != (flags & 8)) - pos = ((flags & 0x7) << 8) + m_input.ReadByte() + 10; + pos = ((flags & 0x7) << 8) + m_input.ReadUInt8() + 10; else pos = (flags & 0x7) + 2; count = 1; @@ -540,22 +537,22 @@ namespace GameRes.Formats.Lucifen { if (0 == (flags & 0xc)) { - y = ((flags & 0x3) << 8) + m_input.ReadByte() + 16; + y = ((flags & 0x3) << 8) + m_input.ReadUInt8() + 16; x = 0; } else if ((flags & 0xc) == 0x4) { - y = ((flags & 0x3) << 8) + m_input.ReadByte() + 16; + y = ((flags & 0x3) << 8) + m_input.ReadUInt8() + 16; x = -1; } else if ((flags & 0xc) == 0x8) { - y = ((flags & 0x3) << 8) + m_input.ReadByte() + 16; + y = ((flags & 0x3) << 8) + m_input.ReadUInt8() + 16; x = 1; } else { - pos = ((flags & 0x3) << 8) + m_input.ReadByte() + 2058; + pos = ((flags & 0x3) << 8) + m_input.ReadUInt8() + 2058; src = dst - 3 * pos; m_output[dst++] = m_output[src++]; m_output[dst++] = m_output[src++]; @@ -587,23 +584,10 @@ namespace GameRes.Formats.Lucifen } #region IDisposable Members - bool disposed = false; - public void Dispose () { - Dispose (true); GC.SuppressFinalize (this); } - - protected virtual void Dispose (bool disposing) - { - if (!disposed) - { - if (disposing) - m_input.Dispose(); - disposed = true; - } - } #endregion } } diff --git a/ArcFormats/MAI/ImageMAI.cs b/ArcFormats/MAI/ImageMAI.cs index 1e4a30a8..8a38ba05 100644 --- a/ArcFormats/MAI/ImageMAI.cs +++ b/ArcFormats/MAI/ImageMAI.cs @@ -58,12 +58,12 @@ namespace GameRes.Formats.MAI throw new NotImplementedException ("CmFormat.Write not implemented"); } - public override ImageMetaData ReadMetaData (Stream stream) + public override ImageMetaData ReadMetaData (IBinaryStream stream) { if ('C' != stream.ReadByte() || 'M' != stream.ReadByte()) return null; - var header = new byte[0x1e]; - if (header.Length != stream.Read (header, 0, header.Length)) + var header = stream.ReadBytes (0x1e); + if (header.Length != 0x1e) return null; if (1 != header[0x0c]) return null; @@ -83,9 +83,9 @@ namespace GameRes.Formats.MAI return info; } - public override ImageData Read (Stream stream, ImageMetaData info) + public override ImageData Read (IBinaryStream stream, ImageMetaData info) { - var reader = new Reader (stream, (CmMetaData)info); + var reader = new Reader (stream.AsStream, (CmMetaData)info); reader.Unpack(); return ImageData.CreateFlipped (info, reader.Format, reader.Palette, reader.Data, reader.Stride); } @@ -166,7 +166,7 @@ namespace GameRes.Formats.MAI throw new NotImplementedException ("AmFormat.Write not implemented"); } - public override ImageMetaData ReadMetaData (Stream stream) + public override ImageMetaData ReadMetaData (IBinaryStream stream) { if ('A' != stream.ReadByte() || 'M' != stream.ReadByte()) return null; @@ -197,9 +197,9 @@ namespace GameRes.Formats.MAI return info; } - public override ImageData Read (Stream stream, ImageMetaData info) + public override ImageData Read (IBinaryStream stream, ImageMetaData info) { - var reader = new Reader (stream, (AmMetaData)info); + var reader = new Reader (stream.AsStream, (AmMetaData)info); reader.Unpack(); return ImageData.Create (info, reader.Format, reader.Palette, reader.Data); } @@ -300,40 +300,37 @@ namespace GameRes.Formats.MAI throw new NotImplementedException ("MaskFormat.Write not implemented"); } - public override ImageMetaData ReadMetaData (Stream stream) + public override ImageMetaData ReadMetaData (IBinaryStream file) { - using (var input = new ArcView.Reader (stream)) - { - uint size = input.ReadUInt32(); - if (size != stream.Length) - return null; - uint width = input.ReadUInt32(); - uint height = input.ReadUInt32(); - int compressed = input.ReadInt32(); - if (compressed > 1 || 0 == compressed && (width*height + 0x410) != size) - return null; - return new CmMetaData { - Width = width, - Height = height, - BPP = 8, - IsCompressed = 1 == compressed, - DataOffset = 0x10, - DataLength = size, - }; - } + uint size = file.ReadUInt32(); + if (size != file.Length) + return null; + uint width = file.ReadUInt32(); + uint height = file.ReadUInt32(); + int compressed = file.ReadInt32(); + if (compressed > 1 || 0 == compressed && (width*height + 0x410) != size) + return null; + return new CmMetaData { + Width = width, + Height = height, + BPP = 8, + IsCompressed = 1 == compressed, + DataOffset = 0x10, + DataLength = size, + }; } - public override ImageData Read (Stream stream, ImageMetaData info) + public override ImageData Read (IBinaryStream stream, ImageMetaData info) { var meta = (CmMetaData)info; stream.Position = meta.DataOffset; - var palette = RleDecoder.ReadPalette (stream, 0x100, 4); + var palette = RleDecoder.ReadPalette (stream.AsStream, 0x100, 4); var pixels = new byte[info.Width*info.Height]; if (meta.IsCompressed) { int packed_size = (int)(stream.Length - meta.DataOffset); - RleDecoder.Unpack (stream, packed_size, pixels, 1); + RleDecoder.Unpack (stream.AsStream, packed_size, pixels, 1); } else if (pixels.Length != stream.Read (pixels, 0, pixels.Length)) throw new InvalidFormatException(); diff --git a/ArcFormats/Macromedia/AudioEDIM.cs b/ArcFormats/Macromedia/AudioEDIM.cs index f4b20926..18925d59 100644 --- a/ArcFormats/Macromedia/AudioEDIM.cs +++ b/ArcFormats/Macromedia/AudioEDIM.cs @@ -42,10 +42,11 @@ namespace GameRes.Formats.Selen Signatures = new uint[] { 0x40010000, 0x64010000 }; } - public override SoundInput TryOpen (Stream file) + public override SoundInput TryOpen (IBinaryStream file) { - uint offset = 4 + Binary.BigEndian (FormatCatalog.ReadSignature (file)); - return base.TryOpen (new StreamRegion (file, offset)); + uint offset = 4 + Binary.BigEndian (file.Signature); + var mp3 = new StreamRegion (file.AsStream, offset); + return base.TryOpen (new BinaryStream (mp3, file.Name)); } public override void Write (SoundInput source, Stream output) diff --git a/ArcFormats/Macromedia/ImageBITD.cs b/ArcFormats/Macromedia/ImageBITD.cs index 7abfdf59..a9c8856d 100644 --- a/ArcFormats/Macromedia/ImageBITD.cs +++ b/ArcFormats/Macromedia/ImageBITD.cs @@ -40,17 +40,17 @@ namespace GameRes.Formats.Selen public override string Description { get { return "Selen RLE-compressed bitmap"; } } public override uint Signature { get { return 0; } } - public override ImageMetaData ReadMetaData (Stream stream) + public override ImageMetaData ReadMetaData (IBinaryStream stream) { if (stream.Length > 0xffffff) return null; - var scanner = new BitdScanner (stream); + var scanner = new BitdScanner (stream.AsStream); return scanner.GetInfo(); } - public override ImageData Read (Stream stream, ImageMetaData info) + public override ImageData Read (IBinaryStream stream, ImageMetaData info) { - var reader = new BitdReader (stream, info); + var reader = new BitdReader (stream.AsStream, info); reader.Unpack(); return ImageData.Create (info, reader.Format, null, reader.Data); } diff --git a/ArcFormats/Majiro/ImageRCT.cs b/ArcFormats/Majiro/ImageRCT.cs index eca85e9f..c85583b2 100644 --- a/ArcFormats/Majiro/ImageRCT.cs +++ b/ArcFormats/Majiro/ImageRCT.cs @@ -42,8 +42,8 @@ namespace GameRes.Formats.Majiro public int Version; public bool IsEncrypted; public uint DataOffset; - public uint DataSize; - public uint AddSize; + public int DataSize; + public int AddSize; } internal class RctOptions : ResourceOptions @@ -76,7 +76,7 @@ namespace GameRes.Formats.Majiro set { KnownKeys = ((RctScheme)value).KnownKeys; } } - public override ImageMetaData ReadMetaData (Stream stream) + public override ImageMetaData ReadMetaData (IBinaryStream stream) { stream.Position = 4; int id = stream.ReadByte(); @@ -93,38 +93,35 @@ namespace GameRes.Formats.Majiro if (version != 0 && version != 1) return null; - using (var reader = new BinaryReader (stream, Encoding.ASCII, true)) + uint width = stream.ReadUInt32(); + uint height = stream.ReadUInt32(); + int data_size = stream.ReadInt32(); + int additional_size = 0; + if (1 == version) { - uint width = reader.ReadUInt32(); - uint height = reader.ReadUInt32(); - uint data_size = reader.ReadUInt32(); - uint additional_size = 0; - if (1 == version) - { - additional_size = reader.ReadUInt16(); - } - if (width > 0x8000 || height > 0x8000) - return null; - - return new RctMetaData - { - Width = width, - Height = height, - OffsetX = 0, - OffsetY = 0, - BPP = 24, - Version = version, - IsEncrypted = is_encrypted, - DataOffset = (uint)stream.Position, - DataSize = data_size, - AddSize = additional_size, - }; + additional_size = stream.ReadUInt16(); } + if (width > 0x8000 || height > 0x8000) + return null; + + return new RctMetaData + { + Width = width, + Height = height, + OffsetX = 0, + OffsetY = 0, + BPP = 24, + Version = version, + IsEncrypted = is_encrypted, + DataOffset = (uint)stream.Position, + DataSize = data_size, + AddSize = additional_size, + }; } byte[] Key = null; - public override ImageData Read (Stream file, ImageMetaData info) + public override ImageData Read (IBinaryStream file, ImageMetaData info) { var meta = (RctMetaData)info; @@ -155,9 +152,9 @@ namespace GameRes.Formats.Majiro ImageData ApplyMaskToImage (RctMetaData info, byte[] image, string mask_name) { - using (var mask_file = VFS.OpenSeekableStream (mask_name)) + using (var mask_file = VFS.OpenBinaryStream (mask_name)) { - var format = FindFormat (mask_file, mask_name); + var format = FindFormat (mask_file); if (null == format || !(format.Item1 is Rc8Format) || info.Width != format.Item2.Width || info.Height != format.Item2.Height) throw new InvalidFormatException(); @@ -198,7 +195,7 @@ namespace GameRes.Formats.Majiro return overlay; } - byte[] ReadPixelsData (Stream file, RctMetaData meta) + byte[] ReadPixelsData (IBinaryStream file, RctMetaData meta) { file.Position = meta.DataOffset + meta.AddSize; if (meta.IsEncrypted) @@ -219,11 +216,11 @@ namespace GameRes.Formats.Majiro } } - byte[] ReadBaseImage (Stream file, RctMetaData meta) + byte[] ReadBaseImage (IBinaryStream file, RctMetaData meta) { file.Position = meta.DataOffset; - byte[] name_bin = new byte[meta.AddSize]; - if (name_bin.Length != file.Read (name_bin, 0, name_bin.Length)) + byte[] name_bin = file.ReadBytes (meta.AddSize); + if (name_bin.Length != meta.AddSize) throw new EndOfStreamException(); try { @@ -232,7 +229,7 @@ namespace GameRes.Formats.Majiro name = VFS.CombinePath (dir_name, name); if (VFS.FileExists (name)) { - using (var base_file = VFS.OpenSeekableStream (name)) + using (var base_file = VFS.OpenBinaryStream (name)) { var base_info = ReadMetaData (base_file) as RctMetaData; if (null != base_info && 0 == base_info.AddSize @@ -248,7 +245,7 @@ namespace GameRes.Formats.Majiro return null; } - Stream OpenEncryptedStream (Stream file, uint data_size) + IBinaryStream OpenEncryptedStream (IBinaryStream file, int data_size) { if (null == Key) { @@ -257,15 +254,15 @@ namespace GameRes.Formats.Majiro throw new UnknownEncryptionScheme(); Key = InitDecryptionKey (password); } - byte[] data = new byte[data_size]; - if (data.Length != file.Read (data, 0, data.Length)) + byte[] data = file.ReadBytes (data_size); + if (data.Length != data_size) throw new EndOfStreamException(); for (int i = 0; i < data.Length; ++i) { data[i] ^= Key[i & 0x3FF]; } - return new MemoryStream (data); + return new BinMemoryStream (data, file.Name); } private byte[] InitDecryptionKey (string password) @@ -517,19 +514,19 @@ namespace GameRes.Formats.Majiro #endregion } - internal class Reader : IDisposable + internal sealed class Reader : IDisposable { - private BinaryReader m_input; + private IBinaryStream m_input; private uint m_width; private byte[] m_data; public byte[] Data { get { return m_data; } } - public Reader (Stream file, RctMetaData info) + public Reader (IBinaryStream file, RctMetaData info) { m_width = info.Width; m_data = new byte[m_width * info.Height * 3]; - m_input = new BinaryReader (file, Encoding.ASCII, true); + m_input = file; } internal static readonly sbyte[] ShiftTable = new sbyte[] { @@ -588,25 +585,10 @@ namespace GameRes.Formats.Majiro } #region IDisposable Members - bool disposed = false; - public void Dispose () { - Dispose (true); GC.SuppressFinalize (this); } - - protected virtual void Dispose (bool disposing) - { - if (!disposed) - { - if (disposing) - m_input.Dispose(); - m_input = null; - m_data = null; - disposed = true; - } - } #endregion } } @@ -618,7 +600,7 @@ namespace GameRes.Formats.Majiro public override string Description { get { return "Majiro game engine indexed image format"; } } public override uint Signature { get { return 0x9a925a98; } } - public override ImageMetaData ReadMetaData (Stream stream) + public override ImageMetaData ReadMetaData (IBinaryStream stream) { stream.Position = 4; int id = stream.ReadByte(); @@ -634,24 +616,21 @@ namespace GameRes.Formats.Majiro if (version != 0) return null; - using (var reader = new BinaryReader (stream, Encoding.ASCII, true)) + uint width = stream.ReadUInt32(); + uint height = stream.ReadUInt32(); + if (width > 0x8000 || height > 0x8000) + return null; + return new ImageMetaData { - uint width = reader.ReadUInt32(); - uint height = reader.ReadUInt32(); - if (width > 0x8000 || height > 0x8000) - return null; - return new ImageMetaData - { - Width = width, - Height = height, - OffsetX = 0, - OffsetY = 0, - BPP = 8, - }; - } + Width = width, + Height = height, + OffsetX = 0, + OffsetY = 0, + BPP = 8, + }; } - public override ImageData Read (Stream file, ImageMetaData info) + public override ImageData Read (IBinaryStream file, ImageMetaData info) { using (var reader = new Reader (file, info)) { @@ -666,9 +645,9 @@ namespace GameRes.Formats.Majiro throw new NotImplementedException ("Rc8Format.Write is not implemented."); } - internal class Reader : IDisposable + internal sealed class Reader : IDisposable { - private BinaryReader m_input; + private IBinaryStream m_input; private uint m_width; private Color[] m_palette; private byte[] m_data; @@ -676,7 +655,7 @@ namespace GameRes.Formats.Majiro public Color[] Palette { get { return m_palette; } } public byte[] Data { get { return m_data; } } - public Reader (Stream file, ImageMetaData info) + public Reader (IBinaryStream file, ImageMetaData info) { m_width = info.Width; file.Position = 0x14; @@ -689,7 +668,7 @@ namespace GameRes.Formats.Majiro m_palette[i] = Color.FromRgb (palette_data[i*3], palette_data[i*3+1], palette_data[i*3+2]); } m_data = new byte[m_width * info.Height]; - m_input = new BinaryReader (file, Encoding.ASCII, true); + m_input = file; } private static readonly sbyte[] ShiftTable = new sbyte[] { @@ -746,24 +725,8 @@ namespace GameRes.Formats.Majiro } #region IDisposable Members - bool disposed = false; - public void Dispose () { - Dispose (true); - GC.SuppressFinalize (this); - } - - protected virtual void Dispose (bool disposing) - { - if (!disposed) - { - if (disposing) - m_input.Dispose(); - m_input = null; - m_data = null; - disposed = true; - } } #endregion } diff --git a/ArcFormats/Malie/ImageDZI.cs b/ArcFormats/Malie/ImageDZI.cs index 2794ec9a..8690ca79 100644 --- a/ArcFormats/Malie/ImageDZI.cs +++ b/ArcFormats/Malie/ImageDZI.cs @@ -56,12 +56,13 @@ namespace GameRes.Formats.Malie static readonly Regex pair_re = new Regex (@"^(\d+),(\d+)"); - public override ImageMetaData ReadMetaData (Stream stream) + public override ImageMetaData ReadMetaData (IBinaryStream file) { - var tex = VFS.FindFile (VFS.CombinePath (VFS.Top.CurrentDirectory, "tex")); + var dir = VFS.GetDirectoryName (file.Name); + var tex = VFS.FindFile (VFS.CombinePath (dir, "tex")); if (!(tex is SubDirEntry)) return null; - using (var reader = new StreamReader (stream, Encoding.UTF8, false, 2048, true)) + using (var reader = new StreamReader (file.AsStream, Encoding.UTF8, false, 2048, true)) { reader.ReadLine(); // skip signature string line = reader.ReadLine(); @@ -98,12 +99,12 @@ namespace GameRes.Formats.Malie if (null == line) return null; line = line.TrimEnd(); - foreach (var file in line.Split (',')) + foreach (var filename in line.Split (',')) { - if (!string.IsNullOrEmpty (file)) + if (!string.IsNullOrEmpty (filename)) { - var filename = VFS.CombinePath (tex.Name, file); - tiles.Add (new DziTile { X = x, Y = y, FileName = filename }); + var fullname = VFS.CombinePath (tex.Name, filename); + tiles.Add (new DziTile { X = x, Y = y, FileName = fullname }); } x += 256; } @@ -121,7 +122,7 @@ namespace GameRes.Formats.Malie } } - public override ImageData Read (Stream stream, ImageMetaData info) + public override ImageData Read (IBinaryStream stream, ImageMetaData info) { var meta = (DziMetaData)info; PixelFormat format = PixelFormats.Bgra32; @@ -135,9 +136,9 @@ namespace GameRes.Formats.Malie var image_entry = VFS.GetFiles (tile.FileName + ".*").FirstOrDefault(); if (null == image_entry) throw new FileNotFoundException ("Tile not found", tile.FileName); - using (var input = VFS.OpenStream (image_entry)) + using (var input = VFS.OpenBinaryStream (image_entry)) { - var image = Read (image_entry.Name, input); + var image = Read (input); if (null == image) throw new FileFormatException ("Unknown DZI tile format"); var converted = image.Bitmap; diff --git a/ArcFormats/Malie/ImageMGF.cs b/ArcFormats/Malie/ImageMGF.cs index 0b382151..fc324859 100644 --- a/ArcFormats/Malie/ImageMGF.cs +++ b/ArcFormats/Malie/ImageMGF.cs @@ -41,25 +41,25 @@ namespace GameRes.Formats.Malie static readonly byte[] PngHeader = { 0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A }; - public override ImageMetaData ReadMetaData (Stream stream) + public override ImageMetaData ReadMetaData (IBinaryStream stream) { - var header = new byte[8]; - if (8 != stream.Read (header, 0, 8)) - return null; + var header = stream.ReadHeader (8).ToArray(); if (!Binary.AsciiEqual (header, "MalieGF")) return null; Buffer.BlockCopy (PngHeader, 0, header, 0, 8); - using (var data = new StreamRegion (stream, 8, stream.Length - 8, true)) - using (var png = new PrefixStream (header, data)) + using (var data = new StreamRegion (stream.AsStream, 8, true)) + using (var pre = new PrefixStream (header, data)) + using (var png = new BinaryStream (pre, stream.Name)) return base.ReadMetaData (png); } - public override ImageData Read (Stream stream, ImageMetaData info) + public override ImageData Read (IBinaryStream stream, ImageMetaData info) { var header = PngHeader.Clone() as byte[]; - using (var data = new StreamRegion (stream, 8, stream.Length - 8, true)) - using (var png = new PrefixStream (header, data)) + using (var data = new StreamRegion (stream.AsStream, 8, true)) + using (var pre = new PrefixStream (header, data)) + using (var png = new BinaryStream (pre, stream.Name)) return base.Read (png, info); } diff --git a/ArcFormats/MangaGamer/ArcMGPK.cs b/ArcFormats/MangaGamer/ArcMGPK.cs index 1e405f42..01296f67 100644 --- a/ArcFormats/MangaGamer/ArcMGPK.cs +++ b/ArcFormats/MangaGamer/ArcMGPK.cs @@ -115,7 +115,7 @@ namespace GameRes.Formats.Mg data = DecryptBlock (data, mgarc.Key); if (entry.Name.EndsWith (".txt", StringComparison.InvariantCultureIgnoreCase)) return DecompressStream (data); - return new MemoryStream (data); + return new BinMemoryStream (data, entry.Name); } } diff --git a/ArcFormats/Marble/ArcMBL.cs b/ArcFormats/Marble/ArcMBL.cs index f0d0b8ea..479a2554 100644 --- a/ArcFormats/Marble/ArcMBL.cs +++ b/ArcFormats/Marble/ArcMBL.cs @@ -184,7 +184,7 @@ namespace GameRes.Formats.Marble data[i] ^= marc.Key[i % marc.Key.Length]; } } - return new MemoryStream (data); + return new BinMemoryStream (data, entry.Name); } public override ResourceOptions GetDefaultOptions () diff --git a/ArcFormats/Marble/AudioWADY.cs b/ArcFormats/Marble/AudioWADY.cs index 11fa54a4..214cde84 100644 --- a/ArcFormats/Marble/AudioWADY.cs +++ b/ArcFormats/Marble/AudioWADY.cs @@ -64,38 +64,36 @@ namespace GameRes.Formats.Marble return Source.ReadByte(); } - public WadyInput (Stream file) : base (new MemoryStream()) + public WadyInput (IBinaryStream input) : base (new MemoryStream()) { - using (var input = new BinaryReader (file)) + input.Seek (5, SeekOrigin.Begin); + MulValue = input.ReadUInt8(); + input.Seek (6, SeekOrigin.Current); + int src_size = input.ReadInt32(); + input.Seek (16, SeekOrigin.Current); + var format = new WaveFormat(); + format.FormatTag = input.ReadUInt16(); + format.Channels = input.ReadUInt16(); + format.SamplesPerSecond = input.ReadUInt32(); + format.AverageBytesPerSecond = input.ReadUInt32(); + format.BlockAlign = input.ReadUInt16(); + format.BitsPerSample = input.ReadUInt16(); + format.ExtraSize = 0; + this.Format = format; + int remaining = (int)(input.Length-input.Position); + if (remaining == src_size) { - input.BaseStream.Seek (5, SeekOrigin.Begin); - MulValue = input.ReadByte(); - input.BaseStream.Seek (6, SeekOrigin.Current); - int src_size = input.ReadInt32(); - input.BaseStream.Seek (16, SeekOrigin.Current); - var format = new WaveFormat(); - format.FormatTag = input.ReadUInt16(); - format.Channels = input.ReadUInt16(); - format.SamplesPerSecond = input.ReadUInt32(); - format.AverageBytesPerSecond = input.ReadUInt32(); - format.BlockAlign = input.ReadUInt16(); - format.BitsPerSample = input.ReadUInt16(); - format.ExtraSize = 0; - this.Format = format; - int remaining = (int)(input.BaseStream.Length-input.BaseStream.Position); - if (remaining == src_size) - { - (Source as MemoryStream).Capacity = src_size * 2; - Decode (input, src_size, Source); - } - else - Decode2 (input, Source); - Source.Position = 0; - this.PcmSize = Source.Length; + (Source as MemoryStream).Capacity = src_size * 2; + Decode (input, src_size, Source); } + else + Decode2 (input, Source); + Source.Position = 0; + this.PcmSize = Source.Length; + input.Dispose(); } - private void Decode (BinaryReader input, int count, Stream output) + private void Decode (IBinaryStream input, int count, Stream output) { using (var buffer = new BinaryWriter (output, Encoding.ASCII, true)) { @@ -103,7 +101,7 @@ namespace GameRes.Formats.Marble ushort sampleR = 0; for (int i = 0; i < count; ++i) { - byte v = input.ReadByte(); + byte v = input.ReadUInt8(); if (0 != (v & 0x80)) sampleL = (ushort)(v << 9); else @@ -112,7 +110,7 @@ namespace GameRes.Formats.Marble if (1 != Format.Channels) { ++i; - v = input.ReadByte(); + v = input.ReadUInt8(); if (0 != (v & 0x80)) sampleR = (ushort)(v << 9); else @@ -123,13 +121,13 @@ namespace GameRes.Formats.Marble } } - private void Decode2 (BinaryReader input, Stream output) + private void Decode2 (IBinaryStream input, Stream output) { if (1 != Format.Channels) { int channel_size = input.ReadInt32(); Decode3 (input, output, 2); - input.BaseStream.Position = 0x38 + channel_size; + input.Position = 0x38 + channel_size; output.Position = 2; Decode3 (input, output, 2); } @@ -137,7 +135,7 @@ namespace GameRes.Formats.Marble Decode3 (input, output, 0); } - private void Decode3 (BinaryReader input, Stream output, int step) + private void Decode3 (IBinaryStream input, Stream output, int step) { input.ReadInt32(); // unpacked_size int count = input.ReadInt32(); @@ -149,7 +147,7 @@ namespace GameRes.Formats.Marble { if (count - 300 == i) sample = 0; - ushort v = input.ReadByte(); + ushort v = input.ReadUInt8(); if (0 != (v & 1)) { ushort v14 = (ushort)((v >> 1) & 0x7F); @@ -163,7 +161,7 @@ namespace GameRes.Formats.Marble } else { - v |= (ushort)(input.ReadByte() << 8); + v |= (ushort)(input.ReadUInt8() << 8); int repeat = SizeTable[(v >> 1) & 7]; short end = (short)(v & 0xFFF0); double inc = (end - sample) / (double)repeat; @@ -221,7 +219,7 @@ namespace GameRes.Formats.Marble public override string Description { get { return "Marble engine wave audio format"; } } public override uint Signature { get { return 0x59444157u; } } // 'WADY' - public override SoundInput TryOpen (Stream file) + public override SoundInput TryOpen (IBinaryStream file) { return new WadyInput (file); } diff --git a/ArcFormats/Marble/ImagePRS.cs b/ArcFormats/Marble/ImagePRS.cs index 2e3bdb28..7386b88f 100644 --- a/ArcFormats/Marble/ImagePRS.cs +++ b/ArcFormats/Marble/ImagePRS.cs @@ -53,11 +53,9 @@ namespace GameRes.Formats.Marble throw new NotImplementedException ("PrsFormat.Write not implemented"); } - public override ImageMetaData ReadMetaData (Stream stream) + public override ImageMetaData ReadMetaData (IBinaryStream stream) { - var header = new byte[0x10]; - if (header.Length != stream.Read (header, 0, header.Length)) - return null; + var header = stream.ReadHeader (0x10); if (header[0] != 'Y' || header[1] != 'B') return null; int bpp = header[3]; @@ -66,15 +64,15 @@ namespace GameRes.Formats.Marble return new PrsMetaData { - Width = LittleEndian.ToUInt16 (header, 12), - Height = LittleEndian.ToUInt16 (header, 14), + Width = header.ToUInt16 (12), + Height = header.ToUInt16 (14), BPP = 8 * bpp, Flag = header[2], - PackedSize = LittleEndian.ToUInt32 (header, 4), + PackedSize = header.ToUInt32 (4), }; } - public override ImageData Read (Stream stream, ImageMetaData info) + public override ImageData Read (IBinaryStream stream, ImageMetaData info) { using (var reader = new Reader (stream, (PrsMetaData)info)) { @@ -85,7 +83,7 @@ namespace GameRes.Formats.Marble internal class Reader : IDisposable { - BinaryReader m_input; + IBinaryStream m_input; byte[] m_output; uint m_size; byte m_flag; @@ -95,9 +93,9 @@ namespace GameRes.Formats.Marble public PixelFormat Format { get; private set; } public int Stride { get; private set; } - public Reader (Stream file, PrsMetaData info) + public Reader (IBinaryStream file, PrsMetaData info) { - m_input = new BinaryReader (file, Encoding.ASCII, true); + m_input = file; m_size = info.PackedSize; m_flag = info.Flag; m_depth = info.BPP / 8; @@ -123,7 +121,7 @@ namespace GameRes.Formats.Marble public void Unpack () { - m_input.BaseStream.Position = 0x10; + m_input.Position = 0x10; int dst = 0; int remaining = (int)m_size; int bit = 0; @@ -133,7 +131,7 @@ namespace GameRes.Formats.Marble bit >>= 1; if (0 == bit) { - ctl = m_input.ReadByte(); + ctl = m_input.ReadUInt8(); --remaining; bit = 0x80; } @@ -141,11 +139,11 @@ namespace GameRes.Formats.Marble break; if (0 == (ctl & bit)) { - m_output[dst++] = m_input.ReadByte(); + m_output[dst++] = m_input.ReadUInt8(); --remaining; continue; } - int b = m_input.ReadByte(); + int b = m_input.ReadUInt8(); --remaining; int length = 0; int shift = 0; @@ -154,14 +152,14 @@ namespace GameRes.Formats.Marble { if (remaining <= 0) break; - shift = m_input.ReadByte(); + shift = m_input.ReadUInt8(); --remaining; shift |= (b & 0x3f) << 8; if (0 != (b & 0x40)) { if (remaining <= 0) break; - int offset = m_input.ReadByte(); + int offset = m_input.ReadUInt8(); --remaining; length = LengthTable[offset]; } diff --git a/ArcFormats/Masys/ImageAG.cs b/ArcFormats/Masys/ImageAG.cs index 315d5178..c559df84 100644 --- a/ArcFormats/Masys/ImageAG.cs +++ b/ArcFormats/Masys/ImageAG.cs @@ -38,22 +38,19 @@ namespace GameRes.Formats.Megu public override string Description { get { return "Masys image format"; } } public override uint Signature { get { return 0x00644741u; } } // 'AGd' - public override ImageMetaData ReadMetaData (Stream stream) + public override ImageMetaData ReadMetaData (IBinaryStream file) { - using (var file = new ArcView.Reader (stream)) - { - file.ReadUInt32(); - var info = new ImageMetaData(); - info.Width = file.ReadUInt32(); - info.Height = file.ReadUInt32(); - file.BaseStream.Position = 0x38; - int alpha_size = file.ReadInt32(); - info.BPP = 0 == alpha_size ? 24 : 32; - return info; - } + file.Position = 4; + var info = new ImageMetaData(); + info.Width = file.ReadUInt32(); + info.Height = file.ReadUInt32(); + file.Position = 0x38; + int alpha_size = file.ReadInt32(); + info.BPP = 0 == alpha_size ? 24 : 32; + return info; } - public override ImageData Read (Stream stream, ImageMetaData info) + public override ImageData Read (IBinaryStream stream, ImageMetaData info) { var reader = new AgReader (stream, info); reader.Unpack(); @@ -83,54 +80,51 @@ namespace GameRes.Formats.Megu public byte[] Data { get { return m_output; } } public PixelFormat Format { get; private set; } - public AgReader (Stream stream, ImageMetaData info) + public AgReader (IBinaryStream input, ImageMetaData info) { m_width = (int)info.Width; m_height = (int)info.Height; - stream.Position = 0x0c; - using (var input = new ArcView.Reader (stream)) + input.Position = 0x0c; + uint offset1 = input.ReadUInt32(); + int size1 = input.ReadInt32(); + uint offset2 = input.ReadUInt32(); + int size2 = input.ReadInt32(); + uint offset3 = input.ReadUInt32(); + int size3 = input.ReadInt32(); + uint offset4 = input.ReadUInt32(); + int size4 = input.ReadInt32(); + uint offset5 = input.ReadUInt32(); + int size5 = input.ReadInt32(); + uint offset6 = input.ReadUInt32(); + int size6 = input.ReadInt32(); + input.Read (m_first, 0, 3); + if (size1 != 0) + in1 = ReadSection (input, offset1, size1); + if (size2 != 0) + in2 = ReadSection (input, offset2, size2); + if (size3 != 0) + in3 = ReadSection (input, offset3, size3); + if (size4 != 0) + in4 = ReadSection (input, offset4, size4); + if (size5 != 0) + in5 = ReadSection (input, offset5, size5); + if (size6 != 0) { - uint offset1 = input.ReadUInt32(); - int size1 = input.ReadInt32(); - uint offset2 = input.ReadUInt32(); - int size2 = input.ReadInt32(); - uint offset3 = input.ReadUInt32(); - int size3 = input.ReadInt32(); - uint offset4 = input.ReadUInt32(); - int size4 = input.ReadInt32(); - uint offset5 = input.ReadUInt32(); - int size5 = input.ReadInt32(); - uint offset6 = input.ReadUInt32(); - int size6 = input.ReadInt32(); - input.Read (m_first, 0, 3); - if (size1 != 0) - in1 = ReadSection (stream, offset1, size1); - if (size2 != 0) - in2 = ReadSection (stream, offset2, size2); - if (size3 != 0) - in3 = ReadSection (stream, offset3, size3); - if (size4 != 0) - in4 = ReadSection (stream, offset4, size4); - if (size5 != 0) - in5 = ReadSection (stream, offset5, size5); - if (size6 != 0) - { - input.BaseStream.Position = offset6; - m_alpha = new byte[m_height*m_width]; - RleDecode (input, m_alpha); - Format = PixelFormats.Bgra32; - m_pixel_size = 4; - } - else - { - Format = PixelFormats.Bgr24; - m_pixel_size = 3; - } - m_output = new byte[m_width*m_height*m_pixel_size]; + input.Position = offset6; + m_alpha = new byte[m_height*m_width]; + RleDecode (input, m_alpha); + Format = PixelFormats.Bgra32; + m_pixel_size = 4; } + else + { + Format = PixelFormats.Bgr24; + m_pixel_size = 3; + } + m_output = new byte[m_width*m_height*m_pixel_size]; } - static private byte[] ReadSection (Stream input, long offset, int size) + static private byte[] ReadSection (IBinaryStream input, long offset, int size) { input.Position = offset; var buf = new byte[size + 4]; @@ -362,13 +356,13 @@ namespace GameRes.Formats.Megu } } - private static void RleDecode (BinaryReader src, byte[] dst_buf) + private static void RleDecode (IBinaryStream src, byte[] dst_buf) { int remaining = dst_buf.Length; int dst = 0; while (remaining > 0) { - byte v = src.ReadByte(); + byte v = src.ReadUInt8(); int count; if (0 != (v & 0x80)) { diff --git a/ArcFormats/MnoViolet/ImageDIF.cs b/ArcFormats/MnoViolet/ImageDIF.cs index 71ecf562..f0802ba8 100644 --- a/ArcFormats/MnoViolet/ImageDIF.cs +++ b/ArcFormats/MnoViolet/ImageDIF.cs @@ -67,23 +67,22 @@ namespace GameRes.Formats.MnoViolet Extensions = new string[] { "dif" }; } - public override ImageMetaData ReadMetaData (Stream stream) + public override ImageMetaData ReadMetaData (IBinaryStream stream) { - var header = new byte[0x7C]; - if (header.Length != stream.Read (header, 0, header.Length)) - return null; - var base_name = Binary.GetCString (header, 4, 100); + var header = stream.ReadHeader (0x7C); + var base_name = header.GetCString (4, 100); if (string.IsNullOrEmpty (base_name)) return null; var files = VFS.GetFiles (base_name+".*"); if (!files.Any()) throw new FileNotFoundException (string.Format ("Base image '{0}' not found", base_name)); var base_entry = files.First(); - using (var input = VFS.OpenSeekableStream (base_entry)) + if (base_entry.Name.Equals (stream.Name, StringComparison.InvariantCultureIgnoreCase)) + throw new InvalidFormatException ("DIF image references itself"); + using (var input = VFS.OpenBinaryStream (base_entry)) { - // ReadMetaData isn't supplied with a filename being processed, so infinite recursion can't be - // prevented here unless we save state in a static member. - var format = ImageFormat.FindFormat (input, base_entry.Name); + // infinite recursion still possible in case of two files referencing each other. + var format = ImageFormat.FindFormat (input); if (null == format) throw new InvalidFormatException (string.Format ("Unable to interpret base image '{0}'", base_name)); format.Item2.FileName = base_entry.Name; @@ -95,27 +94,27 @@ namespace GameRes.Formats.MnoViolet BaseEntry = base_entry, BaseFormat = format.Item1, BaseInfo = format.Item2, - PackedIndexSize = LittleEndian.ToInt32 (header, 0x68), - IndexSize = LittleEndian.ToInt32 (header, 0x6C), - PackedDiffSize = LittleEndian.ToInt32 (header, 0x70), - DiffDataSize = LittleEndian.ToInt32 (header, 0x74), - DiffCount = LittleEndian.ToInt32 (header, 0x78), + PackedIndexSize = header.ToInt32 (0x68), + IndexSize = header.ToInt32 (0x6C), + PackedDiffSize = header.ToInt32 (0x70), + DiffDataSize = header.ToInt32 (0x74), + DiffCount = header.ToInt32 (0x78), }; } } - public override ImageData Read (Stream stream, ImageMetaData info) + public override ImageData Read (IBinaryStream stream, ImageMetaData info) { var meta = (DifMetaData)info; BitmapSource base_bitmap; - using (var input = VFS.OpenSeekableStream (meta.BaseEntry)) + using (var input = VFS.OpenBinaryStream (meta.BaseEntry)) { var image = meta.BaseFormat.Read (input, meta.BaseInfo); base_bitmap = image.Bitmap; } stream.Position = 0x7C; var index = new byte[meta.IndexSize]; - using (var input = new LzssStream (stream, LzssMode.Decompress, true)) + using (var input = new LzssStream (stream.AsStream, LzssMode.Decompress, true)) if (index.Length != input.Read (index, 0, index.Length)) throw new EndOfStreamException(); @@ -134,7 +133,7 @@ namespace GameRes.Formats.MnoViolet } stream.Position = 0x7C + meta.PackedIndexSize; - using (var diff = new LzssStream (stream, LzssMode.Decompress, true)) + using (var diff = new LzssStream (stream.AsStream, LzssMode.Decompress, true)) { int index_src = 0; for (int i = 0; i < meta.DiffCount; ++i) diff --git a/ArcFormats/MnoViolet/ImageGRA.cs b/ArcFormats/MnoViolet/ImageGRA.cs index 97bf9f8b..d439e9b5 100644 --- a/ArcFormats/MnoViolet/ImageGRA.cs +++ b/ArcFormats/MnoViolet/ImageGRA.cs @@ -55,45 +55,42 @@ namespace GameRes.Formats.MnoViolet throw new NotImplementedException ("GraFormat.Write not implemented"); } - public override ImageMetaData ReadMetaData (Stream stream) + public override ImageMetaData ReadMetaData (IBinaryStream file) { - using (var input = new ArcView.Reader (stream)) + uint sign = file.ReadUInt32(); + uint width = file.ReadUInt32(); + uint height = file.ReadUInt32(); + if (0 == width || width > 0x8000 || 0 == height || height > 0x8000) + return null; + int bpp; + if (0x617267 == sign) // 'gra' + bpp = 24; + else if (0x73616D == sign) // 'mas' + bpp = 8; + else if (1 == sign) + bpp = file.ReadInt32(); + else + return null; + if (bpp != 32 && bpp != 24 && bpp != 8) + return null; + int packed_size = file.ReadInt32(); + int data_size = file.ReadInt32(); + return new GraMetaData { - uint sign = input.ReadUInt32(); - uint width = input.ReadUInt32(); - uint height = input.ReadUInt32(); - if (0 == width || width > 0x8000 || 0 == height || height > 0x8000) - return null; - int bpp; - if (0x617267 == sign) // 'gra' - bpp = 24; - else if (0x73616D == sign) // 'mas' - bpp = 8; - else if (1 == sign) - bpp = input.ReadInt32(); - else - return null; - if (bpp != 32 && bpp != 24 && bpp != 8) - return null; - int packed_size = input.ReadInt32(); - int data_size = input.ReadInt32(); - return new GraMetaData - { - Width = width, - Height = height, - PackedSize = packed_size, - UnpackedSize = data_size, - BPP = bpp, - DataOffset = stream.Position, - }; - } + Width = width, + Height = height, + PackedSize = packed_size, + UnpackedSize = data_size, + BPP = bpp, + DataOffset = file.Position, + }; } - public override ImageData Read (Stream stream, ImageMetaData info) + public override ImageData Read (IBinaryStream stream, ImageMetaData info) { var meta = (GraMetaData)info; stream.Position = meta.DataOffset; - using (var reader = new LzssReader (stream, meta.PackedSize, meta.UnpackedSize)) + using (var reader = new LzssReader (stream.AsStream, meta.PackedSize, meta.UnpackedSize)) { reader.Unpack(); int stride = ((int)info.Width*info.BPP/8 + 3) & ~3; diff --git a/ArcFormats/MokoPro/CompressedFile.cs b/ArcFormats/MokoPro/CompressedFile.cs index 84311ca4..95c1359d 100644 --- a/ArcFormats/MokoPro/CompressedFile.cs +++ b/ArcFormats/MokoPro/CompressedFile.cs @@ -98,12 +98,13 @@ namespace GameRes.Formats.Mokopro Extensions = new string[0]; } - public override ImageMetaData ReadMetaData (Stream stream) + public override ImageMetaData ReadMetaData (IBinaryStream stream) { - var moko = new MokoCrypt (stream); + var moko = new MokoCrypt (stream.AsStream); using (var lzss = moko.UnpackStream()) + using (var bmp = new BinaryStream (lzss, stream.Name)) { - var info = base.ReadMetaData (lzss); + var info = base.ReadMetaData (bmp); if (null == info) return null; return new NNNNMetaData @@ -117,11 +118,12 @@ namespace GameRes.Formats.Mokopro } } - public override ImageData Read (Stream stream, ImageMetaData info) + public override ImageData Read (IBinaryStream stream, ImageMetaData info) { var meta = (NNNNMetaData)info; using (var lzss = meta.Input.UnpackStream()) - return base.Read (lzss, meta.BmpInfo); + using (var bmp = new BinaryStream (lzss, stream.Name)) + return base.Read (bmp, meta.BmpInfo); } public override void Write (Stream file, ImageData image) @@ -142,9 +144,9 @@ namespace GameRes.Formats.Mokopro Extensions = new string[0]; } - public override SoundInput TryOpen (Stream stream) + public override SoundInput TryOpen (IBinaryStream stream) { - var moko = new MokoCrypt (stream); + var moko = new MokoCrypt (stream.AsStream); var ogg = moko.UnpackBytes(); var output = new MemoryStream (ogg); try diff --git a/ArcFormats/Moonhir/ArcFPK.cs b/ArcFormats/Moonhir/ArcFPK.cs index 7a7fcf09..3a6f1167 100644 --- a/ArcFormats/Moonhir/ArcFPK.cs +++ b/ArcFormats/Moonhir/ArcFPK.cs @@ -132,7 +132,7 @@ namespace GameRes.Formats.MoonhirGames var data = arc.File.View.ReadBytes (entry.Offset, entry.Size); Decrypt (data, 0, data.Length, farc.Key); int length = LittleEndian.ToInt32 (data, data.Length-8); - input = new MemoryStream (data, 0, length); + input = new BinMemoryStream (data, 0, length, entry.Name); header = data; } if (!Binary.AsciiEqual (header, "FBX\x01")) @@ -143,7 +143,7 @@ namespace GameRes.Formats.MoonhirGames int unpacked_size = LittleEndian.ToInt32 (header, 0xC); input.Position = header[7]; var unpacked = UnpackFbx (input, packed_size, unpacked_size); - return new MemoryStream (unpacked); + return new BinMemoryStream (unpacked, entry.Name); } } diff --git a/ArcFormats/Nags/ImageNGP.cs b/ArcFormats/Nags/ImageNGP.cs index 2206a4da..b5257e6a 100644 --- a/ArcFormats/Nags/ImageNGP.cs +++ b/ArcFormats/Nags/ImageNGP.cs @@ -43,34 +43,31 @@ namespace GameRes.Formats.Nags public override string Description { get { return "NAGS engine image format"; } } public override uint Signature { get { return 0x2050474E; } } // 'NGP ' - public override ImageMetaData ReadMetaData (Stream stream) + public override ImageMetaData ReadMetaData (IBinaryStream file) { - stream.Position = 0x12; - using (var reader = new ArcView.Reader (stream)) + file.Position = 0x12; + int packed_size = file.ReadInt32(); + uint width = file.ReadUInt32(); + uint height = file.ReadUInt32(); + int bpp = file.ReadUInt16() * 8; + file.Position = 0x100; + int unpacked_size = file.ReadInt32(); + if (packed_size <= 0 || unpacked_size <= 0) + return null; + return new NgpMetaData { - int packed_size = reader.ReadInt32(); - uint width = reader.ReadUInt32(); - uint height = reader.ReadUInt32(); - int bpp = reader.ReadUInt16() * 8; - reader.BaseStream.Position = 0x100; - int unpacked_size = reader.ReadInt32(); - if (packed_size <= 0 || unpacked_size <= 0) - return null; - return new NgpMetaData - { - Width = width, - Height = height, - BPP = bpp, - PackedSize = packed_size, - UnpackedSize = unpacked_size, - }; - } + Width = width, + Height = height, + BPP = bpp, + PackedSize = packed_size, + UnpackedSize = unpacked_size, + }; } - public override ImageData Read (Stream stream, ImageMetaData info) + public override ImageData Read (IBinaryStream stream, ImageMetaData info) { var meta = (NgpMetaData)info; - using (var input = new StreamRegion (stream, 0x104, meta.PackedSize, true)) + using (var input = new StreamRegion (stream.AsStream, 0x104, meta.PackedSize, true)) using (var z = new ZLibStream (input, CompressionMode.Decompress)) { var pixels = new byte[meta.UnpackedSize]; diff --git a/ArcFormats/NekoSDK/ImageALP.cs b/ArcFormats/NekoSDK/ImageALP.cs index 3c2af24a..8970997b 100644 --- a/ArcFormats/NekoSDK/ImageALP.cs +++ b/ArcFormats/NekoSDK/ImageALP.cs @@ -33,7 +33,7 @@ namespace GameRes.Formats.NekoSDK [Export(typeof(IBmpExtension))] public class AlpBitmap : IBmpExtension { - public ImageData Read (Stream file, BmpMetaData info) + public ImageData Read (IBinaryStream file, BmpMetaData info) { if (info.BPP != 24 && info.BPP != 32 || !file.CanSeek) return null; diff --git a/ArcFormats/Nexas/ArcPAC.cs b/ArcFormats/Nexas/ArcPAC.cs index 19c017de..f3d9daea 100644 --- a/ArcFormats/Nexas/ArcPAC.cs +++ b/ArcFormats/Nexas/ArcPAC.cs @@ -194,7 +194,7 @@ namespace GameRes.Formats.NeXAS var packed = new byte[entry.Size]; input.Read (packed, 0, packed.Length); var unpacked = HuffmanDecode (packed, (int)pent.UnpackedSize); - return new MemoryStream (unpacked, 0, (int)pent.UnpackedSize); + return new BinMemoryStream (unpacked, 0, (int)pent.UnpackedSize, entry.Name); } case Compression.Deflate: default: diff --git a/ArcFormats/Nexas/ImageGRP.cs b/ArcFormats/Nexas/ImageGRP.cs index 95d6e4d0..f39b391a 100644 --- a/ArcFormats/Nexas/ImageGRP.cs +++ b/ArcFormats/Nexas/ImageGRP.cs @@ -48,21 +48,19 @@ namespace GameRes.Formats.NeXAS Extensions = new string[] { "grp" }; } - public override ImageMetaData ReadMetaData (Stream stream) + public override ImageMetaData ReadMetaData (IBinaryStream stream) { - var header = new byte[0x11]; - if (header.Length != stream.Read (header, 0, header.Length)) - return null; + var header = stream.ReadHeader (0x11); return new GrpMetaData { - Width = LittleEndian.ToUInt32 (header, 5), - Height = LittleEndian.ToUInt32 (header, 9), - BPP = LittleEndian.ToUInt16 (header, 3), - UnpackedSize = LittleEndian.ToInt32 (header, 0xD), + Width = header.ToUInt32 (5), + Height = header.ToUInt32 (9), + BPP = header.ToUInt16 (3), + UnpackedSize = header.ToInt32 (0xD), }; } - public override ImageData Read (Stream stream, ImageMetaData info) + public override ImageData Read (IBinaryStream stream, ImageMetaData info) { using (var reader = new GrpReader (stream, (GrpMetaData)info)) { @@ -79,15 +77,15 @@ namespace GameRes.Formats.NeXAS internal sealed class GrpReader : IDisposable { - BinaryReader m_input; + IBinaryStream m_input; byte[] m_output; public byte[] Data { get { return m_output; } } public PixelFormat Format { get; private set; } - public GrpReader (Stream input, GrpMetaData info) + public GrpReader (IBinaryStream input, GrpMetaData info) { - m_input = new ArcView.Reader (input); + m_input = input; m_output = new byte[info.UnpackedSize]; if (24 == info.BPP) Format = PixelFormats.Bgr24; @@ -99,7 +97,7 @@ namespace GameRes.Formats.NeXAS public void Unpack () { - m_input.BaseStream.Position = 0x11; + m_input.Position = 0x11; int ctl_length = (m_input.ReadInt32() + 7) / 8; var ctl_bytes = m_input.ReadBytes (ctl_length); m_input.ReadInt32(); @@ -114,7 +112,7 @@ namespace GameRes.Formats.NeXAS break; if (0 == bit) { - m_output[dst++] = m_input.ReadByte(); + m_output[dst++] = m_input.ReadUInt8(); } else { @@ -129,14 +127,8 @@ namespace GameRes.Formats.NeXAS } #region IDisposable Members - bool _disposed = false; public void Dispose () { - if (!_disposed) - { - m_input.Dispose(); - _disposed = true; - } } #endregion } diff --git a/ArcFormats/NitroPlus/ArcNitro.cs b/ArcFormats/NitroPlus/ArcNitro.cs index d4612b4d..595f3fd4 100644 --- a/ArcFormats/NitroPlus/ArcNitro.cs +++ b/ArcFormats/NitroPlus/ArcNitro.cs @@ -215,7 +215,7 @@ namespace GameRes.Formats.NitroPlus key = Binary.RotR (key, 8); } if (enc_size == entry.Size) - return new MemoryStream (buf); + return new BinMemoryStream (buf, entry.Name); return new PrefixStream (buf, arc.File.CreateStream (entry.Offset+enc_size, entry.Size-enc_size)); } } diff --git a/ArcFormats/NonColor/ArcDAT.cs b/ArcFormats/NonColor/ArcDAT.cs index 16006ff5..8198d178 100644 --- a/ArcFormats/NonColor/ArcDAT.cs +++ b/ArcFormats/NonColor/ArcDAT.cs @@ -193,7 +193,7 @@ namespace GameRes.Formats.NonColor for (int i = 0; i < dent.RawName.Length-1; ++i) for (int j = 0; j < block_length; ++j) data[n++] ^= dent.RawName[i]; - return new MemoryStream (data); + return new BinMemoryStream (data, entry.Name); } static IDictionary> FileMapIndex = null; diff --git a/ArcFormats/Pajamas/ArcGameDat.cs b/ArcFormats/Pajamas/ArcGameDat.cs index 55eef147..1477ee95 100644 --- a/ArcFormats/Pajamas/ArcGameDat.cs +++ b/ArcFormats/Pajamas/ArcGameDat.cs @@ -27,7 +27,6 @@ using System; using System.Collections.Generic; using System.ComponentModel.Composition; using System.IO; -using GameRes.Utility; namespace GameRes.Formats.Pajamas { @@ -84,10 +83,9 @@ namespace GameRes.Formats.Pajamas public override Stream OpenEntry (ArcFile arc, Entry entry) { - if (!entry.Name.Equals ("textdata.bin", StringComparison.InvariantCultureIgnoreCase)) + if (!entry.Name.EndsWith ("textdata.bin", StringComparison.InvariantCultureIgnoreCase)) return arc.File.CreateStream (entry.Offset, entry.Size); - var data = new byte[entry.Size]; - arc.File.View.Read (entry.Offset, data, 0, entry.Size); + var data = arc.File.View.ReadBytes (entry.Offset, entry.Size); // encrypted PJADV if (0x95 == data[0] && 0x6B == data[1] && 0x3C == data[2] && 0x9D == data[3] && 0x63 == data[4]) @@ -99,7 +97,7 @@ namespace GameRes.Formats.Pajamas key += 0x5C; } } - return new MemoryStream (data); + return new BinMemoryStream (data, entry.Name); } } } diff --git a/ArcFormats/Pajamas/ImageEPA.cs b/ArcFormats/Pajamas/ImageEPA.cs index c44fed68..d970f067 100644 --- a/ArcFormats/Pajamas/ImageEPA.cs +++ b/ArcFormats/Pajamas/ImageEPA.cs @@ -56,41 +56,35 @@ namespace GameRes.Formats.Pajamas throw new NotImplementedException ("EpaFormat.Write not implemented"); } - public override ImageMetaData ReadMetaData (Stream stream) + public override ImageMetaData ReadMetaData (IBinaryStream file) { - using (var header = new ArcView.Reader (stream)) + var info = new EpaMetaData(); + info.Mode = file.ReadInt32() >> 24; + info.ColorType = file.ReadInt32() & 0xff; + switch (info.ColorType) { - var info = new EpaMetaData(); - info.Mode = header.ReadInt32() >> 24; - info.ColorType = header.ReadInt32() & 0xff; - switch (info.ColorType) - { - case 0: info.BPP = 8; break; - case 1: info.BPP = 24; break; - case 2: info.BPP = 32; break; - case 3: info.BPP = 15; break; - case 4: info.BPP = 8; break; - default: return null; - } - info.Width = header.ReadUInt32(); - info.Height = header.ReadUInt32(); - if (2 == info.Mode) - { - info.OffsetX = header.ReadInt32(); - info.OffsetY = header.ReadInt32(); - } - return info; + case 0: info.BPP = 8; break; + case 1: info.BPP = 24; break; + case 2: info.BPP = 32; break; + case 3: info.BPP = 15; break; + case 4: info.BPP = 8; break; + default: return null; } + info.Width = file.ReadUInt32(); + info.Height = file.ReadUInt32(); + if (2 == info.Mode) + { + info.OffsetX = file.ReadInt32(); + info.OffsetY = file.ReadInt32(); + } + return info; } - public override ImageData Read (Stream stream, ImageMetaData info) + public override ImageData Read (IBinaryStream file, ImageMetaData info) { - var meta = info as EpaMetaData; - if (null == meta) - throw new ArgumentException ("EpaFormat.Read should be supplied with EpaMetaData", "info"); - - stream.Position = 2 == meta.Mode ? 0x18 : 0x10; - var reader = new Reader (stream, meta); + var meta = (EpaMetaData)info as EpaMetaData; + file.Position = 2 == meta.Mode ? 0x18 : 0x10; + var reader = new Reader (file.AsStream, meta); reader.Unpack(); return ImageData.Create (meta, reader.Format, reader.Palette, reader.Data); } diff --git a/ArcFormats/Palette/ImagePGA.cs b/ArcFormats/Palette/ImagePGA.cs index c732871a..e42f748e 100644 --- a/ArcFormats/Palette/ImagePGA.cs +++ b/ArcFormats/Palette/ImagePGA.cs @@ -36,13 +36,13 @@ namespace GameRes.Formats.Palette public override uint Signature { get { return 0x50414750; } } // 'PGAP' public override bool CanWrite { get { return true; } } - public override ImageMetaData ReadMetaData (Stream stream) + public override ImageMetaData ReadMetaData (IBinaryStream stream) { using (var png = DeobfuscateStream (stream)) return base.ReadMetaData (png); } - public override ImageData Read (Stream stream, ImageMetaData info) + public override ImageData Read (IBinaryStream stream, ImageMetaData info) { using (var png = DeobfuscateStream (stream)) return base.Read (png, info); @@ -66,15 +66,16 @@ namespace GameRes.Formats.Palette public static readonly byte[] PngHeader = { 0x89, 0x50, 0x4E, 0x47, 0xD, 0xA, 0x1A, 0xA }; public static readonly byte[] PngFooter = { 0, 0, 0, 0, 0x49, 0x45, 0x4E, 0x44, 0xAE, 0x42, 0x60, 0x82 }; - Stream DeobfuscateStream (Stream stream) + IBinaryStream DeobfuscateStream (IBinaryStream stream) { var png_header = new byte[0x10]; stream.Read (png_header, 5, 11); System.Buffer.BlockCopy (PngHeader, 0, png_header, 0, 8); for (int i = 0; i < 8; ++i) png_header[i+8] ^= (byte)"PGAECODE"[i]; - var png_body = new StreamRegion (stream, 11, true); - return new PrefixStream (png_header, png_body); + var png_body = new StreamRegion (stream.AsStream, 11, true); + var pre = new PrefixStream (png_header, png_body); + return new BinaryStream (pre, stream.Name); } } } diff --git a/ArcFormats/Primel/ImageGBC.cs b/ArcFormats/Primel/ImageGBC.cs index 8b883859..628204e8 100644 --- a/ArcFormats/Primel/ImageGBC.cs +++ b/ArcFormats/Primel/ImageGBC.cs @@ -43,23 +43,21 @@ namespace GameRes.Formats.Primel public override string Description { get { return "Primel Adventure System image format"; } } public override uint Signature { get { return 0x46434247; } } // 'GBCF' - public override ImageMetaData ReadMetaData (Stream stream) + public override ImageMetaData ReadMetaData (IBinaryStream stream) { - var header = new byte[0x14]; - if (header.Length != stream.Read (header, 0, header.Length)) - return null; + var header = stream.ReadHeader (0x14); return new GbcMetaData { - Width = LittleEndian.ToUInt32 (header, 8), - Height = LittleEndian.ToUInt32 (header, 0xC), - BPP = LittleEndian.ToUInt16 (header, 0x10), - Flags = LittleEndian.ToUInt16 (header, 0x12), + Width = header.ToUInt32 (8), + Height = header.ToUInt32 (0xC), + BPP = header.ToUInt16 (0x10), + Flags = header.ToUInt16 (0x12), }; } - public override ImageData Read (Stream stream, ImageMetaData info) + public override ImageData Read (IBinaryStream stream, ImageMetaData info) { - using (var reader = new GbcReader (stream, (GbcMetaData)info)) + using (var reader = new GbcReader (stream.AsStream, (GbcMetaData)info)) { reader.Unpack(); return ImageData.Create (info, reader.Format, null, reader.Pixels); diff --git a/ArcFormats/Propeller/ArcMGR.cs b/ArcFormats/Propeller/ArcMGR.cs index 680acac8..1525d7ae 100644 --- a/ArcFormats/Propeller/ArcMGR.cs +++ b/ArcFormats/Propeller/ArcMGR.cs @@ -96,7 +96,7 @@ namespace GameRes.Formats.Propeller { var bmp = new byte[(entry as PackedEntry).UnpackedSize]; Decompress (input, bmp); - return new MemoryStream (bmp); + return new BinMemoryStream (bmp, entry.Name); } } diff --git a/ArcFormats/Propeller/ArcMPK.cs b/ArcFormats/Propeller/ArcMPK.cs index b20c86c2..39c23ffe 100644 --- a/ArcFormats/Propeller/ArcMPK.cs +++ b/ArcFormats/Propeller/ArcMPK.cs @@ -81,7 +81,7 @@ namespace GameRes.Formats.Propeller var data = arc.File.View.ReadBytes (entry.Offset, entry.Size); for (int i = 0; i < data.Length; ++i) data[i] ^= 0x88; - return new MemoryStream (data); + return new BinMemoryStream (data, entry.Name); } } } diff --git a/ArcFormats/Propeller/ImageMGR.cs b/ArcFormats/Propeller/ImageMGR.cs index d3a82dc4..de6af451 100644 --- a/ArcFormats/Propeller/ImageMGR.cs +++ b/ArcFormats/Propeller/ImageMGR.cs @@ -45,60 +45,57 @@ namespace GameRes.Formats.Propeller public override string Description { get { return "Propeller image format"; } } public override uint Signature { get { return 0; } } - public override ImageMetaData ReadMetaData (Stream stream) + public override ImageMetaData ReadMetaData (IBinaryStream stream) { - using (var reader = new ArcView.Reader (stream)) + int count = stream.ReadInt16(); + if (count <= 0 || count >= 0x100) + return null; + int offset; + if (count > 1) { - int count = reader.ReadInt16(); - if (count <= 0 || count >= 0x100) + offset = stream.ReadInt32(); + if (offset != 2 + count * 4) return null; - int offset; - if (count > 1) + } + else + offset = 2; + stream.Position = offset; + int unpacked_size = stream.ReadInt32(); + int packed_size = stream.ReadInt32(); + offset += 8; + if (offset + packed_size > stream.Length) + return null; + byte[] header = new byte[0x36]; + if (0x36 != MgrOpener.Decompress (stream.AsStream, header) + || header[0] != 'B' || header[1] != 'M') + return null; + using (var bmp = new BinMemoryStream (header, stream.Name)) + { + var info = Bmp.ReadMetaData (bmp); + if (null == info) + return null; + return new MgrMetaData { - offset = reader.ReadInt32(); - if (offset != 2 + count * 4) - return null; - } - else - offset = 2; - stream.Position = offset; - int unpacked_size = reader.ReadInt32(); - int packed_size = reader.ReadInt32(); - offset += 8; - if (offset + packed_size > stream.Length) - return null; - byte[] header = new byte[0x36]; - if (0x36 != MgrOpener.Decompress (stream, header) - || header[0] != 'B' || header[1] != 'M') - return null; - using (var bmp = new MemoryStream (header)) - { - var info = Bmp.ReadMetaData (bmp); - if (null == info) - return null; - return new MgrMetaData - { - Width = info.Width, - Height = info.Height, - BPP = info.BPP, - Offset = offset, - PackedSize = packed_size, - UnpackedSize = unpacked_size, - }; - } + Width = info.Width, + Height = info.Height, + BPP = info.BPP, + Offset = offset, + PackedSize = packed_size, + UnpackedSize = unpacked_size, + }; } } - public override ImageData Read (Stream stream, ImageMetaData info) + public override ImageData Read (IBinaryStream stream, ImageMetaData info) { var meta = (MgrMetaData)info; stream.Position = meta.Offset; var data = new byte[meta.UnpackedSize]; - if (data.Length != MgrOpener.Decompress (stream, data)) + if (data.Length != MgrOpener.Decompress (stream.AsStream, data)) throw new InvalidFormatException(); if (meta.BPP != 32) { - using (var bmp = new MemoryStream (data)) + using (var bmp = new BinMemoryStream (data, stream.Name)) return Bmp.Read (bmp, info); } // special case for 32bpp bitmaps with alpha-channel diff --git a/ArcFormats/Qlie/ArcABMP.cs b/ArcFormats/Qlie/ArcABMP.cs index 16d58d18..0e9af691 100644 --- a/ArcFormats/Qlie/ArcABMP.cs +++ b/ArcFormats/Qlie/ArcABMP.cs @@ -140,8 +140,8 @@ namespace GameRes.Formats.Qlie arc.File.View.Read (entry.Offset, packed, 0, entry.Size); var unpacked = PackOpener.Decompress (packed); if (null == unpacked) - return new MemoryStream (packed); - return new MemoryStream (unpacked); + unpacked = packed; + return new BinMemoryStream (unpacked, entry.Name); } static protected void DetectFileType (ArcView file, Entry entry) diff --git a/ArcFormats/Qlie/ArcQLIE.cs b/ArcFormats/Qlie/ArcQLIE.cs index 5f3469a9..b8d241ad 100644 --- a/ArcFormats/Qlie/ArcQLIE.cs +++ b/ArcFormats/Qlie/ArcQLIE.cs @@ -192,7 +192,7 @@ namespace GameRes.Formats.Qlie if (null == qent || null == qarc || (!qent.IsEncrypted && !qent.IsPacked)) return arc.File.CreateStream (entry.Offset, entry.Size); var data = ReadEntryBytes (arc.File, qent, qarc.Hash, qarc.GameKeyData); - return new MemoryStream (data); + return new BinMemoryStream (data, entry.Name); } private byte[] ReadEntryBytes (ArcView file, QlieEntry entry, uint hash, byte[] game_key) diff --git a/ArcFormats/Qlie/ImageDPNG.cs b/ArcFormats/Qlie/ImageDPNG.cs index e563f91c..432c0cdc 100644 --- a/ArcFormats/Qlie/ImageDPNG.cs +++ b/ArcFormats/Qlie/ImageDPNG.cs @@ -48,52 +48,46 @@ namespace GameRes.Formats.Qlie Extensions = new string[] { "png" }; } - public override ImageMetaData ReadMetaData (Stream stream) + public override ImageMetaData ReadMetaData (IBinaryStream file) { - stream.Position = 8; - using (var header = new ArcView.Reader (stream)) - { - var info = new DpngMetaData { BPP = 32 }; - info.TileCount = header.ReadInt32(); - if (info.TileCount <= 0) - return null; - info.Width = header.ReadUInt32(); - info.Height = header.ReadUInt32(); - return info; - } + file.Position = 8; + var info = new DpngMetaData { BPP = 32 }; + info.TileCount = file.ReadInt32(); + if (info.TileCount <= 0) + return null; + info.Width = file.ReadUInt32(); + info.Height = file.ReadUInt32(); + return info; } - public override ImageData Read (Stream stream, ImageMetaData info) + public override ImageData Read (IBinaryStream stream, ImageMetaData info) { var meta = (DpngMetaData)info; var bitmap = new WriteableBitmap ((int)info.Width, (int)info.Height, ImageData.DefaultDpiX, ImageData.DefaultDpiY, PixelFormats.Pbgra32, null); long next_tile = 0x14; - using (var dpng = new ArcView.Reader (stream)) + for (int i = 0; i < meta.TileCount; ++i) { - for (int i = 0; i < meta.TileCount; ++i) + stream.Position = next_tile; + int x = stream.ReadInt32(); + int y = stream.ReadInt32(); + int width = stream.ReadInt32(); + int height = stream.ReadInt32(); + uint size = stream.ReadUInt32(); + stream.Seek (8, SeekOrigin.Current); + next_tile = stream.Position + size; + if (0 == size) + continue; + using (var png = new StreamRegion (stream.AsStream, stream.Position, size, true)) { - stream.Position = next_tile; - int x = dpng.ReadInt32(); - int y = dpng.ReadInt32(); - int width = dpng.ReadInt32(); - int height = dpng.ReadInt32(); - uint size = dpng.ReadUInt32(); - stream.Seek (8, SeekOrigin.Current); - next_tile = stream.Position + size; - if (0 == size) - continue; - using (var png = new StreamRegion (stream, stream.Position, size, true)) - { - var decoder = new PngBitmapDecoder (png, - BitmapCreateOptions.None, BitmapCacheOption.OnLoad); - var frame = new FormatConvertedBitmap (decoder.Frames[0], PixelFormats.Pbgra32, null, 0); - int stride = frame.PixelWidth * 4; - var pixels = new byte[stride * frame.PixelHeight]; - frame.CopyPixels (pixels, stride, 0); - var rect = new Int32Rect (0, 0, frame.PixelWidth, frame.PixelHeight); - bitmap.WritePixels (rect, pixels, stride, x, y); - } + var decoder = new PngBitmapDecoder (png, + BitmapCreateOptions.None, BitmapCacheOption.OnLoad); + var frame = new FormatConvertedBitmap (decoder.Frames[0], PixelFormats.Pbgra32, null, 0); + int stride = frame.PixelWidth * 4; + var pixels = new byte[stride * frame.PixelHeight]; + frame.CopyPixels (pixels, stride, 0); + var rect = new Int32Rect (0, 0, frame.PixelWidth, frame.PixelHeight); + bitmap.WritePixels (rect, pixels, stride, x, y); } } bitmap.Freeze(); diff --git a/ArcFormats/RealLive/ArcG00.cs b/ArcFormats/RealLive/ArcG00.cs index 89d9d66c..8a734136 100644 --- a/ArcFormats/RealLive/ArcG00.cs +++ b/ArcFormats/RealLive/ArcG00.cs @@ -97,8 +97,7 @@ namespace GameRes.Formats.RealLive } byte[] bitmap; using (var input = file.CreateStream (index_offset)) - using (var bin = new BinaryReader (input)) - bitmap = G00Reader.LzDecompress (bin, 2, 1); + bitmap = G00Reader.LzDecompress (input, 2, 1); using (var input = new MemoryStream (bitmap)) using (var reader = new BinaryReader (input)) diff --git a/ArcFormats/RealLive/AudioNWA.cs b/ArcFormats/RealLive/AudioNWA.cs index 05d2eed6..60e1fb19 100644 --- a/ArcFormats/RealLive/AudioNWA.cs +++ b/ArcFormats/RealLive/AudioNWA.cs @@ -50,41 +50,39 @@ namespace GameRes.Formats.RealLive public override string Description { get { return "RealLive engine audio format"; } } public override uint Signature { get { return 0; } } - public override SoundInput TryOpen (Stream file) + public override SoundInput TryOpen (IBinaryStream file) { - var header = new byte[0x28]; - if (header.Length != file.Read (header, 0, header.Length)) - return null; - ushort channels = LittleEndian.ToUInt16 (header, 0); + var header = file.ReadHeader (0x28); + ushort channels = header.ToUInt16 (0); if (0 == channels || channels > 2) return null; - ushort bps = LittleEndian.ToUInt16 (header, 2); + ushort bps = header.ToUInt16 (2); if (bps != 8 && bps != 16) return null; var info = new NwaMetaData { - Compression = LittleEndian.ToInt32 (header, 8), - RunLengthEncoded = 0 != LittleEndian.ToInt32 (header, 0xC), - BlockCount = LittleEndian.ToInt32 (header, 0x10), - PcmSize = LittleEndian.ToInt32 (header, 0x14), - PackedSize = LittleEndian.ToInt32 (header, 0x18), - SampleCount = LittleEndian.ToInt32 (header, 0x1C), - BlockSize = LittleEndian.ToInt32 (header, 0x20), - FinalBlockSize = LittleEndian.ToInt32 (header, 0x24), + Compression = header.ToInt32 (8), + RunLengthEncoded = 0 != header.ToInt32 (0xC), + BlockCount = header.ToInt32 (0x10), + PcmSize = header.ToInt32 (0x14), + PackedSize = header.ToInt32 (0x18), + SampleCount = header.ToInt32 (0x1C), + BlockSize = header.ToInt32 (0x20), + FinalBlockSize = header.ToInt32 (0x24), }; if (info.PcmSize <= 0) return null; info.Format.FormatTag = 1; info.Format.Channels = channels; info.Format.BitsPerSample = bps; - info.Format.SamplesPerSecond = LittleEndian.ToUInt32 (header, 4); + info.Format.SamplesPerSecond = header.ToUInt32 (4); info.Format.BlockAlign = (ushort)(channels * bps/8); info.Format.AverageBytesPerSecond = info.Format.BlockAlign * info.Format.SamplesPerSecond; if (-1 == info.Compression) { if (info.PcmSize > file.Length - 0x2C) return null; - return new RawPcmInput (new StreamRegion (file, 0x2C, info.PcmSize), info.Format); + return new RawPcmInput (new StreamRegion (file.AsStream, 0x2C, info.PcmSize), info.Format); } if (info.Compression > 5) return null; @@ -103,7 +101,7 @@ namespace GameRes.Formats.RealLive internal sealed class NwaDecoder : IDisposable { - BinaryReader m_input; + IBinaryStream m_input; byte[] m_output; NwaMetaData m_info; short[] m_sample; @@ -112,20 +110,20 @@ namespace GameRes.Formats.RealLive public byte[] Output { get { return m_output; } } - public NwaDecoder (Stream input, NwaMetaData info) + public NwaDecoder (IBinaryStream input, NwaMetaData info) { - m_input = new ArcView.Reader (input); + m_input = input; m_info = info; m_output = new byte[m_info.PcmSize]; m_sample = new short[2]; - m_bits = new LsbBitStream (input, true); + m_bits = new LsbBitStream (input.AsStream, true); } int m_dst; public void Decode () { - m_input.BaseStream.Position = 0x2C; + m_input.Position = 0x2C; var offsets = new uint[m_info.BlockCount]; for (int i = 0; i < offsets.Length; ++i) offsets[i] = m_input.ReadUInt32(); @@ -133,10 +131,10 @@ namespace GameRes.Formats.RealLive m_dst = 0; for (int i = 0; i < offsets.Length-1; ++i) { - m_input.BaseStream.Position = offsets[i]; + m_input.Position = offsets[i]; DecodeBlock (m_info.BlockSize); } - m_input.BaseStream.Position = offsets[offsets.Length-1]; + m_input.Position = offsets[offsets.Length-1]; if (m_info.FinalBlockSize > 0) DecodeBlock (m_info.FinalBlockSize); else @@ -149,7 +147,7 @@ namespace GameRes.Formats.RealLive for (int c = 0; c < channel_count; ++c) { if (8 == m_info.Format.BitsPerSample) - m_sample[c] = m_input.ReadByte(); + m_sample[c] = m_input.ReadUInt8(); else m_sample[c] = m_input.ReadInt16(); } @@ -237,15 +235,8 @@ namespace GameRes.Formats.RealLive } #region IDisposable Members - bool _disposed = false; public void Dispose () { - if (!_disposed) - { - m_bits.Dispose(); - m_input.Dispose(); - _disposed = true; - } } #endregion } diff --git a/ArcFormats/RealLive/AudioOWP.cs b/ArcFormats/RealLive/AudioOWP.cs index cc65e38d..0796b235 100644 --- a/ArcFormats/RealLive/AudioOWP.cs +++ b/ArcFormats/RealLive/AudioOWP.cs @@ -36,11 +36,10 @@ namespace GameRes.Formats.RealLive public override string Description { get { return "RealLive engine obfuscated OGG audio"; } } public override uint Signature { get { return 0x6A5E5E76; } } // 'OggS' ^ 0x39 - public override SoundInput TryOpen (Stream file) + public override SoundInput TryOpen (IBinaryStream file) { - Stream input = new CryptoStream (file, new XorTransform (0x39), CryptoStreamMode.Read); - input = new SeekableStream (input); - return OggAudio.Instance.TryOpen (input); + var input = new XoredStream (file.AsStream, 0x39); + return new OggInput (input); } } } diff --git a/ArcFormats/RealLive/ImageG00.cs b/ArcFormats/RealLive/ImageG00.cs index f520ef11..53871a05 100644 --- a/ArcFormats/RealLive/ImageG00.cs +++ b/ArcFormats/RealLive/ImageG00.cs @@ -46,39 +46,36 @@ namespace GameRes.Formats.RealLive public override string Description { get { return "RealLive engine image format"; } } public override uint Signature { get { return 0; } } - public override ImageMetaData ReadMetaData (Stream stream) + public override ImageMetaData ReadMetaData (IBinaryStream file) { - int type = stream.ReadByte(); + int type = file.ReadByte(); if (type > 2) return null; - using (var reader = new ArcView.Reader (stream)) + uint width = file.ReadUInt16(); + uint height = file.ReadUInt16(); + if (0 == width || width > 0x8000 || 0 == height || height > 0x8000) + return null; + if (2 == type) { - uint width = reader.ReadUInt16(); - uint height = reader.ReadUInt16(); - if (0 == width || width > 0x8000 || 0 == height || height > 0x8000) + int count = file.ReadInt32(); + if (count <= 0 || count > 0x100) return null; - if (2 == type) - { - int count = reader.ReadInt32(); - if (count <= 0 || count > 0x100) - return null; - } - else - { - uint length = reader.ReadUInt32(); - if (length + 5 != stream.Length) - return null; - } - return new G00MetaData { - Width = width, - Height = height, - BPP = 1 == type ? 8 : 24, - Type = type, - }; } + else + { + uint length = file.ReadUInt32(); + if (length + 5 != file.Length) + return null; + } + return new G00MetaData { + Width = width, + Height = height, + BPP = 1 == type ? 8 : 24, + Type = type, + }; } - public override ImageData Read (Stream stream, ImageMetaData info) + public override ImageData Read (IBinaryStream stream, ImageMetaData info) { using (var reader = new G00Reader (stream, (G00MetaData)info)) { @@ -103,7 +100,7 @@ namespace GameRes.Formats.RealLive internal sealed class G00Reader : IDisposable { - BinaryReader m_input; + IBinaryStream m_input; byte[] m_output; int m_width; int m_height; @@ -113,17 +110,17 @@ namespace GameRes.Formats.RealLive public PixelFormat Format { get; private set; } public BitmapPalette Palette { get; private set; } - public G00Reader (Stream input, G00MetaData info) + public G00Reader (IBinaryStream input, G00MetaData info) { m_width = (int)info.Width; m_height = (int)info.Height; m_type = info.Type; - m_input = new ArcView.Reader (input); + m_input = input; } public void Unpack () { - m_input.BaseStream.Position = 5; + m_input.Position = 5; if (0 == m_type) UnpackV0(); else if (1 == m_type) @@ -165,7 +162,7 @@ namespace GameRes.Formats.RealLive tile.X = m_input.ReadInt32(); tile.Y = m_input.ReadInt32(); tiles.Add (tile); - m_input.BaseStream.Seek (0x10, SeekOrigin.Current); + m_input.Seek (0x10, SeekOrigin.Current); } using (var input = new MemoryStream (LzDecompress (m_input, 2, 1))) using (var reader = new BinaryReader (input)) @@ -211,7 +208,7 @@ namespace GameRes.Formats.RealLive } } - public static byte[] LzDecompress (BinaryReader input, int min_count, int bytes_pp) + public static byte[] LzDecompress (IBinaryStream input, int min_count, int bytes_pp) { int packed_size = input.ReadInt32() - 8; int output_size = input.ReadInt32(); @@ -223,7 +220,7 @@ namespace GameRes.Formats.RealLive bits >>= 1; if (1 == bits) { - bits = input.ReadByte() | 0x100; + bits = input.ReadUInt8() | 0x100; --packed_size; } if (0 != (bits & 1)) @@ -250,14 +247,8 @@ namespace GameRes.Formats.RealLive } #region IDisposable Members - bool _disposed = false; public void Dispose () { - if (!_disposed) - { - m_input.Dispose(); - _disposed = true; - } } #endregion } diff --git a/ArcFormats/RealLive/ImagePDT.cs b/ArcFormats/RealLive/ImagePDT.cs index 1639ae58..ec3ff60b 100644 --- a/ArcFormats/RealLive/ImagePDT.cs +++ b/ArcFormats/RealLive/ImagePDT.cs @@ -44,23 +44,21 @@ namespace GameRes.Formats.RealLive public override string Description { get { return "AVG32 engine image format"; } } public override uint Signature { get { return 0x31544450; } } // 'PDT1' - public override ImageMetaData ReadMetaData (Stream stream) + public override ImageMetaData ReadMetaData (IBinaryStream stream) { - var header = new byte[32]; - if (header.Length != stream.Read (header, 0, header.Length)) - return null; - if (!Binary.AsciiEqual (header, "PDT10\0")) + var header = stream.ReadHeader (32); + if (!header.AsciiEqual ("PDT10\0")) return null; return new PdtMetaData { - Width = LittleEndian.ToUInt32 (header, 0x0C), - Height = LittleEndian.ToUInt32 (header, 0x10), + Width = header.ToUInt32 (0x0C), + Height = header.ToUInt32 (0x10), BPP = 32, - AlphaOffset = LittleEndian.ToUInt32 (header, 0x1C), + AlphaOffset = header.ToUInt32 (0x1C), }; } - public override ImageData Read (Stream stream, ImageMetaData info) + public override ImageData Read (IBinaryStream stream, ImageMetaData info) { using (var reader = new PdtReader (stream, (PdtMetaData)info)) { @@ -77,7 +75,7 @@ namespace GameRes.Formats.RealLive internal sealed class PdtReader : IDisposable { - BinaryReader m_input; + IBinaryStream m_input; byte[] m_output; PdtMetaData m_info; @@ -85,9 +83,9 @@ namespace GameRes.Formats.RealLive public PixelFormat Format { get; private set; } public BitmapPalette Palette { get; private set; } - public PdtReader (Stream input, PdtMetaData info) + public PdtReader (IBinaryStream input, PdtMetaData info) { - m_input = new ArcView.Reader (input); + m_input = input; m_info = info; m_output = new byte[m_info.Width * m_info.Height * 4]; if (0 == m_info.AlphaOffset) @@ -98,11 +96,11 @@ namespace GameRes.Formats.RealLive public void Unpack () { - m_input.BaseStream.Position = 0x20; + m_input.Position = 0x20; Unpack24(); if (0 != m_info.AlphaOffset) { - m_input.BaseStream.Position = m_info.AlphaOffset; + m_input.Position = m_info.AlphaOffset; var alpha = Unpack8(); int src = 0; for (int i = 3; i < m_output.Length; i += 4) @@ -152,17 +150,17 @@ namespace GameRes.Formats.RealLive mask >>= 1; if (0 == mask) { - bits = m_input.ReadByte(); + bits = m_input.ReadUInt8(); mask = 0x80; } if (0 != (bits & mask)) { - output[dst++] = m_input.ReadByte(); + output[dst++] = m_input.ReadUInt8(); } else { - int count = 2 + m_input.ReadByte(); - int offset = 1 + m_input.ReadByte(); + int count = 2 + m_input.ReadUInt8(); + int offset = 1 + m_input.ReadUInt8(); Binary.CopyOverlapped (output, dst-offset, dst, count); dst += count; } @@ -171,14 +169,8 @@ namespace GameRes.Formats.RealLive } #region IDisposable Members - bool _disposed = false; public void Dispose () { - if (!_disposed) - { - m_input.Dispose(); - _disposed = true; - } } #endregion } diff --git a/ArcFormats/Resources/Formats.dat b/ArcFormats/Resources/Formats.dat index 08f64516..b6ef94e5 100644 Binary files a/ArcFormats/Resources/Formats.dat and b/ArcFormats/Resources/Formats.dat differ diff --git a/ArcFormats/RiddleSoft/ArcPAC.cs b/ArcFormats/RiddleSoft/ArcPAC.cs index 3cc1c673..026eaa1f 100644 --- a/ArcFormats/RiddleSoft/ArcPAC.cs +++ b/ArcFormats/RiddleSoft/ArcPAC.cs @@ -93,7 +93,7 @@ namespace GameRes.Formats.Riddle { var reader = new CmpReader (input, (int)entry.Size, unpacked_size); reader.Unpack(); - return new MemoryStream (reader.Data); + return new BinMemoryStream (reader.Data, entry.Name); } } } diff --git a/ArcFormats/RiddleSoft/ImageGCP.cs b/ArcFormats/RiddleSoft/ImageGCP.cs index 2298b5e3..29d830e1 100644 --- a/ArcFormats/RiddleSoft/ImageGCP.cs +++ b/ArcFormats/RiddleSoft/ImageGCP.cs @@ -53,16 +53,14 @@ namespace GameRes.Formats.Riddle throw new NotImplementedException ("GcpFormat.Write not implemented"); } - public override ImageMetaData ReadMetaData (Stream stream) + public override ImageMetaData ReadMetaData (IBinaryStream stream) { - var header = new byte[12]; - if (header.Length != stream.Read (header, 0, header.Length)) - return null; - int data_size = LittleEndian.ToInt32 (header, 4); - int pack_size = LittleEndian.ToInt32 (header, 8); + var header = stream.ReadHeader (12); + int data_size = header.ToInt32 (4); + int pack_size = header.ToInt32 (8); if (data_size < 54) return null; - var reader = new CmpReader (stream, pack_size, 0x22); // BMP header + var reader = new CmpReader (stream.AsStream, pack_size, 0x22); // BMP header reader.Unpack(); var bmp = reader.Data; if (bmp[0] != 'B' || bmp[1] != 'M') @@ -80,16 +78,13 @@ namespace GameRes.Formats.Riddle }; } - public override ImageData Read (Stream stream, ImageMetaData info) + public override ImageData Read (IBinaryStream stream, ImageMetaData info) { - var meta = info as GcpMetaData; - if (null == meta) - throw new ArgumentException ("GcpFormat.Read should be supplied with GcpMetaData", "info"); - + var meta = (GcpMetaData)info; stream.Position = 12; - var reader = new CmpReader (stream, meta.PackedSize, meta.DataSize); + var reader = new CmpReader (stream.AsStream, meta.PackedSize, meta.DataSize); reader.Unpack(); - using (var bmp = new MemoryStream (reader.Data, false)) + using (var bmp = new MemoryStream (reader.Data)) { var decoder = new BmpBitmapDecoder (bmp, BitmapCreateOptions.None, BitmapCacheOption.OnLoad); diff --git a/ArcFormats/Risa/ImageSYG.cs b/ArcFormats/Risa/ImageSYG.cs index d57d7b2c..ac65de79 100644 --- a/ArcFormats/Risa/ImageSYG.cs +++ b/ArcFormats/Risa/ImageSYG.cs @@ -42,22 +42,20 @@ namespace GameRes.Formats.WestVision public override string Description { get { return "Risa game platform system image"; } } public override uint Signature { get { return 0x47595324; } } // '$SYG' - public override ImageMetaData ReadMetaData (Stream stream) + public override ImageMetaData ReadMetaData (IBinaryStream stream) { - var header = new byte[0x20]; - if (header.Length != stream.Read (header, 0, header.Length)) - return null; - uint alpha_offset = LittleEndian.ToUInt32 (header, 0x1C); + var header = stream.ReadHeader (0x20); + uint alpha_offset = header.ToUInt32 (0x1C); return new SygMetaData { - Width = LittleEndian.ToUInt32 (header, 0x10), - Height = LittleEndian.ToUInt32 (header, 0x14), + Width = header.ToUInt32 (0x10), + Height = header.ToUInt32 (0x14), BPP = 0 == alpha_offset ? 24 : 32, AlphaOffset = alpha_offset, }; } - public override ImageData Read (Stream stream, ImageMetaData info) + public override ImageData Read (IBinaryStream stream, ImageMetaData info) { var meta = (SygMetaData)info; int pixel_count = (int)meta.Width * (int)meta.Height; diff --git a/ArcFormats/SHSystem/ArcHXP.cs b/ArcFormats/SHSystem/ArcHXP.cs index 910cdd13..175dccbf 100644 --- a/ArcFormats/SHSystem/ArcHXP.cs +++ b/ArcFormats/SHSystem/ArcHXP.cs @@ -129,7 +129,7 @@ namespace GameRes.Formats.SHSystem using (var reader = new ShsCompression (input)) { reader.Unpack (data); - return new MemoryStream (data); + return new BinMemoryStream (data, entry.Name); } } } diff --git a/ArcFormats/Sas5/ArcSec5.cs b/ArcFormats/Sas5/ArcSec5.cs index 38a6dcde..c2c9b3ef 100644 --- a/ArcFormats/Sas5/ArcSec5.cs +++ b/ArcFormats/Sas5/ArcSec5.cs @@ -73,7 +73,7 @@ namespace GameRes.Formats.Sas5 var code = new byte[entry.Size]; arc.File.View.Read (entry.Offset, code, 0, entry.Size); DecryptCodeSection (code); - return new MemoryStream (code); + return new BinMemoryStream (code, entry.Name); } static void DecryptCodeSection (byte[] code) diff --git a/ArcFormats/Sas5/ImageIAR.cs b/ArcFormats/Sas5/ImageIAR.cs index fcecf77b..1496bf51 100644 --- a/ArcFormats/Sas5/ImageIAR.cs +++ b/ArcFormats/Sas5/ImageIAR.cs @@ -65,32 +65,27 @@ namespace GameRes.Formats.Sas5 Extensions = new string[] { "" }; } - public override ImageMetaData ReadMetaData (Stream stream) + public override ImageMetaData ReadMetaData (IBinaryStream stream) { - var header = new byte[0x28]; - if (header.Length != stream.Read (header, 0, header.Length)) - return null; - if (!Binary.AsciiEqual (header, 4, "SAS5")) + var header = stream.ReadHeader (0x28); + if (!header.AsciiEqual (4, "SAS5")) return null; return new IarMetaData { - Width = LittleEndian.ToUInt32 (header, 0x08), - Height = LittleEndian.ToUInt32 (header, 0x0C), - OffsetX = -LittleEndian.ToInt32 (header, 0x10), - OffsetY = -LittleEndian.ToInt32 (header, 0x14), - BPP = LittleEndian.ToInt32 (header, 0x18), - Stride = LittleEndian.ToInt32 (header, 0x1C), - PaletteSize = LittleEndian.ToInt32 (header, 0x20), - ImageSize = LittleEndian.ToInt32 (header, 0x24), + Width = header.ToUInt32 (0x08), + Height = header.ToUInt32 (0x0C), + OffsetX = -header.ToInt32 (0x10), + OffsetY = -header.ToInt32 (0x14), + BPP = header.ToInt32 (0x18), + Stride = header.ToInt32 (0x1C), + PaletteSize = header.ToInt32 (0x20), + ImageSize = header.ToInt32 (0x24), }; } - public override ImageData Read (Stream stream, ImageMetaData info) + public override ImageData Read (IBinaryStream stream, ImageMetaData info) { - var meta = info as IarMetaData; - if (null == meta) - throw new ArgumentException ("IarFormat.Read should be supplied with IarMetaData", "info"); - + var meta = (IarMetaData)info; PixelFormat format; if (32 == meta.BPP) format = PixelFormats.Bgra32; @@ -104,9 +99,8 @@ namespace GameRes.Formats.Sas5 stream.Position = 0x28; BitmapPalette palette = null; if (meta.PaletteSize > 0) - palette = ReadPalette (stream, meta.PaletteSize); - var pixels = new byte[meta.ImageSize]; - stream.Read (pixels, 0, pixels.Length); + palette = ReadPalette (stream.AsStream, meta.PaletteSize); + var pixels = stream.ReadBytes (meta.ImageSize); return ImageData.Create (info, format, palette, pixels, meta.Stride); } diff --git a/ArcFormats/ScenePlayer/AudioPMW.cs b/ArcFormats/ScenePlayer/AudioPMW.cs index 7f391204..3b75ed13 100644 --- a/ArcFormats/ScenePlayer/AudioPMW.cs +++ b/ArcFormats/ScenePlayer/AudioPMW.cs @@ -37,13 +37,13 @@ namespace GameRes.Formats.ScenePlayer public override uint Signature { get { return 0; } } public override bool CanWrite { get { return true; } } - public override SoundInput TryOpen (Stream file) + public override SoundInput TryOpen (IBinaryStream file) { int first = file.ReadByte(); if ((first ^ 0x21) != 0x78) // doesn't look like zlib stream return null; file.Position = 0; - using (var input = new XoredStream (file, 0x21, true)) + using (var input = new XoredStream (file.AsStream, 0x21, true)) using (var zstream = new ZLibStream (input, CompressionMode.Decompress)) { SoundInput sound = null; diff --git a/ArcFormats/ScenePlayer/ImagePMP.cs b/ArcFormats/ScenePlayer/ImagePMP.cs index 6b15cc71..760a1eca 100644 --- a/ArcFormats/ScenePlayer/ImagePMP.cs +++ b/ArcFormats/ScenePlayer/ImagePMP.cs @@ -46,23 +46,25 @@ namespace GameRes.Formats.ScenePlayer base.Write (zstream, image); } - public override ImageMetaData ReadMetaData (Stream stream) + public override ImageMetaData ReadMetaData (IBinaryStream stream) { int first = stream.ReadByte() ^ 0x21; if (first != 0x78) // doesn't look like zlib stream return null; stream.Position = 0; - using (var input = new XoredStream (stream, 0x21, true)) + using (var input = new XoredStream (stream.AsStream, 0x21, true)) using (var zstream = new ZLibStream (input, CompressionMode.Decompress)) - return base.ReadMetaData (zstream); + using (var bmp = new BinaryStream (zstream, stream.Name)) + return base.ReadMetaData (bmp); } - public override ImageData Read (Stream stream, ImageMetaData info) + public override ImageData Read (IBinaryStream stream, ImageMetaData info) { - using (var input = new XoredStream (stream, 0x21, true)) + using (var input = new XoredStream (stream.AsStream, 0x21, true)) using (var zstream = new ZLibStream (input, CompressionMode.Decompress)) - return base.Read (zstream, info); + using (var bmp = new BinaryStream (zstream, stream.Name)) + return base.Read (bmp, info); } } } diff --git a/ArcFormats/ScrPlayer/ImageI.cs b/ArcFormats/ScrPlayer/ImageI.cs index b09541f4..94c2adeb 100644 --- a/ArcFormats/ScrPlayer/ImageI.cs +++ b/ArcFormats/ScrPlayer/ImageI.cs @@ -43,22 +43,20 @@ namespace GameRes.Formats.ScrPlayer Extensions = new string[] { "i" }; } - public override ImageMetaData ReadMetaData (Stream stream) + public override ImageMetaData ReadMetaData (IBinaryStream stream) { - var header = new byte[0x20]; - if (header.Length != stream.Read (header, 0, header.Length)) - return null; + var header = stream.ReadHeader (0x20); return new ImageMetaData { - Width = LittleEndian.ToUInt16 (header, 0xC), - Height = LittleEndian.ToUInt16 (header, 0xE), - BPP = LittleEndian.ToUInt16 (header, 0x10), + Width = header.ToUInt16 (0xC), + Height = header.ToUInt16 (0xE), + BPP = header.ToUInt16 (0x10), }; } - public override ImageData Read (Stream stream, ImageMetaData info) + public override ImageData Read (IBinaryStream stream, ImageMetaData info) { - using (var reader = new Img2Reader (stream, info)) + using (var reader = new Img2Reader (stream.AsStream, info)) { reader.Unpack(); return ImageData.Create (info, reader.Format, null, reader.Data); diff --git a/ArcFormats/ShiinaRio/ArcWARC.cs b/ArcFormats/ShiinaRio/ArcWARC.cs index e65882b1..e2b3479d 100644 --- a/ArcFormats/ShiinaRio/ArcWARC.cs +++ b/ArcFormats/ShiinaRio/ArcWARC.cs @@ -188,7 +188,7 @@ namespace GameRes.Formats.ShiinaRio // 椎名里緒 if (warc.Decoder.ExtraCrypt != null) warc.Decoder.ExtraCrypt.Decrypt (unpacked, 0, (uint)unpacked.Length, 0x204); } - return new MemoryStream (unpacked); + return new BinMemoryStream (unpacked, entry.Name); } delegate void UnpackMethod (byte[] input, byte[] output); diff --git a/ArcFormats/ShiinaRio/AudioOGV.cs b/ArcFormats/ShiinaRio/AudioOGV.cs index 79899712..6c95cab2 100644 --- a/ArcFormats/ShiinaRio/AudioOGV.cs +++ b/ArcFormats/ShiinaRio/AudioOGV.cs @@ -36,7 +36,7 @@ namespace GameRes.Formats.ShiinaRio public override string Description { get { return "ShiinaRio audio format (Ogg/Vorbis)"; } } public override uint Signature { get { return 0x0056474f; } } // 'OGV' - public override SoundInput TryOpen (Stream file) + public override SoundInput TryOpen (IBinaryStream file) { file.Position = 0xc; var header = new byte[8]; @@ -51,7 +51,7 @@ namespace GameRes.Formats.ShiinaRio if (!Binary.AsciiEqual (header, 0, "data")) return null; - var input = new StreamRegion (file, file.Position); + var input = new StreamRegion (file.AsStream, file.Position); return new OggInput (input); // input is left undisposed in case of exception. } diff --git a/ArcFormats/ShiinaRio/AudioPAD.cs b/ArcFormats/ShiinaRio/AudioPAD.cs index b18c8e93..86eefc6c 100644 --- a/ArcFormats/ShiinaRio/AudioPAD.cs +++ b/ArcFormats/ShiinaRio/AudioPAD.cs @@ -37,11 +37,9 @@ namespace GameRes.Formats.ShiinaRio public override string Description { get { return "ShiinaRio compressed audio format"; } } public override uint Signature { get { return 0x444150; } } // 'PAD' - public override SoundInput TryOpen (Stream file) + public override SoundInput TryOpen (IBinaryStream file) { - var wav_header = new byte[0x2c]; - if (0x2c != file.Read (wav_header, 0, 0x2c)) - return null; + var wav_header = file.ReadHeader (0x2c).ToArray(); int pcm_size = LittleEndian.ToInt32 (wav_header, 0x28); int channels = LittleEndian.ToUInt16 (wav_header, 0x16); wav_header[0] = (byte)'R'; @@ -50,7 +48,7 @@ namespace GameRes.Formats.ShiinaRio wav_header[3] = (byte)'F'; LittleEndian.Pack (pcm_size+0x24, wav_header, 4); - var decoder = new PadDecoder (file, pcm_size, channels); + var decoder = new PadDecoder (file.AsStream, pcm_size, channels); decoder.Unpack(); var data = new MemoryStream (decoder.Data, 0, pcm_size); var wav = new PrefixStream (wav_header, data); diff --git a/ArcFormats/ShiinaRio/ImageMI4.cs b/ArcFormats/ShiinaRio/ImageMI4.cs index 039eb77a..a8cb0fdf 100644 --- a/ArcFormats/ShiinaRio/ImageMI4.cs +++ b/ArcFormats/ShiinaRio/ImageMI4.cs @@ -37,23 +37,20 @@ namespace GameRes.Formats.ShiinaRio public override string Description { get { return "ShiinaRio image format"; } } public override uint Signature { get { return 0x3449414D; } } // 'MAI4' - public override ImageMetaData ReadMetaData (Stream stream) + public override ImageMetaData ReadMetaData (IBinaryStream file) { - stream.Seek (8, SeekOrigin.Current); - using (var input = new ArcView.Reader (stream)) + file.Position = 8; + uint width = file.ReadUInt32(); + uint height = file.ReadUInt32(); + return new ImageMetaData { - uint width = input.ReadUInt32(); - uint height = input.ReadUInt32(); - return new ImageMetaData - { - Width = width, - Height = height, - BPP = 24, - }; - } + Width = width, + Height = height, + BPP = 24, + }; } - public override ImageData Read (Stream stream, ImageMetaData info) + public override ImageData Read (IBinaryStream stream, ImageMetaData info) { stream.Position = 0x10; using (var reader = new Reader (stream, (int)info.Width, (int)info.Height)) @@ -70,15 +67,15 @@ namespace GameRes.Formats.ShiinaRio internal sealed class Reader : IDisposable { - BinaryReader m_input; + IBinaryStream m_input; byte[] m_output; int m_stride; public byte[] Data { get { return m_output; } } - public Reader (Stream file, int width, int height) + public Reader (IBinaryStream file, int width, int height) { - m_input = new ArcView.Reader (file); + m_input = file; m_stride = width * 3; m_output = new byte[m_stride*height]; } @@ -125,9 +122,9 @@ namespace GameRes.Formats.ShiinaRio { if (GetBit() != 0) { - b = m_input.ReadByte(); - g = m_input.ReadByte(); - r = m_input.ReadByte(); + b = m_input.ReadUInt8(); + g = m_input.ReadUInt8(); + r = m_input.ReadUInt8(); } else if (GetBit() != 0) { @@ -203,14 +200,8 @@ namespace GameRes.Formats.ShiinaRio } #region IDisposable Members - bool disposed = false; public void Dispose () { - if (!disposed) - { - m_input.Dispose (); - disposed = true; - } GC.SuppressFinalize (this); } #endregion diff --git a/ArcFormats/ShiinaRio/ImageS25.cs b/ArcFormats/ShiinaRio/ImageS25.cs index 1bf00f69..04e3b190 100644 --- a/ArcFormats/ShiinaRio/ImageS25.cs +++ b/ArcFormats/ShiinaRio/ImageS25.cs @@ -50,33 +50,30 @@ namespace GameRes.Formats.ShiinaRio // in current implementation, only the first frame is returned. // per-frame access is provided by S25Opener class. - public override ImageMetaData ReadMetaData (Stream stream) + public override ImageMetaData ReadMetaData (IBinaryStream file) { - using (var input = new ArcView.Reader (stream)) - { - input.ReadUInt32(); - int count = input.ReadInt32(); - if (count < 0 || count > 0xfffff) - return null; - uint first_offset = 0; - for (int i = 0; i < count && 0 == first_offset; ++i) - first_offset = input.ReadUInt32(); - if (0 == first_offset) - return null; - input.BaseStream.Position = first_offset; - var info = new S25MetaData(); - info.Width = input.ReadUInt32(); - info.Height = input.ReadUInt32(); - info.OffsetX = input.ReadInt32(); - info.OffsetY = input.ReadInt32(); - info.FirstOffset = first_offset+0x14; - info.Incremental = 0 != (input.ReadUInt32() & 0x80000000u); - info.BPP = 32; - return info; - } + file.Position = 4; + int count = file.ReadInt32(); + if (count < 0 || count > 0xfffff) + return null; + uint first_offset = 0; + for (int i = 0; i < count && 0 == first_offset; ++i) + first_offset = file.ReadUInt32(); + if (0 == first_offset) + return null; + file.Position = first_offset; + var info = new S25MetaData(); + info.Width = file.ReadUInt32(); + info.Height = file.ReadUInt32(); + info.OffsetX = file.ReadInt32(); + info.OffsetY = file.ReadInt32(); + info.FirstOffset = first_offset+0x14; + info.Incremental = 0 != (file.ReadUInt32() & 0x80000000u); + info.BPP = 32; + return info; } - public override ImageData Read (Stream stream, ImageMetaData info) + public override ImageData Read (IBinaryStream stream, ImageMetaData info) { using (var reader = new Reader (stream, (S25MetaData)info)) { @@ -90,9 +87,9 @@ namespace GameRes.Formats.ShiinaRio throw new NotImplementedException ("S25Format.Write not implemented"); } - internal class Reader : IDisposable + internal sealed class Reader : IDisposable { - BinaryReader m_input; + IBinaryStream m_input; int m_width; int m_height; uint m_origin; @@ -101,19 +98,19 @@ namespace GameRes.Formats.ShiinaRio public byte[] Data { get { return m_output; } } - public Reader (Stream file, S25MetaData info) + public Reader (IBinaryStream file, S25MetaData info) { m_width = (int)info.Width; m_height = (int)info.Height; m_output = new byte[m_width * m_height * 4]; - m_input = new ArcView.Reader (file); + m_input = file; m_origin = info.FirstOffset; m_incremental = info.Incremental; } public byte[] Unpack () { - m_input.BaseStream.Position = m_origin; + m_input.Position = m_origin; if (m_incremental) return UnpackIncremental(); var rows = new uint[m_height]; @@ -124,7 +121,7 @@ namespace GameRes.Formats.ShiinaRio for (int y = 0; y < m_height && dst < m_output.Length; ++y) { uint row_pos = rows[y]; - m_input.BaseStream.Position = row_pos; + m_input.Position = row_pos; int row_length = m_input.ReadUInt16(); row_pos += 2; if (0 != (row_pos & 1)) @@ -142,7 +139,7 @@ namespace GameRes.Formats.ShiinaRio void UpdateRepeatCount (Dictionary rows_count) { - m_input.BaseStream.Position = 4; + m_input.Position = 4; int count = m_input.ReadInt32(); var frames = new List (count); for (int i = 0; i < count; ++i) @@ -155,9 +152,9 @@ namespace GameRes.Formats.ShiinaRio { if (offset+0x14 == m_origin) continue; - m_input.BaseStream.Position = offset+4; + m_input.Position = offset+4; int height = m_input.ReadInt32(); - m_input.BaseStream.Position = offset+0x14; + m_input.Position = offset+0x14; for (int i = 0; i < height; ++i) { var row_offset = m_input.ReadUInt32(); @@ -286,7 +283,7 @@ namespace GameRes.Formats.ShiinaRio byte[] ReadLine (uint offset, int repeat) { - m_input.BaseStream.Position = offset; + m_input.Position = offset; int row_length = m_input.ReadUInt16(); if (0 != (offset & 1)) { @@ -355,23 +352,10 @@ namespace GameRes.Formats.ShiinaRio } #region IDisposable Members - bool disposed = false; - public void Dispose () { - Dispose (true); GC.SuppressFinalize (this); } - - protected virtual void Dispose (bool disposing) - { - if (!disposed) - { - if (disposing) - m_input.Dispose(); - disposed = true; - } - } #endregion } } diff --git a/ArcFormats/Silky/ArcIFL.cs b/ArcFormats/Silky/ArcIFL.cs index 45801db0..a7d4f5b5 100644 --- a/ArcFormats/Silky/ArcIFL.cs +++ b/ArcFormats/Silky/ArcIFL.cs @@ -78,7 +78,7 @@ namespace GameRes.Formats.Silky { lzss.FrameFill = 0x20; lzss.Unpack(); - return new MemoryStream (lzss.Data); + return new BinMemoryStream (lzss.Data, entry.Name); } } } diff --git a/ArcFormats/Silky/ImageAKB.cs b/ArcFormats/Silky/ImageAKB.cs index d1fab0a1..87650817 100644 --- a/ArcFormats/Silky/ImageAKB.cs +++ b/ArcFormats/Silky/ImageAKB.cs @@ -46,30 +46,27 @@ namespace GameRes.Formats.Silky public override string Description { get { return "AI6WIN engine image format"; } } public override uint Signature { get { return 0x20424B41; } } // 'AKB ' - public override ImageMetaData ReadMetaData (Stream stream) + public override ImageMetaData ReadMetaData (IBinaryStream file) { - using (var reader = new ArcView.Reader (stream)) - { - reader.ReadInt32(); - var info = new AkbMetaData(); - info.Width = reader.ReadUInt16(); - info.Height = reader.ReadUInt16(); - int flags = reader.ReadInt32() & 0xFFFF; - info.BPP = 0 == flags ? 32 : 24; - info.Background = reader.ReadBytes (4); - info.OffsetX = reader.ReadInt32(); - info.OffsetY = reader.ReadInt32(); - info.InnerWidth = reader.ReadInt32() - info.OffsetX; - info.InnerHeight = reader.ReadInt32() - info.OffsetY; - if (info.InnerWidth > info.Width || info.InnerHeight > info.Height) - return null; - return info; - } + file.ReadInt32(); + var info = new AkbMetaData(); + info.Width = file.ReadUInt16(); + info.Height = file.ReadUInt16(); + int flags = file.ReadInt32() & 0xFFFF; + info.BPP = 0 == flags ? 32 : 24; + info.Background = file.ReadBytes (4); + info.OffsetX = file.ReadInt32(); + info.OffsetY = file.ReadInt32(); + info.InnerWidth = file.ReadInt32() - info.OffsetX; + info.InnerHeight = file.ReadInt32() - info.OffsetY; + if (info.InnerWidth > info.Width || info.InnerHeight > info.Height) + return null; + return info; } - public override ImageData Read (Stream stream, ImageMetaData info) + public override ImageData Read (IBinaryStream file, ImageMetaData info) { - var reader = new AkbReader (stream, (AkbMetaData)info); + var reader = new AkbReader (file.AsStream, (AkbMetaData)info); var image = reader.Unpack(); return ImageData.Create (info, reader.Format, null, image, reader.Stride); } diff --git a/ArcFormats/Silky/ImageGRD.cs b/ArcFormats/Silky/ImageGRD.cs index 05066aca..a902cd2c 100644 --- a/ArcFormats/Silky/ImageGRD.cs +++ b/ArcFormats/Silky/ImageGRD.cs @@ -52,12 +52,12 @@ namespace GameRes.Formats.Silky throw new NotImplementedException ("GrdFormat.Write not implemented"); } - public override ImageMetaData ReadMetaData (Stream stream) + public override ImageMetaData ReadMetaData (IBinaryStream stream) { stream.Seek (4, SeekOrigin.Current); - int data_size = stream.ReadByte() | stream.ReadByte() << 8 | stream.ReadByte() << 16 | stream.ReadByte() << 24; + int data_size = stream.ReadInt32(); stream.Seek (4, SeekOrigin.Current); - using (var reader = new Reader (stream, 0x22)) // BMP header + using (var reader = new Reader (stream.AsStream, 0x22)) // BMP header { reader.Unpack(); var bmp = reader.Data; @@ -76,18 +76,15 @@ namespace GameRes.Formats.Silky } } - public override ImageData Read (Stream stream, ImageMetaData info) + public override ImageData Read (IBinaryStream stream, ImageMetaData info) { - var meta = info as GrdMetaData; - if (null == meta) - throw new ArgumentException ("GrdFormat.Read should be supplied with GrdMetaData", "info"); - + var meta = (GrdMetaData)info; stream.Position = 12; - using (var reader = new Reader (stream, meta.DataSize)) + using (var reader = new Reader (stream.AsStream, meta.DataSize)) { reader.Unpack(); byte[] pixels = reader.Data; - using (var bmp = new MemoryStream (reader.Data, false)) + using (var bmp = new MemoryStream (reader.Data)) { var decoder = new BmpBitmapDecoder (bmp, BitmapCreateOptions.None, BitmapCacheOption.OnLoad); diff --git a/ArcFormats/Silky/ImageIGF.cs b/ArcFormats/Silky/ImageIGF.cs index 1d806b50..09f07d54 100644 --- a/ArcFormats/Silky/ImageIGF.cs +++ b/ArcFormats/Silky/ImageIGF.cs @@ -46,15 +46,13 @@ namespace GameRes.Formats.Silky public override string Description { get { return "Silky's image format"; } } public override uint Signature { get { return 0x5355455Au; } } // 'ZEUS' - public override ImageMetaData ReadMetaData (Stream stream) + public override ImageMetaData ReadMetaData (IBinaryStream stream) { - var header = new byte[0x14]; - if (header.Length != stream.Read (header, 0, header.Length)) - return null; - uint width = LittleEndian.ToUInt32 (header, 4); - uint height = LittleEndian.ToUInt32 (header, 8); - int unpacked_size = LittleEndian.ToInt32 (header, 0xC); - int flags = LittleEndian.ToInt32 (header, 0x10); + var header = stream.ReadHeader (0x14); + uint width = header.ToUInt32 (4); + uint height = header.ToUInt32 (8); + int unpacked_size = header.ToInt32 (0xC); + int flags = header.ToInt32 (0x10); int bpp = flags & 0xff; if (0 == bpp) bpp = 32; @@ -68,11 +66,9 @@ namespace GameRes.Formats.Silky }; } - public override ImageData Read (Stream stream, ImageMetaData info) + public override ImageData Read (IBinaryStream stream, ImageMetaData info) { - var meta = info as IgfMetaData; - if (null == meta) - throw new ArgumentException ("IgfFormat.Read should be supplied with IgfMetaData", "info"); + var meta = (IgfMetaData)info as IgfMetaData; int stride = (int)info.Width*info.BPP/8; stream.Position = 0x14; @@ -80,7 +76,7 @@ namespace GameRes.Formats.Silky if (meta.IsPacked) { int in_size = (int)(stream.Length - 0x14); - using (var lzss = new LzssReader (stream, in_size, meta.UnpackedSize)) + using (var lzss = new LzssReader (stream.AsStream, in_size, meta.UnpackedSize)) { lzss.FrameFill = 0x20; lzss.Unpack(); diff --git a/ArcFormats/Silky/ImageMFG.cs b/ArcFormats/Silky/ImageMFG.cs index 12e7cf43..d0cbf3d6 100644 --- a/ArcFormats/Silky/ImageMFG.cs +++ b/ArcFormats/Silky/ImageMFG.cs @@ -58,49 +58,40 @@ namespace GameRes.Formats.Silky throw new NotImplementedException ("MfgFormat.Write not implemented"); } - public override ImageMetaData ReadMetaData (Stream stream) + public override ImageMetaData ReadMetaData (IBinaryStream file) { - using (var file = new ArcView.Reader (stream)) + file.Position = 3; + byte id = file.ReadUInt8(); + uint data_size = file.ReadUInt32(); + uint width = file.ReadUInt32(); + uint height = file.ReadUInt32(); + uint stride = file.ReadUInt32(); + if (stride < width) + throw new NotSupportedException(); + if (stride*height != data_size) + return null; + return new MfgMetaData { - stream.Seek (3, SeekOrigin.Current); - byte id = file.ReadByte(); - uint data_size = file.ReadUInt32(); - uint width = file.ReadUInt32(); - uint height = file.ReadUInt32(); - uint stride = file.ReadUInt32(); - if (stride < width) - throw new NotSupportedException(); - if (stride*height != data_size) - return null; - return new MfgMetaData - { - Width = width, - Height = height, - BPP = (int)(stride*8/width), - Type = id, - Stride = (int)stride, - }; - } + Width = width, + Height = height, + BPP = (int)(stride*8/width), + Type = id, + Stride = (int)stride, + }; } - public override ImageData Read (Stream stream, ImageMetaData info) + public override ImageData Read (IBinaryStream file, ImageMetaData info) { - var meta = info as MfgMetaData; - if (null == meta) - throw new ArgumentException ("MfgFormat.Read should be supplied with MfgMetaData", "info"); - - stream.Position = 0x14; + var meta = (MfgMetaData)info; + file.Position = 0x14; if ('_' != meta.Type) - using (var file = new ArcView.Reader (stream)) + for (uint i = 0; i < meta.Height; ++i) { - for (uint i = 0; i < meta.Height; ++i) - { - uint n = file.ReadUInt32(); - file.BaseStream.Seek (n*8, SeekOrigin.Current); - } + uint n = file.ReadUInt32(); + file.Seek (n*8, SeekOrigin.Current); } byte[] pixels = new byte[meta.Stride*info.Height]; - if (pixels.Length != stream.Read (pixels, 0, pixels.Length)) + if (pixels.Length != file.Read (pixels, 0, pixels.Length)) throw new InvalidFormatException ("Unexpected end of file"); PixelFormat format; if (24 == meta.BPP) diff --git a/ArcFormats/Silky/ImageMSK.cs b/ArcFormats/Silky/ImageMSK.cs index 39328a23..bc74b0e5 100644 --- a/ArcFormats/Silky/ImageMSK.cs +++ b/ArcFormats/Silky/ImageMSK.cs @@ -43,24 +43,22 @@ namespace GameRes.Formats.Silky Extensions = new string[] { "msk" }; } - public override ImageMetaData ReadMetaData (Stream stream) + public override ImageMetaData ReadMetaData (IBinaryStream stream) { - var header = new byte[12]; - if (12 != stream.Read (header, 0, 12)) - return null; + var header = stream.ReadHeader (12); return new ImageMetaData { - Width = LittleEndian.ToUInt16 (header, 8), - Height = LittleEndian.ToUInt16 (header, 10), - OffsetX = LittleEndian.ToInt16 (header, 4), - OffsetY = LittleEndian.ToInt16 (header, 6), + Width = header.ToUInt16 (8), + Height = header.ToUInt16 (10), + OffsetX = header.ToInt16 (4), + OffsetY = header.ToInt16 (6), BPP = 8, }; } - public override ImageData Read (Stream stream, ImageMetaData info) + public override ImageData Read (IBinaryStream stream, ImageMetaData info) { - using (var reader = new RmskReader (stream, info)) + using (var reader = new RmskReader (stream.AsStream, info)) { reader.Unpack(); return ImageData.CreateFlipped (info, PixelFormats.Gray8, null, reader.Data, (int)info.Width); diff --git a/ArcFormats/Silky/ImageZIT.cs b/ArcFormats/Silky/ImageZIT.cs index 613b5f69..1e324aa2 100644 --- a/ArcFormats/Silky/ImageZIT.cs +++ b/ArcFormats/Silky/ImageZIT.cs @@ -49,22 +49,20 @@ namespace GameRes.Formats.Silky Signatures = new uint[] { 0x1803545A, 0x2084545A, 0x8803545A }; } - public override ImageMetaData ReadMetaData (Stream stream) + public override ImageMetaData ReadMetaData (IBinaryStream stream) { - var header = new byte[0x10]; - if (header.Length != stream.Read (header, 0, header.Length)) - return null; + var header = stream.ReadHeader (0x10); return new ZitMetaData { - Type = LittleEndian.ToUInt16 (header, 2), - Width = LittleEndian.ToUInt16 (header, 8), - Height = LittleEndian.ToUInt16 (header, 10), + Type = header.ToUInt16 (2), + Width = header.ToUInt16 (8), + Height = header.ToUInt16 (10), BPP = 32, - Colors = LittleEndian.ToUInt16 (header, 4), + Colors = header.ToUInt16 (4), }; } - public override ImageData Read (Stream stream, ImageMetaData info) + public override ImageData Read (IBinaryStream stream, ImageMetaData info) { using (var reader = new ZitReader (stream, (ZitMetaData)info)) { @@ -81,7 +79,7 @@ namespace GameRes.Formats.Silky internal class ZitReader : IDisposable { - BinaryReader m_input; + IBinaryStream m_input; byte[] m_output; int m_width; int m_height; @@ -90,9 +88,9 @@ namespace GameRes.Formats.Silky public byte[] Data { get { return m_output; } } - public ZitReader (Stream input, ZitMetaData info) + public ZitReader (IBinaryStream input, ZitMetaData info) { - m_input = new ArcView.Reader (input); + m_input = input; m_width = (int)info.Width; m_height = (int)info.Height; m_type = info.Type; @@ -102,7 +100,7 @@ namespace GameRes.Formats.Silky public void Unpack () { - m_input.BaseStream.Position = 0x10; + m_input.Position = 0x10; switch (m_type) { case 0x1803: Unpack1(); break; @@ -118,9 +116,9 @@ namespace GameRes.Formats.Silky int dst = 0; while (dst < m_output.Length) { - byte b = m_input.ReadByte(); - byte g = m_input.ReadByte(); - byte r = m_input.ReadByte(); + byte b = m_input.ReadUInt8(); + byte g = m_input.ReadUInt8(); + byte r = m_input.ReadUInt8(); if (b != 0 || g != 0xFF || r != 0) { m_output[dst++] = b; @@ -167,14 +165,8 @@ namespace GameRes.Formats.Silky } #region IDisposable Members - bool _disposed = false; public void Dispose () { - if (!_disposed) - { - m_input.Dispose(); - _disposed = true; - } } #endregion } diff --git a/ArcFormats/Slg/AudioVOI.cs b/ArcFormats/Slg/AudioVOI.cs index 41b60a18..fc1426b5 100644 --- a/ArcFormats/Slg/AudioVOI.cs +++ b/ArcFormats/Slg/AudioVOI.cs @@ -35,7 +35,7 @@ namespace GameRes.Formats.Slg public override string Description { get { return "SLG system obfuscated Ogg audio"; } } public override uint Signature { get { return 0; } } // 'OggS' - public override SoundInput TryOpen (Stream file) + public override SoundInput TryOpen (IBinaryStream file) { file.Position = 0x1E; int offset = file.ReadByte(); @@ -45,7 +45,7 @@ namespace GameRes.Formats.Slg if (!(file.ReadByte() == 'O' && file.ReadByte() == 'g' && file.ReadByte() == 'g' && file.ReadByte() == 'S')) return null; - return new OggInput (new StreamRegion (file, 0x20+offset)); + return new OggInput (new StreamRegion (file.AsStream, 0x20+offset)); } } } diff --git a/ArcFormats/Slg/ImageALB.cs b/ArcFormats/Slg/ImageALB.cs index f6b00766..d50a80c9 100644 --- a/ArcFormats/Slg/ImageALB.cs +++ b/ArcFormats/Slg/ImageALB.cs @@ -47,24 +47,23 @@ namespace GameRes.Formats.Slg static readonly Lazy Dds = new Lazy (() => ImageFormat.FindByTag ("DDS")); - public override ImageMetaData ReadMetaData (Stream stream) + public override ImageMetaData ReadMetaData (IBinaryStream stream) { - var header = new byte[0x10]; - if (0x10 != stream.Read (header, 0, 0x10)) - return null; - int unpacked_size = LittleEndian.ToInt32 (header, 8); + var header = stream.ReadHeader (0x10); + int unpacked_size = header.ToInt32 (8); using (var alb = new AlbStream (stream, unpacked_size)) - using (var file = new SeekableStream (alb)) + using (var s = new SeekableStream (alb)) + using (var file = new BinaryStream (s, stream.Name)) { - uint signature = FormatCatalog.ReadSignature (file); - file.Position = 0; + uint signature = file.Signature; ImageFormat format; - if (ImageFormat.Png.Signature == signature) + if (Png.Signature == signature) format = ImageFormat.Png; else if (Dds.Value.Signature == signature) format = Dds.Value; else format = ImageFormat.Jpeg; + file.Position = 0; var info = format.ReadMetaData (file); if (null == info) return null; @@ -82,12 +81,13 @@ namespace GameRes.Formats.Slg } } - public override ImageData Read (Stream stream, ImageMetaData info) + public override ImageData Read (IBinaryStream stream, ImageMetaData info) { var meta = (AlbMetaData)info; stream.Position = 0x10; using (var alb = new AlbStream (stream, meta.UnpackedSize)) - using (var file = new SeekableStream (alb)) + using (var s = new SeekableStream (alb)) + using (var file = new BinaryStream (s, stream.Name)) return meta.Format.Read (file, meta.Info); } @@ -99,7 +99,7 @@ namespace GameRes.Formats.Slg internal class AlbStream : Stream { - BinaryReader m_input; + IBinaryStream m_input; int m_unpacked_size; IEnumerator m_iterator; byte[] m_output; @@ -110,9 +110,9 @@ namespace GameRes.Formats.Slg public override bool CanSeek { get { return false; } } public override bool CanWrite { get { return false; } } - public AlbStream (Stream source, int unpacked_size) + public AlbStream (IBinaryStream source, int unpacked_size) { - m_input = new ArcView.Reader (source); + m_input = source; m_unpacked_size = unpacked_size; m_iterator = ReadSeq(); } @@ -132,7 +132,7 @@ namespace GameRes.Formats.Slg IEnumerator ReadSeq () { var stack = new byte[256]; - while (m_input.PeekChar() != -1) + while (m_input.PeekByte() != -1) { int packed_size = UnpackDict(); int src = 0; @@ -146,7 +146,7 @@ namespace GameRes.Formats.Slg } else if (src < packed_size) { - s = m_input.ReadByte(); + s = m_input.ReadUInt8(); src++; } else @@ -175,16 +175,16 @@ namespace GameRes.Formats.Slg throw new InvalidFormatException(); int table_size = m_input.ReadUInt16(); int packed_size = m_input.ReadUInt16(); - bool is_packed = m_input.ReadByte() != 0; - byte marker = m_input.ReadByte(); + bool is_packed = m_input.ReadUInt8() != 0; + byte marker = m_input.ReadUInt8(); if (is_packed) { for (int i = 0; i < 256; ) { - byte b = m_input.ReadByte(); + byte b = m_input.ReadUInt8(); if (marker == b) { - int count = m_input.ReadByte(); + int count = m_input.ReadUInt8(); for (int j = 0; j < count; ++j) { m_dict[i,0] = (byte)i; @@ -195,7 +195,7 @@ namespace GameRes.Formats.Slg else { m_dict[i,0] = b; - m_dict[i,1] = m_input.ReadByte(); + m_dict[i,1] = m_input.ReadUInt8(); ++i; } } @@ -204,8 +204,8 @@ namespace GameRes.Formats.Slg { for (int i = 0; i < 256; ++i) { - m_dict[i,0] = m_input.ReadByte(); - m_dict[i,1] = m_input.ReadByte(); + m_dict[i,0] = m_input.ReadUInt8(); + m_dict[i,1] = m_input.ReadUInt8(); } } return packed_size; @@ -252,7 +252,6 @@ namespace GameRes.Formats.Slg { if (disposing) { - m_input.Dispose(); m_iterator.Dispose(); } _alb_disposed = true; diff --git a/ArcFormats/Slg/ImageTIG.cs b/ArcFormats/Slg/ImageTIG.cs index c5c7d3d8..f952830c 100644 --- a/ArcFormats/Slg/ImageTIG.cs +++ b/ArcFormats/Slg/ImageTIG.cs @@ -37,17 +37,19 @@ namespace GameRes.Formats.Slg public override uint Signature { get { return 0x7CF3C28B; } } public override bool CanWrite { get { return false; } } - public override ImageMetaData ReadMetaData (Stream stream) + public override ImageMetaData ReadMetaData (IBinaryStream stream) { - using (var proxy = new ProxyStream (stream, true)) - using (var input = new CryptoStream (proxy, new TigTransform(), CryptoStreamMode.Read)) + using (var proxy = new ProxyStream (stream.AsStream, true)) + using (var crypt = new CryptoStream (proxy, new TigTransform(), CryptoStreamMode.Read)) + using (var input = new BinaryStream (crypt, stream.Name)) return base.ReadMetaData (input); } - public override ImageData Read (Stream stream, ImageMetaData info) + public override ImageData Read (IBinaryStream stream, ImageMetaData info) { - using (var proxy = new ProxyStream (stream, true)) - using (var input = new CryptoStream (proxy, new TigTransform(), CryptoStreamMode.Read)) + using (var proxy = new ProxyStream (stream.AsStream, true)) + using (var crypt = new CryptoStream (proxy, new TigTransform(), CryptoStreamMode.Read)) + using (var input = new BinaryStream (crypt, stream.Name)) return base.Read (input, info); } @@ -59,7 +61,7 @@ namespace GameRes.Formats.Slg internal sealed class TigTransform : ICryptoTransform { - const int BlockSize = 256; + const int BlockSize = 1; uint m_key; public bool CanReuseTransform { get { return true; } } diff --git a/ArcFormats/Softpal/ArcPAC.cs b/ArcFormats/Softpal/ArcPAC.cs index 7de7c6c3..e40d450b 100644 --- a/ArcFormats/Softpal/ArcPAC.cs +++ b/ArcFormats/Softpal/ArcPAC.cs @@ -107,7 +107,7 @@ namespace GameRes.Formats.Softpal } } } - return new MemoryStream (data); + return new BinMemoryStream (data, entry.Name); } } diff --git a/ArcFormats/Softpal/AudioBGM.cs b/ArcFormats/Softpal/AudioBGM.cs index 0b73772e..0431cc35 100644 --- a/ArcFormats/Softpal/AudioBGM.cs +++ b/ArcFormats/Softpal/AudioBGM.cs @@ -31,7 +31,7 @@ using GameRes.Utility; namespace GameRes.Formats.Softpal { [Export(typeof(AudioFormat))] - public class BgmAudio : OggAudio + public class BgmAudio : AudioFormat { public override string Tag { get { return "BGM/SOFTPAL"; } } public override string Description { get { return "Softpal BGM format (Ogg/Vorbis)"; } } @@ -42,14 +42,12 @@ namespace GameRes.Formats.Softpal Extensions = new string[] { "ogg" }; } - public override SoundInput TryOpen (Stream file) + public override SoundInput TryOpen (IBinaryStream file) { - var header = new byte[0x10]; // header contains music loop timing - if (0x10 != file.Read (header, 0, 0x10)) + var header = file.ReadHeader (0x10); // header contains music loop timing + if (!header.AsciiEqual (0xC, "OggS")) return null; - if (!Binary.AsciiEqual (header, 0xC, "OggS")) - return null; - var input = new StreamRegion (file, 12); + var input = new StreamRegion (file.AsStream, 12); return new OggInput (input); // input is [intentionally] left undisposed in case of exception. } diff --git a/ArcFormats/Softpal/ImageBPIC.cs b/ArcFormats/Softpal/ImageBPIC.cs index 54be2259..2e9c221e 100644 --- a/ArcFormats/Softpal/ImageBPIC.cs +++ b/ArcFormats/Softpal/ImageBPIC.cs @@ -37,23 +37,21 @@ namespace GameRes.Formats.Softpal public override string Description { get { return "Softpal engine image format"; } } public override uint Signature { get { return 0x43495042; } } // 'BPIC' - public override ImageMetaData ReadMetaData (Stream stream) + public override ImageMetaData ReadMetaData (IBinaryStream stream) { - var header = new byte[0x10]; - if (header.Length != stream.Read (header, 0, header.Length)) - return null; - int pixel_size = LittleEndian.ToInt32 (header, 12); + var header = stream.ReadHeader (0x10); + int pixel_size = header.ToInt32 (12); if (pixel_size != 4 && pixel_size != 3 && pixel_size != 1) return null; return new ImageMetaData { - Width = LittleEndian.ToUInt32(header, 4), - Height = LittleEndian.ToUInt32(header, 8), + Width = header.ToUInt32 (4), + Height = header.ToUInt32 (8), BPP = pixel_size * 8, }; } - public override ImageData Read (Stream stream, ImageMetaData info) + public override ImageData Read (IBinaryStream stream, ImageMetaData info) { stream.Position = 0x10; int pixel_size = info.BPP/8; diff --git a/ArcFormats/Softpal/ImagePGD.cs b/ArcFormats/Softpal/ImagePGD.cs index 86f04c52..61d8b2ba 100644 --- a/ArcFormats/Softpal/ImagePGD.cs +++ b/ArcFormats/Softpal/ImagePGD.cs @@ -43,24 +43,22 @@ namespace GameRes.Formats.Softpal Extensions = new string[] { "pgd" }; } - public override ImageMetaData ReadMetaData (Stream stream) + public override ImageMetaData ReadMetaData (IBinaryStream stream) { - var header = new byte[0x20]; - if (header.Length != stream.Read (header, 0, header.Length)) - return null; - if (!Binary.AsciiEqual (header, 0x1C, "11_C")) + var header = stream.ReadHeader (0x20); + if (!header.AsciiEqual (0x1C, "11_C")) return null; return new ImageMetaData { - Width = LittleEndian.ToUInt32 (header, 0x0C), - Height = LittleEndian.ToUInt32 (header, 0x10), - OffsetX = LittleEndian.ToInt32 (header, 4), - OffsetY = LittleEndian.ToInt32 (header, 8), + Width = header.ToUInt32 (0x0C), + Height = header.ToUInt32 (0x10), + OffsetX = header.ToInt32 (4), + OffsetY = header.ToInt32 (8), BPP = 32, }; } - public override ImageData Read (Stream stream, ImageMetaData info) + public override ImageData Read (IBinaryStream stream, ImageMetaData info) { using (var reader = new PgdReader (stream, 0x20)) { @@ -101,29 +99,27 @@ namespace GameRes.Formats.Softpal Extensions = new string[] { "pgd" }; } - public override ImageMetaData ReadMetaData (Stream stream) + public override ImageMetaData ReadMetaData (IBinaryStream stream) { - var header = new byte[0x24]; - if (header.Length != stream.Read (header, 0, header.Length)) - return null; - if (!Binary.AsciiEqual (header, 0x18, "00_C")) + var header = stream.ReadHeader (0x24); + if (!header.AsciiEqual (0x18, "00_C")) return null; return new ImageMetaData { - Width = LittleEndian.ToUInt32 (header, 8), - Height = LittleEndian.ToUInt32 (header, 12), - OffsetX = LittleEndian.ToInt32 (header, 0), - OffsetY = LittleEndian.ToInt32 (header, 4), + Width = header.ToUInt32 (8), + Height = header.ToUInt32 (12), + OffsetX = header.ToInt32 (0), + OffsetY = header.ToInt32 (4), BPP = 32, }; } - public override ImageData Read (Stream stream, ImageMetaData info) + public override ImageData Read (IBinaryStream stream, ImageMetaData info) { using (var reader = new PgdReader (stream, 0x1C)) { var data = reader.Unpack00(); - using (var tga = new MemoryStream (data)) + using (var tga = new BinMemoryStream (data, stream.Name)) { var tga_info = Tga.ReadMetaData (tga); if (null == tga_info) @@ -153,20 +149,18 @@ namespace GameRes.Formats.Softpal Extensions = new string[] { "pgd" }; } - public override ImageMetaData ReadMetaData (Stream stream) + public override ImageMetaData ReadMetaData (IBinaryStream stream) { - var header = new byte[0x2A]; - if (header.Length != stream.Read (header, 0, header.Length)) - return null; - int x = LittleEndian.ToInt32 (header, 0); - int y = LittleEndian.ToInt32 (header, 4); + var header = stream.ReadHeader (0x2A); + int x = header.ToInt32 (0); + int y = header.ToInt32 (4); if (Math.Abs (x) > 0x2000 || Math.Abs (y) > 0x2000) return null; - uint width = LittleEndian.ToUInt32 (header, 8); - uint height = LittleEndian.ToUInt32 (header, 12); + uint width = header.ToUInt32 (8); + uint height = header.ToUInt32 (12); if (0 == width || 0 == height - || width != LittleEndian.ToUInt16 (header, 0x24) - || height != LittleEndian.ToUInt16 (header, 0x26)) + || width != header.ToUInt16 (0x24) + || height != header.ToUInt16 (0x26)) return null; stream.Position = 0x18; var tga_info = base.ReadMetaData (stream); @@ -177,9 +171,10 @@ namespace GameRes.Formats.Softpal return tga_info; } - public override ImageData Read (Stream stream, ImageMetaData info) + public override ImageData Read (IBinaryStream stream, ImageMetaData info) { - using (var tga = new StreamRegion (stream, 0x18, true)) + using (var mem = new StreamRegion (stream.AsStream, 0x18, true)) + using (var tga = new BinaryStream (mem, stream.Name)) return base.Read (tga, info); } @@ -206,23 +201,21 @@ namespace GameRes.Formats.Softpal Extensions = new string[] { "pgd" }; } - public override ImageMetaData ReadMetaData (Stream stream) + public override ImageMetaData ReadMetaData (IBinaryStream stream) { - var header = new byte[0x20]; - if (header.Length != stream.Read (header, 0, header.Length)) - return null; + var header = stream.ReadHeader (0x20); return new PgdGeMetaData { - Width = LittleEndian.ToUInt32 (header, 0x0C), - Height = LittleEndian.ToUInt32 (header, 0x10), - OffsetX = LittleEndian.ToInt32 (header, 4), - OffsetY = LittleEndian.ToInt32 (header, 8), + Width = header.ToUInt32 (0x0C), + Height = header.ToUInt32 (0x10), + OffsetX = header.ToInt32 (4), + OffsetY = header.ToInt32 (8), BPP = 32, - Method = LittleEndian.ToUInt16 (header, 0x1C), + Method = header.ToUInt16 (0x1C), }; } - public override ImageData Read (Stream stream, ImageMetaData info) + public override ImageData Read (IBinaryStream stream, ImageMetaData info) { using (var reader = new PgdReader (stream, (PgdGeMetaData)info)) { @@ -255,28 +248,26 @@ namespace GameRes.Formats.Softpal Signatures = new uint[] { 0x33444750, 0x32444750 }; // 'PGD3', 'PGD2' } - public override ImageMetaData ReadMetaData (Stream stream) + public override ImageMetaData ReadMetaData (IBinaryStream stream) { - var header = new byte[0x30]; - if (header.Length != stream.Read (header, 0, header.Length)) - return null; - string base_name = Binary.GetCString (header, 0xE, 0x22); + var header = stream.ReadHeader (0x30); + string base_name = header.GetCString (0xE, 0x22); if (string.IsNullOrEmpty (base_name)) return null; return new PgdIncMetaData { - OffsetX = LittleEndian.ToUInt16 (header, 4), - OffsetY = LittleEndian.ToUInt16 (header, 6), - Width = LittleEndian.ToUInt16 (header, 8), - Height = LittleEndian.ToUInt16 (header, 0xA), - BPP = LittleEndian.ToUInt16 (header, 0xC), + OffsetX = header.ToUInt16 (4), + OffsetY = header.ToUInt16 (6), + Width = header.ToUInt16 (8), + Height = header.ToUInt16 (0xA), + BPP = header.ToUInt16 (0xC), BaseName = base_name, }; } static readonly Lazy PalFormat = new Lazy (() => FindByTag ("PGD/GE")); - public override ImageData Read (Stream stream, ImageMetaData info) + public override ImageData Read (IBinaryStream stream, ImageMetaData info) { var meta = (PgdIncMetaData)info; string dir_name = VFS.GetDirectoryName (meta.FileName); @@ -284,7 +275,7 @@ namespace GameRes.Formats.Softpal PgdGeMetaData base_info; byte[] image, overlay; PixelFormat format; - using (var base_file = VFS.OpenSeekableStream (name)) + using (var base_file = VFS.OpenBinaryStream (name)) { base_info = PalFormat.Value.ReadMetaData (base_file) as PgdGeMetaData; if (null == base_info) @@ -334,7 +325,7 @@ namespace GameRes.Formats.Softpal internal sealed class PgdReader : IDisposable { - BinaryReader m_input; + IBinaryStream m_input; byte[] m_output; int m_width; int m_height; @@ -343,23 +334,23 @@ namespace GameRes.Formats.Softpal public PixelFormat Format { get; private set; } - public PgdReader (Stream input, int position) + public PgdReader (IBinaryStream input, int position) { - input.Position = position; - m_input = new ArcView.Reader (input); + m_input = input; + m_input.Position = position; int unpacked_size = m_input.ReadInt32(); m_input.ReadInt32(); // packed_size m_output = new byte[unpacked_size]; } - public PgdReader (Stream input, PgdGeMetaData info) : this (input, 0x20) + public PgdReader (IBinaryStream input, PgdGeMetaData info) : this (input, 0x20) { m_width = (int)info.Width; m_height = (int)info.Height; m_method = info.Method; } - public PgdReader (Stream input, PgdIncMetaData info) : this (input, 0x30) + public PgdReader (IBinaryStream input, PgdIncMetaData info) : this (input, 0x30) { m_width = (int)info.Width; m_height = (int)info.Height; @@ -589,14 +580,8 @@ namespace GameRes.Formats.Softpal } #region IDisposable Members - bool _disposed = false; public void Dispose () { - if (!_disposed) - { - m_input.Dispose(); - _disposed = true; - } } #endregion } diff --git a/ArcFormats/Softpal/ImagePIC.cs b/ArcFormats/Softpal/ImagePIC.cs index 205007f6..9817c182 100644 --- a/ArcFormats/Softpal/ImagePIC.cs +++ b/ArcFormats/Softpal/ImagePIC.cs @@ -49,16 +49,14 @@ namespace GameRes.Formats.Softpal Extensions = new string[] { "" }; } - public override ImageMetaData ReadMetaData (Stream stream) + public override ImageMetaData ReadMetaData (IBinaryStream stream) { - var header = new byte[8]; - if (header.Length != stream.Read (header, 0, header.Length)) - return null; - int bpp = LittleEndian.ToInt16 (header, 0); + var header = stream.ReadHeader (8); + int bpp = header.ToInt16 (0); if (1 != bpp && 3 != bpp && 4 != bpp) return null; - uint width = LittleEndian.ToUInt16 (header, 2); - uint height = LittleEndian.ToUInt16 (header, 4); + uint width = header.ToUInt16 (2); + uint height = header.ToUInt16 (4); if (0 == width || 0 == height || header[6]*8 < width || header[7]*8 < height) return null; return new PicMetaData @@ -71,7 +69,7 @@ namespace GameRes.Formats.Softpal }; } - public override ImageData Read (Stream stream, ImageMetaData info) + public override ImageData Read (IBinaryStream stream, ImageMetaData info) { var meta = (PicMetaData)info; using (var reader = new PicReader (stream, meta)) @@ -89,7 +87,7 @@ namespace GameRes.Formats.Softpal internal sealed class PicReader : IDisposable { - BinaryReader m_input; + IBinaryStream m_input; byte[] m_output; PicMetaData m_info; byte[] m_control; @@ -102,9 +100,9 @@ namespace GameRes.Formats.Softpal readonly byte[] Values4bit = InitBlockValues (8); readonly byte[] Values6bit = InitBlockValues (0x20); - public PicReader (Stream input, PicMetaData info) + public PicReader (IBinaryStream input, PicMetaData info) { - m_input = new ArcView.Reader (input); + m_input = input; m_info = info; m_src_stride = m_info.BlocksWidth * m_info.BPP; } @@ -122,7 +120,7 @@ namespace GameRes.Formats.Softpal public void Unpack () { - m_input.BaseStream.Position = 8; + m_input.Position = 8; m_control = m_input.ReadBytes (m_info.BlocksHeight * m_info.BlocksWidth); m_output = new byte[m_src_stride * m_info.BlocksHeight * 8]; if (8 == m_info.BPP) @@ -259,7 +257,7 @@ namespace GameRes.Formats.Softpal } else { - byte pixel = m_input.ReadByte(); + byte pixel = m_input.ReadUInt8(); DecodeBlock (ctl, pixel, block); } int dst = 8 * (x + 8 * y * m_info.BlocksWidth); @@ -394,14 +392,8 @@ namespace GameRes.Formats.Softpal } #region IDisposable Members - bool _disposed = false; public void Dispose () { - if (!_disposed) - { - m_input.Dispose(); - _disposed = true; - } } #endregion } diff --git a/ArcFormats/StudioEgo/ArcPAK0.cs b/ArcFormats/StudioEgo/ArcPAK0.cs index 8b9ca7ac..febcbdb4 100644 --- a/ArcFormats/StudioEgo/ArcPAK0.cs +++ b/ArcFormats/StudioEgo/ArcPAK0.cs @@ -75,7 +75,7 @@ namespace GameRes.Formats.Ego return base.OpenEntry (arc, entry); var data = arc.File.View.ReadBytes (entry.Offset, entry.Size); DecryptScript (method, data); - return new MemoryStream (data); + return new BinMemoryStream (data, entry.Name); } unsafe void DecryptScript (uint method, byte[] script) diff --git a/ArcFormats/StudioEgo/ImageANT.cs b/ArcFormats/StudioEgo/ImageANT.cs index b6e4f911..c48cdff9 100644 --- a/ArcFormats/StudioEgo/ImageANT.cs +++ b/ArcFormats/StudioEgo/ImageANT.cs @@ -37,20 +37,18 @@ namespace GameRes.Formats.Ego public override string Description { get { return "Studio e.go! bitmap format"; } } public override uint Signature { get { return 0x49544E41; } } // 'ANTI' - public override ImageMetaData ReadMetaData (Stream stream) + public override ImageMetaData ReadMetaData (IBinaryStream stream) { - var header = new byte[0x18]; - if (header.Length != stream.Read (header, 0, header.Length)) - return null; + var header = stream.ReadHeader (0x18); return new ImageMetaData { - Width = LittleEndian.ToUInt32 (header, 0xC), - Height = LittleEndian.ToUInt32 (header, 0x10), + Width = header.ToUInt32 (0xC), + Height = header.ToUInt32 (0x10), BPP = 32, }; } - public override ImageData Read (Stream stream, ImageMetaData info) + public override ImageData Read (IBinaryStream stream, ImageMetaData info) { var pixels = new byte[info.Width*info.Height*4]; stream.Position = 0x18; diff --git a/ArcFormats/SuperNekoX/ArcGPC.cs b/ArcFormats/SuperNekoX/ArcGPC.cs index ece9ab91..9491c2eb 100644 --- a/ArcFormats/SuperNekoX/ArcGPC.cs +++ b/ArcFormats/SuperNekoX/ArcGPC.cs @@ -75,37 +75,34 @@ namespace GameRes.Formats.SuperNekoX public override Stream OpenEntry (ArcFile arc, Entry entry) { var pent = entry as PackedEntry; - Stream input = arc.File.CreateStream (entry.Offset, entry.Size); + IBinaryStream input = arc.File.CreateStream (entry.Offset, entry.Size, entry.Name); if (null != pent && pent.IsPacked) { - Stream unpacked; + IBinaryStream unpacked; using (input) { var data = new byte[pent.UnpackedSize]; - UnpackEntry (input, data); - unpacked = new MemoryStream (data); + UnpackEntry (input.AsStream, data); + unpacked = new BinMemoryStream (data, entry.Name); } input = unpacked; } if (input.Length > 4 && input.Length < 0x10000) { - using (var reader = new ArcView.Reader (input)) + int unpacked_size = input.ReadUInt16(); + int packed_size = input.ReadUInt16(); + if (packed_size == input.Length-4) { - int unpacked_size = reader.ReadUInt16(); - int packed_size = reader.ReadUInt16(); - if (packed_size == input.Length-4) + using (input) { - using (input) - { - var data = new byte[unpacked_size]; - UnpackLz77 (input, data); - return new MemoryStream (data); - } + var data = new byte[unpacked_size]; + UnpackLz77 (input.AsStream, data); + return new BinMemoryStream (data, entry.Name); } - input.Position = 0; } + input.Position = 0; } - return input; + return input.AsStream; } void DetectFileTypes (ArcView file, List dir) diff --git a/ArcFormats/Tactics/ArcTactics.cs b/ArcFormats/Tactics/ArcTactics.cs index e3286237..3a72a20c 100644 --- a/ArcFormats/Tactics/ArcTactics.cs +++ b/ArcFormats/Tactics/ArcTactics.cs @@ -87,9 +87,9 @@ namespace GameRes.Formats.Tactics var tarc = arc as TacticsArcFile; var tent = (PackedEntry)entry; if (null == tarc || null == tarc.Password && !tent.IsPacked) - return arc.File.CreateStream (entry.Offset, entry.Size); + return base.OpenEntry (arc, entry); if (null == tarc.Password) - return new LzssStream (arc.File.CreateStream (entry.Offset, entry.Size)); + return new LzssStream (base.OpenEntry (arc, entry)); var data = arc.File.View.ReadBytes (entry.Offset, entry.Size); int p = 0; @@ -102,9 +102,9 @@ namespace GameRes.Formats.Tactics if (tarc.CustomLzss && tent.IsPacked) { data = UnpackCustomLzss (data); - return new MemoryStream (data); + return new BinMemoryStream (data, entry.Name); } - Stream input = new MemoryStream (data); + Stream input = new BinMemoryStream (data, entry.Name); if (tent.IsPacked) input = new LzssStream (input); return input; diff --git a/ArcFormats/Tactics/ImageTGF.cs b/ArcFormats/Tactics/ImageTGF.cs index 524eb002..369bc731 100644 --- a/ArcFormats/Tactics/ImageTGF.cs +++ b/ArcFormats/Tactics/ImageTGF.cs @@ -53,16 +53,13 @@ namespace GameRes.Formats.Tactics throw new NotImplementedException ("TgfFormat.Write not implemented"); } - public override ImageMetaData ReadMetaData (Stream stream) + public override ImageMetaData ReadMetaData (IBinaryStream stream) { - var header = new byte[8]; - if (header.Length != stream.Read (header, 0, header.Length)) - return null; - uint length = LittleEndian.ToUInt32 (header, 0); - int chunk_size = LittleEndian.ToInt32 (header, 4); + uint length = stream.ReadUInt32(); + int chunk_size = stream.ReadInt32(); if (length > 0xffffff || chunk_size <= 0 || length < chunk_size) return null; - using (var reader = new Reader (stream, (uint)Math.Max (0x20, chunk_size+2), chunk_size)) + using (var reader = new Reader (stream.AsStream, (uint)Math.Max (0x20, chunk_size+2), chunk_size)) { reader.Unpack(); var bmp = reader.Data; @@ -79,14 +76,11 @@ namespace GameRes.Formats.Tactics } } - public override ImageData Read (Stream stream, ImageMetaData info) + public override ImageData Read (IBinaryStream stream, ImageMetaData info) { - var meta = info as TgfMetaData; - if (null == meta) - throw new ArgumentException ("TgfFormat.Read should be supplied with TgfMetaData", "info"); - + var meta = (TgfMetaData)info; stream.Position = 8; - using (var reader = new Reader (stream, meta.BitmapSize, meta.ChunkSize)) + using (var reader = new Reader (stream.AsStream, meta.BitmapSize, meta.ChunkSize)) { reader.Unpack(); using (var bmp = new MemoryStream (reader.Data)) diff --git a/ArcFormats/TamaSoft/AudioESD.cs b/ArcFormats/TamaSoft/AudioESD.cs index 2a6ff490..55150228 100644 --- a/ArcFormats/TamaSoft/AudioESD.cs +++ b/ArcFormats/TamaSoft/AudioESD.cs @@ -37,21 +37,19 @@ namespace GameRes.Formats.Tama public override string Description { get { return "TamaSoft ADV system audio"; } } public override uint Signature { get { return 0x20445345; } } // 'ESD ' - public override SoundInput TryOpen (Stream file) + public override SoundInput TryOpen (IBinaryStream file) { - var header = new byte[0x20]; - if (header.Length != file.Read (header, 0, header.Length)) - return null; + var header = file.ReadHeader (0x20); var format = new WaveFormat { FormatTag = 1, - Channels = LittleEndian.ToUInt16 (header, 0x10), - SamplesPerSecond = LittleEndian.ToUInt32 (header, 8), - BitsPerSample = LittleEndian.ToUInt16 (header, 0xC), + Channels = header.ToUInt16 (0x10), + SamplesPerSecond = header.ToUInt32 (8), + BitsPerSample = header.ToUInt16 (0xC), }; format.BlockAlign = (ushort)(format.Channels * format.BitsPerSample / 8); format.AverageBytesPerSecond = format.SamplesPerSecond * format.BlockAlign; - var pcm = new StreamRegion (file, 0x20); + var pcm = new StreamRegion (file.AsStream, 0x20); return new RawPcmInput (pcm, format); } } diff --git a/ArcFormats/TamaSoft/ImageBTN.cs b/ArcFormats/TamaSoft/ImageBTN.cs index c1c7a614..18cf9731 100644 --- a/ArcFormats/TamaSoft/ImageBTN.cs +++ b/ArcFormats/TamaSoft/ImageBTN.cs @@ -25,7 +25,6 @@ using System.ComponentModel.Composition; using System.IO; -using GameRes.Utility; namespace GameRes.Formats.Tama { @@ -41,14 +40,13 @@ namespace GameRes.Formats.Tama public override string Description { get { return "TamaSoft ADV system button image"; } } public override uint Signature { get { return 0x4E544245; } } // 'EBTN' - public override ImageMetaData ReadMetaData (Stream stream) + public override ImageMetaData ReadMetaData (IBinaryStream stream) { - var header = new byte[8]; - if (header.Length != stream.Read (header, 0, 8)) - return null; - int count = LittleEndian.ToInt32 (header, 4); + stream.Position = 4; + int count = stream.ReadInt32(); int offset = 0x30 + count * 4; - using (var input = new StreamRegion (stream, offset, true)) + using (var data = new StreamRegion (stream.AsStream, offset, true)) + using (var input = new BinaryStream (data, stream.Name)) { var info = base.ReadMetaData (input); if (null == info) @@ -63,10 +61,11 @@ namespace GameRes.Formats.Tama } } - public override ImageData Read (Stream stream, ImageMetaData info) + public override ImageData Read (IBinaryStream stream, ImageMetaData info) { var meta = (BtnMetaData)info; - using (var input = new StreamRegion (stream, meta.SurOffset, true)) + using (var data = new StreamRegion (stream.AsStream, meta.SurOffset, true)) + using (var input = new BinaryStream (data, stream.Name)) return base.Read (input, info); } diff --git a/ArcFormats/TamaSoft/ImageSUR.cs b/ArcFormats/TamaSoft/ImageSUR.cs index 5f6afcb0..8a21acf1 100644 --- a/ArcFormats/TamaSoft/ImageSUR.cs +++ b/ArcFormats/TamaSoft/ImageSUR.cs @@ -38,24 +38,22 @@ namespace GameRes.Formats.Tama public override string Description { get { return "TamaSoft ADV system image"; } } public override uint Signature { get { return 0x52555345; } } // 'ESUR' - public override ImageMetaData ReadMetaData (Stream stream) + public override ImageMetaData ReadMetaData (IBinaryStream stream) { - var header = new byte[0x10]; - if (header.Length != stream.Read (header, 0, header.Length)) - return null; + var header = stream.ReadHeader (0x10); return new ImageMetaData { - Width = LittleEndian.ToUInt32 (header, 8), - Height = LittleEndian.ToUInt32 (header, 12), + Width = header.ToUInt32 (8), + Height = header.ToUInt32 (12), BPP = 32, }; } - public override ImageData Read (Stream stream, ImageMetaData info) + public override ImageData Read (IBinaryStream stream, ImageMetaData info) { var pixels = new byte[info.Width * info.Height * 4]; stream.Position = 0x20; - UnpackLzss (stream, pixels); + UnpackLzss (stream.AsStream, pixels); return ImageData.Create (info, PixelFormats.Bgra32, null, pixels); } diff --git a/ArcFormats/TechnoBrain/ImageIPH.cs b/ArcFormats/TechnoBrain/ImageIPH.cs index 3720557a..6997d16d 100644 --- a/ArcFormats/TechnoBrain/ImageIPH.cs +++ b/ArcFormats/TechnoBrain/ImageIPH.cs @@ -44,36 +44,33 @@ namespace GameRes.Formats.TechnoBrain public override string Description { get { return "TechnoBrain's 'Inteligent Picture Format'"; } } public override uint Signature { get { return 0; } } // 'RIFF' - public override ImageMetaData ReadMetaData (Stream stream) + public override ImageMetaData ReadMetaData (IBinaryStream file) { // 'RIFF' isn't included into signature to avoid auto-detection of the WAV files as IPH images. - if (0x46464952 != FormatCatalog.ReadSignature (stream)) // 'RIFF' + if (0x46464952 != file.Signature) // 'RIFF' return null; - using (var reader = new ArcView.Reader (stream)) - { - if (0x38 != reader.ReadInt32()) - return null; - var signature = reader.ReadInt32(); - if (signature != 0x20485049 && signature != 0x00485049) // 'IPH' - return null; - if (0x20746D66 != reader.ReadInt32()) // 'fmt ' - return null; - reader.BaseStream.Position = 0x38; - if (0x20706D62 != reader.ReadInt32()) // 'bmp ' - return null; - var info = new IphMetaData(); - info.PackedSize = reader.ReadInt32(); - info.Width = reader.ReadUInt16(); - info.Height = reader.ReadUInt16(); - reader.BaseStream.Position = 0x50; - info.BPP = reader.ReadUInt16(); - info.IsCompressed = 0 != reader.ReadInt16(); - // XXX int16@[0x54] is a transparency color or 0xFFFF if image is not transparent - return info; - } + if (0x38 != file.ReadInt32()) + return null; + var signature = file.ReadInt32(); + if (signature != 0x20485049 && signature != 0x00485049) // 'IPH' + return null; + if (0x20746D66 != file.ReadInt32()) // 'fmt ' + return null; + file.Position = 0x38; + if (0x20706D62 != file.ReadInt32()) // 'bmp ' + return null; + var info = new IphMetaData(); + info.PackedSize = file.ReadInt32(); + info.Width = file.ReadUInt16(); + info.Height = file.ReadUInt16(); + file.Position = 0x50; + info.BPP = file.ReadUInt16(); + info.IsCompressed = 0 != file.ReadInt16(); + // XXX int16@[0x54] is a transparency color or 0xFFFF if image is not transparent + return info; } - public override ImageData Read (Stream stream, ImageMetaData info) + public override ImageData Read (IBinaryStream stream, ImageMetaData info) { if (info.BPP != 16) throw new NotSupportedException ("Not supported IPH color depth"); @@ -92,7 +89,7 @@ namespace GameRes.Formats.TechnoBrain internal sealed class IphReader : IDisposable { - BinaryReader m_input; + IBinaryStream m_input; byte[] m_output; int m_width; int m_height; @@ -101,10 +98,10 @@ namespace GameRes.Formats.TechnoBrain public PixelFormat Format { get { return PixelFormats.Bgr555; } } public byte[] Data { get { return m_output; } } - public IphReader (Stream input, IphMetaData info) + public IphReader (IBinaryStream input, IphMetaData info) { m_info = info; - m_input = new ArcView.Reader (input); + m_input = input; m_width = (int)info.Width; m_height = (int)info.Height; m_output = new byte[m_width*m_height*2]; @@ -112,7 +109,7 @@ namespace GameRes.Formats.TechnoBrain public void Unpack () { - m_input.BaseStream.Position = 0x58; + m_input.Position = 0x58; if (!m_info.IsCompressed) { m_input.Read (m_output, 0, m_output.Length); @@ -123,14 +120,14 @@ namespace GameRes.Formats.TechnoBrain for (int y = 0; y < m_height; ++y) { int row = stride * y; - byte ctl = m_input.ReadByte(); + byte ctl = m_input.ReadUInt8(); if (ctl != 0) { int dst = row; int pixel = 0; while (dst < m_output.Length) { - ctl = m_input.ReadByte(); + ctl = m_input.ReadUInt8(); if (0xFF == ctl) break; if (0xFE == ctl) @@ -145,7 +142,7 @@ namespace GameRes.Formats.TechnoBrain } else if (ctl < 0x80) { - byte lo = m_input.ReadByte(); + byte lo = m_input.ReadUInt8(); m_output[dst++] = lo; m_output[dst++] = ctl; pixel = ctl << 8 | lo; @@ -169,13 +166,13 @@ namespace GameRes.Formats.TechnoBrain m_input.Read (m_output, row, stride); m_input.ReadByte(); } - ctl = m_input.ReadByte(); + ctl = m_input.ReadUInt8(); if (0 != ctl) { int dst = 0; for (;;) { - ctl = m_input.ReadByte(); + ctl = m_input.ReadUInt8(); if (0xFF == ctl) break; if (ctl >= 0x80) @@ -209,14 +206,8 @@ namespace GameRes.Formats.TechnoBrain } #region IDisposable Members - bool _disposed = false; public void Dispose () { - if (!_disposed) - { - m_input.Dispose(); - _disposed = true; - } } #endregion } diff --git a/ArcFormats/Tmr-Hiro/ArcPAC.cs b/ArcFormats/Tmr-Hiro/ArcPAC.cs index deac5f4f..a27b5661 100644 --- a/ArcFormats/Tmr-Hiro/ArcPAC.cs +++ b/ArcFormats/Tmr-Hiro/ArcPAC.cs @@ -136,7 +136,7 @@ namespace GameRes.Formats.TmrHiro ++pos; } } - return new MemoryStream (data); + return new BinMemoryStream (data, entry.Name); } } } diff --git a/ArcFormats/Tmr-Hiro/AudioTmr.cs b/ArcFormats/Tmr-Hiro/AudioTmr.cs index a7bcf13e..8f9b08a5 100644 --- a/ArcFormats/Tmr-Hiro/AudioTmr.cs +++ b/ArcFormats/Tmr-Hiro/AudioTmr.cs @@ -44,15 +44,15 @@ namespace GameRes.Formats.TmrHiro Extensions = new string[] { "" }; } - public override SoundInput TryOpen (Stream file) + public override SoundInput TryOpen (IBinaryStream file) { if (file.ReadByte() != 0x44) return null; file.Position = 4; if (file.ReadByte() != 0) return null; - int length = ReadInt32 (file); - if (-1 == length || length != file.Length - 9) + int length = file.ReadInt32(); + if (length != file.Length - 9) return null; var format = new WaveFormat { @@ -63,17 +63,8 @@ namespace GameRes.Formats.TmrHiro BlockAlign = 4, AverageBytesPerSecond = 44100*4, }; - var pcm = new StreamRegion (file, 9, length); + var pcm = new StreamRegion (file.AsStream, 9, length); return new RawPcmInput (pcm, format); } - - private static int ReadInt32 (Stream file) - { - int dword = file.ReadByte(); - dword |= file.ReadByte() << 8; - dword |= file.ReadByte() << 16; - dword |= file.ReadByte() << 24; - return dword; - } } } diff --git a/ArcFormats/Tmr-Hiro/ImageGRD.cs b/ArcFormats/Tmr-Hiro/ImageGRD.cs index 8729fa62..61ec3a87 100644 --- a/ArcFormats/Tmr-Hiro/ImageGRD.cs +++ b/ArcFormats/Tmr-Hiro/ImageGRD.cs @@ -52,45 +52,43 @@ namespace GameRes.Formats.TmrHiro Extensions = new string[] { "grd", "" }; } - public override ImageMetaData ReadMetaData (Stream stream) + public override ImageMetaData ReadMetaData (IBinaryStream stream) { - var header = new byte[0x20]; - if (header.Length != stream.Read (header, 0, header.Length)) - return null; + var header = stream.ReadHeader (0x20); if (header[0] != 1 && header[0] != 2) return null; if (header[1] != 1 && header[1] != 0xA1 && header[1] != 0xA2) return null; - int bpp = LittleEndian.ToUInt16 (header, 6); + int bpp = header.ToUInt16 (6); if (bpp != 24 && bpp != 32) return null; - int screen_width = LittleEndian.ToUInt16 (header, 2); - int screen_height = LittleEndian.ToUInt16 (header, 4); - int left = LittleEndian.ToUInt16 (header, 8); - int right = LittleEndian.ToUInt16 (header, 0xA); - int top = LittleEndian.ToUInt16 (header, 0xC); - int bottom = LittleEndian.ToUInt16 (header, 0xE); + int screen_width = header.ToUInt16 (2); + int screen_height = header.ToUInt16 (4); + int left = header.ToUInt16 (8); + int right = header.ToUInt16 (0xA); + int top = header.ToUInt16 (0xC); + int bottom = header.ToUInt16 (0xE); var info = new GrdMetaData { - Format = LittleEndian.ToUInt16 (header, 0), + Format = header.ToUInt16 (0), Width = (uint)(right-left), Height = (uint)(bottom-top), BPP = bpp, OffsetX = left, OffsetY = screen_height - bottom, - AlphaSize = LittleEndian.ToInt32 (header, 0x10), - RSize = LittleEndian.ToInt32 (header, 0x14), - GSize = LittleEndian.ToInt32 (header, 0x18), - BSize = LittleEndian.ToInt32 (header, 0x1C), + AlphaSize = header.ToInt32 (0x10), + RSize = header.ToInt32 (0x14), + GSize = header.ToInt32 (0x18), + BSize = header.ToInt32 (0x1C), }; if (0x20 + info.AlphaSize + info.RSize + info.BSize + info.GSize != stream.Length) return null; return info; } - public override ImageData Read (Stream stream, ImageMetaData info) + public override ImageData Read (IBinaryStream stream, ImageMetaData info) { var meta = (GrdMetaData)info; - var reader = new GrdReader (stream, meta); + var reader = new GrdReader (stream.AsStream, meta); reader.Unpack(); return ImageData.Create (info, reader.Format, null, reader.Data); } diff --git a/ArcFormats/TopCat/ArcTCD3.cs b/ArcFormats/TopCat/ArcTCD3.cs index 08a8a8cf..897859f3 100644 --- a/ArcFormats/TopCat/ArcTCD3.cs +++ b/ArcFormats/TopCat/ArcTCD3.cs @@ -122,7 +122,7 @@ namespace GameRes.Formats.TopCat return OpenScript (arc, entry); if (entry.Name.EndsWith (".SPD", StringComparison.InvariantCultureIgnoreCase)) return OpenSpdc (arc, entry); - return arc.File.CreateStream (entry.Offset, entry.Size); + return base.OpenEntry (arc, entry); } Stream OpenSpdc (ArcFile arc, Entry entry) @@ -187,7 +187,7 @@ namespace GameRes.Formats.TopCat using (var input = arc.File.CreateStream (entry.Offset+4, entry.Size-4)) UnpackLz (input, data); DecryptScript (data); - return new MemoryStream (data); + return new BinMemoryStream (data, entry.Name); } void UnpackLz (Stream input, byte[] output) @@ -253,7 +253,7 @@ namespace GameRes.Formats.TopCat LittleEndian.Pack (crc, data, src+0x16); src += page_size; } - return new MemoryStream (data); + return new BinMemoryStream (data, entry.Name); } } diff --git a/ArcFormats/TopCat/ImageSPD.cs b/ArcFormats/TopCat/ImageSPD.cs index 384b50bb..fb6214ab 100644 --- a/ArcFormats/TopCat/ImageSPD.cs +++ b/ArcFormats/TopCat/ImageSPD.cs @@ -62,11 +62,9 @@ namespace GameRes.Formats.TopCat Signatures = new uint[] { 0x43445053, 0x38445053 }; // 'SPD8' } - public override ImageMetaData ReadMetaData (Stream stream) + public override ImageMetaData ReadMetaData (IBinaryStream stream) { - var header = new byte[0x14]; - if (header.Length != stream.Read (header, 0, header.Length)) - return null; + var header = stream.ReadHeader (0x14).ToArray(); unsafe { fixed (byte* raw = header) @@ -88,13 +86,13 @@ namespace GameRes.Formats.TopCat } } - public override ImageData Read (Stream stream, ImageMetaData info) + public override ImageData Read (IBinaryStream stream, ImageMetaData info) { var meta = (SpdMetaData)info; if (Compression.Jpeg == meta.Method) - return ReadJpeg (stream, meta); + return ReadJpeg (stream.AsStream, meta); - using (var reader = new SpdReader (stream, meta)) + using (var reader = new SpdReader (stream.AsStream, meta)) { reader.Unpack(); return ImageData.Create (info, reader.Format, null, reader.Data); diff --git a/ArcFormats/Triangle/ImageIAF.cs b/ArcFormats/Triangle/ImageIAF.cs index d8b7007f..50de3430 100644 --- a/ArcFormats/Triangle/ImageIAF.cs +++ b/ArcFormats/Triangle/ImageIAF.cs @@ -48,7 +48,7 @@ namespace GameRes.Formats.Triangle public override uint Signature { get { return 0; } } public override bool CanWrite { get { return false; } } - public override ImageMetaData ReadMetaData (Stream stream) + public override ImageMetaData ReadMetaData (IBinaryStream stream) { var header = new byte[0x14]; if (12 != stream.Read (header, 0, 12)) @@ -100,7 +100,7 @@ namespace GameRes.Formats.Triangle return null; unpacked_size &= (int)~0xC0000000; stream.Position = data_offset; - byte[] bmp = UnpackBitmap (stream, pack_type, packed_size, 0x26); + byte[] bmp = UnpackBitmap (stream.AsStream, pack_type, packed_size, 0x26); if (bmp[0] != 'B' && bmp[0] != 'C' || bmp[1] != 'M') return null; return new IafMetaData @@ -117,11 +117,11 @@ namespace GameRes.Formats.Triangle }; } - public override ImageData Read (Stream stream, ImageMetaData info) + public override ImageData Read (IBinaryStream stream, ImageMetaData info) { var meta = (IafMetaData)info; stream.Position = meta.DataOffset; - var bitmap = UnpackBitmap (stream, meta.PackType, meta.PackedSize, meta.UnpackedSize); + var bitmap = UnpackBitmap (stream.AsStream, meta.PackType, meta.PackedSize, meta.UnpackedSize); if ('C' == bitmap[0]) { bitmap[0] = (byte)'B'; @@ -151,7 +151,7 @@ namespace GameRes.Formats.Triangle // fallback to a plain bitmap } } - using (var bmp = new MemoryStream (bitmap)) + using (var bmp = new BinMemoryStream (bitmap, stream.Name)) return base.Read (bmp, info); } diff --git a/ArcFormats/UMeSoft/ArcPK.cs b/ArcFormats/UMeSoft/ArcPK.cs index 1f89e150..3d0294f2 100644 --- a/ArcFormats/UMeSoft/ArcPK.cs +++ b/ArcFormats/UMeSoft/ArcPK.cs @@ -94,7 +94,7 @@ namespace GameRes.Formats.UMeSoft var data = LzUnpack (input, output_size); for (int i = 0; i < data.Length; ++i) data[i] ^= 0x42; - return new MemoryStream (data); + return new BinMemoryStream (data, entry.Name); } } diff --git a/ArcFormats/UMeSoft/ImageGRX.cs b/ArcFormats/UMeSoft/ImageGRX.cs index e69d74e4..10b54823 100644 --- a/ArcFormats/UMeSoft/ImageGRX.cs +++ b/ArcFormats/UMeSoft/ImageGRX.cs @@ -45,25 +45,27 @@ namespace GameRes.Formats.UMeSoft public override string Description { get { return "U-Me Soft image format"; } } public override uint Signature { get { return 0x1A585247; } } // 'GRX' - public override ImageMetaData ReadMetaData (Stream stream) + public override ImageMetaData ReadMetaData (IBinaryStream file) { - byte[] header = new byte[0x10]; - if (header.Length != stream.Read (header, 0, header.Length)) - return null; - return new GrxMetaData - { - Width = LittleEndian.ToUInt16 (header, 8), - Height = LittleEndian.ToUInt16 (header, 10), - BPP = LittleEndian.ToUInt16 (header, 6), - IsPacked = 0 != header[4], - HasAlpha = 0 != header[5], - AlphaOffset = LittleEndian.ToInt32 (header, 12), - }; + file.Position = 4; + return ReadInfo (file); } - public override ImageData Read (Stream stream, ImageMetaData info) + internal GrxMetaData ReadInfo (IBinaryStream file) { - var reader = new Reader (stream, (GrxMetaData)info); + var info = new GrxMetaData(); + info.IsPacked = file.ReadByte() != 0; + info.HasAlpha = file.ReadByte() != 0; + info.BPP = file.ReadUInt16(); + info.Width = file.ReadUInt16(); + info.Height = file.ReadUInt16(); + info.AlphaOffset = file.ReadInt32(); + return info; + } + + public override ImageData Read (IBinaryStream file, ImageMetaData info) + { + var reader = new Reader (file.AsStream, (GrxMetaData)info); reader.Unpack(); return ImageData.Create (info, reader.Format, null, reader.Data, reader.Stride); } @@ -292,38 +294,36 @@ namespace GameRes.Formats.UMeSoft Extensions = new string[] { "grx" }; } - public override ImageMetaData ReadMetaData (Stream stream) + public override ImageMetaData ReadMetaData (IBinaryStream file) { - using (var reader = new ArcView.Reader (stream)) + file.Position = 4; + int offset = file.ReadInt32(); + if (offset <= 8) + return null; + file.Position = offset; + uint signature = file.ReadUInt32(); + if (signature != base.Signature) + return null; + var info = ReadInfo (file); + return new SgxMetaData { - stream.Seek (4, SeekOrigin.Current); - int offset = reader.ReadInt32(); - if (offset <= 8) - return null; - stream.Position = offset; - uint signature = reader.ReadUInt32(); - if (signature != base.Signature) - return null; - stream.Seek (-4, SeekOrigin.Current); - var info = base.ReadMetaData (stream) as GrxMetaData; - if (null == info) - return null; - return new SgxMetaData - { - Width = info.Width, - Height = info.Height, - BPP = info.BPP, - GrxOffset = offset, - GrxInfo = info - }; - } + Width = info.Width, + Height = info.Height, + BPP = info.BPP, + GrxOffset = offset, + GrxInfo = info + }; } - public override ImageData Read (Stream stream, ImageMetaData info) + public override ImageData Read (IBinaryStream stream, ImageMetaData info) { var meta = (SgxMetaData)info; - using (var grx = new StreamRegion (stream, meta.GrxOffset, true)) - return base.Read (grx, meta.GrxInfo); + using (var grx = new StreamRegion (stream.AsStream, meta.GrxOffset, true)) + { + var reader = new Reader (grx, (GrxMetaData)meta.GrxInfo); + reader.Unpack(); + return ImageData.Create (info, reader.Format, null, reader.Data, reader.Stride); + } } } } diff --git a/ArcFormats/Vitamin/ImageMFC.cs b/ArcFormats/Vitamin/ImageMFC.cs index b82ea858..d3bea9af 100644 --- a/ArcFormats/Vitamin/ImageMFC.cs +++ b/ArcFormats/Vitamin/ImageMFC.cs @@ -49,17 +49,16 @@ namespace GameRes.Formats.Vitamin Extensions = new string[] { "mfc" }; } - public override ImageMetaData ReadMetaData (Stream stream) + public override ImageMetaData ReadMetaData (IBinaryStream stream) { - var header = new byte[0x18]; - if (header.Length != stream.Read (header, 0, header.Length)) - return null; + var header = stream.ReadHeader (0x18); if (header[4] != 1 || header[5] != 0 || header[6] != 1 || header[7] != 4) return null; - int alpha_size = LittleEndian.ToInt32 (header, 12); - using (var sbi = new StreamRegion (stream, alpha_size, true)) + int alpha_size = header.ToInt32 (12); + using (var reg = new StreamRegion (stream.AsStream, alpha_size, true)) + using (var sbi = new BinaryStream (reg, stream.Name)) { var info = base.ReadMetaData (sbi) as SbiMetaData; if (null == info) @@ -75,15 +74,15 @@ namespace GameRes.Formats.Vitamin } } - public override ImageData Read (Stream stream, ImageMetaData info) + public override ImageData Read (IBinaryStream stream, ImageMetaData info) { var meta = (MfcMetaData)info; // XXX implemented like in reference code, width is expected to be even. var alpha = new byte[info.Width * info.Height / 2]; stream.Position = 0x18; - RleUnpack (stream, meta.AlphaSize - 0x18, alpha); + RleUnpack (stream.AsStream, meta.AlphaSize - 0x18, alpha); byte[] pixels; - using (var sbi = new StreamRegion (stream, meta.AlphaSize, true)) + using (var sbi = new StreamRegion (stream.AsStream, meta.AlphaSize, true)) using (var reader = new SbiReader (sbi, meta.BaseInfo)) { reader.Unpack(); diff --git a/ArcFormats/Vitamin/ImageSBI.cs b/ArcFormats/Vitamin/ImageSBI.cs index e439ea75..4055522c 100644 --- a/ArcFormats/Vitamin/ImageSBI.cs +++ b/ArcFormats/Vitamin/ImageSBI.cs @@ -51,11 +51,9 @@ namespace GameRes.Formats.Vitamin Extensions = new string[] { "cmp" }; } - public override ImageMetaData ReadMetaData (Stream stream) + public override ImageMetaData ReadMetaData (IBinaryStream stream) { - var header = new byte[0x20]; - if (header.Length != stream.Read (header, 0, header.Length)) - return null; + var header = stream.ReadHeader (0x20); if (header[4] != 1 || header[5] != 0) return null; int bpp = header[6]; @@ -63,18 +61,18 @@ namespace GameRes.Formats.Vitamin return null; return new SbiMetaData { - Width = LittleEndian.ToUInt16 (header, 7), - Height = LittleEndian.ToUInt16 (header, 9), + Width = header.ToUInt16 (7), + Height = header.ToUInt16 (9), BPP = bpp, - InputSize = LittleEndian.ToInt32 (header, 0xB), + InputSize = header.ToInt32 (0xB), IsPacked = 0 != header[0x10], HasPalette = 8 == bpp && 0 == header[0xF], }; } - public override ImageData Read (Stream stream, ImageMetaData info) + public override ImageData Read (IBinaryStream stream, ImageMetaData info) { - using (var reader = new SbiReader (stream, (SbiMetaData)info)) + using (var reader = new SbiReader (stream.AsStream, (SbiMetaData)info)) { reader.Unpack(); return ImageData.Create (info, reader.Format, reader.Palette, reader.Data, reader.Stride); diff --git a/ArcFormats/VnEngine/ImageZAW.cs b/ArcFormats/VnEngine/ImageZAW.cs index 79bfe215..9320a780 100644 --- a/ArcFormats/VnEngine/ImageZAW.cs +++ b/ArcFormats/VnEngine/ImageZAW.cs @@ -38,12 +38,10 @@ namespace GameRes.Formats.VnEngine public override string Description { get { return "GEM/vnengine image format"; } } public override uint Signature { get { return 0x57415A; } } // 'ZAW' - public override ImageMetaData ReadMetaData (Stream stream) + public override ImageMetaData ReadMetaData (IBinaryStream stream) { - var header = new byte[0x40]; - if (header.Length != stream.Read (header, 0, header.Length)) - return null; - uint crc = LittleEndian.ToUInt32 (header, 4); + var header = stream.ReadHeader (0x40).ToArray(); + uint crc = header.ToUInt32 (4); LittleEndian.Pack (0u, header, 4); if (crc != Crc32.Compute (header, 0, header.Length)) return null; @@ -52,21 +50,21 @@ namespace GameRes.Formats.VnEngine : 1 == header[12] ? 24 : 8; return new ImageMetaData { - Width = LittleEndian.ToUInt32 (header, 0x10), - Height = LittleEndian.ToUInt32 (header, 0x14), - OffsetX = LittleEndian.ToInt32 (header, 0x18), - OffsetY = LittleEndian.ToInt32 (header, 0x1C), + Width = header.ToUInt32 (0x10), + Height = header.ToUInt32 (0x14), + OffsetX = header.ToInt32 (0x18), + OffsetY = header.ToInt32 (0x1C), BPP = bpp, }; } - public override ImageData Read (Stream stream, ImageMetaData info) + public override ImageData Read (IBinaryStream stream, ImageMetaData info) { int pixel_size = info.BPP / 8; int stride = (int)info.Width * pixel_size; var pixels = new byte[stride * (int)info.Height]; stream.Position = 0x40; - using (var input = new ZLibStream (stream, CompressionMode.Decompress, true)) + using (var input = new ZLibStream (stream.AsStream, CompressionMode.Decompress, true)) input.Read (pixels, 0, pixels.Length); if (24 == info.BPP) return ImageData.Create (info, PixelFormats.Rgb24, null, pixels, stride); diff --git a/ArcFormats/WebP/ImageWEBP.cs b/ArcFormats/WebP/ImageWEBP.cs index a18f16fe..e86909fa 100644 --- a/ArcFormats/WebP/ImageWEBP.cs +++ b/ArcFormats/WebP/ImageWEBP.cs @@ -60,9 +60,9 @@ namespace GameRes.Formats.Google public override string Description { get { return "Google WebP image format"; } } public override uint Signature { get { return 0; } } - public override ImageMetaData ReadMetaData (Stream stream) + public override ImageMetaData ReadMetaData (IBinaryStream stream) { - if (0x46464952 != FormatCatalog.ReadSignature (stream)) // 'RIFF' + if (0x46464952 != stream.Signature) // 'RIFF' return null; var header = new byte[0x10]; if (8 != stream.Read (header, 0, 8)) @@ -141,9 +141,9 @@ namespace GameRes.Formats.Google return (uint)(src[offset] | src[offset+1] << 8 | src[offset+2] << 16); } - public override ImageData Read (Stream stream, ImageMetaData info) + public override ImageData Read (IBinaryStream stream, ImageMetaData info) { - using (var reader = new WebPDecoder (stream, (WebPMetaData)info)) + using (var reader = new WebPDecoder (stream.AsStream, (WebPMetaData)info)) { reader.Decode(); return ImageData.Create (info, reader.Format, null, reader.Output); diff --git a/ArcFormats/WildBug/AudioWPN.cs b/ArcFormats/WildBug/AudioWPN.cs index 0b41d73e..b994d010 100644 --- a/ArcFormats/WildBug/AudioWPN.cs +++ b/ArcFormats/WildBug/AudioWPN.cs @@ -37,19 +37,18 @@ namespace GameRes.Formats.WildBug public override string Description { get { return "Wild Bug's audio format"; } } public override uint Signature { get { return 0x1A444257; } } // 'WBD' - public override SoundInput TryOpen (Stream file) + public override SoundInput TryOpen (IBinaryStream file) { - var header = new byte[0x24]; - if (header.Length != file.Read (header, 0, header.Length)) + var header = file.ReadHeader (0x24); + if (!header.AsciiEqual (4, "WAV")) return null; - if (!Binary.AsciiEqual (header, 4, "WAV")) + if (header.ToInt32 (8) < 2) return null; - if (LittleEndian.ToInt32 (header, 8) < 2) - return null; - int fmt_offset = LittleEndian.ToInt32 (header, 0x10); - int fmt_size = LittleEndian.ToInt32 (header, 0x14); - int data_offset = LittleEndian.ToInt32 (header, 0x1C); - int data_size = LittleEndian.ToInt32 (header, 0x20); + int fmt_offset = header.ToInt32 (0x10); + int fmt_size = header.ToInt32 (0x14); + int data_offset = header.ToInt32 (0x1C); + int data_size = header.ToInt32 (0x20); + var wav_header = new byte[8+12+fmt_size+8]; Buffer.BlockCopy (WavHeader, 0, wav_header, 0, WavHeader.Length); LittleEndian.Pack (wav_header.Length-8+data_size, wav_header, 4); @@ -62,7 +61,7 @@ namespace GameRes.Formats.WildBug wav_header[d++]= (byte)'t'; wav_header[d++]= (byte)'a'; LittleEndian.Pack (data_size, wav_header, d); - var wav_data = new StreamRegion (file, data_offset, data_size); + var wav_data = new StreamRegion (file.AsStream, data_offset, data_size); return new WaveInput (new PrefixStream (wav_header, wav_data)); } diff --git a/ArcFormats/WildBug/AudioWWA.cs b/ArcFormats/WildBug/AudioWWA.cs index c51ee912..1d506b15 100644 --- a/ArcFormats/WildBug/AudioWWA.cs +++ b/ArcFormats/WildBug/AudioWWA.cs @@ -37,12 +37,10 @@ namespace GameRes.Formats.WildBug public override string Description { get { return "Wild Bug's compressed audio format"; } } public override uint Signature { get { return 0x1A585057; } } // 'WPX' - public override SoundInput TryOpen (Stream file) + public override SoundInput TryOpen (IBinaryStream file) { - var header = new byte[0x10]; - if (header.Length != file.Read (header, 0, header.Length)) - return null; - if (!Binary.AsciiEqual (header, 4, "WAV")) + var header = file.ReadHeader (0x10); + if (!header.AsciiEqual (4, "WAV")) return null; int count = header[0xE]; int dir_size = header[0xF]; @@ -50,22 +48,19 @@ namespace GameRes.Formats.WildBug return null; file.Position = 0x10; - header = new byte[count * dir_size]; - if (header.Length != file.Read (header, 0, header.Length)) - throw new InvalidFormatException(); + var index = file.ReadBytes (count * dir_size); - var section = WpxSection.Find (header, 0x20, count, dir_size); + var section = WpxSection.Find (index, 0x20, count, dir_size); if (null == section || section.UnpackedSize < 0x10 || section.DataFormat != 0x80) throw new InvalidFormatException(); - var fmt = new byte[section.UnpackedSize]; file.Position = section.Offset; - file.Read (fmt, 0, section.UnpackedSize); + var fmt = file.ReadBytes (section.UnpackedSize); - section = WpxSection.Find (header, 0x21, count, dir_size); + section = WpxSection.Find (index, 0x21, count, dir_size); if (null == section) throw new InvalidFormatException(); - var reader = new WwaReader (file, section); + var reader = new WwaReader (file.AsStream, section); var data = reader.Unpack (section.DataFormat); if (null == data) throw new InvalidFormatException(); diff --git a/ArcFormats/WildBug/ImageWBM.cs b/ArcFormats/WildBug/ImageWBM.cs index 28e3febb..69b9a775 100644 --- a/ArcFormats/WildBug/ImageWBM.cs +++ b/ArcFormats/WildBug/ImageWBM.cs @@ -74,29 +74,25 @@ namespace GameRes.Formats.WildBug public override string Description { get { return "Wild Bug's image format"; } } public override uint Signature { get { return 0x1A585057; } } // 'WPX' - public override ImageMetaData ReadMetaData (Stream stream) + public override ImageMetaData ReadMetaData (IBinaryStream stream) { - byte[] header = new byte[0x10]; - if (header.Length != stream.Read (header, 0, header.Length)) - return null; - if (!Binary.AsciiEqual (header, 4, "BMP")) + var header = stream.ReadHeader (0x10); + if (!header.AsciiEqual (4, "BMP")) return null; int count = header[0xE]; int dir_size = header[0xF]; if (1 != header[0xC] || 0 == count || 0 == dir_size) return null; - header = new byte[count * dir_size]; - if (header.Length != stream.Read (header, 0, header.Length)) - return null; - var section = WpxSection.Find (header, 0x10, count, dir_size); + var dir = stream.ReadBytes (count * dir_size); + var section = WpxSection.Find (dir, 0x10, count, dir_size); if (null == section) return null; if (section.UnpackedSize < 0x10) return null; - stream.Seek (section.Offset, SeekOrigin.Begin); - byte[] data = new byte[section.UnpackedSize]; - if (data.Length != stream.Read (data, 0, data.Length)) + stream.Position = section.Offset; + var data = stream.ReadBytes (section.UnpackedSize); + if (data.Length != section.UnpackedSize) return null; return new WbmMetaData @@ -106,15 +102,13 @@ namespace GameRes.Formats.WildBug BPP = data[0xC], EntryCount = count, EntrySize = dir_size, - Header = header, + Header = header.ToArray(), }; } - public override ImageData Read (Stream stream, ImageMetaData info) + public override ImageData Read (IBinaryStream stream, ImageMetaData info) { - var meta = info as WbmMetaData; - if (null == meta) - throw new ArgumentException ("WbmFormat.Read should be supplied with WbmMetaData", "info"); + var meta = (WbmMetaData)info; var section = WpxSection.Find (meta.Header, 0x11, meta.EntryCount, meta.EntrySize); if (null == section) @@ -215,7 +209,7 @@ namespace GameRes.Formats.WildBug internal class WbmReader : WpxDecoder { - public WbmReader (Stream input, WpxSection section) : base (input, section) + public WbmReader (IBinaryStream input, WpxSection section) : base (input.AsStream, section) { } diff --git a/ArcFormats/Will/ArcPulltop.cs b/ArcFormats/Will/ArcPulltop.cs index b7e7c35a..d1dafedb 100644 --- a/ArcFormats/Will/ArcPulltop.cs +++ b/ArcFormats/Will/ArcPulltop.cs @@ -102,7 +102,7 @@ namespace GameRes.Formats.Will { data[i] = Binary.RotByteR (data[i], 2); } - return new MemoryStream (data); + return new BinMemoryStream (data, entry.Name); } } } diff --git a/ArcFormats/Will/ArcWILL.cs b/ArcFormats/Will/ArcWILL.cs index f1541cc5..871178c8 100644 --- a/ArcFormats/Will/ArcWILL.cs +++ b/ArcFormats/Will/ArcWILL.cs @@ -123,7 +123,7 @@ namespace GameRes.Formats.Will return arc.File.CreateStream (entry.Offset, entry.Size); var data = arc.File.View.ReadBytes (entry.Offset, entry.Size); DecodeScript (data); - return new MemoryStream (data); + return new BinMemoryStream (data, entry.Name); } private static void DecodeScript (byte[] data) diff --git a/ArcFormats/Will/ImageWIP.cs b/ArcFormats/Will/ImageWIP.cs index d748f15c..f5d5a877 100644 --- a/ArcFormats/Will/ImageWIP.cs +++ b/ArcFormats/Will/ImageWIP.cs @@ -51,36 +51,32 @@ namespace GameRes.Formats.Will Extensions = new string[] { "wip", "msk", "mos" }; } - public override ImageMetaData ReadMetaData (Stream stream) + public override ImageMetaData ReadMetaData (IBinaryStream file) { - using (var file = new BinaryReader (stream, Encoding.ASCII, true)) + file.Position = 4; + int frames = file.ReadUInt16(); + int bpp = file.ReadUInt16(); + uint width = file.ReadUInt32(); + uint height = file.ReadUInt32(); + int x = file.ReadInt32(); + int y = file.ReadInt32(); + file.ReadInt32(); // 0 + uint frame_size = file.ReadUInt32(); + if (24 != bpp && 8 != bpp) { - if (Signature != file.ReadUInt32()) - return null; - int frames = file.ReadUInt16(); - int bpp = file.ReadUInt16(); - uint width = file.ReadUInt32(); - uint height = file.ReadUInt32(); - int x = file.ReadInt32(); - int y = file.ReadInt32(); - file.ReadUInt32(); // 0 - uint frame_size = file.ReadUInt32(); - if (24 != bpp && 8 != bpp) - { - Trace.WriteLine ("unsupported bpp", "WipFormat"); - return null; - } - return new WipMetaData - { - Width = width, - Height = height, - OffsetX = x, - OffsetY = y, - BPP = bpp, - FrameCount = frames, - FrameSize = frame_size, - }; + Trace.WriteLine ("unsupported bpp", "WipFormat"); + return null; } + return new WipMetaData + { + Width = width, + Height = height, + OffsetX = x, + OffsetY = y, + BPP = bpp, + FrameCount = frames, + FrameSize = frame_size, + }; } public override void Write (Stream file, ImageData image) @@ -88,7 +84,7 @@ namespace GameRes.Formats.Will throw new NotImplementedException ("WipFormat.Write not implemented"); } - public override ImageData Read (Stream file, ImageMetaData info) + public override ImageData Read (IBinaryStream file, ImageMetaData info) { var meta = (WipMetaData)info; if (meta.FrameCount > 1) @@ -106,7 +102,7 @@ namespace GameRes.Formats.Will palette[i] = Color.FromRgb (palette_data[i*4], palette_data[i*4+1], palette_data[i*4+2]); } } - using (var reader = new Reader (file, meta)) + using (var reader = new Reader (file.AsStream, meta)) { reader.Unpack(); if (24 == meta.BPP) @@ -134,7 +130,7 @@ namespace GameRes.Formats.Will } } - internal class Reader : IDisposable + internal sealed class Reader : IDisposable { private BinaryReader m_input; private uint m_length; @@ -199,21 +195,13 @@ namespace GameRes.Formats.Will bool disposed = false; public void Dispose () - { - Dispose (true); - GC.SuppressFinalize (this); - } - - protected virtual void Dispose (bool disposing) { if (!disposed) { - if (disposing) - m_input.Dispose(); - m_input = null; - m_data = null; + m_input.Dispose(); disposed = true; } + GC.SuppressFinalize (this); } #endregion } diff --git a/ArcFormats/Xuse/ArcWAG.cs b/ArcFormats/Xuse/ArcWAG.cs index 493205b4..03b19be5 100644 --- a/ArcFormats/Xuse/ArcWAG.cs +++ b/ArcFormats/Xuse/ArcWAG.cs @@ -82,7 +82,7 @@ namespace GameRes.Formats.Xuse return arc.File.CreateStream (entry.Offset, entry.Size); var data = arc.File.View.ReadBytes (entry.Offset, entry.Size); Decrypt ((uint)entry.Offset, warc.Key, data); - return new MemoryStream (data); + return new BinMemoryStream (data, entry.Name); } static private byte[] GenerateKey (byte[] keyword) diff --git a/ArcFormats/Xuse/ArcXuse.cs b/ArcFormats/Xuse/ArcXuse.cs index 6e4d3c37..df8a4e90 100644 --- a/ArcFormats/Xuse/ArcXuse.cs +++ b/ArcFormats/Xuse/ArcXuse.cs @@ -185,7 +185,7 @@ namespace GameRes.Formats.Xuse { data[i] ^= key[i&0xF]; } - return new MemoryStream (data); + return new BinMemoryStream (data, entry.Name); } } } diff --git a/ArcFormats/YuRis/ArcYPF.cs b/ArcFormats/YuRis/ArcYPF.cs index 2ead8653..6b6ccb97 100644 --- a/ArcFormats/YuRis/ArcYPF.cs +++ b/ArcFormats/YuRis/ArcYPF.cs @@ -138,7 +138,7 @@ namespace GameRes.Formats.YuRis { var packed_entry = entry as PackedEntry; var ypf = arc as YpfArchive; - Stream input = arc.File.CreateStream (entry.Offset, entry.Size); + Stream input = base.OpenEntry (arc, entry); if (null != packed_entry && packed_entry.IsPacked) input = new ZLibStream (input, CompressionMode.Decompress); uint unpacked_size = null == packed_entry ? entry.Size : packed_entry.UnpackedSize; @@ -151,7 +151,7 @@ namespace GameRes.Formats.YuRis input.Read (data, 0, data.Length); if (Binary.AsciiEqual (data, 0, "YSTB")) DecryptYstb (data, ypf.ScriptKey); - return new MemoryStream (data); + return new BinMemoryStream (data, entry.Name); } } diff --git a/ArcFormats/YuRis/ImageYCG.cs b/ArcFormats/YuRis/ImageYCG.cs index 0a310519..89135c1b 100644 --- a/ArcFormats/YuRis/ImageYCG.cs +++ b/ArcFormats/YuRis/ImageYCG.cs @@ -48,27 +48,25 @@ namespace GameRes.Formats.YuRis public override string Description { get { return "YU-RIS compressed image format"; } } public override uint Signature { get { return 0x474359; } } // 'YCG' - public override ImageMetaData ReadMetaData (Stream stream) + public override ImageMetaData ReadMetaData (IBinaryStream stream) { - var header = new byte[0x38]; - if (header.Length != stream.Read (header, 0, header.Length)) - return null; + var header = stream.ReadHeader (0x38); return new YcgMetaData { - Width = LittleEndian.ToUInt32 (header, 4), - Height = LittleEndian.ToUInt32 (header, 8), - BPP = LittleEndian.ToInt32 (header, 12), - CompressionMethod = LittleEndian.ToInt32 (header, 0x10), - UnpackedSize1 = LittleEndian.ToInt32 (header, 0x20), - CompressedSize1 = LittleEndian.ToInt32 (header, 0x24), - UnpackedSize2 = LittleEndian.ToInt32 (header, 0x30), - CompressedSize2 = LittleEndian.ToInt32 (header, 0x34), + Width = header.ToUInt32 (4), + Height = header.ToUInt32 (8), + BPP = header.ToInt32 (12), + CompressionMethod = header.ToInt32 (0x10), + UnpackedSize1 = header.ToInt32 (0x20), + CompressedSize1 = header.ToInt32 (0x24), + UnpackedSize2 = header.ToInt32 (0x30), + CompressedSize2 = header.ToInt32 (0x34), }; } - public override ImageData Read (Stream stream, ImageMetaData info) + public override ImageData Read (IBinaryStream stream, ImageMetaData info) { - var reader = new YcgReader (stream, (YcgMetaData)info); + var reader = new YcgReader (stream.AsStream, (YcgMetaData)info); reader.Unpack(); return ImageData.Create (info, reader.Format, null, reader.Data); } diff --git a/ArcFormats/Yuka/ArcYKC.cs b/ArcFormats/Yuka/ArcYKC.cs index cd7175f1..99bdd42f 100644 --- a/ArcFormats/Yuka/ArcYKC.cs +++ b/ArcFormats/Yuka/ArcYKC.cs @@ -93,15 +93,14 @@ namespace GameRes.Formats.Yuka || !entry.Name.EndsWith (".yks", StringComparison.InvariantCultureIgnoreCase) || !arc.File.View.AsciiEqual (entry.Offset, "YKS001") || 1 != arc.File.View.ReadUInt16 (entry.Offset+6)) - return arc.File.CreateStream (entry.Offset, entry.Size); + return base.OpenEntry (arc, entry); // decrypt script contents - var data = new byte[entry.Size]; - arc.File.View.Read (entry.Offset, data, 0, entry.Size); + var data = arc.File.View.ReadBytes (entry.Offset, entry.Size); uint text_offset = LittleEndian.ToUInt32 (data, 0x20); for (uint i = text_offset; i < data.Length; ++i) data[i] ^= 0xAA; data[6] = 0; - return new MemoryStream (data); + return new BinMemoryStream (data, entry.Name); } public override void Create (Stream output, IEnumerable list, ResourceOptions options, diff --git a/ArcFormats/Yuka/ImageYKG.cs b/ArcFormats/Yuka/ImageYKG.cs index ea93b4aa..421eb289 100644 --- a/ArcFormats/Yuka/ImageYKG.cs +++ b/ArcFormats/Yuka/ImageYKG.cs @@ -52,44 +52,43 @@ namespace GameRes.Formats.Yuka static readonly byte[] PngPrefix = new byte[4] { 0x89, 'P'^0, 'N'^0, 'G'^0 }; - public override ImageMetaData ReadMetaData (Stream stream) + public override ImageMetaData ReadMetaData (IBinaryStream file) { - var header = new byte[0x40]; - if (header.Length != stream.Read (header, 0, header.Length)) - return null; - if (!Binary.AsciiEqual (header, 4, "00\0\0")) + var header = file.ReadHeader (0x40); + if (!header.AsciiEqual (4, "00\0\0")) return null; var ykg = new YkgMetaData { - DataOffset = LittleEndian.ToUInt32 (header, 0x28), - DataSize = LittleEndian.ToUInt32 (header, 0x2C) + DataOffset = header.ToUInt32 (0x28), + DataSize = header.ToUInt32 (0x2C) }; if (0 == ykg.DataOffset) - ykg.DataOffset = LittleEndian.ToUInt32 (header, 8); + ykg.DataOffset = header.ToUInt32 (8); if (ykg.DataOffset < 0x30) return null; if (0 == ykg.DataSize) - ykg.DataSize = (uint)(stream.Length - ykg.DataOffset); + ykg.DataSize = (uint)(file.Length - ykg.DataOffset); ImageMetaData info = null; - using (var img = new StreamRegion (stream, ykg.DataOffset, ykg.DataSize, true)) + using (var reg = new StreamRegion (file.AsStream, ykg.DataOffset, ykg.DataSize, true)) + using (var img = new BinaryStream (reg, file.Name)) { - if (4 != img.Read (header, 0, 4)) - return null; - if (Binary.AsciiEqual (header, "BM")) + var img_header = img.ReadHeader (4); + if (header.AsciiEqual ("BM")) { img.Position = 0; - info = ImageFormat.Bmp.ReadMetaData (img); + info = Bmp.ReadMetaData (img); ykg.Format = YkgImage.Bmp; } - else if (Binary.AsciiEqual (header, "\x89PNG")) + else if (header.AsciiEqual ("\x89PNG")) { img.Position = 0; info = Png.ReadMetaData (img); ykg.Format = YkgImage.Png; } - else if (Binary.AsciiEqual (header, "\x89GNP")) + else if (header.AsciiEqual ("\x89GNP")) { - using (var body = new StreamRegion (stream, ykg.DataOffset+4, ykg.DataSize-4, true)) - using (var png = new PrefixStream (PngPrefix, body)) + using (var body = new StreamRegion (file.AsStream, ykg.DataOffset+4, ykg.DataSize-4, true)) + using (var pre = new PrefixStream (PngPrefix, body)) + using (var png = new BinaryStream (pre, file.Name)) info = Png.ReadMetaData (png); ykg.Format = YkgImage.Gnp; } @@ -104,23 +103,24 @@ namespace GameRes.Formats.Yuka return ykg; } - public override ImageData Read (Stream stream, ImageMetaData info) + public override ImageData Read (IBinaryStream stream, ImageMetaData info) { - var meta = info as YkgMetaData; - if (null == meta) - throw new ArgumentException ("YkgFormat.Read should be supplied with YkgMetaData", "info"); + var meta = (YkgMetaData)info; switch (meta.Format) { case YkgImage.Bmp: - using (var bmp = new StreamRegion (stream, meta.DataOffset, meta.DataSize, true)) + using (var reg = new StreamRegion (stream.AsStream, meta.DataOffset, meta.DataSize, true)) + using (var bmp = new BinaryStream (reg, stream.Name)) return Bmp.Read (bmp, info); case YkgImage.Png: - using (var png = new StreamRegion (stream, meta.DataOffset, meta.DataSize, true)) + using (var reg = new StreamRegion (stream.AsStream, meta.DataOffset, meta.DataSize, true)) + using (var png = new BinaryStream (reg, stream.Name)) return Png.Read (png, info); case YkgImage.Gnp: - using (var body = new StreamRegion (stream, meta.DataOffset+4, meta.DataSize-4, true)) - using (var png = new PrefixStream (PngPrefix, body)) + using (var body = new StreamRegion (stream.AsStream, meta.DataOffset+4, meta.DataSize-4, true)) + using (var pre = new PrefixStream (PngPrefix, body)) + using (var png = new BinaryStream (pre, stream.Name)) return Png.Read (png, info); default: throw new InvalidFormatException(); diff --git a/ArcFormats/Zyx/ArcBDF.cs b/ArcFormats/Zyx/ArcBDF.cs index 7c622378..d0a43142 100644 --- a/ArcFormats/Zyx/ArcBDF.cs +++ b/ArcFormats/Zyx/ArcBDF.cs @@ -222,7 +222,7 @@ namespace GameRes.Formats.Zyx var bdf = arc as BdfArchive; var frame = entry as BdfFrame; if (null == bdf || null == frame) - return arc.File.CreateStream (entry.Offset, entry.Size); + return base.OpenEntry (arc, entry); var pixels = bdf.ReadFrame (frame); var header = new byte[0x12]; diff --git a/ArcFormats/Zyx/ImageSPL.cs b/ArcFormats/Zyx/ImageSPL.cs index 30d11e7c..bd26100b 100644 --- a/ArcFormats/Zyx/ImageSPL.cs +++ b/ArcFormats/Zyx/ImageSPL.cs @@ -49,54 +49,48 @@ namespace GameRes.Formats.Zyx public override string Description { get { return "Zyx tiled image format"; } } public override uint Signature { get { return 0; } } - public override ImageMetaData ReadMetaData (Stream stream) + public override ImageMetaData ReadMetaData (IBinaryStream file) { - using (var reader = new ArcView.Reader (stream)) + int count = file.ReadInt16(); + if (count <= 0) + return null; + var tiles = new Tile[count]; + for (int i = 0; i < count; ++i) { - int count = reader.ReadInt16(); - if (count <= 0) + var tile = new Tile(); + tile.Left = file.ReadInt16(); + tile.Top = file.ReadInt16(); + if (tile.Left < 0 || tile.Top < 0) return null; - var tiles = new Tile[count]; - for (int i = 0; i < count; ++i) - { - var tile = new Tile(); - tile.Left = reader.ReadInt16(); - tile.Top = reader.ReadInt16(); - if (tile.Left < 0 || tile.Top < 0) - return null; - tile.Right = reader.ReadInt16(); - tile.Bottom = reader.ReadInt16(); - if (tile.Right <= tile.Left || tile.Bottom <= tile.Top) - return null; - tiles[i] = tile; - } - int width = reader.ReadInt16(); - int height = reader.ReadInt16(); - if (width <= 0 || height <= 0) + tile.Right = file.ReadInt16(); + tile.Bottom = file.ReadInt16(); + if (tile.Right <= tile.Left || tile.Bottom <= tile.Top) return null; - foreach (var tile in tiles) - { - if (tile.Right > width || tile.Bottom > height) - return null; - } - return new SplMetaData - { - Width = (uint)width, - Height = (uint)height, - BPP = 24, - Tiles = tiles, - DataOffset = stream.Position, - }; + tiles[i] = tile; } + int width = file.ReadInt16(); + int height = file.ReadInt16(); + if (width <= 0 || height <= 0) + return null; + foreach (var tile in tiles) + { + if (tile.Right > width || tile.Bottom > height) + return null; + } + return new SplMetaData + { + Width = (uint)width, + Height = (uint)height, + BPP = 24, + Tiles = tiles, + DataOffset = file.Position, + }; } - public override ImageData Read (Stream stream, ImageMetaData info) + public override ImageData Read (IBinaryStream stream, ImageMetaData info) { - var meta = info as SplMetaData; - if (null == meta) - throw new System.ArgumentException ("SplFormat.Read should be supplied with SplMetaData", "info"); - - var reader = new SplReader (stream, meta); + var meta = (SplMetaData)info; + var reader = new SplReader (stream.AsStream, meta); reader.Unpack (); return ImageData.Create (info, PixelFormats.Bgr24, null, reader.Data); } diff --git a/ArcFormats/elf/ImageG24.cs b/ArcFormats/elf/ImageG24.cs index 80d31362..5ef39e5e 100644 --- a/ArcFormats/elf/ImageG24.cs +++ b/ArcFormats/elf/ImageG24.cs @@ -38,33 +38,30 @@ namespace GameRes.Formats.Elf public override string Description { get { return "Ai5 engine RGB image format"; } } public override uint Signature { get { return 0; } } - public override ImageMetaData ReadMetaData (Stream stream) + public override ImageMetaData ReadMetaData (IBinaryStream input) { - using (var input = new ArcView.Reader (stream)) - { - int x = input.ReadInt16(); - int y = input.ReadInt16(); - int w = input.ReadInt16(); - int h = input.ReadInt16(); - if (w <= 0 || w > 0x1000 || h <= 0 || h > 0x1000 - || x < 0 || x > 0x800 || y < 0 || y > 0x800) - return null; - return new ImageMetaData { - Width = (uint)w, - Height = (uint)h, - OffsetX = x, - OffsetY = y, - BPP = 24, - }; - } + int x = input.ReadInt16(); + int y = input.ReadInt16(); + int w = input.ReadInt16(); + int h = input.ReadInt16(); + if (w <= 0 || w > 0x1000 || h <= 0 || h > 0x1000 + || x < 0 || x > 0x800 || y < 0 || y > 0x800) + return null; + return new ImageMetaData { + Width = (uint)w, + Height = (uint)h, + OffsetX = x, + OffsetY = y, + BPP = 24, + }; } - public override ImageData Read (Stream stream, ImageMetaData info) + public override ImageData Read (IBinaryStream stream, ImageMetaData info) { stream.Position = 8; int stride = (((int)info.Width * 3 + 3) & -4); var pixels = new byte[stride * (int)info.Height]; - using (var reader = new LzssStream (stream, LzssMode.Decompress, true)) + using (var reader = new LzssStream (stream.AsStream, LzssMode.Decompress, true)) { if (pixels.Length != reader.Read (pixels, 0, pixels.Length)) throw new InvalidFormatException(); diff --git a/ArcFormats/elf/ImageGCC.cs b/ArcFormats/elf/ImageGCC.cs index ff1b4033..58437006 100644 --- a/ArcFormats/elf/ImageGCC.cs +++ b/ArcFormats/elf/ImageGCC.cs @@ -52,34 +52,26 @@ namespace GameRes.Formats.Elf Signatures = new uint[] { 0x6d343252, 0x6E343252, 0x6D343247, 0x6E343247 }; } - public override ImageMetaData ReadMetaData (Stream stream) + public override ImageMetaData ReadMetaData (IBinaryStream stream) { - var header = new byte[12]; - if (header.Length != stream.Read (header, 0, header.Length)) - return null; - + var header = stream.ReadHeader (12); return new GccMetaData { - Width = LittleEndian.ToUInt16 (header, 8), - Height = LittleEndian.ToUInt16 (header, 10), + Width = header.ToUInt16 (8), + Height = header.ToUInt16 (10), BPP = 'm' == header[3] ? 32 : 24, - OffsetX = LittleEndian.ToInt16 (header, 4), - OffsetY = LittleEndian.ToInt16 (header, 6), - Signature = LittleEndian.ToUInt32 (header, 0), + OffsetX = header.ToInt16 (4), + OffsetY = header.ToInt16 (6), + Signature = header.ToUInt32 (0), }; } - public override ImageData Read (Stream stream, ImageMetaData info) + public override ImageData Read (IBinaryStream stream, ImageMetaData info) { - var meta = info as GccMetaData; - if (null == meta) - throw new ArgumentException ("GccFormat.Read should be supplied with GccMetaData", "info"); - - var reader = new Reader (stream, meta); - { - reader.Unpack(); - return ImageData.Create (info, reader.Format, null, reader.Data); - } + var meta = (GccMetaData)info; + var reader = new Reader (stream.AsStream, meta); + reader.Unpack(); + return ImageData.Create (info, reader.Format, null, reader.Data); } public override void Write (Stream file, ImageData image) diff --git a/ArcFormats/elf/ImageGP8.cs b/ArcFormats/elf/ImageGP8.cs index c532ba8f..18192ccc 100644 --- a/ArcFormats/elf/ImageGP8.cs +++ b/ArcFormats/elf/ImageGP8.cs @@ -39,16 +39,14 @@ namespace GameRes.Formats.Elf public override string Description { get { return "Ai5 engine indexed image format"; } } public override uint Signature { get { return 0; } } - public override ImageMetaData ReadMetaData (Stream stream) + public override ImageMetaData ReadMetaData (IBinaryStream file) { - if (stream.Length <= 0x408) + if (file.Length <= 0x408) return null; - var header = new byte[8]; - stream.Read (header, 0, 8); - if (0 != LittleEndian.ToInt32 (header, 0)) + if (0 != file.ReadInt32()) return null; - int w = LittleEndian.ToInt16 (header, 4); - int h = LittleEndian.ToInt16 (header, 6); + int w = file.ReadInt16(); + int h = file.ReadInt16(); if (w <= 0 || w > 0x1000 || h <= 0 || h > 0x1000) return null; return new ImageMetaData { @@ -58,12 +56,12 @@ namespace GameRes.Formats.Elf }; } - public override ImageData Read (Stream stream, ImageMetaData info) + public override ImageData Read (IBinaryStream stream, ImageMetaData info) { stream.Position = 8; - var palette = ReadPalette (stream); + var palette = ReadPalette (stream.AsStream); var pixels = new byte[info.Width * info.Height]; - using (var reader = new LzssStream (stream, LzssMode.Decompress, true)) + using (var reader = new LzssStream (stream.AsStream, LzssMode.Decompress, true)) { if (pixels.Length != reader.Read (pixels, 0, pixels.Length)) throw new InvalidFormatException(); @@ -103,32 +101,29 @@ namespace GameRes.Formats.Elf Extensions = new string[] { "msk" }; } - public override ImageMetaData ReadMetaData (Stream stream) + public override ImageMetaData ReadMetaData (IBinaryStream input) { - using (var input = new ArcView.Reader (stream)) - { - int x = input.ReadInt16(); - int y = input.ReadInt16(); - int w = input.ReadInt16(); - int h = input.ReadInt16(); - if (w <= 0 || w > 0x1000 || h <= 0 || h > 0x1000 - || x < 0 || x > 0x800 || y < 0 || y > 0x800) - return null; - return new ImageMetaData { - Width = (uint)w, - Height = (uint)h, - OffsetX = x, - OffsetY = y, - BPP = 8, - }; - } + int x = input.ReadInt16(); + int y = input.ReadInt16(); + int w = input.ReadInt16(); + int h = input.ReadInt16(); + if (w <= 0 || w > 0x1000 || h <= 0 || h > 0x1000 + || x < 0 || x > 0x800 || y < 0 || y > 0x800) + return null; + return new ImageMetaData { + Width = (uint)w, + Height = (uint)h, + OffsetX = x, + OffsetY = y, + BPP = 8, + }; } - public override ImageData Read (Stream stream, ImageMetaData info) + public override ImageData Read (IBinaryStream stream, ImageMetaData info) { stream.Position = 8; var pixels = new byte[info.Width * info.Height]; - using (var reader = new LzssStream (stream, LzssMode.Decompress, true)) + using (var reader = new LzssStream (stream.AsStream, LzssMode.Decompress, true)) { if (pixels.Length != reader.Read (pixels, 0, pixels.Length)) throw new InvalidFormatException(); diff --git a/ArcFormats/elf/ImageGPH.cs b/ArcFormats/elf/ImageGPH.cs index 71246bee..f132e2d8 100644 --- a/ArcFormats/elf/ImageGPH.cs +++ b/ArcFormats/elf/ImageGPH.cs @@ -119,12 +119,12 @@ namespace GameRes.Formats.Elf public void Unpack () { - m_input.BaseStream.Position = m_info.DataOffset+2; + m_input.Position = m_info.DataOffset+2; if (0 == (m_info.Flags & 4)) ReadPalette(); else SetDefaultPalette(); - m_input.BaseStream.Seek (8, SeekOrigin.Current); + m_input.Seek (8, SeekOrigin.Current); int stride = Stride; if (stride <= 0x10) @@ -379,7 +379,7 @@ namespace GameRes.Formats.Elf void ReadNext () { bits &= 0xFF00; - int b = m_input.BaseStream.ReadByte(); + int b = m_input.ReadByte(); if (-1 != b) bits |= b; } diff --git a/ArcFormats/elf/ImageHIZ.cs b/ArcFormats/elf/ImageHIZ.cs index 031ce9b1..1ebfb668 100644 --- a/ArcFormats/elf/ImageHIZ.cs +++ b/ArcFormats/elf/ImageHIZ.cs @@ -46,43 +46,38 @@ namespace GameRes.Formats.Elf public override string Description { get { return "elf bitmap format"; } } public override uint Signature { get { return 0x007A6968; } } // 'hiz' - public override ImageMetaData ReadMetaData (Stream stream) + public override ImageMetaData ReadMetaData (IBinaryStream file) { - using (var input = new ArcView.Reader (stream)) // sub_4BF900 + file.Position = 4; + int n = file.ReadInt32(); + if (100 != n) + return null; + uint right = file.ReadUInt32() ^ 0xAA5A5A5A; + uint bottom = file.ReadUInt32() ^ 0xAC9326AF; + int unknown1 = file.ReadInt32(); // @0x10 + if (unknown1 == 0x375A8436) + return null; + uint unpacked_size = file.ReadUInt32() ^ 0x19739D6A; // @0x14 + if (unpacked_size != right*bottom*4) + return null; + return new HizMetaData { - input.ReadInt32(); - int n = input.ReadInt32(); - if (100 != n) - return null; - uint right = input.ReadUInt32() ^ 0xAA5A5A5A; - uint bottom = input.ReadUInt32() ^ 0xAC9326AF; - int unknown1 = input.ReadInt32(); // @0x10 - if (unknown1 == 0x375A8436) - return null; - uint unpacked_size = input.ReadUInt32() ^ 0x19739D6A; // @0x14 - if (unpacked_size != right*bottom*4) - return null; - return new HizMetaData - { - Width = right, - Height = bottom, - BPP = 32, - IsPacked = true, - DataOffset = 0x4c, - UnpackedSize = unpacked_size, - }; - } + Width = right, + Height = bottom, + BPP = 32, + IsPacked = true, + DataOffset = 0x4c, + UnpackedSize = unpacked_size, + }; } - public override ImageData Read (Stream stream, ImageMetaData info) + public override ImageData Read (IBinaryStream stream, ImageMetaData info) { - var meta = info as HizMetaData; - if (null == meta) - throw new ArgumentException ("HizFormat.Read should be supplied with HizMetaData", "info"); + var meta = (HizMetaData)info; var pixels = new byte[meta.UnpackedSize]; stream.Position = meta.DataOffset; - using (var lzss = new LzssStream (stream, LzssMode.Decompress, true)) + using (var lzss = new LzssStream (stream.AsStream, LzssMode.Decompress, true)) { var channel = new byte[info.Width*info.Height]; for (int p = 0; p < 4; ++p) @@ -110,24 +105,22 @@ namespace GameRes.Formats.Elf public override string Description { get { return "elf composite image format"; } } public override uint Signature { get { return 0x00706968; } } // 'hip' - public override ImageMetaData ReadMetaData (Stream stream) + public override ImageMetaData ReadMetaData (IBinaryStream stream) { - byte[] header = new byte[0x18]; - if (0x18 != stream.Read (header, 0, 0x18)) - return null; + var header = stream.ReadHeader (0x18); int index_offset = 0xC; - uint first_offset = LittleEndian.ToUInt32 (header, index_offset); + uint first_offset = header.ToUInt32 (index_offset); if (0 == first_offset) { index_offset += 4; - first_offset = LittleEndian.ToUInt32 (header, index_offset); + first_offset = header.ToUInt32 (index_offset); if (0 == first_offset) return null; } index_offset += 4; long first_length; - uint second_offset = LittleEndian.ToUInt32 (header, index_offset); + uint second_offset = header.ToUInt32 (index_offset); if (0 == second_offset) first_length = stream.Length - first_offset; else if (second_offset < first_offset) @@ -135,7 +128,8 @@ namespace GameRes.Formats.Elf else first_length = second_offset - first_offset; - using (var hiz = new StreamRegion (stream, first_offset, first_length, true)) + using (var reg = new StreamRegion (stream.AsStream, first_offset, first_length, true)) + using (var hiz = new BinaryStream (reg, stream.Name)) { var info = base.ReadMetaData (hiz); (info as HizMetaData).DataOffset += 0x18; diff --git a/ArcFormats/uGOS/ArcDET.cs b/ArcFormats/uGOS/ArcDET.cs index 85104a08..b4445e94 100644 --- a/ArcFormats/uGOS/ArcDET.cs +++ b/ArcFormats/uGOS/ArcDET.cs @@ -87,7 +87,7 @@ namespace GameRes.Formats.uGOS using (var input = arc.File.CreateStream (entry.Offset, entry.Size)) { if (Unpack (input, output)) - return new MemoryStream (output); + return new BinMemoryStream (output, entry.Name); else return base.OpenEntry (arc, entry); } diff --git a/ArcFormats/uGOS/ImageBMP.cs b/ArcFormats/uGOS/ImageBMP.cs index 03916e80..ebbaa455 100644 --- a/ArcFormats/uGOS/ImageBMP.cs +++ b/ArcFormats/uGOS/ImageBMP.cs @@ -44,22 +44,20 @@ namespace GameRes.Formats.uGOS Signatures = new uint[] { 0x206546, 0x186546 }; } - public override ImageMetaData ReadMetaData (Stream stream) + public override ImageMetaData ReadMetaData (IBinaryStream stream) { - var header = new byte[0x10]; - if (header.Length != stream.Read (header, 0, header.Length)) - return null; + var header = stream.ReadHeader (0x10); return new ImageMetaData { - Width = LittleEndian.ToUInt16 (header, 4), - Height = LittleEndian.ToUInt16 (header, 6), + Width = header.ToUInt16 (4), + Height = header.ToUInt16 (6), BPP = header[2], }; } - public override ImageData Read (Stream stream, ImageMetaData info) + public override ImageData Read (IBinaryStream file, ImageMetaData info) { - using (var reader = new Reader (stream, info)) + using (var reader = new Reader (file, info)) { reader.Unpack(); return ImageData.CreateFlipped (info, reader.Format, null, reader.Data, reader.Stride); @@ -73,7 +71,7 @@ namespace GameRes.Formats.uGOS internal sealed class Reader : IDisposable { - BinaryReader m_input; + IBinaryStream m_input; byte[] m_output; int m_width; int m_height; @@ -83,9 +81,9 @@ namespace GameRes.Formats.uGOS public byte[] Data { get { return m_output; } } public int Stride { get; private set; } - public Reader (Stream input, ImageMetaData info) + public Reader (IBinaryStream input, ImageMetaData info) { - m_input = new ArcView.Reader (input); + m_input = input; m_width = (int)info.Width; m_height = (int)info.Height; m_bpp = info.BPP; @@ -122,7 +120,7 @@ namespace GameRes.Formats.uGOS public void Unpack () { - m_input.BaseStream.Position = 0x10; + m_input.Position = 0x10; InitTable1(); InitTable2(); InitTable3(); @@ -183,10 +181,10 @@ namespace GameRes.Formats.uGOS if (0 == v35) { // v60 = (((src1[2] + ((src1[1] + (*src1 << 8)) << 8)) << 8) ^ 0x80000080u) >> byte_4CBA80[v38]; - uint v60 = (uint)m_input.ReadByte() << 16; + uint v60 = (uint)m_input.ReadUInt8() << 16; uint v38 = m_bits & 0xFF; - v60 |= (uint)m_input.ReadByte () << 8; - v60 |= m_input.ReadByte (); + v60 |= (uint)m_input.ReadUInt8 () << 8; + v60 |= m_input.ReadUInt8 (); v60 <<= 8; v60 = (v60 ^ 0x80000080u) >> byte_4CBA80[v38]; m_bits = v60; @@ -208,7 +206,7 @@ namespace GameRes.Formats.uGOS int d; if (0x80 == (m_bits & 0xFF)) { - byte n = m_input.ReadByte(); + byte n = m_input.ReadUInt8(); d = n >> 7; m_bits = (uint)(n << 1 | 1); } @@ -260,7 +258,7 @@ namespace GameRes.Formats.uGOS int v23 = byte_4CAD28[2 * m_bits + 1]; while (0 == v23) { - int b = m_input.ReadByte(); + int b = m_input.ReadUInt8(); v26 += byte_4CAF28[2 * b]; v23 = byte_4CAF28[2 * b + 1]; } @@ -269,12 +267,12 @@ namespace GameRes.Formats.uGOS if (v26 - v27 > 0) { uint v29 = (uint)(v26 - v27 - 1); - int v30 = m_input.ReadByte() + (int)((v28 << v27) & 0xFFFFFF00); + int v30 = m_input.ReadUInt8() + (int)((v28 << v27) & 0xFFFFFF00); if ((int)v29 >= 8) { for (uint v31 = v29 >> 3; v31 != 0; --v31) { - v30 = (v30 << 8) | m_input.ReadByte(); + v30 = (v30 << 8) | m_input.ReadUInt8(); } v29 -= 8 * (v29 >> 3); } @@ -305,13 +303,13 @@ namespace GameRes.Formats.uGOS uint v7 = ((uint)(v5 - 9) >> 3) + 1; do { - v6 = m_input.ReadByte() + (v6 << 8); + v6 = m_input.ReadUInt8() + (v6 << 8); v5 -= 8; --v7; } while (0 != v7); } - m_bits = m_input.ReadByte(); + m_bits = m_input.ReadUInt8(); alpha = (uint)((v6 << v5) + (m_bits >> (8 - v5))); m_bits = ((m_bits << v5) + (1u << (v5 - 1))) & 0xFFu; } @@ -326,7 +324,7 @@ namespace GameRes.Formats.uGOS uint v4 = byte_4CAD28[2 * v2 + 1]; while (0 == v4) { - v2 = m_input.ReadByte(); + v2 = m_input.ReadUInt8(); i += byte_4CAF28[2 * v2]; v4 = byte_4CAF28[2 * v2 + 1]; } @@ -345,11 +343,11 @@ namespace GameRes.Formats.uGOS { for (uint v8 = ((uint)(v6 - 9) >> 3) + 1; v8 != 0; --v8) { - v7 = (v7 << 8) | m_input.ReadByte(); + v7 = (v7 << 8) | m_input.ReadUInt8(); } v6 += -8 * (int)(((uint)(v6 - 9) >> 3) + 1); } - m_bits = m_input.ReadByte(); + m_bits = m_input.ReadUInt8(); uint n = (v7 << v6) + (m_bits >> (8 - v6)); m_bits = (m_bits << v6) + (1u << (v6 - 1)); return (int)n - 2; @@ -433,14 +431,8 @@ namespace GameRes.Formats.uGOS } #region IDisposable Members - bool _disposed = false; public void Dispose () { - if (!_disposed) - { - m_input.Dispose(); - _disposed = true; - } } #endregion } diff --git a/GameRes/ArcView.cs b/GameRes/ArcView.cs index fd2efa82..522d5d46 100644 --- a/GameRes/ArcView.cs +++ b/GameRes/ArcView.cs @@ -232,9 +232,9 @@ namespace GameRes return new ArcStream (this, offset, (uint)size); } - public ArcStream CreateStream (long offset, uint size) + public ArcStream CreateStream (long offset, uint size, string name = null) { - return new ArcStream (this, offset, size); + return new ArcStream (this, offset, size, name); } public MemoryMappedViewAccessor CreateViewAccessor (long offset, uint size) @@ -510,27 +510,30 @@ namespace GameRes m_start = 0; m_size = file.MaxOffset; m_position = 0; + Name = file.Name; } - public ArcStream (Frame view) + public ArcStream (Frame view, string name = null) { m_view = view; m_start = m_view.Offset; m_size = m_view.Reserved; m_position = 0; + Name = name ?? ""; } - public ArcStream (ArcView file, long offset, uint size) - : this (new Frame (file, offset, size)) + public ArcStream (ArcView file, long offset, uint size, string name = null) + : this (new Frame (file, offset, size), name) { } - public ArcStream (Frame view, long offset, uint size) + public ArcStream (Frame view, long offset, uint size, string name = null) { m_view = view; m_start = offset; m_size = Math.Min (size, m_view.Reserve (offset, size)); m_position = 0; + Name = name ?? ""; } /// @@ -573,7 +576,7 @@ namespace GameRes return b; } - public sbyte ReadSByte () + public sbyte ReadInt8 () { int b = ReadByte(); if (-1 == b) @@ -581,6 +584,11 @@ namespace GameRes return (sbyte)b; } + public byte ReadUInt8 () + { + return (byte)ReadInt8(); + } + public short ReadInt16 () { if (m_position + 2 > m_size) diff --git a/GameRes/BinaryStream.cs b/GameRes/BinaryStream.cs index 67238406..0706dbec 100644 --- a/GameRes/BinaryStream.cs +++ b/GameRes/BinaryStream.cs @@ -43,6 +43,7 @@ namespace GameRes /// uint Signature { get; } Stream AsStream { get; } + bool CanSeek { get; } long Length { get; } long Position { get; set; } @@ -57,7 +58,8 @@ namespace GameRes /// Next byte, or -1 if end of stream is reached. int PeekByte (); - sbyte ReadSByte (); + sbyte ReadInt8 (); + byte ReadUInt8 (); short ReadInt16 (); ushort ReadUInt16 (); int ReadInt24 (); @@ -102,127 +104,6 @@ namespace GameRes byte[] ReadBytes (int count); } - /// - /// Array segment with copy-on-write semantics. - /// - public struct CowArray : IReadOnlyList - { - T[] m_source; - int m_offset; - int m_count; - bool m_own_copy; - - public CowArray (T[] src) : this (src, 0, src.Length) - { - } - - public CowArray (T[] src, int start, int length) - { - m_source = src; - m_offset = start; - m_count = length; - m_own_copy = false; - } - - public int Count { get { return m_count; } } - public int Length { get { return Count; } } - public T this[int pos] - { - get { return m_source[m_offset+pos]; } - set - { - if (!m_own_copy) - { - Reclaim(); - } - m_source[pos] = value; - } - } - - public IEnumerator GetEnumerator () - { - for (int i = 0; i < m_count; ++i) - yield return m_source[m_offset + i]; - } - - IEnumerator IEnumerable.GetEnumerator () - { - return GetEnumerator(); - } - - public T[] ToArray () - { - if (m_own_copy) - return m_source; - var copy = new T[m_count]; - Array.Copy (m_source, m_offset, copy, 0, m_count); - return copy; - } - - internal void Reclaim () - { - m_source = ToArray(); - m_offset = 0; - m_own_copy = true; - } - } - - public static class CowByteArray - { - public static ushort ToUInt16 (this CowArray arr, int index) - { - return (ushort)(arr[index] | arr[index+1] << 8); - } - - public static short ToInt16 (this CowArray arr, int index) - { - return (short)(arr[index] | arr[index+1] << 8); - } - - public static int ToInt24 (this CowArray arr, int index) - { - return arr[index] | arr[index+1] << 8 | arr[index+2] << 16; - } - - public static uint ToUInt32 (this CowArray arr, int index) - { - return (uint)(arr[index] | arr[index+1] << 8 | arr[index+2] << 16 | arr[index+3] << 24); - } - - public static int ToInt32 (this CowArray arr, int index) - { - return (int)ToUInt32 (arr, index); - } - - public static ulong ToUInt64 (this CowArray arr, int index) - { - return (ulong)ToUInt32 (arr, index) | ((ulong)ToUInt32 (arr, index+4) << 32); - } - - public static long ToInt64 (this CowArray arr, int index) - { - return (long)ToUInt64 (arr, index); - } - - public static bool AsciiEqual (this CowArray arr, int index, string str) - { - arr.Reclaim(); - return Binary.AsciiEqual (arr.ToArray(), index, str); - } - - public static bool AsciiEqual (this CowArray arr, string str) - { - arr.Reclaim(); - return Binary.AsciiEqual (arr.ToArray(), str); - } - - public static string GetCString (this CowArray arr, int index, int length_limit) - { - arr.Reclaim(); - return Binary.GetCString (arr.ToArray(), index, length_limit); - } - } - public class BinaryStream : Stream, IBinaryStream { Stream m_source; @@ -238,9 +119,9 @@ namespace GameRes public uint Signature { get { return m_signature.Value; } } public Stream AsStream { get { return this; } } - public BinaryStream (Stream input, bool leave_open = false) : this (input, "", leave_open) - { - } +// public BinaryStream (Stream input, bool leave_open = false) : this (input, "", leave_open) +// { +// } public BinaryStream (Stream input, string name, bool leave_open = false) { @@ -266,12 +147,24 @@ namespace GameRes } } - public static BinaryStream FromFile (string filename) + public static IBinaryStream FromFile (string filename) { var stream = File.OpenRead (filename); return new BinaryStream (stream, filename); } + public static IBinaryStream FromArray (byte[] data, string filename) + { + return new BinMemoryStream (data, filename); + } + + public static IBinaryStream FromStream (Stream input, string filename) + { + if (input is IBinaryStream) + return input as IBinaryStream; + return new BinaryStream (input, filename); + } + uint ReadSignature () { if (m_header_size >= 4) @@ -338,13 +231,18 @@ namespace GameRes return m_buffer[m_buffer_pos]; } - public sbyte ReadSByte () + public sbyte ReadInt8 () { if (1 != FillBuffer (1)) throw new EndOfStreamException(); return (sbyte)m_buffer[m_buffer_pos++]; } + public byte ReadUInt8 () + { + return (byte)ReadInt8(); + } + public short ReadInt16 () { if (2 != FillBuffer (2)) @@ -549,4 +447,239 @@ namespace GameRes } #endregion } + + public class BinMemoryStream : Stream, IBinaryStream + { + byte[] m_source; + int m_start; + int m_length; + int m_position; + uint m_signature; + + public string Name { get; private set; } + public uint Signature { get { return m_signature; } } + public Stream AsStream { get { return this; } } + + public BinMemoryStream (byte[] input, string name) : this (input, 0, input.Length, name) + { } + + public BinMemoryStream (byte[] input, int pos, int length, string name) + { + m_source = input; + m_start = pos; + m_length = length; + Init (name); + } + + public BinMemoryStream (MemoryStream input, string name) + { + try + { + m_source = input.GetBuffer(); + if (null == m_source) + m_source = new byte[0]; + } + catch (UnauthorizedAccessException) + { + m_source = input.ToArray(); + } + m_start = 0; + m_length = (int)input.Length; + Init (name); + } + + void Init (string name) + { + m_position = 0; + Name = name ?? ""; + if (m_length >= 4) + m_signature = LittleEndian.ToUInt32 (m_source, 0); + } + + public CowArray ReadHeader (int size) + { + if (size > m_length) + { + m_position = m_length; + throw new EndOfStreamException(); + } + m_position = size; + return new CowArray (m_source, m_start, size); + } + + public int PeekByte () + { + if (m_position >= m_length) + return -1; + return m_source[m_start+m_position]; + } + + public sbyte ReadInt8 () + { + if (m_position >= m_length) + throw new EndOfStreamException(); + return (sbyte)m_source[m_start+m_position++]; + } + + public byte ReadUInt8 () + { + return (byte)ReadInt8(); + } + + public short ReadInt16 () + { + if (m_length - m_position < 2) + throw new EndOfStreamException(); + short v = LittleEndian.ToInt16 (m_source, m_start+m_position); + m_position += 2; + return v; + } + + public ushort ReadUInt16 () + { + return (ushort)ReadInt16(); + } + + public int ReadInt24 () + { + if (m_length - m_position < 3) + throw new EndOfStreamException(); + int v = LittleEndian.ToUInt16 (m_source, m_start+m_position); + v |= m_source[m_start+m_position+2] << 16; + m_position += 3; + return v; + } + + public int ReadInt32 () + { + if (m_length - m_position < 4) + throw new EndOfStreamException(); + int v = LittleEndian.ToInt32 (m_source, m_start+m_position); + m_position += 4; + return v; + } + + public uint ReadUInt32 () + { + return (uint)ReadInt32(); + } + + public long ReadInt64 () + { + if (m_length - m_position < 8) + throw new EndOfStreamException(); + long v = LittleEndian.ToInt64 (m_source, m_start+m_position); + m_position += 8; + return v; + } + + public ulong ReadUInt64 () + { + return (ulong)ReadInt64(); + } + + public string ReadCString (int length) + { + return ReadCString (length, Encodings.cp932); + } + + public string ReadCString (int length, Encoding enc) + { + length = Math.Min (length, m_length - m_position); + int i = Array.IndexOf (m_source, 0, m_start+m_position, length); + if (-1 == i) + i = length; + string s = enc.GetString (m_source, m_start+m_position, i); + m_position += length; + return s; + } + + public string ReadCString () + { + return ReadCString (Encodings.cp932); + } + + public string ReadCString (Encoding enc) + { + int start = m_start+m_position; + int count = Array.IndexOf (m_source, 0, start, m_length-m_position); + if (-1 == count) + { + count = m_length - m_position; + m_position = m_length; + } + else + { + m_position += count + 1; + } + return enc.GetString (m_source, start, count); + } + + public byte[] ReadBytes (int count) + { + count = Math.Min (count, m_length - m_position); + var buffer = new byte[count]; + Buffer.BlockCopy (m_source, m_start+m_position, buffer, 0, count); + return buffer; + } + + #region IO.Stream Members + public override bool CanRead { get { return true; } } + public override bool CanSeek { get { return true; } } + public override bool CanWrite { get { return false; } } + public override long Length { get { return m_length; } } + public override long Position + { + get { return m_position; } + set { m_position = (int)Math.Max (Math.Min (m_length, value), 0); } + } + + public override int Read (byte[] buffer, int offset, int count) + { + count = Math.Min (count, m_length - m_position); + Buffer.BlockCopy (m_source, m_start+m_position, buffer, offset, count); + m_position += count; + return count; + } + + public override int ReadByte () + { + if (m_position < m_length) + return m_source[m_start+m_position++]; + else + return -1; + } + + public override void Flush() + { + } + + public override long Seek (long offset, SeekOrigin origin) + { + if (SeekOrigin.Begin == origin) + Position = offset; + else if (SeekOrigin.Current == origin) + Position = m_position + offset; + else + Position = Length + offset; + + return m_position; + } + + public override void SetLength (long length) + { + throw new NotSupportedException ("BinaryStream.SetLength method is not supported"); + } + + public override void Write (byte[] buffer, int offset, int count) + { + throw new NotSupportedException ("BinaryStream.Write method is not supported"); + } + + public override void WriteByte (byte value) + { + throw new NotSupportedException ("BinaryStream.WriteByte method is not supported"); + } + #endregion + } } diff --git a/GameRes/ByteArray.cs b/GameRes/ByteArray.cs new file mode 100644 index 00000000..b9352405 --- /dev/null +++ b/GameRes/ByteArray.cs @@ -0,0 +1,196 @@ +//! \file ByteArray.cs +//! \date Sun Oct 16 06:25:52 2016 +//! \brief specialized collection for copy-on-write byte arrays. +// +// Copyright (C) 2016 by morkt +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to +// deal in the Software without restriction, including without limitation the +// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or +// sell copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS +// IN THE SOFTWARE. +// + +using System; +using System.Collections; +using System.Collections.Generic; +using GameRes.Utility; + +namespace GameRes +{ + /// + /// Array segment with copy-on-write semantics. + /// + public struct CowArray : IList + { + T[] m_source; + int m_offset; + int m_count; + bool m_own_copy; + + public CowArray (T[] src) : this (src, 0, src.Length) + { + } + + public CowArray (T[] src, int start, int length) + { + m_source = src; + m_offset = start; + m_count = length; + m_own_copy = false; + } + + public int Count { get { return m_count; } } + public int Length { get { return Count; } } + public bool IsReadOnly { get { return true; } } + public T this[int pos] + { + get { return m_source[m_offset+pos]; } + set + { + if (!m_own_copy) + { + Reclaim(); + } + m_source[pos] = value; + } + } + + public int IndexOf (T item) + { + return Array.IndexOf (m_source, item, m_offset, m_count); + } + + public bool Contains (T item) + { + return IndexOf (item) != -1; + } + + public void CopyTo (T[] arr, int dst) + { + Array.Copy (m_source, m_offset, arr, dst, m_count); + } + + public IEnumerator GetEnumerator () + { + for (int i = 0; i < m_count; ++i) + yield return m_source[m_offset + i]; + } + + IEnumerator IEnumerable.GetEnumerator () + { + return GetEnumerator(); + } + + public T[] ToArray () + { + if (m_own_copy) + return m_source; + var copy = new T[m_count]; + Array.Copy (m_source, m_offset, copy, 0, m_count); + return copy; + } + + internal void Reclaim () + { + m_source = ToArray(); + m_offset = 0; + m_own_copy = true; + } + + #region Not supported methods + public void Insert (int index, T item) + { + throw new NotSupportedException(); + } + + public bool Remove (T item) + { + throw new NotSupportedException(); + } + + public void RemoveAt (int index) + { + throw new NotSupportedException(); + } + + public void Add (T item) + { + throw new NotSupportedException(); + } + + public void Clear () + { + throw new NotSupportedException(); + } + #endregion + } + + public static class ByteArrayExt + { + public static ushort ToUInt16 (this CowArray arr, int index) + { + return (ushort)(arr[index] | arr[index+1] << 8); + } + + public static short ToInt16 (this CowArray arr, int index) + { + return (short)(arr[index] | arr[index+1] << 8); + } + + public static int ToInt24 (this CowArray arr, int index) + { + return arr[index] | arr[index+1] << 8 | arr[index+2] << 16; + } + + public static uint ToUInt32 (this TArray arr, int index) where TArray : IList + { + return (uint)(arr[index] | arr[index+1] << 8 | arr[index+2] << 16 | arr[index+3] << 24); + } + + public static int ToInt32 (this TArray arr, int index) where TArray : IList + { + return (int)ToUInt32 (arr, index); + } + + public static ulong ToUInt64 (this CowArray arr, int index) + { + return (ulong)ToUInt32 (arr, index) | ((ulong)ToUInt32 (arr, index+4) << 32); + } + + public static long ToInt64 (this CowArray arr, int index) + { + return (long)ToUInt64 (arr, index); + } + + public static bool AsciiEqual (this CowArray arr, int index, string str) + { + arr.Reclaim(); + return Binary.AsciiEqual (arr.ToArray(), index, str); + } + + public static bool AsciiEqual (this CowArray arr, string str) + { + arr.Reclaim(); + return Binary.AsciiEqual (arr.ToArray(), str); + } + + public static string GetCString (this CowArray arr, int index, int length_limit) + { + arr.Reclaim(); + return Binary.GetCString (arr.ToArray(), index, length_limit); + } + } +} diff --git a/GameRes/GameRes.cs b/GameRes/GameRes.cs index f4a969ad..c8da1a68 100644 --- a/GameRes/GameRes.cs +++ b/GameRes/GameRes.cs @@ -194,7 +194,7 @@ namespace GameRes /// public virtual Stream OpenEntry (ArcFile arc, Entry entry) { - return arc.File.CreateStream (entry.Offset, entry.Size); + return arc.File.CreateStream (entry.Offset, entry.Size, entry.Name); } /// diff --git a/GameRes/GameRes.csproj b/GameRes/GameRes.csproj index d44ee8ab..0e6fa073 100644 --- a/GameRes/GameRes.csproj +++ b/GameRes/GameRes.csproj @@ -67,6 +67,7 @@ + diff --git a/GameRes/Utility.cs b/GameRes/Utility.cs index 594fc7a3..69cce941 100644 --- a/GameRes/Utility.cs +++ b/GameRes/Utility.cs @@ -23,6 +23,7 @@ // IN THE SOFTWARE. // +using System.Collections.Generic; using System.Text; namespace GameRes.Utility @@ -145,22 +146,22 @@ namespace GameRes.Utility public static class BigEndian { - public static ushort ToUInt16 (byte[] value, int index) + public static ushort ToUInt16 (TArray value, int index) where TArray : IList { return (ushort)(value[index] << 8 | value[index+1]); } - public static short ToInt16 (byte[] value, int index) + public static short ToInt16 (TArray value, int index) where TArray : IList { return (short)(value[index] << 8 | value[index+1]); } - public static uint ToUInt32 (byte[] value, int index) + public static uint ToUInt32 (TArray value, int index) where TArray : IList { return (uint)(value[index] << 24 | value[index+1] << 16 | value[index+2] << 8 | value[index+3]); } - public static int ToInt32 (byte[] value, int index) + public static int ToInt32 (TArray value, int index) where TArray : IList { return (int)ToUInt32 (value, index); } @@ -178,7 +179,7 @@ namespace GameRes.Utility return (short)(value[index] | value[index+1] << 8); } - public static uint ToUInt32 (byte[] value, int index) + public static uint ToUInt32 (TArray value, int index) where TArray : IList { return (uint)(value[index] | value[index+1] << 8 | value[index+2] << 16 | value[index+3] << 24); }