IBinaryStream migration.

This commit is contained in:
morkt 2016-10-16 09:22:53 +04:00
parent d0c1d5da01
commit bb18303eb4
251 changed files with 3277 additions and 3630 deletions

View File

@ -248,7 +248,7 @@ namespace GameRes.Formats.AZSys
Buffer.BlockCopy (data, 0, header, 0, 0x10); Buffer.BlockCopy (data, 0, header, 0, 0x10);
return new PrefixStream (header, asb); return new PrefixStream (header, asb);
} }
return new MemoryStream (data); return new BinMemoryStream (data, entry.Name);
} }
uint ReadSysenvSeed (ArcView file, IEnumerable<Entry> dir, uint key) uint ReadSysenvSeed (ArcView file, IEnumerable<Entry> dir, uint key)

View File

@ -54,51 +54,48 @@ namespace GameRes.Formats.AZSys
throw new System.NotImplementedException ("CpbFormat.Write not implemented"); throw new System.NotImplementedException ("CpbFormat.Write not implemented");
} }
public override ImageMetaData ReadMetaData (Stream stream) public override ImageMetaData ReadMetaData (IBinaryStream file)
{ {
stream.Seek (4, SeekOrigin.Current); file.Position = 4;
int type = stream.ReadByte(); int type = file.ReadByte();
int bpp = stream.ReadByte(); int bpp = file.ReadByte();
if (24 != bpp && 32 != bpp) if (24 != bpp && 32 != bpp)
throw new NotSupportedException ("Not supported CPB image format"); throw new NotSupportedException ("Not supported CPB image format");
using (var input = new ArcView.Reader (stream)) int version = file.ReadInt16();
if (1 != version && 0 != version)
throw new NotSupportedException ("Not supported CPB image version");
var info = new CpbMetaData {
Type = type,
Version = version,
BPP = bpp,
};
if (1 == version)
{ {
int version = input.ReadInt16 (); file.ReadUInt32();
if (1 != version && 0 != version) info.Width = file.ReadUInt16();
throw new NotSupportedException ("Not supported CPB image version"); info.Height = file.ReadUInt16();
var info = new CpbMetaData { info.Channel[0] = file.ReadUInt32();
Type = type, info.Channel[1] = file.ReadUInt32();
Version = version, info.Channel[2] = file.ReadUInt32();
BPP = bpp, info.Channel[3] = file.ReadUInt32();
};
if (1 == version)
{
input.ReadUInt32();
info.Width = input.ReadUInt16();
info.Height = input.ReadUInt16();
info.Channel[0] = input.ReadUInt32();
info.Channel[1] = input.ReadUInt32();
info.Channel[2] = input.ReadUInt32();
info.Channel[3] = input.ReadUInt32();
}
else
{
info.Width = input.ReadUInt16();
info.Height = input.ReadUInt16();
input.ReadUInt32();
info.Channel[0] = input.ReadUInt32();
info.Channel[1] = input.ReadUInt32();
info.Channel[2] = input.ReadUInt32();
info.Channel[3] = input.ReadUInt32();
}
info.DataOffset = (uint)stream.Position;
return info;
} }
else
{
info.Width = file.ReadUInt16();
info.Height = file.ReadUInt16();
file.ReadUInt32();
info.Channel[0] = file.ReadUInt32();
info.Channel[1] = file.ReadUInt32();
info.Channel[2] = file.ReadUInt32();
info.Channel[3] = file.ReadUInt32();
}
info.DataOffset = (uint)file.Position;
return info;
} }
public override ImageData Read (Stream stream, ImageMetaData info) public override ImageData Read (IBinaryStream stream, ImageMetaData info)
{ {
var reader = new Reader (stream, (CpbMetaData)info); var reader = new Reader (stream.AsStream, (CpbMetaData)info);
reader.Unpack(); reader.Unpack();
return ImageData.Create (info, reader.Format, reader.Palette, reader.Data); return ImageData.Create (info, reader.Format, reader.Palette, reader.Data);
} }

View File

@ -45,46 +45,38 @@ namespace GameRes.Formats.AZSys
public override string Description { get { return "AZ system image format"; } } public override string Description { get { return "AZ system image format"; } }
public override uint Signature { get { return 0x31505954; } } // 'TYP1' public override uint Signature { get { return 0x31505954; } } // 'TYP1'
public Typ1Format () public override ImageMetaData ReadMetaData (IBinaryStream stream)
{
Extensions = new string[] { "cpb" };
}
public override ImageMetaData ReadMetaData (Stream stream)
{ {
stream.Position = 4; stream.Position = 4;
int bpp = stream.ReadByte(); int bpp = stream.ReadByte();
bool has_palette = stream.ReadByte() != 0; bool has_palette = stream.ReadByte() != 0;
using (var input = new ArcView.Reader (stream)) var info = new Typ1MetaData { BPP = bpp };
info.Width = stream.ReadUInt16();
info.Height = stream.ReadUInt16();
uint packed_size = stream.ReadUInt32();
uint palette_size = 8 == bpp ? 0x400u : 0u;
if (packed_size+palette_size+0xE == stream.Length)
{ {
var info = new Typ1MetaData { BPP = bpp }; info.SeparateChannels = false;
info.Width = input.ReadUInt16(); info.HasPalette = palette_size > 0;
info.Height = input.ReadUInt16(); info.PackedSize = packed_size;
uint packed_size = input.ReadUInt32();
uint palette_size = 8 == bpp ? 0x400u : 0u;
if (packed_size+palette_size+0xE == stream.Length)
{
info.SeparateChannels = false;
info.HasPalette = palette_size > 0;
info.PackedSize = packed_size;
}
else
{
info.SeparateChannels = true;
info.HasPalette = has_palette;
info.Channel[0] = input.ReadUInt32();
info.Channel[1] = input.ReadUInt32();
info.Channel[2] = input.ReadUInt32();
info.Channel[3] = input.ReadUInt32();
}
return info;
} }
else
{
info.SeparateChannels = true;
info.HasPalette = has_palette;
info.Channel[0] = stream.ReadUInt32();
info.Channel[1] = stream.ReadUInt32();
info.Channel[2] = stream.ReadUInt32();
info.Channel[3] = stream.ReadUInt32();
}
return info;
} }
public override ImageData Read (Stream stream, ImageMetaData info) public override ImageData Read (IBinaryStream stream, ImageMetaData info)
{ {
var meta = (Typ1MetaData)info; var meta = (Typ1MetaData)info;
var reader = new Reader (stream, meta); var reader = new Reader (stream.AsStream, meta);
reader.Unpack(); reader.Unpack();
return ImageData.Create (meta, reader.Format, reader.Palette, reader.Data); return ImageData.Create (meta, reader.Format, reader.Palette, reader.Data);
} }

View File

@ -102,7 +102,7 @@ namespace GameRes.Formats.Abel
{ {
data[i] = (byte)(0xFF - data[i]); data[i] = (byte)(0xFF - data[i]);
} }
return new MemoryStream (data); return new BinMemoryStream (data, entry.Name);
} }
Stream OpenCmpEntry (ArcFile arc, Entry entry) Stream OpenCmpEntry (ArcFile arc, Entry entry)

View File

@ -52,9 +52,9 @@ namespace GameRes.Formats.Abel
Extensions = new string[] { "gps", "cmp" }; Extensions = new string[] { "gps", "cmp" };
} }
public override ImageMetaData ReadMetaData (IBinaryStream stream) public override ImageMetaData ReadMetaData (IBinaryStream file)
{ {
var header = stream.ReadHeader (0x29); var header = file.ReadHeader (0x29);
if (header.Length != 0x29) if (header.Length != 0x29)
return null; return null;
var gps = new GpsMetaData var gps = new GpsMetaData
@ -66,7 +66,8 @@ namespace GameRes.Formats.Abel
PackedSize = header.ToInt32 (0x15), PackedSize = header.ToInt32 (0x15),
}; };
// read BMP header // read BMP header
using (var input = OpenGpsStream (stream, gps.Compression, 0x54)) using (var stream = OpenGpsStream (file, gps.Compression, 0x54))
using (var input = BinaryStream.FromStream (stream, file.Name))
{ {
var bmp_info = base.ReadMetaData (input); var bmp_info = base.ReadMetaData (input);
if (null == bmp_info) if (null == bmp_info)
@ -76,11 +77,12 @@ namespace GameRes.Formats.Abel
} }
} }
public override ImageData Read (IBinaryStream stream, ImageMetaData info) public override ImageData Read (IBinaryStream file, ImageMetaData info)
{ {
var gps = (GpsMetaData)info; var gps = (GpsMetaData)info;
stream.Position = 0x29; file.Position = 0x29;
using (var input = OpenGpsStream (stream, gps.Compression, gps.UnpackedSize)) using (var stream = OpenGpsStream (file, gps.Compression, gps.UnpackedSize))
using (var input = BinaryStream.FromStream (stream, file.Name))
return base.Read (input, info); return base.Read (input, info);
} }
@ -89,30 +91,28 @@ namespace GameRes.Formats.Abel
throw new System.NotImplementedException ("GpsFormat.Write not implemented"); throw new System.NotImplementedException ("GpsFormat.Write not implemented");
} }
IBinaryStream OpenGpsStream (IBinaryStream input, byte compression, int unpacked_size) Stream OpenGpsStream (IBinaryStream input, byte compression, int unpacked_size)
{ {
Stream gps = null;
if (0 == compression) if (0 == compression)
gps = new StreamRegion (input.AsStream, 0x29, true); return new StreamRegion (input.AsStream, 0x29, true);
else if (1 == compression) else if (1 == compression)
gps = OpenRLEStream (input.AsStream, unpacked_size); return OpenRLEStream (input.AsStream, unpacked_size);
else if (2 == compression) else if (2 == compression)
gps = new LzssStream (input.AsStream, LzssMode.Decompress, true); return new LzssStream (input.AsStream, LzssMode.Decompress, true);
else if (3 == compression) else if (3 == compression)
{ {
using (var lzss = new LzssStream (input.AsStream, LzssMode.Decompress, true)) using (var lzss = new LzssStream (input.AsStream, LzssMode.Decompress, true))
gps = OpenRLEStream (lzss, unpacked_size); return OpenRLEStream (lzss, unpacked_size);
} }
else else
throw new InvalidFormatException(); throw new InvalidFormatException();
return new BinaryStream (gps);
} }
Stream OpenRLEStream (Stream input, int output_size) Stream OpenRLEStream (Stream input, int output_size)
{ {
var output = new byte[output_size]; var output = new byte[output_size];
UnpackRLE (input, output); UnpackRLE (input, output);
return new MemoryStream (output); return new BinMemoryStream (output, "");
} }
void UnpackRLE (Stream input, byte[] output) void UnpackRLE (Stream input, byte[] output)

View File

@ -123,7 +123,7 @@ namespace GameRes.Formats.Actgs
arc.File.View.Read (entry.Offset, data, 0, entry.Size); arc.File.View.Read (entry.Offset, data, 0, entry.Size);
Decrypt (data, 1, data.Length-1, actarc.Key); Decrypt (data, 1, data.Length-1, actarc.Key);
data[0] = (byte)'N'; data[0] = (byte)'N';
return new MemoryStream (data); return new BinMemoryStream (data, entry.Name);
} }
if (arc.File.View.AsciiEqual (entry.Offset, "PAK ")) if (arc.File.View.AsciiEqual (entry.Offset, "PAK "))
{ {
@ -136,7 +136,7 @@ namespace GameRes.Formats.Actgs
arc.File.View.Read (entry.Offset, header, 0, length); arc.File.View.Read (entry.Offset, header, 0, length);
Decrypt (header, 0, (int)length, actarc.Key); Decrypt (header, 0, (int)length, actarc.Key);
if (entry.Size <= 0x20) if (entry.Size <= 0x20)
return new MemoryStream (header); return new BinMemoryStream (header, entry.Name);
var rest = arc.File.CreateStream (entry.Offset+0x20, entry.Size-0x20); var rest = arc.File.CreateStream (entry.Offset+0x20, entry.Size-0x20);
return new PrefixStream (header, rest); return new PrefixStream (header, rest);
} }

View File

