(ArchiveFormat): added ContainedFormats property.

This commit is contained in:
morkt 2018-09-22 02:46:09 +04:00
parent cba145573f
commit 68f3904e27
4 changed files with 40 additions and 9 deletions

View File

@ -43,6 +43,9 @@ namespace GameRes
/// <summary>Short archive format description.</summary>
public string Description { get { return m_interface.Description; } }
/// <summary>Tags of formats related to this archive format (could be null).</summary>
public IEnumerable<string> ContainedFormats { get { return m_interface.ContainedFormats; } }
/// <summary>Memory-mapped view of the archive.</summary>
public ArcView File { get { return m_arc; } }

View File

@ -26,6 +26,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace GameRes
{
@ -41,8 +42,34 @@ namespace GameRes
/// </summary>
public abstract bool IsHierarchic { get; }
/// <summary>
/// Tags of formats related to this archive format (could be null).
/// </summary>
public IEnumerable<string> ContainedFormats { get; protected set; }
public abstract ArcFile TryOpen (ArcView view);
/// <summary>
/// Create GameRes.Entry corresponding to <paramref name="filename"/> extension.
/// </summary>
/// <exception cref="System.ArgumentException">May be thrown if filename contains invalid
/// characters.</exception>
public EntryType Create<EntryType> (string filename) where EntryType : Entry, new()
{
EntryType entry = null;
var formats = FormatCatalog.Instance.LookupFileName (filename);
if (formats.Any())
{
if (ContainedFormats != null && ContainedFormats.Any())
formats = formats.OrderByDescending (f => ContainedFormats.Contains (f.Tag));
entry = new EntryType { Type = formats.First().Type };
}
if (null == entry)
entry = new EntryType();
entry.Name = filename;
return entry;
}
/// <summary>
/// Extract file referenced by <paramref name="entry"/> into current directory.
/// </summary>

View File

@ -239,6 +239,13 @@ namespace GameRes
{
var ext = new Lazy<string> (() => Path.GetExtension (filename).TrimStart ('.').ToLowerInvariant(), false);
var tried = Enumerable.Empty<ResourceType>();
IEnumerable<string> preferred = null;
if (VFS.IsVirtual)
{
var arc_fs = VFS.Top as ArchiveFileSystem;
if (arc_fs != null)
preferred = arc_fs.Source.ContainedFormats;
}
for (;;)
{
var range = LookupSignature<ResourceType> (signature);
@ -247,6 +254,8 @@ namespace GameRes
// check formats that match filename extension first
if (range.Skip (1).Any()) // if range.Count() > 1
range = range.OrderByDescending (f => f.Extensions.Any (e => e == ext.Value));
if (preferred != null && preferred.Any())
range = range.OrderByDescending (f => preferred.Contains (f.Tag));
foreach (var impl in range)
{
yield return impl;
@ -268,7 +277,7 @@ namespace GameRes
EntryType entry = null;
var formats = LookupFileName (filename);
if (formats.Any())
entry = formats.First().Create<EntryType>();
entry = new EntryType { Type = formats.First().Type };
if (null == entry)
entry = new EntryType();
entry.Name = filename;

View File

@ -109,14 +109,6 @@ namespace GameRes
/// <summary>Resource access scheme suitable for serialization.</summary>
public virtual ResourceScheme Scheme { get; set; }
/// <summary>
/// Create empty Entry that corresponds to implemented resource.
/// </summary>
public EntryType Create<EntryType> () where EntryType : Entry, new()
{
return new EntryType { Type = this.Type };
}
protected IResource ()
{
Extensions = new string[] { GetDefaultExtension() };