From 1f9a8e5a09e388fbf60b935ee8895d34b9d8f869 Mon Sep 17 00:00:00 2001 From: morkt Date: Sat, 9 Apr 2016 00:34:28 +0400 Subject: [PATCH] (IFileSystem): added GetFiles(pattern) method. --- GameRes/FileSystem.cs | 45 ++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 42 insertions(+), 3 deletions(-) diff --git a/GameRes/FileSystem.cs b/GameRes/FileSystem.cs index 580457f5..76bf43b0 100644 --- a/GameRes/FileSystem.cs +++ b/GameRes/FileSystem.cs @@ -62,6 +62,11 @@ namespace GameRes /// IEnumerable GetFiles (); + /// + /// Returns enumeration of files within current directory that match specified pattern. + /// + IEnumerable GetFiles (string pattern); + /// /// System.IO.Path.Combine() analog. /// @@ -131,6 +136,20 @@ namespace GameRes } } + public IEnumerable GetFiles (string pattern) + { + string path = Path.GetDirectoryName (pattern); + pattern = Path.GetFileName (pattern); + path = CombinePath (CurrentDirectory, path); + var info = new DirectoryInfo (path); + foreach (var file in info.EnumerateFiles (pattern)) + { + if (0 != (file.Attributes & (FileAttributes.Hidden | FileAttributes.System))) + continue; + yield return EntryFromFileInfo (file); + } + } + public IEnumerable GetFilesRecursive () { var info = new DirectoryInfo (CurrentDirectory); @@ -192,7 +211,7 @@ namespace GameRes public bool FileExists (string filename) { - return m_dir.ContainsKey (filename); + return m_dir.ContainsKey (CombinePath (CurrentDirectory, filename)); } public Stream OpenStream (Entry entry) @@ -218,6 +237,8 @@ namespace GameRes public abstract string CombinePath (string path1, string path2); + public abstract IEnumerable GetFiles (string pattern); + #region IDisposable Members bool _arc_disposed = false; @@ -280,6 +301,12 @@ namespace GameRes return m_arc.Dir; } + public override IEnumerable GetFiles (string pattern) + { + var glob = new FileNameGlob (pattern); + return m_arc.Dir.Where (f => glob.IsMatch (f.Name)); + } + public override string CombinePath (string path1, string path2) { return Path.Combine (path1, path2); @@ -366,6 +393,19 @@ namespace GameRes select file; } + public override IEnumerable GetFiles (string pattern) + { + string path = Path.GetDirectoryName (pattern); + pattern = Path.GetFileName (pattern); + path = CombinePath (CurrentDirectory, path); + if (!string.IsNullOrEmpty (path)) + path += PathDelimiter; + var glob = new FileNameGlob (pattern); + return from file in m_arc.Dir + where file.Name.StartsWith (path) && glob.IsMatch (Path.GetFileName (file.Name)) + select file; + } + public IEnumerable GetFilesRecursive (IEnumerable list) { var result = new List(); @@ -676,8 +716,7 @@ namespace GameRes /// public static IEnumerable GetFiles (string pattern) { - var glob = new FileNameGlob (pattern); - return GetFiles().Where (f => glob.IsMatch (Path.GetFileName (f.Name))); + return m_vfs.Top.GetFiles (pattern); } }