mirror of
https://github.com/crskycode/GARbro.git
synced 2026-07-08 01:30:18 +08:00
IBinaryStream migration - continued.
This commit is contained in:
@@ -39,12 +39,12 @@ namespace GameRes.Formats.Circus
|
||||
OffsetMap = offset_map;
|
||||
}
|
||||
|
||||
internal Stream OpenByOffset (uint offset)
|
||||
internal IBinaryStream OpenByOffset (uint offset)
|
||||
{
|
||||
Entry entry;
|
||||
if (!OffsetMap.TryGetValue (offset, out entry))
|
||||
throw new FileNotFoundException();
|
||||
return OpenEntry (entry);
|
||||
return OpenBinaryEntry (entry);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -48,31 +48,29 @@ namespace GameRes.Formats.Circus
|
||||
public override string Description { get { return "Circus image format"; } }
|
||||
public override uint Signature { get { return 0x47585243; } } // 'CRXG'
|
||||
|
||||
public override ImageMetaData ReadMetaData (Stream stream)
|
||||
public override ImageMetaData ReadMetaData (IBinaryStream stream)
|
||||
{
|
||||
var header = new byte[0x14];
|
||||
if (header.Length != stream.Read (header, 0, header.Length))
|
||||
return null;
|
||||
int compression = LittleEndian.ToUInt16 (header, 0xC);
|
||||
var header = stream.ReadHeader (0x14);
|
||||
int compression = header.ToUInt16 (0xC);
|
||||
if (compression < 1 || compression > 3)
|
||||
return null;
|
||||
int depth = LittleEndian.ToInt16 (header, 0x10);
|
||||
int depth = header.ToInt16 (0x10);
|
||||
var info = new CrxMetaData
|
||||
{
|
||||
Width = LittleEndian.ToUInt16 (header, 8),
|
||||
Height = LittleEndian.ToUInt16 (header, 10),
|
||||
OffsetX = LittleEndian.ToInt16 (header, 4),
|
||||
OffsetY = LittleEndian.ToInt16 (header, 6),
|
||||
Width = header.ToUInt16 (8),
|
||||
Height = header.ToUInt16 (10),
|
||||
OffsetX = header.ToInt16 (4),
|
||||
OffsetY = header.ToInt16 (6),
|
||||
BPP = 0 == depth ? 24 : 1 == depth ? 32 : 8,
|
||||
Compression = compression,
|
||||
CompressionFlags = LittleEndian.ToUInt16 (header, 0xE),
|
||||
CompressionFlags = header.ToUInt16 (0xE),
|
||||
Colors = depth,
|
||||
Mode = LittleEndian.ToUInt16 (header, 0x12),
|
||||
Mode = header.ToUInt16 (0x12),
|
||||
};
|
||||
return info;
|
||||
}
|
||||
|
||||
public override ImageData Read (Stream stream, ImageMetaData info)
|
||||
public override ImageData Read (IBinaryStream stream, ImageMetaData info)
|
||||
{
|
||||
using (var reader = new Reader (stream, (CrxMetaData)info))
|
||||
{
|
||||
@@ -88,7 +86,7 @@ namespace GameRes.Formats.Circus
|
||||
|
||||
internal sealed class Reader : IDisposable
|
||||
{
|
||||
BinaryReader m_input;
|
||||
IBinaryStream m_input;
|
||||
byte[] m_output;
|
||||
int m_width;
|
||||
int m_height;
|
||||
@@ -103,7 +101,7 @@ namespace GameRes.Formats.Circus
|
||||
public BitmapPalette Palette { get; private set; }
|
||||
public int Stride { get { return m_stride; } }
|
||||
|
||||
public Reader (Stream input, CrxMetaData info)
|
||||
public Reader (IBinaryStream input, CrxMetaData info)
|
||||
{
|
||||
m_width = (int)info.Width;
|
||||
m_height = (int)info.Height;
|
||||
@@ -120,8 +118,8 @@ namespace GameRes.Formats.Circus
|
||||
}
|
||||
m_stride = (m_width * m_bpp / 8 + 3) & ~3;
|
||||
m_output = new byte[m_height*m_stride];
|
||||
m_input = new ArcView.Reader (input);
|
||||
input.Position = 0x14;
|
||||
m_input = input;
|
||||
m_input.Position = 0x14;
|
||||
if (8 == m_bpp)
|
||||
ReadPalette (info.Colors);
|
||||
}
|
||||
@@ -157,7 +155,7 @@ namespace GameRes.Formats.Circus
|
||||
if (m_compression >= 3)
|
||||
{
|
||||
int count = m_input.ReadInt32();
|
||||
m_input.BaseStream.Seek (count * 0x10, SeekOrigin.Current);
|
||||
m_input.Seek (count * 0x10, SeekOrigin.Current);
|
||||
}
|
||||
if (0 != (m_flags & 0x10))
|
||||
{
|
||||
@@ -321,7 +319,7 @@ namespace GameRes.Formats.Circus
|
||||
{
|
||||
int pixel_size = m_bpp / 8;
|
||||
int src_stride = m_width * pixel_size;
|
||||
using (var zlib = new ZLibStream (m_input.BaseStream, CompressionMode.Decompress, true))
|
||||
using (var zlib = new ZLibStream (m_input.AsStream, CompressionMode.Decompress, true))
|
||||
using (var src = new BinaryReader (zlib))
|
||||
{
|
||||
if (m_bpp >= 24)
|
||||
@@ -400,16 +398,8 @@ namespace GameRes.Formats.Circus
|
||||
}
|
||||
|
||||
#region IDisposable Members
|
||||
bool m_disposed = false;
|
||||
|
||||
public void Dispose ()
|
||||
{
|
||||
if (!m_disposed)
|
||||
{
|
||||
m_input.Dispose();
|
||||
m_disposed = true;
|
||||
}
|
||||
GC.SuppressFinalize (this);
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
|
||||
@@ -50,17 +50,14 @@ namespace GameRes.Formats.Circus
|
||||
Extensions = new string[] { "crx" };
|
||||
}
|
||||
|
||||
public override ImageMetaData ReadMetaData (Stream stream)
|
||||
public override ImageMetaData ReadMetaData (IBinaryStream stream)
|
||||
{
|
||||
var header = new byte[0x24];
|
||||
if (header.Length != stream.Read (header, 0, header.Length))
|
||||
return null;
|
||||
var header = stream.ReadHeader (0x24);
|
||||
CrxdMetaData info = null;
|
||||
if (Binary.AsciiEqual (header, 0x20, "CRXJ"))
|
||||
if (header.AsciiEqual (0x20, "CRXJ"))
|
||||
{
|
||||
stream.Position = 0x28;
|
||||
stream.Read (header, 0, 4);
|
||||
uint diff_offset = LittleEndian.ToUInt32 (header, 0);
|
||||
uint diff_offset = stream.ReadUInt32();
|
||||
using (var crx = OpenByOffset (diff_offset))
|
||||
{
|
||||
if (null == crx)
|
||||
@@ -70,9 +67,10 @@ namespace GameRes.Formats.Circus
|
||||
info.DiffOffset = diff_offset;
|
||||
}
|
||||
}
|
||||
else if (Binary.AsciiEqual (header, 0x20, "CRXG"))
|
||||
else if (header.AsciiEqual (0x20, "CRXG"))
|
||||
{
|
||||
using (var crx = new StreamRegion (stream, 0x20, true))
|
||||
using (var crx_input = new StreamRegion (stream.AsStream, 0x20, true))
|
||||
using (var crx = new BinaryStream (crx_input))
|
||||
{
|
||||
var diff_info = base.ReadMetaData (crx) as CrxMetaData;
|
||||
if (null == diff_info)
|
||||
@@ -91,7 +89,7 @@ namespace GameRes.Formats.Circus
|
||||
}
|
||||
if (info != null)
|
||||
{
|
||||
info.BaseOffset = LittleEndian.ToUInt32 (header, 8);
|
||||
info.BaseOffset = header.ToUInt32 (8);
|
||||
info.BaseFileName = Binary.GetCString (header, 0xC, 0x14);
|
||||
}
|
||||
return info;
|
||||
@@ -108,27 +106,34 @@ namespace GameRes.Formats.Circus
|
||||
return arc.OpenByOffset (offset);
|
||||
}
|
||||
|
||||
Stream OpenDiffStream (Stream diff, CrxdMetaData info)
|
||||
IBinaryStream OpenDiffStream (IBinaryStream diff, CrxdMetaData info)
|
||||
{
|
||||
Stream input;
|
||||
if (0 == info.DiffOffset)
|
||||
return new StreamRegion (diff, 0x20, true);
|
||||
diff = OpenByOffset (info.DiffOffset);
|
||||
if (null == diff)
|
||||
throw new FileNotFoundException ("Referenced diff image not found");
|
||||
return new StreamRegion (diff, 0x20);
|
||||
{
|
||||
input = new StreamRegion (diff.AsStream, 0x20, true);
|
||||
}
|
||||
else
|
||||
{
|
||||
diff = OpenByOffset (info.DiffOffset);
|
||||
if (null == diff)
|
||||
throw new FileNotFoundException ("Referenced diff image not found");
|
||||
input = new StreamRegion (diff, 0x20);
|
||||
}
|
||||
return new BinaryStream (input);
|
||||
}
|
||||
|
||||
public override ImageData Read (Stream stream, ImageMetaData info)
|
||||
public override ImageData Read (IBinaryStream stream, ImageMetaData info)
|
||||
{
|
||||
var meta = (CrxdMetaData)info;
|
||||
Stream base_file = OpenByOffset (meta.BaseOffset);
|
||||
IBinaryStream base_file = OpenByOffset (meta.BaseOffset);
|
||||
if (null == base_file)
|
||||
{
|
||||
var dir_name = VFS.GetDirectoryName (meta.FileName);
|
||||
var name = VFS.CombinePath (dir_name, meta.BaseFileName);
|
||||
if (!VFS.FileExists (name))
|
||||
throw new FileNotFoundException ("Base image not found", meta.BaseFileName);
|
||||
base_file = VFS.OpenSeekableStream (name);
|
||||
base_file = VFS.OpenBinaryStream (name);
|
||||
}
|
||||
using (base_file)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user