From af46fefc3960b38b8928b8470bf0b61988bd18b4 Mon Sep 17 00:00:00 2001 From: morkt Date: Sun, 26 Nov 2017 00:35:07 +0400 Subject: [PATCH] (Seraphim): archives implementations split into separate classes. --- ArcFormats/ArcFormats.csproj | 2 + ArcFormats/Seraphim/ArcSCN.cs | 128 +++++++++++++++++++++++++++++++ ArcFormats/Seraphim/ArcSeraph.cs | 75 +++++------------- ArcFormats/Seraphim/ArcVoice.cs | 91 ++++++++++++++++++++++ 4 files changed, 241 insertions(+), 55 deletions(-) create mode 100644 ArcFormats/Seraphim/ArcSCN.cs create mode 100644 ArcFormats/Seraphim/ArcVoice.cs diff --git a/ArcFormats/ArcFormats.csproj b/ArcFormats/ArcFormats.csproj index befc34bc..eef3aa9a 100644 --- a/ArcFormats/ArcFormats.csproj +++ b/ArcFormats/ArcFormats.csproj @@ -144,6 +144,8 @@ + + diff --git a/ArcFormats/Seraphim/ArcSCN.cs b/ArcFormats/Seraphim/ArcSCN.cs new file mode 100644 index 00000000..08863793 --- /dev/null +++ b/ArcFormats/Seraphim/ArcSCN.cs @@ -0,0 +1,128 @@ +//! \file ArcSCN.cs +//! \date 2017 Nov 25 +//! \brief Seraphim engine scripts archive. +// +// Copyright (C) 2017 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; + +namespace GameRes.Formats.Seraphim +{ + [Export(typeof(ArchiveFormat))] + public class ScnOpener : ArchiveFormat + { + public override string Tag { get { return "SERAPH/SCN"; } } + public override string Description { get { return "Seraphim engine scripts archive"; } } + public override uint Signature { get { return 0; } } + public override bool IsHierarchic { get { return false; } } + public override bool CanWrite { get { return false; } } + + public ScnOpener () + { + Extensions = new string[] { "dat" }; + } + + public override ArcFile TryOpen (ArcView file) + { + string name = Path.GetFileName (file.Name); + if (!name.Equals ("SCNPAC.DAT", StringComparison.InvariantCultureIgnoreCase)) + return null; + int count = file.View.ReadInt32 (0); + if (!IsSaneCount (count)) + return null; + uint index_size = 4 * (uint)count; + if (index_size > file.View.Reserve (4, index_size)) + return null; + + int index_offset = 4; + uint next_offset = file.View.ReadUInt32 (index_offset); + if (next_offset < index_offset + index_size) + return null; + var dir = new List (count); + for (int i = 0; i < count; ++i) + { + index_offset += 4; + var entry = new Entry { Name = i.ToString ("D5"), Type = "script" }; + entry.Offset = next_offset; + next_offset = file.View.ReadUInt32 (index_offset); + if (next_offset < entry.Offset || next_offset > file.MaxOffset) + return null; + entry.Size = next_offset - (uint)entry.Offset; + dir.Add (entry); + } + return new ArcFile (file, this, dir); + } + + public override Stream OpenEntry (ArcFile arc, Entry entry) + { + if (0 == entry.Size) + return Stream.Null; + var input = arc.File.CreateStream (entry.Offset, entry.Size); + if (0 == input.Signature || 0 != (input.Signature & 0xFF000000)) + return input; + try + { + var data = LzDecompress (input); + input.Dispose(); + return new BinMemoryStream (data, entry.Name); + } + catch + { + input.Position = 0; + return input; + } + } + + internal byte[] LzDecompress (IBinaryStream input) + { + int unpacked_size = input.ReadInt32(); + var data = new byte[unpacked_size]; + int dst = 0; + while (dst < unpacked_size) + { + int ctl = input.ReadByte(); + if (-1 == ctl) + throw new EndOfStreamException(); + if (0 != (ctl & 0x80)) + { + byte lo = input.ReadUInt8(); + int offset = ((ctl << 3 | lo >> 5) & 0x3FF) + 1; + int count = (lo & 0x1F) + 1; + Binary.CopyOverlapped (data, dst-offset, dst, count); + dst += count; + } + else + { + int count = ctl + 1; + if (input.Read (data, dst, count) != count) + throw new EndOfStreamException(); + dst += count; + } + } + return data; + } + } +} diff --git a/ArcFormats/Seraphim/ArcSeraph.cs b/ArcFormats/Seraphim/ArcSeraph.cs index 50865115..c064fea1 100644 --- a/ArcFormats/Seraphim/ArcSeraph.cs +++ b/ArcFormats/Seraphim/ArcSeraph.cs @@ -32,40 +32,35 @@ using GameRes.Compression; namespace GameRes.Formats.Seraphim { - internal class ArchPacEntry : Entry - { - } - + /// + /// this archive format has different index offsets hardcoded into game executable. + /// [Export(typeof(ArchiveFormat))] - public class DatOpener : ArchiveFormat + public class ArchPacOpener : ArchiveFormat { - public override string Tag { get { return "DAT"; } } + public override string Tag { get { return "SERAPH/ARCH"; } } public override string Description { get { return "Seraphim engine resource archive"; } } public override uint Signature { get { return 0; } } public override bool IsHierarchic { get { return false; } } - public override bool CanCreate { get { return false; } } + public override bool CanWrite { get { return false; } } public bool IsAmbiguous { get { return true; } } - static readonly Regex VoiceRe = new Regex (@"^Voice\d\.dat$", RegexOptions.IgnoreCase); + public ArchPacOpener () + { + Extensions = new string[] { "dat" }; + } + +// const long ArchPacOffset = 0x0C23659F; + const long ArchPacOffset = 0x0A0B0AEA; public override ArcFile TryOpen (ArcView file) { if (file.MaxOffset > uint.MaxValue) return null; string name = Path.GetFileName (file.Name); - if (name.Equals ("ArchPac.dat", StringComparison.InvariantCultureIgnoreCase)) - return OpenArchPac (file); - else if (VoiceRe.Match (name).Success) - return OpenVoice (file); -// else if (name.Equals ("ArchCash.dat", StringComparison.InvariantCultureIgnoreCase)) -// return OpenArchCash (file); - return null; - } + if (!name.Equals ("ArchPac.dat", StringComparison.InvariantCultureIgnoreCase)) + return null; - const long ArchPacOffset = 0x0C23659F; - - private ArcFile OpenArchPac (ArcView file) - { long index_offset = ArchPacOffset; int base_count = file.View.ReadInt32 (index_offset); int file_count = file.View.ReadInt32 (index_offset + 4); @@ -95,7 +90,7 @@ namespace GameRes.Formats.Seraphim index_offset += 4; for (int i = 0; i < base_offsets[j].Item2; ++i) { - var entry = new ArchPacEntry { Name = string.Format ("{0}-{1:D5}.cts", j, i), Type = "image" }; + var entry = new PackedEntry { Name = string.Format ("{0}-{1:D5}.cts", j, i), Type = "image" }; entry.Offset = next_offset; next_offset = file.View.ReadUInt32 (index_offset); index_offset += 4; @@ -105,42 +100,12 @@ namespace GameRes.Formats.Seraphim entry.Offset += base_offsets[j].Item1; if (!entry.CheckPlacement (file.MaxOffset)) return null; - dir.Add (entry); + if (entry.Size > 0) + dir.Add (entry); } } - return new ArcFile (file, this, dir); - } - - private ArcFile OpenVoice (ArcView file) - { - int count = file.View.ReadInt16 (0); - if (!IsSaneCount (count)) + if (0 == dir.Count) return null; - uint data_offset = 2 + 4 * (uint)count; - if (data_offset > file.View.Reserve (0, data_offset)) - return null; - - int index_offset = 2; - uint next_offset = file.View.ReadUInt32 (index_offset); - if (next_offset < data_offset) - return null; - var dir = new List (count); - for (int i = 0; i < count; ++i) - { - index_offset += 4; - var entry = new Entry { Name = string.Format ("{0:D5}.wav", i), Type = "audio" }; - entry.Offset = next_offset; - if (i + 1 == count) - next_offset = (uint)file.MaxOffset; - else - next_offset = file.View.ReadUInt32 (index_offset); - if (next_offset <= entry.Offset) - return null; - entry.Size = next_offset - (uint)entry.Offset; - if (!entry.CheckPlacement (file.MaxOffset)) - return null; - dir.Add (entry); - } return new ArcFile (file, this, dir); } @@ -149,7 +114,7 @@ namespace GameRes.Formats.Seraphim if (0 == entry.Size) return Stream.Null; var input = arc.File.CreateStream (entry.Offset, entry.Size); - if (!(entry is ArchPacEntry)) + if (!(entry is PackedEntry)) return input; int signature = arc.File.View.ReadUInt16 (entry.Offset); if (0x9C78 != signature) diff --git a/ArcFormats/Seraphim/ArcVoice.cs b/ArcFormats/Seraphim/ArcVoice.cs new file mode 100644 index 00000000..9d667020 --- /dev/null +++ b/ArcFormats/Seraphim/ArcVoice.cs @@ -0,0 +1,91 @@ +//! \file ArcVoice.cs +//! \date 2017 Nov 25 +//! \brief Seraphim engine audio archive. +// +// Copyright (C) 2015-2017 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 System.Text.RegularExpressions; +using GameRes.Compression; + +namespace GameRes.Formats.Seraphim +{ + [Export(typeof(ArchiveFormat))] + public class VoiceDatOpener : ArchiveFormat + { + public override string Tag { get { return "SERAPH/VOICE"; } } + public override string Description { get { return "Seraphim engine resource archive"; } } + public override uint Signature { get { return 0; } } + public override bool IsHierarchic { get { return false; } } + public override bool CanWrite { get { return false; } } + public bool IsAmbiguous { get { return true; } } + + public VoiceDatOpener () + { + Extensions = new string[] { "dat" }; + } + + static readonly Regex VoiceRe = new Regex (@"^Voice\d\.dat$", RegexOptions.IgnoreCase); + + public override ArcFile TryOpen (ArcView file) + { + if (file.MaxOffset > uint.MaxValue) + return null; + string name = Path.GetFileName (file.Name); + if (!VoiceRe.Match (name).Success) + return null; + + int count = file.View.ReadInt16 (0); + if (!IsSaneCount (count)) + return null; + uint data_offset = 2 + 4 * (uint)count; + if (data_offset > file.View.Reserve (0, data_offset)) + return null; + + int index_offset = 2; + uint next_offset = file.View.ReadUInt32 (index_offset); + if (next_offset < data_offset) + return null; + var dir = new List (count); + for (int i = 0; i < count; ++i) + { + index_offset += 4; + var entry = new Entry { Name = string.Format ("{0:D5}.wav", i), Type = "audio" }; + entry.Offset = next_offset; + if (i + 1 == count) + next_offset = (uint)file.MaxOffset; + else + next_offset = file.View.ReadUInt32 (index_offset); + if (next_offset <= entry.Offset) + return null; + entry.Size = next_offset - (uint)entry.Offset; + if (!entry.CheckPlacement (file.MaxOffset)) + return null; + dir.Add (entry); + } + return new ArcFile (file, this, dir); + } + } +}