(BinaryStream): various fixes.

This commit is contained in:
morkt
2016-12-15 19:47:40 +04:00
parent 9a0c6ef5cd
commit 8ffe411583

View File

@@ -155,7 +155,16 @@ namespace GameRes
{ {
var bin = input as IBinaryStream; var bin = input as IBinaryStream;
if (null == bin) if (null == bin)
bin = new BinaryStream (input, filename); {
var mem_stream = input as MemoryStream;
if (mem_stream != null)
{
bin = new BinMemoryStream (mem_stream, filename);
mem_stream.Dispose();
}
else
bin = new BinaryStream (input, filename);
}
else else
bin.Name = filename; bin.Name = filename;
return bin; return bin;
@@ -202,7 +211,7 @@ namespace GameRes
return count; return count;
if (m_buffer_pos + count > m_buffer.Length) if (m_buffer_pos + count > m_buffer.Length)
{ {
if (cached + count > m_buffer.Length) if (count > m_buffer.Length)
{ {
var copy = new byte[(count + 0xF) & ~0xF]; var copy = new byte[(count + 0xF) & ~0xF];
if (cached > 0) if (cached > 0)
@@ -257,8 +266,7 @@ namespace GameRes
{ {
if (3 != FillBuffer (3)) if (3 != FillBuffer (3))
throw new EndOfStreamException(); throw new EndOfStreamException();
int v = LittleEndian.ToUInt16 (m_buffer, m_buffer_pos); int v = m_buffer.ToInt24 (m_buffer_pos);
v |= m_buffer[m_buffer_pos+2] << 16;
m_buffer_pos += 3; m_buffer_pos += 3;
return v; return v;
} }
@@ -490,7 +498,7 @@ namespace GameRes
m_position = 0; m_position = 0;
Name = name ?? ""; Name = name ?? "";
if (m_length >= 4) if (m_length >= 4)
m_signature = LittleEndian.ToUInt32 (m_source, 0); m_signature = LittleEndian.ToUInt32 (m_source, m_start);
} }
public CowArray<byte> ReadHeader (int size) public CowArray<byte> ReadHeader (int size)
@@ -541,8 +549,7 @@ namespace GameRes
{ {
if (m_length - m_position < 3) if (m_length - m_position < 3)
throw new EndOfStreamException(); throw new EndOfStreamException();
int v = LittleEndian.ToUInt16 (m_source, m_start+m_position); int v = m_source.ToInt24 (m_start+m_position);
v |= m_source[m_start+m_position+2] << 16;
m_position += 3; m_position += 3;
return v; return v;
} }
@@ -583,10 +590,11 @@ namespace GameRes
public string ReadCString (int length, Encoding enc) public string ReadCString (int length, Encoding enc)
{ {
length = Math.Min (length, m_length - m_position); length = Math.Min (length, m_length - m_position);
int i = Array.IndexOf<byte> (m_source, 0, m_start+m_position, length); int start = m_start+m_position;
int i = Array.IndexOf<byte> (m_source, 0, start, length);
if (-1 == i) if (-1 == i)
i = length; i = start+length;
string s = enc.GetString (m_source, m_start+m_position, i); string s = enc.GetString (m_source, start, i-start);
m_position += length; m_position += length;
return s; return s;
} }
@@ -599,14 +607,16 @@ namespace GameRes
public string ReadCString (Encoding enc) public string ReadCString (Encoding enc)
{ {
int start = m_start+m_position; int start = m_start+m_position;
int count = Array.IndexOf<byte> (m_source, 0, start, m_length-m_position); int eos_pos = Array.IndexOf<byte> (m_source, 0, start, m_length-m_position);
if (-1 == count) int count;
if (-1 == eos_pos)
{ {
count = m_length - m_position; count = m_length - m_position;
m_position = m_length; m_position = m_length;
} }
else else
{ {
count = eos_pos - start;
m_position += count + 1; m_position += count + 1;
} }
return enc.GetString (m_source, start, count); return enc.GetString (m_source, start, count);