From 95ffcf075a3e40ea5c4210da0529ee1aae5f6fb7 Mon Sep 17 00:00:00 2001 From: ManicSteiner Date: Tue, 7 Jan 2025 16:18:29 +0800 Subject: [PATCH] add support of LZS-TIM2 image --- ArcFormats/ArcFormats.csproj | 2 + ArcFormats/DigitalWorks/ArcPACsingle.cs | 88 +++++++++++++++++++++++++ ArcFormats/DigitalWorks/ImageTM2arc.cs | 51 ++++++++++++++ 3 files changed, 141 insertions(+) create mode 100644 ArcFormats/DigitalWorks/ArcPACsingle.cs create mode 100644 ArcFormats/DigitalWorks/ImageTM2arc.cs diff --git a/ArcFormats/ArcFormats.csproj b/ArcFormats/ArcFormats.csproj index f8e37d86..a6c9279c 100644 --- a/ArcFormats/ArcFormats.csproj +++ b/ArcFormats/ArcFormats.csproj @@ -131,7 +131,9 @@ + + WidgetDXA.xaml diff --git a/ArcFormats/DigitalWorks/ArcPACsingle.cs b/ArcFormats/DigitalWorks/ArcPACsingle.cs new file mode 100644 index 00000000..310823c1 --- /dev/null +++ b/ArcFormats/DigitalWorks/ArcPACsingle.cs @@ -0,0 +1,88 @@ +//! \file ArcPACPS2.cs +//! \date 2018 Sep 18 +//! \brief Digital Works PS2 resource archive. +// +// Copyright (C) 2018 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.DigitalWorks +{ + [Export(typeof(ArchiveFormat))] + public class PacSingleOpener : ArchiveFormat + { + public override string Tag { get { return "PAC/LZS-TIM2"; } } + public override string Description { get { return "LZS-TIM2 Image archive"; } } + public override uint Signature { get { return 0x535A4C; } } // 'LZS' + public override bool IsHierarchic { get { return false; } } + public override bool CanWrite { get { return false; } } + + /** + Target games: + Cafe Little Wish SLPM-65294 + F Fanatic SLPM-65296 + */ + + public override ArcFile TryOpen (ArcView file) + { + if (!file.View.AsciiEqual(9, "TIM2")) + return null; + var dir = new List (1); + var entry = FormatCatalog.Instance.Create (file.Name); + entry.Offset = 0L; + entry.Size = (uint)file.MaxOffset; + if (!entry.CheckPlacement (file.MaxOffset)) + return null; + dir.Add (entry); + + return new ArcFile (file, this, dir); + } + + public override Stream OpenEntry (ArcFile arc, Entry entry) + { + var pent = entry as PackedEntry; + if (null == pent) + return base.OpenEntry (arc, entry); + if (!pent.IsPacked) + { + if (!arc.File.View.AsciiEqual (entry.Offset, "LZS\0")) + return base.OpenEntry (arc, entry); + pent.IsPacked = true; + pent.UnpackedSize = arc.File.View.ReadUInt32 (entry.Offset+4); + } + var input = arc.File.CreateStream (entry.Offset+8, entry.Size-8); + bool embedded_lzs = (input.Signature & ~0xF0u) == 0x535A4C0F; // 'LZS' + var lzs = new LzssStream (input); + if (embedded_lzs) + { + var header = new byte[8]; + lzs.Read (header, 0, 8); + pent.UnpackedSize = header.ToUInt32 (4); + lzs = new LzssStream (lzs); + } + return lzs; + } + } +} diff --git a/ArcFormats/DigitalWorks/ImageTM2arc.cs b/ArcFormats/DigitalWorks/ImageTM2arc.cs new file mode 100644 index 00000000..9b57c6a0 --- /dev/null +++ b/ArcFormats/DigitalWorks/ImageTM2arc.cs @@ -0,0 +1,51 @@ +using GameRes.Compression; +using System; +using System.Collections.Generic; +using System.ComponentModel.Composition; +using System.IO; + +namespace GameRes.Formats.DigitalWorks +{ + [Export(typeof(ImageFormat))] + public class TM2ArkFormat : Tim2Format + { + public override string Tag { get { return "TIM2/PS2 compressed"; } } + public override string Description { get { return "PlayStation/2 image format with LZSS compress"; } } + public override uint Signature { get { return 0x535A4C; } } // 'LZS' + public TM2ArkFormat() + { + Extensions = new string[] { "tm2" }; + } + public override ImageMetaData ReadMetaData(IBinaryStream stream) + { + stream.Position = 9; + uint real_sign = stream.ReadUInt32(); + //Tim2Format tm2raw = new Tim2Format(); + if (real_sign != base.Signature) + { + return null; + } + stream.Position = 4; + uint unpacked_size = stream.ReadUInt32(); + if (unpacked_size <= 0x20 || unpacked_size > 0x5000000) // ~83MB + return null; + stream.Position = 8; + using (var lzss = new LzssStream(stream.AsStream, LzssMode.Decompress, true)) + using (var input = new SeekableStream(lzss)) + using (var tm2 = new BinaryStream(input, stream.Name)) + return base.ReadMetaData(tm2); + } + public override ImageData Read(IBinaryStream stream, ImageMetaData info) + { + stream.Position = 8; + using (var lzss = new LzssStream(stream.AsStream, LzssMode.Decompress, true)) + using (var input = new SeekableStream(lzss)) + using (var tm2 = new BinaryStream(input, stream.Name)) + return base.Read(tm2, info); + } + public override void Write(Stream file, ImageData image) + { + throw new System.NotImplementedException("TM2ArkFormat.Write not implemented"); + } + } +}