(ArcAI5Opener): iterate through known schemes.

This commit is contained in:
morkt 2015-10-14 10:38:21 +04:00
parent a5c359d922
commit 5305c19354

View File

@ -50,6 +50,8 @@ namespace GameRes.Formats.Elf
public override bool CanCreate { get { return false; } }
public static readonly Dictionary<string, ArcIndexScheme> KnownSchemes = new Dictionary<string, ArcIndexScheme> {
{ "Dorei Kaigo", new ArcIndexScheme
{ NameLength = 0x14, NameKey = 0x06, SizeKey = 0x55303024, OffsetKey = 0x57683256 } },
{ "Jokei Kazoku ~Inbou~", new ArcIndexScheme
{ NameLength = 0x1E, NameKey = 0x73, SizeKey = 0xAF5789BC, OffsetKey = 0x59FACB45 } },
};
@ -64,35 +66,66 @@ namespace GameRes.Formats.Elf
int count = file.View.ReadInt32 (0);
if (!IsSaneCount (count))
return null;
long index_offset = 4;
var scheme = KnownSchemes.First().Value;
uint index_size = (uint)(count * (scheme.NameLength + 8));
if (index_size > file.View.Reserve (index_offset, index_size))
var reader = new IndexReader (file, count);
foreach (var scheme in KnownSchemes.Values)
{
try
{
var dir = reader.Read (scheme);
if (dir != null)
return new ArcFile (file, this, dir);
}
catch { /* ignore parse errors */ }
}
return null;
var name_buf = new byte[scheme.NameLength];
var dir = new List<Entry>();
for (int i = 0; i < count; ++i)
}
internal class IndexReader
{
file.View.Read (index_offset, name_buf, 0, (uint)scheme.NameLength);
for (int n = 0; n < name_buf.Length; ++n)
ArcView m_file;
int m_count;
List<Entry> m_dir;
byte[] m_name_buf = new byte[0x100];
public IndexReader (ArcView file, int count)
{
name_buf[n] ^= scheme.NameKey;
if (0 == name_buf[n])
m_file = file;
m_count = count;
m_dir = new List<Entry> (m_count);
}
public List<Entry> Read (ArcIndexScheme scheme)
{
if (scheme.NameLength > m_name_buf.Length)
m_name_buf = new byte[scheme.NameLength];
m_dir.Clear();
int index_offset = 4;
uint index_size = (uint)(m_count * (scheme.NameLength + 8));
if (index_size > m_file.View.Reserve (index_offset, index_size))
return null;
for (int i = 0; i < m_count; ++i)
{
m_file.View.Read (index_offset, m_name_buf, 0, (uint)scheme.NameLength);
for (int n = 0; n < m_name_buf.Length; ++n)
{
m_name_buf[n] ^= scheme.NameKey;
if (0 == m_name_buf[n])
break;
}
string name = Binary.GetCString (name_buf, 0, name_buf.Length);
string name = Binary.GetCString (m_name_buf, 0, m_name_buf.Length);
if (0 == name.Length)
return null;
index_offset += scheme.NameLength;
var entry = FormatCatalog.Instance.Create<Entry> (name);
entry.Size = file.View.ReadUInt32 (index_offset) ^ scheme.SizeKey;
entry.Offset = file.View.ReadUInt32 (index_offset+4) ^ scheme.OffsetKey;
if (entry.Offset < index_size+4 || !entry.CheckPlacement (file.MaxOffset))
entry.Size = m_file.View.ReadUInt32 (index_offset) ^ scheme.SizeKey;
entry.Offset = m_file.View.ReadUInt32 (index_offset+4) ^ scheme.OffsetKey;
if (entry.Offset < index_size+4 || !entry.CheckPlacement (m_file.MaxOffset))
return null;
dir.Add (entry);
m_dir.Add (entry);
index_offset += 8;
}
return new ArcFile (file, this, dir);
return m_dir;
}
}
/*