From b3ccb3cd5037930eb5085e1d7951653f0e6f3549 Mon Sep 17 00:00:00 2001 From: morkt Date: Sun, 23 Sep 2018 03:52:53 +0400 Subject: [PATCH] (GameRes): moved preferred formats logic to GetTypeFromName method. --- GameRes/ArchiveFormat.cs | 21 ++++++++------------- GameRes/FormatCatalog.cs | 22 ++++++++++------------ 2 files changed, 18 insertions(+), 25 deletions(-) diff --git a/GameRes/ArchiveFormat.cs b/GameRes/ArchiveFormat.cs index 6a5ca538..2ba7937d 100644 --- a/GameRes/ArchiveFormat.cs +++ b/GameRes/ArchiveFormat.cs @@ -56,18 +56,10 @@ namespace GameRes /// characters. public EntryType Create (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; + return new EntryType { + Name = filename, + Type = FormatCatalog.Instance.GetTypeFromName (filename, ContainedFormats), + }; } /// @@ -85,7 +77,10 @@ namespace GameRes /// public virtual Stream OpenEntry (ArcFile arc, Entry entry) { - return arc.File.CreateStream (entry.Offset, entry.Size, entry.Name); + if (entry.Size > 0) + return arc.File.CreateStream (entry.Offset, entry.Size, entry.Name); + else + return Stream.Null; } /// diff --git a/GameRes/FormatCatalog.cs b/GameRes/FormatCatalog.cs index 1e2c520a..98354db2 100644 --- a/GameRes/FormatCatalog.cs +++ b/GameRes/FormatCatalog.cs @@ -274,22 +274,20 @@ namespace GameRes /// characters. public EntryType Create (string filename) where EntryType : Entry, new() { - EntryType entry = null; - var formats = LookupFileName (filename); - if (formats.Any()) - entry = new EntryType { Type = formats.First().Type }; - if (null == entry) - entry = new EntryType(); - entry.Name = filename; - return entry; + return new EntryType { + Name = filename, + Type = GetTypeFromName (filename), + }; } - public string GetTypeFromName (string filename) + public string GetTypeFromName (string filename, IEnumerable preferred_formats = null) { var formats = LookupFileName (filename); - if (formats.Any()) - return formats.First().Type; - return ""; + if (!formats.Any()) + return ""; + if (preferred_formats != null && preferred_formats.Any()) + formats = formats.OrderByDescending (f => preferred_formats.Contains (f.Tag)); + return formats.First().Type; } public void InvokeParametersRequest (object source, ParametersRequestEventArgs args)