GARbro-mirror/ArcFormats/NitroPlus/ArcPAK.cs

118 lines
4.7 KiB
C#
Raw Normal View History

2016-05-21 14:31:09 +08:00
//! \file ArcPAK.cs
//! \date Sat May 21 00:12:14 2016
//! \brief MAGI resource archive.
//
2018-11-17 04:41:35 +08:00
// Copyright (C) 2016-2018 by morkt
2016-05-21 14:31:09 +08:00
//
// 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 GameRes.Compression;
namespace GameRes.Formats.Magi
{
// this format is very similar to NitroPlus.PakOpener v3, but has no encryption.
//
[Export(typeof(ArchiveFormat))]
public class PakOpener : ArchiveFormat
{
public override string Tag { get { return "PAK/MAGI"; } }
public override string Description { get { return "MAGI resource archive"; } }
public override uint Signature { get { return 3; } }
public override bool IsHierarchic { get { return true; } }
public override bool CanWrite { get { return false; } }
2016-05-21 14:31:09 +08:00
2018-11-17 04:41:35 +08:00
public PakOpener ()
{
Signatures = new uint[] { 3, 4 };
}
2016-05-21 14:31:09 +08:00
public override ArcFile TryOpen (ArcView file)
{
2018-11-17 04:41:35 +08:00
int version = file.View.ReadInt32 (0);
2016-05-21 14:31:09 +08:00
int count = file.View.ReadInt32 (4);
if (!IsSaneCount (count))
return null;
uint index_size = file.View.ReadUInt32 (0xC);
2016-05-24 02:40:49 +08:00
if (index_size < 2 || index_size > file.MaxOffset)
2016-05-21 14:31:09 +08:00
return null;
long base_offset = 0x118 + index_size;
using (var mem = file.CreateStream (0x118, index_size))
using (var z = new ZLibStream (mem, CompressionMode.Decompress))
2018-11-17 04:41:35 +08:00
using (var index = new BinaryStream (z, file.Name))
2016-05-21 14:31:09 +08:00
{
var dir = new List<Entry> (count);
2018-11-17 04:41:35 +08:00
string cur_dir = "";
2016-05-21 14:31:09 +08:00
for (int i = 0; i < count; ++i)
{
int name_length = index.ReadInt32();
2018-11-17 04:41:35 +08:00
if (name_length <= 0)
2016-05-21 14:31:09 +08:00
return null;
2018-11-17 04:41:35 +08:00
var name = index.ReadCString (name_length);
if (version > 3)
{
bool is_dir = index.ReadInt32() != 0;
if (is_dir)
{
cur_dir = name;
index.ReadInt64();
index.ReadInt32();
index.ReadInt64();
continue;
}
if (cur_dir.Length > 0)
name = Path.Combine (cur_dir, name);
}
var entry = Create<PackedEntry> (name);
2016-10-14 16:34:25 +08:00
entry.Offset = index.ReadUInt32() + base_offset;
entry.UnpackedSize = index.ReadUInt32();
2016-05-21 14:31:09 +08:00
index.ReadUInt32();
2016-10-14 16:34:25 +08:00
uint is_packed = index.ReadUInt32();
uint packed_size = index.ReadUInt32();
entry.IsPacked = is_packed != 0 && packed_size != 0;
2016-05-21 14:31:09 +08:00
if (entry.IsPacked)
entry.Size = packed_size;
else
2016-10-14 16:34:25 +08:00
entry.Size = entry.UnpackedSize;
2016-05-21 14:31:09 +08:00
if (!entry.CheckPlacement (file.MaxOffset))
return null;
dir.Add (entry);
}
return new ArcFile (file, this, dir);
}
}
public override Stream OpenEntry (ArcFile arc, Entry entry)
{
2018-11-17 04:41:35 +08:00
Stream input = arc.File.CreateStream (entry.Offset, entry.Size, entry.Name);
2016-05-21 14:31:09 +08:00
var pentry = entry as PackedEntry;
2018-11-17 04:41:35 +08:00
if (null != pentry && pentry.IsPacked)
2016-10-14 16:34:25 +08:00
input = new ZLibStream (input, CompressionMode.Decompress);
return input;
2016-05-21 14:31:09 +08:00
}
}
}