implemented DDP archives.

This commit is contained in:
morkt 2016-07-28 04:54:42 +04:00
parent ebcb29a44e
commit 2f9964c166
3 changed files with 233 additions and 18 deletions

View File

@ -0,0 +1,197 @@
//! \file ArcDDP.cs
//! \date Wed Jul 27 13:21:01 2016
//! \brief DDSystem resource archive.
//
// Copyright (C) 2016 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;
using GameRes.Formats.SHSystem;
using GameRes.Utility;
namespace GameRes.Formats.DDSystem
{
[Export(typeof(ArchiveFormat))]
public class Ddp2Opener : ArchiveFormat
{
public override string Tag { get { return "DDP2"; } }
public override string Description { get { return "DDSystem engine resource archive"; } }
public override uint Signature { get { return 0x32504444; } } // 'DDP2'
public override bool IsHierarchic { get { return false; } }
public override bool CanCreate { get { return false; } }
public Ddp2Opener ()
{
Extensions = new string[] { "dat" };
}
public override ArcFile TryOpen (ArcView file)
{
int count = file.View.ReadInt32 (4);
if (!IsSaneCount (count))
return null;
var base_name = Path.GetFileNameWithoutExtension (file.Name);
uint index_offset = 0x20;
var dir = new List<Entry> (count);
for (int i = 0; i < count; ++i)
{
var entry = new PackedEntry {
Offset = file.View.ReadUInt32 (index_offset),
UnpackedSize = file.View.ReadUInt32 (index_offset+4),
Size = file.View.ReadUInt32 (index_offset+8),
Name = string.Format ("{0}#{1:D5}", base_name, i),
};
entry.IsPacked = entry.Size != 0;
if (!entry.IsPacked)
entry.Size = entry.UnpackedSize;
if (!entry.CheckPlacement (file.MaxOffset))
return null;
index_offset += 0x10;
dir.Add (entry);
}
DetectFileTypes (file, dir);
return new ArcFile (file, this, dir);
}
internal static void DetectFileTypes (ArcView file, List<Entry> dir)
{
byte[] signature_buffer = new byte[4];
foreach (PackedEntry entry in dir)
{
uint signature;
if (entry.IsPacked)
{
using (var input = file.CreateStream (entry.Offset, Math.Min (entry.Size, 0x20u)))
using (var reader = new ShsCompression (input))
{
reader.Unpack (signature_buffer);
signature = LittleEndian.ToUInt32 (signature_buffer, 0);
}
}
else
{
signature = file.View.ReadUInt32 (entry.Offset);
}
if (0x78534444 == signature) // 'DDSx'
{
entry.Type = "script";
entry.Name = Path.ChangeExtension (entry.Name, "hxb");
}
else if (0 != signature)
{
IResource res;
if (0x020000 == signature || 0x0A0000 == signature)
res = ImageFormat.Tga;
else
res = AutoEntry.DetectFileType (signature);
if (res != null)
{
entry.Type = res.Type;
var ext = res.Extensions.FirstOrDefault();
if (!string.IsNullOrEmpty (ext))
entry.Name = Path.ChangeExtension (entry.Name, ext);
}
}
}
}
public override Stream OpenEntry (ArcFile arc, Entry entry)
{
var input = arc.File.CreateStream (entry.Offset, entry.Size);
var pent = entry as PackedEntry;
if (null == pent || !pent.IsPacked)
return input;
var data = new byte[pent.UnpackedSize];
using (var reader = new ShsCompression (input))
{
reader.Unpack (data);
if (data.Length > 16 && Binary.AsciiEqual (data, 0, "DDSxHXB"))
DecryptHxb (data);
return new MemoryStream (data);
}
}
internal void DecryptHxb (byte[] data)
{
int length = data[8] << 16 | data[9] << 8 | data[10];
if (length != data.Length)
return;
int key = (((length << 5) ^ 0xA5) * (length + 0x6F349)) ^ 0x34A9B129;
var key_bits = new byte[4];
LittleEndian.Pack (key, key_bits, 0);
for (int i = 0x10; i < data.Length; ++i)
{
data[i] ^= key_bits[i & 3];
}
}
}
[Export(typeof(ArchiveFormat))]
public class Ddp3Opener : Ddp2Opener
{
public override string Tag { get { return "DDP3"; } }
public override string Description { get { return "DDSystem engine resource archive"; } }
public override uint Signature { get { return 0x33504444; } } // 'DDP3'
public override bool IsHierarchic { get { return false; } }
public override bool CanCreate { get { return false; } }
public override ArcFile TryOpen (ArcView file)
{
int count = file.View.ReadInt32 (4);
if (!IsSaneCount (count))
return null;
var index = Him5Opener.ReadIndex (file, 0x20, count);
var dir = new List<Entry>();
foreach (var section in index)
{
int index_offset = section.Item1;
for (int section_size = section.Item2; section_size > 0; )
{
int entry_size = file.View.ReadByte (index_offset);
if (entry_size < 17)
break;
var entry = new PackedEntry {
Offset = file.View.ReadUInt32 (index_offset+1),
UnpackedSize = file.View.ReadUInt32 (index_offset+5),
Size = file.View.ReadUInt32 (index_offset+9),
Name = file.View.ReadString (index_offset+17, (uint)entry_size-17),
};
entry.IsPacked = entry.Size != 0;
if (!entry.IsPacked)
entry.Size = entry.UnpackedSize;
if (!entry.CheckPlacement (file.MaxOffset))
return null;
index_offset += entry_size;
section_size -= entry_size;
dir.Add (entry);
}
}
DetectFileTypes (file, dir);
return new ArcFile (file, this, dir);
}
}
}

