From 045d4c0a24c1f27c9d8b15a6e8fb415394e171a4 Mon Sep 17 00:00:00 2001 From: morkt Date: Mon, 22 Jan 2018 15:52:49 +0400 Subject: [PATCH] implemented PalmTree 'AR' archives. --- ArcFormats/ArcFormats.csproj | 1 + ArcFormats/PalmTree/ArcAR.cs | 159 +++++++++++++++++++++++++++++++++++ 2 files changed, 160 insertions(+) create mode 100644 ArcFormats/PalmTree/ArcAR.cs diff --git a/ArcFormats/ArcFormats.csproj b/ArcFormats/ArcFormats.csproj index 1097d01b..f8479635 100644 --- a/ArcFormats/ArcFormats.csproj +++ b/ArcFormats/ArcFormats.csproj @@ -178,6 +178,7 @@ + diff --git a/ArcFormats/PalmTree/ArcAR.cs b/ArcFormats/PalmTree/ArcAR.cs new file mode 100644 index 00000000..b98750b6 --- /dev/null +++ b/ArcFormats/PalmTree/ArcAR.cs @@ -0,0 +1,159 @@ +//! \file ArcAR.cs +//! \date 2018 Jan 22 +//! \brief PalmTree script engine 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.Formats.PkWare; + +namespace GameRes.Formats.PalmTree +{ + /// + /// Ordinary PkWare ZIP archive with 'PK' signature changed to 'AR' + /// + [Export(typeof(ArchiveFormat))] + public class ArcOpener : ZipOpener + { + public override string Tag { get { return "ARC/AR"; } } + public override string Description { get { return "PalmTree script engine resource archive"; } } + public override uint Signature { get { return 0; } } + public override bool IsHierarchic { get { return true; } } + public override bool CanWrite { get { return false; } } + + static readonly byte[] ArDirSignature = { (byte)'A', (byte)'R', 5, 6 }; + + public override ArcFile TryOpen (ArcView file) + { + if (-1 == SearchForSignature (file, ArDirSignature)) + return null; + var input = new ArPkStream (file.CreateStream()); + try + { + return OpenZipArchive (file, input); + } + catch + { + input.Dispose(); + throw; + } + } + + public override ResourceOptions GetDefaultOptions () + { + return null; + } + } + + /// + /// Stream that changes all 'AR' signatures to 'PK' on the fly. + /// + /// + /// this is almost like reading ZIP file directory, maybe just write custom ZIP archive reader instead? + /// + internal class ArPkStream : InputProxyStream + { + List m_ar_blocks; + long m_last_scan_pos; + bool m_scan_failed; + + public ArPkStream (Stream input) : base (input) + { + m_ar_blocks = new List(); + m_last_scan_pos = 0; + m_scan_failed = false; + } + + public override int Read (byte[] buffer, int offset, int count) + { + long pos = this.Position; + if (pos + count > m_last_scan_pos && !m_scan_failed) + { + BuildDirectory (pos + count); + this.Position = pos; + } + count = BaseStream.Read (buffer, offset, count); + long buf_pos = pos; + long buf_end = buf_pos + count; + foreach (long ar_pos in m_ar_blocks) + { + if (buf_end <= ar_pos) + break; + if (buf_pos >= ar_pos+2) + continue; + int signature_pos = (int)(ar_pos - pos); + if (signature_pos >= 0) + buffer[offset+signature_pos ] = (byte)'P'; + if (signature_pos + 1 >= 0) + buffer[offset+signature_pos+1] = (byte)'K'; + buf_pos = ar_pos + 2; + } + return count; + } + + byte[] pk_buffer = new byte[0x22]; + + void BuildDirectory (long last_pos) + { + long pos = m_last_scan_pos; + while (pos < last_pos) + { + this.Position = pos; + int read = BaseStream.Read (pk_buffer, 0, 0x22); + if (read < 4 || !pk_buffer.AsciiEqual ("AR")) + { + m_scan_failed = true; + break; + } + m_ar_blocks.Add (pos); + uint block_type = pk_buffer.ToUInt16 (2); + if (0x0201 == block_type && read >= 0x22) + { + uint name_length = pk_buffer.ToUInt16 (0x1C); + uint extra_length = pk_buffer.ToUInt16 (0x1E); + uint cmt_length = pk_buffer.ToUInt16 (0x20); + pos += 0x2EL + name_length + extra_length + cmt_length; + } + else if (0x0403 == block_type && read >= 0x1E) + { + uint packed_size = pk_buffer.ToUInt32 (0x12); + uint name_length = pk_buffer.ToUInt16 (0x1A); + uint extra_length = pk_buffer.ToUInt16 (0x1C); + pos += 0x1EL + name_length + extra_length + packed_size; + } + else if (0x0605 == block_type && read >= 0x16) + { + uint cmt_length = pk_buffer.ToUInt16 (0x14); + pos += 0x16L + cmt_length; + } + else + { + pos += 4; + m_scan_failed = true; + } + } + m_last_scan_pos = pos; + } + } +}