@ -107,17 +107,15 @@ namespace GameRes.Formats.AdPack
throw new NotImplementedException ("EdtFormat.Write not implemented"); throw new NotImplementedException ("EdtFormat.Write not implemented");
} }
public override ImageMetaData ReadMetaData (Stream stream) public override ImageMetaData ReadMetaData (IBinaryStream stream)
{ {
var header = new byte[0x22]; var header = stream.ReadHeader (0x22);
if (header.Length != stream.Read (header, 0, header.Length)) if (!header.AsciiEqual (".TRUE\x8d\x5d\x8c\xcb\x00"))
return null; return null;
if (!Binary.AsciiEqual (header, ".TRUE\x8d\x5d\x8c\xcb\x00")) uint width = header.ToUInt16 (0x0e);
return null; uint height = header.ToUInt16 (0x10);
uint width = LittleEndian.ToUInt16 (header, 0x0e); uint comp_size = header.ToUInt32 (0x1a);
uint height = LittleEndian.ToUInt16 (header, 0x10); uint extra_size = header.ToUInt32 (0x1e);
uint comp_size = LittleEndian.ToUInt32 (header, 0x1a);
uint extra_size = LittleEndian.ToUInt32 (header, 0x1e);
if (extra_size % 3 != 0 || 0 == extra_size) if (extra_size % 3 != 0 || 0 == extra_size)
return null; return null;
@ -131,21 +129,18 @@ namespace GameRes.Formats.AdPack
}; };
} }
public override ImageData Read (Stream stream, ImageMetaData info) public override ImageData Read (IBinaryStream stream, ImageMetaData info)
{ {
var meta = info as EdtMetaData; var meta = (EdtMetaData)info;
if (null == meta)
throw new ArgumentException ("EdtFormat.Read should be supplied with EdtMetaData", "info");
stream.Position = 0x22; stream.Position = 0x22;
using (var reader = new Reader (stream, meta)) using (var reader = new Reader (stream.AsStream, meta))
{ {
reader.Unpack(); reader.Unpack();
return ImageData.Create (meta, PixelFormats.Bgr24, null, reader.Data, (int)meta.Width*3); return ImageData.Create (meta, PixelFormats.Bgr24, null, reader.Data, (int)meta.Width*3);
} }
} }
internal class Reader : BitReader, IDisposable internal sealed class Reader : BitReader, IDisposable
{ {
MemoryStream m_packed; MemoryStream m_packed;
MemoryStream m_extra; MemoryStream m_extra;
@ -244,24 +239,15 @@ namespace GameRes.Formats.AdPack
#region IDisposable Members #region IDisposable Members
bool disposed = false; bool disposed = false;
public void Dispose () public void Dispose ()
{
Dispose (true);
GC.SuppressFinalize (this);
}
protected virtual void Dispose (bool disposing)
{ {
if (!disposed) if (!disposed)
{ {
if (disposing) m_packed.Dispose();
{ m_extra.Dispose();
m_packed.Dispose();
m_extra.Dispose();
}
disposed = true; disposed = true;
} }
GC.SuppressFinalize (this);
} }
#endregion #endregion
} }
@ -284,17 +270,15 @@ namespace GameRes.Formats.AdPack
throw new NotImplementedException ("Ed8Format.Write not implemented"); throw new NotImplementedException ("Ed8Format.Write not implemented");
} }
public override ImageMetaData ReadMetaData (Stream stream) public override ImageMetaData ReadMetaData (IBinaryStream stream)
{ {
var header = new byte[0x1a]; var header = stream.ReadHeader (0x1A);
if (header.Length != stream.Read (header, 0, header.Length)) if (!header.AsciiEqual (".8Bit\x8d\x5d\x8c\xcb\x00"))
return null; return null;
if (!Binary.AsciiEqual (header, ".8Bit\x8d\x5d\x8c\xcb\x00")) uint width = header.ToUInt16 (0x0e);
return null; uint height = header.ToUInt16 (0x10);
uint width = LittleEndian.ToUInt16 (header, 0x0e); uint palette_size = header.ToUInt32 (0x12);
uint height = LittleEndian.ToUInt16 (header, 0x10); uint comp_size = header.ToUInt32 (0x16);
uint palette_size = LittleEndian.ToUInt32 (header, 0x12);
uint comp_size = LittleEndian.ToUInt32 (header, 0x16);
if (palette_size > 0x100) if (palette_size > 0x100)
return null; return null;
@ -308,14 +292,11 @@ namespace GameRes.Formats.AdPack
}; };
} }
public override ImageData Read (Stream stream, ImageMetaData info) public override ImageData Read (IBinaryStream stream, ImageMetaData info)
{ {
var meta = info as Ed8MetaData; var meta = (Ed8MetaData)info;
if (null == meta)
throw new ArgumentException ("Ed8Format.Read should be supplied with Ed8MetaData", "info");
stream.Position = 0x1a; stream.Position = 0x1a;
var reader = new Reader (stream, meta); var reader = new Reader (stream.AsStream, meta);
reader.Unpack(); reader.Unpack();
var palette = new BitmapPalette (reader.Palette); var palette = new BitmapPalette (reader.Palette);
return ImageData.Create (info, PixelFormats.Indexed8, palette, reader.Data, (int)info.Width); return ImageData.Create (info, PixelFormats.Indexed8, palette, reader.Data, (int)info.Width);

View File

@ -38,20 +38,18 @@ namespace GameRes.Formats.AdvSys
public override string Description { get { return "AdvSys engine image format"; } } public override string Description { get { return "AdvSys engine image format"; } }
public override uint Signature { get { return 0x5F325247; } } // 'GR2_' public override uint Signature { get { return 0x5F325247; } } // 'GR2_'
public override ImageMetaData ReadMetaData (Stream stream) public override ImageMetaData ReadMetaData (IBinaryStream stream)
{ {
var header = new byte[16]; var header = stream.ReadHeader (0x10);
if (16 != stream.Read (header, 0, 16))
return null;
return new ImageMetaData return new ImageMetaData
{ {
Width = LittleEndian.ToUInt16 (header, 4), Width = header.ToUInt16 (4),
Height = LittleEndian.ToUInt16 (header, 6), Height = header.ToUInt16 (6),
BPP = LittleEndian.ToInt16 (header, 12) * 8 BPP = header.ToInt16 (12) * 8
}; };
} }
public override ImageData Read (Stream stream, ImageMetaData info) public override ImageData Read (IBinaryStream stream, ImageMetaData info)
{ {
stream.Position = 0x10; stream.Position = 0x10;
int stride = ((int)info.Width * info.BPP/8 + 3) & ~3; int stride = ((int)info.Width * info.BPP/8 + 3) & ~3;
@ -92,18 +90,16 @@ namespace GameRes.Formats.AdvSys
Extensions = new string[] { "gr2" }; Extensions = new string[] { "gr2" };
} }
public override ImageMetaData ReadMetaData (Stream stream) public override ImageMetaData ReadMetaData (IBinaryStream stream)
{ {
var header = new byte[20]; var header = stream.ReadHeader (20);
if (20 != stream.Read (header, 0, 20)) if (!header.AsciiEqual ("*Pola* "))
return null; return null;
if (!Binary.AsciiEqual (header, "*Pola* ")) int unpacked_size = header.ToInt32 (8);
return null;
int unpacked_size = LittleEndian.ToInt32 (header, 8);
using (var reader = new PolaReader (stream, 64)) using (var reader = new PolaReader (stream, 64))
{ {
reader.Unpack(); reader.Unpack();
using (var temp = new MemoryStream (reader.Data)) using (var temp = BinaryStream.FromArray (reader.Data, stream.Name))
{ {
var info = base.ReadMetaData (temp); var info = base.ReadMetaData (temp);
if (null == info) if (null == info)
@ -119,17 +115,14 @@ namespace GameRes.Formats.AdvSys
} }
} }
public override ImageData Read (Stream stream, ImageMetaData info) public override ImageData Read (IBinaryStream stream, ImageMetaData info)
{ {
var meta = info as PolaMetaData; var meta = (PolaMetaData)info;
if (null == meta)
throw new ArgumentException ("PolaFormat.Read should be supplied with PolaMetaData", "info");
stream.Position = 0x14; stream.Position = 0x14;
using (var reader = new PolaReader (stream, meta.UnpackedSize)) using (var reader = new PolaReader (stream, meta.UnpackedSize))
{ {
reader.Unpack(); reader.Unpack();
using (var temp = new MemoryStream (reader.Data)) using (var temp = BinaryStream.FromArray (reader.Data, stream.Name))
return base.Read (temp, info); return base.Read (temp, info);
} }
} }
@ -142,14 +135,14 @@ namespace GameRes.Formats.AdvSys
internal sealed class PolaReader : IDisposable internal sealed class PolaReader : IDisposable
{ {
BinaryReader m_input; IBinaryStream m_input;
byte[] m_output; byte[] m_output;
public byte[] Data { get { return m_output; } } public byte[] Data { get { return m_output; } }
public PolaReader (Stream input, int unpacked_size) public PolaReader (IBinaryStream input, int unpacked_size)
{ {
m_input = new ArcView.Reader (input); m_input = input;
m_output = new byte[unpacked_size+2]; m_output = new byte[unpacked_size+2];
} }
@ -161,14 +154,14 @@ namespace GameRes.Formats.AdvSys
{ {
if (0 != NextBit()) if (0 != NextBit())
{ {
m_output[dst++] = m_input.ReadByte(); m_output[dst++] = m_input.ReadUInt8();
continue; continue;
} }
int offset, count = 0; int offset, count = 0;
if (0 != NextBit()) if (0 != NextBit())
{ {
offset = m_input.ReadByte() - 256; offset = m_input.ReadUInt8() - 256;
if (0 == NextBit()) if (0 == NextBit())
{ {
@ -228,7 +221,7 @@ namespace GameRes.Formats.AdvSys
} }
else else
{ {
count = m_input.ReadByte() + 17; count = m_input.ReadUInt8() + 17;
} }
if (dst + count > m_output.Length) if (dst + count > m_output.Length)
count = m_output.Length - dst; count = m_output.Length - dst;
@ -237,7 +230,7 @@ namespace GameRes.Formats.AdvSys
} }
else else
{ {
offset = m_input.ReadByte() - 256; offset = m_input.ReadUInt8() - 256;
if (0 == NextBit()) if (0 == NextBit())
{ {
if (offset != -1) if (offset != -1)
@ -280,14 +273,8 @@ namespace GameRes.Formats.AdvSys
} }
#region IDisposable Members #region IDisposable Members
bool m_disposed = false;
public void Dispose () public void Dispose ()
{ {
if (!m_disposed)
{
m_input.Dispose();
m_disposed = true;
}
} }
#endregion #endregion
} }

View File

@ -48,7 +48,7 @@ namespace GameRes.Formats.AdvSys
var header = stream.ReadHeader (12); var header = stream.ReadHeader (12);
if (header.Length != 12) if (header.Length != 12)
return null; return null;
if (!Binary.AsciiEqual (header, 4, "GWD")) if (!header.AsciiEqual (4, "GWD"))
return null; return null;
return new GwdMetaData return new GwdMetaData
{ {
@ -72,7 +72,7 @@ namespace GameRes.Formats.AdvSys
if (24 == info.BPP && 1 == stream.ReadByte()) if (24 == info.BPP && 1 == stream.ReadByte())
{ {
using (var part = new StreamRegion (stream.AsStream, stream.Position, true)) using (var part = new StreamRegion (stream.AsStream, stream.Position, true))
using (var alpha_stream = new BinaryStream (part)) using (var alpha_stream = new BinaryStream (part, stream.Name))
{ {
var alpha_info = ReadMetaData (alpha_stream) as GwdMetaData; var alpha_info = ReadMetaData (alpha_stream) as GwdMetaData;
if (null != alpha_info && 8 == alpha_info.BPP if (null != alpha_info && 8 == alpha_info.BPP

View File

@ -101,7 +101,7 @@ namespace GameRes.Formats.AliceSoft
for (int i = 0; i < prefix.Length; ++i) for (int i = 0; i < prefix.Length; ++i)
prefix[i] ^= AffKey[i & 0xF]; prefix[i] ^= AffKey[i & 0xF];
if (data_size <= 0x40) if (data_size <= 0x40)
return new MemoryStream (prefix); return new BinMemoryStream (prefix, entry.Name);
var rest = arc.File.CreateStream (entry.Offset+0x10+encrypted_length, data_size-encrypted_length); var rest = arc.File.CreateStream (entry.Offset+0x10+encrypted_length, data_size-encrypted_length);
return new PrefixStream (prefix, rest); return new PrefixStream (prefix, rest);
} }

View File

@ -137,13 +137,15 @@ namespace GameRes.Formats.AliceSoft
long qnt_pos = m_input.Position; long qnt_pos = m_input.Position;
if (m_input.ReadUInt32() != Qnt.Signature) if (m_input.ReadUInt32() != Qnt.Signature)
throw new InvalidFormatException(); throw new InvalidFormatException();
m_input.Seek (-4, SeekOrigin.Current); QntMetaData qnt_info;
var qnt_info = Qnt.ReadMetaData (m_input) as QntMetaData; using (var reg = new StreamRegion (m_input.AsStream, qnt_pos, true))
using (var qnt = new BinaryStream (reg, m_input.Name))
qnt_info = Qnt.ReadMetaData (qnt) as QntMetaData;
if (null == qnt_info) if (null == qnt_info)
throw new InvalidFormatException(); throw new InvalidFormatException();
m_input.Position = qnt_pos + 0x44; m_input.Position = qnt_pos + 0x44;
var overlay = new QntFormat.Reader (m_input, qnt_info); var overlay = new QntFormat.Reader (m_input.AsStream, qnt_info);
overlay.Unpack(); overlay.Unpack();
m_overlay_bpp = overlay.BPP; m_overlay_bpp = overlay.BPP;
if (m_mask != null) if (m_mask != null)
@ -213,13 +215,13 @@ namespace GameRes.Formats.AliceSoft
string dir_name = VFS.GetDirectoryName (m_info.FileName); string dir_name = VFS.GetDirectoryName (m_info.FileName);
string base_name = Path.ChangeExtension (m_info.BaseName, "qnt"); string base_name = Path.ChangeExtension (m_info.BaseName, "qnt");
base_name = VFS.CombinePath (dir_name, base_name); base_name = VFS.CombinePath (dir_name, base_name);
using (var base_file = VFS.OpenSeekableStream (base_name)) using (var base_file = VFS.OpenBinaryStream (base_name))
{ {
var base_info = Qnt.ReadMetaData (base_file) as QntMetaData; var base_info = Qnt.ReadMetaData (base_file) as QntMetaData;
if (null != base_info && m_info.Width == base_info.Width && m_info.Height == base_info.Height) if (null != base_info && m_info.Width == base_info.Width && m_info.Height == base_info.Height)
{ {
base_info.FileName = base_name; base_info.FileName = base_name;
var reader = new QntFormat.Reader (base_file, base_info); var reader = new QntFormat.Reader (base_file.AsStream, base_info);
reader.Unpack(); reader.Unpack();
m_base_bpp = reader.BPP; m_base_bpp = reader.BPP;
m_base = reader.Data; m_base = reader.Data;

View File

@ -49,36 +49,34 @@ namespace GameRes.Formats.AliceSoft
throw new System.NotImplementedException ("QntFormat.Write not implemented"); throw new System.NotImplementedException ("QntFormat.Write not implemented");
} }
public override ImageMetaData ReadMetaData (Stream stream) public override ImageMetaData ReadMetaData (IBinaryStream stream)
{ {
var header = new byte[0x44]; var header = stream.ReadHeader (0x44);
if (header.Length != stream.Read (header, 0, header.Length)) int version = header.ToInt32 (4);
return null;
int version = LittleEndian.ToInt32 (header, 4);
if (version <= 0 || version > 2) if (version <= 0 || version > 2)
return null; return null;
if (0x44 != LittleEndian.ToUInt32 (header, 8)) if (0x44 != header.ToUInt32 (8))
return null; return null;
uint width = LittleEndian.ToUInt32 (header, 0x14); uint width = header.ToUInt32 (0x14);
uint height = LittleEndian.ToUInt32 (header, 0x18); uint height = header.ToUInt32 (0x18);
if (0 == width || 0 == height) if (0 == width || 0 == height)
return null; return null;
return new QntMetaData return new QntMetaData
{ {
Width = width, Width = width,
Height = height, Height = height,
OffsetX = LittleEndian.ToInt32 (header, 0x0c), OffsetX = header.ToInt32 (0x0c),
OffsetY = LittleEndian.ToInt32 (header, 0x10), OffsetY = header.ToInt32 (0x10),
BPP = LittleEndian.ToInt32 (header, 0x1c), BPP = header.ToInt32 (0x1c),
RGBSize = LittleEndian.ToUInt32 (header, 0x24), RGBSize = header.ToUInt32 (0x24),
AlphaSize = LittleEndian.ToUInt32 (header, 0x28), AlphaSize = header.ToUInt32 (0x28),
}; };
} }
public override ImageData Read (Stream stream, ImageMetaData info) public override ImageData Read (IBinaryStream stream, ImageMetaData info)
{ {
stream.Position = 0x44; stream.Position = 0x44;
var reader = new Reader (stream, (QntMetaData)info); var reader = new Reader (stream.AsStream, (QntMetaData)info);
reader.Unpack(); reader.Unpack();
int stride = (int)info.Width * (reader.BPP / 8); int stride = (int)info.Width * (reader.BPP / 8);
PixelFormat format = 24 == reader.BPP ? PixelFormats.Bgr24 : PixelFormats.Bgra32; PixelFormat format = 24 == reader.BPP ? PixelFormats.Bgr24 : PixelFormats.Bgra32;

View File

@ -272,7 +272,7 @@ namespace GameRes.Formats.Amaterasu
uint WriteAmiEntry (PackedEntry entry, Stream output) uint WriteAmiEntry (PackedEntry entry, Stream output)
{ {
uint packed_size = 0; uint packed_size = 0;
using (var input = File.OpenRead (entry.Name)) using (var input = VFS.OpenBinaryStream (entry))
{ {
long file_size = input.Length; long file_size = input.Length;
if (file_size > uint.MaxValue) if (file_size > uint.MaxValue)
@ -284,7 +284,7 @@ namespace GameRes.Formats.Amaterasu
} }
else else
{ {
input.CopyTo (output); input.AsStream.CopyTo (output);
} }
} }
return packed_size; return packed_size;
@ -293,19 +293,19 @@ namespace GameRes.Formats.Amaterasu
static Lazy<GrpFormat> s_grp_format = new Lazy<GrpFormat> (() => static Lazy<GrpFormat> s_grp_format = new Lazy<GrpFormat> (() =>
FormatCatalog.Instance.ImageFormats.OfType<GrpFormat>().FirstOrDefault()); FormatCatalog.Instance.ImageFormats.OfType<GrpFormat>().FirstOrDefault());
uint WriteImageEntry (PackedEntry entry, Stream input, Stream output) uint WriteImageEntry (PackedEntry entry, IBinaryStream input, Stream output)
{ {
var grp = s_grp_format.Value; var grp = s_grp_format.Value;
if (null == grp) // probably never happens if (null == grp) // probably never happens
throw new FileFormatException ("GRP image encoder not available"); throw new FileFormatException ("GRP image encoder not available");
bool is_grp = grp.Signature == FormatCatalog.ReadSignature (input); bool is_grp = grp.Signature == input.Signature;
input.Position = 0; input.Position = 0;
var start = output.Position; var start = output.Position;
using (var zstream = new ZLibStream (output, CompressionMode.Compress, CompressionLevel.Level9, true)) using (var zstream = new ZLibStream (output, CompressionMode.Compress, CompressionLevel.Level9, true))
{ {
if (is_grp) if (is_grp)
{ {
input.CopyTo (zstream); input.AsStream.CopyTo (zstream);
} }
else else
{ {

View File

@ -40,23 +40,19 @@ namespace GameRes.Formats.Amaterasu
public override uint Signature { get { return 0x00505247; } } // 'GRP' public override uint Signature { get { return 0x00505247; } } // 'GRP'
public override bool CanWrite { get { return true; } } public override bool CanWrite { get { return true; } }
public override ImageMetaData ReadMetaData (Stream stream) public override ImageMetaData ReadMetaData (IBinaryStream file)
{ {
using (var file = new BinaryReader (stream, Encoding.ASCII, true)) file.Position = 4;
{ var meta = new ImageMetaData();
if (file.ReadUInt32() != Signature) meta.OffsetX = file.ReadInt16();
return null; meta.OffsetY = file.ReadInt16();
var meta = new ImageMetaData(); meta.Width = file.ReadUInt16();
meta.OffsetX = file.ReadInt16(); meta.Height = file.ReadUInt16();
meta.OffsetY = file.ReadInt16(); meta.BPP = 32;
meta.Width = file.ReadUInt16(); return meta;
meta.Height = file.ReadUInt16();
meta.BPP = 32;
return meta;
}
} }
public override ImageData Read (Stream file, ImageMetaData info) public override ImageData Read (IBinaryStream file, ImageMetaData info)
{ {
int width = (int)info.Width; int width = (int)info.Width;
int height = (int)info.Height; int height = (int)info.Height;

View File

@ -92,7 +92,7 @@ namespace GameRes.Formats.Ags
data[i] ^= key; data[i] ^= key;
key += earc.Key.Increment; key += earc.Key.Increment;
} }
return new MemoryStream (data); return new BinMemoryStream (data, entry.Name);
} }
public static readonly EncryptionScheme DefaultScheme = new EncryptionScheme { public static readonly EncryptionScheme DefaultScheme = new EncryptionScheme {

View File

@ -41,12 +41,12 @@ namespace GameRes.Formats.Ags
Extensions = new string[] { "pcm" }; Extensions = new string[] { "pcm" };
} }
public override SoundInput TryOpen (Stream file) public override SoundInput TryOpen (IBinaryStream file)
{ {
uint signature = FormatCatalog.ReadSignature (file) & 0xF0FFFFFF; uint signature = file.Signature & 0xF0FFFFFF;
if (0x564157 != signature) // 'WAV' if (0x564157 != signature) // 'WAV'
return null; return null;
return new PcmInput (file); return new PcmInput (file.AsStream);
} }
} }

View File

@ -28,10 +28,7 @@ using System.Collections.Generic;
using System.ComponentModel.Composition; using System.ComponentModel.Composition;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Media; using System.Windows.Media;
using System.Windows.Media.Imaging;
using GameRes.Utility; using GameRes.Utility;
namespace GameRes.Formats.Ags namespace GameRes.Formats.Ags
@ -196,40 +193,37 @@ namespace GameRes.Formats.Ags
throw new System.NotImplementedException ("CgFormat.Write not implemented"); throw new System.NotImplementedException ("CgFormat.Write not implemented");
} }
public override ImageMetaData ReadMetaData (Stream stream) public override ImageMetaData ReadMetaData (IBinaryStream file)
{ {
int sig = stream.ReadByte(); int sig = file.ReadByte();
if (sig >= 0x20) if (sig >= 0x20)
return null; return null;
using (var input = new ArcView.Reader (stream)) int width = file.ReadInt16();
int height = file.ReadInt16();
if (width <= 0 || height <= 0 || width > 4096 || height > 4096)
return null;
var meta = new CgMetaData
{ {
int width = input.ReadInt16(); Width = (uint)width,
int height = input.ReadInt16(); Height = (uint)height,
if (width <= 0 || height <= 0 || width > 4096 || height > 4096) BPP = 24,
Type = sig,
};
if (0 != (sig & 7))
{
meta.OffsetX = file.ReadInt16();
meta.OffsetY = file.ReadInt16();
meta.Right = file.ReadInt16();
meta.Bottom = file.ReadInt16();
if (meta.OffsetX > meta.Right || meta.OffsetY > meta.Bottom ||
meta.Right > width || meta.Bottom > height ||
meta.OffsetX < 0 || meta.OffsetY < 0)
return null; return null;
var meta = new CgMetaData
{
Width = (uint)width,
Height = (uint)height,
BPP = 24,
Type = sig,
};
if (0 != (sig & 7))
{
meta.OffsetX = input.ReadInt16();
meta.OffsetY = input.ReadInt16();
meta.Right = input.ReadInt16();
meta.Bottom = input.ReadInt16();
if (meta.OffsetX > meta.Right || meta.OffsetY > meta.Bottom ||
meta.Right > width || meta.Bottom > height ||
meta.OffsetX < 0 || meta.OffsetY < 0)
return null;
}
return meta;
} }
return meta;
} }
public override ImageData Read (Stream stream, ImageMetaData info) public override ImageData Read (IBinaryStream stream, ImageMetaData info)
{ {
var meta = (CgMetaData)info; var meta = (CgMetaData)info;
using (var reader = new Reader (stream, meta)) using (var reader = new Reader (stream, meta))
@ -239,9 +233,9 @@ namespace GameRes.Formats.Ags
} }
} }
internal class Reader : IDisposable internal sealed class Reader : IDisposable
{ {
BinaryReader m_input; IBinaryStream m_input;
byte[] m_output; byte[] m_output;
int m_type; int m_type;
int m_width; int m_width;
@ -253,7 +247,7 @@ namespace GameRes.Formats.Ags
public byte[] Data { get { return m_output; } } public byte[] Data { get { return m_output; } }
public Reader (Stream file, CgMetaData info, byte[] base_image = null) public Reader (IBinaryStream file, CgMetaData info, byte[] base_image = null)
{ {
m_type = info.Type; m_type = info.Type;
m_width = (int)info.Width; m_width = (int)info.Width;
@ -263,13 +257,13 @@ namespace GameRes.Formats.Ags
m_right = info.Right == 0 ? m_width : info.Right; m_right = info.Right == 0 ? m_width : info.Right;
m_bottom = info.Bottom == 0 ? m_height : info.Bottom; m_bottom = info.Bottom == 0 ? m_height : info.Bottom;
m_output = base_image ?? new byte[3*m_width*m_height]; m_output = base_image ?? new byte[3*m_width*m_height];
m_input = new BinaryReader (file, Encoding.ASCII, true); m_input = file;
ShiftTable = InitShiftTable(); ShiftTable = InitShiftTable();
if (0 != (info.Type & 7)) if (0 != (info.Type & 7))
file.Position = 13; m_input.Position = 13;
else else
file.Position = 5; m_input.Position = 5;
} }
static readonly short[] ShiftX = new short[] { // 409b6c static readonly short[] ShiftX = new short[] { // 409b6c
@ -307,7 +301,7 @@ namespace GameRes.Formats.Ags
int dst = left; int dst = left;
while (dst != right) while (dst != right)
{ {
byte v9 = m_input.ReadByte(); byte v9 = m_input.ReadUInt8();
if (0 != (v9 & 0x80)) if (0 != (v9 & 0x80))
{ {
if (0 != (v9 & 0x40)) if (0 != (v9 & 0x40))
@ -318,10 +312,10 @@ namespace GameRes.Formats.Ags
} }
else else
{ {
byte v15 = m_input.ReadByte(); byte v15 = m_input.ReadUInt8();
m_output[dst] = (byte)(((v9 << 1) + (v15 & 1)) << 1); m_output[dst] = (byte)(((v9 << 1) + (v15 & 1)) << 1);
m_output[dst + 1] = (byte)(v15 & 0xfe); m_output[dst + 1] = (byte)(v15 & 0xfe);
m_output[dst + 2] = m_input.ReadByte(); m_output[dst + 2] = m_input.ReadUInt8();
} }
dst += 3; dst += 3;
continue; continue;
@ -330,13 +324,13 @@ namespace GameRes.Formats.Ags
int count = v9 & 0xF; int count = v9 & 0xF;
if (0 == count) if (0 == count)
{ {
count = (int)m_input.ReadByte() + 15; count = (int)m_input.ReadUInt8() + 15;
if (270 == count) if (270 == count)
{ {
int v12; int v12;
do do
{ {
v12 = m_input.ReadByte(); v12 = m_input.ReadUInt8();
count += v12; count += v12;
} }
while (v12 == 0xff); while (v12 == 0xff);
@ -364,7 +358,7 @@ namespace GameRes.Formats.Ags
int dst = left; int dst = left;
while (dst != right) while (dst != right)
{ {
byte v13 = m_input.ReadByte(); byte v13 = m_input.ReadUInt8();
if (0 != (v13 & 0x80)) if (0 != (v13 & 0x80))
{ {
int color = 3 * (v13 & 0x7F); int color = 3 * (v13 & 0x7F);
@ -378,13 +372,13 @@ namespace GameRes.Formats.Ags
int count = v13 & 0xF; int count = v13 & 0xF;
if (0 == count) if (0 == count)
{ {
count = m_input.ReadByte() + 15; count = m_input.ReadUInt8() + 15;
if (270 == count) if (270 == count)
{ {
int v16; int v16;
do do
{ {
v16 = m_input.ReadByte(); v16 = m_input.ReadUInt8();
count += v16; count += v16;
} }
while (v16 == 0xff); while (v16 == 0xff);
@ -403,25 +397,10 @@ namespace GameRes.Formats.Ags
} }
#region IDisposable Members #region IDisposable Members
bool disposed = false;
public void Dispose () public void Dispose ()
{ {
Dispose (true);
GC.SuppressFinalize (this); GC.SuppressFinalize (this);
} }
protected virtual void Dispose (bool disposing)
{
if (!disposed)
{
if (disposing)
{
m_input.Dispose();
}
disposed = true;
}
}
#endregion #endregion
} }
} }

View File

@ -178,7 +178,7 @@ namespace GameRes.Formats.Ankh
{ {
var unpacked = new byte[entry.UnpackedSize]; var unpacked = new byte[entry.UnpackedSize];
reader.UnpackHDJ (unpacked, 0); reader.UnpackHDJ (unpacked, 0);
return new MemoryStream (unpacked); return new BinMemoryStream (unpacked, entry.Name);
} }
} }
@ -201,7 +201,7 @@ namespace GameRes.Formats.Ankh
reader.UnpackA (unpacked, header_size, channels); reader.UnpackA (unpacked, header_size, channels);
else else
reader.UnpackS (unpacked, header_size, channels); reader.UnpackS (unpacked, header_size, channels);
return new MemoryStream (unpacked); return new BinMemoryStream (unpacked, entry.Name);
} }
} }
@ -276,7 +276,7 @@ namespace GameRes.Formats.Ankh
dst += count; dst += count;
} }
} }
return new MemoryStream (output); return new BinMemoryStream (output, entry.Name);
} }
} }
} }

View File

@ -65,7 +65,7 @@ namespace GameRes.Formats.Ice
if (0 == format.AverageBytesPerSecond if (0 == format.AverageBytesPerSecond
|| format.SamplesPerSecond * format.BlockAlign != format.AverageBytesPerSecond) || format.SamplesPerSecond * format.BlockAlign != format.AverageBytesPerSecond)
return null; return null;
var pcm = new StreamRegion (file, 0x16, pcm_size); var pcm = new StreamRegion (file.AsStream, 0x16, pcm_size);
return new RawPcmInput (pcm, format); return new RawPcmInput (pcm, format);
} }
} }

View File

@ -188,7 +188,7 @@ namespace GameRes.Formats.Aoi
{ {
data[i] ^= KeyFromOffset (offset++); data[i] ^= KeyFromOffset (offset++);
} }
return new MemoryStream (data); return new BinMemoryStream (data, entry.Name);
} }
static byte KeyFromOffset (uint offset) static byte KeyFromOffset (uint offset)

View File

@ -36,14 +36,12 @@ namespace GameRes.Formats.Aoi
public override string Description { get { return "Aoi engine audio format"; } } public override string Description { get { return "Aoi engine audio format"; } }
public override uint Signature { get { return 0x4F696F41; } } // 'AoiO' public override uint Signature { get { return 0x4F696F41; } } // 'AoiO'
public override SoundInput TryOpen (Stream file) public override SoundInput TryOpen (IBinaryStream file)
{ {
var header = new byte[0x30]; var header = file.ReadHeader (0x30);
if (header.Length != file.Read (header, 0, header.Length)) if (!header.AsciiEqual (0, "AoiOgg") || !header.AsciiEqual (0x2C, "OggS"))
return null; return null;
if (!Binary.AsciiEqual (header, 0, "AoiOgg") || !Binary.AsciiEqual (header, 0x2C, "OggS")) var ogg = new StreamRegion (file.AsStream, 0x2C);
return null;
var ogg = new StreamRegion (file, 0x2C);
return new OggInput (ogg); return new OggInput (ogg);
} }
} }

View File

@ -118,7 +118,7 @@ namespace GameRes.Formats.Aoi
case 5: case 5:
count = (count >> 8) & 0xFF; count = (count >> 8) & 0xFF;
input.BaseStream.Seek ((count - count / 4) * 4, SeekOrigin.Current); input.Seek ((count - count / 4) * 4, SeekOrigin.Current);
count *= 4; count *= 4;
break; break;
@ -134,9 +134,9 @@ namespace GameRes.Formats.Aoi
var base_name = ReadBaseName (input, meta); var base_name = ReadBaseName (input, meta);
if (VFS.FileExists (base_name)) if (VFS.FileExists (base_name))
{ {
using (var base_file = VFS.OpenSeekableStream (base_name)) using (var base_file = VFS.OpenBinaryStream (base_name))
{ {
var base_image = Read (base_name, base_file); var base_image = Read (base_file);
BlendImage (meta, pixels, base_image.Bitmap); BlendImage (meta, pixels, base_image.Bitmap);
} }
} }
@ -152,13 +152,13 @@ namespace GameRes.Formats.Aoi
string ReadBaseName (IBinaryStream input, AgfMetaData info) string ReadBaseName (IBinaryStream input, AgfMetaData info)
{ {
input.BaseStream.Position = info.DataOffset + info.BaseNameOffset; input.Position = info.DataOffset + info.BaseNameOffset;
using (var reader = new BinaryReader (input.AsStream, Encoding.Unicode, true)) using (var reader = new BinaryReader (input.AsStream, Encoding.Unicode, true))
{ {
var name = new StringBuilder(); var name = new StringBuilder();
for (;;) for (;;)
{ {
char c = input.ReadChar(); char c = reader.ReadChar();
if (0 == c) if (0 == c)
break; break;
name.Append (c); name.Append (c);

View File

@ -125,14 +125,14 @@ namespace GameRes.Formats.AST
var data = arc.File.View.ReadBytes (entry.Offset, entry.Size); var data = arc.File.View.ReadBytes (entry.Offset, entry.Size);
for (int i = 0; i < data.Length; ++i) for (int i = 0; i < data.Length; ++i)
data[i] ^= 0xff; data[i] ^= 0xff;
return new MemoryStream (data); return new BinMemoryStream (data, entry.Name);
} }
return arc.File.CreateStream (entry.Offset, entry.Size); return arc.File.CreateStream (entry.Offset, entry.Size);
} }
using (var input = arc.File.CreateStream (entry.Offset, entry.Size)) using (var input = arc.File.CreateStream (entry.Offset, entry.Size))
{ {
var data = UnpackLzss (input, pent.Size, pent.UnpackedSize); var data = UnpackLzss (input, pent.Size, pent.UnpackedSize);
return new MemoryStream (data); return new BinMemoryStream (data, entry.Name);
} }
} }

View File

@ -91,7 +91,7 @@ namespace GameRes.Formats.AVC
int base_offset = (int)(entry.Offset-arcf.HeaderOffset); int base_offset = (int)(entry.Offset-arcf.HeaderOffset);
for (int i = 0; i < data.Length; ++i) for (int i = 0; i < data.Length; ++i)
data[i] ^= arcf.Key[((base_offset+i)&7)]; data[i] ^= arcf.Key[((base_offset+i)&7)];
return new MemoryStream (data, false); return new BinMemoryStream (data, entry.Name);
} }
internal class AdvReader internal class AdvReader

View File

@ -122,7 +122,7 @@ namespace GameRes.Formats.Ail
{ {
byte[] data = new byte[pentry.UnpackedSize]; byte[] data = new byte[pentry.UnpackedSize];
LzssUnpack (input, data); LzssUnpack (input, data);
return new MemoryStream (data); return new BinMemoryStream (data, entry.Name);
} }
} }

View File

