implemented TCD3 archives.

This commit is contained in:
morkt 2016-09-26 10:08:18 +04:00
parent 5cc52546e2
commit 0faa25b356
2 changed files with 237 additions and 128 deletions

View File

@ -34,12 +34,16 @@ namespace GameRes.Formats.TopCat
{
internal class TcdSection
{
public string Extension;
public uint DataSize;
public uint IndexOffset;
public int DirCount;
public int DirNameLength;
public int FileCount;
public int FileNameLength;
public int DirNamesSize;
public int FileNamesSize;
}
internal struct TcdDirEntry
@ -49,29 +53,9 @@ namespace GameRes.Formats.TopCat
public int FirstIndex;
}
internal class TcdEntry : AutoEntry
internal class TcdEntry : Entry
{
public int Index;
public TcdEntry (int index, string name, ArcView file, long offset)
: base (name, () => DetectFileType (file, offset))
{
Index = index;
Offset = offset;
}
static readonly Lazy<ImageFormat> SpdcFormat = new Lazy<ImageFormat> (() => ImageFormat.FindByTag ("SPD"));
private static IResource DetectFileType (ArcView file, long offset)
{
uint signature = file.View.ReadUInt32 (offset);
byte spdc_key = (byte)(signature - 'S');
if ('P' == (((signature >> 8) - spdc_key) & 0xFF) &&
'D' == (((signature >> 16) - spdc_key) & 0xFF) &&
'C' == (((signature >> 24) - spdc_key) & 0xFF))
return SpdcFormat.Value;
return AutoEntry.DetectFileType (signature);
}
}
internal class TcdArchive : ArcFile
@ -93,7 +77,7 @@ namespace GameRes.Formats.TopCat
[Export(typeof(ArchiveFormat))]
public class TcdOpener : ArchiveFormat
{
public override string Tag { get { return "TCD3"; } }
public override string Tag { get { return "TCD"; } }
public override string Description { get { return "TopCat data archive"; } }
public override uint Signature { get { return 0x33444354; } } // 'TCD3'
public override bool IsHierarchic { get { return true; } }
@ -101,7 +85,7 @@ namespace GameRes.Formats.TopCat
public TcdOpener ()
{
Extensions = new string[] { "tcd" };
Signatures = new uint[] { 0x32444354, 0x33444354 }; // 'TCD2', 'TCD3'
}
public static Dictionary<string, int> KnownKeys = new Dictionary<string, int>();
@ -114,133 +98,71 @@ namespace GameRes.Formats.TopCat
public override ArcFile TryOpen (ArcView file)
{
int count = file.View.ReadInt32 (4);
if (!IsSaneCount (count))
int version = file.View.ReadByte (3) - '0';
TcdIndexReader reader;
if (2 == version)
reader = new TcdReaderV2 (file);
else
reader = new TcdReaderV3 (file);
using (reader)
{
var dir = reader.ReadIndex();
if (null == dir)
return null;
uint current_offset = 8;
var sections = new List<TcdSection> (5);
for (int i = 0; i < 5; ++i, current_offset += 0x20)
{
uint index_offset = file.View.ReadUInt32 (current_offset+4);
if (0 == index_offset)
continue;
var section = new TcdSection
{
IndexOffset = index_offset,
DataSize = file.View.ReadUInt32 (current_offset),
DirCount = file.View.ReadInt32 (current_offset+8),
DirNameLength = file.View.ReadInt32 (current_offset+0x0C),
FileCount = file.View.ReadInt32 (current_offset+0x10),
FileNameLength = file.View.ReadInt32 (current_offset+0x14),
};
sections.Add (section);
return new TcdArchive (file, this, dir);
}
var list = new List<Entry> (count);
foreach (var section in sections)
{
current_offset = section.IndexOffset;
uint dir_size = (uint)(section.DirCount * section.DirNameLength);
var dir_names = new byte[dir_size];
if (dir_size != file.View.Read (current_offset, dir_names, 0, dir_size))
return null;
current_offset += dir_size;
DecryptNames (dir_names, section.DirNameLength);
var dirs = new TcdDirEntry[section.DirCount];
for (int i = 0; i < dirs.Length; ++i)
{
dirs[i].FileCount = file.View.ReadInt32 (current_offset);
dirs[i].NamesOffset = file.View.ReadInt32 (current_offset+4);
dirs[i].FirstIndex = file.View.ReadInt32 (current_offset+8);
current_offset += 0x10;
}
uint entries_size = (uint)(section.FileCount * section.FileNameLength);
var file_names = new byte[entries_size];
if (entries_size != file.View.Read (current_offset, file_names, 0, entries_size))
return null;
current_offset += entries_size;
DecryptNames (file_names, section.FileNameLength);
var offsets = new uint[section.FileCount + 1];
for (int i = 0; i < offsets.Length; ++i)
{
offsets[i] = file.View.ReadUInt32 (current_offset);
current_offset += 4;
}
int dir_name_offset = 0;
foreach (var dir in dirs)
{
string dir_name = Binary.GetCString (dir_names, dir_name_offset, section.DirNameLength);
dir_name_offset += section.DirNameLength;
int index = dir.FirstIndex;
int name_offset = dir.NamesOffset;
for (int i = 0; i < dir.FileCount; ++i)
{
string name = Binary.GetCString (file_names, name_offset, section.FileNameLength);
name_offset += section.FileNameLength;
name = dir_name + '\\' + name;
var entry = new TcdEntry (index, name, file, offsets[index]);
entry.Size = offsets[index+1] - offsets[index];
++index;
list.Add (entry);
}
}
}
return new TcdArchive (file, this, list);
}
private void DecryptNames (byte[] buffer, int name_length)
{
byte key = buffer[name_length-1];
for (int i = 0; i < buffer.Length; ++i)
buffer[i] -= key;
}
public override Stream OpenEntry (ArcFile arc, Entry entry)
{
var tcde = entry as TcdEntry;
var tcda = arc as TcdArchive;
if (null == tcde || null == tcda || entry.Size <= 0x14)
if (null == tcde || null == tcda)
return arc.File.CreateStream (entry.Offset, entry.Size);
int signature = arc.File.View.ReadInt32 (entry.Offset);
if (0x43445053 == signature) // 'SPDC'
return arc.File.CreateStream (entry.Offset, entry.Size);
if (0x5367674F == signature) // 'OggS'
if (entry.Name.EndsWith (".SPD", StringComparison.InvariantCultureIgnoreCase))
return OpenSpdc (tcda, tcde);
if (entry.Name.EndsWith (".OGG", StringComparison.InvariantCultureIgnoreCase))
return RestoreOggStream (arc, entry);
if (entry.Name.EndsWith (".TSF", StringComparison.InvariantCultureIgnoreCase) ||
entry.Name.EndsWith (".TCT", StringComparison.InvariantCultureIgnoreCase))
return OpenScript (tcda, tcde);
return arc.File.CreateStream (entry.Offset, entry.Size);
}
var header = new byte[0x14];
arc.File.View.Read (entry.Offset, header, 0, 0x14);
Stream OpenSpdc (TcdArchive arc, TcdEntry entry)
{
int signature = arc.File.View.ReadInt32 (entry.Offset);
if (0x43445053 == signature || entry.Size <= 0x14) // 'SPDC'
return arc.File.CreateStream (entry.Offset, entry.Size);
var header = arc.File.View.ReadBytes (entry.Offset, 0x14);
byte header_key = (byte)(header[0x12] + header[0x10]);
header[0] -= header_key;
header[1] -= header_key;
header[2] -= header_key;
header[3] -= header_key;
bool spdc_entry = Binary.AsciiEqual (header, "SPDC");
bool spdc_entry = Binary.AsciiEqual (header, "SPD") && (header[3] == 'C' || header[3] == '8');
if (!spdc_entry)
{
LittleEndian.Pack (signature, header, 0);
if (null == tcda.Key)
if (null == arc.Key)
{
foreach (var key in KnownKeys.Values)
foreach (var key in TcdOpener.KnownKeys.Values)
{
int first = signature + key * (tcde.Index + 3);
int first = signature + key * (entry.Index + 3);
if (0x43445053 == first) // 'SPDC'
{
tcda.Key = key;
arc.Key = key;
spdc_entry = true;
break;
}
}
}
else if (0x43445053 == signature + tcda.Key.Value * (tcde.Index + 3))
else if (0x43445053 == (signature + arc.Key.Value * (entry.Index + 3)))
{
spdc_entry = true;
}
if (spdc_entry && 0 != tcda.Key.Value)
if (spdc_entry && 0 != arc.Key.Value)
{
unsafe
{
@ -248,21 +170,18 @@ namespace GameRes.Formats.TopCat
{
int* dw = (int*)raw;
for (int i = 0; i < 5; ++i)
dw[i] += tcda.Key.Value * (tcde.Index + 3 + i);
dw[i] += arc.Key.Value * (entry.Index + 3 + i);
}
}
}
if (!spdc_entry && entry.Name.StartsWith ("TXT\\", StringComparison.InvariantCultureIgnoreCase)
&& signature > 0 && signature < 0x01000000)
return OpenScript (tcda, tcde, signature);
}
var rest = arc.File.CreateStream (entry.Offset+0x14, entry.Size-0x14);
return new PrefixStream (header, rest);
}
Stream OpenScript (TcdArchive arc, TcdEntry entry, int unpacked_size)
Stream OpenScript (TcdArchive arc, TcdEntry entry)
{
int unpacked_size = arc.File.View.ReadInt32 (entry.Offset);
byte[] data = new byte[unpacked_size];
using (var input = arc.File.CreateStream (entry.Offset+4, entry.Size-4))
UnpackLz (input, data);
@ -336,4 +255,185 @@ namespace GameRes.Formats.TopCat
return new MemoryStream (data);
}
}
internal abstract class TcdIndexReader : IDisposable
{
BinaryReader m_input;
int m_section_count;
public int Count { get; private set; }
protected BinaryReader Input { get { return m_input; } }
protected TcdIndexReader (ArcView file, int section_count)
{
Count = file.View.ReadInt32 (4);
var input = file.CreateStream();
m_input = new BinaryReader (input);
m_section_count = section_count;
}
protected string[] Extensions = { ".TCT", ".TSF", ".SPD", ".OGG", ".WAV" };
public List<Entry> ReadIndex ()
{
if (!ArchiveFormat.IsSaneCount (Count))
return null;
var sections = ReadSections (m_section_count);
var list = new List<Entry> (Count);
foreach (var section in sections)
{
m_input.BaseStream.Position = section.IndexOffset;
var dir_names = m_input.ReadBytes (section.DirNamesSize);
if (section.DirNamesSize != dir_names.Length)
return null;
byte section_key = dir_names[section.DirNameLength-1];
DecryptNames (dir_names, section_key);
var dirs = new TcdDirEntry[section.DirCount];
for (int i = 0; i < dirs.Length; ++i)
{
dirs[i].FileCount = m_input.ReadInt32();
dirs[i].NamesOffset = m_input.ReadInt32();
dirs[i].FirstIndex = m_input.ReadInt32();
m_input.ReadInt32();
}
var file_names = m_input.ReadBytes (section.FileNamesSize);
if (file_names.Length != section.FileNamesSize)
return null;
DecryptNames (file_names, section_key);
var offsets = new uint[section.FileCount + 1];
for (int i = 0; i < offsets.Length; ++i)
{
offsets[i] = m_input.ReadUInt32();
}
int dir_name_offset = 0;
foreach (var dir in dirs)
{
string dir_name = GetName (dir_names, section.DirNameLength, ref dir_name_offset);
int index = dir.FirstIndex;
int name_offset = dir.NamesOffset;
for (int i = 0; i < dir.FileCount; ++i)
{
string name = GetName (file_names, section.FileNameLength, ref name_offset);
name = Path.Combine (dir_name, name);
name = Path.ChangeExtension (name, section.Extension);
var entry = FormatCatalog.Instance.Create<TcdEntry> (name);
entry.Offset = offsets[index];
entry.Size = offsets[index+1] - offsets[index];
entry.Index = index;
++index;
list.Add (entry);
}
}
}
return list;
}
IList<TcdSection> ReadSections (int count)
{
var sections = new List<TcdSection> (count);
uint current_offset = 8;
for (int i = 0; i < count; ++i)
{
m_input.BaseStream.Position = current_offset;
var section = ReadSection (i);
if (section != null)
sections.Add (section);
current_offset += 0x20;
}
return sections;
}
void DecryptNames (byte[] buffer, byte key)
{
for (int i = 0; i < buffer.Length; ++i)
buffer[i] -= key;
}
protected abstract TcdSection ReadSection (int number);
protected abstract string GetName (byte[] names, int name_length, ref int offset);
#region IDisposable Members
bool _disposed = false;
public void Dispose ()
{
if (!_disposed)
{
m_input.Dispose();
_disposed = true;
}
}
#endregion
}
internal class TcdReaderV2 : TcdIndexReader
{
public TcdReaderV2 (ArcView file) : base (file, 4)
{
}
protected override TcdSection ReadSection (int number)
{
uint data_size = Input.ReadUInt32();
if (0 == data_size)
return null;
var section = new TcdSection { DataSize = data_size };
section.Extension = Extensions[number];
section.FileCount = Input.ReadInt32();
section.DirCount = Input.ReadInt32();
section.IndexOffset = Input.ReadUInt32();
section.DirNameLength = Input.ReadInt32();
section.FileNameLength = Input.ReadInt32();
section.DirNamesSize = section.DirNameLength;
section.FileNamesSize = section.FileNameLength;
return section;
}
protected override string GetName (byte[] names, int name_length, ref int offset)
{
int name_end = Array.IndexOf<byte> (names, 0, offset);
if (-1 == name_end)
name_end = names.Length;
name_length = name_end - offset;
string name = Encodings.cp932.GetString (names, offset, name_length);
offset += name_length + 1;
return name;
}
}
internal class TcdReaderV3 : TcdIndexReader
{
public TcdReaderV3 (ArcView file) : base (file, 5)
{
}
protected override TcdSection ReadSection (int number)
{
uint data_size = Input.ReadUInt32();
uint index_offset = Input.ReadUInt32();
if (0 == index_offset)
return null;
var section = new TcdSection { DataSize = data_size };
section.Extension = Extensions[number];
section.IndexOffset = index_offset;
section.DirCount = Input.ReadInt32();
section.DirNameLength = Input.ReadInt32();
section.FileCount = Input.ReadInt32();
section.FileNameLength = Input.ReadInt32();
section.DirNamesSize = section.DirNameLength * section.DirCount;
section.FileNamesSize = section.FileNameLength * section.FileCount;
return section;
}
protected override string GetName (byte[] names, int name_length, ref int offset)
{
string name = Binary.GetCString (names, offset, name_length);
offset += name_length;
return name;
}
}
}

