From b139f52a37a674a78d3a5e07f4d9a37ab6e44479 Mon Sep 17 00:00:00 2001 From: morkt Date: Tue, 19 Jun 2018 14:15:04 +0400 Subject: [PATCH] (Legacy): AQA archives, CTF images and MSF audio. --- Legacy/Unknown/ArcAQA.cs | 75 ++++++++++++++++ Legacy/Unknown/AudioMSF.cs | 61 +++++++++++++ Legacy/Unknown/ImageCTF.cs | 175 +++++++++++++++++++++++++++++++++++++ 3 files changed, 311 insertions(+) create mode 100644 Legacy/Unknown/ArcAQA.cs create mode 100644 Legacy/Unknown/AudioMSF.cs create mode 100644 Legacy/Unknown/ImageCTF.cs diff --git a/Legacy/Unknown/ArcAQA.cs b/Legacy/Unknown/ArcAQA.cs new file mode 100644 index 00000000..46c61236 --- /dev/null +++ b/Legacy/Unknown/ArcAQA.cs @@ -0,0 +1,75 @@ +//! \file ArcAQA.cs +//! \date 2018 Jun 18 +//! \brief 'Unknown' 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; +using System.Collections.Generic; +using System.ComponentModel.Composition; +using System.IO; +using GameRes.Utility; + +// [040917][Unknown] Abandoner + +namespace GameRes.Formats.Unknown +{ + [Export(typeof(ArchiveFormat))] + public class AqaOpener : ArchiveFormat + { + public override string Tag { get { return "AQA"; } } + public override string Description { get { return "'Unknown' resource archive"; } } + public override uint Signature { get { return 0x20415141; } } // 'AQA ' + public override bool IsHierarchic { get { return true; } } + public override bool CanWrite { get { return false; } } + + public override ArcFile TryOpen (ArcView file) + { + int count = file.View.ReadInt32 (12); + if (!IsSaneCount (count)) + return null; + ushort key = (ushort)(((101 * file.View.ReadUInt32 (8) + 777) & 0xFFFF) + 1); + uint index_size = 0x90 * (uint)count; + var index = file.View.ReadBytes (0x18, 0x90 * (uint)count); + for (int i = 0; i < index.Length; i += 2) + { + index[i ] ^= (byte)key; + index[i+1] ^= (byte)(key >> 8); + } + uint data_offset = index_size + 0x18; + int offset = 0; + var dir = new List (count); + for (int i = 0; i < count; ++i) + { + var name = Binary.GetCString (index, offset, 0x80); + var entry = FormatCatalog.Instance.Create (name); + entry.Offset = index.ToUInt32 (offset+0x88) + data_offset; + entry.Size = index.ToUInt32 (offset+0x80); + if (!entry.CheckPlacement (file.MaxOffset)) + return null; + dir.Add (entry); + offset += 0x90; + } + return new ArcFile (file, this, dir); + } + } +} diff --git a/Legacy/Unknown/AudioMSF.cs b/Legacy/Unknown/AudioMSF.cs new file mode 100644 index 00000000..c5d31713 --- /dev/null +++ b/Legacy/Unknown/AudioMSF.cs @@ -0,0 +1,61 @@ +//! \file AudioMSF.cs +//! \date 2018 Jun 19 +//! \brief 'Unknown' PCM audio format. +// +// 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.ComponentModel.Composition; +using System.IO; + +namespace GameRes.Formats.Unknown +{ + [Export(typeof(AudioFormat))] + public class MsfAudio : AudioFormat + { + public override string Tag { get { return "MSF"; } } + public override string Description { get { return "'Unknown' PCM audio format"; } } + public override uint Signature { get { return 0x2046534D; } } // 'MSF ' + public override bool CanWrite { get { return false; } } + + public override SoundInput TryOpen (IBinaryStream file) + { + var header = file.ReadBytes (0x30); + ushort key = (ushort)(101 * header.ToUInt16 (4) + 778); + for (int i = 0; i < header.Length; i += 2) + { + header[i ] ^= (byte)key; + header[i+1] ^= (byte)(key >> 8); + } + var format = new WaveFormat { + FormatTag = header.ToUInt16 (0x1C), + Channels = header.ToUInt16 (0x1E), + SamplesPerSecond = header.ToUInt32 (0x20), + AverageBytesPerSecond = header.ToUInt32 (0x24), + BlockAlign = header.ToUInt16 (0x28), + BitsPerSample = header.ToUInt16 (0x2A), + }; + uint pcm_size = header.ToUInt32 (0x18); + var pcm = new StreamRegion (file.AsStream, 0x30, pcm_size); + return new RawPcmInput (pcm, format); + } + } +} diff --git a/Legacy/Unknown/ImageCTF.cs b/Legacy/Unknown/ImageCTF.cs new file mode 100644 index 00000000..2976520c --- /dev/null +++ b/Legacy/Unknown/ImageCTF.cs @@ -0,0 +1,175 @@ +//! \file ImageCTF.cs +//! \date 2018 Jun 18 +//! \brief 'Unknown' image format. +// +// 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.ComponentModel.Composition; +using System.IO; +using System.Windows.Media; +using GameRes.Compression; + +namespace GameRes.Formats.Unknown +{ + internal class CtfMetaData : ImageMetaData + { + public int UnpackedSize; + public int ROffset; + public int GOffset; + public int BOffset; + public int AlphaOffset; + public bool IsCompressed; + } + + [Export(typeof(ImageFormat))] + public class CtfFormat : ImageFormat + { + public override string Tag { get { return "CTF"; } } + public override string Description { get { return "'Unknown' image format"; } } + public override uint Signature { get { return 0x46465443; } } // 'CTFF' + + public override ImageMetaData ReadMetaData (IBinaryStream file) + { + var header = file.ReadHeader (0x28); + int bpp = header[0x20]; + if (bpp != 24) + return null; + int alpha_offset = header.ToInt32 (0x1C); + return new CtfMetaData { + Width = header.ToUInt16 (4), + Height = header.ToUInt16 (6), + BPP = alpha_offset != 0 ? 32 : bpp, + UnpackedSize = header.ToInt32 (0xC), + ROffset = header.ToInt32 (0x10), + GOffset = header.ToInt32 (0x14), + BOffset = header.ToInt32 (0x18), + AlphaOffset = alpha_offset, + IsCompressed = header[0x22] == 0xFF, + }; + } + + public override ImageData Read (IBinaryStream file, ImageMetaData info) + { + var reader = new CtfReader (file, (CtfMetaData)info); + var pixels = reader.Unpack(); + return ImageData.Create (info, reader.Format, null, pixels, reader.Stride); + } + + public override void Write (Stream file, ImageData image) + { + throw new System.NotImplementedException ("CtfFormat.Write not implemented"); + } + } + + internal class CtfReader + { + IBinaryStream m_input; + CtfMetaData m_info; + byte[] m_output; + int m_stride; + + public PixelFormat Format { get; private set; } + public int Stride { get { return m_stride; } } + + public CtfReader (IBinaryStream input, CtfMetaData info) + { + m_input = input; + m_info = info; + m_stride = ((int)info.Width * info.BPP / 8 + 3) & -3; + m_output = new byte[m_stride * (int)info.Height]; + if (24 == m_info.BPP) + Format = PixelFormats.Bgr24; + else + Format = PixelFormats.Bgra32; + } + + public byte[] Unpack () + { + m_input.Position = 0x48; + var channels = new byte[m_info.UnpackedSize]; + using (var lzss = new LzssStream (m_input.AsStream, LzssMode.Decompress, true)) + using (var input = new BinaryStream (lzss, m_input.Name)) + UnpackRle (input, channels); + + int dst = 0; + int pixel_size = m_info.BPP / 8; + int rsrc = m_info.ROffset; + int gsrc = m_info.GOffset; + int bsrc = m_info.BOffset; + int asrc = m_info.AlphaOffset; + bool has_alpha = asrc != 0; + int width = (int)m_info.Width; + int height = (int)m_info.Height; + int gap = m_stride - (width * pixel_size); + for (uint y = 0; y < height; ++y) + { + for (int x = 0; x < width; ++x) + { + m_output[dst ] = channels[bsrc++]; + m_output[dst+1] = channels[gsrc++]; + m_output[dst+2] = channels[rsrc++]; + if (has_alpha) + m_output[dst+3] = channels[asrc++]; + dst += pixel_size; + } + dst += gap; + } + return m_output; + } + + void UnpackRle (IBinaryStream input, byte[] output) + { + var header = input.ReadBytes (0x18); + byte count_limit = header[5]; + int dst = 0; + while (dst < output.Length) + { + int v = input.ReadByte(); + if (-1 == v) + break; + int count = 1; + while (count < count_limit) + { + if (input.PeekByte() != v) + break; + ++count; + input.ReadByte(); + } + if (count == count_limit) + { + byte ctl = input.ReadUInt8(); + if (ctl > 0x7F) + { + byte lo = input.ReadUInt8(); + count += lo + ((ctl & 0x7F) << 8) + 128; + } + else + { + count += ctl; + } + } + for (int i = 0; i < count; ++i) + output[dst++] = (byte)v; + } + } + } +}