@ -159,7 +159,7 @@ namespace GameRes.Formats.Kogado
var unpacked = new byte[packed_entry.UnpackedSize]; var unpacked = new byte[packed_entry.UnpackedSize];
var mariel = new MarielEncoder(); var mariel = new MarielEncoder();
mariel.Unpack (input, unpacked, unpacked.Length); mariel.Unpack (input, unpacked, unpacked.Length);
return new MemoryStream (unpacked); return new BinMemoryStream (unpacked, entry.Name);
} }
finally finally
{ {

View File

@ -150,7 +150,7 @@ namespace GameRes.Formats.Nexton
var data = arc.File.View.ReadBytes (entry.Offset, entry.Size); var data = arc.File.View.ReadBytes (entry.Offset, entry.Size);
for (int i = 0; i != data.Length; ++i) for (int i = 0; i != data.Length; ++i)
data[i] ^= nxent.Key; data[i] ^= nxent.Key;
return new MemoryStream (data); return new BinMemoryStream (data, entry.Name);
} }
private static string ReadName (ArcView view, long offset, uint size, uint key, Encoding enc) private static string ReadName (ArcView view, long offset, uint size, uint key, Encoding enc)

View File

@ -50,7 +50,7 @@ namespace GameRes.Formats.Neko
/// <summary> /// <summary>
/// Read a directory record from archive index. /// Read a directory record from archive index.
/// </summary> /// </summary>
DirRecord ReadDir (BinaryReader input); DirRecord ReadDir (IBinaryStream input);
/// <summary> /// <summary>
/// Returns offset of an entry that immediately follows specified one. /// Returns offset of an entry that immediately follows specified one.
/// </summary> /// </summary>
@ -65,15 +65,14 @@ namespace GameRes.Formats.Neko
internal sealed class IndexReader : IDisposable internal sealed class IndexReader : IDisposable
{ {
BinaryReader m_input; IBinaryStream m_input;
int m_index_size; int m_index_size;
long m_max_offset; long m_max_offset;
INekoFormat m_format; INekoFormat m_format;
public IndexReader (ArcView file, INekoFormat enc, byte[] index, int index_size) public IndexReader (ArcView file, INekoFormat enc, byte[] index, int index_size)
{ {
var input = new MemoryStream (index, 0, index_size); m_input = new BinMemoryStream (index, 0, index_size, file.Name);
m_input = new BinaryReader (input);
m_index_size = index_size; m_index_size = index_size;
m_max_offset = file.MaxOffset; m_max_offset = file.MaxOffset;
m_format = enc; m_format = enc;
@ -85,7 +84,7 @@ namespace GameRes.Formats.Neko
var files_map = GetNamesMap (KnownFileNames); var files_map = GetNamesMap (KnownFileNames);
var dir = new List<Entry>(); var dir = new List<Entry>();
while (m_input.BaseStream.Position < m_index_size) while (m_input.Position < m_index_size)
{ {
var dir_info = m_format.ReadDir (m_input); var dir_info = m_format.ReadDir (m_input);
string dir_name; string dir_name;
@ -273,7 +272,7 @@ namespace GameRes.Formats.Neko
return arc.File.CreateStream (entry.Offset, entry.Size); return arc.File.CreateStream (entry.Offset, entry.Size);
int length; int length;
var data = ReadBlock (arc.File.View, pak.Decoder, entry.Offset, out length); var data = ReadBlock (arc.File.View, pak.Decoder, entry.Offset, out length);
return new MemoryStream (data, 0, length); return new BinMemoryStream (data, 0, length, entry.Name);
} }
static uint HashFromString (uint seed, byte[] str, int offset, int length) static uint HashFromString (uint seed, byte[] str, int offset, int length)
@ -358,7 +357,7 @@ namespace GameRes.Formats.Neko
return hash; return hash;
} }
public DirRecord ReadDir (BinaryReader input) public DirRecord ReadDir (IBinaryStream input)
{ {
return new DirRecord { return new DirRecord {
Hash = input.ReadUInt32(), Hash = input.ReadUInt32(),
@ -468,7 +467,7 @@ namespace GameRes.Formats.Neko
data = new byte[aligned_size]; data = new byte[aligned_size];
arc.File.View.Read (entry.Offset+12, data, 0, (uint)size); arc.File.View.Read (entry.Offset+12, data, 0, (uint)size);
narc.Decoder.Decrypt (key, data, 0, aligned_size); narc.Decoder.Decrypt (key, data, 0, aligned_size);
return new MemoryStream (data, 0, size); return new BinMemoryStream (data, 0, size, entry.Name);
} }
} }
@ -506,7 +505,7 @@ namespace GameRes.Formats.Neko
return hash; return hash;
} }
public DirRecord ReadDir (BinaryReader input) public DirRecord ReadDir (IBinaryStream input)
{ {
uint hash = input.ReadUInt32(); uint hash = input.ReadUInt32();
int count = input.ReadInt32(); int count = input.ReadInt32();

View File

@ -102,7 +102,7 @@ namespace GameRes.Formats.SystemEpsylon
} }
} }
} }
return new MemoryStream (input); return new BinMemoryStream (input, entry.Name);
} }
} }
} }

View File

@ -76,7 +76,7 @@ namespace GameRes.Formats.Terios
using (var reader = new PandoraCompression (input, unpacked_size)) using (var reader = new PandoraCompression (input, unpacked_size))
{ {
var data = reader.Unpack(); var data = reader.Unpack();
return new MemoryStream (data); return new BinMemoryStream (data, entry.Name);
} }
} }
catch catch

View File

@ -108,7 +108,7 @@ namespace GameRes.Formats.SPack
using (var reader = new PackedReader (packed_entry, input)) using (var reader = new PackedReader (packed_entry, input))
{ {
reader.Unpack(); reader.Unpack();
return new MemoryStream (reader.Data); return new BinMemoryStream (reader.Data, entry.Name);
} }
} }
return input; return input;

View File

@ -86,7 +86,7 @@ namespace GameRes.Formats.Astronauts
{ {
var data = arc.File.View.ReadBytes (entry.Offset, entry.Size); var data = arc.File.View.ReadBytes (entry.Offset, entry.Size);
Decrypt (data, entry.Size); Decrypt (data, entry.Size);
return new MemoryStream (data); return new BinMemoryStream (data, entry.Name);
} }
static void Decrypt (byte[] data, uint length) static void Decrypt (byte[] data, uint length)

View File

