2014-07-21 23:26:28 +04:00
|
|
|
//! \file ArcFile.cs
|
|
|
|
//! \date Tue Jul 08 12:53:45 2014
|
|
|
|
//! \brief Game Archive file class.
|
|
|
|
//
|
2015-05-14 15:58:16 +04:00
|
|
|
// Copyright (C) 2014-2015 by morkt
|
2014-07-28 00:50:18 +04:00
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
//
|
2014-07-21 23:26:28 +04:00
|
|
|
|
|
|
|
using System;
|
|
|
|
using System.IO;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
|
|
|
namespace GameRes
|
|
|
|
{
|
|
|
|
public class ArcFile : IDisposable
|
|
|
|
{
|
|
|
|
private ArcView m_arc;
|
|
|
|
private ArchiveFormat m_interface;
|
|
|
|
private ICollection<Entry> m_dir;
|
|
|
|
|
|
|
|
/// <summary>Tag that identifies this archive format.</summary>
|
|
|
|
public string Tag { get { return m_interface.Tag; } }
|
|
|
|
|
|
|
|
/// <summary>Short archive format description.</summary>
|
|
|
|
public string Description { get { return m_interface.Description; } }
|
|
|
|
|
2018-09-22 02:46:09 +04:00
|
|
|
/// <summary>Tags of formats related to this archive format (could be null).</summary>
|
|
|
|
public IEnumerable<string> ContainedFormats { get { return m_interface.ContainedFormats; } }
|
|
|
|
|
2014-07-21 23:26:28 +04:00
|
|
|
/// <summary>Memory-mapped view of the archive.</summary>
|
|
|
|
public ArcView File { get { return m_arc; } }
|
|
|
|
|
|
|
|
/// <summary>Archive contents.</summary>
|
|
|
|
public ICollection<Entry> Dir { get { return m_dir; } }
|
|
|
|
|
|
|
|
public ArcFile (ArcView arc, ArchiveFormat impl, ICollection<Entry> dir)
|
|
|
|
{
|
|
|
|
m_arc = arc;
|
|
|
|
m_interface = impl;
|
|
|
|
m_dir = dir;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Try to open <paramref name="filename"/> as archive.
|
|
|
|
/// </summary>
|
2014-07-29 14:17:41 +04:00
|
|
|
/// <returns>
|
|
|
|
/// ArcFile object if file is opened successfully, null otherwise.
|
|
|
|
/// </returns>
|
2014-07-21 23:26:28 +04:00
|
|
|
public static ArcFile TryOpen (string filename)
|
|
|
|
{
|
2016-08-13 23:52:59 +04:00
|
|
|
return TryOpen (VFS.FindFile (filename));
|
|
|
|
}
|
|
|
|
|
|
|
|
public static ArcFile TryOpen (Entry entry)
|
|
|
|
{
|
2015-08-31 10:48:27 +04:00
|
|
|
if (entry.Size < 4)
|
2015-07-11 10:19:05 +04:00
|
|
|
return null;
|
2015-08-31 10:48:27 +04:00
|
|
|
var file = VFS.OpenView (entry);
|
2014-07-21 23:26:28 +04:00
|
|
|
try
|
|
|
|
{
|
|
|
|
uint signature = file.View.ReadUInt32 (0);
|
2016-12-17 18:51:33 +04:00
|
|
|
foreach (var impl in FormatCatalog.Instance.FindFormats<ArchiveFormat> (entry.Name, signature))
|
2014-07-21 23:26:28 +04:00
|
|
|
{
|
2016-12-17 18:51:33 +04:00
|
|
|
try
|
2014-07-21 23:26:28 +04:00
|
|
|
{
|
2016-12-17 18:51:33 +04:00
|
|
|
var arc = impl.TryOpen (file);
|
|
|
|
if (null != arc)
|
2014-07-21 23:26:28 +04:00
|
|
|
{
|
2016-12-17 18:51:33 +04:00
|
|
|
file = null; // file ownership passed to ArcFile
|
|
|
|
return arc;
|
2014-07-21 23:26:28 +04:00
|
|
|
}
|
|
|
|
}
|
2016-12-17 18:51:33 +04:00
|
|
|
catch (OperationCanceledException X)
|
|
|
|
{
|
|
|
|
FormatCatalog.Instance.LastError = X;
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
catch (Exception X)
|
|
|
|
{
|
|
|
|
// ignore failed open attmepts
|
|
|
|
Trace.WriteLine (string.Format ("[{0}] {1}: {2}", impl.Tag, entry.Name, X.Message));
|
|
|
|
FormatCatalog.Instance.LastError = X;
|
|
|
|
}
|
2014-07-21 23:26:28 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
finally
|
|
|
|
{
|
|
|
|
if (null != file)
|
|
|
|
file.Dispose();
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Extract all entries from the archive into current directory.
|
|
|
|
/// <paramref name="callback"/> could be used to observe/control extraction process.
|
|
|
|
/// </summary>
|
2014-07-27 03:37:51 +04:00
|
|
|
public void ExtractFiles (EntryCallback callback)
|
2014-07-21 23:26:28 +04:00
|
|
|
{
|
|
|
|
int i = 0;
|
|
|
|
foreach (var entry in Dir.OrderBy (e => e.Offset))
|
|
|
|
{
|
2014-07-27 03:37:51 +04:00
|
|
|
var action = callback (i, entry, null);
|
|
|
|
if (ArchiveOperation.Abort == action)
|
2014-07-21 23:26:28 +04:00
|
|
|
break;
|
2014-07-27 03:37:51 +04:00
|
|
|
if (ArchiveOperation.Skip != action)
|
2014-07-21 23:26:28 +04:00
|
|
|
Extract (entry);
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Extract specified <paramref name="entry"/> into current directory.
|
|
|
|
/// </summary>
|
|
|
|
public void Extract (Entry entry)
|
|
|
|
{
|
|
|
|
if (-1 != entry.Offset)
|
|
|
|
m_interface.Extract (this, entry);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Open specified <paramref name="entry"/> as Stream.
|
|
|
|
/// </summary>
|
|
|
|
public Stream OpenEntry (Entry entry)
|
|
|
|
{
|
|
|
|
return m_interface.OpenEntry (this, entry);
|
|
|
|
}
|
|
|
|
|
2015-06-08 19:58:51 +04:00
|
|
|
/// <summary>
|
|
|
|
/// Open specified <paramref name="entry"/> as memory-mapped view.
|
|
|
|
/// </summary>
|
|
|
|
public ArcView OpenView (Entry entry)
|
|
|
|
{
|
2015-06-19 15:59:06 +04:00
|
|
|
using (var stream = OpenEntry (entry))
|
|
|
|
{
|
|
|
|
uint size;
|
2015-07-25 10:12:56 +04:00
|
|
|
var packed_entry = entry as PackedEntry;
|
2015-06-19 15:59:06 +04:00
|
|
|
if (stream.CanSeek)
|
|
|
|
size = (uint)stream.Length;
|
2015-07-25 10:12:56 +04:00
|
|
|
else if (null != packed_entry && packed_entry.IsPacked)
|
2016-06-11 06:38:47 +04:00
|
|
|
{
|
2015-07-25 10:12:56 +04:00
|
|
|
size = packed_entry.UnpackedSize;
|
2016-06-11 06:38:47 +04:00
|
|
|
if (0 == size)
|
|
|
|
{
|
|
|
|
using (var copy = new MemoryStream())
|
|
|
|
{
|
|
|
|
stream.CopyTo (copy);
|
|
|
|
copy.Position = 0;
|
|
|
|
return new ArcView (copy, entry.Name, (uint)copy.Length);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-06-19 15:59:06 +04:00
|
|
|
else
|
|
|
|
size = entry.Size;
|
2016-06-11 06:38:47 +04:00
|
|
|
if (0 == size)
|
|
|
|
throw new FileSizeException (Strings.garStrings.MsgFileIsEmpty);
|
2015-06-19 15:59:06 +04:00
|
|
|
return new ArcView (stream, entry.Name, size);
|
|
|
|
}
|
2015-06-08 19:58:51 +04:00
|
|
|
}
|
|
|
|
|
2015-06-02 22:53:20 +04:00
|
|
|
/// <summary>
|
|
|
|
/// Open specified <paramref name="entry"/> as a seekable Stream.
|
|
|
|
/// </summary>
|
|
|
|
public Stream OpenSeekableEntry (Entry entry)
|
|
|
|
{
|
|
|
|
var input = OpenEntry (entry);
|
|
|
|
if (input.CanSeek)
|
|
|
|
return input;
|
|
|
|
using (input)
|
|
|
|
{
|
2015-06-27 14:31:41 +04:00
|
|
|
int capacity = (int)entry.Size;
|
|
|
|
var packed_entry = entry as PackedEntry;
|
|
|
|
if (packed_entry != null && packed_entry.UnpackedSize != 0)
|
|
|
|
capacity = (int)packed_entry.UnpackedSize;
|
|
|
|
var copy = new MemoryStream (capacity);
|
2015-06-02 22:53:20 +04:00
|
|
|
input.CopyTo (copy);
|
2015-06-27 14:31:41 +04:00
|
|
|
copy.Position = 0;
|
2015-06-02 22:53:20 +04:00
|
|
|
return copy;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-15 09:34:46 +04:00
|
|
|
public IBinaryStream OpenBinaryEntry (Entry entry)
|
|
|
|
{
|
|
|
|
var input = OpenSeekableEntry (entry);
|
2016-10-16 16:15:37 +04:00
|
|
|
return BinaryStream.FromStream (input, entry.Name);
|
2016-10-15 09:34:46 +04:00
|
|
|
}
|
|
|
|
|
2016-10-25 18:18:51 +04:00
|
|
|
public IImageDecoder OpenImage (Entry entry)
|
|
|
|
{
|
|
|
|
return m_interface.OpenImage (this, entry);
|
|
|
|
}
|
|
|
|
|
2015-09-06 05:31:06 +04:00
|
|
|
public ArchiveFileSystem CreateFileSystem ()
|
2015-06-08 19:58:51 +04:00
|
|
|
{
|
|
|
|
if (m_interface.IsHierarchic)
|
2015-09-02 04:25:35 +04:00
|
|
|
return new TreeArchiveFileSystem (this);
|
2015-06-08 19:58:51 +04:00
|
|
|
else
|
|
|
|
return new FlatArchiveFileSystem (this);
|
|
|
|
}
|
|
|
|
|
2014-07-21 23:26:28 +04:00
|
|
|
#region IDisposable Members
|
|
|
|
bool disposed = false;
|
|
|
|
|
|
|
|
public void Dispose ()
|
|
|
|
{
|
|
|
|
Dispose (true);
|
|
|
|
GC.SuppressFinalize (this);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected virtual void Dispose (bool disposing)
|
|
|
|
{
|
|
|
|
if (!disposed)
|
|
|
|
{
|
|
|
|
if (disposing)
|
|
|
|
m_arc.Dispose();
|
|
|
|
disposed = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endregion
|
|
|
|
}
|
|
|
|
}
|