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

@@ -154,7 +154,7 @@ namespace GameRes.Formats.Lilim
internal sealed class AbmReader : IDisposable
{
BinaryReader m_input;
IBinaryStream m_input;
byte[] m_output;
AbmImageData m_info;
int m_bpp;
@@ -162,15 +162,14 @@ namespace GameRes.Formats.Lilim
public byte[] Data { get { return m_output; } }
public int BPP { get { return m_bpp; } }
public AbmReader (Stream file, AbmImageData info)
public AbmReader (IBinaryStream file, AbmImageData info)
{
m_info = info;
if (2 == m_info.Mode)
{
m_bpp = 32;
file.Position = m_info.BaseOffset;
using (var base_frame = new ArcView.Reader (file))
m_output = UnpackV2 (base_frame);
m_output = UnpackV2 (file);
}
else if (1 == m_info.Mode || 32 == m_info.Mode || 24 == m_info.Mode)
{
@@ -185,24 +184,18 @@ namespace GameRes.Formats.Lilim
if (1 == m_info.Mode)
{
if (total_length != file.Read (m_output, 0, (total_length)))
throw new EndOfStreamException();
throw new EndOfStreamException ();
}
else if (24 == m_bpp)
UnpackStream24 (file, m_output, total_length);
else
{
using (var base_frame = new ArcView.Reader (file))
{
if (24 == m_bpp)
UnpackStream24 (base_frame, m_output, total_length);
else
UnpackStream32 (base_frame, m_output, total_length);
}
}
UnpackStream32 (file, m_output, total_length);
}
else
throw new NotImplementedException();
if (0 != m_info.FrameOffset)
{
m_input = new ArcView.Reader (file);
m_input = file;
}
}
@@ -216,7 +209,7 @@ namespace GameRes.Formats.Lilim
if (null == m_input)
return m_output;
m_input.BaseStream.Position = m_info.FrameOffset;
m_input.Position = m_info.FrameOffset;
if (1 == m_info.Mode)
return UnpackStream24 (m_input, m_output, m_output.Length);
if (2 == m_info.Mode)
@@ -228,7 +221,7 @@ namespace GameRes.Formats.Lilim
throw new NotImplementedException();
}
byte[] UnpackV2 (BinaryReader input)
byte[] UnpackV2 (IBinaryStream input)
{
frame_x = input.ReadInt32();
frame_y = input.ReadInt32();
@@ -242,22 +235,22 @@ namespace GameRes.Formats.Lilim
return UnpackStream32 (input, output, total_length);
}
byte[] UnpackStream24 (BinaryReader input, byte[] output, int total_length)
byte[] UnpackStream24 (IBinaryStream input, byte[] output, int total_length)
{
int dst = 0;
while (dst < total_length)
{
int v = input.ReadByte();
int v = input.ReadUInt8();
if (0 == v)
{
int count = input.ReadByte();
int count = input.ReadUInt8();
if (0 == count)
continue;
dst += count;
}
else if (0xff == v)
{
int count = input.ReadByte();
int count = input.ReadUInt8();
if (0 == count)
continue;
count = Math.Min (count, total_length-dst);
@@ -266,22 +259,22 @@ namespace GameRes.Formats.Lilim
}
else
{
output[dst++] = input.ReadByte();
output[dst++] = input.ReadUInt8();
}
}
return output;
}
byte[] UnpackStream32 (BinaryReader input, byte[] output, int total_length)
byte[] UnpackStream32 (IBinaryStream input, byte[] output, int total_length)
{
int dst = 0;
int component = 0;
while (dst < total_length)
{
byte v = input.ReadByte();
byte v = input.ReadUInt8();
if (0 == v)
{
int count = input.ReadByte();
int count = input.ReadUInt8();
if (0 == count)
continue;
for (int i = 0; i < count; ++i)
@@ -296,12 +289,12 @@ namespace GameRes.Formats.Lilim
}
else if (0xff == v)
{
int count = input.ReadByte();
int count = input.ReadUInt8();
if (0 == count)
continue;
for (int i = 0; i < count && dst < total_length; ++i)
{
output[dst++] = input.ReadByte();
output[dst++] = input.ReadUInt8();
if (++component == 3)
{
output[dst++] = 0xff;
@@ -311,7 +304,7 @@ namespace GameRes.Formats.Lilim
}
else
{
output[dst++] = input.ReadByte();
output[dst++] = input.ReadUInt8();
if (++component == 3)
{
output[dst++] = v;
@@ -341,16 +334,9 @@ namespace GameRes.Formats.Lilim
}
#region IDisposable Members
bool m_disposed = false;
public void Dispose ()
{
if (!m_disposed)
{
if (m_input != null)
m_input.Dispose();
m_disposed = true;
GC.SuppressFinalize (this);
}
GC.SuppressFinalize (this);
}
#endregion
}

View File

@@ -109,7 +109,7 @@ namespace GameRes.Formats.Lilim
var decoder = new HuffmanDecoder (packed, unpacked);
decoder.Unpack();
return new MemoryStream (unpacked);
return new BinMemoryStream (unpacked, entry.Name);
}
}

View File

@@ -38,28 +38,26 @@ namespace GameRes.Formats.Lilim
public override string Description { get { return "LiLiM/Le.Chocolat compressed bitmap"; } }
public override uint Signature { get { return 0; } }
public override ImageMetaData ReadMetaData (Stream stream)
public override ImageMetaData ReadMetaData (IBinaryStream stream)
{
var header = new byte[0x46];
if (header.Length != stream.Read (header, 0, header.Length))
return null;
var header = stream.ReadHeader (0x46);
if ('B' != header[0] || 'M' != header[1])
return null;
int type = (sbyte)header[0x1C];
uint frame_offset;
if (1 == type || 2 == type)
{
int count = LittleEndian.ToUInt16 (header, 0x3A);
int count = header.ToUInt16 (0x3A);
if (count > 0xFF)
return null;
frame_offset = LittleEndian.ToUInt32 (header, 0x42);
frame_offset = header.ToUInt32 (0x42);
}
else if (32 == type || 24 == type)
{
uint unpacked_size = LittleEndian.ToUInt32 (header, 2);
uint unpacked_size = header.ToUInt32 (2);
if (0 == unpacked_size || unpacked_size == stream.Length) // probably an ordinary bmp file
return null;
frame_offset = LittleEndian.ToUInt32 (header, 0xA);
frame_offset = header.ToUInt32 (0xA);
}
else
return null;
@@ -67,15 +65,15 @@ namespace GameRes.Formats.Lilim
return null;
return new AbmImageData
{
Width = LittleEndian.ToUInt32 (header, 0x12),
Height = LittleEndian.ToUInt32 (header, 0x16),
Width = header.ToUInt32 (0x12),
Height = header.ToUInt32 (0x16),
BPP = 24,
Mode = type,
BaseOffset = frame_offset,
};
}
public override ImageData Read (Stream stream, ImageMetaData info)
public override ImageData Read (IBinaryStream stream, ImageMetaData info)
{
using (var reader = new AbmReader (stream, (AbmImageData)info))
{