2015-07-01 05:06:35 +08:00
|
|
|
//! \file ArcAi5Win.cs
|
|
|
|
//! \date Mon Jun 29 04:41:29 2015
|
|
|
|
//! \brief Ai5Win engine resource archive.
|
|
|
|
//
|
|
|
|
// Copyright (C) 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.Collections.Generic;
|
|
|
|
using System.ComponentModel.Composition;
|
|
|
|
using System.IO;
|
|
|
|
using System.Linq;
|
2016-01-20 05:04:00 +08:00
|
|
|
using GameRes.Compression;
|
2015-07-01 05:06:35 +08:00
|
|
|
using GameRes.Utility;
|
|
|
|
|
|
|
|
namespace GameRes.Formats.Elf
|
|
|
|
{
|
2015-12-03 19:52:50 +08:00
|
|
|
[Serializable]
|
2015-07-01 05:06:35 +08:00
|
|
|
public class ArcIndexScheme
|
|
|
|
{
|
|
|
|
public int NameLength;
|
|
|
|
public byte NameKey;
|
|
|
|
public uint SizeKey;
|
|
|
|
public uint OffsetKey;
|
|
|
|
}
|
|
|
|
|
2015-12-03 19:52:50 +08:00
|
|
|
[Serializable]
|
|
|
|
public class Ai5Scheme : ResourceScheme
|
|
|
|
{
|
|
|
|
public Dictionary<string, ArcIndexScheme> KnownSchemes;
|
|
|
|
}
|
|
|
|
|
2015-07-01 05:06:35 +08:00
|
|
|
[Export(typeof(ArchiveFormat))]
|
2015-10-12 10:53:14 +08:00
|
|
|
public class ArcAI5Opener : ArchiveFormat
|
2015-07-01 05:06:35 +08:00
|
|
|
{
|
|
|
|
public override string Tag { get { return "ARC/AI5WIN"; } }
|
|
|
|
public override string Description { get { return "AI5WIN engine resource archive"; } }
|
|
|
|
public override uint Signature { get { return 0; } }
|
|
|
|
public override bool IsHierarchic { get { return false; } }
|
2016-10-11 04:05:22 +08:00
|
|
|
public override bool CanWrite { get { return false; } }
|
2015-07-01 05:06:35 +08:00
|
|
|
|
2015-12-03 19:52:50 +08:00
|
|
|
public static Dictionary<string, ArcIndexScheme> KnownSchemes = new Dictionary<string, ArcIndexScheme>();
|
2015-07-01 05:06:35 +08:00
|
|
|
|
2015-10-12 10:53:14 +08:00
|
|
|
public ArcAI5Opener ()
|
2015-07-01 05:06:35 +08:00
|
|
|
{
|
|
|
|
Extensions = new string[] { "arc" };
|
|
|
|
}
|
|
|
|
|
2015-12-03 19:52:50 +08:00
|
|
|
public override ResourceScheme Scheme
|
|
|
|
{
|
|
|
|
get { return new Ai5Scheme { KnownSchemes = KnownSchemes }; }
|
|
|
|
set { KnownSchemes = ((Ai5Scheme)value).KnownSchemes; }
|
|
|
|
}
|
|
|
|
|
2015-07-01 05:06:35 +08:00
|
|
|
public override ArcFile TryOpen (ArcView file)
|
|
|
|
{
|
2015-12-03 19:52:50 +08:00
|
|
|
if (0 == KnownSchemes.Count)
|
|
|
|
return null;
|
2015-07-01 05:06:35 +08:00
|
|
|
int count = file.View.ReadInt32 (0);
|
2015-10-12 10:53:14 +08:00
|
|
|
if (!IsSaneCount (count))
|
2015-07-01 05:06:35 +08:00
|
|
|
return null;
|
2015-10-14 14:38:21 +08:00
|
|
|
var reader = new IndexReader (file, count);
|
|
|
|
foreach (var scheme in KnownSchemes.Values)
|
2015-07-01 05:06:35 +08:00
|
|
|
{
|
2015-10-14 14:38:21 +08:00
|
|
|
try
|
2015-07-01 05:06:35 +08:00
|
|
|
{
|
2015-10-14 14:38:21 +08:00
|
|
|
var dir = reader.Read (scheme);
|
|
|
|
if (dir != null)
|
|
|
|
return new ArcFile (file, this, dir);
|
2015-07-01 05:06:35 +08:00
|
|
|
}
|
2015-10-14 14:38:21 +08:00
|
|
|
catch { /* ignore parse errors */ }
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2016-01-20 05:04:00 +08:00
|
|
|
public override Stream OpenEntry (ArcFile arc, Entry entry)
|
|
|
|
{
|
|
|
|
var input = arc.File.CreateStream (entry.Offset, entry.Size);
|
|
|
|
if (entry.Name.EndsWith (".mes", StringComparison.InvariantCultureIgnoreCase)
|
|
|
|
|| entry.Name.EndsWith (".lib", StringComparison.InvariantCultureIgnoreCase))
|
|
|
|
return new LzssStream (input);
|
|
|
|
return input;
|
|
|
|
}
|
|
|
|
|
2015-10-14 14:38:21 +08:00
|
|
|
internal class IndexReader
|
|
|
|
{
|
|
|
|
ArcView m_file;
|
|
|
|
int m_count;
|
|
|
|
List<Entry> m_dir;
|
|
|
|
byte[] m_name_buf = new byte[0x100];
|
|
|
|
|
|
|
|
public IndexReader (ArcView file, int count)
|
|
|
|
{
|
|
|
|
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))
|
2015-07-01 05:06:35 +08:00
|
|
|
return null;
|
2015-10-14 14:38:21 +08:00
|
|
|
for (int i = 0; i < m_count; ++i)
|
|
|
|
{
|
|
|
|
m_file.View.Read (index_offset, m_name_buf, 0, (uint)scheme.NameLength);
|
2015-12-03 19:52:50 +08:00
|
|
|
int n;
|
|
|
|
for (n = 0; n < m_name_buf.Length; ++n)
|
2015-10-14 14:38:21 +08:00
|
|
|
{
|
|
|
|
m_name_buf[n] ^= scheme.NameKey;
|
|
|
|
if (0 == m_name_buf[n])
|
|
|
|
break;
|
2015-12-03 19:52:50 +08:00
|
|
|
if (m_name_buf[n] < 0x20)
|
|
|
|
return null;
|
2015-10-14 14:38:21 +08:00
|
|
|
}
|
2015-12-03 19:52:50 +08:00
|
|
|
if (0 == n)
|
2015-10-14 14:38:21 +08:00
|
|
|
return null;
|
2015-12-03 19:52:50 +08:00
|
|
|
string name = Encodings.cp932.GetString (m_name_buf, 0, n);
|
2015-10-14 14:38:21 +08:00
|
|
|
index_offset += scheme.NameLength;
|
|
|
|
var entry = FormatCatalog.Instance.Create<Entry> (name);
|
|
|
|
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;
|
|
|
|
m_dir.Add (entry);
|
|
|
|
index_offset += 8;
|
|
|
|
}
|
|
|
|
return m_dir;
|
2015-07-01 05:06:35 +08:00
|
|
|
}
|
|
|
|
}
|
2015-07-28 16:21:19 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
internal class IndexReader
|
|
|
|
{
|
|
|
|
ArcView m_file;
|
|
|
|
int m_count;
|
|
|
|
byte[] m_first = new byte[0x108];
|
|
|
|
|
|
|
|
public const int MinNameLength = 0x10;
|
|
|
|
public const int MaxNameLength = 0x100;
|
|
|
|
|
|
|
|
public IndexReader (ArcView file)
|
|
|
|
{
|
|
|
|
m_file = file;
|
|
|
|
m_count = m_file.View.ReadInt32 (0);
|
|
|
|
m_file.View.Read (4, m_first, 0, m_first.Length);
|
|
|
|
}
|
|
|
|
|
|
|
|
ArcIndexScheme m_scheme = new ArcIndexScheme();
|
|
|
|
|
|
|
|
public ArcIndexScheme Parse ()
|
|
|
|
{
|
|
|
|
if (m_count <= 0 || m_count > 0xfffff)
|
|
|
|
return null;
|
|
|
|
|
|
|
|
uint supposed_first_offset = (uint)(m_count * (name_length + 8));
|
|
|
|
|
|
|
|
uint first_size = LittleEndian.ToUInt32 (first_entry, name_length);
|
|
|
|
uint first_offset = LittleEndian.ToUInt32 (first_entry, name_length+4);
|
|
|
|
|
|
|
|
uint supposed_offset_key = first_offset ^ supposed_first_offset;
|
|
|
|
int last_index_offset = 4 + (m_count - 1) * (name_length + 8);
|
|
|
|
uint last_size = m_file.View.ReadUInt32 (last_index_offset + name_length);
|
|
|
|
uint last_offset = m_file.View.ReadUInt32 (last_index_offset + name_length + 4);
|
|
|
|
last_offset ^= supposed_offset_key;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ParseFirstEntry (int name_length)
|
|
|
|
{
|
|
|
|
int index_offset = 4;
|
|
|
|
}
|
|
|
|
|
|
|
|
public byte NameKey { get; private set; }
|
|
|
|
|
|
|
|
int GuessNameLength (int initial)
|
|
|
|
{
|
|
|
|
int name_pos = initial;
|
|
|
|
byte sym;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
do
|
|
|
|
{
|
|
|
|
sym = first_entry[name_pos++];
|
|
|
|
}
|
|
|
|
while (name_pos < MaxNameLength && sym != first_entry[name_pos]);
|
|
|
|
if (MaxNameLength == name_pos)
|
|
|
|
return 0;
|
|
|
|
while (name_pos < MaxNameLength && sym == first_entry[name_pos])
|
|
|
|
{
|
|
|
|
++name_pos;
|
|
|
|
}
|
|
|
|
if (MaxNameLength == name_pos && sym == first_entry[name_pos] && sym == first_entry[name_pos+1])
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
while (name_pos < MinNameLength || 0 != (name_pos & 1));
|
|
|
|
NameKey = sym;
|
|
|
|
return name_pos;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*/
|
2015-07-01 05:06:35 +08:00
|
|
|
}
|
|
|
|
}
|