mirror of
https://github.com/crskycode/GARbro.git
synced 2024-11-27 15:44:00 +08:00
140 lines
4.7 KiB
C#
140 lines
4.7 KiB
C#
//! \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)
|
|
{
|
|
if (m_header.Length == m_position)
|
|
m_stream.Position = 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);
|
|
}
|
|
}
|
|
}
|
|
}
|