From df01ce1893f430eddf679fda79695e11051aba3e Mon Sep 17 00:00:00 2001 From: morkt Date: Sun, 16 Oct 2016 18:29:54 +0400 Subject: [PATCH] use IBinaryStream interface instead of BinaryReader where appropriate. --- ArcFormats/Ankh/ArcGRP.cs | 25 +++++---- ArcFormats/ArcKogado.cs | 89 +++++++++++++++----------------- ArcFormats/ArcPBX.cs | 22 +++----- ArcFormats/ArcSPack.cs | 28 ++++------ ArcFormats/Cyberworks/ArcDAT.cs | 9 ++-- ArcFormats/Emote/ArcPSB.cs | 19 +++---- ArcFormats/Ethornell/ImageBGI.cs | 7 ++- ArcFormats/FC01/ArcMCA.cs | 2 +- ArcFormats/FC01/ArcMRG.cs | 26 ++++------ ArcFormats/FC01/ImageACD.cs | 2 +- ArcFormats/FC01/ImageCLM.cs | 2 +- ArcFormats/FC01/ImageMCG.cs | 4 +- ArcFormats/G2/ArcGCEX.cs | 26 ++++------ ArcFormats/Interheart/ArcFPK.cs | 36 ++++--------- ArcFormats/RealLive/ArcG00.cs | 28 +++++----- ArcFormats/RealLive/ImageG00.cs | 25 +++++---- ArcFormats/SHSystem/ArcHXP.cs | 24 ++++----- ArcFormats/Sas5/ArcIAR.cs | 32 +++++------- ArcFormats/Tactics/ImageTGF.cs | 30 +++-------- ArcFormats/TopCat/ArcTCD3.cs | 13 +++-- ArcFormats/Vitamin/ImageMFC.cs | 3 +- ArcFormats/Vitamin/ImageSBI.cs | 18 +++---- ArcFormats/Will/ImageWIP.cs | 24 +++------ 23 files changed, 195 insertions(+), 299 deletions(-) diff --git a/ArcFormats/Ankh/ArcGRP.cs b/ArcFormats/Ankh/ArcGRP.cs index 00f66030..4e0b6c56 100644 --- a/ArcFormats/Ankh/ArcGRP.cs +++ b/ArcFormats/Ankh/ArcGRP.cs @@ -208,10 +208,9 @@ namespace GameRes.Formats.Ankh Stream OpenTpw (ArcFile arc, PackedEntry entry) { var output = new byte[entry.UnpackedSize]; - using (var file = arc.File.CreateStream (entry.Offset, entry.Size)) - using (var input = new BinaryReader (file)) + using (var input = arc.File.CreateStream (entry.Offset, entry.Size)) { - file.Position = 8; + input.Position = 8; var offsets = new int[4]; offsets[0] = input.ReadUInt16(); offsets[1] = offsets[0] * 2; @@ -220,7 +219,7 @@ namespace GameRes.Formats.Ankh int dst = 0; while (dst < output.Length) { - byte ctl = input.ReadByte(); + byte ctl = input.ReadUInt8(); if (0 == ctl) break; int count; @@ -235,7 +234,7 @@ namespace GameRes.Formats.Ankh count = input.ReadUInt16(); else count = (ctl + 0xC3) & 0xFF; - byte v = input.ReadByte(); + byte v = input.ReadUInt8(); while (count --> 0) output[dst++] = v; } @@ -245,8 +244,8 @@ namespace GameRes.Formats.Ankh count = input.ReadUInt16(); else count = (ctl + 0x92) & 0xFF; - byte v1 = input.ReadByte(); - byte v2 = input.ReadByte(); + byte v1 = input.ReadUInt8(); + byte v2 = input.ReadUInt8(); while (count --> 0) { output[dst++] = v1; @@ -270,7 +269,7 @@ namespace GameRes.Formats.Ankh else { count = (ctl & 0x3F) + 3; - int offset = input.ReadByte(); + int offset = input.ReadUInt8(); offset = (offset & 0x3F) - offsets[offset >> 6]; Binary.CopyOverlapped (output, dst+offset, dst, count); dst += count; @@ -283,13 +282,13 @@ namespace GameRes.Formats.Ankh internal sealed class GrpUnpacker : IDisposable { - BinaryReader m_input; + IBinaryStream m_input; uint m_bits; int m_cached_bits; - public GrpUnpacker (Stream input) + public GrpUnpacker (IBinaryStream input) { - m_input = new ArcView.Reader (input); + m_input = input; } public void UnpackHDJ (byte[] output, int dst) @@ -361,7 +360,7 @@ namespace GameRes.Formats.Ankh public void UnpackS (byte[] output, int dst, int channels) { if (channels != 1) - m_input.BaseStream.Seek ((channels-1) * 4, SeekOrigin.Current); + m_input.Seek ((channels-1) * 4, SeekOrigin.Current); int step = channels * 2; for (int i = 0; i < channels; ++i) { @@ -419,7 +418,7 @@ namespace GameRes.Formats.Ankh public void UnpackA (byte[] output, int dst, int channels) { if (channels != 1) - m_input.BaseStream.Seek ((channels-1) * 4, SeekOrigin.Current); + m_input.Seek ((channels-1) * 4, SeekOrigin.Current); int step = 2 * channels; for (int i = 0; i < channels; ++i) { diff --git a/ArcFormats/ArcKogado.cs b/ArcFormats/ArcKogado.cs index 214c1988..1b529155 100644 --- a/ArcFormats/ArcKogado.cs +++ b/ArcFormats/ArcKogado.cs @@ -341,59 +341,56 @@ namespace GameRes.Formats.Kogado internal class MarielEncoder { - public void Unpack (Stream input, byte[] dest, int dest_size) + public void Unpack (IBinaryStream input, byte[] dest, int dest_size) { int out_pos = 0; - using (var reader = new BinaryReader (input, Encoding.ASCII, true)) + uint bits = 0; + while (dest_size > 0) { - uint bits = 0; - while (dest_size > 0) + bool carry = 0 != (bits & 0x80000000); + bits <<= 1; + if (0 == bits) { - bool carry = 0 != (bits & 0x80000000); - bits <<= 1; - if (0 == bits) - { - bits = reader.ReadUInt32(); - carry = 0 != (bits & 0x80000000); - bits = (bits << 1) | 1u; - } - int b = input.ReadByte(); + bits = input.ReadUInt32(); + carry = 0 != (bits & 0x80000000); + bits = (bits << 1) | 1u; + } + int b = input.ReadByte(); + if (-1 == b) + break; + if (!carry) + { + dest[out_pos++] = (byte)b; + dest_size--; + continue; + } + int offset = (b & 0x0f) + 1; + int count = ((b >> 4) & 0x0f) + 1; + if (0x0f == count) + { + b = input.ReadByte(); if (-1 == b) break; - if (!carry) - { - dest[out_pos++] = (byte)b; - dest_size--; - continue; - } - int offset = (b & 0x0f) + 1; - int count = ((b >> 4) & 0x0f) + 1; - if (0x0f == count) - { - b = input.ReadByte(); - if (-1 == b) - break; - count = (byte)b; - } - else if (count > 0x0f) - { - count = reader.ReadUInt16(); - } - if (offset >= 0x0b) - { - offset -= 0x0b; - offset <<= 8; - offset |= reader.ReadByte(); - } - if (count > dest_size) - count = dest_size; - int src = out_pos - offset; - if (src < 0 || src >= out_pos) - break; - Binary.CopyOverlapped (dest, src, out_pos, count); - out_pos += count; - dest_size -= count; + count = (byte)b; } + else if (count > 0x0f) + { + count = input.ReadUInt16(); + } + if (offset >= 0x0b) + { + offset -= 0x0b; + offset <<= 8; + offset |= input.ReadUInt8(); + } + if (count > dest_size) + count = dest_size; + int src = out_pos - offset; + if (src < 0 || src >= out_pos) + break; + Binary.CopyOverlapped (dest, src, out_pos, count); + out_pos += count; + dest_size -= count; } } } diff --git a/ArcFormats/ArcPBX.cs b/ArcFormats/ArcPBX.cs index a3976b99..c7899984 100644 --- a/ArcFormats/ArcPBX.cs +++ b/ArcFormats/ArcPBX.cs @@ -89,30 +89,30 @@ namespace GameRes.Formats.Terios internal sealed class PandoraCompression : IDisposable { - BinaryReader m_input; + IBinaryStream m_input; byte[] m_output; public byte[] Data { get { return m_output; } } - public PandoraCompression (Stream input, int unpacked_size) + public PandoraCompression (IBinaryStream input, int unpacked_size) { - m_input = new ArcView.Reader (input); + m_input = input; m_output = new byte[unpacked_size]; } public byte[] Unpack () { int dst = 0; - m_output[dst++] = m_input.ReadByte(); + m_output[dst++] = m_input.ReadUInt8(); while (dst < m_output.Length) { - int ctl = m_input.ReadByte(); + int ctl = m_input.ReadUInt8(); int count; if (ctl >= 0x80) { if (ctl >= 0xC0) { - int offset = m_input.ReadByte(); + int offset = m_input.ReadUInt8(); offset += 0x101 + ((ctl & 0x3F) << 8); offset = dst - offset; if (offset < 0) @@ -146,7 +146,7 @@ namespace GameRes.Formats.Terios } else { - offset = m_input.ReadByte() + 1; + offset = m_input.ReadUInt8() + 1; } Binary.CopyOverlapped (m_output, dst - offset, dst, count); dst += count; @@ -161,7 +161,7 @@ namespace GameRes.Formats.Terios } if (ctl >= 0x40) { - count = m_input.ReadByte() + ((ctl & 0x1F) << 8) + 0x41; + count = m_input.ReadUInt8() + ((ctl & 0x1F) << 8) + 0x41; } else { @@ -175,14 +175,8 @@ namespace GameRes.Formats.Terios } #region IDisposable Members - bool _disposed = false; public void Dispose () { - if (!_disposed) - { - m_input.Dispose(); - _disposed = true; - } } #endregion } diff --git a/ArcFormats/ArcSPack.cs b/ArcFormats/ArcSPack.cs index 185b97f7..7d759c4d 100644 --- a/ArcFormats/ArcSPack.cs +++ b/ArcFormats/ArcSPack.cs @@ -115,17 +115,17 @@ namespace GameRes.Formats.SPack } } - internal class PackedReader : IDisposable + internal sealed class PackedReader : IDisposable { - BinaryReader m_input; + IBinaryStream m_input; uint m_packed_size; byte[] m_output; public byte[] Data { get { return m_output; } } - public PackedReader (SPackEntry entry, Stream input) + public PackedReader (SPackEntry entry, IBinaryStream input) { - m_input = new BinaryReader (input); + m_input = input; m_packed_size = entry.Size; m_output = new byte[entry.UnpackedSize]; } @@ -149,7 +149,7 @@ namespace GameRes.Formats.SPack { int copy_count, offset; - offset = m_input.ReadByte(); + offset = m_input.ReadUInt8(); src++; copy_count = offset >> 4; offset &= 0x0f; @@ -160,7 +160,7 @@ namespace GameRes.Formats.SPack } else if (14 == copy_count) { - copy_count = m_input.ReadByte(); + copy_count = m_input.ReadUInt8(); src++; } else @@ -170,7 +170,7 @@ namespace GameRes.Formats.SPack offset++; else { - offset = ((offset - 10) << 8) | m_input.ReadByte(); + offset = ((offset - 10) << 8) | m_input.ReadUInt8(); src++; } @@ -181,7 +181,7 @@ namespace GameRes.Formats.SPack } else { - m_output[dst++] = m_input.ReadByte(); + m_output[dst++] = m_input.ReadUInt8(); src++; } mask >>= 1; @@ -193,21 +193,13 @@ namespace GameRes.Formats.SPack 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.Dispose(); disposed = true; } + GC.SuppressFinalize (this); } #endregion } diff --git a/ArcFormats/Cyberworks/ArcDAT.cs b/ArcFormats/Cyberworks/ArcDAT.cs index f4237860..a7bba7ce 100644 --- a/ArcFormats/Cyberworks/ArcDAT.cs +++ b/ArcFormats/Cyberworks/ArcDAT.cs @@ -116,15 +116,14 @@ namespace GameRes.Formats.Cyberworks var type = new char[2]; var dir = new List (count); bool has_images = false; - using (var input = new MemoryStream (toc)) - using (var index = new BinaryReader (input)) + using (var index = new BinMemoryStream (toc, file.Name)) { - while (input.Position < input.Length) + while (index.Position < index.Length) { entry_size = index.ReadInt32(); if (entry_size <= 0) return null; - var next_pos = index.BaseStream.Position + entry_size; + var next_pos = index.Position + entry_size; uint id = index.ReadUInt32(); var entry = new PackedEntry { Name = id.ToString ("D6") }; entry.UnpackedSize = index.ReadUInt32(); @@ -159,7 +158,7 @@ namespace GameRes.Formats.Cyberworks } dir.Add (entry); } - index.BaseStream.Position = next_pos; + index.Position = next_pos; } } if (0 == dir.Count) diff --git a/ArcFormats/Emote/ArcPSB.cs b/ArcFormats/Emote/ArcPSB.cs index 1243e09d..7ee27e1b 100644 --- a/ArcFormats/Emote/ArcPSB.cs +++ b/ArcFormats/Emote/ArcPSB.cs @@ -124,11 +124,11 @@ namespace GameRes.Formats.Emote /// internal sealed class PsbReader : IDisposable { - BinaryReader m_input; + IBinaryStream m_input; - public PsbReader (Stream input) + public PsbReader (IBinaryStream input) { - m_input = new ArcView.Reader (input); + m_input = input; } public int Version { get { return m_version; } } @@ -213,7 +213,7 @@ namespace GameRes.Formats.Emote bool ReadHeader () { - m_input.BaseStream.Position = 4; + m_input.Position = 4; m_version = m_input.ReadUInt16(); m_flags = m_input.ReadUInt16(); if (m_version < 3) @@ -232,7 +232,7 @@ namespace GameRes.Formats.Emote m_chunk_data = LittleEndian.ToInt32 (header, 0x18); m_root = LittleEndian.ToInt32 (header, 0x1C); - int buffer_length = (int)m_input.BaseStream.Length; + int buffer_length = (int)m_input.Length; if (!(m_names >= 0x28 && m_names < m_chunk_data && m_strings >= 0x28 && m_strings < m_chunk_data && m_strings_data >= 0x28 && m_strings_data < m_chunk_data @@ -244,7 +244,7 @@ namespace GameRes.Formats.Emote if (null == m_data || m_data.Length < m_chunk_data) m_data = new byte[m_chunk_data]; - int data_pos = (int)m_input.BaseStream.Position; + int data_pos = (int)m_input.Position; m_input.Read (m_data, data_pos, m_chunk_data-data_pos); if (0 != (m_flags & 2)) Decrypt (m_data, m_names, m_chunk_offsets-m_names); @@ -548,14 +548,8 @@ namespace GameRes.Formats.Emote } #region IDisposable Members - bool _disposed = false; public void Dispose () { - if (!_disposed) - { - m_input.Dispose(); - _disposed = true; - } } #endregion } @@ -586,6 +580,7 @@ namespace GameRes.Formats.Emote public override ImageMetaData ReadMetaData (IBinaryStream stream) { stream.Position = 4; + // need BinaryReader because of ReadString using (var reader = new BinaryReader (stream.AsStream, Encoding.UTF8, true)) { var info = new PsbTexMetaData { BPP = 32 }; diff --git a/ArcFormats/Ethornell/ImageBGI.cs b/ArcFormats/Ethornell/ImageBGI.cs index 35e89dab..99edac15 100644 --- a/ArcFormats/Ethornell/ImageBGI.cs +++ b/ArcFormats/Ethornell/ImageBGI.cs @@ -629,8 +629,7 @@ namespace GameRes.Formats.BGI public void UnpackAlpha (int offset) { - using (var data = new MemoryStream (this.Input, offset, Input.Length-offset)) - using (var input = new BinaryReader (data)) + using (var input = new BinMemoryStream (this.Input, offset, Input.Length-offset)) { if (1 != input.ReadInt32()) return; @@ -640,7 +639,7 @@ namespace GameRes.Formats.BGI { ctl >>= 1; if (1 == ctl) - ctl = input.ReadByte() | 0x100; + ctl = input.ReadUInt8() | 0x100; if (0 != (ctl & 1)) { @@ -666,7 +665,7 @@ namespace GameRes.Formats.BGI } else { - Output[dst] = input.ReadByte(); + Output[dst] = input.ReadUInt8(); dst += 4; } } diff --git a/ArcFormats/FC01/ArcMCA.cs b/ArcFormats/FC01/ArcMCA.cs index 7205b73b..c29f514e 100644 --- a/ArcFormats/FC01/ArcMCA.cs +++ b/ArcFormats/FC01/ArcMCA.cs @@ -99,7 +99,7 @@ namespace GameRes.Formats.FC01 MrgOpener.Decrypt (data, 0, data.Length, mca.Key); if (method > 0) { - using (var input = new MemoryStream (data)) + using (var input = new BinMemoryStream (data)) using (var lzss = new MrgLzssReader (input, data.Length, unpacked_size)) { lzss.Unpack(); diff --git a/ArcFormats/FC01/ArcMRG.cs b/ArcFormats/FC01/ArcMRG.cs index f2752e7c..abf16532 100644 --- a/ArcFormats/FC01/ArcMRG.cs +++ b/ArcFormats/FC01/ArcMRG.cs @@ -106,7 +106,7 @@ namespace GameRes.Formats.FC01 var packed_entry = entry as MrgEntry; if (null == packed_entry || !packed_entry.IsPacked || packed_entry.Method > 3) return arc.File.CreateStream (entry.Offset, entry.Size); - Stream input; + IBinaryStream input; if (packed_entry.Method >= 2) { if (entry.Size < 0x108) @@ -127,7 +127,7 @@ namespace GameRes.Formats.FC01 return new BinMemoryStream (reader.Data, entry.Name); } } - return input; + return input.AsStream; } static public void Decrypt (byte[] data, int index, int length, int key) @@ -235,7 +235,7 @@ namespace GameRes.Formats.FC01 var mrg_entry = entry as Mrg2Entry; if (null == mrg_entry || mrg_entry.Method > 3) return arc.File.CreateStream (entry.Offset, entry.Size); - Stream input; + IBinaryStream input; if (0 == mrg_entry.Method) { var data = arc.File.View.ReadBytes (entry.Offset, entry.Size); @@ -260,7 +260,7 @@ namespace GameRes.Formats.FC01 return new BinMemoryStream (reader.Data, entry.Name); } } - return input; + return input.AsStream; } void Decrypt (byte[] data, int index, int length, uint checksum, uint key) @@ -298,15 +298,15 @@ namespace GameRes.Formats.FC01 /// internal sealed class MrgLzssReader : IDisposable { - BinaryReader m_input; + IBinaryStream m_input; byte[] m_output; int m_size; public byte[] Data { get { return m_output; } } - public MrgLzssReader (Stream input, int input_length, int output_length) + public MrgLzssReader (IBinaryStream input, int input_length, int output_length) { - m_input = new ArcView.Reader (input); + m_input = input; m_output = new byte[output_length]; m_size = input_length; } @@ -320,7 +320,7 @@ namespace GameRes.Formats.FC01 int remaining = m_size; while (remaining > 0) { - int ctl = m_input.ReadByte(); + int ctl = m_input.ReadUInt8(); --remaining; for (int bit = 1; remaining > 0 && bit != 0x100; bit <<= 1) { @@ -328,7 +328,7 @@ namespace GameRes.Formats.FC01 return; if (0 != (ctl & bit)) { - byte b = m_input.ReadByte(); + byte b = m_input.ReadUInt8(); --remaining; frame[frame_pos++] = b; frame_pos &= frame_mask; @@ -357,16 +357,8 @@ namespace GameRes.Formats.FC01 } #region IDisposable Members - bool disposed = false; - public void Dispose () { - if (!disposed) - { - m_input.Dispose(); - disposed = true; - } - GC.SuppressFinalize (this); } #endregion } diff --git a/ArcFormats/FC01/ImageACD.cs b/ArcFormats/FC01/ImageACD.cs index 95b1ea9f..6c81e517 100644 --- a/ArcFormats/FC01/ImageACD.cs +++ b/ArcFormats/FC01/ImageACD.cs @@ -69,7 +69,7 @@ namespace GameRes.Formats.FC01 var meta = (AcdMetaData)info; stream.Position = meta.DataOffset; - using (var reader = new MrgLzssReader (stream.AsStream, meta.PackedSize, meta.UnpackedSize)) + using (var reader = new MrgLzssReader (stream, 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 10cd3e6b..b30276fc 100644 --- a/ArcFormats/FC01/ImageCLM.cs +++ b/ArcFormats/FC01/ImageCLM.cs @@ -85,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.AsStream, packed_size, meta.UnpackedSize)) + using (var reader = new MrgLzssReader (stream, 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 e1e38b2f..227c1c46 100644 --- a/ArcFormats/FC01/ImageMCG.cs +++ b/ArcFormats/FC01/ImageMCG.cs @@ -197,7 +197,7 @@ namespace GameRes.Formats.FC01 { var copy = m_input.Clone() as byte[]; MrgOpener.Decrypt (copy, 0, copy.Length-1, (byte)key); - using (var input = new MemoryStream (copy)) + using (var input = new BinMemoryStream (copy)) using (var lzss = new MrgLzssReader (input, m_input.Length, Stride * m_height)) { lzss.Unpack(); @@ -211,7 +211,7 @@ namespace GameRes.Formats.FC01 } } #endif - using (var input = new MemoryStream (m_input)) + using (var input = new BinMemoryStream (m_input)) using (var lzss = new MrgLzssReader (input, m_input.Length, Stride * m_height)) { lzss.Unpack(); diff --git a/ArcFormats/G2/ArcGCEX.cs b/ArcFormats/G2/ArcGCEX.cs index dba94f48..117526c0 100644 --- a/ArcFormats/G2/ArcGCEX.cs +++ b/ArcFormats/G2/ArcGCEX.cs @@ -132,7 +132,7 @@ namespace GameRes.Formats.G2 internal class GceReader : IDisposable { - BinaryReader m_input; + IBinaryStream m_input; int m_unpacked_size; byte[] m_output = null; int m_dst; @@ -150,9 +150,9 @@ namespace GameRes.Formats.G2 } } - public GceReader (Stream input, int unpacked_size) + public GceReader (IBinaryStream input, int unpacked_size) { - m_input = new ArcView.Reader (input); + m_input = input; m_unpacked_size = unpacked_size; } @@ -170,13 +170,13 @@ namespace GameRes.Formats.G2 m_input.ReadInt32(); int cmd_len = m_input.ReadInt32(); - long cmd_pos = m_input.BaseStream.Position + data_length; + long cmd_pos = m_input.Position + data_length; ReadControlStream (cmd_pos, cmd_len); int next = m_dst + segment_length; UnpackGce1Segment (segment_length); m_dst = next; - m_input.BaseStream.Position = cmd_pos + cmd_len; + m_input.Position = cmd_pos + cmd_len; } else if (Binary.AsciiEqual (id, "GCE0")) { @@ -203,7 +203,7 @@ namespace GameRes.Formats.G2 while (n --> 0) { m_frame[frame_pos] = m_dst; - byte b = m_input.ReadByte(); + byte b = m_input.ReadUInt8(); frame_pos = ((frame_pos << 8) | b) & 0xFFFF; m_output[m_dst++] = b; } @@ -242,15 +242,15 @@ namespace GameRes.Formats.G2 void ReadControlStream (long pos, int length) { - var data_pos = m_input.BaseStream.Position; + var data_pos = m_input.Position; if (null == m_control || m_control.Length < length) m_control = new byte[length]; - m_input.BaseStream.Position = pos; + m_input.Position = pos; if (length != m_input.Read (m_control, 0, length)) throw new EndOfStreamException(); m_control_pos = 0; m_control_len = length; - m_input.BaseStream.Position = data_pos; + m_input.Position = data_pos; m_bit_pos = 8; } @@ -268,16 +268,8 @@ namespace GameRes.Formats.G2 } #region IDisposable Members - bool m_disposed = false; public void Dispose () { - if (!m_disposed) - { - if (m_input != null) - m_input.Dispose(); - m_disposed = true; - GC.SuppressFinalize (this); - } } #endregion } diff --git a/ArcFormats/Interheart/ArcFPK.cs b/ArcFormats/Interheart/ArcFPK.cs index c8e8c905..473169df 100644 --- a/ArcFormats/Interheart/ArcFPK.cs +++ b/ArcFormats/Interheart/ArcFPK.cs @@ -131,12 +131,10 @@ namespace GameRes.Formats.CandySoft var input = arc.File.CreateStream (entry.Offset, entry.Size); if (entry.Size <= 8) return input; - var sign = FormatCatalog.ReadSignature (input); + var sign = input.Signature; if (0x32434c5a != sign) // 'ZLC2' - { - input.Position = 0; return input; - } + using (input) using (var reader = new Zlc2Reader (input, (int)entry.Size)) { @@ -148,16 +146,16 @@ namespace GameRes.Formats.CandySoft internal class Zlc2Reader : IDisposable { - BinaryReader m_input; + IBinaryStream m_input; byte[] m_output; int m_size; public byte[] Data { get { return m_output; } } - public Zlc2Reader (Stream input, int input_length) + public Zlc2Reader (IBinaryStream input, int input_length) { input.Position = 4; - m_input = new ArcView.Reader (input); + m_input = input; uint output_length = m_input.ReadUInt32(); m_output = new byte[output_length]; m_size = input_length - 8; @@ -169,7 +167,7 @@ namespace GameRes.Formats.CandySoft int dst = 0; while (remaining > 0 && dst < m_output.Length) { - int ctl = m_input.ReadByte(); + int ctl = m_input.ReadUInt8(); remaining--; for (int mask = 0x80; mask != 0 && remaining > 0 && dst < m_output.Length; mask >>= 1) { @@ -178,8 +176,8 @@ namespace GameRes.Formats.CandySoft if (remaining < 2) return; - int offset = m_input.ReadByte(); - int count = m_input.ReadByte(); + int offset = m_input.ReadUInt8(); + int count = m_input.ReadUInt8(); offset |= (count & 0xF0) << 4; count = (count & 0x0F) + 3; @@ -193,31 +191,15 @@ namespace GameRes.Formats.CandySoft } else { - m_output[dst++] = m_input.ReadByte(); + m_output[dst++] = m_input.ReadUInt8(); } } } } #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/RealLive/ArcG00.cs b/ArcFormats/RealLive/ArcG00.cs index 8a734136..2be65d03 100644 --- a/ArcFormats/RealLive/ArcG00.cs +++ b/ArcFormats/RealLive/ArcG00.cs @@ -99,15 +99,14 @@ namespace GameRes.Formats.RealLive using (var input = file.CreateStream (index_offset)) bitmap = G00Reader.LzDecompress (input, 2, 1); - using (var input = new MemoryStream (bitmap)) - using (var reader = new BinaryReader (input)) + using (var input = new BinMemoryStream (bitmap)) { - if (reader.ReadInt32() != count) + if (input.ReadInt32() != count) return null; for (int i = 0; i < count; ++i) { - dir[i].Offset = reader.ReadUInt32(); - dir[i].Size = reader.ReadUInt32(); + dir[i].Offset = input.ReadUInt32(); + dir[i].Size = input.ReadUInt32(); } } dir = dir.Where (e => e.Size != 0).ToList(); @@ -123,12 +122,11 @@ namespace GameRes.Formats.RealLive return Stream.Null; var g00arc = (G00Archive)arc; var g00ent = (G00Entry)entry; - using (var input = new MemoryStream (g00arc.Bitmap)) - using (var reader = new BinaryReader (input)) + using (var input = new BinMemoryStream (g00arc.Bitmap)) { input.Position = g00ent.Offset; - int tile_type = reader.ReadUInt16(); - int count = reader.ReadUInt16(); + int tile_type = input.ReadUInt16(); + int count = input.ReadUInt16(); if (tile_type != 1) throw new InvalidFormatException(); input.Seek (0x70, SeekOrigin.Current); @@ -136,11 +134,11 @@ namespace GameRes.Formats.RealLive var pixels = new byte[(int)g00arc.ImageInfo.Height * dst_stride]; for (int i = 0; i < count; ++i) { - int tile_x = reader.ReadUInt16(); - int tile_y = reader.ReadUInt16(); - reader.ReadInt16(); - int tile_width = reader.ReadUInt16(); - int tile_height = reader.ReadUInt16(); + int tile_x = input.ReadUInt16(); + int tile_y = input.ReadUInt16(); + input.ReadInt16(); + int tile_width = input.ReadUInt16(); + int tile_height = input.ReadUInt16(); input.Seek (0x52, SeekOrigin.Current); tile_x += g00ent.X; @@ -152,7 +150,7 @@ namespace GameRes.Formats.RealLive int tile_stride = tile_width * 4; for (int row = 0; row < tile_height; ++row) { - reader.Read (pixels, dst, tile_stride); + input.Read (pixels, dst, tile_stride); dst += dst_stride; } } diff --git a/ArcFormats/RealLive/ImageG00.cs b/ArcFormats/RealLive/ImageG00.cs index 53871a05..0ba66bd9 100644 --- a/ArcFormats/RealLive/ImageG00.cs +++ b/ArcFormats/RealLive/ImageG00.cs @@ -164,33 +164,32 @@ namespace GameRes.Formats.RealLive tiles.Add (tile); m_input.Seek (0x10, SeekOrigin.Current); } - using (var input = new MemoryStream (LzDecompress (m_input, 2, 1))) - using (var reader = new BinaryReader (input)) + using (var input = new BinMemoryStream (LzDecompress (m_input, 2, 1))) { - if (reader.ReadInt32() != tile_count) + if (input.ReadInt32() != tile_count) throw new InvalidFormatException(); int dst_stride = m_width * 4; m_output = new byte[m_height * dst_stride]; for (int i = 0; i < tile_count; ++i) { - tiles[i].Offset = reader.ReadUInt32(); - tiles[i].Length = reader.ReadInt32(); + tiles[i].Offset = input.ReadUInt32(); + tiles[i].Length = input.ReadInt32(); } var tile = tiles.First (t => t.Length != 0); input.Position = tile.Offset; - int tile_type = reader.ReadUInt16(); - int count = reader.ReadUInt16(); + int tile_type = input.ReadUInt16(); + int count = input.ReadUInt16(); if (tile_type != 1) throw new InvalidFormatException(); input.Seek (0x70, SeekOrigin.Current); for (int i = 0; i < count; ++i) { - int tile_x = reader.ReadUInt16(); - int tile_y = reader.ReadUInt16(); - reader.ReadInt16(); - int tile_width = reader.ReadUInt16(); - int tile_height = reader.ReadUInt16(); + int tile_x = input.ReadUInt16(); + int tile_y = input.ReadUInt16(); + input.ReadInt16(); + int tile_width = input.ReadUInt16(); + int tile_height = input.ReadUInt16(); input.Seek (0x52, SeekOrigin.Current); tile_x += tile.X; @@ -201,7 +200,7 @@ namespace GameRes.Formats.RealLive int tile_stride = tile_width * 4; for (int row = 0; row < tile_height; ++row) { - reader.Read (m_output, dst, tile_stride); + input.Read (m_output, dst, tile_stride); dst += dst_stride; } } diff --git a/ArcFormats/SHSystem/ArcHXP.cs b/ArcFormats/SHSystem/ArcHXP.cs index 175dccbf..9cbf4257 100644 --- a/ArcFormats/SHSystem/ArcHXP.cs +++ b/ArcFormats/SHSystem/ArcHXP.cs @@ -121,7 +121,7 @@ namespace GameRes.Formats.SHSystem public override Stream OpenEntry (ArcFile arc, Entry entry) { - var input = base.OpenEntry (arc, entry); + var input = arc.File.CreateStream (entry.Offset, entry.Size, entry.Name); var pent = entry as PackedEntry; if (null == pent || !pent.IsPacked) return input; @@ -198,11 +198,11 @@ namespace GameRes.Formats.SHSystem internal class ShsCompression : IDisposable { - BinaryReader m_input; + IBinaryStream m_input; - public ShsCompression (Stream input, bool leave_open = false) + public ShsCompression (IBinaryStream input) { - m_input = new BinaryReader (input, Encoding.UTF8, leave_open); + m_input = input; } public int Unpack (byte[] output) @@ -211,13 +211,13 @@ namespace GameRes.Formats.SHSystem while (dst < output.Length) { int count; - int ctl = m_input.ReadByte(); + int ctl = m_input.ReadUInt8(); if (ctl < 32) { switch (ctl) { case 0x1D: - count = m_input.ReadByte() + 0x1E; + count = m_input.ReadUInt8() + 0x1E; break; case 0x1E: count = Binary.BigEndian (m_input.ReadUInt16()) + 0x11E; @@ -244,13 +244,13 @@ namespace GameRes.Formats.SHSystem } else { - offset = m_input.ReadByte(); + offset = m_input.ReadUInt8(); if (0x40 == (ctl & 0x60)) count = (ctl & 0x1F) + 4; else { offset |= (ctl & 0x1F) << 8; - ctl = m_input.ReadByte(); + ctl = m_input.ReadUInt8(); if (0xFE == ctl) count = Binary.BigEndian (m_input.ReadUInt16()) + 0x102; else if (0xFF == ctl) @@ -263,7 +263,7 @@ namespace GameRes.Formats.SHSystem else { count = (ctl >> 5) & 3; - offset = ((ctl & 0x1F) << 8) | m_input.ReadByte(); + offset = ((ctl & 0x1F) << 8) | m_input.ReadUInt8(); } count += 3; offset++; @@ -276,14 +276,8 @@ namespace GameRes.Formats.SHSystem } #region IDisposable Members - bool m_disposed = false; public void Dispose () { - if (!m_disposed) - { - m_input.Dispose(); - m_disposed = true; - } } #endregion } diff --git a/ArcFormats/Sas5/ArcIAR.cs b/ArcFormats/Sas5/ArcIAR.cs index 72e216ed..0ea21374 100644 --- a/ArcFormats/Sas5/ArcIAR.cs +++ b/ArcFormats/Sas5/ArcIAR.cs @@ -165,8 +165,7 @@ namespace GameRes.Formats.Sas5 IarImage CombineImage (IarImage overlay, IarArchive iarc) { - using (var mem = new MemoryStream (overlay.Data)) - using (var input = new BinaryReader (mem)) + using (var input = new BinMemoryStream (overlay.Data)) { var dir = (List)iarc.Dir; int base_index = input.ReadInt32(); @@ -218,14 +217,13 @@ namespace GameRes.Formats.Sas5 layers.Info.BPP = 32; var pixels = new byte[layers.Info.Stride * (int)layers.Info.Height]; var output = new IarImage (layers.Info, pixels); - using (var mem = new MemoryStream (layers.Data)) - using (var input = new BinaryReader (mem)) + using (var input = new BinMemoryStream (layers.Data)) { int offset_x = 0, offset_y = 0; var dir = (List)iarc.Dir; - while (input.BaseStream.Position < input.BaseStream.Length) + while (input.Position < input.Length) { - int cmd = input.ReadByte(); + int cmd = input.ReadUInt8(); switch (cmd) { case 0x21: @@ -416,11 +414,11 @@ namespace GameRes.Formats.Sas5 internal sealed class IarDecompressor : IDisposable { - BinaryReader m_input; + IBinaryStream m_input; - public IarDecompressor (Stream input) + public IarDecompressor (IBinaryStream input) { - m_input = new ArcView.Reader (input); + m_input = input; } int m_bits = 1; @@ -433,7 +431,7 @@ namespace GameRes.Formats.Sas5 { if (1 == GetNextBit()) { - output[dst++] = m_input.ReadByte(); + output[dst++] = m_input.ReadUInt8(); continue; } int offset, count; @@ -461,7 +459,7 @@ namespace GameRes.Formats.Sas5 } } } - offset += (tmp << 8) | m_input.ReadByte(); + offset += (tmp << 8) | m_input.ReadUInt8(); if (1 == GetNextBit()) count = 3; else if (1 == GetNextBit()) @@ -473,7 +471,7 @@ namespace GameRes.Formats.Sas5 else if (1 == GetNextBit()) count = 7 + GetNextBit(); else if (1 == GetNextBit()) - count = 17 + m_input.ReadByte(); + count = 17 + m_input.ReadUInt8(); else { count = GetNextBit() << 2; @@ -490,11 +488,11 @@ namespace GameRes.Formats.Sas5 offset = GetNextBit() << 10; offset |= GetNextBit() << 9; offset |= GetNextBit() << 8; - offset = (offset | m_input.ReadByte()) + 0x100; + offset = (offset | m_input.ReadUInt8()) + 0x100; } else { - offset = 1 + m_input.ReadByte(); + offset = 1 + m_input.ReadUInt8(); if (0x100 == offset) break; } @@ -516,14 +514,8 @@ namespace GameRes.Formats.Sas5 } #region IDisposable Members - bool _disposed = false; public void Dispose () { - if (!_disposed) - { - m_input.Dispose(); - _disposed = true; - } } #endregion } diff --git a/ArcFormats/Tactics/ImageTGF.cs b/ArcFormats/Tactics/ImageTGF.cs index 369bc731..317df61c 100644 --- a/ArcFormats/Tactics/ImageTGF.cs +++ b/ArcFormats/Tactics/ImageTGF.cs @@ -59,7 +59,7 @@ namespace GameRes.Formats.Tactics int chunk_size = stream.ReadInt32(); if (length > 0xffffff || chunk_size <= 0 || length < chunk_size) return null; - using (var reader = new Reader (stream.AsStream, (uint)Math.Max (0x20, chunk_size+2), chunk_size)) + using (var reader = new Reader (stream, (uint)Math.Max (0x20, chunk_size+2), chunk_size)) { reader.Unpack(); var bmp = reader.Data; @@ -80,7 +80,7 @@ namespace GameRes.Formats.Tactics { var meta = (TgfMetaData)info; stream.Position = 8; - using (var reader = new Reader (stream.AsStream, meta.BitmapSize, meta.ChunkSize)) + using (var reader = new Reader (stream, meta.BitmapSize, meta.ChunkSize)) { reader.Unpack(); using (var bmp = new MemoryStream (reader.Data)) @@ -96,17 +96,17 @@ namespace GameRes.Formats.Tactics internal class Reader : IDisposable { - BinaryReader m_input; + IBinaryStream m_input; byte[] m_output; int m_chunk_size; public byte[] Data { get { return m_output; } } - public Reader (Stream file, uint bmp_size, int chunk_size) + public Reader (IBinaryStream file, uint bmp_size, int chunk_size) { m_chunk_size = chunk_size; m_output = new byte[bmp_size]; - m_input = new BinaryReader (file, Encoding.ASCII, true); + m_input = file; } public void Unpack () @@ -114,12 +114,12 @@ namespace GameRes.Formats.Tactics int dst = 0; while (dst < m_output.Length) { - int code = m_input.ReadByte(); + int code = m_input.ReadUInt8(); switch (code) { case 0: { - int count = m_input.ReadByte(); + int count = m_input.ReadUInt8(); if (dst + count > m_output.Length) count = m_output.Length - dst; m_input.Read (m_output, dst, count); @@ -128,7 +128,7 @@ namespace GameRes.Formats.Tactics } case 1: { - int count = m_input.ReadByte() * m_chunk_size; + int count = m_input.ReadUInt8() * m_chunk_size; if (dst + count > m_output.Length) count = m_output.Length - dst; m_input.Read (m_output, dst, count); @@ -156,22 +156,8 @@ namespace GameRes.Formats.Tactics } #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/TopCat/ArcTCD3.cs b/ArcFormats/TopCat/ArcTCD3.cs index 897859f3..a4175ae1 100644 --- a/ArcFormats/TopCat/ArcTCD3.cs +++ b/ArcFormats/TopCat/ArcTCD3.cs @@ -259,17 +259,16 @@ namespace GameRes.Formats.TopCat internal abstract class TcdIndexReader : IDisposable { - BinaryReader m_input; + IBinaryStream m_input; int m_section_count; - public int Count { get; private set; } - protected BinaryReader Input { get { return m_input; } } + public int Count { get; private set; } + protected IBinaryStream Input { get { return m_input; } } protected TcdIndexReader (ArcView file, int section_count) { Count = file.View.ReadInt32 (4); - var input = file.CreateStream(); - m_input = new BinaryReader (input); + m_input = file.CreateStream(); m_section_count = section_count; } @@ -284,7 +283,7 @@ namespace GameRes.Formats.TopCat var list = new List (Count); foreach (var section in sections) { - m_input.BaseStream.Position = section.IndexOffset; + m_input.Position = section.IndexOffset; var dir_names = m_input.ReadBytes (section.DirNamesSize); if (section.DirNamesSize != dir_names.Length) return null; @@ -339,7 +338,7 @@ namespace GameRes.Formats.TopCat uint current_offset = 8; for (int i = 0; i < count; ++i) { - m_input.BaseStream.Position = current_offset; + m_input.Position = current_offset; var section = ReadSection (i); if (section != null) sections.Add (section); diff --git a/ArcFormats/Vitamin/ImageMFC.cs b/ArcFormats/Vitamin/ImageMFC.cs index d3bea9af..1053efc9 100644 --- a/ArcFormats/Vitamin/ImageMFC.cs +++ b/ArcFormats/Vitamin/ImageMFC.cs @@ -82,7 +82,8 @@ namespace GameRes.Formats.Vitamin stream.Position = 0x18; RleUnpack (stream.AsStream, meta.AlphaSize - 0x18, alpha); byte[] pixels; - using (var sbi = new StreamRegion (stream.AsStream, meta.AlphaSize, true)) + using (var reg = new StreamRegion (stream.AsStream, meta.AlphaSize, true)) + using (var sbi = new BinaryStream (reg, stream.Name)) using (var reader = new SbiReader (sbi, meta.BaseInfo)) { reader.Unpack(); diff --git a/ArcFormats/Vitamin/ImageSBI.cs b/ArcFormats/Vitamin/ImageSBI.cs index 4055522c..b957d457 100644 --- a/ArcFormats/Vitamin/ImageSBI.cs +++ b/ArcFormats/Vitamin/ImageSBI.cs @@ -72,7 +72,7 @@ namespace GameRes.Formats.Vitamin public override ImageData Read (IBinaryStream stream, ImageMetaData info) { - using (var reader = new SbiReader (stream.AsStream, (SbiMetaData)info)) + using (var reader = new SbiReader (stream, (SbiMetaData)info)) { reader.Unpack(); return ImageData.Create (info, reader.Format, reader.Palette, reader.Data, reader.Stride); @@ -87,7 +87,7 @@ namespace GameRes.Formats.Vitamin internal sealed class SbiReader : IDisposable { - BinaryReader m_input; + IBinaryStream m_input; SbiMetaData m_info; byte[] m_output; int m_stride; @@ -97,9 +97,9 @@ namespace GameRes.Formats.Vitamin public BitmapPalette Palette { get; private set; } public int Stride { get { return m_stride; } } - public SbiReader (Stream input, SbiMetaData info) + public SbiReader (IBinaryStream input, SbiMetaData info) { - m_input = new ArcView.Reader (input); + m_input = input; m_info = info; m_stride = ((int)m_info.Width * m_info.BPP / 8 + 3) & ~3; m_output = new byte[m_stride * (int)m_info.Height]; @@ -122,7 +122,7 @@ namespace GameRes.Formats.Vitamin public void Unpack () { - m_input.BaseStream.Position = 0x20; + m_input.Position = 0x20; int input_size = m_info.InputSize - 0x20; if (m_info.HasPalette) { @@ -146,7 +146,7 @@ namespace GameRes.Formats.Vitamin var buffer = new byte[0x180]; while (input_size > 0) { - int count = m_input.ReadByte(); + int count = m_input.ReadUInt8(); --input_size; if (count < 0x80) { @@ -203,14 +203,8 @@ namespace GameRes.Formats.Vitamin } #region IDisposable Members - bool _disposed = false; public void Dispose () { - if (!_disposed) - { - m_input.Dispose(); - _disposed = true; - } } #endregion } diff --git a/ArcFormats/Will/ImageWIP.cs b/ArcFormats/Will/ImageWIP.cs index f5d5a877..1c4240d2 100644 --- a/ArcFormats/Will/ImageWIP.cs +++ b/ArcFormats/Will/ImageWIP.cs @@ -102,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.AsStream, meta)) + using (var reader = new Reader (file, meta)) { reader.Unpack(); if (24 == meta.BPP) @@ -132,19 +132,19 @@ namespace GameRes.Formats.Will internal sealed class Reader : IDisposable { - private BinaryReader m_input; + private IBinaryStream m_input; private uint m_length; private byte[] m_data; public byte[] Data { get { return m_data; } } - public Reader (Stream file, WipMetaData info) + public Reader (IBinaryStream file, WipMetaData info) { m_length = info.FrameSize; // int stride = (int)info.Width*((info.BPP+7)/8); int stride = (int)info.Width*4; m_data = new byte[stride * (int)info.Height]; - m_input = new ArcView.Reader (file); + m_input = file; } private byte[] m_window = new byte[0x1000]; @@ -159,14 +159,14 @@ namespace GameRes.Formats.Will control >>= 1; if (0 == (control & 0x100)) { - control = m_input.ReadByte() | 0xFF00; + control = m_input.ReadUInt8() | 0xFF00; --length; } if (0 != (control & 1)) { if (length < 1) throw new InvalidFormatException(); - byte b = m_input.ReadByte(); + byte b = m_input.ReadUInt8(); --length; m_data[dst++] = b; m_window[window_index++] = b; @@ -176,8 +176,8 @@ namespace GameRes.Formats.Will { if (length < 2) throw new InvalidFormatException(); - int hi = m_input.ReadByte(); - int lo = m_input.ReadByte(); + int hi = m_input.ReadUInt8(); + int lo = m_input.ReadUInt8(); length -= 2; int offset = hi << 4 | lo >> 4; for (int count = (lo & 0xF) + 2; count > 0; --count) @@ -192,16 +192,8 @@ namespace GameRes.Formats.Will } #region IDisposable Members - bool disposed = false; - public void Dispose () { - if (!disposed) - { - m_input.Dispose(); - disposed = true; - } - GC.SuppressFinalize (this); } #endregion }