Merge pull request #6 from scientificworld/master

Added support for Sas5's GAR archive and code patch format, added a certain game's resource files list.
This commit is contained in:
Pkuism
2025-11-30 18:50:09 +08:00
committed by GitHub
6 changed files with 4381 additions and 1 deletions

View File

@@ -776,6 +776,7 @@
<Compile Include="Qlie\WidgetQLIE.xaml.cs">
<DependentUpon>WidgetQLIE.xaml</DependentUpon>
</Compile>
<Compile Include="Sas5\ArcGAR.cs" />
<Compile Include="Sas5\ArcIAR.cs" />
<Compile Include="Sas5\ArcSec5.cs" />
<Compile Include="Sas5\ArcWAR.cs" />

View File

Binary file not shown.

View File

File diff suppressed because it is too large Load Diff

112
ArcFormats/Sas5/ArcGAR.cs Normal file
View File

@@ -0,0 +1,112 @@
//! \file ArcGAR.cs
//! \date 2025 Nov 24
//! \brief Sas5 engine video archive.
//
// 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.Collections.Generic;
using System.ComponentModel.Composition;
using System.IO;
using GameRes.Utility;
namespace GameRes.Formats.Sas5
{
[Export(typeof(ArchiveFormat))]
public class GarOpener : ArchiveFormat
{
public override string Tag { get { return "GAR/SAS5"; } }
public override string Description { get { return "SAS5 engine video archive"; } }
public override uint Signature { get { return 0x20524147; } } // 'GAR '
public override bool IsHierarchic { get { return false; } }
public override bool CanWrite { get { return false; } }
public GarOpener ()
{
Extensions = new string[] { "gar" };
}
public override ArcFile TryOpen (ArcView file)
{
int version = file.View.ReadInt32 (4);
if (version != 1)
return null;
uint index_offset = file.View.ReadUInt32 (8);
int block_start = file.View.ReadInt32 (0x14);
int count = file.View.ReadInt32 (index_offset) - 1;
if (!IsSaneCount (count))
return null;
var GetEntryName = CreateEntryNameDelegate (file.Name);
index_offset += 0x20;
int real_count = 0;
for (int i = 0; i < count; ++i)
{
int block = file.View.ReadInt32 (index_offset + i * 0x14);
real_count += Convert.ToInt32 (block != block_start);
}
var dir = new List<Entry> (real_count);
for (int i = 0; i < count; ++i)
{
int block = file.View.ReadInt32 (index_offset);
if (block == block_start)
continue;
var entry = new Entry {
Name = GetEntryName (i),
Offset = file.View.ReadUInt32 (index_offset + 4),
Size = file.View.ReadUInt32 (index_offset + 12),
};
if (!entry.CheckPlacement (file.MaxOffset))
return null;
if (block > block_start)
{
entry.Type = "video";
}
dir.Add (entry);
index_offset += 0x14;
}
return new ArcFile (file, this, dir);
}
internal Func<int, string> CreateEntryNameDelegate (string arc_name)
{
var index = Sec5Opener.LookupIndex (arc_name);
string base_name = Path.GetFileNameWithoutExtension (arc_name);
if (null == index)
return n => GetDefaultName (base_name, n);
else
return (n) => {
Entry entry;
if (index.TryGetValue (n, out entry))
return entry.Name;
return GetDefaultName (base_name, n);
};
}
internal static string GetDefaultName (string base_name, int n)
{
return string.Format ("{0}#{1:D5}", base_name, n);
}
}
}

View File

