(Seraphim): archives implementations split into separate classes.

This commit is contained in:
morkt 2017-11-26 00:35:07 +04:00
parent 59259051fe
commit af46fefc39
4 changed files with 241 additions and 55 deletions

View File

@ -144,6 +144,8 @@
<Compile Include="Lilim\ArcFGA.cs" />
<Compile Include="ScenePlayer\ArcPMX.cs" />
<Compile Include="Scoop\ArcGX.cs" />
<Compile Include="Seraphim\ArcSCN.cs" />
<Compile Include="Seraphim\ArcVoice.cs" />
<Compile Include="Silky\ImageGRD.cs" />
<Compile Include="Slg\ArcSPD.cs" />
<Compile Include="Software House Parsley\ArcCG.cs" />

View File

@ -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<Entry> (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;
}
}
}

View File

@ -32,40 +32,35 @@ using GameRes.Compression;
namespace GameRes.Formats.Seraphim
{
internal class ArchPacEntry : Entry
{
}
/// <summary>
/// this archive format has different index offsets hardcoded into game executable.
/// </summary>
[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<Entry> (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)

View File

@ -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<Entry> (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);
}
}
}