View File

@ -276,6 +276,7 @@ Ouka Ryouran<br/>
Oyako Ninjutsu Kunoichi PonPon!!<br/>
RGH ~Koi to Hero to Gakuen to~<br/>
Riding Incubus<br/>
Rui wa Tomo o Yobu<br/>
Seirei Tenshou<br/>
Se-kirara<br/>
Sharin no Kuni, Himawari no Shoujo<br/>
@ -291,12 +292,14 @@ Zettai Karen! Ojou-sama<br/>
<tr class="last"><td>*.tlg</td><td><tt>TLG0.0</tt><br/><tt>TLG5.0</tt><br/><tt>TLG6.0</tt></td><td>No</td></tr>
<tr class="odd"><td>*.ypf</td><td><tt>YPF</tt></td><td>Yes</td><td rowspan="2">YU-RIS</td><td rowspan="2">
Eroge! ~H mo Game mo Kaihatsu Zanmai~<br/>
Koi Mekuri Clover<br/>
Mamono Musume-tachi to no Rakuen ~Slime &amp; Scylla~<br/>
Mashou no Nie 3 ~Hakudaku no Umi ni Shizumu Injoku no Reiki~<br/>
Ryuuyoku no Melodia -Diva with the blessed dragonol-<br/>
Sei Monmusu Festival!!<br/>
Shin Chikan Ou<br/>
Unionism Quartet<br/>
Usotsuki Ouji to Nayameru Ohime-sama -Princess Syndrome-<br/>
</td></tr>
<tr class="odd last"><td>*.ycg</td><td><tt>YCG</tt></td><td>No</td></tr>
<tr><td>*.isa</td><td><tt>ISM ARCHIVED</tt></td><td>No</td><td rowspan="2">ISM</td><td rowspan="2">Green ~Akizora no Screen~</td></tr>
@ -333,6 +336,7 @@ Oshaburi Announcer<br/>
Sakura Machizaka Stories vol.1<br/>
Sakura Machizaka Stories vol.2<br/>
Shoujo Senki Soul Eater<br/>
Trouble Succubus<br/>
</td></tr>
<tr class="odd"><td>*.prs</td><td><tt>YB</tt></td><td>No</td></tr>
<tr class="odd last"><td>*.way</td><td><tt>WADY</tt></td><td>No</td></tr>
@ -415,6 +419,7 @@ Itsuka, Dokoka de ~Ano Ameoto no Kioku~<span class="footnote">2.36 or 2.37</span
Kichiku Nakadashi Suieibu<span class="footnote">ShiinaRio v2.41</span><br/>
Mahou Shoujo no Taisetsu na Koto <span class="footnote">ShiinaRio v2.47</span><br/>
Maki Fes! <span class="footnote">ShiinaRio v2.50</span><br/>
Mimi o Sumaseba <span class="footnote">ShiinaRio v2.47</span><br/>
Nagagutsu wo Haita Deco <span class="footnote">ShiinaRio v2.39</span><br/>
Najimi no Oba-chan <span class="footnote">ShiinaRio v2.47</span><br/>
Niizuma to Yuukaihan <span class="footnote">ShiinaRio v2.45</span><br/>
@ -741,6 +746,7 @@ Gigai no Alruna<br/>
</td></tr>
<tr class="odd"><td>sys4ini.bin<br/>sys3ini.bin<br/>*.alf</td><td><tt>S4IC</tt><br/><tt>S3IC</tt></td><td>No</td><td rowspan="2">Eushully</td><td rowspan="2">
Kuutei Senki ~Tasogare ni Shizumu Kusabi~<br/>
Mahou ga Sekai o Sukuimasu<br/>
Meishoku no Reiki<br/>
Soukai no Oujo-tachi<br/>
Soukai no Valkyria <br/>
@ -756,14 +762,15 @@ Saiminjutsu Re<br/>
Wizard Links<br/>
</td></tr>
<tr class="last"><td>*.med</td><td><tt>MD</tt></td><td>No</td></tr>
<tr class="odd"><td>*.tcd</td><td><tt>TCD3</tt></td><td>No</td><td rowspan="2">TopCat</td><td rowspan="2">
<tr class="odd"><td>*.tcd</td><td><tt>TCD2</tt><br/><tt>TCD3</tt></td><td>No</td><td rowspan="2">TopCat</td><td rowspan="2">
Atori no Sora to Shinchuu no Tsuki<br/>
Favorite Sweet!<br/>
Nanapuri<br/>
Sabae no Ou Scenario II<br/>
Soushinjutsu 3<br/>
Wakan Kazoku<br/>
</td></tr>
<tr class="odd last"><td>*.spd</td><td><tt>SPDC</tt></td><td>No</td></tr>
<tr class="odd last"><td>*.spd</td><td><tt>SPDC</tt><br/><tt>SPD8</tt></td><td>No</td></tr>
<tr><td>*.dat</td><td><tt>CHE00</tt><br/><tt>MYK00</tt></td><td>No</td><td>Cherry Soft</td><td>
Blood Royal<br/>
Heroine<br/>
@ -1051,6 +1058,7 @@ Gakkou Yarashii Kaidan<br/>
Hanamaru! 2<br/>
Hime Kami 1/2<br/>
In'youchuu Goku ~Ryoujoku Jigoku Taimaroku~<br/>
In'youchuu Kyou ~Ryoujoku Byoutou Taimaroku~<br/>
In'youchuu Rei ~Ryoujoku Shiro Taima Emaki~<br/>
In'youchuu Shoku ~Ryoushokutou Taimaroku~<br/>
Koitsuma Biyori ~Yukino-san wa Hitozuma Kanrinin~<br/>
@ -1140,10 +1148,11 @@ Triangle Heart 1-2-3<br/>
Pretty Devil Paradise ~Millium Makai Dakkan Shirei~<br/>
S Sensei no Koto<br/>
</td></tr>
<tr class="odd"><td>*.epk</td><td><tt>EPK</tt></td><td>No</td><td rowspan="2">TamaSoft</td><td rowspan="2">
<tr class="odd"><td>*.epk</td><td><tt>EPK</tt></td><td>No</td><td rowspan="3">TamaSoft</td><td rowspan="3">
Lost Child<br/>
</td></tr>
<tr class="odd last"><td>*.sur</td><td><tt>ESUR</tt></td><td>No</td></tr>
<tr class="odd"><td>*.sur</td><td><tt>ESUR</tt></td><td>No</td></tr>
<tr class="odd last"><td>*.esd</td><td><tt>ESD</tt></td><td>No</td></tr>
</table>
<p><a name="note-1" class="footnote">1</a> Non-encrypted only</p>
</body>