From 84985d18f5cdb9edff9b5c42d929c48937632c30 Mon Sep 17 00:00:00 2001 From: morkt Date: Mon, 17 Oct 2016 16:57:13 +0400 Subject: [PATCH] removed redundant ArcView.Reader instances. --- ArcFormats/Ffa/ArcBlackPackage.cs | 22 ++++++------- ArcFormats/Glib2/ImagePGX.cs | 53 +++++++++++++++---------------- ArcFormats/Kaguya/ArcLINK.cs | 17 ++++------ ArcFormats/Kiss/ArcARC.cs | 3 +- ArcFormats/SuperNekoX/ArcGPC.cs | 7 ++-- 5 files changed, 47 insertions(+), 55 deletions(-) diff --git a/ArcFormats/Ffa/ArcBlackPackage.cs b/ArcFormats/Ffa/ArcBlackPackage.cs index d65e98c9..eb72e2ed 100644 --- a/ArcFormats/Ffa/ArcBlackPackage.cs +++ b/ArcFormats/Ffa/ArcBlackPackage.cs @@ -82,18 +82,18 @@ namespace GameRes.Formats.Ffa if (!entry.Name.EndsWith (".so4", StringComparison.InvariantCultureIgnoreCase) && !entry.Name.EndsWith (".so5", StringComparison.InvariantCultureIgnoreCase)) return input; - using (var header = new ArcView.Reader (input)) + int packed = input.ReadInt32(); + int unpacked = input.ReadInt32(); + if (packed+8 != entry.Size || packed <= 0 || unpacked <= 0) { - int packed = header.ReadInt32(); - int unpacked = header.ReadInt32(); - if (packed+8 != entry.Size || packed <= 0 || unpacked <= 0) - return input; - using (input) - using (var reader = new LzssReader (input, packed, unpacked)) - { - reader.Unpack(); - return new BinMemoryStream (reader.Data, entry.Name); - } + input.Position = 0; + return input; + } + using (input) + using (var reader = new LzssReader (input, packed, unpacked)) + { + reader.Unpack(); + return new BinMemoryStream (reader.Data, entry.Name); } } } diff --git a/ArcFormats/Glib2/ImagePGX.cs b/ArcFormats/Glib2/ImagePGX.cs index e7c1d03f..e78995ed 100644 --- a/ArcFormats/Glib2/ImagePGX.cs +++ b/ArcFormats/Glib2/ImagePGX.cs @@ -77,13 +77,13 @@ namespace GameRes.Formats.Glib2 stream.Position = 0x20; if (0 != (meta.Flags & 0x1000)) { - ReadGms (stream.AsStream); + ReadGms (stream); } 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.AsStream, pixels); + LzssUnpack (stream, pixels); var layer = InfoCache.GetInfo (info.FileName); if (null != layer && null != layer.Rect) { @@ -93,7 +93,7 @@ namespace GameRes.Formats.Glib2 return ImageData.Create (info, format, null, pixels); } - void ReadGms (Stream input) + void ReadGms (IBinaryStream input) { var header = new byte[0x10]; if (0x10 != input.Read (header, 0, 0x10)) @@ -123,42 +123,39 @@ namespace GameRes.Formats.Glib2 throw new System.NotImplementedException ("PgxFormat.Write not implemented"); } - static void LzssUnpack (Stream input, byte[] output) + static void LzssUnpack (IBinaryStream input, byte[] output) { var frame = new byte[0x1000]; int frame_pos = 0xFEE; - using (var lz = new ArcView.Reader (input)) + int dst = 0; + int bits = 1; + while (dst < output.Length) { - int dst = 0; - int bits = 1; - while (dst < output.Length) - { - if (1 == bits) - bits = lz.ReadByte() | 0x100; + if (1 == bits) + bits = input.ReadUInt8() | 0x100; - if (0 != (bits & 1)) + if (0 != (bits & 1)) + { + byte b = input.ReadUInt8(); + output[dst++] = b; + frame[frame_pos++] = b; + frame_pos &= 0xFFF; + } + else + { + byte lo = input.ReadUInt8(); + byte hi = input.ReadUInt8(); + int offset = (hi & 0xF0) << 4 | lo; + int count = Math.Min ((~hi & 0xF) + 3, output.Length-dst); + for (int i = 0; i < count; ++i) { - byte b = lz.ReadByte(); + byte b = frame[offset++ & 0xFFF]; output[dst++] = b; frame[frame_pos++] = b; frame_pos &= 0xFFF; } - else - { - byte lo = lz.ReadByte(); - byte hi = lz.ReadByte(); - int offset = (hi & 0xF0) << 4 | lo; - int count = Math.Min ((~hi & 0xF) + 3, output.Length-dst); - for (int i = 0; i < count; ++i) - { - byte b = frame[offset++ & 0xFFF]; - output[dst++] = b; - frame[frame_pos++] = b; - frame_pos &= 0xFFF; - } - } - bits >>= 1; } + bits >>= 1; } } } diff --git a/ArcFormats/Kaguya/ArcLINK.cs b/ArcFormats/Kaguya/ArcLINK.cs index 37652bbb..28636efa 100644 --- a/ArcFormats/Kaguya/ArcLINK.cs +++ b/ArcFormats/Kaguya/ArcLINK.cs @@ -97,18 +97,15 @@ namespace GameRes.Formats.Kaguya public byte[] Data { get { return m_output; } } - public BmrDecoder (Stream input) + public BmrDecoder (IBinaryStream input) { input.Position = 3; - using (var header = new ArcView.Reader (input)) - { - m_step = header.ReadByte(); - m_final_size = header.ReadInt32(); - m_key = header.ReadInt32(); - int unpacked_size = header.ReadInt32(); - m_output = new byte[unpacked_size]; - m_input = new MsbBitStream (input, true); - } + m_step = input.ReadUInt8(); + m_final_size = input.ReadInt32(); + m_key = input.ReadInt32(); + int unpacked_size = input.ReadInt32(); + m_output = new byte[unpacked_size]; + m_input = new MsbBitStream (input.AsStream, true); } public void Unpack () diff --git a/ArcFormats/Kiss/ArcARC.cs b/ArcFormats/Kiss/ArcARC.cs index e0adbf1c..a14a1f61 100644 --- a/ArcFormats/Kiss/ArcARC.cs +++ b/ArcFormats/Kiss/ArcARC.cs @@ -54,7 +54,6 @@ namespace GameRes.Formats.Kiss var dir = new List (count); using (var input = file.CreateStream()) - using (var reader = new ArcView.Reader (input)) { long prev_offset = 4; input.Position = 4; @@ -64,7 +63,7 @@ namespace GameRes.Formats.Kiss if (string.IsNullOrWhiteSpace (name)) return null; var entry = FormatCatalog.Instance.Create (name); - entry.Offset = reader.ReadInt64(); + entry.Offset = input.ReadInt64(); if (entry.Offset < prev_offset || entry.Offset > file.MaxOffset) return null; dir.Add (entry); diff --git a/ArcFormats/SuperNekoX/ArcGPC.cs b/ArcFormats/SuperNekoX/ArcGPC.cs index 9491c2eb..5aac4556 100644 --- a/ArcFormats/SuperNekoX/ArcGPC.cs +++ b/ArcFormats/SuperNekoX/ArcGPC.cs @@ -108,14 +108,13 @@ namespace GameRes.Formats.SuperNekoX void DetectFileTypes (ArcView file, List dir) { using (var input = file.CreateStream()) - using (var reader = new ArcView.Reader (input)) { var buffer = new byte[0x10]; foreach (PackedEntry entry in dir) { input.Position = entry.Offset; - uint packed_size = reader.ReadUInt32(); - entry.UnpackedSize = reader.ReadUInt32(); + uint packed_size = input.ReadUInt32(); + entry.UnpackedSize = input.ReadUInt32(); entry.Offset += 8; if (0 == packed_size) { @@ -135,7 +134,7 @@ namespace GameRes.Formats.SuperNekoX signature = LittleEndian.ToUInt32 (buffer, 0); } else - signature = reader.ReadUInt32(); + signature = input.ReadUInt32(); IResource res; if (0x020000 == signature || 0x0A0000 == signature) res = ImageFormat.Tga;