(InputProxyStream): generalization of input filters.

This commit is contained in:
morkt 2016-07-02 06:06:32 +04:00
parent cc22f988c6
commit 883ee94a7e
8 changed files with 39 additions and 174 deletions

View File

@ -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");
}
}
}

View File

@ -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");
}
}
}

View File

@ -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");
}
}
/// <summary>
/// Represents a region within existing stream.
/// Underlying stream should allow seeking (CanSeek == true).
/// </summary>
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");
}
}
/// <summary>

View File

@ -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);
}
}

View File

@ -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);
}
}
}
}

View File

@ -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; } }

View File

@ -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);

View File

@ -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");
}
}
}