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

@@ -107,7 +107,7 @@ namespace GameRes.Formats.Softpal
}
}
}
return new MemoryStream (data);
return new BinMemoryStream (data, entry.Name);
}
}

View File

@@ -31,7 +31,7 @@ using GameRes.Utility;
namespace GameRes.Formats.Softpal
{
[Export(typeof(AudioFormat))]
public class BgmAudio : OggAudio
public class BgmAudio : AudioFormat
{
public override string Tag { get { return "BGM/SOFTPAL"; } }
public override string Description { get { return "Softpal BGM format (Ogg/Vorbis)"; } }
@@ -42,14 +42,12 @@ namespace GameRes.Formats.Softpal
Extensions = new string[] { "ogg" };
}
public override SoundInput TryOpen (Stream file)
public override SoundInput TryOpen (IBinaryStream file)
{
var header = new byte[0x10]; // header contains music loop timing
if (0x10 != file.Read (header, 0, 0x10))
var header = file.ReadHeader (0x10); // header contains music loop timing
if (!header.AsciiEqual (0xC, "OggS"))
return null;
if (!Binary.AsciiEqual (header, 0xC, "OggS"))
return null;
var input = new StreamRegion (file, 12);
var input = new StreamRegion (file.AsStream, 12);
return new OggInput (input);
// input is [intentionally] left undisposed in case of exception.
}

View File

@@ -37,23 +37,21 @@ namespace GameRes.Formats.Softpal
public override string Description { get { return "Softpal engine image format"; } }
public override uint Signature { get { return 0x43495042; } } // 'BPIC'
public override ImageMetaData ReadMetaData (Stream stream)
public override ImageMetaData ReadMetaData (IBinaryStream stream)
{
var header = new byte[0x10];
if (header.Length != stream.Read (header, 0, header.Length))
return null;
int pixel_size = LittleEndian.ToInt32 (header, 12);
var header = stream.ReadHeader (0x10);
int pixel_size = header.ToInt32 (12);
if (pixel_size != 4 && pixel_size != 3 && pixel_size != 1)
return null;
return new ImageMetaData
{
Width = LittleEndian.ToUInt32(header, 4),
Height = LittleEndian.ToUInt32(header, 8),
Width = header.ToUInt32 (4),
Height = header.ToUInt32 (8),
BPP = pixel_size * 8,
};
}
public override ImageData Read (Stream stream, ImageMetaData info)
public override ImageData Read (IBinaryStream stream, ImageMetaData info)
{
stream.Position = 0x10;
int pixel_size = info.BPP/8;

View File

@@ -43,24 +43,22 @@ namespace GameRes.Formats.Softpal
Extensions = new string[] { "pgd" };
}
public override ImageMetaData ReadMetaData (Stream stream)
public override ImageMetaData ReadMetaData (IBinaryStream stream)
{
var header = new byte[0x20];
if (header.Length != stream.Read (header, 0, header.Length))
return null;
if (!Binary.AsciiEqual (header, 0x1C, "11_C"))
var header = stream.ReadHeader (0x20);
if (!header.AsciiEqual (0x1C, "11_C"))
return null;
return new ImageMetaData
{
Width = LittleEndian.ToUInt32 (header, 0x0C),
Height = LittleEndian.ToUInt32 (header, 0x10),
OffsetX = LittleEndian.ToInt32 (header, 4),
OffsetY = LittleEndian.ToInt32 (header, 8),
Width = header.ToUInt32 (0x0C),
Height = header.ToUInt32 (0x10),
OffsetX = header.ToInt32 (4),
OffsetY = header.ToInt32 (8),
BPP = 32,
};
}
public override ImageData Read (Stream stream, ImageMetaData info)
public override ImageData Read (IBinaryStream stream, ImageMetaData info)
{
using (var reader = new PgdReader (stream, 0x20))
{
@@ -101,29 +99,27 @@ namespace GameRes.Formats.Softpal
Extensions = new string[] { "pgd" };
}
public override ImageMetaData ReadMetaData (Stream stream)
public override ImageMetaData ReadMetaData (IBinaryStream stream)
{
var header = new byte[0x24];
if (header.Length != stream.Read (header, 0, header.Length))
return null;
if (!Binary.AsciiEqual (header, 0x18, "00_C"))
var header = stream.ReadHeader (0x24);
if (!header.AsciiEqual (0x18, "00_C"))
return null;
return new ImageMetaData
{
Width = LittleEndian.ToUInt32 (header, 8),
Height = LittleEndian.ToUInt32 (header, 12),
OffsetX = LittleEndian.ToInt32 (header, 0),
OffsetY = LittleEndian.ToInt32 (header, 4),
Width = header.ToUInt32 (8),
Height = header.ToUInt32 (12),
OffsetX = header.ToInt32 (0),
OffsetY = header.ToInt32 (4),
BPP = 32,
};
}
public override ImageData Read (Stream stream, ImageMetaData info)
public override ImageData Read (IBinaryStream stream, ImageMetaData info)
{
using (var reader = new PgdReader (stream, 0x1C))
{
var data = reader.Unpack00();
using (var tga = new MemoryStream (data))
using (var tga = new BinMemoryStream (data, stream.Name))
{
var tga_info = Tga.ReadMetaData (tga);
if (null == tga_info)
@@ -153,20 +149,18 @@ namespace GameRes.Formats.Softpal
Extensions = new string[] { "pgd" };
}
public override ImageMetaData ReadMetaData (Stream stream)
public override ImageMetaData ReadMetaData (IBinaryStream stream)
{
var header = new byte[0x2A];
if (header.Length != stream.Read (header, 0, header.Length))
return null;
int x = LittleEndian.ToInt32 (header, 0);
int y = LittleEndian.ToInt32 (header, 4);
var header = stream.ReadHeader (0x2A);
int x = header.ToInt32 (0);
int y = header.ToInt32 (4);
if (Math.Abs (x) > 0x2000 || Math.Abs (y) > 0x2000)
return null;
uint width = LittleEndian.ToUInt32 (header, 8);
uint height = LittleEndian.ToUInt32 (header, 12);
uint width = header.ToUInt32 (8);
uint height = header.ToUInt32 (12);
if (0 == width || 0 == height
|| width != LittleEndian.ToUInt16 (header, 0x24)
|| height != LittleEndian.ToUInt16 (header, 0x26))
|| width != header.ToUInt16 (0x24)
|| height != header.ToUInt16 (0x26))
return null;
stream.Position = 0x18;
var tga_info = base.ReadMetaData (stream);
@@ -177,9 +171,10 @@ namespace GameRes.Formats.Softpal
return tga_info;
}
public override ImageData Read (Stream stream, ImageMetaData info)
public override ImageData Read (IBinaryStream stream, ImageMetaData info)
{
using (var tga = new StreamRegion (stream, 0x18, true))
using (var mem = new StreamRegion (stream.AsStream, 0x18, true))
using (var tga = new BinaryStream (mem, stream.Name))
return base.Read (tga, info);
}
@@ -206,23 +201,21 @@ namespace GameRes.Formats.Softpal
Extensions = new string[] { "pgd" };
}
public override ImageMetaData ReadMetaData (Stream stream)
public override ImageMetaData ReadMetaData (IBinaryStream stream)
{
var header = new byte[0x20];
if (header.Length != stream.Read (header, 0, header.Length))
return null;
var header = stream.ReadHeader (0x20);
return new PgdGeMetaData
{
Width = LittleEndian.ToUInt32 (header, 0x0C),
Height = LittleEndian.ToUInt32 (header, 0x10),
OffsetX = LittleEndian.ToInt32 (header, 4),
OffsetY = LittleEndian.ToInt32 (header, 8),
Width = header.ToUInt32 (0x0C),
Height = header.ToUInt32 (0x10),
OffsetX = header.ToInt32 (4),
OffsetY = header.ToInt32 (8),
BPP = 32,
Method = LittleEndian.ToUInt16 (header, 0x1C),
Method = header.ToUInt16 (0x1C),
};
}
public override ImageData Read (Stream stream, ImageMetaData info)
public override ImageData Read (IBinaryStream stream, ImageMetaData info)
{
using (var reader = new PgdReader (stream, (PgdGeMetaData)info))
{
@@ -255,28 +248,26 @@ namespace GameRes.Formats.Softpal
Signatures = new uint[] { 0x33444750, 0x32444750 }; // 'PGD3', 'PGD2'
}
public override ImageMetaData ReadMetaData (Stream stream)
public override ImageMetaData ReadMetaData (IBinaryStream stream)
{
var header = new byte[0x30];
if (header.Length != stream.Read (header, 0, header.Length))
return null;
string base_name = Binary.GetCString (header, 0xE, 0x22);
var header = stream.ReadHeader (0x30);
string base_name = header.GetCString (0xE, 0x22);
if (string.IsNullOrEmpty (base_name))
return null;
return new PgdIncMetaData
{
OffsetX = LittleEndian.ToUInt16 (header, 4),
OffsetY = LittleEndian.ToUInt16 (header, 6),
Width = LittleEndian.ToUInt16 (header, 8),
Height = LittleEndian.ToUInt16 (header, 0xA),
BPP = LittleEndian.ToUInt16 (header, 0xC),
OffsetX = header.ToUInt16 (4),
OffsetY = header.ToUInt16 (6),
Width = header.ToUInt16 (8),
Height = header.ToUInt16 (0xA),
BPP = header.ToUInt16 (0xC),
BaseName = base_name,
};
}
static readonly Lazy<ImageFormat> PalFormat = new Lazy<ImageFormat> (() => FindByTag ("PGD/GE"));
public override ImageData Read (Stream stream, ImageMetaData info)
public override ImageData Read (IBinaryStream stream, ImageMetaData info)
{
var meta = (PgdIncMetaData)info;
string dir_name = VFS.GetDirectoryName (meta.FileName);
@@ -284,7 +275,7 @@ namespace GameRes.Formats.Softpal
PgdGeMetaData base_info;
byte[] image, overlay;
PixelFormat format;
using (var base_file = VFS.OpenSeekableStream (name))
using (var base_file = VFS.OpenBinaryStream (name))
{
base_info = PalFormat.Value.ReadMetaData (base_file) as PgdGeMetaData;
if (null == base_info)
@@ -334,7 +325,7 @@ namespace GameRes.Formats.Softpal
internal sealed class PgdReader : IDisposable
{
BinaryReader m_input;
IBinaryStream m_input;
byte[] m_output;
int m_width;
int m_height;
@@ -343,23 +334,23 @@ namespace GameRes.Formats.Softpal
public PixelFormat Format { get; private set; }
public PgdReader (Stream input, int position)
public PgdReader (IBinaryStream input, int position)
{
input.Position = position;
m_input = new ArcView.Reader (input);
m_input = input;
m_input.Position = position;
int unpacked_size = m_input.ReadInt32();
m_input.ReadInt32(); // packed_size
m_output = new byte[unpacked_size];
}
public PgdReader (Stream input, PgdGeMetaData info) : this (input, 0x20)
public PgdReader (IBinaryStream input, PgdGeMetaData info) : this (input, 0x20)
{
m_width = (int)info.Width;
m_height = (int)info.Height;
m_method = info.Method;
}
public PgdReader (Stream input, PgdIncMetaData info) : this (input, 0x30)
public PgdReader (IBinaryStream input, PgdIncMetaData info) : this (input, 0x30)
{
m_width = (int)info.Width;
m_height = (int)info.Height;
@@ -589,14 +580,8 @@ namespace GameRes.Formats.Softpal
}
#region IDisposable Members
bool _disposed = false;
public void Dispose ()
{
if (!_disposed)
{
m_input.Dispose();
_disposed = true;
}
}
#endregion
}

View File

@@ -49,16 +49,14 @@ namespace GameRes.Formats.Softpal
Extensions = new string[] { "" };
}
public override ImageMetaData ReadMetaData (Stream stream)
public override ImageMetaData ReadMetaData (IBinaryStream stream)
{
var header = new byte[8];
if (header.Length != stream.Read (header, 0, header.Length))
return null;
int bpp = LittleEndian.ToInt16 (header, 0);
var header = stream.ReadHeader (8);
int bpp = header.ToInt16 (0);
if (1 != bpp && 3 != bpp && 4 != bpp)
return null;
uint width = LittleEndian.ToUInt16 (header, 2);
uint height = LittleEndian.ToUInt16 (header, 4);
uint width = header.ToUInt16 (2);
uint height = header.ToUInt16 (4);
if (0 == width || 0 == height || header[6]*8 < width || header[7]*8 < height)
return null;
return new PicMetaData
@@ -71,7 +69,7 @@ namespace GameRes.Formats.Softpal
};
}
public override ImageData Read (Stream stream, ImageMetaData info)
public override ImageData Read (IBinaryStream stream, ImageMetaData info)
{
var meta = (PicMetaData)info;
using (var reader = new PicReader (stream, meta))
@@ -89,7 +87,7 @@ namespace GameRes.Formats.Softpal
internal sealed class PicReader : IDisposable
{
BinaryReader m_input;
IBinaryStream m_input;
byte[] m_output;
PicMetaData m_info;
byte[] m_control;
@@ -102,9 +100,9 @@ namespace GameRes.Formats.Softpal
readonly byte[] Values4bit = InitBlockValues (8);
readonly byte[] Values6bit = InitBlockValues (0x20);
public PicReader (Stream input, PicMetaData info)
public PicReader (IBinaryStream input, PicMetaData info)
{
m_input = new ArcView.Reader (input);
m_input = input;
m_info = info;
m_src_stride = m_info.BlocksWidth * m_info.BPP;
}
@@ -122,7 +120,7 @@ namespace GameRes.Formats.Softpal
public void Unpack ()
{
m_input.BaseStream.Position = 8;
m_input.Position = 8;
m_control = m_input.ReadBytes (m_info.BlocksHeight * m_info.BlocksWidth);
m_output = new byte[m_src_stride * m_info.BlocksHeight * 8];
if (8 == m_info.BPP)
@@ -259,7 +257,7 @@ namespace GameRes.Formats.Softpal
}
else
{
byte pixel = m_input.ReadByte();
byte pixel = m_input.ReadUInt8();
DecodeBlock (ctl, pixel, block);
}
int dst = 8 * (x + 8 * y * m_info.BlocksWidth);
@@ -394,14 +392,8 @@ namespace GameRes.Formats.Softpal
}
#region IDisposable Members
bool _disposed = false;
public void Dispose ()
{
if (!_disposed)
{
m_input.Dispose();
_disposed = true;
}
}
#endregion
}