@ -94,11 +94,9 @@ namespace GameRes.Formats
public override uint Signature { get { return 0; } } public override uint Signature { get { return 0; } }
public override bool CanWrite { get { return false; } } public override bool CanWrite { get { return false; } }
public override SoundInput TryOpen (Stream file) public override SoundInput TryOpen (IBinaryStream file)
{ {
byte[] header = new byte[10]; var header = file.ReadHeader (10).ToArray();
if (header.Length != file.Read (header, 0, header.Length))
return null;
long start_offset = SkipId3Tag (header); long start_offset = SkipId3Tag (header);
if (0 != start_offset) if (0 != start_offset)
{ {
@ -109,7 +107,7 @@ namespace GameRes.Formats
if (0xff != header[0] || 0xe2 != (header[1] & 0xe6) || 0xf0 == (header[2] & 0xf0)) if (0xff != header[0] || 0xe2 != (header[1] & 0xe6) || 0xf0 == (header[2] & 0xf0))
return null; return null;
file.Position = 0; file.Position = 0;
return new Mp3Input (file); return new Mp3Input (file.AsStream);
} }
long SkipId3Tag (byte[] buffer) long SkipId3Tag (byte[] buffer)

View File

@ -131,9 +131,9 @@ namespace GameRes.Formats
public override uint Signature { get { return 0x5367674f; } } // 'OggS' public override uint Signature { get { return 0x5367674f; } } // 'OggS'
public override bool CanWrite { get { return false; } } public override bool CanWrite { get { return false; } }
public override SoundInput TryOpen (Stream file) public override SoundInput TryOpen (IBinaryStream file)
{ {
return new OggInput (file); return new OggInput (file.AsStream);
} }
public static AudioFormat Instance { get { return s_OggFormat.Value; } } public static AudioFormat Instance { get { return s_OggFormat.Value; } }

View File

@ -67,8 +67,8 @@ namespace GameRes.Formats.Creative
switch (block_type) switch (block_type)
{ {
case 1: case 1:
freq = m_input.ReadByte(); freq = m_input.ReadUInt8();
codec = m_input.ReadByte(); codec = m_input.ReadUInt8();
Copy (block_size-2, pcm); Copy (block_size-2, pcm);
m_format.Channels = 1; m_format.Channels = 1;
m_format.SamplesPerSecond = 1000000u / (256u - freq); m_format.SamplesPerSecond = 1000000u / (256u - freq);
@ -80,16 +80,16 @@ namespace GameRes.Formats.Creative
break; break;
case 8: case 8:
freq = m_input.ReadUInt16(); freq = m_input.ReadUInt16();
codec = m_input.ReadByte(); codec = m_input.ReadUInt8();
m_format.Channels = (ushort)(m_input.ReadByte()+1); m_format.Channels = (ushort)(m_input.ReadUInt8()+1);
m_format.SamplesPerSecond = 256000000u / (Format.Channels * (65536u - freq)); m_format.SamplesPerSecond = 256000000u / (Format.Channels * (65536u - freq));
m_format.BitsPerSample = 8; m_format.BitsPerSample = 8;
format_read = true; format_read = true;
break; break;
case 9: case 9:
m_format.SamplesPerSecond = m_input.ReadUInt32(); m_format.SamplesPerSecond = m_input.ReadUInt32();
m_format.BitsPerSample = m_input.ReadByte(); m_format.BitsPerSample = m_input.ReadUInt8();
m_format.Channels = (ushort)(m_input.ReadByte()+1); m_format.Channels = (ushort)(m_input.ReadUInt8()+1);
codec = m_input.ReadUInt16(); codec = m_input.ReadUInt16();
format_read = true; format_read = true;
m_input.ReadInt32(); m_input.ReadInt32();

View File

@ -45,20 +45,18 @@ namespace GameRes.Formats.Banana
public override string Description { get { return "BANANA Shu-Shu image format"; } } public override string Description { get { return "BANANA Shu-Shu image format"; } }
public override uint Signature { get { return 0; } } public override uint Signature { get { return 0; } }
public override ImageMetaData ReadMetaData (Stream stream) public override ImageMetaData ReadMetaData (IBinaryStream stream)
{ {
var header = new byte[0x24]; var header = stream.ReadHeader (0x24);
if (header.Length != stream.Read (header, 0, header.Length)) if (0 != header.ToInt32 (0x10) || 0 != header.ToInt32 (0x14))
return null; return null;
if (0 != LittleEndian.ToInt32 (header, 0x10) || 0 != LittleEndian.ToInt32 (header, 0x14)) int left = header.ToInt32 (0);
return null; int top = header.ToInt32 (4);
int left = LittleEndian.ToInt32 (header, 0); int right = header.ToInt32 (8);
int top = LittleEndian.ToInt32 (header, 4); int bottom = header.ToInt32 (0xC);
int right = LittleEndian.ToInt32 (header, 8); int back_width = header.ToInt32 (0x18);
int bottom = LittleEndian.ToInt32 (header, 0xC); int back_height = header.ToInt32 (0x1C);
int back_width = LittleEndian.ToInt32 (header, 0x18); uint alpha_channel = header.ToUInt32 (0x20);
int back_height = LittleEndian.ToInt32 (header, 0x1C);
uint alpha_channel = LittleEndian.ToUInt32 (header, 0x20);
int width = right - left; int width = right - left;
int height = bottom - top; int height = bottom - top;
if (left >= back_width || top >= back_height if (left >= back_width || top >= back_height
@ -78,12 +76,12 @@ namespace GameRes.Formats.Banana
}; };
} }
public override ImageData Read (Stream stream, ImageMetaData info) public override ImageData Read (IBinaryStream stream, ImageMetaData info)
{ {
int stride = (int)info.Width * 3; int stride = (int)info.Width * 3;
var pixels = new byte[stride * (int)info.Height]; var pixels = new byte[stride * (int)info.Height];
stream.Position = 0x24; stream.Position = 0x24;
using (var lz = new LzssStream (stream, LzssMode.Decompress, true)) using (var lz = new LzssStream (stream.AsStream, LzssMode.Decompress, true))
{ {
if (pixels.Length != lz.Read (pixels, 0, pixels.Length)) if (pixels.Length != lz.Read (pixels, 0, pixels.Length))
throw new InvalidFormatException(); throw new InvalidFormatException();
@ -104,7 +102,7 @@ namespace GameRes.Formats.Banana
stream.Position = 0x24 + meta.AlphaOffset; stream.Position = 0x24 + meta.AlphaOffset;
var alpha = new byte[meta.BackWidth*meta.BackHeight]; var alpha = new byte[meta.BackWidth*meta.BackHeight];
using (var lz = new LzssStream (stream, LzssMode.Decompress, true)) using (var lz = new LzssStream (stream.AsStream, LzssMode.Decompress, true))
{ {
if (alpha.Length != lz.Read (alpha, 0, alpha.Length)) if (alpha.Length != lz.Read (alpha, 0, alpha.Length))
throw new InvalidFormatException(); throw new InvalidFormatException();

View File

@ -49,36 +49,34 @@ namespace GameRes.Formats.Bishop
public override string Description { get { return "Bishop image format"; } } public override string Description { get { return "Bishop image format"; } }
public override uint Signature { get { return 0x2D535342; } } // 'BSS-' public override uint Signature { get { return 0x2D535342; } } // 'BSS-'
public override ImageMetaData ReadMetaData (Stream stream) public override ImageMetaData ReadMetaData (IBinaryStream stream)
{ {
var header = new byte[0x60]; var header = stream.ReadHeader (0x60);
if (header.Length != stream.Read (header, 0, header.Length))
return null;
int base_offset = 0; int base_offset = 0;
if (Binary.AsciiEqual (header, 0, "BSS-Composition\0")) if (header.AsciiEqual ("BSS-Composition\0"))
base_offset = 0x20; base_offset = 0x20;
if (!Binary.AsciiEqual (header, base_offset, "BSS-Graphics\0")) if (!header.AsciiEqual (base_offset, "BSS-Graphics\0"))
return null; return null;
int type = header[base_offset+0x30]; int type = header[base_offset+0x30];
if (type > 2) if (type > 2)
return null; return null;
return new BsgMetaData return new BsgMetaData
{ {
Width = LittleEndian.ToUInt16 (header, base_offset+0x16), Width = header.ToUInt16 (base_offset+0x16),
Height = LittleEndian.ToUInt16 (header, base_offset+0x18), Height = header.ToUInt16 (base_offset+0x18),
OffsetX = LittleEndian.ToInt16 (header, base_offset+0x20), OffsetX = header.ToInt16 (base_offset+0x20),
OffsetY = LittleEndian.ToInt16 (header, base_offset+0x22), OffsetY = header.ToInt16 (base_offset+0x22),
UnpackedSize = LittleEndian.ToInt32 (header, base_offset+0x12), UnpackedSize = header.ToInt32 (base_offset+0x12),
BPP = 2 == type ? 8 : 32, BPP = 2 == type ? 8 : 32,
ColorMode = type, ColorMode = type,
CompressionMode = header[base_offset+0x31], CompressionMode = header[base_offset+0x31],
DataOffset = LittleEndian.ToInt32 (header, base_offset+0x32)+base_offset, DataOffset = header.ToInt32 (base_offset+0x32)+base_offset,
DataSize = LittleEndian.ToInt32 (header, base_offset+0x36), DataSize = header.ToInt32 (base_offset+0x36),
PaletteOffset = LittleEndian.ToInt32 (header, base_offset+0x3A)+base_offset, PaletteOffset = header.ToInt32 (base_offset+0x3A)+base_offset,
}; };
} }
public override ImageData Read (Stream stream, ImageMetaData info) public override ImageData Read (IBinaryStream stream, ImageMetaData info)
{ {
var meta = (BsgMetaData)info; var meta = (BsgMetaData)info;
using (var reader = new BsgReader (stream, meta)) using (var reader = new BsgReader (stream, meta))
@ -96,7 +94,7 @@ namespace GameRes.Formats.Bishop
internal sealed class BsgReader : IDisposable internal sealed class BsgReader : IDisposable
{ {
BinaryReader m_input; IBinaryStream m_input;
BsgMetaData m_info; BsgMetaData m_info;
byte[] m_output; byte[] m_output;
@ -105,13 +103,13 @@ namespace GameRes.Formats.Bishop
public BitmapPalette Palette { get; private set; } public BitmapPalette Palette { get; private set; }
public int Stride { get; private set; } public int Stride { get; private set; }
public BsgReader (Stream input, BsgMetaData info) public BsgReader (IBinaryStream input, BsgMetaData info)
{ {
m_info = info; m_info = info;
if (m_info.CompressionMode > 2) if (m_info.CompressionMode > 2)
throw new NotSupportedException ("Not supported BSS Graphics compression"); throw new NotSupportedException ("Not supported BSS Graphics compression");
m_input = new ArcView.Reader (input); m_input = input;
m_output = new byte[m_info.UnpackedSize]; m_output = new byte[m_info.UnpackedSize];
switch (m_info.ColorMode) switch (m_info.ColorMode)
{ {
@ -133,7 +131,7 @@ namespace GameRes.Formats.Bishop
public void Unpack () public void Unpack ()
{ {
m_input.BaseStream.Position = m_info.DataOffset; m_input.Position = m_info.DataOffset;
if (0 == m_info.CompressionMode) if (0 == m_info.CompressionMode)
{ {
if (1 == m_info.ColorMode) if (1 == m_info.ColorMode)
@ -179,13 +177,13 @@ namespace GameRes.Formats.Bishop
int remaining = m_input.ReadInt32(); int remaining = m_input.ReadInt32();
while (remaining > 0) while (remaining > 0)
{ {
int count = m_input.ReadSByte(); int count = m_input.ReadInt8();
--remaining; --remaining;
if (count >= 0) if (count >= 0)
{ {
for (int i = 0; i <= count; ++i) for (int i = 0; i <= count; ++i)
{ {
m_output[dst] = m_input.ReadByte(); m_output[dst] = m_input.ReadUInt8();
--remaining; --remaining;
dst += pixel_size; dst += pixel_size;
} }
@ -193,7 +191,7 @@ namespace GameRes.Formats.Bishop
else else
{ {
count = 1 - count; count = 1 - count;
byte repeat = m_input.ReadByte(); byte repeat = m_input.ReadUInt8();
--remaining; --remaining;
for (int i = 0; i < count; ++i) for (int i = 0; i < count; ++i)
{ {
@ -207,20 +205,20 @@ namespace GameRes.Formats.Bishop
void UnpackLz (int plane, int pixel_size) void UnpackLz (int plane, int pixel_size)
{ {
int dst = plane; int dst = plane;
byte control = m_input.ReadByte(); byte control = m_input.ReadUInt8();
int remaining = m_input.ReadInt32() - 5; int remaining = m_input.ReadInt32() - 5;
while (remaining > 0) while (remaining > 0)
{ {
byte c = m_input.ReadByte(); byte c = m_input.ReadUInt8();
--remaining; --remaining;
if (c == control) if (c == control)
{ {
int offset = m_input.ReadByte(); int offset = m_input.ReadUInt8();
--remaining; --remaining;
if (offset != control) if (offset != control)
{ {
int count = m_input.ReadByte(); int count = m_input.ReadUInt8();
--remaining; --remaining;
if (offset > control) if (offset > control)
@ -245,7 +243,7 @@ namespace GameRes.Formats.Bishop
BitmapPalette ReadPalette () BitmapPalette ReadPalette ()
{ {
m_input.BaseStream.Position = m_info.PaletteOffset; m_input.Position = m_info.PaletteOffset;
var palette_data = new byte[0x400]; var palette_data = new byte[0x400];
if (palette_data.Length != m_input.Read (palette_data, 0, palette_data.Length)) if (palette_data.Length != m_input.Read (palette_data, 0, palette_data.Length))
throw new InvalidFormatException(); throw new InvalidFormatException();
@ -259,14 +257,8 @@ namespace GameRes.Formats.Bishop
} }
#region IDisposable Members #region IDisposable Members
bool _disposed = false;
public void Dispose () public void Dispose ()
{ {
if (!_disposed)
{
m_input.Dispose();
_disposed = true;
}
} }
#endregion #endregion
} }

View File

@ -44,7 +44,7 @@ namespace GameRes.Formats.BlackCyc
Extensions = new string[] { "vaw", "wgq" }; Extensions = new string[] { "vaw", "wgq" };
} }
public override SoundInput TryOpen (Stream file) public override SoundInput TryOpen (IBinaryStream file)
{ {
var header = ResourceHeader.Read (file); var header = ResourceHeader.Read (file);
if (null == header) if (null == header)
@ -76,8 +76,8 @@ namespace GameRes.Formats.BlackCyc
} }
else else
return null; return null;
var input = new StreamRegion (file, offset, file.Length-offset); var input = new StreamRegion (file.AsStream, offset, file.Length-offset);
return format.TryOpen (input); return format.TryOpen (new BinaryStream (input, file.Name));
} }
public override void Write (SoundInput source, Stream output) public override void Write (SoundInput source, Stream output)
@ -85,7 +85,7 @@ namespace GameRes.Formats.BlackCyc
throw new System.NotImplementedException ("EdimFormat.Write not implemenented"); throw new System.NotImplementedException ("EdimFormat.Write not implemenented");
} }
SoundInput Unpack (Stream input) SoundInput Unpack (IBinaryStream input)
{ {
input.Position = 0x40; input.Position = 0x40;
var header = new byte[0x24]; var header = new byte[0x24];
@ -106,7 +106,7 @@ namespace GameRes.Formats.BlackCyc
{ {
pcm.Write (header, 0, header_size); pcm.Write (header, 0, header_size);
using (var output = new BinaryWriter (pcm, Encoding.Default, true)) using (var output = new BinaryWriter (pcm, Encoding.Default, true))
using (var bits = new LsbBitStream (input, true)) using (var bits = new LsbBitStream (input.AsStream, true))
{ {
int written = 0; int written = 0;
short sample = 0; short sample = 0;
@ -128,7 +128,7 @@ namespace GameRes.Formats.BlackCyc
} }
} }
pcm.Position = 0; pcm.Position = 0;
var sound = Wav.TryOpen (pcm); var sound = Wav.TryOpen (new BinMemoryStream (pcm, input.Name));
if (sound != null) if (sound != null)
input.Dispose(); input.Dispose();
else else

View File

@ -43,11 +43,9 @@ namespace GameRes.Formats.BlackCyc
public int PackType { get; private set; } public int PackType { get; private set; }
public bool AType { get; private set; } public bool AType { get; private set; }
public static ResourceHeader Read (Stream file) public static ResourceHeader Read (IBinaryStream file)
{ {
var header = new byte[0x40]; var header = file.ReadHeader (0x40).ToArray();
if (0x40 != file.Read (header, 0, 0x40))
return null;
var header_string = Encoding.ASCII.GetString (header, 0x30, 0x10); var header_string = Encoding.ASCII.GetString (header, 0x30, 0x10);
var match = PackTypeRe.Match (header_string); var match = PackTypeRe.Match (header_string);
@ -88,9 +86,9 @@ namespace GameRes.Formats.BlackCyc
}; };
} }
public override ImageMetaData ReadMetaData (Stream stream) public override ImageMetaData ReadMetaData (IBinaryStream file)
{ {
var header = ResourceHeader.Read (stream); var header = ResourceHeader.Read (file);
if (null == header) if (null == header)
return null; return null;
if (Binary.AsciiEqual (header.Bytes, "IF PACKTYPE==")) if (Binary.AsciiEqual (header.Bytes, "IF PACKTYPE=="))
@ -99,7 +97,8 @@ namespace GameRes.Formats.BlackCyc
!Binary.AsciiEqual (header.Bytes, 0x2C, "BMP ") || !Binary.AsciiEqual (header.Bytes, 0x2C, "BMP ") ||
header.PackType != 0 && header.PackType != 1) header.PackType != 0 && header.PackType != 1)
return null; return null;
using (var bmp = new StreamRegion (stream, 0x40, true)) using (var reg = new StreamRegion (file.AsStream, 0x40, true))
using (var bmp = new BinaryStream (reg, file.Name))
{ {
var info = Bmp.ReadMetaData (bmp); var info = Bmp.ReadMetaData (bmp);
if (null == info) if (null == info)
@ -110,7 +109,7 @@ namespace GameRes.Formats.BlackCyc
Height = info.Height, Height = info.Height,
BPP = info.BPP, BPP = info.BPP,
BaseType = "BMP", BaseType = "BMP",
PackedSize = (int)(stream.Length-0x40), PackedSize = (int)(file.Length-0x40),
PackType = header.PackType, PackType = header.PackType,
HasAlpha = header.AType, HasAlpha = header.AType,
}; };
@ -122,7 +121,7 @@ namespace GameRes.Formats.BlackCyc
case 0: // BMP case 0: // BMP
case 5: // JPEG case 5: // JPEG
case 8: // PNG case 8: // PNG
packed_size = (int)(stream.Length-0x40); packed_size = (int)(file.Length-0x40);
break; break;
case 2: // BMP+MASK case 2: // BMP+MASK
@ -146,12 +145,13 @@ namespace GameRes.Formats.BlackCyc
}; };
} }
public override ImageData Read (Stream stream, ImageMetaData info) public override ImageData Read (IBinaryStream stream, ImageMetaData info)
{ {
var meta = (DwqMetaData)info; var meta = (DwqMetaData)info;
BitmapSource bitmap = null; BitmapSource bitmap = null;
using (var input = new StreamRegion (stream, 0x40, meta.PackedSize, true)) using (var sreg = new StreamRegion (stream.AsStream, 0x40, meta.PackedSize, true))
using (var input = new BinaryStream (sreg, stream.Name))
{ {
switch (meta.PackType) switch (meta.PackType)
{ {
@ -192,7 +192,7 @@ namespace GameRes.Formats.BlackCyc
int mask_offset = 0x40+meta.PackedSize; int mask_offset = 0x40+meta.PackedSize;
if (mask_offset != stream.Length) if (mask_offset != stream.Length)
{ {
using (var mask = new StreamRegion (stream, mask_offset, true)) using (var mask = new StreamRegion (stream.AsStream, mask_offset, true))
{ {
var reader = new DwqBmpReader (mask, meta); var reader = new DwqBmpReader (mask, meta);
if (8 == reader.Format.BitsPerPixel) // mask should be represented as 8bpp bitmap if (8 == reader.Format.BitsPerPixel) // mask should be represented as 8bpp bitmap
@ -238,17 +238,15 @@ namespace GameRes.Formats.BlackCyc
PixelFormats.Bgra32, null, pixels, stride); PixelFormats.Bgra32, null, pixels, stride);
} }
private BitmapSource ReadFuckedUpBmpImage (Stream file, ImageMetaData info) private BitmapSource ReadFuckedUpBmpImage (IBinaryStream file, ImageMetaData info)
{ {
var header = new byte[0x36]; var header = file.ReadHeader (0x36);
if (header.Length != file.Read (header, 0, header.Length)) int w = header.ToInt32 (0x12);
throw new InvalidFormatException(); int h = header.ToInt32 (0x16);
int w = LittleEndian.ToInt32 (header, 0x12);
int h = LittleEndian.ToInt32 (header, 0x16);
if (w != info.Width || h != info.Height) if (w != info.Width || h != info.Height)
throw new InvalidFormatException(); throw new InvalidFormatException();
int bpp = LittleEndian.ToUInt16 (header, 0x1c); int bpp = header.ToUInt16 (0x1c);
PixelFormat format; PixelFormat format;
switch (bpp) switch (bpp)
{ {
@ -261,8 +259,8 @@ namespace GameRes.Formats.BlackCyc
BitmapPalette palette = null; BitmapPalette palette = null;
if (8 == bpp) if (8 == bpp)
{ {
int colors = Math.Min (LittleEndian.ToInt32 (header, 0x2E), 0x100); int colors = Math.Min (header.ToInt32 (0x2E), 0x100);
palette = DwqBmpReader.ReadPalette (file, colors); palette = DwqBmpReader.ReadPalette (file.AsStream, colors);
} }
int pixel_size = bpp / 8; int pixel_size = bpp / 8;
int stride = ((int)info.Width * pixel_size + 3) & ~3; int stride = ((int)info.Width * pixel_size + 3) & ~3;

View File

@ -51,31 +51,25 @@ namespace GameRes.Formats.BlackRainbow
public override uint Signature { get { return 0x444d425fu; } } // '_BMD' public override uint Signature { get { return 0x444d425fu; } } // '_BMD'
public override bool CanWrite { get { return true; } } public override bool CanWrite { get { return true; } }
public override ImageMetaData ReadMetaData (Stream stream) public override ImageMetaData ReadMetaData (IBinaryStream stream)
{ {
var header = new byte[0x14]; var header = stream.ReadHeader (0x14);
if (header.Length != stream.Read (header, 0, header.Length))
return null;
return new BmdMetaData return new BmdMetaData
{ {
Width = LittleEndian.ToUInt32 (header, 8), Width = header.ToUInt32 (8),
Height = LittleEndian.ToUInt32 (header, 12), Height = header.ToUInt32 (12),
BPP = 32, BPP = 32,
PackedSize = LittleEndian.ToUInt32 (header, 4), PackedSize = header.ToUInt32 (4),
Flag = LittleEndian.ToInt32 (header, 0x10), Flag = header.ToInt32 (0x10),
}; };
} }
public override ImageData Read (Stream stream, ImageMetaData info) public override ImageData Read (IBinaryStream stream, ImageMetaData info)
{ {
var meta = info as BmdMetaData; var meta = (BmdMetaData)info;
if (null == meta)
throw new ArgumentException ("BmdFormat.Read should be supplied with BmdMetaData", "info");
stream.Position = 0x14; stream.Position = 0x14;
int image_size = (int)(meta.Width*meta.Height*4); int image_size = (int)(meta.Width*meta.Height*4);
using (var reader = new LzssReader (stream, (int)meta.PackedSize, image_size)) using (var reader = new LzssReader (stream.AsStream, (int)meta.PackedSize, image_size))
{ {
PixelFormat format = meta.Flag != 0 ? PixelFormats.Bgra32 : PixelFormats.Bgr32; PixelFormat format = meta.Flag != 0 ? PixelFormats.Bgra32 : PixelFormats.Bgr32;
reader.Unpack(); reader.Unpack();

View File

@ -55,21 +55,21 @@ namespace GameRes.Formats.BlackRainbow
} }
} }
public override ImageMetaData ReadMetaData (Stream stream) public override ImageMetaData ReadMetaData (IBinaryStream file)
{ {
var header = new byte[8]; var header = file.ReadHeader (8);
if (header.Length != stream.Read (header, 0, header.Length)) using (var zstream = new ZLibStream (file.AsStream, CompressionMode.Decompress, true))
return null; using (var bmp = new BinaryStream (zstream, file.Name))
using (var zstream = new ZLibStream (stream, CompressionMode.Decompress, true)) return base.ReadMetaData (bmp);
return base.ReadMetaData (zstream);
} }
public override ImageData Read (Stream stream, ImageMetaData info) public override ImageData Read (IBinaryStream file, ImageMetaData info)
{ {
stream.Seek (8, SeekOrigin.Current); file.Seek (8, SeekOrigin.Current);
using (var zstream = new ZLibStream (stream, CompressionMode.Decompress, true)) using (var zstream = new ZLibStream (file.AsStream, CompressionMode.Decompress, true))
using (var input = new SeekableStream (zstream)) using (var input = new SeekableStream (zstream))
return base.Read (input, info); using (var bmp = new BinaryStream (input, file.Name))
return base.Read (bmp, info);
} }
} }
} }

View File

@ -43,48 +43,40 @@ namespace GameRes.Formats.BlueGale
public override string Description { get { return "BlueGale compressed image format"; } } public override string Description { get { return "BlueGale compressed image format"; } }
public override uint Signature { get { return 0x5F706D61; } } // 'amp_' public override uint Signature { get { return 0x5F706D61; } } // 'amp_'
public ZbmFormat () public override ImageMetaData ReadMetaData (IBinaryStream stream)
{
Extensions = new string[] { "zbm" };
}
public override ImageMetaData ReadMetaData (Stream stream)
{ {
stream.Position = 4; stream.Position = 4;
using (var reader = new ArcView.Reader (stream)) int version = stream.ReadInt16();
if (version != 1)
return null;
int unpacked_size = stream.ReadInt32();
int data_offset = stream.ReadInt32();
if (unpacked_size < 0x36 || data_offset < stream.Position)
return null;
var header = new byte[0x20];
stream.Position = data_offset;
Unpack (stream.AsStream, header);
Decrypt (header);
if ('B' != header[0] || 'M' != header[1])
return null;
return new ZbmMetaData
{ {
int version = reader.ReadInt16(); Width = LittleEndian.ToUInt32 (header, 0x12),
if (version != 1) Height = LittleEndian.ToUInt32 (header, 0x16),
return null; BPP = LittleEndian.ToInt16 (header, 0x1C),
int unpacked_size = reader.ReadInt32(); UnpackedSize = unpacked_size,
int data_offset = reader.ReadInt32(); DataOffset = data_offset,
if (unpacked_size < 0x36 || data_offset < stream.Position) };
return null;
var header = new byte[0x20];
stream.Position = data_offset;
Unpack (stream, header);
Decrypt (header);
if ('B' != header[0] || 'M' != header[1])
return null;
return new ZbmMetaData
{
Width = LittleEndian.ToUInt32 (header, 0x12),
Height = LittleEndian.ToUInt32 (header, 0x16),
BPP = LittleEndian.ToInt16 (header, 0x1C),
UnpackedSize = unpacked_size,
DataOffset = data_offset,
};
}
} }
public override ImageData Read (Stream stream, ImageMetaData info) public override ImageData Read (IBinaryStream stream, ImageMetaData info)
{ {
var meta = (ZbmMetaData)info; var meta = (ZbmMetaData)info;
var data = new byte[meta.UnpackedSize]; var data = new byte[meta.UnpackedSize];
stream.Position = meta.DataOffset; stream.Position = meta.DataOffset;
Unpack (stream, data); Unpack (stream.AsStream, data);
Decrypt (data); Decrypt (data);
using (var bmp = new MemoryStream (data)) using (var bmp = new BinMemoryStream (data, stream.Name))
return Bmp.Read (bmp, info); return Bmp.Read (bmp, info);
} }

View File

@ -90,7 +90,7 @@ namespace GameRes.Formats.BlueGale
LittleEndian.Pack (pent.UnpackedSize, output, 2); LittleEndian.Pack (pent.UnpackedSize, output, 2);
int header_size = LittleEndian.ToInt32 (output, 0xE); int header_size = LittleEndian.ToInt32 (output, 0xE);
LittleEndian.Pack (header_size+0xE, output, 0xA); LittleEndian.Pack (header_size+0xE, output, 0xA);
return new MemoryStream (output); return new BinMemoryStream (output, entry.Name);
} }
} }
} }

View File

@ -102,7 +102,7 @@ namespace GameRes.Formats.Bruns
if (meta.Compressed) if (meta.Compressed)
input = new ZLibStream (input, CompressionMode.Decompress); input = new ZLibStream (input, CompressionMode.Decompress);
using (var bin = new BinaryStream (input, stream.Name, true)) using (var bin = new BinaryStream (input, stream.Name, true))
return meta.Format.Read (input, meta.Info); return meta.Format.Read (bin, meta.Info);
} }
finally finally
{ {

View File

@ -200,7 +200,7 @@ namespace GameRes.Formats.CaramelBox
using (input) using (input)
{ {
var data = UnpackLze (input, a3ent.UnpackedSize); var data = UnpackLze (input, a3ent.UnpackedSize);
return new MemoryStream (data); return new BinMemoryStream (data, entry.Name);
} }
} }

View File

@ -130,7 +130,7 @@ namespace GameRes.Formats.CaramelBox
return input; return input;
using (input) using (input)
using (var tz = new TzCompression (input)) using (var tz = new TzCompression (input))
return new MemoryStream (tz.Unpack()); return new BinMemoryStream (tz.Unpack(), entry.Name);
} }
static int ReadInt24 (byte[] data, int pos) static int ReadInt24 (byte[] data, int pos)

View File

@ -208,7 +208,7 @@ namespace GameRes.Formats.CatSystem
{ {
byte[] data = arc.File.View.ReadBytes (entry.Offset, entry.Size); byte[] data = arc.File.View.ReadBytes (entry.Offset, entry.Size);
arc.Encryption.Decipher (data, data.Length/8*8); arc.Encryption.Decipher (data, data.Length/8*8);
return new MemoryStream (data); return new BinMemoryStream (data, entry.Name);
} }
public override Stream OpenEntry (ArcFile arc, Entry entry) public override Stream OpenEntry (ArcFile arc, Entry entry)

View File

@ -44,37 +44,34 @@ namespace GameRes.Formats.CatSystem
public override string Description { get { return "CatSystem engine image format"; } } public override string Description { get { return "CatSystem engine image format"; } }
public override uint Signature { get { return 0x322D4748; } } // 'HG-2' public override uint Signature { get { return 0x322D4748; } } // 'HG-2'
public override ImageMetaData ReadMetaData (Stream stream) public override ImageMetaData ReadMetaData (IBinaryStream stream)
{ {
stream.Position = 8; stream.Position = 8;
using (var header = new ArcView.Reader (stream)) var info = new Hg2MetaData();
{ int type = stream.ReadInt32();
var info = new Hg2MetaData(); if (0x25 == type)
int type = header.ReadInt32(); info.HeaderSize = 0x58;
if (0x25 == type) else if (0x20 == type)
info.HeaderSize = 0x58; info.HeaderSize = 0x50;
else if (0x20 == type) else
info.HeaderSize = 0x50; return null;
else info.Width = stream.ReadUInt32();
return null; info.Height = stream.ReadUInt32();
info.Width = header.ReadUInt32(); info.BPP = stream.ReadInt32();
info.Height = header.ReadUInt32(); stream.Seek (8, SeekOrigin.Current);
info.BPP = header.ReadInt32(); info.DataPacked = stream.ReadInt32();
header.BaseStream.Seek (8, SeekOrigin.Current); info.DataUnpacked = stream.ReadInt32();
info.DataPacked = header.ReadInt32(); info.CtlPacked = stream.ReadInt32();
info.DataUnpacked = header.ReadInt32(); info.CtlUnpacked = stream.ReadInt32();
info.CtlPacked = header.ReadInt32(); stream.Seek (8, SeekOrigin.Current);
info.CtlUnpacked = header.ReadInt32(); info.CanvasWidth = stream.ReadUInt32();
header.BaseStream.Seek (8, SeekOrigin.Current); info.CanvasHeight = stream.ReadUInt32();
info.CanvasWidth = header.ReadUInt32(); info.OffsetX = stream.ReadInt32();
info.CanvasHeight = header.ReadUInt32(); info.OffsetY = stream.ReadInt32();
info.OffsetX = header.ReadInt32(); return info;
info.OffsetY = header.ReadInt32();
return info;
}
} }
public override ImageData Read (Stream stream, ImageMetaData info) public override ImageData Read (IBinaryStream stream, ImageMetaData info)
{ {
using (var reader = new Hg2Reader (stream, (Hg2MetaData)info)) using (var reader = new Hg2Reader (stream, (Hg2MetaData)info))
{ {
@ -94,7 +91,7 @@ namespace GameRes.Formats.CatSystem
{ {
Hg2MetaData m_hg2; Hg2MetaData m_hg2;
public Hg2Reader (Stream input, Hg2MetaData info) : base (input, info) public Hg2Reader (IBinaryStream input, Hg2MetaData info) : base (input, info)
{ {
m_hg2 = info; m_hg2 = info;
} }

View File

@ -25,7 +25,6 @@
using System; using System;
using System.IO; using System.IO;
using System.Linq;
using System.ComponentModel.Composition; using System.ComponentModel.Composition;
using System.Windows.Media.Imaging; using System.Windows.Media.Imaging;
using System.Windows.Media; using System.Windows.Media;
@ -48,35 +47,34 @@ namespace GameRes.Formats.CatSystem
public override string Description { get { return "CatSystem engine image format"; } } public override string Description { get { return "CatSystem engine image format"; } }
public override uint Signature { get { return 0x332d4748; } } // 'HG-3' public override uint Signature { get { return 0x332d4748; } } // 'HG-3'
public override ImageMetaData ReadMetaData (Stream stream) public override ImageMetaData ReadMetaData (IBinaryStream stream)
{ {
var header = new byte[0x4c]; var header = stream.ReadHeader (0x4c);
if (0x4c != stream.Read (header, 0, header.Length)) if (header.ToUInt32 (4) != 0x0c)
return null; return null;
if (LittleEndian.ToUInt32 (header, 4) != 0x0c) if (!header.AsciiEqual (0x14, "stdinfo\0"))
return null;
if (!Binary.AsciiEqual (header, 0x14, "stdinfo\0"))
return null; return null;
return new HgMetaData return new HgMetaData
{ {
HeaderSize = LittleEndian.ToUInt32 (header, 0x1C), HeaderSize = header.ToUInt32 (0x1C),
Width = LittleEndian.ToUInt32 (header, 0x24), Width = header.ToUInt32 (0x24),
Height = LittleEndian.ToUInt32 (header, 0x28), Height = header.ToUInt32 (0x28),
OffsetX = LittleEndian.ToInt32 (header, 0x30), OffsetX = header.ToInt32 (0x30),
OffsetY = LittleEndian.ToInt32 (header, 0x34), OffsetY = header.ToInt32 (0x34),
BPP = LittleEndian.ToInt32 (header, 0x2C), BPP = header.ToInt32 (0x2C),
CanvasWidth = LittleEndian.ToUInt32 (header, 0x44), CanvasWidth = header.ToUInt32 (0x44),
CanvasHeight = LittleEndian.ToUInt32 (header, 0x48), CanvasHeight = header.ToUInt32 (0x48),
}; };
} }
public override ImageData Read (Stream stream, ImageMetaData info) public override ImageData Read (IBinaryStream stream, ImageMetaData info)
{ {
var meta = (HgMetaData)info; var meta = (HgMetaData)info;
if (0x20 != meta.BPP) if (0x20 != meta.BPP)
throw new NotSupportedException ("Not supported HG-3 color depth"); throw new NotSupportedException ("Not supported HG-3 color depth");
using (var input = new StreamRegion (stream, 0x14, true)) using (var reg = new StreamRegion (stream.AsStream, 0x14, true))
using (var input = new BinaryStream (reg, stream.Name))
using (var reader = new Hg3Reader (input, meta)) using (var reader = new Hg3Reader (input, meta))
{ {
var pixels = reader.Unpack(); var pixels = reader.Unpack();
@ -95,17 +93,17 @@ namespace GameRes.Formats.CatSystem
internal class HgReader : IDisposable internal class HgReader : IDisposable
{ {
private BinaryReader m_input; private IBinaryStream m_input;
protected HgMetaData m_info; protected HgMetaData m_info;
protected int m_pixel_size; protected int m_pixel_size;
protected BinaryReader Input { get { return m_input; } } protected IBinaryStream Input { get { return m_input; } }
protected Stream InputStream { get { return m_input.BaseStream; } } protected Stream InputStream { get { return m_input.AsStream; } }
public int Stride { get; protected set; } public int Stride { get; protected set; }
protected HgReader (Stream input, HgMetaData info) protected HgReader (IBinaryStream input, HgMetaData info)
{ {
m_input = new ArcView.Reader (input); m_input = input;
m_info = info; m_info = info;
m_pixel_size = m_info.BPP / 8; m_pixel_size = m_info.BPP / 8;
Stride = (int)m_info.Width * m_pixel_size; Stride = (int)m_info.Width * m_pixel_size;
@ -228,25 +226,10 @@ namespace GameRes.Formats.CatSystem
} }
#region IDisposable Members #region IDisposable Members
bool _disposed = false;
public void Dispose () public void Dispose ()
{ {
Dispose (true);
GC.SuppressFinalize (this); GC.SuppressFinalize (this);
} }
protected virtual void Dispose (bool disposing)
{
if (!_disposed)
{
if (disposing)
{
m_input.Dispose();
}
_disposed = true;
}
}
#endregion #endregion
} }
@ -254,17 +237,17 @@ namespace GameRes.Formats.CatSystem
{ {
public bool Flipped { get; private set; } public bool Flipped { get; private set; }
public Hg3Reader (Stream input, HgMetaData info) : base (input, info) public Hg3Reader (IBinaryStream input, HgMetaData info) : base (input, info)
{ {
} }
public byte[] Unpack () public byte[] Unpack ()
{ {
InputStream.Position = m_info.HeaderSize; InputStream.Position = m_info.HeaderSize;
var img_type = Input.ReadChars (8); var img_type = Input.ReadBytes (8);
if (img_type.SequenceEqual ("img0000\0")) if (Binary.AsciiEqual (img_type, "img0000\0"))
return UnpackImg0000(); return UnpackImg0000();
else if (img_type.SequenceEqual ("img_jpg\0")) else if (Binary.AsciiEqual (img_type, "img_jpg\0"))
return UnpackJpeg(); return UnpackJpeg();
else else
throw new NotSupportedException ("Not supported HG-3 image"); throw new NotSupportedException ("Not supported HG-3 image");
@ -309,11 +292,11 @@ namespace GameRes.Formats.CatSystem
src += src_pixel_size; src += src_pixel_size;
} }
InputStream.Position = next_section; Input.Position = next_section;
var section_header = Input.ReadChars (8); var section_header = Input.ReadBytes (8);
if (!section_header.SequenceEqual ("img_al\0\0")) if (!Binary.AsciiEqual (section_header, "img_al\0\0"))
return output; return output;
InputStream.Seek (8, SeekOrigin.Current); Input.Seek (8, SeekOrigin.Current);
int alpha_size = Input.ReadInt32(); int alpha_size = Input.ReadInt32();
using (var alpha_in = new StreamRegion (InputStream, InputStream.Position+4, alpha_size, true)) using (var alpha_in = new StreamRegion (InputStream, InputStream.Position+4, alpha_size, true))
using (var alpha = new ZLibStream (alpha_in, CompressionMode.Decompress)) using (var alpha = new ZLibStream (alpha_in, CompressionMode.Decompress))

View File

@ -110,7 +110,7 @@ namespace GameRes.Formats.Cherry
{ {
data[text_offset+i] ^= (byte)i; data[text_offset+i] ^= (byte)i;
} }
return new MemoryStream (data); return new BinMemoryStream (data, entry.Name);
} }
} }
@ -209,7 +209,7 @@ namespace GameRes.Formats.Cherry
} }
Decrypt (data, 0x18, (int)(data.Length - 0x18)); Decrypt (data, 0x18, (int)(data.Length - 0x18));
} }
return new MemoryStream (data); return new BinMemoryStream (data, entry.Name);
} }
} }
} }

View File

@ -54,16 +54,14 @@ namespace GameRes.Formats.Cherry
Extensions = new string[] { "grp" }; Extensions = new string[] { "grp" };
} }
public override ImageMetaData ReadMetaData (Stream stream) public override ImageMetaData ReadMetaData (IBinaryStream stream)
{ {
var header = new byte[0x18]; var header = stream.ReadHeader (0x18);
if (header.Length != stream.Read (header, 0, header.Length)) uint width = header.ToUInt32 (0);
return null; uint height = header.ToUInt32 (4);
uint width = LittleEndian.ToUInt32 (header, 0); int bpp = header.ToInt32 (8);
uint height = LittleEndian.ToUInt32 (header, 4); int packed_size = header.ToInt32 (0x0C);
int bpp = LittleEndian.ToInt32 (header, 8); int unpacked_size = header.ToInt32 (0x10);
int packed_size = LittleEndian.ToInt32 (header, 0x0C);
int unpacked_size = LittleEndian.ToInt32 (header, 0x10);
if (0 == width || 0 == height || width > 0x7fff || height > 0x7fff if (0 == width || 0 == height || width > 0x7fff || height > 0x7fff
|| (bpp != 24 && bpp != 8) || (bpp != 24 && bpp != 8)
|| unpacked_size <= 0 || packed_size < 0) || unpacked_size <= 0 || packed_size < 0)
@ -75,16 +73,16 @@ namespace GameRes.Formats.Cherry
BPP = bpp, BPP = bpp,
PackedSize = packed_size, PackedSize = packed_size,
UnpackedSize = unpacked_size, UnpackedSize = unpacked_size,
Offset = LittleEndian.ToInt32 (header, 0x14), Offset = header.ToInt32 (0x14),
HeaderSize = 0x18, HeaderSize = 0x18,
AlphaChannel = false, AlphaChannel = false,
}; };
} }
public override ImageData Read (Stream stream, ImageMetaData info) public override ImageData Read (IBinaryStream file, ImageMetaData info)
{ {
var meta = (GrpMetaData)info; var meta = (GrpMetaData)info;
var reader = new GrpReader (stream, meta); var reader = new GrpReader (file.AsStream, meta);
return reader.CreateImage(); return reader.CreateImage();
} }
@ -101,18 +99,16 @@ namespace GameRes.Formats.Cherry
public override string Description { get { return "Cherry Soft compressed image format"; } } public override string Description { get { return "Cherry Soft compressed image format"; } }
public override uint Signature { get { return 0; } } public override uint Signature { get { return 0; } }
public override ImageMetaData ReadMetaData (Stream stream) public override ImageMetaData ReadMetaData (IBinaryStream stream)
{ {
var header = new byte[0x28]; var header = stream.ReadHeader (0x28);
if (header.Length != stream.Read (header, 0, header.Length)) if (0xFFFF != header.ToInt32 (8))
return null; return null;
if (0xFFFF != LittleEndian.ToInt32 (header, 8)) int packed_size = header.ToInt32 (0);
return null; int unpacked_size = header.ToInt32 (4);
int packed_size = LittleEndian.ToInt32 (header, 0); uint width = header.ToUInt32 (0x10);
int unpacked_size = LittleEndian.ToInt32 (header, 4); uint height = header.ToUInt32 (0x14);
uint width = LittleEndian.ToUInt32 (header, 0x10); int bpp = header.ToInt32 (0x18);
uint height = LittleEndian.ToUInt32 (header, 0x14);
int bpp = LittleEndian.ToInt32 (header, 0x18);
if (0 == width || 0 == height || width > 0x7fff || height > 0x7fff if (0 == width || 0 == height || width > 0x7fff || height > 0x7fff
|| (bpp != 32 && bpp != 24 && bpp != 8) || (bpp != 32 && bpp != 24 && bpp != 8)
|| unpacked_size <= 0 || packed_size < 0) || unpacked_size <= 0 || packed_size < 0)
@ -126,7 +122,7 @@ namespace GameRes.Formats.Cherry
UnpackedSize = unpacked_size, UnpackedSize = unpacked_size,
Offset = 0xFFFF, Offset = 0xFFFF,
HeaderSize = 0x28, HeaderSize = 0x28,
AlphaChannel = LittleEndian.ToInt32 (header, 0x24) != 0, AlphaChannel = header.ToInt32 (0x24) != 0,
}; };
} }
} }

View File

@ -117,7 +117,7 @@ namespace GameRes.Formats.Circus
{ {
data = UnpackCps (data); data = UnpackCps (data);
} }
return new MemoryStream (data); return new BinMemoryStream (data, entry.Name);
} }
byte[] UnpackCps (byte[] input) byte[] UnpackCps (byte[] input)

View File

@ -38,46 +38,43 @@ namespace GameRes.Formats.Circus
public override string Description { get { return "Circus PCM audio"; } } public override string Description { get { return "Circus PCM audio"; } }
public override uint Signature { get { return 0x4d435058; } } // 'XPCM' public override uint Signature { get { return 0x4d435058; } } // 'XPCM'
public override SoundInput TryOpen (Stream file) public override SoundInput TryOpen (IBinaryStream file)
{ {
file.Position = 4; file.Position = 4;
using (var input = new ArcView.Reader (file)) int src_size = file.ReadInt32();
if (src_size <= 0)
throw new InvalidFormatException();
int mode = file.ReadInt32();
int extra = (mode >> 8) & 0xff;
mode &= 0xff;
if (5 == mode)
{ {
int src_size = input.ReadInt32(); uint ogg_size = file.ReadUInt32();
if (src_size <= 0) var ogg = new StreamRegion (file.AsStream, 0x10, ogg_size);
throw new InvalidFormatException(); return new OggInput (ogg);
int mode = input.ReadInt32();
int extra = (mode >> 8) & 0xff;
mode &= 0xff;
if (5 == mode)
{
uint ogg_size = input.ReadUInt32();
var ogg = new StreamRegion (file, 0x10, ogg_size);
return new OggInput (ogg);
}
var format = new WaveFormat();
format.FormatTag = input.ReadUInt16();
format.Channels = input.ReadUInt16();
format.SamplesPerSecond = input.ReadUInt32();
format.AverageBytesPerSecond = input.ReadUInt32();
format.BlockAlign = input.ReadUInt16();
format.BitsPerSample = input.ReadUInt16();
Stream pcm;
if (0 == mode)
{
pcm = new StreamRegion (file, file.Position, src_size);
}
else if (1 == mode || 3 == mode)
{
var decoder = new PcmDecoder (input, src_size, extra, (XpcmCompression)mode);
pcm = new MemoryStream (decoder.Unpack(), 0, src_size);
file.Dispose();
}
else
throw new NotSupportedException ("Not supported Circus PCM audio compression");
return new RawPcmInput (pcm, format);
} }
var format = new WaveFormat();
format.FormatTag = file.ReadUInt16();
format.Channels = file.ReadUInt16();
format.SamplesPerSecond = file.ReadUInt32();
format.AverageBytesPerSecond = file.ReadUInt32();
format.BlockAlign = file.ReadUInt16();
format.BitsPerSample = file.ReadUInt16();
Stream pcm;
if (0 == mode)
{
pcm = new StreamRegion (file.AsStream, file.Position, src_size);
}
else if (1 == mode || 3 == mode)
{
var decoder = new PcmDecoder (file, src_size, extra, (XpcmCompression)mode);
pcm = new MemoryStream (decoder.Unpack(), 0, src_size);
file.Dispose();
}
else
throw new NotSupportedException ("Not supported Circus PCM audio compression");
return new RawPcmInput (pcm, format);
} }
} }
@ -97,7 +94,7 @@ namespace GameRes.Formats.Circus
public byte[] Data { get { return m_pcm_data; } } public byte[] Data { get { return m_pcm_data; } }
public PcmDecoder (BinaryReader input, int pcm_size, int extra, XpcmCompression mode) public PcmDecoder (IBinaryStream input, int pcm_size, int extra, XpcmCompression mode)
{ {
if (extra < 0 || extra > 3) if (extra < 0 || extra > 3)
throw new InvalidFormatException(); throw new InvalidFormatException();
@ -114,7 +111,7 @@ namespace GameRes.Formats.Circus
} }
else if (XpcmCompression.Zlib == mode) else if (XpcmCompression.Zlib == mode)
{ {
using (var z = new ZLibStream (input.BaseStream, CompressionMode.Decompress, true)) using (var z = new ZLibStream (input.AsStream, CompressionMode.Decompress, true))
z.Read (m_encoded, 0, m_encoded.Length); z.Read (m_encoded, 0, m_encoded.Length);
} }
else else

View File

@ -266,23 +266,23 @@ namespace GameRes.Formats.Circus
{ {
flag >>= 1; flag >>= 1;
if (0 == (flag & 0x100)) if (0 == (flag & 0x100))
flag = m_input.ReadByte() | 0xff00; flag = m_input.ReadUInt8() | 0xff00;
if (0 != (flag & 1)) if (0 != (flag & 1))
{ {
byte dat = m_input.ReadByte(); byte dat = m_input.ReadUInt8();
window[win_pos++] = dat; window[win_pos++] = dat;
win_pos &= 0xffff; win_pos &= 0xffff;
m_output[dst++] = dat; m_output[dst++] = dat;
} }
else else
{ {
byte control = m_input.ReadByte(); byte control = m_input.ReadUInt8();
int count, offset; int count, offset;
if (control >= 0xc0) if (control >= 0xc0)
{ {
offset = ((control & 3) << 8) | m_input.ReadByte(); offset = ((control & 3) << 8) | m_input.ReadUInt8();
count = 4 + ((control >> 2) & 0xf); count = 4 + ((control >> 2) & 0xf);
} }
else if (0 != (control & 0x80)) else if (0 != (control & 0x80))
@ -290,7 +290,7 @@ namespace GameRes.Formats.Circus
offset = control & 0x1f; offset = control & 0x1f;
count = 2 + ((control >> 5) & 3); count = 2 + ((control >> 5) & 3);
if (0 == offset) if (0 == offset)
offset = m_input.ReadByte(); offset = m_input.ReadUInt8();
} }
else if (0x7f == control) else if (0x7f == control)
{ {

View File

@ -70,7 +70,7 @@ namespace GameRes.Formats.Circus
else if (header.AsciiEqual (0x20, "CRXG")) else if (header.AsciiEqual (0x20, "CRXG"))
{ {
using (var crx_input = new StreamRegion (stream.AsStream, 0x20, true)) using (var crx_input = new StreamRegion (stream.AsStream, 0x20, true))
using (var crx = new BinaryStream (crx_input)) using (var crx = new BinaryStream (crx_input, stream.Name))
{ {
var diff_info = base.ReadMetaData (crx) as CrxMetaData; var diff_info = base.ReadMetaData (crx) as CrxMetaData;
if (null == diff_info) if (null == diff_info)
@ -90,12 +90,12 @@ namespace GameRes.Formats.Circus
if (info != null) if (info != null)
{ {
info.BaseOffset = header.ToUInt32 (8); info.BaseOffset = header.ToUInt32 (8);
info.BaseFileName = Binary.GetCString (header, 0xC, 0x14); info.BaseFileName = header.GetCString (0xC, 0x14);
} }
return info; return info;
} }
Stream OpenByOffset (uint offset) IBinaryStream OpenByOffset (uint offset)
{ {
var vfs = VFS.Top as ArchiveFileSystem; var vfs = VFS.Top as ArchiveFileSystem;
if (null == vfs) if (null == vfs)
@ -118,9 +118,9 @@ namespace GameRes.Formats.Circus
diff = OpenByOffset (info.DiffOffset); diff = OpenByOffset (info.DiffOffset);
if (null == diff) if (null == diff)
throw new FileNotFoundException ("Referenced diff image not found"); throw new FileNotFoundException ("Referenced diff image not found");
input = new StreamRegion (diff, 0x20); input = new StreamRegion (diff.AsStream, 0x20);
} }
return new BinaryStream (input); return new BinaryStream (input, diff.Name);
} }
public override ImageData Read (IBinaryStream stream, ImageMetaData info) public override ImageData Read (IBinaryStream stream, ImageMetaData info)

View File

@ -279,7 +279,7 @@ namespace GameRes.Formats.Purple
data = UnpackPs2 (data); data = UnpackPs2 (data);
else if (data.Length > 0x40 && Binary.AsciiEqual (data, 0, "PB3B")) else if (data.Length > 0x40 && Binary.AsciiEqual (data, 0, "PB3B"))
DecryptPb3 (data); DecryptPb3 (data);
return new MemoryStream (data); return new BinMemoryStream (data, entry.Name);
} }
void DecryptIndexStage1 (byte[] data, uint key, CmvsScheme scheme) void DecryptIndexStage1 (byte[] data, uint key, CmvsScheme scheme)

View File

@ -122,7 +122,7 @@ namespace GameRes.Formats.Pvns
{ {
data = DecryptScript (data, parc.ScriptKey); data = DecryptScript (data, parc.ScriptKey);
} }
return new MemoryStream (data); return new BinMemoryStream (data, entry.Name);
} }
static void Decrypt (byte[] data, byte[] key) static void Decrypt (byte[] data, byte[] key)

View File

@ -47,33 +47,30 @@ namespace GameRes.Formats.Purple
public override string Description { get { return "Purple Software image format"; } } public override string Description { get { return "Purple Software image format"; } }
public override uint Signature { get { return 0x42334250; } } // 'PB3B' public override uint Signature { get { return 0x42334250; } } // 'PB3B'
public override ImageMetaData ReadMetaData (Stream stream) public override ImageMetaData ReadMetaData (IBinaryStream stream)
{ {
stream.Position = 4; stream.Position = 4;
using (var reader = new ArcView.Reader (stream)) int input_size = stream.ReadInt32();
stream.Position = 0x18;
int t2 = stream.ReadInt32();
int t1 = stream.ReadUInt16();
uint width = stream.ReadUInt16();
uint height = stream.ReadUInt16();
int bpp = stream.ReadUInt16();
return new Pb3MetaData
{ {
int input_size = reader.ReadInt32(); Width = width,
stream.Position = 0x18; Height = height,
int t2 = reader.ReadInt32(); BPP = bpp,
int t1 = reader.ReadUInt16(); Type = t1,
uint width = reader.ReadUInt16(); SubType = t2,
uint height = reader.ReadUInt16(); InputSize = input_size,
int bpp = reader.ReadUInt16(); };
return new Pb3MetaData
{
Width = width,
Height = height,
BPP = bpp,
Type = t1,
SubType = t2,
InputSize = input_size,
};
}
} }
public override ImageData Read (Stream stream, ImageMetaData info) public override ImageData Read (IBinaryStream stream, ImageMetaData info)
{ {
var reader = new Pb3Reader (stream, (Pb3MetaData)info); var reader = new Pb3Reader (stream.AsStream, (Pb3MetaData)info);
reader.Unpack(); reader.Unpack();
return ImageData.Create (info, reader.Format, null, reader.Data); return ImageData.Create (info, reader.Format, null, reader.Data);
} }
@ -350,9 +347,9 @@ namespace GameRes.Formats.Purple
if (name.Equals (m_info.FileName, StringComparison.InvariantCultureIgnoreCase)) if (name.Equals (m_info.FileName, StringComparison.InvariantCultureIgnoreCase))
throw new InvalidFormatException(); throw new InvalidFormatException();
// two files referencing each other still could create infinite recursion // two files referencing each other still could create infinite recursion
using (var base_file = VFS.OpenSeekableStream (name)) using (var base_file = VFS.OpenBinaryStream (name))
{ {
var image_data = ImageFormat.Read (name, base_file); var image_data = ImageFormat.Read (base_file);
int stride = image_data.Bitmap.PixelWidth * 4; int stride = image_data.Bitmap.PixelWidth * 4;
var pixels = new byte[stride * image_data.Bitmap.PixelHeight]; var pixels = new byte[stride * image_data.Bitmap.PixelHeight];
image_data.Bitmap.CopyPixels (pixels, stride, 0); image_data.Bitmap.CopyPixels (pixels, stride, 0);

View File

@ -104,7 +104,7 @@ namespace GameRes.Formats.Cri
} }
Array.Reverse (output, (int)prefix_size, unpacked_size); Array.Reverse (output, (int)prefix_size, unpacked_size);
arc.File.View.Read (entry.Offset+0x10+packed_size, output, 0, prefix_size); arc.File.View.Read (entry.Offset+0x10+packed_size, output, 0, prefix_size);
return new MemoryStream (output); return new BinMemoryStream (output, entry.Name);
} }
void DetectFileTypes (ArcView file, List<Entry> dir) void DetectFileTypes (ArcView file, List<Entry> dir)

View File

@ -43,7 +43,7 @@ namespace GameRes.Formats.Cri
uint signature = file.Signature; uint signature = file.Signature;
if (0x80 != (signature & 0xFFFF)) if (0x80 != (signature & 0xFFFF))
return null; return null;
uint header_size = Binary.BigEndian (signature & 0xFFFF0000); int header_size = (int)Binary.BigEndian (signature & 0xFFFF0000);
if (header_size < 0x10 || header_size >= file.Length) if (header_size < 0x10 || header_size >= file.Length)
return null; return null;
var header = file.ReadBytes (header_size); var header = file.ReadBytes (header_size);

View File

@ -61,57 +61,52 @@ namespace GameRes.Formats.Cri
throw new NotImplementedException ("BipFormat.Write not implemented"); throw new NotImplementedException ("BipFormat.Write not implemented");
} }
public override ImageMetaData ReadMetaData (Stream stream) public override ImageMetaData ReadMetaData (IBinaryStream input)
{ {
using (var input = new BinaryReader (stream, Encoding.ASCII, true)) int sig = input.ReadInt32();
{ if (sig != 5 && sig != 10)
int sig = input.ReadInt32(); return null;
if (sig != 5 && sig != 10) uint header_end = (uint)sig*4;
return null; uint index_offset = input.ReadUInt32();
uint header_end = (uint)sig*4;
uint index_offset = input.ReadUInt32();
input.BaseStream.Position = header_end-4; input.Position = header_end-4;
uint data_offset = input.ReadUInt32() + 8; uint data_offset = input.ReadUInt32() + 8;
if (index_offset >= data_offset || index_offset < header_end) if (index_offset >= data_offset || index_offset < header_end)
return null; return null;
input.BaseStream.Position = index_offset; input.Position = index_offset;
int tile_count = input.ReadInt16(); int tile_count = input.ReadInt16();
int flag = input.ReadInt16(); int flag = input.ReadInt16();
if (tile_count <= 0 || 0 != flag) if (tile_count <= 0 || 0 != flag)
return null; return null;
input.ReadInt32(); input.ReadInt32();
uint w = input.ReadUInt16(); uint w = input.ReadUInt16();
uint h = input.ReadUInt16(); uint h = input.ReadUInt16();
if (0 == w || 0 == h) if (0 == w || 0 == h)
return null; return null;
var meta = new BipMetaData { Width = w, Height = h, BPP = 32 }; var meta = new BipMetaData { Width = w, Height = h, BPP = 32 };
meta.Tiles.Capacity = tile_count; meta.Tiles.Capacity = tile_count;
for (int i = 0; i < tile_count; ++i) for (int i = 0; i < tile_count; ++i)
{ {
input.ReadInt64(); input.ReadInt64();
var tile = new BipTile(); var tile = new BipTile();
tile.Left = input.ReadUInt16(); tile.Left = input.ReadUInt16();
tile.Top = input.ReadUInt16(); tile.Top = input.ReadUInt16();
tile.Width = input.ReadUInt16(); tile.Width = input.ReadUInt16();
tile.Height = input.ReadUInt16(); tile.Height = input.ReadUInt16();
if (tile.Left + tile.Width > meta.Width) if (tile.Left + tile.Width > meta.Width)
meta.Width = (uint)(tile.Left + tile.Width); meta.Width = (uint)(tile.Left + tile.Width);
if (tile.Top + tile.Height > meta.Height) if (tile.Top + tile.Height > meta.Height)
meta.Height = (uint)(tile.Top + tile.Height); meta.Height = (uint)(tile.Top + tile.Height);
input.ReadInt64(); input.ReadInt64();
tile.Offset = input.ReadUInt32() + data_offset; tile.Offset = input.ReadUInt32() + data_offset;
meta.Tiles.Add (tile); meta.Tiles.Add (tile);
}
return meta;
} }
return meta;
} }
public override ImageData Read (Stream stream, ImageMetaData info) public override ImageData Read (IBinaryStream stream, ImageMetaData info)
{ {
var meta = info as BipMetaData; var meta = (BipMetaData)info;
if (null == meta)
throw new ArgumentException ("BipFormat.Read should be supplied with BipMetaData", "info");
var header = new byte[0x7c]; var header = new byte[0x7c];
var bitmap = new WriteableBitmap ((int)meta.Width, (int)meta.Height, var bitmap = new WriteableBitmap ((int)meta.Width, (int)meta.Height,
@ -127,7 +122,7 @@ namespace GameRes.Formats.Cri
int alpha = LittleEndian.ToInt32 (header, 0x68); int alpha = LittleEndian.ToInt32 (header, 0x68);
int x = LittleEndian.ToInt32 (header, 0x6c); int x = LittleEndian.ToInt32 (header, 0x6c);
int y = LittleEndian.ToInt32 (header, 0x70); int y = LittleEndian.ToInt32 (header, 0x70);
using (var png = new StreamRegion (stream, stream.Position, data_size, true)) using (var png = new StreamRegion (stream.AsStream, stream.Position, data_size, true))
{ {
var decoder = new PngBitmapDecoder (png, var decoder = new PngBitmapDecoder (png,
BitmapCreateOptions.None, BitmapCacheOption.OnLoad); BitmapCreateOptions.None, BitmapCacheOption.OnLoad);

View File

@ -49,7 +49,7 @@ namespace GameRes.Formats.Cri
return null; return null;
using (var lzss = new LzssStream (stream.AsStream, LzssMode.Decompress, true)) using (var lzss = new LzssStream (stream.AsStream, LzssMode.Decompress, true))
using (var input = new SeekableStream (lzss)) using (var input = new SeekableStream (lzss))
using (var xtx = new BinaryStream (input)) using (var xtx = new BinaryStream (input, stream.Name))
return base.ReadMetaData (xtx); return base.ReadMetaData (xtx);
} }
@ -58,7 +58,7 @@ namespace GameRes.Formats.Cri
stream.Position = 4; stream.Position = 4;
using (var lzss = new LzssStream (stream.AsStream, LzssMode.Decompress, true)) using (var lzss = new LzssStream (stream.AsStream, LzssMode.Decompress, true))
using (var input = new SeekableStream (lzss)) using (var input = new SeekableStream (lzss))
using (var xtx = new BinaryStream (input)) using (var xtx = new BinaryStream (input, stream.Name))
return base.Read (xtx, info); return base.Read (xtx, info);
} }

View File

@ -36,9 +36,9 @@ namespace GameRes.Formats.Crowd
public override string Description { get { return "Crowd engine audio format (Ogg/Vorbis)"; } } public override string Description { get { return "Crowd engine audio format (Ogg/Vorbis)"; } }
public override uint Signature { get { return 0x004D5243; } } // 'CRM' public override uint Signature { get { return 0x004D5243; } } // 'CRM'
public override SoundInput TryOpen (Stream file) public override SoundInput TryOpen (IBinaryStream file)
{ {
var ogg = new StreamRegion (file, 8); var ogg = new StreamRegion (file.AsStream, 8);
return new OggInput (ogg); return new OggInput (ogg);
// in case of exception ogg stream is left undisposed // in case of exception ogg stream is left undisposed
} }

View File

@ -43,23 +43,21 @@ namespace GameRes.Formats.Crowd
static readonly byte[] SignatureText = Encoding.ASCII.GetBytes ("cwd format - version 1.00 -"); static readonly byte[] SignatureText = Encoding.ASCII.GetBytes ("cwd format - version 1.00 -");
public override ImageMetaData ReadMetaData (Stream stream) public override ImageMetaData ReadMetaData (IBinaryStream stream)
{ {
var header = new byte[0x38]; var header = stream.ReadHeader (0x38);
if (header.Length != stream.Read (header, 0, header.Length))
return null;
if (!header.Take (SignatureText.Length).SequenceEqual (SignatureText)) if (!header.Take (SignatureText.Length).SequenceEqual (SignatureText))
return null; return null;
uint key = header[0x34] + 0x259Au; uint key = header[0x34] + 0x259Au;
return new ImageMetaData return new ImageMetaData
{ {
Width = LittleEndian.ToUInt32 (header, 0x2c) + key, Width = header.ToUInt32 (0x2c) + key,
Height = LittleEndian.ToUInt32 (header, 0x30) + key, Height = header.ToUInt32 (0x30) + key,
BPP = 15, BPP = 15,
}; };
} }
public override ImageData Read (Stream stream, ImageMetaData info) public override ImageData Read (IBinaryStream stream, ImageMetaData info)
{ {
stream.Position = 0x38; stream.Position = 0x38;
int size = (int)info.Width * (int)info.Height * 2; int size = (int)info.Width * (int)info.Height * 2;
@ -82,36 +80,34 @@ namespace GameRes.Formats.Crowd
public override string Description { get { return "LZ-compressed Crowd bitmap"; } } public override string Description { get { return "LZ-compressed Crowd bitmap"; } }
public override uint Signature { get { return 0x44445A53u; } } // 'SZDD' public override uint Signature { get { return 0x44445A53u; } } // 'SZDD'
public override ImageMetaData ReadMetaData (Stream stream) public override ImageMetaData ReadMetaData (IBinaryStream stream)
{ {
stream.Position = 0x0e; stream.Position = 0x0e;
using (var lz = new LzssReader (stream, 100, 0x38)) // extract CWD header using (var lz = new LzssReader (stream.AsStream, 100, 0x38)) // extract CWD header
{ {
lz.FrameSize = 0x1000; lz.FrameSize = 0x1000;
lz.FrameFill = 0x20; lz.FrameFill = 0x20;
lz.FrameInitPos = 0x1000 - 0x10; lz.FrameInitPos = 0x1000 - 0x10;
lz.Unpack(); lz.Unpack();
using (var cwd = new MemoryStream (lz.Data)) using (var cwd = new BinMemoryStream (lz.Data, stream.Name))
return base.ReadMetaData (cwd); return base.ReadMetaData (cwd);
} }
} }
public override ImageData Read (Stream stream, ImageMetaData info) public override ImageData Read (IBinaryStream file, ImageMetaData info)
{ {
if (stream.Length > int.MaxValue) if (file.Length > int.MaxValue)
throw new FileSizeException(); throw new FileSizeException();
var header = new byte[14]; var header = file.ReadHeader (14);
if (header.Length != stream.Read (header, 0, header.Length)) int data_length = header.ToInt32 (10);
throw new InvalidFormatException(); int input_length = (int)(file.Length-file.Position);
int data_length = LittleEndian.ToInt32 (header, 10); using (var lz = new LzssReader (file.AsStream, input_length, data_length))
int input_length = (int)(stream.Length-stream.Position);
using (var lz = new LzssReader (stream, input_length, data_length))
{ {
lz.FrameSize = 0x1000; lz.FrameSize = 0x1000;
lz.FrameFill = 0x20; lz.FrameFill = 0x20;
lz.FrameInitPos = 0x1000 - 0x10; lz.FrameInitPos = 0x1000 - 0x10;
lz.Unpack(); lz.Unpack();
using (var cwd = new MemoryStream (lz.Data)) using (var cwd = new BinMemoryStream (lz.Data, file.Name))
return base.Read (cwd, info); return base.Read (cwd, info);
} }
} }

View File

@ -40,50 +40,47 @@ namespace GameRes.Formats.Crowd
public override uint Signature { get { return 0x50445743; } } // 'CWDP' public override uint Signature { get { return 0x50445743; } } // 'CWDP'
public override bool CanWrite { get { return true; } } public override bool CanWrite { get { return true; } }
public override ImageMetaData ReadMetaData (Stream stream) public override ImageMetaData ReadMetaData (IBinaryStream input)
{ {
using (var input = new ArcView.Reader (stream)) input.Position = 4;
uint width = Binary.BigEndian (input.ReadUInt32());
uint height = Binary.BigEndian (input.ReadUInt32());
if (0 == width || 0 == height)
return null;
int bpp = input.ReadByte();
int color_type = input.ReadByte();
switch (color_type)
{ {
input.ReadInt32(); case 2: bpp *= 3; break;
uint width = Binary.BigEndian (input.ReadUInt32()); case 4: bpp *= 2; break;
uint height = Binary.BigEndian (input.ReadUInt32()); case 6: bpp *= 4; break;
if (0 == width || 0 == height) case 3:
return null; case 0: break;
int bpp = input.ReadByte(); default: return null;
int color_type = input.ReadByte();
switch (color_type)
{
case 2: bpp *= 3; break;
case 4: bpp *= 2; break;
case 6: bpp *= 4; break;
case 3:
case 0: break;
default: return null;
}
return new ImageMetaData
{
Width = width,
Height = height,
BPP = bpp,
};
} }
return new ImageMetaData
{
Width = width,
Height = height,
BPP = bpp,
};
} }
public override ImageData Read (Stream stream, ImageMetaData info) public override ImageData Read (IBinaryStream file, ImageMetaData info)
{ {
var header = new byte[0x15]; var header = new byte[0x15];
using (var mem = new MemoryStream((int)(0x14 + stream.Length + 12))) using (var mem = new MemoryStream((int)(0x14 + file.Length + 12)))
using (var png = new BinaryWriter (mem)) using (var png = new BinaryWriter (mem))
{ {
png.Write (0x474E5089u); // png header png.Write (0x474E5089u); // png header
png.Write (0x0A1A0A0Du); png.Write (0x0A1A0A0Du);
png.Write (0x0D000000u); png.Write (0x0D000000u);
png.Write (0x52444849u); // 'IHDR' png.Write (0x52444849u); // 'IHDR'
stream.Position = 4; file.Position = 4;
stream.Read (header, 0, header.Length); file.Read (header, 0, header.Length);
png.Write (header, 0, header.Length); png.Write (header, 0, header.Length);
png.Write (0x54414449u); // 'IDAT' png.Write (0x54414449u); // 'IDAT'
stream.CopyTo (mem); file.AsStream.CopyTo (mem);
header[1] = 0; header[1] = 0;
header[2] = 0; header[2] = 0;
header[3] = 0; header[3] = 0;
@ -149,7 +146,9 @@ namespace GameRes.Formats.Crowd
png.Read (header, 0, header.Length); png.Read (header, 0, header.Length);
cwp.Write (0x50445743u); // 'CWDP' cwp.Write (0x50445743u); // 'CWDP'
cwp.Write (header, 0, header.Length); cwp.Write (header, 0, header.Length);
var idat = PngFormat.FindChunk (png, "IDAT"); long idat;
using (var bin = new BinMemoryStream (png, ""))
idat = PngFormat.FindChunk (bin, "IDAT");
if (-1 == idat) if (-1 == idat)
throw new InvalidFormatException ("CWP conversion failed"); throw new InvalidFormatException ("CWP conversion failed");
png.Position = idat; png.Position = idat;

View File

@ -39,10 +39,10 @@ namespace GameRes.Formats.Crowd
public override uint Signature { get { return 0x44445A53u; } } // 'SZDD' public override uint Signature { get { return 0x44445A53u; } } // 'SZDD'
public override bool CanWrite { get { return false; } } public override bool CanWrite { get { return false; } }
public override ImageMetaData ReadMetaData (Stream stream) public override ImageMetaData ReadMetaData (IBinaryStream stream)
{ {
stream.Position = 0x0e; stream.Position = 0x0e;
using (var lz = new LzssReader (stream, 100, 54)) // extract BMP header using (var lz = new LzssReader (stream.AsStream, 100, 54)) // extract BMP header
{ {
lz.FrameSize = 0x1000; lz.FrameSize = 0x1000;
lz.FrameFill = 0x20; lz.FrameFill = 0x20;
@ -51,21 +51,19 @@ namespace GameRes.Formats.Crowd
var header = lz.Data; var header = lz.Data;
for (int i = 0; i < 54; ++i) for (int i = 0; i < 54; ++i)
header[i] ^= 0xff; header[i] ^= 0xff;
using (var bmp = new MemoryStream (header)) using (var bmp = new BinMemoryStream (header, stream.Name))
return base.ReadMetaData (bmp); return base.ReadMetaData (bmp);
} }
} }
public override ImageData Read (Stream stream, ImageMetaData info) public override ImageData Read (IBinaryStream stream, ImageMetaData info)
{ {
if (stream.Length > int.MaxValue) if (stream.Length > int.MaxValue)
throw new FileSizeException(); throw new FileSizeException();
var header = new byte[14]; var header = stream.ReadHeader (14);
if (header.Length != stream.Read (header, 0, header.Length)) int data_length = header.ToInt32 (10);
throw new InvalidFormatException();
int data_length = LittleEndian.ToInt32 (header, 10);
int input_length = (int)(stream.Length-stream.Position); int input_length = (int)(stream.Length-stream.Position);
using (var lz = new LzssReader (stream, input_length, data_length)) using (var lz = new LzssReader (stream.AsStream, input_length, data_length))
{ {
lz.FrameSize = 0x1000; lz.FrameSize = 0x1000;
lz.FrameFill = 0x20; lz.FrameFill = 0x20;
@ -75,7 +73,7 @@ namespace GameRes.Formats.Crowd
int count = Math.Min (100, data.Length); int count = Math.Min (100, data.Length);
for (int i = 0; i < count; ++i) for (int i = 0; i < count; ++i)
data[i] ^= 0xff; data[i] ^= 0xff;
using (var bmp = new MemoryStream (data)) using (var bmp = new BinMemoryStream (data, stream.Name))
return base.Read (bmp, info); return base.Read (bmp, info);
} }
} }

View File

@ -149,7 +149,7 @@ namespace GameRes.Formats.CsWare
header[i] = (byte)(pent.Key - header[i] - 1); header[i] = (byte)(pent.Key - header[i] - 1);
} }
if (header_size == entry.Size) if (header_size == entry.Size)
return new MemoryStream (header); return new BinMemoryStream (header, entry.Name);
var rest = arc.File.CreateStream (entry.Offset+512, entry.Size-512); var rest = arc.File.CreateStream (entry.Offset+512, entry.Size-512);
return new PrefixStream (header, rest); return new PrefixStream (header, rest);
} }

View File

@ -90,9 +90,8 @@ namespace GameRes.Formats.Cyberworks
input = new MemoryStream (header); input = new MemoryStream (header);
else else
input = new PrefixStream (header, new StreamRegion (file.AsStream, file.Position)); input = new PrefixStream (header, new StreamRegion (file.AsStream, file.Position));
var ogg = new BinaryStream (input, file.Name); var sound = new OggInput (input);
var sound = OggAudio.Instance.TryOpen (ogg); if (header.Length >= file.Length)
if (sound != null && header.Length >= file.Length)
file.Dispose(); file.Dispose();
return sound; return sound;
} }

View File

@ -130,7 +130,7 @@ namespace GameRes.Formats.DDSystem
reader.Unpack (data); reader.Unpack (data);
if (data.Length > 16 && Binary.AsciiEqual (data, 0, "DDSxHXB")) if (data.Length > 16 && Binary.AsciiEqual (data, 0, "DDSxHXB"))
DecryptHxb (data); DecryptHxb (data);
return new MemoryStream (data); return new BinMemoryStream (data, entry.Name);
} }
} }

View File

@ -157,7 +157,7 @@ namespace GameRes.Formats.Dac
var data = new byte[entry.Size]; var data = new byte[entry.Size];
arc.File.View.Read (entry.Offset, data, 0, entry.Size); arc.File.View.Read (entry.Offset, data, 0, entry.Size);
DecryptEntry (data, parc.Key1, parc.Key2, pentry); DecryptEntry (data, parc.Key1, parc.Key2, pentry);
return new MemoryStream (data); return new BinMemoryStream (data, entry.Name);
} }
private void DecryptIndex (byte[] buf, int base_offset, int length, byte last) private void DecryptIndex (byte[] buf, int base_offset, int length, byte last)

