implemented NPK2 archives (#16).

compressed entries not supported.
This commit is contained in:
morkt 2016-02-17 12:01:40 +04:00
parent b42a425b80
commit c912ddd5a7
4 changed files with 211 additions and 0 deletions

View File

@ -63,6 +63,8 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="Actgs\ArcDAT.cs" /> <Compile Include="Actgs\ArcDAT.cs" />
<Compile Include="ArcCG.cs" />
<Compile Include="NitroPlus\ArcNPK.cs" />
<Compile Include="Softpal\ArcPAC.cs" /> <Compile Include="Softpal\ArcPAC.cs" />
<Compile Include="Softpal\AudioBGM.cs" /> <Compile Include="Softpal\AudioBGM.cs" />
<Compile Include="Softpal\ImagePGD.cs" /> <Compile Include="Softpal\ImagePGD.cs" />

View File

@ -0,0 +1,203 @@
//! \file ArcNPK.cs
//! \date Sat Feb 06 06:07:52 2016
//! \brief Mware 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 System.Security.Cryptography;
namespace GameRes.Formats.NitroPlus
{
internal class NpkEntry : PackedEntry
{
public readonly List<NpkSegment> Segments = new List<NpkSegment>();
}
internal class NpkSegment
{
public long Offset;
public uint AlignedSize;
public uint Size;
public uint UnpackedSize;
}
internal class NpkArchive : ArcFile
{
public readonly Aes Encryption;
public NpkArchive (ArcView arc, ArchiveFormat impl, ICollection<Entry> dir, Aes enc)
: base (arc, impl, dir)
{
Encryption = enc;
}
#region IDisposable Members
bool _npk_disposed = false;
protected override void Dispose (bool disposing)
{
if (_npk_disposed)
return;
if (disposing)
Encryption.Dispose();
_npk_disposed = true;
base.Dispose (disposing);
}
#endregion
}
[Export(typeof(ArchiveFormat))]
public class NpkOpener : ArchiveFormat
{
public override string Tag { get { return "NPK"; } }
public override string Description { get { return "Mware engine resource archive"; } }
public override uint Signature { get { return 0x324B504E; } } // 'NPK2'
public override bool IsHierarchic { get { return true; } }
public override bool CanCreate { get { return false; } }
public static Dictionary<string, byte[]> KnownKeys = new Dictionary<string, byte[]>();
public override ResourceScheme Scheme
{
get { return new Npk2Scheme { KnownKeys = KnownKeys }; }
set { KnownKeys = ((Npk2Scheme)value).KnownKeys; }
}
public override ArcFile TryOpen (ArcView file)
{
int count = file.View.ReadInt32 (0x18);
if (!IsSaneCount (count))
return null;
var key = QueryEncryption();
if (null == key)
return null;
var aes = Aes.Create();
try
{
aes.Mode = CipherMode.CBC;
aes.Padding = PaddingMode.PKCS7;
aes.Key = key;
aes.IV = file.View.ReadBytes (8, 0x10);
uint index_size = file.View.ReadUInt32 (0x1C);
using (var decryptor = aes.CreateDecryptor())
using (var enc_index = file.CreateStream (0x20, index_size))
using (var dec_index = new CryptoStream (enc_index, decryptor, CryptoStreamMode.Read))
using (var index = new ArcView.Reader (dec_index))
{
var dir = ReadIndex (index, count, file.MaxOffset);
if (null == dir)
return null;
var arc = new NpkArchive (file, this, dir, aes);
aes = null; // object ownership passed to NpkArchive, don't dispose
return arc;
}
}
finally
{
if (aes != null)
aes.Dispose();
}
}
List<Entry> ReadIndex (BinaryReader index, int count, long max_offset)
{
var name_buffer = new byte[0x80];
var dir = new List<Entry> (count);
for (int i = 0; i < count; ++i)
{
index.ReadByte();
int name_length = index.ReadUInt16();
if (name_length > name_buffer.Length)
name_buffer = new byte[name_length];
index.Read (name_buffer, 0, name_length);
var name = Encodings.cp932.GetString (name_buffer, 0, name_length);
var entry = FormatCatalog.Instance.Create<NpkEntry> (name);
entry.UnpackedSize = index.ReadUInt32();
index.Read (name_buffer, 0, 0x20); // skip
int segment_count = index.ReadInt32();
if (segment_count <= 0)
return null;
entry.Segments.Capacity = segment_count;
uint packed_size = 0;
for (int j = 0; j < segment_count; ++j)
{
var segment = new NpkSegment();
segment.Offset = index.ReadInt64();
segment.AlignedSize = index.ReadUInt32();
segment.Size = index.ReadUInt32();
segment.UnpackedSize = index.ReadUInt32();
entry.Segments.Add (segment);
packed_size += segment.AlignedSize;
}
entry.Offset = entry.Segments[0].Offset;
entry.Size = packed_size;
if (!entry.CheckPlacement (max_offset))
return null;
dir.Add (entry);
}
return dir;
}
public override Stream OpenEntry (ArcFile arc, Entry entry)
{
var narc = arc as NpkArchive;
var nent = entry as NpkEntry;
if (null == narc || null == nent)
return base.OpenEntry (arc, entry);
if (1 == nent.Segments.Count)
{
var input = narc.File.CreateStream (nent.Segments[0].Offset, nent.Segments[0].AlignedSize);
var decryptor = narc.Encryption.CreateDecryptor();
return new CryptoStream (input, decryptor, CryptoStreamMode.Read);
}
var output = new MemoryStream ((int)nent.Size);
foreach (var segment in nent.Segments)
{
using (var decryptor = narc.Encryption.CreateDecryptor())
using (var input = narc.File.CreateStream (segment.Offset, segment.AlignedSize))
using (var dec = new CryptoStream (input, decryptor, CryptoStreamMode.Read))
dec.CopyTo (output);
}
output.Position = 0;
return output;
}
byte[] QueryEncryption ()
{
if (0 == KnownKeys.Count)
return null;
return KnownKeys.Values.First();
}
}
[Serializable]
public class Npk2Scheme : ResourceScheme
{
public Dictionary<string, byte[]> KnownKeys;
}
}

Binary file not shown.

View File

@ -156,6 +156,7 @@ Folklore Jam<br/>
I/O<br/> I/O<br/>
Natsuiro Kouen ~Denpatou no Shita de Ai wo Kataru~<br/> Natsuiro Kouen ~Denpatou no Shita de Ai wo Kataru~<br/>
Onegai O-Hoshi-sama<br/> Onegai O-Hoshi-sama<br/>
Reijou Caster 2<br/>
Toriko Hime ~Hakudaku Mamire no Reijou~<br/> Toriko Hime ~Hakudaku Mamire no Reijou~<br/>
Tsuma Youji<br/> Tsuma Youji<br/>
Tsuma Youji 2<br/> Tsuma Youji 2<br/>
@ -295,6 +296,7 @@ Zansho Omimai Moushiagemasu<span class="footnote">ShiinaRio v2.41</span><br/>
<tr><td>*.pad</td><td><tt>PAD</tt></td><td>No</td></tr> <tr><td>*.pad</td><td><tt>PAD</tt></td><td>No</td></tr>
<tr class="odd"><td>*</td><td><tt>ARC2</tt><br/><tt>ARC1</tt></td><td>No</td><td>AST</td><td> <tr class="odd"><td>*</td><td><tt>ARC2</tt><br/><tt>ARC1</tt></td><td>No</td><td>AST</td><td>
Jokyoushi wo Kurau<br/> Jokyoushi wo Kurau<br/>
Lovers Collection<br/>
Nachtmusik<br/> Nachtmusik<br/>
Nachtmusik Another<br/> Nachtmusik Another<br/>
Samayou Mitama ni Yasuragi no Toki wo<br/> Samayou Mitama ni Yasuragi no Toki wo<br/>
@ -360,6 +362,7 @@ Iroha ~Aki no Yuuhi ni Kagefumi o~<br/>
Konneko<br/> Konneko<br/>
Natsu no Ame<br/> Natsu no Ame<br/>
Nerawareta Megami Tenshi Angeltia<br/> Nerawareta Megami Tenshi Angeltia<br/>
re-Laive<br/>
Sakura Musubi<br/> Sakura Musubi<br/>
Santaful☆Summer<br/> Santaful☆Summer<br/>
Space☆Trouble<br/> Space☆Trouble<br/>
@ -678,6 +681,9 @@ Houmon Hanbai ~Otona no Omocha Irimasen ka?~<br/>
Unity Marriage ~Futari no Hanayome~<br/> Unity Marriage ~Futari no Hanayome~<br/>
</td></tr> </td></tr>
<tr class="odd"><td>*.pgd</td><td><tt>GE</tt></td><td>No</td></tr> <tr class="odd"><td>*.pgd</td><td><tt>GE</tt></td><td>No</td></tr>
<tr><td>*.npk</td><td><tt>NPK2</tt></td><td>No</td><td>Nitro+</td><td>
Tokyo Necro<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>