From 3214e05609cdeed896d6e190f15156c0ede696cd Mon Sep 17 00:00:00 2001 From: morkt Date: Thu, 26 Feb 2015 22:07:49 +0400 Subject: [PATCH] Moved PrefixStream class implementation to separate file. --- ArcFormats/ArcCommon.cs | 139 ++++++++++++++++++++++++++ ArcFormats/ArcFormats.csproj | 1 + ArcFormats/ArcNitro.cs | 2 +- ArcFormats/ArcRPA.cs | 111 +------------------- ArcFormats/Properties/AssemblyInfo.cs | 4 +- 5 files changed, 144 insertions(+), 113 deletions(-) create mode 100644 ArcFormats/ArcCommon.cs diff --git a/ArcFormats/ArcCommon.cs b/ArcFormats/ArcCommon.cs new file mode 100644 index 00000000..1be580a3 --- /dev/null +++ b/ArcFormats/ArcCommon.cs @@ -0,0 +1,139 @@ +//! \file ArcCommon.cs +//! \date Tue Aug 19 09:45:38 2014 +//! \brief Classes and functions common for various resource files. +// +// Copyright (C) 2014-2015 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.IO; + +namespace GameRes.Formats +{ + public class PrefixStream : Stream + { + byte[] m_header; + Stream m_stream; + long m_position = 0; + + public PrefixStream (byte[] header, Stream main) + { + m_header = header; + m_stream = main; + } + + public override bool CanRead { get { return m_stream.CanRead; } } + public override bool CanSeek { get { return m_stream.CanSeek; } } + public override bool CanWrite { get { return false; } } + public override long Length { get { return m_stream.Length + m_header.Length; } } + public override long Position + { + get { return m_position; } + set + { + m_position = Math.Max (value, 0); + if (m_position > m_header.Length) + { + long stream_pos = m_stream.Seek (m_position - m_header.Length, SeekOrigin.Begin); + m_position = m_header.Length + stream_pos; + } + } + } + + public override void Flush() + { + m_stream.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 int Read (byte[] buffer, int offset, int count) + { + int read = 0; + if (m_position < m_header.Length) + { + int header_count = Math.Min (count, m_header.Length - (int)m_position); + Buffer.BlockCopy (m_header, (int)m_position, buffer, offset, header_count); + m_position += header_count; + read += header_count; + offset += header_count; + count -= header_count; + if (count > 0) + m_stream.Position = 0; + } + if (count > 0) + { + int stream_read = m_stream.Read (buffer, offset, count); + m_position += stream_read; + read += stream_read; + } + return read; + } + + public override int ReadByte () + { + if (m_position < m_header.Length) + return m_header[m_position++]; + if (m_position == m_header.Length) + m_stream.Position = 0; + int b = m_stream.ReadByte(); + if (-1 != b) + m_position++; + return b; + } + + public override void SetLength (long length) + { + throw new NotSupportedException ("PrefixStream.SetLength method is not supported"); + } + + public override void Write (byte[] buffer, int offset, int count) + { + throw new NotSupportedException ("PrefixStream.Write method is not supported"); + } + + public override void WriteByte (byte value) + { + throw new NotSupportedException ("PrefixStream.WriteByte method is not supported"); + } + + bool disposed = false; + protected override void Dispose (bool disposing) + { + if (!disposed) + { + m_stream.Dispose(); + disposed = true; + base.Dispose (disposing); + } + } + } +} diff --git a/ArcFormats/ArcFormats.csproj b/ArcFormats/ArcFormats.csproj index 372dd680..8c1c5228 100644 --- a/ArcFormats/ArcFormats.csproj +++ b/ArcFormats/ArcFormats.csproj @@ -64,6 +64,7 @@ + diff --git a/ArcFormats/ArcNitro.cs b/ArcFormats/ArcNitro.cs index 0e4a3e84..cece59e0 100644 --- a/ArcFormats/ArcNitro.cs +++ b/ArcFormats/ArcNitro.cs @@ -145,7 +145,7 @@ namespace GameRes.Formats.NitroPlus } if (enc_size == entry.Size) return new MemoryStream (buf, false); - return new RenPy.RpaStream (buf, arc.File.CreateStream (entry.Offset+enc_size, entry.Size-enc_size)); + return new PrefixStream (buf, arc.File.CreateStream (entry.Offset+enc_size, entry.Size-enc_size)); } } } diff --git a/ArcFormats/ArcRPA.cs b/ArcFormats/ArcRPA.cs index 3be446c6..8ebfa4fe 100644 --- a/ArcFormats/ArcRPA.cs +++ b/ArcFormats/ArcRPA.cs @@ -129,7 +129,7 @@ namespace GameRes.Formats.RenPy var rpa_entry = entry as RpaEntry; if (null == rpa_entry || null == rpa_entry.Header) return input; - return new RpaStream (rpa_entry.Header, input); + return new PrefixStream (rpa_entry.Header, input); } public override ResourceOptions GetDefaultOptions () @@ -839,113 +839,4 @@ namespace GameRes.Formats.RenPy return Encoding.UTF8.GetString (m_bytes); } } - - public class RpaStream : Stream - { - byte[] m_header; - Stream m_stream; - long m_position = 0; - - public RpaStream (byte[] header, Stream main) - { - m_header = header; - m_stream = main; - } - - public override bool CanRead { get { return m_stream.CanRead; } } - public override bool CanSeek { get { return m_stream.CanSeek; } } - public override bool CanWrite { get { return false; } } - public override long Length { get { return m_stream.Length + m_header.Length; } } - public override long Position - { - get { return m_position; } - set - { - m_position = Math.Max (value, 0); - if (m_position > m_header.Length) - { - long stream_pos = m_stream.Seek (m_position - m_header.Length, SeekOrigin.Begin); - m_position = m_header.Length + stream_pos; - } - } - } - - public override void Flush() - { - m_stream.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 int Read (byte[] buffer, int offset, int count) - { - int read = 0; - if (m_position < m_header.Length) - { - int header_count = Math.Min (count, m_header.Length - (int)m_position); - Buffer.BlockCopy (m_header, (int)m_position, buffer, offset, header_count); - m_position += header_count; - read += header_count; - offset += header_count; - count -= header_count; - if (count > 0) - m_stream.Position = 0; - } - if (count > 0) - { - int stream_read = m_stream.Read (buffer, offset, count); - m_position += stream_read; - read += stream_read; - } - return read; - } - - public override int ReadByte () - { - if (m_position < m_header.Length) - return m_header[m_position++]; - if (m_position == m_header.Length) - m_stream.Position = 0; - int b = m_stream.ReadByte(); - if (-1 != b) - m_position++; - return b; - } - - public override void SetLength (long length) - { - throw new NotSupportedException ("RpaStream.SetLength method is not supported"); - } - - public override void Write (byte[] buffer, int offset, int count) - { - throw new NotSupportedException ("RpaStream.Write method is not supported"); - } - - public override void WriteByte (byte value) - { - throw new NotSupportedException ("RpaStream.WriteByte method is not supported"); - } - - bool disposed = false; - protected override void Dispose (bool disposing) - { - if (!disposed) - { - m_stream.Dispose(); - disposed = true; - base.Dispose (disposing); - } - } - } } diff --git a/ArcFormats/Properties/AssemblyInfo.cs b/ArcFormats/Properties/AssemblyInfo.cs index 4e318937..adf36034 100644 --- a/ArcFormats/Properties/AssemblyInfo.cs +++ b/ArcFormats/Properties/AssemblyInfo.cs @@ -32,5 +32,5 @@ using System.Runtime.InteropServices; // You can specify all the values or you can default the Build and Revision Numbers // by using the '*' as shown below: // [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion ("1.0.3.24")] -[assembly: AssemblyFileVersion ("1.0.3.24")] +[assembly: AssemblyVersion ("1.0.3.25")] +[assembly: AssemblyFileVersion ("1.0.3.25")]