View File

@ -43,28 +43,22 @@ namespace GameRes.Formats.Dac
public override string Description { get { return "DAC engine image format"; } } public override string Description { get { return "DAC engine image format"; } }
public override uint Signature { get { return 0x00434744; } } // 'DGC' public override uint Signature { get { return 0x00434744; } } // 'DGC'
public override ImageMetaData ReadMetaData (Stream stream) public override ImageMetaData ReadMetaData (IBinaryStream file)
{ {
using (var input = new ArcView.Reader (stream)) file.Position = 4;
{ var info = new DgcMetaData();
input.ReadInt32(); info.Flags = file.ReadUInt32();
var info = new DgcMetaData(); info.Width = file.ReadUInt16();
info.Flags = input.ReadUInt32(); info.Height = file.ReadUInt16();
info.Width = input.ReadUInt16(); if (info.Width > 0x7fff || info.Height > 0x7fff)
info.Height = input.ReadUInt16(); return null;
if (info.Width > 0x7fff || info.Height > 0x7fff) info.BPP = 0 == (info.Flags & Reader.FlagAlphaChannel) ? 24 : 32;
return null; return info;
info.BPP = 0 == (info.Flags & Reader.FlagAlphaChannel) ? 24 : 32;
return info;
}
} }
public override ImageData Read (Stream stream, ImageMetaData info) public override ImageData Read (IBinaryStream stream, ImageMetaData info)
{ {
var meta = info as DgcMetaData; var meta = (DgcMetaData)info;
if (null == meta)
throw new ArgumentException ("DgcFormat.Read should be supplied with DgcMetaData", "info");
stream.Position = 12; stream.Position = 12;
using (var reader = new Reader (stream, meta)) using (var reader = new Reader (stream, meta))
{ {
@ -80,7 +74,7 @@ namespace GameRes.Formats.Dac
internal class Reader : IDataUnpacker, IDisposable internal class Reader : IDataUnpacker, IDisposable
{ {
BinaryReader m_input; IBinaryStream m_input;
byte[] m_output; byte[] m_output;
readonly int m_width; readonly int m_width;
readonly int m_height; readonly int m_height;
@ -96,11 +90,11 @@ namespace GameRes.Formats.Dac
public byte[] Data { get { return m_output; } } public byte[] Data { get { return m_output; } }
public PixelFormat Format { get; private set; } public PixelFormat Format { get; private set; }
public Reader (Stream input, DgcMetaData info) public Reader (IBinaryStream input, DgcMetaData info)
{ {
m_width = (int)info.Width; m_width = (int)info.Width;
m_height = (int)info.Height; m_height = (int)info.Height;
m_input = new ArcView.Reader (input); m_input = input;
m_use_dict = 0 != (info.Flags & FlagUseDictionary); m_use_dict = 0 != (info.Flags & FlagUseDictionary);
m_has_alpha = 0 != (info.Flags & FlagAlphaChannel); m_has_alpha = 0 != (info.Flags & FlagAlphaChannel);
m_max_dict_size = (int)(info.Flags & 0xffffff); m_max_dict_size = (int)(info.Flags & 0xffffff);
@ -162,7 +156,7 @@ namespace GameRes.Formats.Dac
if (dict_len > 256) if (dict_len > 256)
i = m_input.ReadUInt16(); i = m_input.ReadUInt16();
else else
i = m_input.ReadByte(); i = m_input.ReadUInt8();
i *= 3; i *= 3;
m_output[dst] = dict[i]; m_output[dst] = dict[i];
m_output[dst+1] = dict[i+1]; m_output[dst+1] = dict[i+1];
@ -178,7 +172,7 @@ namespace GameRes.Formats.Dac
{ {
var dict = new byte[m_max_dict_size * 3]; var dict = new byte[m_max_dict_size * 3];
int dict_len = m_input.ReadByte() + 1; int dict_len = m_input.ReadUInt8() + 1;
m_input.Read (dict, 0, dict_len * 3); m_input.Read (dict, 0, dict_len * 3);
for (int y = 0; y < m_height; y++) for (int y = 0; y < m_height; y++)
@ -198,7 +192,7 @@ namespace GameRes.Formats.Dac
{ {
for (int x = 0; x < m_width; x++) for (int x = 0; x < m_width; x++)
{ {
int i = 3 * m_input.ReadByte(); int i = 3 * m_input.ReadUInt8();
m_output[dst] = dict[i]; m_output[dst] = dict[i];
m_output[dst+1] = dict[i+1]; m_output[dst+1] = dict[i+1];
m_output[dst+2] = dict[i+2]; m_output[dst+2] = dict[i+2];
@ -260,7 +254,7 @@ namespace GameRes.Formats.Dac
{ {
for (int x = 0; x < m_width; x++) for (int x = 0; x < m_width; x++)
{ {
m_output[dst] = m_input.ReadByte(); m_output[dst] = m_input.ReadUInt8();
dst += m_pixel_size; dst += m_pixel_size;
} }
} }
@ -290,7 +284,7 @@ namespace GameRes.Formats.Dac
int index = 0; int index = 0;
if (0 != (ctl & 0x2000)) if (0 != (ctl & 0x2000))
{ {
index = m_input.ReadByte(); index = m_input.ReadUInt8();
--length; --length;
} }
else else
@ -314,7 +308,7 @@ namespace GameRes.Formats.Dac
int index = 0; int index = 0;
if (0 != (ctl & 0x2000)) if (0 != (ctl & 0x2000))
{ {
index = m_input.ReadByte(); index = m_input.ReadUInt8();
--length; --length;
} }
else else
@ -337,11 +331,11 @@ namespace GameRes.Formats.Dac
{ {
while (length > 0) while (length > 0)
{ {
byte ctl = m_input.ReadByte(); byte ctl = m_input.ReadUInt8();
--length; --length;
if (0 != ctl) if (0 != ctl)
{ {
int index = 3 * m_input.ReadByte(); int index = 3 * m_input.ReadUInt8();
--length; --length;
while (0 != ctl--) while (0 != ctl--)
{ {
@ -353,13 +347,13 @@ namespace GameRes.Formats.Dac
} }
else else
{ {
ctl = m_input.ReadByte(); ctl = m_input.ReadUInt8();
--length; --length;
if (0 == (ctl & 0x80)) if (0 == (ctl & 0x80))
{ {
for (int count = ctl + 2; 0 != count; --count) for (int count = ctl + 2; 0 != count; --count)
{ {
int src = 3 * m_input.ReadByte(); int src = 3 * m_input.ReadUInt8();
--length; --length;
m_output[dst] = dict[src]; m_output[dst] = dict[src];
m_output[dst+1] = dict[src+1]; m_output[dst+1] = dict[src+1];
@ -369,7 +363,7 @@ namespace GameRes.Formats.Dac
} }
else else
{ {
int offset = (short)((ctl << 8) | m_input.ReadByte()); int offset = (short)((ctl << 8) | m_input.ReadUInt8());
--length; --length;
int count = (offset & 0x3F) + 4; int count = (offset & 0x3F) + 4;
offset >>= 6; offset >>= 6;
@ -386,11 +380,11 @@ namespace GameRes.Formats.Dac
{ {
while (length > 0) while (length > 0)
{ {
byte ctl = m_input.ReadByte(); byte ctl = m_input.ReadUInt8();
--length; --length;
if (0 != ctl) if (0 != ctl)
{ {
byte alpha = m_input.ReadByte(); byte alpha = m_input.ReadUInt8();
--length; --length;
while (0 != ctl--) while (0 != ctl--)
{ {
@ -400,7 +394,7 @@ namespace GameRes.Formats.Dac
} }
else else
{ {
ctl = m_input.ReadByte(); ctl = m_input.ReadUInt8();
--length; --length;
if (0 == (ctl & 0x80)) if (0 == (ctl & 0x80))
{ {
@ -408,13 +402,13 @@ namespace GameRes.Formats.Dac
length -= count; length -= count;
while (0 != count--) while (0 != count--)
{ {
m_output[dst] = m_input.ReadByte(); m_output[dst] = m_input.ReadUInt8();
dst += m_pixel_size; dst += m_pixel_size;
} }
} }
else else
{ {
int offset = (short)((ctl << 8) | m_input.ReadByte()); int offset = (short)((ctl << 8) | m_input.ReadUInt8());
--length; --length;
int count = (offset & 0x3F) + 4; int count = (offset & 0x3F) + 4;
offset >>= 6; offset >>= 6;
@ -479,25 +473,10 @@ namespace GameRes.Formats.Dac
} }
#region IDisposable Members #region IDisposable Members
bool disposed = false;
public void Dispose () public void Dispose ()
{ {
Dispose (true);
GC.SuppressFinalize (this); GC.SuppressFinalize (this);
} }
protected virtual void Dispose (bool disposing)
{
if (!disposed)
{
if (disposing)
{
m_input.Dispose();
}
disposed = true;
}
}
#endregion #endregion
} }
} }

View File

@ -53,13 +53,14 @@ namespace GameRes.Formats.Dogenzaka
public static readonly byte[] KnownKey = Encoding.ASCII.GetBytes ("Hlk9D28p"); public static readonly byte[] KnownKey = Encoding.ASCII.GetBytes ("Hlk9D28p");
public override ImageMetaData ReadMetaData (Stream stream) public override ImageMetaData ReadMetaData (IBinaryStream stream)
{ {
using (var sha = SHA1.Create()) using (var sha = SHA1.Create())
{ {
var key = sha.ComputeHash (KnownKey).Take (16).ToArray(); var key = sha.ComputeHash (KnownKey).Take (16).ToArray();
using (var proxy = new InputProxyStream (stream, true)) using (var proxy = new InputProxyStream (stream.AsStream, true))
using (var input = new CryptoStream (proxy, new Rc4Transform (key), CryptoStreamMode.Read)) using (var crypto = new CryptoStream (proxy, new Rc4Transform (key), CryptoStreamMode.Read))
using (var input = new BinaryStream (crypto, stream.Name))
{ {
var info = base.ReadMetaData (input); var info = base.ReadMetaData (input);
if (null == info) if (null == info)
@ -77,12 +78,13 @@ namespace GameRes.Formats.Dogenzaka
} }
} }
public override ImageData Read (Stream stream, ImageMetaData info) public override ImageData Read (IBinaryStream stream, ImageMetaData info)
{ {
var rc4 = (Rc4PngMetaData)info; var rc4 = (Rc4PngMetaData)info;
using (var sha = SHA1.Create()) using (var sha = SHA1.Create())
using (var proxy = new InputProxyStream (stream, true)) using (var proxy = new InputProxyStream (stream.AsStream, true))
using (var input = new CryptoStream (proxy, new Rc4Transform (rc4.Key), CryptoStreamMode.Read)) using (var crypto = new CryptoStream (proxy, new Rc4Transform (rc4.Key), CryptoStreamMode.Read))
using (var input = new BinaryStream (crypto, stream.Name))
return base.Read (input, info); return base.Read (input, info);
} }

View File

@ -31,11 +31,11 @@ namespace GameRes.Formats.??????
public override string Description { get { return "?????? image format"; } } public override string Description { get { return "?????? image format"; } }
public override uint Signature { get { return 0; } } public override uint Signature { get { return 0; } }
public override ImageMetaData ReadMetaData (Stream stream) public override ImageMetaData ReadMetaData (IBinaryStream file)
{ {
} }
public override ImageData Read (Stream stream, ImageMetaData info) public override ImageData Read (IBinaryStream file, ImageMetaData info)
{ {
var meta = (xxxMetaData)info; var meta = (xxxMetaData)info;

View File

@ -145,7 +145,7 @@ namespace GameRes.Formats.DxLib
using (input) using (input)
{ {
var data = Unpack (input); var data = Unpack (input);
return new MemoryStream (data); return new BinMemoryStream (data, entry.Name);
} }
} }

View File

@ -162,7 +162,7 @@ namespace GameRes.Formats.DxLib
var offset = scr_arc.Encryption.StartOffset; var offset = scr_arc.Encryption.StartOffset;
scr_arc.Encryption.Decrypt (data, offset, data.Length-offset); scr_arc.Encryption.Decrypt (data, offset, data.Length-offset);
} }
return new MemoryStream (data); return new BinMemoryStream (data, entry.Name);
} }
public override ResourceOptions GetDefaultOptions () public override ResourceOptions GetDefaultOptions ()

View File

@ -333,7 +333,7 @@ namespace GameRes.Formats.Eagls
{ {
byte[] input = File.View.ReadBytes (entry.Offset, entry.Size); byte[] input = File.View.ReadBytes (entry.Offset, entry.Size);
Encryption.Decrypt (input); Encryption.Decrypt (input);
return new MemoryStream (input); return new BinMemoryStream (input, entry.Name);
} }
} }
} }

View File

@ -45,9 +45,9 @@ namespace GameRes.Formats.Eagls
public override uint Signature { get { return 0; } } public override uint Signature { get { return 0; } }
public override bool CanWrite { get { return false; } } public override bool CanWrite { get { return false; } }
public override ImageMetaData ReadMetaData (Stream stream) public override ImageMetaData ReadMetaData (IBinaryStream file)
{ {
using (var lzs = new LzssStream (stream, LzssMode.Decompress, true)) using (var lzs = new LzssStream (file.AsStream, LzssMode.Decompress, true))
{ {
if (lzs.ReadByte() != 'B' || lzs.ReadByte() != 'M') if (lzs.ReadByte() != 'B' || lzs.ReadByte() != 'M')
return null; return null;
@ -71,10 +71,11 @@ namespace GameRes.Formats.Eagls
} }
} }
public override ImageData Read (Stream stream, ImageMetaData info) public override ImageData Read (IBinaryStream file, ImageMetaData info)
{ {
var meta = (GrMetaData)info; var meta = (GrMetaData)info;
using (var bmp = new LzssStream (stream, LzssMode.Decompress, true)) using (var lzs = new LzssStream (file.AsStream, LzssMode.Decompress, true))
using (var bmp = new BinaryStream (lzs, file.Name))
{ {
if (32 != info.BPP) if (32 != info.BPP)
return base.Read (bmp, info); return base.Read (bmp, info);

View File

@ -43,24 +43,22 @@ namespace GameRes.Formats.Emic
Signatures = new uint[] { 0x1050574D, 0x4C594554 }; // 'TEYL' Signatures = new uint[] { 0x1050574D, 0x4C594554 }; // 'TEYL'
} }
public override ImageMetaData ReadMetaData (Stream stream) public override ImageMetaData ReadMetaData (IBinaryStream file)
{ {
var header = new byte[12]; file.Position = 4;
if (header.Length != stream.Read (header, 0, header.Length))
return null;
return new ImageMetaData return new ImageMetaData
{ {
Width = LittleEndian.ToUInt32 (header, 4), Width = file.ReadUInt32(),
Height = LittleEndian.ToUInt32 (header, 8), Height = file.ReadUInt32(),
BPP = 32, BPP = 32,
}; };
} }
public override ImageData Read (Stream stream, ImageMetaData info) public override ImageData Read (IBinaryStream file, ImageMetaData info)
{ {
var pixels = new byte[info.Width*info.Height*4]; var pixels = new byte[info.Width*info.Height*4];
stream.Position = 12; file.Position = 12;
if (pixels.Length != stream.Read (pixels, 0, pixels.Length)) if (pixels.Length != file.Read (pixels, 0, pixels.Length))
throw new EndOfStreamException(); throw new EndOfStreamException();
return ImageData.Create (info, PixelFormats.Bgra32, null, pixels); return ImageData.Create (info, PixelFormats.Bgra32, null, pixels);
} }

View File

@ -128,7 +128,7 @@ namespace GameRes.Formats.EmonEngine
lzss.Config.FrameInitPos = entry.LzssInitPos; lzss.Config.FrameInitPos = entry.LzssInitPos;
lzss.Read (data, part1_size, unpacked_size); lzss.Read (data, part1_size, unpacked_size);
} }
return new MemoryStream (data); return new BinMemoryStream (data, entry.Name);
} }
else else
{ {

View File

@ -583,10 +583,10 @@ namespace GameRes.Formats.Emote
Extensions = new string[0]; Extensions = new string[0];
} }
public override ImageMetaData ReadMetaData (Stream stream) public override ImageMetaData ReadMetaData (IBinaryStream stream)
{ {
stream.Position = 4; stream.Position = 4;
using (var reader = new BinaryReader (stream, Encoding.UTF8, true)) using (var reader = new BinaryReader (stream.AsStream, Encoding.UTF8, true))
{ {
var info = new PsbTexMetaData { BPP = 32 }; var info = new PsbTexMetaData { BPP = 32 };
info.DataOffset = reader.ReadInt32(); info.DataOffset = reader.ReadInt32();
@ -599,14 +599,14 @@ namespace GameRes.Formats.Emote
} }
} }
public override ImageData Read (Stream stream, ImageMetaData info) public override ImageData Read (IBinaryStream stream, ImageMetaData info)
{ {
var meta = (PsbTexMetaData)info; var meta = (PsbTexMetaData)info;
var pixels = new byte[meta.Width * meta.Height * 4]; var pixels = new byte[meta.Width * meta.Height * 4];
if ("RGBA8" == meta.TexType) if ("RGBA8" == meta.TexType)
ReadRgba8 (stream, meta, pixels); ReadRgba8 (stream.AsStream, meta, pixels);
else if ("RGBA4444" == meta.TexType) else if ("RGBA4444" == meta.TexType)
ReadRgba4444 (stream, meta, pixels); ReadRgba4444 (stream.AsStream, meta, pixels);
else else
throw new NotImplementedException (string.Format ("PSB texture format '{0}' not implemented", meta.TexType)); throw new NotImplementedException (string.Format ("PSB texture format '{0}' not implemented", meta.TexType));
return ImageData.Create (info, PixelFormats.Bgra32, null, pixels); return ImageData.Create (info, PixelFormats.Bgra32, null, pixels);

View File

@ -42,17 +42,15 @@ namespace GameRes.Formats.Entis
public override string Description { get { return "Entis engine compressed audio format"; } } public override string Description { get { return "Entis engine compressed audio format"; } }
public override uint Signature { get { return 0x69746e45u; } } // 'Enti' public override uint Signature { get { return 0x69746e45u; } } // 'Enti'
public override SoundInput TryOpen (Stream file) public override SoundInput TryOpen (IBinaryStream file)
{ {
byte[] header = new byte[0x40]; var header = file.ReadHeader (0x40);
if (header.Length != file.Read (header, 0, header.Length)) if (0x03000100 != header.ToUInt32 (8))
return null; return null;
if (0x03000100 != LittleEndian.ToUInt32 (header, 8)) if (!header.AsciiEqual (0x10, "Music Interleaved"))
return null;
if (!Binary.AsciiEqual (header, 0x10, "Music Interleaved"))
return null; return null;
return new MioInput (file); return new MioInput (file.AsStream);
} }
} }

View File

@ -149,17 +149,15 @@ namespace GameRes.Formats.Entis
public override string Description { get { return "Entis rasterized image format"; } } public override string Description { get { return "Entis rasterized image format"; } }
public override uint Signature { get { return 0x69746e45u; } } // 'Enti' public override uint Signature { get { return 0x69746e45u; } } // 'Enti'
public override ImageMetaData ReadMetaData (Stream stream) public override ImageMetaData ReadMetaData (IBinaryStream stream)
{ {
byte[] header = new byte[0x40]; var header = stream.ReadHeader (0x40);
if (header.Length != stream.Read (header, 0, header.Length)) if (0x03000100 != header.ToUInt32 (8))
return null; return null;
if (0x03000100 != LittleEndian.ToUInt32 (header, 8)) if (!header.AsciiEqual (0x10, "Entis Rasterized Image")
&& !header.AsciiEqual (0x10, "Moving Entis Image"))
return null; return null;
if (!Binary.AsciiEqual (header, 0x10, "Entis Rasterized Image") using (var reader = new EriFile (stream.AsStream))
&& !Binary.AsciiEqual (header, 0x10, "Moving Entis Image"))
return null;
using (var reader = new EriFile (stream))
{ {
var section = reader.ReadSection(); var section = reader.ReadSection();
if (section.Id != "Header " || section.Length <= 0) if (section.Id != "Header " || section.Length <= 0)
@ -240,7 +238,7 @@ namespace GameRes.Formats.Entis
} }
} }
public override ImageData Read (Stream stream, ImageMetaData info) public override ImageData Read (IBinaryStream stream, ImageMetaData info)
{ {
var reader = ReadImageData (stream, (EriMetaData)info); var reader = ReadImageData (stream, (EriMetaData)info);
return ImageData.Create (info, reader.Format, reader.Palette, reader.Data, reader.Stride); return ImageData.Create (info, reader.Format, reader.Palette, reader.Data, reader.Stride);
@ -261,11 +259,11 @@ namespace GameRes.Formats.Entis
return colors; return colors;
} }
internal EriReader ReadImageData (Stream stream, EriMetaData meta) internal EriReader ReadImageData (IBinaryStream stream, EriMetaData meta)
{ {
stream.Position = meta.StreamPos; stream.Position = meta.StreamPos;
Color[] palette = null; Color[] palette = null;
using (var input = new EriFile (stream)) using (var input = new EriFile (stream.AsStream))
{ {
for (;;) // ReadSection throws an exception in case of EOF for (;;) // ReadSection throws an exception in case of EOF
{ {
@ -276,13 +274,13 @@ namespace GameRes.Formats.Entis
break; break;
if ("Palette " == section.Id && meta.BPP <= 8 && section.Length <= 0x400) if ("Palette " == section.Id && meta.BPP <= 8 && section.Length <= 0x400)
{ {
palette = ReadPalette (stream, (int)section.Length); palette = ReadPalette (stream.AsStream, (int)section.Length);
continue; continue;
} }
input.BaseStream.Seek (section.Length, SeekOrigin.Current); input.BaseStream.Seek (section.Length, SeekOrigin.Current);
} }
} }
var reader = new EriReader (stream, meta, palette); var reader = new EriReader (stream.AsStream, meta, palette);
reader.DecodeImage(); reader.DecodeImage();
if (!string.IsNullOrEmpty (meta.Description)) if (!string.IsNullOrEmpty (meta.Description))
@ -298,7 +296,7 @@ namespace GameRes.Formats.Entis
throw new InvalidFormatException(); throw new InvalidFormatException();
ref_file = VFS.CombinePath (VFS.GetDirectoryName (meta.FileName), ref_file); ref_file = VFS.CombinePath (VFS.GetDirectoryName (meta.FileName), ref_file);
using (var ref_src = VFS.OpenSeekableStream (ref_file)) using (var ref_src = VFS.OpenBinaryStream (ref_file))
{ {
var ref_info = ReadMetaData (ref_src) as EriMetaData; var ref_info = ReadMetaData (ref_src) as EriMetaData;
if (null == ref_info) if (null == ref_info)

View File

@ -83,7 +83,7 @@ namespace GameRes.Formats.BGI
using (var decoder = new DscDecoder (input)) using (var decoder = new DscDecoder (input))
{ {
decoder.Unpack(); decoder.Unpack();
return new MemoryStream (decoder.Output); return new BinMemoryStream (decoder.Output, entry.Name);
} }
} }
return new ArcView.ArcStream (input, entry_offset, entry.Size); return new ArcView.ArcStream (input, entry_offset, entry.Size);

View File

@ -41,18 +41,16 @@ namespace GameRes.Formats.BGI
Extensions = new string[] { "" }; Extensions = new string[] { "" };
} }
public override SoundInput TryOpen (Stream file) public override SoundInput TryOpen (IBinaryStream file)
{ {
var header = new byte[8]; var header = file.ReadHeader (8);
if (8 != file.Read (header, 0, 8)) if (!header.AsciiEqual (4, "bw "))
return null; return null;
if (!Binary.AsciiEqual (header, 4, "bw ")) uint offset = header.ToUInt32 (0);
return null;
uint offset = LittleEndian.ToUInt32 (header, 0);
if (offset >= file.Length) if (offset >= file.Length)
return null; return null;
var input = new StreamRegion (file, offset); var input = new StreamRegion (file.AsStream, offset);
return new OggInput (input); return new OggInput (input);
// input is left undisposed in case of exception. // input is left undisposed in case of exception.
} }

View File

@ -51,29 +51,26 @@ namespace GameRes.Formats.BGI
throw new System.NotImplementedException ("BgiFormat.Write not implemented"); throw new System.NotImplementedException ("BgiFormat.Write not implemented");
} }
public override ImageMetaData ReadMetaData (Stream stream) public override ImageMetaData ReadMetaData (IBinaryStream stream)
{ {
using (var input = new ArcView.Reader (stream)) int width = stream.ReadInt16();
int height = stream.ReadInt16();
if (width <= 0 || height <= 0)
return null;
int bpp = stream.ReadInt32();
if (24 != bpp && 32 != bpp && 8 != bpp)
return null;
if (0 != stream.ReadInt64())
return null;
return new ImageMetaData
{ {
int width = input.ReadInt16(); Width = (uint)width,
int height = input.ReadInt16(); Height = (uint)height,
if (width <= 0 || height <= 0) BPP = bpp,
return null; };
int bpp = input.ReadInt32();
if (24 != bpp && 32 != bpp && 8 != bpp)
return null;
if (0 != input.ReadInt64())
return null;
return new ImageMetaData
{
Width = (uint)width,
Height = (uint)height,
BPP = bpp,
};
}
} }
public override ImageData Read (Stream stream, ImageMetaData info) public override ImageData Read (IBinaryStream stream, ImageMetaData info)
{ {
PixelFormat format; PixelFormat format;
if (24 == info.BPP) if (24 == info.BPP)
@ -119,33 +116,29 @@ namespace GameRes.Formats.BGI
throw new System.NotImplementedException ("BgiFormat.Write not implemented"); throw new System.NotImplementedException ("BgiFormat.Write not implemented");
} }
public override ImageMetaData ReadMetaData (Stream stream) public override ImageMetaData ReadMetaData (IBinaryStream stream)
{ {
var header = new byte[0x30]; var header = stream.ReadHeader (0x30);
if (header.Length != stream.Read (header, 0, header.Length)) if (!header.AsciiEqual ("CompressedBG___"))
return null;
if (!Binary.AsciiEqual (header, "CompressedBG___"))
return null; return null;
return new CbgMetaData return new CbgMetaData
{ {
Width = LittleEndian.ToUInt16 (header, 0x10), Width = header.ToUInt16 (0x10),
Height = LittleEndian.ToUInt16 (header, 0x12), Height = header.ToUInt16 (0x12),
BPP = LittleEndian.ToInt32 (header, 0x14), BPP = header.ToInt32 (0x14),
IntermediateLength = LittleEndian.ToInt32 (header, 0x20), IntermediateLength = header.ToInt32 (0x20),
Key = LittleEndian.ToUInt32 (header, 0x24), Key = header.ToUInt32 (0x24),
EncLength = LittleEndian.ToInt32 (header, 0x28), EncLength = header.ToInt32 (0x28),
CheckSum = header[0x2C], CheckSum = header[0x2C],
CheckXor = header[0x2D], CheckXor = header[0x2D],
Version = LittleEndian.ToUInt16 (header, 0x2E), Version = header.ToUInt16 (0x2E),
}; };
} }
public override ImageData Read (Stream stream, ImageMetaData info) public override ImageData Read (IBinaryStream stream, ImageMetaData info)
{ {
var meta = info as CbgMetaData; var meta = (CbgMetaData)info as CbgMetaData;
if (null == meta) using (var reader = new CbgReader (stream.AsStream, meta))
throw new ArgumentException ("CompressedBGFormat.Read should be supplied with CbgMetaData", "info");
using (var reader = new CbgReader (stream, meta))
{ {
reader.Unpack(); reader.Unpack();
return ImageData.Create (meta, reader.Format, null, reader.Data, reader.Stride); return ImageData.Create (meta, reader.Format, null, reader.Data, reader.Stride);

View File

@ -51,7 +51,7 @@ namespace GameRes.Formats.Eushully
Signatures = new uint[] { 0x46474341, 0 }; Signatures = new uint[] { 0x46474341, 0 };
} }
public override ImageMetaData ReadMetaData (Stream stream) public override ImageMetaData ReadMetaData (IBinaryStream stream)
{ {
var header = new byte[0x20]; var header = new byte[0x20];
if (0x18 != stream.Read (header, 0, 0x18)) if (0x18 != stream.Read (header, 0, 0x18))
@ -64,7 +64,7 @@ namespace GameRes.Formats.Eushully
return null; return null;
int unpacked_size = LittleEndian.ToInt32 (header, 0x10); int unpacked_size = LittleEndian.ToInt32 (header, 0x10);
int packed_size = LittleEndian.ToInt32 (header, 0x14); int packed_size = LittleEndian.ToInt32 (header, 0x14);
using (var unpacked = AgfReader.OpenSection (stream, unpacked_size, packed_size)) using (var unpacked = AgfReader.OpenSection (stream.AsStream, unpacked_size, packed_size))
using (var reader = new BinaryReader (unpacked)) using (var reader = new BinaryReader (unpacked))
{ {
if (0x20 != reader.Read (header, 0, 0x20)) if (0x20 != reader.Read (header, 0, 0x20))
@ -101,7 +101,7 @@ namespace GameRes.Formats.Eushully
return palette; return palette;
} }
public override ImageData Read (Stream stream, ImageMetaData info) public override ImageData Read (IBinaryStream stream, ImageMetaData info)
{ {
using (var reader = new AgfReader (stream, (AgfMetaData)info)) using (var reader = new AgfReader (stream, (AgfMetaData)info))
{ {
@ -118,7 +118,7 @@ namespace GameRes.Formats.Eushully
internal sealed class AgfReader : IDisposable internal sealed class AgfReader : IDisposable
{ {
BinaryReader m_input; IBinaryStream m_input;
byte[] m_output; byte[] m_output;
int m_width; int m_width;
int m_height; int m_height;
@ -129,9 +129,9 @@ namespace GameRes.Formats.Eushully
public PixelFormat Format { get; private set; } public PixelFormat Format { get; private set; }
public byte[] Data { get { return m_output; } } public byte[] Data { get { return m_output; } }
public AgfReader (Stream input, AgfMetaData info) public AgfReader (IBinaryStream input, AgfMetaData info)
{ {
m_input = new ArcView.Reader (input); m_input = input;
m_bpp = info.BPP; m_bpp = info.BPP;
m_source_bpp = info.SourceBPP; m_source_bpp = info.SourceBPP;
input.Position = info.DataOffset; input.Position = info.DataOffset;
@ -155,15 +155,15 @@ namespace GameRes.Formats.Eushully
m_input.ReadInt32(); m_input.ReadInt32();
int data_size = m_input.ReadInt32(); int data_size = m_input.ReadInt32();
int packed_size = m_input.ReadInt32(); int packed_size = m_input.ReadInt32();
var data_pos = m_input.BaseStream.Position; var data_pos = m_input.Position;
var bmp_data = new byte[data_size]; var bmp_data = new byte[data_size];
using (var unpacked = OpenSection (m_input.BaseStream, data_size, packed_size)) using (var unpacked = OpenSection (m_input.AsStream, data_size, packed_size))
if (data_size != unpacked.Read (bmp_data, 0, data_size)) if (data_size != unpacked.Read (bmp_data, 0, data_size))
throw new EndOfStreamException(); throw new EndOfStreamException();
byte[] alpha = null; byte[] alpha = null;
if (32 == m_bpp) if (32 == m_bpp)
{ {
m_input.BaseStream.Position = data_pos + packed_size; m_input.Position = data_pos + packed_size;
alpha = ReadAlphaChannel(); alpha = ReadAlphaChannel();
if (null == alpha) if (null == alpha)
m_bpp = 24; m_bpp = 24;
@ -231,21 +231,15 @@ namespace GameRes.Formats.Eushully
if (m_width*m_height != unpacked_size) if (m_width*m_height != unpacked_size)
return null; return null;
var alpha = new byte[unpacked_size]; var alpha = new byte[unpacked_size];
using (var unpacked = OpenSection (m_input.BaseStream, unpacked_size, packed_size)) using (var unpacked = OpenSection (m_input.AsStream, unpacked_size, packed_size))
if (unpacked_size != unpacked.Read (alpha, 0, unpacked_size)) if (unpacked_size != unpacked.Read (alpha, 0, unpacked_size))
return null; return null;
return alpha; return alpha;
} }
#region IDisposable methods #region IDisposable methods
bool _disposed = false;
public void Dispose () public void Dispose ()
{ {
if (!_disposed)
{
m_input.Dispose();
_disposed = true;
}
} }
#endregion #endregion
} }

View File

@ -53,7 +53,7 @@ namespace GameRes.Formats.Eushully
Extensions = new string[] { "gpcf" }; // made-up, real files have no extension Extensions = new string[] { "gpcf" }; // made-up, real files have no extension
} }
public override ImageMetaData ReadMetaData (Stream stream) public override ImageMetaData ReadMetaData (IBinaryStream stream)
{ {
int alpha_channel = stream.ReadByte(); int alpha_channel = stream.ReadByte();
int method = stream.ReadByte(); int method = stream.ReadByte();
@ -64,30 +64,28 @@ namespace GameRes.Formats.Eushully
|| align1 < 0 || align1 > 4 || align2 < 0 || align2 > 4 || align1 < 0 || align1 > 4 || align2 < 0 || align2 > 4
|| bpp <= 0 || !(bpp <= 16 || 24 == bpp || 32 == bpp)) || bpp <= 0 || !(bpp <= 16 || 24 == bpp || 32 == bpp))
return null; return null;
using (var reader = new ArcView.Reader (stream))
int palette_size = stream.ReadInt32();
uint width = stream.ReadUInt16();
uint height = stream.ReadUInt16();
if (palette_size <= 0 || 0 == width || 0 == height || palette_size >= stream.Length)
return null;
if (bpp <= 8 && palette_size > 0x100)
return null;
return new GpMetaData
{ {
int palette_size = reader.ReadInt32(); Width = width,
uint width = reader.ReadUInt16(); Height = height,
uint height = reader.ReadUInt16(); BPP = bpp,
if (palette_size <= 0 || 0 == width || 0 == height || palette_size >= stream.Length) HasAlpha = alpha_channel != 0,
return null; Method = method,
if (bpp <= 8 && palette_size > 0x100) ElementSize = align1,
return null; PixelsPerElement = align2,
return new GpMetaData PaletteSize = palette_size,
{ };
Width = width,
Height = height,
BPP = bpp,
HasAlpha = alpha_channel != 0,
Method = method,
ElementSize = align1,
PixelsPerElement = align2,
PaletteSize = palette_size,
};
}
} }
public override ImageData Read (Stream stream, ImageMetaData info) public override ImageData Read (IBinaryStream stream, ImageMetaData info)
{ {
var meta = (GpMetaData)info; var meta = (GpMetaData)info;
using (var reader = new GpReader (stream, meta)) using (var reader = new GpReader (stream, meta))
@ -105,7 +103,7 @@ namespace GameRes.Formats.Eushully
internal sealed class GpReader : IDisposable internal sealed class GpReader : IDisposable
{ {
BinaryReader m_input; IBinaryStream m_input;
GpMetaData m_info; GpMetaData m_info;
int m_width; int m_width;
int m_height; int m_height;
@ -115,17 +113,17 @@ namespace GameRes.Formats.Eushully
public byte[] Data { get; private set; } public byte[] Data { get; private set; }
public int Stride { get; private set; } public int Stride { get; private set; }
public GpReader (Stream input, GpMetaData info) public GpReader (IBinaryStream input, GpMetaData info)
{ {
m_info = info; m_info = info;
m_width = (int)m_info.Width; m_width = (int)m_info.Width;
m_height = (int)m_info.Height; m_height = (int)m_info.Height;
m_input = new ArcView.Reader (input); m_input = input;
} }
public void Unpack () public void Unpack ()
{ {
m_input.BaseStream.Position = 0xD; m_input.Position = 0xD;
switch (m_info.Method) switch (m_info.Method)
{ {
case 0: UnpackV0(); break; case 0: UnpackV0(); break;
@ -293,8 +291,8 @@ namespace GameRes.Formats.Eushully
int i = 3; int i = 3;
while (i < Data.Length) while (i < Data.Length)
{ {
byte alpha = m_input.ReadByte(); byte alpha = m_input.ReadUInt8();
int count = m_input.ReadByte(); int count = m_input.ReadUInt8();
for (int j = 0; j < count && i < Data.Length; ++j) for (int j = 0; j < count && i < Data.Length; ++j)
{ {
Data[i] = alpha; Data[i] = alpha;
@ -305,14 +303,8 @@ namespace GameRes.Formats.Eushully
} }
#region IDisposable Members #region IDisposable Members
bool _disposed = false;
public void Dispose () public void Dispose ()
{ {
if (!_disposed)
{
m_input.Dispose();
_disposed = true;
}
} }
#endregion #endregion
} }

View File

@ -69,29 +69,26 @@ namespace GameRes.Formats.ExHibit
set { KnownKeys = ((GyuMap)value).KnownKeys; } set { KnownKeys = ((GyuMap)value).KnownKeys; }
} }
public override ImageMetaData ReadMetaData (Stream stream) public override ImageMetaData ReadMetaData (IBinaryStream stream)
{ {
using (var reader = new ArcView.Reader (stream)) stream.Position = 4;
return new GyuMetaData
{ {
reader.ReadInt32(); Flags = stream.ReadUInt16(),
return new GyuMetaData CompressionMode = stream.ReadUInt16(),
{ Key = stream.ReadUInt32(),
Flags = reader.ReadUInt16(), BPP = stream.ReadInt32(),
CompressionMode = reader.ReadUInt16(), Width = stream.ReadUInt32(),
Key = reader.ReadUInt32(), Height = stream.ReadUInt32(),
BPP = reader.ReadInt32(), DataSize = stream.ReadInt32(),
Width = reader.ReadUInt32(), AlphaSize = stream.ReadInt32(),
Height = reader.ReadUInt32(), PaletteSize = stream.ReadInt32(),
DataSize = reader.ReadInt32(), };
AlphaSize = reader.ReadInt32(),
PaletteSize = reader.ReadInt32(),
};
}
} }
IDictionary<int, uint> CurrentMap = null; IDictionary<int, uint> CurrentMap = null;
public override ImageData Read (Stream stream, ImageMetaData info) public override ImageData Read (IBinaryStream stream, ImageMetaData info)
{ {
var meta = (GyuMetaData)info; var meta = (GyuMetaData)info;
if (0 == meta.Key) if (0 == meta.Key)
@ -111,7 +108,7 @@ namespace GameRes.Formats.ExHibit
throw new UnknownEncryptionScheme ("Unknown image encryption key"); throw new UnknownEncryptionScheme ("Unknown image encryption key");
} }
} }
var reader = new GyuReader (stream, meta); var reader = new GyuReader (stream.AsStream, meta);
reader.Unpack(); reader.Unpack();
return ImageData.CreateFlipped (meta, reader.Format, reader.Palette, reader.Data, reader.Stride); return ImageData.CreateFlipped (meta, reader.Format, reader.Palette, reader.Data, reader.Stride);
} }

View File

@ -114,7 +114,7 @@ namespace GameRes.Formats.FC01
var data = arc.File.View.ReadBytes (entry.Offset, entry.Size); var data = arc.File.View.ReadBytes (entry.Offset, entry.Size);
var reader = new MrgDecoder (data); var reader = new MrgDecoder (data);
reader.Unpack(); reader.Unpack();
input = new MemoryStream (reader.Data); input = new BinMemoryStream (reader.Data, entry.Name);
} }
else else
input = arc.File.CreateStream (entry.Offset, entry.Size); input = arc.File.CreateStream (entry.Offset, entry.Size);
@ -124,7 +124,7 @@ namespace GameRes.Formats.FC01
using (var reader = new MrgLzssReader (input, (int)input.Length, (int)packed_entry.UnpackedSize)) using (var reader = new MrgLzssReader (input, (int)input.Length, (int)packed_entry.UnpackedSize))
{ {
reader.Unpack(); reader.Unpack();
return new MemoryStream (reader.Data); return new BinMemoryStream (reader.Data, entry.Name);
} }
} }
return input; return input;
@ -240,14 +240,14 @@ namespace GameRes.Formats.FC01
{ {
var data = arc.File.View.ReadBytes (entry.Offset, entry.Size); var data = arc.File.View.ReadBytes (entry.Offset, entry.Size);
Decrypt (data, 0, data.Length, mrg_entry.Key, mrg_entry.ArcKey); Decrypt (data, 0, data.Length, mrg_entry.Key, mrg_entry.ArcKey);
input = new MemoryStream (data); input = new BinMemoryStream (data, entry.Name);
} }
else if (mrg_entry.Method >= 2) else if (mrg_entry.Method >= 2)
{ {
var data = arc.File.View.ReadBytes (entry.Offset, entry.Size); var data = arc.File.View.ReadBytes (entry.Offset, entry.Size);
var reader = new MrgDecoder (data); var reader = new MrgDecoder (data);
reader.Unpack(); reader.Unpack();
input = new MemoryStream (reader.Data); input = new BinMemoryStream (reader.Data, entry.Name);
} }
else else
input = arc.File.CreateStream (entry.Offset, entry.Size); input = arc.File.CreateStream (entry.Offset, entry.Size);
@ -257,7 +257,7 @@ namespace GameRes.Formats.FC01
using (var reader = new MrgLzssReader (input, (int)input.Length, (int)mrg_entry.UnpackedSize)) using (var reader = new MrgLzssReader (input, (int)input.Length, (int)mrg_entry.UnpackedSize))
{ {
reader.Unpack(); reader.Unpack();
return new MemoryStream (reader.Data); return new BinMemoryStream (reader.Data, entry.Name);
} }
} }
return input; return input;

View File

@ -45,20 +45,18 @@ namespace GameRes.Formats.FC01
public override string Description { get { return "F&C Co. image format"; } } public override string Description { get { return "F&C Co. image format"; } }
public override uint Signature { get { return 0x20444341; } } // 'ACD' public override uint Signature { get { return 0x20444341; } } // 'ACD'
public override ImageMetaData ReadMetaData (Stream stream) public override ImageMetaData ReadMetaData (IBinaryStream stream)
{ {
var header = new byte[0x1c]; var header = stream.ReadHeader (0x1c);
if (header.Length != stream.Read (header, 0, header.Length)) int header_size = header.ToInt32 (8);
return null; if (!header.AsciiEqual (4, "1.00") || header_size < 0x1c)
int header_size = LittleEndian.ToInt32 (header, 8);
if (!Binary.AsciiEqual (header, 4, "1.00") || header_size < 0x1c)
throw new NotSupportedException ("Not supported ACD image version"); throw new NotSupportedException ("Not supported ACD image version");
int packed_size = LittleEndian.ToInt32 (header, 0x0C); int packed_size = header.ToInt32 (0x0C);
int unpacked_size = LittleEndian.ToInt32 (header, 0x10); int unpacked_size = header.ToInt32 (0x10);
return new AcdMetaData return new AcdMetaData
{ {
Width = LittleEndian.ToUInt32 (header, 0x14), Width = header.ToUInt32 (0x14),
Height = LittleEndian.ToUInt32 (header, 0x18), Height = header.ToUInt32 (0x18),
BPP = 24, BPP = 24,
DataOffset = header_size, DataOffset = header_size,
PackedSize = packed_size, PackedSize = packed_size,
@ -66,14 +64,12 @@ namespace GameRes.Formats.FC01
}; };
} }
public override ImageData Read (Stream stream, ImageMetaData info) public override ImageData Read (IBinaryStream stream, ImageMetaData info)
{ {
var meta = info as AcdMetaData; var meta = (AcdMetaData)info;
if (null == meta)
throw new ArgumentException ("AcdFormat.Read should be supplied with AcdMetaData", "info");
stream.Position = meta.DataOffset; stream.Position = meta.DataOffset;
using (var reader = new MrgLzssReader (stream, meta.PackedSize, meta.UnpackedSize)) using (var reader = new MrgLzssReader (stream.AsStream, meta.PackedSize, meta.UnpackedSize))
{ {
reader.Unpack(); reader.Unpack();
var decoder = new AcdDecoder (reader.Data, meta); var decoder = new AcdDecoder (reader.Data, meta);

View File

@ -45,20 +45,18 @@ namespace GameRes.Formats.FC01
public override string Description { get { return "F&C Co. image format"; } } public override string Description { get { return "F&C Co. image format"; } }
public override uint Signature { get { return 0x204D4C43; } } // 'CLM' public override uint Signature { get { return 0x204D4C43; } } // 'CLM'
public override ImageMetaData ReadMetaData (Stream stream) public override ImageMetaData ReadMetaData (IBinaryStream stream)
{ {
var header = new byte[0x40]; var header = stream.ReadHeader (0x40);
if (header.Length != stream.Read (header, 0, header.Length)) if (!header.AsciiEqual (4, "1.00"))
return null; return null;
if (!Binary.AsciiEqual (header, 4, "1.00")) uint data_offset = header.ToUInt32 (0x10);
return null;
uint data_offset = LittleEndian.ToUInt32 (header, 0x10);
if (data_offset < 0x40) if (data_offset < 0x40)
return null; return null;
uint width = LittleEndian.ToUInt32 (header, 0x1C); uint width = header.ToUInt32 (0x1C);
uint height = LittleEndian.ToUInt32 (header, 0x20); uint height = header.ToUInt32 (0x20);
int bpp = LittleEndian.ToInt32 (header, 0x24); int bpp = header.ToInt32 (0x24);
int unpacked_size = LittleEndian.ToInt32 (header, 0x28); int unpacked_size = header.ToInt32 (0x28);
return new ClmMetaData return new ClmMetaData
{ {
Width = width, Width = width,
@ -69,7 +67,7 @@ namespace GameRes.Formats.FC01
}; };
} }
public override ImageData Read (Stream stream, ImageMetaData info) public override ImageData Read (IBinaryStream stream, ImageMetaData info)
{ {
var meta = (ClmMetaData)info; var meta = (ClmMetaData)info;
stream.Position = meta.DataOffset; stream.Position = meta.DataOffset;
@ -78,7 +76,7 @@ namespace GameRes.Formats.FC01
if (8 == meta.BPP) if (8 == meta.BPP)
{ {
format = PixelFormats.Indexed8; format = PixelFormats.Indexed8;
palette = ReadPalette (stream); palette = ReadPalette (stream.AsStream);
} }
else if (24 == meta.BPP) else if (24 == meta.BPP)
format = PixelFormats.Bgr24; format = PixelFormats.Bgr24;
@ -87,7 +85,7 @@ namespace GameRes.Formats.FC01
else else
throw new NotSupportedException ("Not supported CLM color depth"); throw new NotSupportedException ("Not supported CLM color depth");
int packed_size = (int)(stream.Length - stream.Position); int packed_size = (int)(stream.Length - stream.Position);
using (var reader = new MrgLzssReader (stream, packed_size, meta.UnpackedSize)) using (var reader = new MrgLzssReader (stream.AsStream, packed_size, meta.UnpackedSize))
{ {
reader.Unpack(); reader.Unpack();
return ImageData.Create (info, format, palette, reader.Data); return ImageData.Create (info, format, palette, reader.Data);

View File

@ -67,31 +67,29 @@ namespace GameRes.Formats.FC01
set { KnownKeys = ((McgScheme)value).KnownKeys; } set { KnownKeys = ((McgScheme)value).KnownKeys; }
} }
public override ImageMetaData ReadMetaData (Stream stream) public override ImageMetaData ReadMetaData (IBinaryStream stream)
{ {
byte[] header = new byte[0x40]; var header = stream.ReadHeader (0x40);
if (header.Length != stream.Read (header, 0, header.Length))
return null;
if (header[5] != '.') if (header[5] != '.')
return null; return null;
int version = header[4] * 100 + header[6] * 10 + header[7] - 0x14D0; int version = header[4] * 100 + header[6] * 10 + header[7] - 0x14D0;
if (version != 200 && version != 101) if (version != 200 && version != 101)
throw new NotSupportedException ("Not supported MCG format version"); throw new NotSupportedException ("Not supported MCG format version");
int header_size = LittleEndian.ToInt32 (header, 0x10); int header_size = header.ToInt32 (0x10);
if (header_size < 0x40) if (header_size < 0x40)
return null; return null;
int bpp = LittleEndian.ToInt32 (header, 0x24); int bpp = header.ToInt32 (0x24);
if (24 != bpp) if (24 != bpp)
throw new NotSupportedException ("Not supported MCG image bitdepth"); throw new NotSupportedException ("Not supported MCG image bitdepth");
return new McgMetaData return new McgMetaData
{ {
Width = LittleEndian.ToUInt32 (header, 0x1c), Width = header.ToUInt32 (0x1c),
Height = LittleEndian.ToUInt32 (header, 0x20), Height = header.ToUInt32 (0x20),
OffsetX = LittleEndian.ToInt32 (header, 0x14), OffsetX = header.ToInt32 (0x14),
OffsetY = LittleEndian.ToInt32 (header, 0x18), OffsetY = header.ToInt32 (0x18),
BPP = bpp, BPP = bpp,
DataOffset = header_size, DataOffset = header_size,
PackedSize = LittleEndian.ToInt32 (header, 0x38) - header_size, PackedSize = header.ToInt32 (0x38) - header_size,
Version = version, Version = version,
}; };
} }
@ -99,7 +97,7 @@ namespace GameRes.Formats.FC01
// cache key value so that dialog does not pop up on every file accessed. // cache key value so that dialog does not pop up on every file accessed.
byte? LastKey = null; byte? LastKey = null;
public override ImageData Read (Stream stream, ImageMetaData info) public override ImageData Read (IBinaryStream stream, ImageMetaData info)
{ {
var meta = (McgMetaData)info; var meta = (McgMetaData)info;
byte key = Settings.Default.MCGLastKey; byte key = Settings.Default.MCGLastKey;
@ -113,7 +111,7 @@ namespace GameRes.Formats.FC01
else else
key = LastKey.Value; key = LastKey.Value;
} }
var reader = new McgDecoder (stream, meta, key); var reader = new McgDecoder (stream.AsStream, meta, key);
reader.Unpack(); reader.Unpack();
if (reader.Key != 0) if (reader.Key != 0)
LastKey = reader.Key; LastKey = reader.Key;

View File

@ -80,7 +80,7 @@ namespace GameRes.Formats.FVP
using (var decoder = new LzwDecoder (input, unpacked_size)) using (var decoder = new LzwDecoder (input, unpacked_size))
{ {
decoder.Unpack(); decoder.Unpack();
return new MemoryStream (decoder.Output); return new BinMemoryStream (decoder.Output, entry.Name);
} }
} }
} }

View File

@ -47,28 +47,26 @@ namespace GameRes.Formats.FVP
public override string Description { get { return "Favorite View Point image format"; } } public override string Description { get { return "Favorite View Point image format"; } }
public override uint Signature { get { return 0x31637A68; } } // 'HZC1' public override uint Signature { get { return 0x31637A68; } } // 'HZC1'
public override ImageMetaData ReadMetaData (Stream stream) public override ImageMetaData ReadMetaData (IBinaryStream stream)
{ {
var header = new byte[0x2C]; var header = stream.ReadHeader (0x2C);
if (header.Length != stream.Read (header, 0, header.Length)) if (!header.AsciiEqual (0xC, "NVSG"))
return null; return null;
if (!Binary.AsciiEqual (header, 0xC, "NVSG")) int type = header.ToUInt16 (0x12);
return null;
int type = LittleEndian.ToUInt16 (header, 0x12);
return new HzcMetaData return new HzcMetaData
{ {
Width = LittleEndian.ToUInt16 (header, 0x14), Width = header.ToUInt16 (0x14),
Height = LittleEndian.ToUInt16 (header, 0x16), Height = header.ToUInt16 (0x16),
OffsetX = LittleEndian.ToInt16 (header, 0x18), OffsetX = header.ToInt16 (0x18),
OffsetY = LittleEndian.ToInt16 (header, 0x1A), OffsetY = header.ToInt16 (0x1A),
BPP = 0 == type ? 24 : type > 2 ? 8 : 32, BPP = 0 == type ? 24 : type > 2 ? 8 : 32,
Type = type, Type = type,
UnpackedSize = LittleEndian.ToInt32 (header, 4), UnpackedSize = header.ToInt32 (4),
HeaderSize = LittleEndian.ToInt32 (header, 8), HeaderSize = header.ToInt32 (8),
}; };
} }
public override ImageData Read (Stream stream, ImageMetaData info) public override ImageData Read (IBinaryStream stream, ImageMetaData info)
{ {
var meta = (HzcMetaData)info; var meta = (HzcMetaData)info;
BitmapPalette palette = null; BitmapPalette palette = null;
@ -90,7 +88,7 @@ namespace GameRes.Formats.FVP
} }
} }
stream.Position = 12 + meta.HeaderSize; stream.Position = 12 + meta.HeaderSize;
using (var z = new ZLibStream (stream, CompressionMode.Decompress, true)) using (var z = new ZLibStream (stream.AsStream, CompressionMode.Decompress, true))
{ {
var pixels = new byte[stride * (int)meta.Height]; var pixels = new byte[stride * (int)meta.Height];
if (pixels.Length != z.Read (pixels, 0, pixels.Length)) if (pixels.Length != z.Read (pixels, 0, pixels.Length))

View File

@ -92,7 +92,7 @@ namespace GameRes.Formats.Ffa
using (var reader = new LzssReader (input, packed, unpacked)) using (var reader = new LzssReader (input, packed, unpacked))
{ {
reader.Unpack(); reader.Unpack();
return new MemoryStream (reader.Data); return new BinMemoryStream (reader.Data, entry.Name);
} }
} }
} }

View File

@ -39,18 +39,9 @@ namespace GameRes.Formats.Ffa
public override string Description { get { return "FFA System wave audio format"; } } public override string Description { get { return "FFA System wave audio format"; } }
public override uint Signature { get { return 0; } } public override uint Signature { get { return 0; } }
private static int ReadInt32 (Stream file) public override SoundInput TryOpen (IBinaryStream file)
{ {
int dword = file.ReadByte(); int packed = file.ReadInt32();
dword |= file.ReadByte() << 8;
dword |= file.ReadByte() << 16;
dword |= file.ReadByte() << 24;
return dword;
}
public override SoundInput TryOpen (Stream file)
{
int packed = ReadInt32 (file);
if (packed < 0) if (packed < 0)
return null; return null;
byte[] input; byte[] input;
@ -58,10 +49,10 @@ namespace GameRes.Formats.Ffa
{ {
if ((packed + 8) != file.Length) if ((packed + 8) != file.Length)
return null; return null;
int unpacked = ReadInt32 (file); int unpacked = file.ReadInt32();
if (unpacked <= 0) if (unpacked <= 0)
return null; return null;
using (var reader = new LzssReader (file, packed, unpacked)) using (var reader = new LzssReader (file.AsStream, packed, unpacked))
{ {
reader.Unpack(); reader.Unpack();
if (Binary.AsciiEqual (reader.Data, 0, "RIFF")) if (Binary.AsciiEqual (reader.Data, 0, "RIFF"))
@ -75,7 +66,7 @@ namespace GameRes.Formats.Ffa
} }
else else
{ {
if (0x46464952 != ReadInt32 (file)) // 'RIFF' if (0x46464952 != file.ReadInt32()) // 'RIFF'
return null; return null;
file.Position = 0; file.Position = 0;
input = new byte[file.Length]; input = new byte[file.Length];

View File

@ -36,7 +36,7 @@ namespace GameRes.Formats.Ffa
public override string Description { get { return "FFA System PCM audio format"; } } public override string Description { get { return "FFA System PCM audio format"; } }
public override uint Signature { get { return 0x4D435041; } } // 'APCM' public override uint Signature { get { return 0x4D435041; } } // 'APCM'
public override SoundInput TryOpen (Stream file) public override SoundInput TryOpen (IBinaryStream file)
{ {
return new Wa2Input (file); return new Wa2Input (file);
} }
@ -51,27 +51,25 @@ namespace GameRes.Formats.Ffa
get { return (int)Format.AverageBytesPerSecond * 8; } get { return (int)Format.AverageBytesPerSecond * 8; }
} }
public Wa2Input (Stream file) : base (null) public Wa2Input (IBinaryStream file) : base (null)
{ {
var header = new byte[0x2C]; var header = file.ReadHeader (0x2C);
if (header.Length != file.Read (header, 0, header.Length)) if (!header.AsciiEqual (8, "WAVEfmt "))
throw new EndOfStreamException();
if (!Binary.AsciiEqual (header, 8, "WAVEfmt "))
throw new InvalidFormatException(); throw new InvalidFormatException();
var format = new WaveFormat(); var format = new WaveFormat();
format.FormatTag = LittleEndian.ToUInt16 (header, 0x14); format.FormatTag = header.ToUInt16 (0x14);
format.Channels = LittleEndian.ToUInt16 (header, 0x16); format.Channels = header.ToUInt16 (0x16);
format.SamplesPerSecond = LittleEndian.ToUInt32 (header, 0x18); format.SamplesPerSecond = header.ToUInt32 (0x18);
format.AverageBytesPerSecond = LittleEndian.ToUInt32 (header, 0x1C); format.AverageBytesPerSecond = header.ToUInt32 (0x1C);
format.BlockAlign = LittleEndian.ToUInt16 (header, 0x20); format.BlockAlign = header.ToUInt16 (0x20);
format.BitsPerSample = LittleEndian.ToUInt16 (header, 0x22); format.BitsPerSample = header.ToUInt16 (0x22);
format.ExtraSize = 0; format.ExtraSize = 0;
this.Format = format; this.Format = format;
uint pcm_size = LittleEndian.ToUInt32 (header, 0x28); uint pcm_size = header.ToUInt32 (0x28);
var pcm = new byte[pcm_size]; var pcm = new byte[pcm_size];
Decode (file, pcm); Decode (file.AsStream, pcm);
Source = new MemoryStream (pcm); Source = new MemoryStream (pcm);
this.PcmSize = pcm_size; this.PcmSize = pcm_size;
file.Dispose(); file.Dispose();

View File

@ -59,40 +59,37 @@ namespace GameRes.Formats.Ffa
throw new NotImplementedException ("Pt1Format.Write not implemented"); throw new NotImplementedException ("Pt1Format.Write not implemented");
} }
public override ImageMetaData ReadMetaData (Stream stream) public override ImageMetaData ReadMetaData (IBinaryStream file)
{ {
using (var input = new ArcView.Reader (stream)) int type = file.ReadInt32();
{ if (type < 0 || type > 3)
int type = input.ReadInt32(); return null;
if (type < 0 || type > 3) if (-1 != file.ReadInt32())
return null; return null;
if (-1 != input.ReadInt32()) int x = file.ReadInt32();
return null; int y = file.ReadInt32();
int x = input.ReadInt32(); uint width = file.ReadUInt32();
int y = input.ReadInt32(); uint height = file.ReadUInt32();
uint width = input.ReadUInt32(); uint comp_size = file.ReadUInt32();
uint height = input.ReadUInt32(); uint uncomp_size = file.ReadUInt32();
uint comp_size = input.ReadUInt32(); if (uncomp_size != width*height*3u)
uint uncomp_size = input.ReadUInt32(); return null;
if (uncomp_size != width*height*3u) return new Pt1MetaData {
return null; Width = width,
return new Pt1MetaData { Height = height,
Width = width, OffsetX = x,
Height = height, OffsetY = y,
OffsetX = x, BPP = 3 == type ? 32 : 24,
OffsetY = y, Type = type,
BPP = 3 == type ? 32 : 24, PackedSize = comp_size,
Type = type, UnpackedSize = uncomp_size
PackedSize = comp_size, };
UnpackedSize = uncomp_size
};
}
} }
public override ImageData Read (Stream stream, ImageMetaData info) public override ImageData Read (IBinaryStream stream, ImageMetaData info)
{ {
var meta = (Pt1MetaData)info; var meta = (Pt1MetaData)info;
var reader = new Reader (stream, meta); var reader = new Reader (stream.AsStream, meta);
reader.Unpack(); reader.Unpack();
return ImageData.Create (meta, reader.Format, null, reader.Data); return ImageData.Create (meta, reader.Format, null, reader.Data);
} }

View File

@ -125,7 +125,7 @@ namespace GameRes.Formats.G2
using (input) using (input)
using (var reader = new GceReader (input, (int)pentry.UnpackedSize)) using (var reader = new GceReader (input, (int)pentry.UnpackedSize))
{ {
return new MemoryStream (reader.Data); return new BinMemoryStream (reader.Data, entry.Name);
} }
} }
} }

View File

@ -42,25 +42,23 @@ namespace GameRes.Formats.G2
Extensions = new string[] { "argb", "arg" }; Extensions = new string[] { "argb", "arg" };
} }
public override ImageMetaData ReadMetaData (Stream stream) public override ImageMetaData ReadMetaData (IBinaryStream stream)
{ {
var header = new byte[0x10]; stream.Position = 4;
if (header.Length != stream.Read (header, 0, header.Length)) uint bitmap = stream.ReadUInt32();
return null;
uint bitmap = LittleEndian.ToUInt32 (header, 4);
if (0x08080808 != bitmap) if (0x08080808 != bitmap)
return null; return null;
return new ImageMetaData return new ImageMetaData
{ {
Width = LittleEndian.ToUInt32 (header, 8), Width = stream.ReadUInt32(),
Height = LittleEndian.ToUInt32 (header, 12), Height = stream.ReadUInt32(),
BPP = 32, BPP = 32,
}; };
} }
public override ImageData Read (Stream stream, ImageMetaData info) public override ImageData Read (IBinaryStream stream, ImageMetaData info)
{ {
stream.Seek (0x10, SeekOrigin.Current); stream.Position = 0x10;
var pixels = new byte[info.Width*info.Height*4]; var pixels = new byte[info.Width*info.Height*4];
if (pixels.Length != stream.Read (pixels, 0, pixels.Length)) if (pixels.Length != stream.Read (pixels, 0, pixels.Length))
throw new EndOfStreamException(); throw new EndOfStreamException();

Some files were not shown because too many files have changed in this diff Show More