add KLZ arc

This commit is contained in:
ManicSteiner
2025-01-20 11:22:36 +08:00
parent 480fe0e579
commit 7971bd0d4d
3 changed files with 124 additions and 0 deletions

View File

@@ -144,6 +144,7 @@
<Compile Include="GScripter\ArcDATA.cs" />
<Compile Include="Ism\ImagePNG.cs" />
<Compile Include="Kid\ArcDATRAW.cs" />
<Compile Include="Kid\ArcKLZ.cs" />
<Compile Include="Kid\ImageBIP.cs" />
<Compile Include="Kid\ImageBIParc.cs" />
<Compile Include="Kid\ImageKLZ.cs" />

122
ArcFormats/Kid/ArcKLZ.cs Normal file
View File

@@ -0,0 +1,122 @@
using GameRes.Utility;
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.IO;
namespace GameRes.Formats.Kid
{
[Export(typeof(ArchiveFormat))]
public class KlzOpener: ArchiveFormat
{
public override string Tag { get { return "KLZ/KID PS2"; } }
public override string Description { get { return "KID PS2 compressed image format with multi TIM2"; } }
public override uint Signature { get { return 0; } }
public override bool IsHierarchic { get { return false; } }
public override bool CanWrite { get { return false; } }
public KlzOpener()
{
Extensions = new string[] { "klz" };
}
public override ArcFile TryOpen(ArcView file)
{
if (!file.Name.HasExtension(".klz"))
return null;
uint unpacked_size = Binary.BigEndian(file.View.ReadUInt32(0));
if (unpacked_size <= 0x20 || unpacked_size > 0x5000000)
return null;
var backend = file.CreateStream();
var input = KlzFormat.LzhStreamDecode(backend);
var base_name = Path.GetFileNameWithoutExtension(file.Name);
var dir = GetEntries(input, base_name);
if (dir == null || dir.Count == 0)
{
return null;
}
else
{
return new KlzArchive(file, this, dir, input);
}
//throw new NotImplementedException();
}
public override Stream OpenEntry(ArcFile arc, Entry entry)
{
return new StreamRegion(((KlzArchive)arc).Source, entry.Offset, entry.Size, true);
}
internal static List<Entry> GetEntries (Stream input, string base_name)
{
var entries = new List<Entry>();
BinaryReader m_input = new ArcView.Reader(input);
int count = 0;
m_input.BaseStream.Position = 0;
while (m_input.BaseStream.Position < m_input.BaseStream.Length)
{
while (true)
{
try
{
uint sign = m_input.ReadUInt32();
m_input.ReadBytes(12);
if (sign == 0x324D4954) //TIM2
{
//m_input.BaseStream.Seek(-4, SeekOrigin.Current);
break;
}
}
catch (EndOfStreamException)
{
return entries;
}
}
long tell = m_input.BaseStream.Position - 16;
uint size = m_input.ReadUInt32() + 16;
string name = base_name + "_" + count.ToString("D2");
if (tell + size > m_input.BaseStream.Length)
{
size = (uint)(m_input.BaseStream.Length - tell);
name += "_incomplete";
}
var entry = new Entry {
Name = name + ".tm2",
Size = size,
Offset = tell,
Type = "image"
};
count++;
entries.Add(entry);
m_input.BaseStream.Position = tell + size;
}
return entries;
}
}
internal class KlzArchive : ArcFile
{
public readonly Stream Source;
public KlzArchive(ArcView arc, ArchiveFormat impl, ICollection<Entry> dir, Stream input)
: base (arc, impl, dir)
{
Source = input;
}
#region IDisposable Members
bool _spc_disposed = false;
protected override void Dispose(bool disposing)
{
if (_spc_disposed)
return;
if (disposing)
{
Source.Dispose();
}
_spc_disposed = true;
base.Dispose(disposing);
}
#endregion
}
}

View File

@@ -16,6 +16,7 @@ namespace GameRes.Formats.Kid
public KlzFormat()
{
Extensions = new string[] { "klz" };
Settings = null;
}
public override ImageMetaData ReadMetaData(IBinaryStream stream)