2015-02-27 02:07:49 +08:00
|
|
|
//! \file ArcCommon.cs
|
|
|
|
//! \date Tue Aug 19 09:45:38 2014
|
|
|
|
//! \brief Classes and functions common for various resource files.
|
|
|
|
//
|
|
|
|
// Copyright (C) 2014-2015 by morkt
|
|
|
|
//
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
// of this software and associated documentation files (the "Software"), to
|
|
|
|
// deal in the Software without restriction, including without limitation the
|
|
|
|
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
|
|
|
// sell copies of the Software, and to permit persons to whom the Software is
|
|
|
|
// furnished to do so, subject to the following conditions:
|
|
|
|
//
|
|
|
|
// The above copyright notice and this permission notice shall be included in
|
|
|
|
// all copies or substantial portions of the Software.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
|
|
|
// IN THE SOFTWARE.
|
|
|
|
//
|
|
|
|
|
|
|
|
using System;
|
|
|
|
using System.IO;
|
2015-04-04 09:26:14 +08:00
|
|
|
using System.Linq;
|
|
|
|
using System.Text;
|
2015-02-27 02:07:49 +08:00
|
|
|
|
|
|
|
namespace GameRes.Formats
|
|
|
|
{
|
2015-04-04 09:26:14 +08:00
|
|
|
public class AutoEntry : Entry
|
|
|
|
{
|
|
|
|
private Lazy<IResource> m_res;
|
|
|
|
private Lazy<string> m_name;
|
|
|
|
private Lazy<string> m_type;
|
|
|
|
|
|
|
|
public override string Name
|
|
|
|
{
|
|
|
|
get { return m_name.Value; }
|
|
|
|
set { m_name = new Lazy<string> (() => value); }
|
|
|
|
}
|
|
|
|
public override string Type
|
|
|
|
{
|
|
|
|
get { return m_type.Value; }
|
|
|
|
set { m_type = new Lazy<string> (() => value); }
|
|
|
|
}
|
|
|
|
|
|
|
|
public AutoEntry (string name, Func<IResource> type_checker)
|
|
|
|
{
|
|
|
|
m_res = new Lazy<IResource> (type_checker);
|
|
|
|
m_name = new Lazy<string> (() => GetName (name));
|
|
|
|
m_type = new Lazy<string> (GetEntryType);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static AutoEntry Create (ArcView file, long offset, string base_name)
|
|
|
|
{
|
|
|
|
return new AutoEntry (base_name, () => {
|
|
|
|
uint signature = file.View.ReadUInt32 (offset);
|
|
|
|
return FormatCatalog.Instance.LookupSignature (signature).FirstOrDefault();
|
2015-04-17 02:38:47 +08:00
|
|
|
}) { Offset = offset };
|
2015-04-04 09:26:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
private string GetName (string name)
|
|
|
|
{
|
|
|
|
if (null == m_res.Value)
|
|
|
|
return name;
|
|
|
|
var ext = m_res.Value.Extensions.FirstOrDefault();
|
|
|
|
if (string.IsNullOrEmpty (ext))
|
|
|
|
return name;
|
2015-04-17 02:38:47 +08:00
|
|
|
return Path.ChangeExtension (name, ext);
|
2015-04-04 09:26:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
private string GetEntryType ()
|
|
|
|
{
|
|
|
|
return null == m_res.Value ? "" : m_res.Value.Type;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-27 02:07:49 +08:00
|
|
|
public class PrefixStream : Stream
|
|
|
|
{
|
|
|
|
byte[] m_header;
|
|
|
|
Stream m_stream;
|
|
|
|
long m_position = 0;
|
|
|
|
|
|
|
|
public PrefixStream (byte[] header, Stream main)
|
|
|
|
{
|
|
|
|
m_header = header;
|
|
|
|
m_stream = main;
|
|
|
|
}
|
|
|
|
|
|
|
|
public override bool CanRead { get { return m_stream.CanRead; } }
|
|
|
|
public override bool CanSeek { get { return m_stream.CanSeek; } }
|
|
|
|
public override bool CanWrite { get { return false; } }
|
|
|
|
public override long Length { get { return m_stream.Length + m_header.Length; } }
|
|
|
|
public override long Position
|
|
|
|
{
|
|
|
|
get { return m_position; }
|
|
|
|
set
|
|
|
|
{
|
|
|
|
m_position = Math.Max (value, 0);
|
|
|
|
if (m_position > m_header.Length)
|
|
|
|
{
|
|
|
|
long stream_pos = m_stream.Seek (m_position - m_header.Length, SeekOrigin.Begin);
|
|
|
|
m_position = m_header.Length + stream_pos;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public override void Flush()
|
|
|
|
{
|
|
|
|
m_stream.Flush();
|
|
|
|
}
|
|
|
|
|
|
|
|
public override long Seek (long offset, SeekOrigin origin)
|
|
|
|
{
|
|
|
|
if (SeekOrigin.Begin == origin)
|
|
|
|
Position = offset;
|
|
|
|
else if (SeekOrigin.Current == origin)
|
|
|
|
Position = m_position + offset;
|
|
|
|
else
|
|
|
|
Position = Length + offset;
|
|
|
|
|
|
|
|
return m_position;
|
|
|
|
}
|
|
|
|
|
|
|
|
public override int Read (byte[] buffer, int offset, int count)
|
|
|
|
{
|
|
|
|
int read = 0;
|
|
|
|
if (m_position < m_header.Length)
|
|
|
|
{
|
|
|
|
int header_count = Math.Min (count, m_header.Length - (int)m_position);
|
|
|
|
Buffer.BlockCopy (m_header, (int)m_position, buffer, offset, header_count);
|
|
|
|
m_position += header_count;
|
|
|
|
read += header_count;
|
|
|
|
offset += header_count;
|
|
|
|
count -= header_count;
|
|
|
|
}
|
|
|
|
if (count > 0)
|
|
|
|
{
|
2015-03-05 21:40:13 +08:00
|
|
|
if (m_header.Length == m_position)
|
|
|
|
m_stream.Position = 0;
|
2015-02-27 02:07:49 +08:00
|
|
|
int stream_read = m_stream.Read (buffer, offset, count);
|
|
|
|
m_position += stream_read;
|
|
|
|
read += stream_read;
|
|
|
|
}
|
|
|
|
return read;
|
|
|
|
}
|
|
|
|
|
|
|
|
public override int ReadByte ()
|
|
|
|
{
|
|
|
|
if (m_position < m_header.Length)
|
|
|
|
return m_header[m_position++];
|
|
|
|
if (m_position == m_header.Length)
|
|
|
|
m_stream.Position = 0;
|
|
|
|
int b = m_stream.ReadByte();
|
|
|
|
if (-1 != b)
|
|
|
|
m_position++;
|
|
|
|
return b;
|
|
|
|
}
|
|
|
|
|
|
|
|
public override void SetLength (long length)
|
|
|
|
{
|
|
|
|
throw new NotSupportedException ("PrefixStream.SetLength method is not supported");
|
|
|
|
}
|
|
|
|
|
|
|
|
public override void Write (byte[] buffer, int offset, int count)
|
|
|
|
{
|
|
|
|
throw new NotSupportedException ("PrefixStream.Write method is not supported");
|
|
|
|
}
|
|
|
|
|
|
|
|
public override void WriteByte (byte value)
|
|
|
|
{
|
|
|
|
throw new NotSupportedException ("PrefixStream.WriteByte method is not supported");
|
|
|
|
}
|
|
|
|
|
|
|
|
bool disposed = false;
|
|
|
|
protected override void Dispose (bool disposing)
|
|
|
|
{
|
|
|
|
if (!disposed)
|
|
|
|
{
|
|
|
|
m_stream.Dispose();
|
|
|
|
disposed = true;
|
|
|
|
base.Dispose (disposing);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-04-04 09:26:14 +08:00
|
|
|
|
|
|
|
public class LzssReader : IDisposable
|
|
|
|
{
|
|
|
|
BinaryReader m_input;
|
|
|
|
byte[] m_output;
|
|
|
|
int m_size;
|
|
|
|
|
|
|
|
public byte[] Data { get { return m_output; } }
|
|
|
|
|
|
|
|
public LzssReader (Stream input, int input_length, int output_length)
|
|
|
|
{
|
|
|
|
m_input = new BinaryReader (input, Encoding.ASCII, true);
|
|
|
|
m_output = new byte[output_length];
|
|
|
|
m_size = input_length;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Unpack ()
|
|
|
|
{
|
|
|
|
int dst = 0;
|
|
|
|
var frame = new byte[0x1000];
|
|
|
|
int frame_pos = 0xfee;
|
|
|
|
int remaining = (int)m_size;
|
|
|
|
while (remaining > 0)
|
|
|
|
{
|
|
|
|
int ctl = m_input.ReadByte();
|
|
|
|
--remaining;
|
|
|
|
for (int bit = 1; remaining > 0 && bit != 0x100; bit <<= 1)
|
|
|
|
{
|
|
|
|
if (dst >= m_output.Length)
|
|
|
|
return;
|
|
|
|
if (0 != (ctl & bit))
|
|
|
|
{
|
|
|
|
byte b = m_input.ReadByte();
|
|
|
|
--remaining;
|
|
|
|
frame[frame_pos++] = b;
|
|
|
|
frame_pos &= 0xfff;
|
|
|
|
m_output[dst++] = b;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (remaining < 2)
|
|
|
|
return;
|
|
|
|
int lo = m_input.ReadByte();
|
|
|
|
int hi = m_input.ReadByte();
|
|
|
|
remaining -= 2;
|
|
|
|
int offset = (hi & 0xf0) << 4 | lo;
|
|
|
|
for (int count = 3 + (hi & 0xF); count != 0; --count)
|
|
|
|
{
|
|
|
|
if (dst >= m_output.Length)
|
|
|
|
break;
|
|
|
|
byte v = frame[offset++];
|
|
|
|
offset &= 0xfff;
|
|
|
|
frame[frame_pos++] = v;
|
|
|
|
frame_pos &= 0xfff;
|
|
|
|
m_output[dst++] = v;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#region IDisposable Members
|
|
|
|
bool disposed = false;
|
|
|
|
|
|
|
|
public void Dispose ()
|
|
|
|
{
|
|
|
|
Dispose (true);
|
|
|
|
GC.SuppressFinalize (this);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected virtual void Dispose (bool disposing)
|
|
|
|
{
|
|
|
|
if (!disposed)
|
|
|
|
{
|
|
|
|
if (disposing)
|
|
|
|
{
|
|
|
|
m_input.Dispose();
|
|
|
|
}
|
|
|
|
disposed = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endregion
|
|
|
|
}
|
2015-04-14 14:28:00 +08:00
|
|
|
|
|
|
|
public class HuffmanDecoder
|
|
|
|
{
|
|
|
|
byte[] m_src;
|
|
|
|
byte[] m_dst;
|
|
|
|
|
|
|
|
ushort[] lhs = new ushort[512];
|
|
|
|
ushort[] rhs = new ushort[512];
|
2015-04-20 18:06:20 +08:00
|
|
|
ushort token = 256;
|
2015-04-14 14:28:00 +08:00
|
|
|
|
|
|
|
int input_pos;
|
|
|
|
int remaining;
|
2015-04-20 18:06:20 +08:00
|
|
|
int m_cached_bits;
|
|
|
|
int m_cache;
|
2015-04-14 14:28:00 +08:00
|
|
|
|
|
|
|
public HuffmanDecoder (byte[] src, int index, int length, byte[] dst)
|
|
|
|
{
|
|
|
|
m_src = src;
|
|
|
|
m_dst = dst;
|
|
|
|
input_pos = index;
|
|
|
|
remaining = length;
|
2015-04-20 18:06:20 +08:00
|
|
|
m_cached_bits = 0;
|
|
|
|
m_cache = 0;
|
2015-04-14 14:28:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public HuffmanDecoder (byte[] src, byte[] dst) : this (src, 0, src.Length, dst)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
public byte[] Unpack ()
|
|
|
|
{
|
2015-04-20 18:06:20 +08:00
|
|
|
int dst = 0;
|
|
|
|
token = 256;
|
|
|
|
ushort v3 = CreateTree();
|
|
|
|
while (dst < m_dst.Length)
|
2015-04-14 14:28:00 +08:00
|
|
|
{
|
2015-04-20 18:06:20 +08:00
|
|
|
ushort symbol = v3;
|
|
|
|
while ( symbol >= 0x100u )
|
2015-04-14 14:28:00 +08:00
|
|
|
{
|
2015-04-20 18:06:20 +08:00
|
|
|
if ( 0 != GetBits (1) )
|
|
|
|
symbol = rhs[symbol];
|
|
|
|
else
|
|
|
|
symbol = lhs[symbol];
|
2015-04-14 14:28:00 +08:00
|
|
|
}
|
2015-04-20 18:06:20 +08:00
|
|
|
m_dst[dst++] = (byte)symbol;
|
2015-04-14 14:28:00 +08:00
|
|
|
}
|
|
|
|
return m_dst;
|
|
|
|
}
|
|
|
|
|
2015-04-20 18:06:20 +08:00
|
|
|
ushort CreateTree()
|
2015-04-14 14:28:00 +08:00
|
|
|
{
|
|
|
|
if ( 0 != GetBits (1) )
|
|
|
|
{
|
2015-04-20 18:06:20 +08:00
|
|
|
ushort v = token++;
|
|
|
|
lhs[v] = CreateTree();
|
|
|
|
rhs[v] = CreateTree();
|
|
|
|
return v;
|
2015-04-14 14:28:00 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return (ushort)GetBits (8);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
uint GetBits (int n)
|
|
|
|
{
|
2015-04-20 18:06:20 +08:00
|
|
|
while ( n > m_cached_bits )
|
2015-04-14 14:28:00 +08:00
|
|
|
{
|
2015-04-20 18:06:20 +08:00
|
|
|
int v = m_src[input_pos++];
|
2015-04-14 14:28:00 +08:00
|
|
|
--remaining;
|
2015-04-20 18:06:20 +08:00
|
|
|
m_cache = v | (m_cache << 8);
|
|
|
|
m_cached_bits += 8;
|
2015-04-14 14:28:00 +08:00
|
|
|
}
|
2015-04-20 18:06:20 +08:00
|
|
|
uint mask = (uint)m_cache;
|
|
|
|
m_cached_bits -= n;
|
|
|
|
m_cache &= ~(-1 << m_cached_bits);
|
|
|
|
return (uint)(((-1 << m_cached_bits) & mask) >> m_cached_bits);
|
2015-04-14 14:28:00 +08:00
|
|
|
}
|
|
|
|
}
|
2015-02-27 02:07:49 +08:00
|
|
|
}
|