Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implemented '.zt' archives. #481

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions ArcFormats/ArcFormats.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@
<Compile Include="AdvSys\ImageGWD.cs" />
<Compile Include="Ail\ArcLNK2.cs" />
<Compile Include="BlueGale\ImageBBM.cs" />
<Compile Include="CatSystem\ArcZT.cs" />
<Compile Include="JamCreation\ArcDAT.cs" />
<Compile Include="JamCreation\ImageDPO.cs" />
<Compile Include="AliceSoft\ArcAAR.cs" />
Expand Down
120 changes: 120 additions & 0 deletions ArcFormats/CatSystem/ArcZT.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
//! \file ArcZT.cs
//! \date 2021 May 25
//! \brief CatSystem2 pack file.
//
// Copyright (C) 2021 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.Collections.Generic;
using System.ComponentModel.Composition;
using System.IO;
using GameRes.Compression;

namespace GameRes.Formats.CatSystem
{
[Export(typeof(ArchiveFormat))]
public class ZtOpener : ArchiveFormat
{
public override string Tag { get { return "ZT/PACK"; } }
public override string Description { get { return "CatSystem2 pack file"; } }
public override uint Signature { get { return 0; } }
public override bool IsHierarchic { get { return true; } }
public override bool CanWrite { get { return false; } }

public ZtOpener ()
{
Extensions = new string[] { "zt" };
}

struct ZtSubdirectory
{
public string Name;
public long Offset;
}

public override ArcFile TryOpen (ArcView file)
{
if (file.MaxOffset < 0x11C)
return null;
if (0x110 > file.View.ReadUInt32 (8) || 1 < file.View.ReadUInt32 (0xC))
return null; // First entry is too small, or not a file or folder
var dir = new List<Entry> ();
var subdirs = new Queue<ZtSubdirectory> ();
const string sep = "\\";
string parent_name = "";
long offset = 0;
uint offset_next;
do
{
offset_next = file.View.ReadUInt32 (offset);
uint entry_size = file.View.ReadUInt32 (offset+8);
if (0 != offset_next && 0xC + entry_size > offset_next)
return null;
uint attributes = file.View.ReadUInt32 (offset+0xC);
string name = file.View.ReadString (offset+0x10, 0x104);
if (1 < attributes || 0 == name.Length)
return null;

if (0 == attributes)
{
var entry = FormatCatalog.Instance.Create<PackedEntry> (parent_name + name);
uint packed_size = file.View.ReadUInt32 (offset+0x114);
if (0x110 + packed_size != entry_size)
return null;
entry.Offset = offset + 0x11C;
entry.Size = packed_size;
entry.UnpackedSize = file.View.ReadUInt32 (offset+0x118);
entry.IsPacked = true;
if (!entry.CheckPlacement (file.MaxOffset))
return null;
dir.Add (entry);
}
else if (0 != file.View.ReadUInt32 (offset+4))
{
subdirs.Enqueue (new ZtSubdirectory { Name = parent_name + name + sep, Offset = offset });
}

if (0 == offset_next && 0 != subdirs.Count)
{ // No more entries in current directory, go to next subdirectory
var subdir = subdirs.Dequeue ();
parent_name = subdir.Name;
offset = subdir.Offset;
offset_next = file.View.ReadUInt32 (offset+4); // offset_child
if (0xC + file.View.ReadUInt32 (offset+8) > offset_next)
return null;
}
offset += offset_next;
}
while (0 != offset_next);

return new ArcFile (file, this, dir);
}

public override Stream OpenEntry (ArcFile arc, Entry entry)
{
var pentry = (PackedEntry)entry;
if (0 == pentry.UnpackedSize)
return Stream.Null;
var input = arc.File.CreateStream (entry.Offset, entry.Size);
return new ZLibStream (input, CompressionMode.Decompress);
}
}
}