From 81ab95ebf1db5c3fc429ecb4dc256dc1da531aa0 Mon Sep 17 00:00:00 2001 From: morkt Date: Wed, 2 Sep 2015 04:25:35 +0400 Subject: [PATCH] (ArchiveFileSystem): base abstract class for flat ant tree archive file systems. --- GameRes/ArcFile.cs | 2 +- GameRes/FileSystem.cs | 100 +++++++++++++++++++++++++----------------- 2 files changed, 60 insertions(+), 42 deletions(-) diff --git a/GameRes/ArcFile.cs b/GameRes/ArcFile.cs index 550ec121..afb85763 100644 --- a/GameRes/ArcFile.cs +++ b/GameRes/ArcFile.cs @@ -192,7 +192,7 @@ namespace GameRes public IFileSystem CreateFileSystem () { if (m_interface.IsHierarchic) - return new ArchiveFileSystem (this); + return new TreeArchiveFileSystem (this); else return new FlatArchiveFileSystem (this); } diff --git a/GameRes/FileSystem.cs b/GameRes/FileSystem.cs index c6728249..ee1f89b4 100644 --- a/GameRes/FileSystem.cs +++ b/GameRes/FileSystem.cs @@ -170,29 +170,16 @@ namespace GameRes } } - public class FlatArchiveFileSystem : IFileSystem + public abstract class ArchiveFileSystem : IFileSystem { protected readonly ArcFile m_arc; protected readonly Dictionary m_dir; public ArcFile Source { get { return m_arc; } } - public virtual string CurrentDirectory - { - get { return ""; } - set - { - if (string.IsNullOrEmpty (value)) - return; - if (".." == value || "." == value) - return; - if ("\\" == value || "/" == value) - return; - throw new DirectoryNotFoundException(); - } - } + public abstract string CurrentDirectory { get; set; } - public FlatArchiveFileSystem (ArcFile arc) + public ArchiveFileSystem (ArcFile arc) { m_arc = arc; m_dir = new Dictionary (arc.Dir.Count, StringComparer.InvariantCultureIgnoreCase); @@ -222,28 +209,13 @@ namespace GameRes return m_arc.OpenView (entry); } - public virtual Entry FindFile (string filename) - { - Entry entry = null; - if (!m_dir.TryGetValue (filename, out entry)) - throw new FileNotFoundException(); - return entry; - } + public abstract Entry FindFile (string filename); - public virtual IEnumerable GetFiles () - { - return m_arc.Dir; - } + public abstract IEnumerable GetFiles (); - public virtual IEnumerable GetFilesRecursive () - { - return m_arc.Dir; - } + public abstract IEnumerable GetFilesRecursive (); - public virtual string CombinePath (string path1, string path2) - { - return Path.Combine (path1, path2); - } + public abstract string CombinePath (string path1, string path2); #region IDisposable Members bool _arc_disposed = false; @@ -268,7 +240,52 @@ namespace GameRes #endregion } - public class ArchiveFileSystem : FlatArchiveFileSystem + public class FlatArchiveFileSystem : ArchiveFileSystem + { + public override string CurrentDirectory + { + get { return ""; } + set + { + if (string.IsNullOrEmpty (value)) + return; + if (".." == value || "." == value) + return; + if ("\\" == value || "/" == value) + return; + throw new DirectoryNotFoundException(); + } + } + + public FlatArchiveFileSystem (ArcFile arc) : base (arc) + { + } + + public override Entry FindFile (string filename) + { + Entry entry = null; + if (!m_dir.TryGetValue (filename, out entry)) + throw new FileNotFoundException(); + return entry; + } + + public override IEnumerable GetFiles () + { + return m_arc.Dir; + } + + public override IEnumerable GetFilesRecursive () + { + return m_arc.Dir; + } + + public override string CombinePath (string path1, string path2) + { + return Path.Combine (path1, path2); + } + } + + public class TreeArchiveFileSystem : ArchiveFileSystem { private string m_cwd; @@ -276,7 +293,7 @@ namespace GameRes private static readonly char[] m_path_delimiters = { '/', '\\' }; - public ArchiveFileSystem (ArcFile arc) : base (arc) + public TreeArchiveFileSystem (ArcFile arc) : base (arc) { m_cwd = ""; PathDelimiter = "/"; @@ -436,8 +453,9 @@ namespace GameRes if (entry.Name == LastVisitedPath && null != LastVisitedArc) { Push (LastVisitedPath, LastVisitedArc); - if (LastVisitedArc is FlatArchiveFileSystem) - CurrentArchive = (LastVisitedArc as FlatArchiveFileSystem).Source; + var fs = LastVisitedArc as ArchiveFileSystem; + if (null != fs) + CurrentArchive = fs.Source; return; } Flush(); @@ -469,8 +487,8 @@ namespace GameRes Flush(); LastVisitedArc = m_fs_stack.Pop(); LastVisitedPath = m_arc_name_stack.Pop(); - if (m_fs_stack.Count > 1 && m_fs_stack.Peek() is FlatArchiveFileSystem) - CurrentArchive = (m_fs_stack.Peek() as FlatArchiveFileSystem).Source; + if (m_fs_stack.Count > 1 && m_fs_stack.Peek() is ArchiveFileSystem) + CurrentArchive = (m_fs_stack.Peek() as ArchiveFileSystem).Source; else CurrentArchive = null; }