(ArcFile.TryOpen): some tweaks.

This commit is contained in:
morkt 2016-08-13 23:52:59 +04:00
parent b3d74fb17c
commit 0a606433f4
2 changed files with 11 additions and 5 deletions

View File

@ -64,25 +64,30 @@ namespace GameRes
/// </returns> /// </returns>
public static ArcFile TryOpen (string filename) public static ArcFile TryOpen (string filename)
{ {
var entry = VFS.FindFile (filename); return TryOpen (VFS.FindFile (filename));
}
public static ArcFile TryOpen (Entry entry)
{
if (entry.Size < 4) if (entry.Size < 4)
return null; return null;
var filename = entry.Name;
var ext = new Lazy<string> (() => Path.GetExtension (filename).TrimStart ('.').ToLowerInvariant()); var ext = new Lazy<string> (() => Path.GetExtension (filename).TrimStart ('.').ToLowerInvariant());
var file = VFS.OpenView (entry); var file = VFS.OpenView (entry);
try try
{ {
uint signature = file.View.ReadUInt32 (0); uint signature = file.View.ReadUInt32 (0);
var tried = new HashSet<ArchiveFormat>(); var tried = Enumerable.Empty<ArchiveFormat>();
for (;;) for (;;)
{ {
var range = FormatCatalog.Instance.LookupSignature<ArchiveFormat> (signature); var range = FormatCatalog.Instance.LookupSignature<ArchiveFormat> (signature);
if (tried.Any())
range = range.Except (tried);
// check formats that match filename extension first // check formats that match filename extension first
if (range.Skip(1).Any()) // if range.Count() > 1 if (range.Skip(1).Any()) // if range.Count() > 1
range = range.OrderByDescending (f => f.Extensions.Any (e => e == ext.Value)); range = range.OrderByDescending (f => f.Extensions.Any (e => e == ext.Value));
foreach (var impl in range) foreach (var impl in range)
{ {
if (!tried.Add (impl))
continue;
try try
{ {
var arc = impl.TryOpen (file); var arc = impl.TryOpen (file);
@ -107,6 +112,7 @@ namespace GameRes
if (0 == signature) if (0 == signature)
break; break;
signature = 0; signature = 0;
tried = range;
} }
} }
finally finally

View File

@ -550,7 +550,7 @@ namespace GameRes
return; return;
} }
Flush(); Flush();
var arc = ArcFile.TryOpen (entry.Name); var arc = ArcFile.TryOpen (entry);
if (null == arc) if (null == arc)
{ {
if (FormatCatalog.Instance.LastError is OperationCanceledException) if (FormatCatalog.Instance.LastError is OperationCanceledException)