View File

@ -81,9 +81,9 @@ namespace GameRes.Formats.SHSystem
{ {
uint packed_size = file.View.ReadUInt32 (entry.Offset); uint packed_size = file.View.ReadUInt32 (entry.Offset);
uint unpacked_size = file.View.ReadUInt32 (entry.Offset+4); uint unpacked_size = file.View.ReadUInt32 (entry.Offset+4);
if (0 == packed_size) entry.IsPacked = 0 != packed_size;
if (!entry.IsPacked)
packed_size = unpacked_size; packed_size = unpacked_size;
entry.IsPacked = packed_size != unpacked_size;
entry.Size = packed_size; entry.Size = packed_size;
entry.UnpackedSize = unpacked_size; entry.UnpackedSize = unpacked_size;
entry.Offset += 8; entry.Offset += 8;
@ -104,12 +104,10 @@ namespace GameRes.Formats.SHSystem
if (0 != signature) if (0 != signature)
{ {
IResource res; IResource res;
if (0x4D42 == (signature & 0xFFFF)) if (0x020000 == signature || 0x0A0000 == signature)
res = ImageFormat.Bmp;
else if (0x020000 == signature || 0x0A0000 == signature)
res = ImageFormat.Tga; res = ImageFormat.Tga;
else else
res = FormatCatalog.Instance.LookupSignature (signature).FirstOrDefault(); res = AutoEntry.DetectFileType (signature);
if (res != null) if (res != null)
{ {
entry.Type = res.Type; entry.Type = res.Type;
@ -158,20 +156,11 @@ namespace GameRes.Formats.SHSystem
if (!IsSaneCount (count)) if (!IsSaneCount (count))
return null; return null;
int index_offset = 8; var index = ReadIndex (file, 8, count);
var index = new List<Tuple<int, int>> (count);
for (int i = 0; i < count; ++i)
{
int size = file.View.ReadInt32 (index_offset);
int offset = file.View.ReadInt32 (index_offset+4);
index_offset += 8;
if (size != 0)
index.Add (Tuple.Create (offset, size));
}
var dir = new List<Entry>(); var dir = new List<Entry>();
foreach (var section in index) foreach (var section in index)
{ {
index_offset = section.Item1; int index_offset = section.Item1;
for (int section_size = section.Item2; section_size > 0; ) for (int section_size = section.Item2; section_size > 0; )
{ {
int entry_size = file.View.ReadByte (index_offset); int entry_size = file.View.ReadByte (index_offset);
@ -191,6 +180,20 @@ namespace GameRes.Formats.SHSystem
DetectFileTypes (file, dir); DetectFileTypes (file, dir);
return new ArcFile (file, this, dir); return new ArcFile (file, this, dir);
} }
internal static List<Tuple<int, int>> ReadIndex (ArcView file, uint index_offset, int count)
{
var index = new List<Tuple<int, int>> (count);
for (int i = 0; i < count; ++i)
{
int size = file.View.ReadInt32 (index_offset);
int offset = file.View.ReadInt32 (index_offset+4);
index_offset += 8;
if (size != 0)
index.Add (Tuple.Create (offset, size));
}
return index;
}
} }
internal class ShsCompression : IDisposable internal class ShsCompression : IDisposable

View File

@ -68,6 +68,7 @@ Eiyuu x Maou<br/>
Wasurenagusa ~Forget-me-Not~<br/> Wasurenagusa ~Forget-me-Not~<br/>
</td></tr> </td></tr>
<tr class="odd"><td>*.gsp</td><td>-</td><td>No</td><td rowspan="2">GSD</td><td rowspan="2"> <tr class="odd"><td>*.gsp</td><td>-</td><td>No</td><td rowspan="2">GSD</td><td rowspan="2">
I Cup Bonyuu Kangofu<br/>
Natsuiro ☆ Communication ♪<br/> Natsuiro ☆ Communication ♪<br/>
Saimin Gakuen<br/> Saimin Gakuen<br/>
</td></tr> </td></tr>
@ -456,6 +457,7 @@ Chijoku no Kankei ~Inkou Kyoushi~<br/>
Chijoku no Kankei 2<br/> Chijoku no Kankei 2<br/>
Jii -Nozoki no Houshuu-<br/> Jii -Nozoki no Houshuu-<br/>
Itazura ZERO<br/> Itazura ZERO<br/>
Oira wa Bandai 2<br/>
</td></tr> </td></tr>
<tr><td>*.kg</td><td><tt>GCGK</tt></td><td>No</td></tr> <tr><td>*.kg</td><td><tt>GCGK</tt></td><td>No</td></tr>
<tr class="odd"><td>*.noa<br/>*.dat</td><td><tt>Entis\x1a</tt></td><td>No</td><td rowspan="2">Entis GLS</td><td rowspan="2"> <tr class="odd"><td>*.noa<br/>*.dat</td><td><tt>Entis\x1a</tt></td><td>No</td><td rowspan="2">Entis GLS</td><td rowspan="2">
@ -472,6 +474,7 @@ Santaful☆Summer<br/>
Space☆Trouble<br/> Space☆Trouble<br/>
Tonarizuma<br/> Tonarizuma<br/>
Yatohime Zankikou<br/> Yatohime Zankikou<br/>
Zettai Joshi Ryouiki!<br/>
</td></tr> </td></tr>
<tr class="odd"><td>*.eri<br/>*.mio</td><td><tt>Entis\x1a</tt></td><td>No</td></tr> <tr class="odd"><td>*.eri<br/>*.mio</td><td><tt>Entis\x1a</tt></td><td>No</td></tr>
<tr><td>*.saf</td><td>-</td><td>No</td><td>Lune</td><td>Rinkan Gakuen</td></tr> <tr><td>*.saf</td><td>-</td><td>No</td><td>Lune</td><td>Rinkan Gakuen</td></tr>
@ -570,8 +573,9 @@ Toshiue Lesson ~Mama to Oba-san to Sensei to~<br/>
Tutorial Summer<br/> Tutorial Summer<br/>
</td></tr> </td></tr>
<tr><td>*.iks</td><td><tt>NPSR</tt></td><td>No</td><td>X[iks]</td><td>Shikkan ~Hazukashimerareta Karada, Oreta Kokoro~</td></tr> <tr><td>*.iks</td><td><tt>NPSR</tt></td><td>No</td><td>X[iks]</td><td>Shikkan ~Hazukashimerareta Karada, Oreta Kokoro~</td></tr>
<tr class="odd"><td>*.wbp</td><td><tt>ARCFORM3 WBUG</tt></td><td>No</td><td rowspan="4">Wild Bug</td><td rowspan="4"> <tr class="odd"><td>*.wbp</td><td><tt>ARCFORM3 WBUG</tt><br/><tt>ARCFORM4 WBUG</tt></td><td>No</td><td rowspan="4">Wild Bug</td><td rowspan="4">
Happy Planning<br/> Happy Planning<br/>
Ryoujoku Hitozuma Club<br/>
Yuukyou Gangu 2<br/> Yuukyou Gangu 2<br/>
</td></tr> </td></tr>
<tr class="odd"><td>*.wbm</td><td><tt>WPX\x1ABMP</tt></td><td>No</td></tr> <tr class="odd"><td>*.wbm</td><td><tt>WPX\x1ABMP</tt></td><td>No</td></tr>
@ -965,6 +969,17 @@ Zoku Etsuraku no Tane<br/>
<tr class="odd"><td>*.bin</td><td><tt>OZ</tt></td><td>No</td><td>Patisserie</td><td> <tr class="odd"><td>*.bin</td><td><tt>OZ</tt></td><td>No</td><td>Patisserie</td><td>
Ore no Tamanokoshi ni Notte Kure<br/> Ore no Tamanokoshi ni Notte Kure<br/>
</td></tr> </td></tr>
<tr><td>*.arc</td><td>-</td><td>No</td><td>KISS</td><td>
xx na Kanojo no Tsukurikata<br/>
</td></tr>
<tr class="odd"><td>*.pak</td><td><tt>IPAC</tt></td><td>No</td><td rowspan="3">Nounai Kanojo</td><td rowspan="3">
Osananajimi wa Bed Yakuza!<br/>
</td></tr>
<tr class="odd"><td>*.ies</td><td><tt>IES2</tt></td><td>No</td></tr>
<tr class="odd"><td>*.wst</td><td><tt>WST2</tt></td><td>No</td></tr>
<tr><td>*.dat</td><td><tt>DDP2</tt><br/><tt>DDP3</tt></td><td>No</td><td>DDSystem</td><td>
Nukiani!! Sweet Home<br/>
</td></tr>
</table> </table>
<p><a name="note-1" class="footnote">1</a> Non-encrypted only</p> <p><a name="note-1" class="footnote">1</a> Non-encrypted only</p>
</body> </body>