From 883ee94a7ef150ae1e239a0867e658d585da85e8 Mon Sep 17 00:00:00 2001 From: morkt Date: Sat, 2 Jul 2016 06:06:32 +0400 Subject: [PATCH] (InputProxyStream): generalization of input filters. --- ArcFormats/Bruns/AudioUM3.cs | 12 +------- ArcFormats/Bruns/ImageEENC.cs | 19 +----------- ArcFormats/CommonStreams.cs | 55 +++++++++++++---------------------- ArcFormats/Entis/AudioMIO.cs | 40 +++++-------------------- ArcFormats/Ikura/ImageGGP.cs | 45 +++------------------------- ArcFormats/KiriKiri/ArcXP3.cs | 2 +- ArcFormats/LzssStream.cs | 26 ++--------------- ArcFormats/VnEngine/ArcAXR.cs | 14 +-------- 8 files changed, 39 insertions(+), 174 deletions(-) diff --git a/ArcFormats/Bruns/AudioUM3.cs b/ArcFormats/Bruns/AudioUM3.cs index c1a2bea2..b4290377 100644 --- a/ArcFormats/Bruns/AudioUM3.cs +++ b/ArcFormats/Bruns/AudioUM3.cs @@ -42,7 +42,7 @@ namespace GameRes.Formats } } - internal class Um3Stream : ProxyStream + internal class Um3Stream : InputProxyStream { public Um3Stream (Stream main, bool leave_open = false) : base (main, leave_open) @@ -72,15 +72,5 @@ namespace GameRes.Formats b ^= 0xFF; return b; } - - public override void Write (byte[] buffer, int offset, int count) - { - throw new NotSupportedException ("Um3Stream.Write not supported"); - } - - public override void WriteByte (byte value) - { - throw new NotSupportedException ("Um3Stream.Write not supported"); - } } } diff --git a/ArcFormats/Bruns/ImageEENC.cs b/ArcFormats/Bruns/ImageEENC.cs index 72e53e31..1a5804cc 100644 --- a/ArcFormats/Bruns/ImageEENC.cs +++ b/ArcFormats/Bruns/ImageEENC.cs @@ -114,7 +114,7 @@ namespace GameRes.Formats.Bruns } } - internal class EencStream : ProxyStream + internal class EencStream : InputProxyStream { uint m_key; @@ -124,8 +124,6 @@ namespace GameRes.Formats.Bruns m_key = key; } - public override bool CanWrite { get { return false; } } - public override int Read (byte[] buffer, int offset, int count) { int pos = (int)Position & 3; @@ -146,20 +144,5 @@ namespace GameRes.Formats.Bruns b ^= (byte)(m_key >> (pos << 3)); return b; } - - public override void SetLength (long length) - { - throw new NotSupportedException ("EencStream.SetLength method is not supported"); - } - - public override void Write (byte[] buffer, int offset, int count) - { - throw new NotSupportedException ("EencStream.Write method is not supported"); - } - - public override void WriteByte (byte value) - { - throw new NotSupportedException ("EencStream.WriteByte method is not supported"); - } } } diff --git a/ArcFormats/CommonStreams.cs b/ArcFormats/CommonStreams.cs index 4e62215b..fdc0523c 100644 --- a/ArcFormats/CommonStreams.cs +++ b/ArcFormats/CommonStreams.cs @@ -91,7 +91,26 @@ namespace GameRes.Formats } } - public class PrefixStream : ProxyStream + public class InputProxyStream : ProxyStream + { + public InputProxyStream (Stream input, bool leave_open = false) : base (input, leave_open) + { + } + + public override bool CanWrite { get { return false; } } + + public override void Write (byte[] buffer, int offset, int count) + { + throw new NotSupportedException ("Stream.Write method is not supported"); + } + + public override void SetLength (long length) + { + throw new NotSupportedException ("Stream.SetLength method is not supported"); + } + } + + public class PrefixStream : InputProxyStream { byte[] m_header; long m_position = 0; @@ -102,7 +121,6 @@ namespace GameRes.Formats m_header = header; } - public override bool CanWrite { get { return false; } } public override long Length { get { return BaseStream.Length + m_header.Length; } } public override long Position { @@ -166,28 +184,13 @@ namespace GameRes.Formats 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"); - } } /// /// Represents a region within existing stream. /// Underlying stream should allow seeking (CanSeek == true). /// - public class StreamRegion : ProxyStream + public class StreamRegion : InputProxyStream { private long m_begin; private long m_end; @@ -206,7 +209,6 @@ namespace GameRes.Formats } public override bool CanSeek { get { return true; } } - public override bool CanWrite { get { return false; } } public override long Length { get { return m_end - m_begin; } } public override long Position { @@ -245,21 +247,6 @@ namespace GameRes.Formats else return -1; } - - public override void SetLength (long length) - { - throw new NotSupportedException ("StreamRegion.SetLength method is not supported"); - } - - public override void Write (byte[] buffer, int offset, int count) - { - throw new NotSupportedException ("StreamRegion.Write method is not supported"); - } - - public override void WriteByte (byte value) - { - throw new NotSupportedException ("StreamRegion.WriteByte method is not supported"); - } } /// diff --git a/ArcFormats/Entis/AudioMIO.cs b/ArcFormats/Entis/AudioMIO.cs index e5bf124e..9c787f7d 100644 --- a/ArcFormats/Entis/AudioMIO.cs +++ b/ArcFormats/Entis/AudioMIO.cs @@ -159,26 +159,22 @@ namespace GameRes.Formats.Entis public uint Size; } - class ChunkStream : Stream + class ChunkStream : InputProxyStream { - Stream m_source; MioChunk m_chunk; - public ChunkStream (Stream source, MioChunk chunk) + public ChunkStream (Stream source, MioChunk chunk) : base (source, true) { - m_source = source; m_chunk = chunk; - m_source.Position = m_chunk.Position; + BaseStream.Position = m_chunk.Position; } public override bool CanRead { get { return true; } } - public override bool CanWrite { get { return false; } } - public override bool CanSeek { get { return m_source.CanSeek; } } public override long Length { get { return m_chunk.Size; } } public override long Position { - get { return m_source.Position-m_chunk.Position; } + get { return BaseStream.Position-m_chunk.Position; } set { Seek (value, SeekOrigin.Begin); } } @@ -187,43 +183,23 @@ namespace GameRes.Formats.Entis if (origin == SeekOrigin.Begin) offset += m_chunk.Position; else if (origin == SeekOrigin.Current) - offset += m_source.Position; + offset += BaseStream.Position; else offset += m_chunk.Position + m_chunk.Size; if (offset < m_chunk.Position) offset = m_chunk.Position; - m_source.Position = offset; + BaseStream.Position = offset; return offset - m_chunk.Position; } - public override void Flush() - { - m_source.Flush(); - } - public override int Read (byte[] buf, int index, int count) { - long remaining = (m_chunk.Position + m_chunk.Size) - m_source.Position; + long remaining = (m_chunk.Position + m_chunk.Size) - BaseStream.Position; if (count > remaining) count = (int)remaining; if (count <= 0) return 0; - return m_source.Read (buf, index, count); - } - - public override void SetLength (long length) - { - throw new System.NotSupportedException (); - } - - public override void Write (byte[] buffer, int offset, int count) - { - throw new System.NotSupportedException (); - } - - public override void WriteByte (byte value) - { - throw new System.NotSupportedException (); + return BaseStream.Read (buf, index, count); } } diff --git a/ArcFormats/Ikura/ImageGGP.cs b/ArcFormats/Ikura/ImageGGP.cs index 3a7ba78a..94088f37 100644 --- a/ArcFormats/Ikura/ImageGGP.cs +++ b/ArcFormats/Ikura/ImageGGP.cs @@ -87,28 +87,23 @@ namespace GameRes.Formats.Ikura } } - internal class EncryptedStream : Stream + internal class EncryptedStream : InputProxyStream { - private Stream m_stream; private byte[] m_key; private long m_position; - private bool m_should_dispose; public EncryptedStream (Stream main, byte[] key, bool leave_open = false) + : base (main, leave_open) { if (null == key) throw new ArgumentNullException ("key"); if (key.Length < 8) throw new ArgumentException ("key"); - m_stream = main; m_key = key; m_position = 0; - m_should_dispose = !leave_open; } - public override bool CanRead { get { return m_stream.CanRead; } } public override bool CanSeek { get { return false; } } - public override bool CanWrite { get { return false; } } public override long Length { get { throw new NotSupportedException(); } } public override long Position { @@ -121,14 +116,9 @@ namespace GameRes.Formats.Ikura throw new NotSupportedException(); } - public override void Flush() - { - m_stream.Flush(); - } - public override int Read (byte[] buffer, int offset, int count) { - int read = m_stream.Read (buffer, offset, count); + int read = BaseStream.Read (buffer, offset, count); if (read > 0) { for (int i = 0; i < read; ++i) @@ -141,39 +131,12 @@ namespace GameRes.Formats.Ikura public override int ReadByte () { - int b = m_stream.ReadByte(); + int b = BaseStream.ReadByte(); if (-1 != b) { b ^= m_key[m_position++ & 7]; } return b; } - - public override void SetLength (long length) - { - throw new NotSupportedException ("EncryptedStream.SetLength method is not supported"); - } - - public override void Write (byte[] buffer, int offset, int count) - { - throw new NotSupportedException ("EncryptedStream.Write method is not supported"); - } - - public override void WriteByte (byte value) - { - throw new NotSupportedException ("EncryptedStream.WriteByte method is not supported"); - } - - bool m_disposed = false; - protected override void Dispose (bool disposing) - { - if (!m_disposed) - { - if (m_should_dispose && disposing) - m_stream.Dispose(); - m_disposed = true; - base.Dispose (disposing); - } - } } } diff --git a/ArcFormats/KiriKiri/ArcXP3.cs b/ArcFormats/KiriKiri/ArcXP3.cs index 071bbf65..cd420145 100644 --- a/ArcFormats/KiriKiri/ArcXP3.cs +++ b/ArcFormats/KiriKiri/ArcXP3.cs @@ -761,7 +761,7 @@ NextEntry: long m_offset = 0; bool m_eof = false; - public override bool CanRead { get { return m_stream != null; } } + public override bool CanRead { get { return !disposed; } } public override bool CanSeek { get { return false; } } public override bool CanWrite { get { return false; } } public override long Length { get { return m_entry.UnpackedSize; } } diff --git a/ArcFormats/LzssStream.cs b/ArcFormats/LzssStream.cs index 534d7451..41ffe958 100644 --- a/ArcFormats/LzssStream.cs +++ b/ArcFormats/LzssStream.cs @@ -138,26 +138,21 @@ namespace GameRes.Compression #endregion } - public class LzssStream : Stream + public class LzssStream : GameRes.Formats.InputProxyStream { - Stream m_input; LzssCoroutine m_reader; - bool m_should_dispose; public LzssStream (Stream input, LzssMode mode = LzssMode.Decompress, bool leave_open = false) + : base (input, leave_open) { if (mode != LzssMode.Decompress) throw new NotImplementedException ("LzssStream compression not implemented"); - m_input = input; m_reader = new LzssCoroutine (input); - m_should_dispose = !leave_open; } public LzssSettings Config { get { return m_reader; } } - public override bool CanRead { get { return m_input.CanRead; } } public override bool CanSeek { get { return false; } } - public override bool CanWrite { get { return false; } } public override long Length { get { throw new NotSupportedException ("LzssStream.Length property is not supported"); } @@ -184,29 +179,12 @@ namespace GameRes.Compression throw new NotSupportedException ("LzssStream.Seek method is not supported"); } - public override void SetLength (long length) - { - throw new NotSupportedException ("LzssStream.SetLength method is not supported"); - } - - public override void Write (byte[] buffer, int offset, int count) - { - throw new NotSupportedException ("LzssStream.Write method is not supported"); - } - - public override void WriteByte (byte value) - { - throw new NotSupportedException ("LzssStream.WriteByte method is not supported"); - } - #region IDisposable Members bool m_disposed = false; protected override void Dispose (bool disposing) { if (!m_disposed) { - if (m_should_dispose && disposing) - m_input.Dispose(); m_reader.Dispose(); m_disposed = true; base.Dispose (disposing); diff --git a/ArcFormats/VnEngine/ArcAXR.cs b/ArcFormats/VnEngine/ArcAXR.cs index a808b86b..75e98de0 100644 --- a/ArcFormats/VnEngine/ArcAXR.cs +++ b/ArcFormats/VnEngine/ArcAXR.cs @@ -134,7 +134,7 @@ namespace GameRes.Formats.VnEngine } } - internal class AxrEncryptedStream : ProxyStream + internal class AxrEncryptedStream : InputProxyStream { byte[] m_key; @@ -144,8 +144,6 @@ namespace GameRes.Formats.VnEngine m_key = key; } - public override bool CanWrite { get { return false; } } - public override int Read (byte[] buffer, int offset, int count) { int start = (int)Position; @@ -165,15 +163,5 @@ namespace GameRes.Formats.VnEngine b ^= m_key[start]; return b; } - - public override void Write (byte[] buffer, int offset, int count) - { - throw new NotSupportedException ("AxrEncryptedStream.Write method is not supported"); - } - - public override void WriteByte (byte value) - { - throw new NotSupportedException ("AxrEncryptedStream.WriteByte method is not supported"); - } } }