mirror of
https://github.com/crskycode/GARbro.git
synced 2024-12-24 03:44:13 +08:00
(FormatCatalog.FindFormats): new generic method.
This commit is contained in:
parent
7c989a0150
commit
f3e3446c9a
@ -71,48 +71,32 @@ 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 (;;)
|
||||
foreach (var impl in FormatCatalog.Instance.FindFormats<ArchiveFormat> (entry.Name, signature))
|
||||
{
|
||||
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)
|
||||
try
|
||||
{
|
||||
try
|
||||
var arc = impl.TryOpen (file);
|
||||
if (null != arc)
|
||||
{
|
||||
var arc = impl.TryOpen (file);
|
||||
if (null != arc)
|
||||
{
|
||||
file = null; // file ownership passed to ArcFile
|
||||
return arc;
|
||||
}
|
||||
}
|
||||
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, filename, X.Message));
|
||||
FormatCatalog.Instance.LastError = X;
|
||||
file = null; // file ownership passed to ArcFile
|
||||
return arc;
|
||||
}
|
||||
}
|
||||
if (0 == signature)
|
||||
break;
|
||||
signature = 0;
|
||||
tried = range;
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
finally
|
||||
|
@ -171,27 +171,23 @@ namespace GameRes
|
||||
|
||||
public static SoundInput Read (IBinaryStream file)
|
||||
{
|
||||
uint signature = file.Signature;
|
||||
for (;;)
|
||||
foreach (var impl in FormatCatalog.Instance.FindFormats<AudioFormat> (file.Name, file.Signature))
|
||||
{
|
||||
var range = FormatCatalog.Instance.LookupSignature<AudioFormat> (signature);
|
||||
foreach (var impl in range)
|
||||
try
|
||||
{
|
||||
try
|
||||
{
|
||||
file.Position = 0;
|
||||
SoundInput sound = impl.TryOpen (file);
|
||||
if (null != sound)
|
||||
return sound;
|
||||
}
|
||||
catch (System.Exception X)
|
||||
{
|
||||
FormatCatalog.Instance.LastError = X;
|
||||
}
|
||||
file.Position = 0;
|
||||
SoundInput sound = impl.TryOpen (file);
|
||||
if (null != sound)
|
||||
return sound;
|
||||
}
|
||||
catch (OperationCanceledException)
|
||||
{
|
||||
throw;
|
||||
}
|
||||
catch (System.Exception X)
|
||||
{
|
||||
FormatCatalog.Instance.LastError = X;
|
||||
}
|
||||
if (0 == signature)
|
||||
break;
|
||||
signature = 0;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
@ -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
|
||||
|
@ -131,37 +131,23 @@ 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 (;;)
|
||||
foreach (var impl in FormatCatalog.Instance.FindFormats<ImageFormat> (file.Name, file.Signature))
|
||||
{
|
||||
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)
|
||||
try
|
||||
{
|
||||
try
|
||||
file.Position = 0;
|
||||
ImageMetaData metadata = impl.ReadMetaData (file);
|
||||
if (null != metadata)
|
||||
{
|
||||
file.Position = 0;
|
||||
ImageMetaData metadata = impl.ReadMetaData (file);
|
||||
if (null != metadata)
|
||||
{
|
||||
metadata.FileName = file.Name;
|
||||
return Tuple.Create (impl, metadata);
|
||||
}
|
||||
metadata.FileName = file.Name;
|
||||
return Tuple.Create (impl, metadata);
|
||||
}
|
||||
catch (OperationCanceledException)
|
||||
{
|
||||
throw;
|
||||
}
|
||||
catch { }
|
||||
}
|
||||
if (0 == signature)
|
||||
break;
|
||||
signature = 0;
|
||||
catch (OperationCanceledException)
|
||||
{
|
||||
throw;
|
||||
}
|
||||
catch { }
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user