(FormatCatalog.FindFormats): new generic method.

This commit is contained in:
morkt 2016-12-17 18:51:33 +04:00
parent 7c989a0150
commit f3e3446c9a
4 changed files with 69 additions and 77 deletions

View File

@ -71,22 +71,11 @@ namespace GameRes
{
if (entry.Size < 4)
return null;
var filename = entry.Name;
var ext = new Lazy<string> (() => Path.GetExtension (filename).TrimStart ('.').ToLowerInvariant(), false);
var file = VFS.OpenView (entry);
try
{
uint signature = file.View.ReadUInt32 (0);
var tried = Enumerable.Empty<ArchiveFormat>();
for (;;)
{
var range = FormatCatalog.Instance.LookupSignature<ArchiveFormat> (signature);
if (tried.Any())
range = range.Except (tried);
// 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));
foreach (var impl in range)
foreach (var impl in FormatCatalog.Instance.FindFormats<ArchiveFormat> (entry.Name, signature))
{
try
{
@ -105,15 +94,10 @@ namespace GameRes
catch (Exception X)
{
// ignore failed open attmepts
Trace.WriteLine (string.Format ("[{0}] {1}: {2}", impl.Tag, filename, X.Message));
Trace.WriteLine (string.Format ("[{0}] {1}: {2}", impl.Tag, entry.Name, X.Message));
FormatCatalog.Instance.LastError = X;
}
}
if (0 == signature)
break;
signature = 0;
tried = range;
}
}
finally
{

View File

@ -171,11 +171,7 @@ namespace GameRes
public static SoundInput Read (IBinaryStream file)
{
uint signature = file.Signature;
for (;;)
{
var range = FormatCatalog.Instance.LookupSignature<AudioFormat> (signature);
foreach (var impl in range)
foreach (var impl in FormatCatalog.Instance.FindFormats<AudioFormat> (file.Name, file.Signature))
{
try
{
@ -184,15 +180,15 @@ namespace GameRes
if (null != sound)
return sound;
}
catch (OperationCanceledException)
{
throw;
}
catch (System.Exception X)
{
FormatCatalog.Instance.LastError = X;
}
}
if (0 == signature)
break;
signature = 0;
}
return null;
}

View File

@ -163,6 +163,32 @@ namespace GameRes
return LookupSignature (signature).OfType<Type>();
}
/// <summary>
/// Enumerate resources matching specified <paramref name="signature"/> and filename extension.
/// </summary>
internal IEnumerable<ResourceType> FindFormats<ResourceType> (string filename, uint signature) where ResourceType : IResource
{
var ext = new Lazy<string> (() => Path.GetExtension (filename).TrimStart ('.').ToLowerInvariant(), false);
var tried = Enumerable.Empty<ResourceType>();
for (;;)
{
var range = LookupSignature<ResourceType> (signature);
if (tried.Any())
range = range.Except (tried);
// 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));
foreach (var impl in range)
{
yield return impl;
}
if (0 == signature)
break;
signature = 0;
tried = range;
}
}
/// <summary>
/// Create GameRes.Entry corresponding to <paramref name="filename"/> extension.
/// <exception cref="System.ArgumentException">May be thrown if filename contains invalid

View File

@ -131,17 +131,7 @@ namespace GameRes
public static System.Tuple<ImageFormat, ImageMetaData> FindFormat (IBinaryStream file)
{
uint signature = file.Signature;
Lazy<string> ext = null;
if (!string.IsNullOrEmpty (file.Name))
ext = new Lazy<string> (() => Path.GetExtension (file.Name).TrimStart ('.').ToLowerInvariant(), false);
for (;;)
{
var range = FormatCatalog.Instance.LookupSignature<ImageFormat> (signature);
// check formats that match filename extension first
if (ext != null && range.Skip(1).Any())
range = range.OrderByDescending (f => f.Extensions.Any (e => e == ext.Value));
foreach (var impl in range)
foreach (var impl in FormatCatalog.Instance.FindFormats<ImageFormat> (file.Name, file.Signature))
{
try
{
@ -159,10 +149,6 @@ namespace GameRes
}
catch { }
}
if (0 == signature)
break;
signature = 0;
}
return null;
}