@@ -76,7 +76,7 @@ namespace GameRes.Formats.Sas5
return new BinMemoryStream (code, entry.Name);
}
static void DecryptCodeSection (byte[] code)
protected static void DecryptCodeSection (byte[] code)
{
byte key = 0;
for (int i = 0; i < code.Length; ++i)
@@ -180,6 +180,203 @@ namespace GameRes.Formats.Sas5
using (var resr = new Res2Reader (input))
return resr.Read();
}
static internal Entry GetEntryBySectionName (ArcView file, string section_name)
{
if (file == null)
return null;
uint offset = 8;
while (offset < file.MaxOffset)
{
string name = file.View.ReadString (offset, 4, Encoding.ASCII);
if ("ENDS" == name)
break;
uint section_size = file.View.ReadUInt32 (offset+4);
offset += 8;
if (section_name == name)
{
var entry = new Entry {
Name = name,
Offset = offset,
Size = section_size,
};
return entry;
}
offset += section_size;
}
return null;
}
}
internal class Sep5Archive : ArcFile
{
private ArcView m_sec5;
public ArcView Sec5File { get { return m_sec5; } }
public Sep5Archive (ArcView arc, ArcView sec5, ArchiveFormat impl, ICollection<Entry> dir)
: base (arc, impl, dir)
{
m_sec5 = sec5;
}
}
internal class Sep5Entry : PackedEntry
{
public byte PType;
}
[Export(typeof(ArchiveFormat))]
public class Sep5Opener : Sec5Opener
{
public override string Tag { get { return "SEP5"; } }
public override string Description { get { return "SAS5 engine patched resource index file"; } }
public override uint Signature { get { return 0x35504553; } } // 'SEP5'
public override bool IsHierarchic { get { return false; } }
public override bool CanWrite { get { return false; } }
public override ArcFile TryOpen (ArcView file)
{
uint offset = 8;
var dir = new List<Entry>();
ArcView sec5 = null;
while (offset < file.MaxOffset)
{
string name = file.View.ReadString (offset, 4, Encoding.ASCII);
if ("ENDS" == name)
break;
uint section_size = file.View.ReadUInt32 (offset + 4);
offset += 8;
if ("OLDF" == name)
sec5 = new ArcView (file.View.ReadString (offset + 0x18, section_size - 0x18, Encoding.ASCII));
else
{
byte patch_type = file.View.ReadByte (offset);
uint size;
switch (patch_type)
{
case 0:
if (section_size != 1) return null;
var ent = GetEntryBySectionName (sec5, name);
if (ent == null) return null;
size = ent.Size;
break;
case 1:
size = section_size - 1;
break;
case 2:
if ("CODE" == name)
{
var tmp = new byte[0x1D];
file.View.Read (offset + 1, tmp, 0, 0x1D);
DecryptCodeSection (tmp);
size = BitConverter.ToUInt32 (tmp, 0x19);
}
else
size = file.View.ReadUInt32 (offset + 0x1A);
break;
default:
return null;
}
var entry = new Sep5Entry {
Name = name,
Offset = offset + 1,
Size = section_size - 1,
UnpackedSize = size,
PType = patch_type,
};
dir.Add (entry);
}
offset += section_size;
}
if (dir.Count > 0 && sec5 != null)
return new Sep5Archive (file, sec5, this, dir);
return null;
}
public override Stream OpenEntry (ArcFile arc, Entry entry)
{
var sent = entry as Sep5Entry;
var sep5 = arc as Sep5Archive;
var ent = GetEntryBySectionName (sep5.Sec5File, sent.Name);
var patch = new byte[sent.Size];
sep5.File.View.Read (sent.Offset, patch, 0, sent.Size);
var src = new byte[ent.Size];
sep5.Sec5File.View.Read (ent.Offset, src, 0, ent.Size);
if (sent.Name == "CODE")
{
DecryptCodeSection (src);
DecryptCodeSection (patch);
}
if (sent.PType == 0)
return new BinMemoryStream (src, sent.Name);
if (sent.PType == 1)
return new BinMemoryStream (patch, sent.Name);
var data = new byte[sent.UnpackedSize];
using (var mem = new MemoryStream (patch))
using (var reader = new BinaryReader (mem))
{
byte compressed_flag = reader.ReadByte();
var psizes = new uint[3];
var usizes = new uint[3];
var buffers = new byte[3][];
for (int i = 0; i < 3; ++i)
psizes[i] = reader.ReadUInt32();
for (int i = 0; i < 3; ++i)
usizes[i] = reader.ReadUInt32();
reader.ReadUInt32();
for (int i = 0; i < 3; ++i)
{
var packed = new byte[psizes[i]];
var unpacked = new byte[usizes[i]];
reader.Read (packed, 0, packed.Length);
if ((compressed_flag & (1 << i)) != 0)
{
using (var stream = new BinMemoryStream (packed))
using (var decompressor = new IarDecompressor (stream))
decompressor.Unpack (unpacked);
buffers[i] = unpacked;
}
else
buffers[i] = packed;
}
ApplyPatch (src, data, buffers[0], buffers[1], buffers[2]);
}
return new BinMemoryStream (data, sent.Name);
}
protected static void ApplyPatch (byte[] original, byte[] output, byte[] control, byte[] basic, byte[] append)
{
int original_index = 0, output_index = 0, basic_index = 0, append_index = 0;
using (var mem = new MemoryStream (control))
using (var reader = new BinaryReader (mem))
{
int base_size, tail_size, offset;
for (int i = 0; i < control.Length / 12; ++i)
{
base_size = reader.ReadInt32();
tail_size = reader.ReadInt32();
offset = reader.ReadInt32();
Buffer.BlockCopy (basic, basic_index, output, output_index, base_size);
for (int j = 0; j < base_size; j++)
output[output_index + j] += original[original_index + j];
Buffer.BlockCopy (append, append_index, output, output_index + base_size, tail_size);
basic_index += base_size;
append_index += tail_size;
original_index += base_size + offset;
output_index += base_size + tail_size;
}
}
}
}
internal sealed class Res2Reader : IDisposable