mirror of
https://github.com/crskycode/GARbro.git
synced 2026-07-08 01:30:18 +08:00
implemented 'vff' archives and GAL images.
This commit is contained in:
244
ArcFormats/LiveMaker/ArcVF.cs
Normal file
244
ArcFormats/LiveMaker/ArcVF.cs
Normal file
@@ -0,0 +1,244 @@
|
||||
//! \file ArcVF.cs
|
||||
//! \date Wed Jun 08 00:27:36 2016
|
||||
//! \brief LiveMaker resource archive.
|
||||
//
|
||||
// Copyright (C) 2016 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.Diagnostics;
|
||||
using System.IO;
|
||||
using GameRes.Compression;
|
||||
|
||||
namespace GameRes.Formats.LiveMaker
|
||||
{
|
||||
[Export(typeof(ArchiveFormat))]
|
||||
public class VffOpener : ArchiveFormat
|
||||
{
|
||||
public override string Tag { get { return "DAT/vf"; } }
|
||||
public override string Description { get { return "LiveMaker resource archive"; } }
|
||||
public override uint Signature { get { return 0x666676; } } // 'vff'
|
||||
public override bool IsHierarchic { get { return true; } }
|
||||
public override bool CanCreate { get { return false; } }
|
||||
|
||||
public VffOpener ()
|
||||
{
|
||||
Extensions = new string[] { "dat" };
|
||||
Signatures = new uint[] { 0x666676, 0 };
|
||||
}
|
||||
|
||||
public override ArcFile TryOpen (ArcView file)
|
||||
{
|
||||
ArcView index_file = file;
|
||||
ArcView extra_file = null;
|
||||
try
|
||||
{
|
||||
// possible filesystem structure:
|
||||
// game.dat -- main archive body
|
||||
// game.001 -- [optional] extra part
|
||||
// game.ext -- [optional] separate index (could be included into the main body)
|
||||
|
||||
if (!file.Name.EndsWith (".dat", StringComparison.InvariantCultureIgnoreCase))
|
||||
return null;
|
||||
uint signature = index_file.View.ReadUInt32 (0);
|
||||
if (0x666676 != signature)
|
||||
{
|
||||
var ext_filename = Path.ChangeExtension (file.Name, ".ext");
|
||||
if (!VFS.FileExists (ext_filename))
|
||||
return null;
|
||||
index_file = VFS.OpenView (ext_filename);
|
||||
signature = index_file.View.ReadUInt32 (0);
|
||||
if (0x666676 != signature)
|
||||
return null;
|
||||
}
|
||||
int count = index_file.View.ReadInt32 (6);
|
||||
if (!IsSaneCount (count))
|
||||
return null;
|
||||
|
||||
var dir = ReadIndex (index_file, count);
|
||||
if (null == dir)
|
||||
return null;
|
||||
long max_offset = file.MaxOffset;
|
||||
for (int i = 0; i < dir.Count; ++i)
|
||||
{
|
||||
if (!dir[i].CheckPlacement (max_offset))
|
||||
{
|
||||
if (extra_file != null)
|
||||
{
|
||||
// remove entries that don't fit into game.dat+game.001
|
||||
int discard = dir.Count - i;
|
||||
Trace.WriteLine (string.Format ("{0} entries didn't fit and were discarded", discard), "[vff]");
|
||||
dir.RemoveRange (i, discard);
|
||||
break;
|
||||
}
|
||||
var extra_filename = Path.ChangeExtension (file.Name, ".001");
|
||||
if (!VFS.FileExists (extra_filename))
|
||||
return null;
|
||||
extra_file = VFS.OpenView (extra_filename);
|
||||
max_offset += extra_file.MaxOffset;
|
||||
if (!dir[i].CheckPlacement (max_offset))
|
||||
return null;
|
||||
}
|
||||
}
|
||||
if (null == extra_file)
|
||||
return new ArcFile (file, this, dir);
|
||||
return new VffArchive (file, this, dir, extra_file);
|
||||
}
|
||||
catch
|
||||
{
|
||||
if (extra_file != null)
|
||||
extra_file.Dispose();
|
||||
throw;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (index_file != file)
|
||||
index_file.Dispose();
|
||||
}
|
||||
}
|
||||
|
||||
public override Stream OpenEntry (ArcFile arc, Entry entry)
|
||||
{
|
||||
var vff = arc as VffArchive;
|
||||
Stream input = null;
|
||||
if (vff != null)
|
||||
input = vff.OpenStream (entry);
|
||||
else
|
||||
input = arc.File.CreateStream (entry.Offset, entry.Size);
|
||||
|
||||
var pent = entry as PackedEntry;
|
||||
if (pent != null && pent.IsPacked)
|
||||
return new ZLibStream (input, CompressionMode.Decompress);
|
||||
else
|
||||
return input;
|
||||
}
|
||||
|
||||
List<Entry> ReadIndex (ArcView file, int count)
|
||||
{
|
||||
uint index_offset = 0xA;
|
||||
var name_buffer = new byte[0x100];
|
||||
var rnd = new TpRandom (0x75D6EE39u);
|
||||
var dir = new List<Entry> (count);
|
||||
for (int i = 0; i < count; ++i)
|
||||
{
|
||||
uint name_length = file.View.ReadUInt32 (index_offset);
|
||||
index_offset += 4;
|
||||
if (0 == name_length || name_length > name_buffer.Length)
|
||||
return null;
|
||||
if (name_length != file.View.Read (index_offset, name_buffer, 0, name_length))
|
||||
return null;
|
||||
index_offset += name_length;
|
||||
|
||||
var name = DecryptName (name_buffer, (int)name_length, rnd);
|
||||
dir.Add (FormatCatalog.Instance.Create<PackedEntry> (name));
|
||||
}
|
||||
rnd.Reset();
|
||||
uint offset = file.View.ReadUInt32 (index_offset) ^ rnd.GetRand32();
|
||||
foreach (var entry in dir)
|
||||
{
|
||||
index_offset += 8;
|
||||
uint next_offset = file.View.ReadUInt32 (index_offset) ^ rnd.GetRand32();
|
||||
entry.Offset = offset;
|
||||
entry.Size = next_offset - offset;
|
||||
offset = next_offset;
|
||||
}
|
||||
index_offset += 8;
|
||||
foreach (PackedEntry entry in dir)
|
||||
{
|
||||
entry.IsPacked = 0 == file.View.ReadByte (index_offset++);
|
||||
}
|
||||
return dir;
|
||||
}
|
||||
|
||||
string DecryptName (byte[] name_buf, int name_length, TpRandom key)
|
||||
{
|
||||
for (int i = 0; i < name_length; ++i)
|
||||
{
|
||||
name_buf[i] ^= (byte)key.GetRand32();
|
||||
}
|
||||
return Encodings.cp932.GetString (name_buf, 0, name_length);
|
||||
}
|
||||
}
|
||||
|
||||
internal class TpRandom
|
||||
{
|
||||
uint m_seed;
|
||||
uint m_current;
|
||||
|
||||
public TpRandom (uint seed)
|
||||
{
|
||||
m_seed = seed;
|
||||
m_current = 0;
|
||||
}
|
||||
|
||||
public uint GetRand32 ()
|
||||
{
|
||||
m_current += m_current << 2;
|
||||
m_current += m_seed;
|
||||
return m_current;
|
||||
}
|
||||
|
||||
public void Reset ()
|
||||
{
|
||||
m_current = 0;
|
||||
}
|
||||
}
|
||||
|
||||
internal class VffArchive : ArcFile
|
||||
{
|
||||
readonly ArcView ExtraFile;
|
||||
|
||||
public VffArchive (ArcView arc, ArchiveFormat impl, ICollection<Entry> dir, ArcView extra_file)
|
||||
: base (arc, impl, dir)
|
||||
{
|
||||
ExtraFile = extra_file;
|
||||
}
|
||||
|
||||
internal Stream OpenStream (Entry entry)
|
||||
{
|
||||
if (entry.Offset < File.MaxOffset && entry.Offset+entry.Size > File.MaxOffset)
|
||||
{
|
||||
var first_part = File.View.ReadBytes (entry.Offset, entry.Size);
|
||||
var second_part = ExtraFile.CreateStream (0, entry.Size - (uint)first_part.Length);
|
||||
return new PrefixStream (first_part, second_part);
|
||||
}
|
||||
else if (entry.Offset >= File.MaxOffset)
|
||||
{
|
||||
return ExtraFile.CreateStream (entry.Offset, entry.Size);
|
||||
}
|
||||
else
|
||||
return File.CreateStream (entry.Offset, entry.Size);
|
||||
}
|
||||
|
||||
bool _vff_disposed = false;
|
||||
protected override void Dispose (bool disposing)
|
||||
{
|
||||
if (!_vff_disposed)
|
||||
{
|
||||
if (disposing && ExtraFile != null)
|
||||
ExtraFile.Dispose();
|
||||
_vff_disposed = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user