From 7ff67b19220c28f67cc86f09ee53acdaeaac3770 Mon Sep 17 00:00:00 2001 From: morkt Date: Wed, 23 Dec 2015 19:48:21 +0400 Subject: [PATCH] implemented Tmr-Hiro PAC archives. --- ArcFormats/ArcFormats.csproj | 3 + ArcFormats/Tmr-Hiro/ArcPAC.cs | 142 +++++++++++++++++++++++ ArcFormats/Tmr-Hiro/AudioTmr.cs | 116 +++++++++++++++++++ ArcFormats/Tmr-Hiro/ImageGRD.cs | 193 ++++++++++++++++++++++++++++++++ supported.html | 5 + 5 files changed, 459 insertions(+) create mode 100644 ArcFormats/Tmr-Hiro/ArcPAC.cs create mode 100644 ArcFormats/Tmr-Hiro/AudioTmr.cs create mode 100644 ArcFormats/Tmr-Hiro/ImageGRD.cs diff --git a/ArcFormats/ArcFormats.csproj b/ArcFormats/ArcFormats.csproj index 42d64a11..a4ad776e 100644 --- a/ArcFormats/ArcFormats.csproj +++ b/ArcFormats/ArcFormats.csproj @@ -207,6 +207,9 @@ + + + diff --git a/ArcFormats/Tmr-Hiro/ArcPAC.cs b/ArcFormats/Tmr-Hiro/ArcPAC.cs new file mode 100644 index 00000000..4c44a3e3 --- /dev/null +++ b/ArcFormats/Tmr-Hiro/ArcPAC.cs @@ -0,0 +1,142 @@ +//! \file ArcPAC.cs +//! \date Wed Dec 23 15:37:30 2015 +//! \brief Tmr-Hiro ADV System archives. +// +// Copyright (C) 2015 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 GameRes.Utility; +using System.Collections.Generic; +using System.ComponentModel.Composition; +using System.IO; + +namespace GameRes.Formats.TmrHiro +{ + [Export(typeof(ArchiveFormat))] + public class PacOpener : ArchiveFormat + { + public override string Tag { get { return "PAC/TMR-HIRO"; } } + public override string Description { get { return "Tmr-Hiro ADV System resource archive"; } } + public override uint Signature { get { return 0; } } + public override bool IsHierarchic { get { return false; } } + public override bool CanCreate { get { return false; } } + + public PacOpener () + { + Extensions = new string[] { "pac" }; + } + + public override ArcFile TryOpen (ArcView file) + { + int count = file.View.ReadInt16 (0); + if (!IsSaneCount (count)) + return null; + uint name_length = file.View.ReadByte (2); + if (0 == name_length) + return null; + uint data_offset = file.View.ReadUInt32 (3); + if (data_offset >= file.MaxOffset) + return null; + int version; + uint index_size = 7 + (name_length + 8) * (uint)count; + if (data_offset == index_size) + version = 1; + else if (data_offset == index_size + 4 * (uint)count) + version = 2; + else + return null; + + var dir = new List (count); + uint index_offset = 7; + for (int i = 0; i < count; ++i) + { + var name = file.View.ReadString (index_offset, name_length); + index_offset += name_length; + var entry = new Entry { Name = name }; + if (1 == version) + { + entry.Offset = file.View.ReadUInt32 (index_offset) + data_offset; + entry.Size = file.View.ReadUInt32 (index_offset+4); + index_offset += 8; + } + else + { + entry.Offset = file.View.ReadInt64 (index_offset) + data_offset; + entry.Size = file.View.ReadUInt32 (index_offset+8); + index_offset += 12; + } + if (!entry.CheckPlacement (file.MaxOffset)) + return null; + dir.Add (entry); + } + var arc_name = Path.GetFileNameWithoutExtension (file.Name).ToLower(); + foreach (var entry in dir) + { + var signature = file.View.ReadUInt32 (entry.Offset); + if (0x5367674F == signature) // 'OggS' + { + entry.Name = Path.ChangeExtension (entry.Name, "ogg"); + entry.Type = "audio"; + } + else if ((((signature & 0xFF) == 1 || (signature & 0xFF) == 2)) && "grd" == arc_name) + { + entry.Name = Path.ChangeExtension (entry.Name, "grd"); + entry.Type = "image"; + } + else if (0x44 == signature && entry.Size-9 == file.View.ReadUInt32 (entry.Offset+5)) + { + entry.Type = "audio"; + } + else if (6 == file.View.ReadInt16 (entry.Offset+4) && 0x140050 == file.View.ReadUInt32 (entry.Offset+6)) + { + entry.Type = "script"; + if ("srp" == arc_name) + entry.Name = Path.ChangeExtension (entry.Name, "srp"); + } + } + return new ArcFile (file, this, dir); + } + + public override Stream OpenEntry (ArcFile arc, Entry entry) + { + if ("script" != entry.Type + || 6 != arc.File.View.ReadInt16 (entry.Offset+4) + || 0x140050 != arc.File.View.ReadUInt32 (entry.Offset+6)) + return base.OpenEntry (arc, entry); + int record_count = arc.File.View.ReadInt32 (entry.Offset); + var data = arc.File.View.ReadBytes (entry.Offset, entry.Size); + int pos = 4; + for (int i = 0; i < record_count; ++i) + { + int chunk_size = LittleEndian.ToUInt16 (data, pos) - 4; + pos += 6; + if (pos + chunk_size > data.Length) + return base.OpenEntry (arc, entry); + for (int j = 0; j < chunk_size; ++j) + { + data[pos] = Binary.RotByteR (data[pos], 4); + ++pos; + } + } + return new MemoryStream (data); + } + } +} diff --git a/ArcFormats/Tmr-Hiro/AudioTmr.cs b/ArcFormats/Tmr-Hiro/AudioTmr.cs new file mode 100644 index 00000000..5a79610e --- /dev/null +++ b/ArcFormats/Tmr-Hiro/AudioTmr.cs @@ -0,0 +1,116 @@ +//! \file AudioTmr.cs +//! \date Wed Dec 23 16:23:48 2015 +//! \brief Tmr-Hiro WAV audio. +// +// Copyright (C) 2015 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.ComponentModel.Composition; +using System.IO; +using System.Text; +using GameRes.Compression; +using GameRes.Utility; + +namespace GameRes.Formats.TmrHiro +{ + [Export(typeof(AudioFormat))] + public class TmrHiroAudio : AudioFormat + { + public override string Tag { get { return "WAV/TMR-HIRO"; } } + public override string Description { get { return "Tmr-Hiro wave audio"; } } + public override uint Signature { get { return 0x44; } } + + public TmrHiroAudio () + { + Extensions = new string[] { "" }; + } + + public override SoundInput TryOpen (Stream file) + { + file.Position = 4; + if (file.ReadByte() != 0) + return null; + int length = ReadInt32 (file); + if (length != file.Length - 9) + return null; + return new RawInput (file, length); + } + + private static int ReadInt32 (Stream file) + { + int dword = file.ReadByte(); + dword |= file.ReadByte() << 8; + dword |= file.ReadByte() << 16; + dword |= file.ReadByte() << 24; + return dword; + } + } + + public class RawInput : SoundInput + { + public override string SourceFormat { get { return "raw"; } } + + public override int SourceBitrate + { + get { return (int)Format.AverageBytesPerSecond * 8; } + } + + public RawInput (Stream file, int data_length) : base (new StreamRegion (file, 9, data_length)) + { + this.Format = new WaveFormat + { + FormatTag = 1, + Channels = 2, + SamplesPerSecond = 44100, + BitsPerSample = 16, + BlockAlign = 4, + AverageBytesPerSecond = 44100*4, + }; + this.PcmSize = data_length; + } + + #region IO.Stream methods + public override long Position + { + get { return Source.Position; } + set { Source.Position = value; } + } + + public override bool CanSeek { get { return Source.CanSeek; } } + + public override long Seek (long offset, SeekOrigin origin) + { + return Source.Seek (offset, origin); + } + + public override int Read (byte[] buffer, int offset, int count) + { + return Source.Read (buffer, offset, count); + } + + public override int ReadByte () + { + return Source.ReadByte(); + } + #endregion + } +} diff --git a/ArcFormats/Tmr-Hiro/ImageGRD.cs b/ArcFormats/Tmr-Hiro/ImageGRD.cs new file mode 100644 index 00000000..f21e98f2 --- /dev/null +++ b/ArcFormats/Tmr-Hiro/ImageGRD.cs @@ -0,0 +1,193 @@ +//! \file ImageGRD.cs +//! \date Wed Dec 23 17:00:30 2015 +//! \brief Tmr-Hiro ADV System image format. +// +// Copyright (C) 2015 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 GameRes.Utility; +using System.ComponentModel.Composition; +using System.IO; +using System.Windows.Media; + +namespace GameRes.Formats.TmrHiro +{ + internal class GrdMetaData : ImageMetaData + { + public int Format; + public int AlphaSize; + public int RSize; + public int GSize; + public int BSize; + } + + [Export(typeof(ImageFormat))] + public class GrdFormat : ImageFormat + { + public override string Tag { get { return "GRD/TMR-HIRO"; } } + public override string Description { get { return "Tmr-Hiro ADV System image format"; } } + public override uint Signature { get { return 0; } } + + public GrdFormat () + { + Extensions = new string[] { "grd", "" }; + } + + public override ImageMetaData ReadMetaData (Stream stream) + { + var header = new byte[0x20]; + if (header.Length != stream.Read (header, 0, header.Length)) + return null; + if (header[0] != 1 && header[0] != 2) + return null; + if (header[1] != 1 && header[1] != 0xA1 && header[1] != 0xA2) + return null; + int bpp = LittleEndian.ToUInt16 (header, 6); + if (bpp != 24 && bpp != 32) + return null; + int left = LittleEndian.ToUInt16 (header, 8); + int right = LittleEndian.ToUInt16 (header, 0xA); + int top = LittleEndian.ToUInt16 (header, 0xC); + int bottom = LittleEndian.ToUInt16 (header, 0xE); + var info = new GrdMetaData { + Format = LittleEndian.ToUInt16 (header, 0), + Width = (uint)(right-left), + Height = (uint)(bottom-top), + BPP = bpp, + OffsetX = left, + OffsetY = top, + AlphaSize = LittleEndian.ToInt32 (header, 0x10), + RSize = LittleEndian.ToInt32 (header, 0x14), + GSize = LittleEndian.ToInt32 (header, 0x18), + BSize = LittleEndian.ToInt32 (header, 0x1C), + }; + if (0x20 + info.AlphaSize + info.RSize + info.BSize + info.GSize != stream.Length) + return null; + return info; + } + + public override ImageData Read (Stream stream, ImageMetaData info) + { + var meta = (GrdMetaData)info; + var reader = new GrdReader (stream, meta); + reader.Unpack(); + return ImageData.Create (info, reader.Format, null, reader.Data); + } + + public override void Write (Stream file, ImageData image) + { + throw new System.NotImplementedException ("GrdFormat.Write not implemented"); + } + } + + internal sealed class GrdReader + { + Stream m_input; + GrdMetaData m_info; + byte[] m_output; + int m_pack_type; + int m_pixel_size; + byte[] m_channel; + + public PixelFormat Format { get; private set; } + public byte[] Data { get { return m_output; } } + + public GrdReader (Stream input, GrdMetaData info) + { + m_input = input; + m_info = info; + if (32 == m_info.BPP) + Format = PixelFormats.Bgra32; + else + Format = PixelFormats.Bgr24; + int channel_size = (int)(m_info.Width * m_info.Height); + m_pack_type = m_info.Format >> 8; + m_pixel_size = m_info.BPP / 8; + m_output = new byte[m_pixel_size * channel_size]; + m_channel = new byte[channel_size]; + } + + public void Unpack () + { + int next_pos = 0x20; + if (32 == m_info.BPP) + { + UnpackChannel (3, next_pos, m_info.AlphaSize); + next_pos += m_info.AlphaSize; + } + UnpackChannel (2, next_pos, m_info.RSize); + next_pos += m_info.RSize; + UnpackChannel (1, next_pos, m_info.GSize); + next_pos += m_info.GSize; + UnpackChannel (0, next_pos, m_info.BSize); + } + + void UnpackChannel (int dst, int src_pos, int src_size) + { + m_input.Position = src_pos; + + if (1 == m_pack_type) + { + UnpackRLE (src_size); + } + else + { + throw new System.NotImplementedException ("Huffman-compressed images not implemented"); + } + for (int y = (int)m_info.Height-1; y >= 0; --y) + { + int src = y * (int)m_info.Width; + for (uint x = 0; x < m_info.Width; ++x) + { + m_output[dst] = m_channel[src++]; + dst += m_pixel_size; + } + } + } + + void UnpackRLE (int src_size) + { + int src = 0; + int dst = 0; + while (src < src_size) + { + int count = m_input.ReadByte(); + if (-1 == count) + return; + ++src; + if (count > 0x7F) + { + count &= 0x7F; + byte v = (byte)m_input.ReadByte(); + ++src; + for (int i = 0; i < count; ++i) + m_channel[dst++] = v; + } + else + { + m_input.Read (m_channel, dst, count); + src += count; + dst += count; + } + } + } + } +} diff --git a/supported.html b/supported.html index d8899fd9..8e6f6126 100644 --- a/supported.html +++ b/supported.html @@ -206,6 +206,7 @@ Saiminjutsu
Chikatetsu Fuusa Jiken
Eien no Owari ni
Ikusa Otome Valkyrie
+Onsoku Hishou Sonic Mercedes
Sakura Machizaka Stories vol.1
Sakura Machizaka Stories vol.2
@@ -225,6 +226,7 @@ Nikutai Ten'i
*.igfZEUSNo *.dat
*.arc-
M2TYPENoFFA System/G-SYS Dokusen Kango
+Genecracer Saki
Hensai Keikaku
Inen no Yakata
Inran OL Sawatari Tokiko
@@ -573,6 +575,9 @@ AstralAir no Shiroki Towa
Otome Renshin Prister
Wondering Repair!
+*.pac-NoTmr-Hiro ADV System +Hitozuma Sakunyuu Hyakkaten
+

1 Non-encrypted only