From f9448e4613578dd17106cc3189931c59a32cbc1a Mon Sep 17 00:00:00 2001 From: morkt Date: Tue, 22 Sep 2015 22:39:10 +0400 Subject: [PATCH] fixed filesystem traversal. --- GameRes/FileSystem.cs | 33 ++++++++++++--------------------- MainWindow.xaml.cs | 3 +++ 2 files changed, 15 insertions(+), 21 deletions(-) diff --git a/GameRes/FileSystem.cs b/GameRes/FileSystem.cs index 3f5ee865..a0789d99 100644 --- a/GameRes/FileSystem.cs +++ b/GameRes/FileSystem.cs @@ -582,31 +582,22 @@ namespace GameRes if (!value.Any()) return; var desired = value.ToArray(); - if (desired.Length == Count) + int desired_vfs_count = desired.Length; + var arc_iterator = m_vfs.ArcStack.Reverse().GetEnumerator(); + int i = 0; + while (i < desired_vfs_count - 1 && arc_iterator.MoveNext()) { - var cur_stack = m_vfs.ArcStack.Reverse(); - if (1 == Count || cur_stack.SequenceEqual (desired.Take (desired.Length-1))) - { - // desired path is identical to current, adjust final component only - m_vfs.Top.CurrentDirectory = desired.Last(); - return; - } + if (arc_iterator.Current != desired[i]) + break; + ++i; } - // desired path is different, rebuild filesystem hierarchy from scratch - var new_vfs = new FileSystemStack(); - try + while (Count > i+1) + m_vfs.Pop(); + while (Count < desired_vfs_count) { - for (int i = 0; i < desired.Length-1; ++i) - new_vfs.ChDir (new_vfs.Top.FindFile (desired[i])); - new_vfs.Top.CurrentDirectory = desired.Last(); - m_vfs.Dispose(); + m_vfs.ChDir (m_vfs.Top.FindFile (desired[Count-1])); } - catch - { - new_vfs.Dispose(); - throw; - } - m_vfs = new_vfs; + m_vfs.Top.CurrentDirectory = desired.Last(); } } diff --git a/MainWindow.xaml.cs b/MainWindow.xaml.cs index 1ed13070..6df8556a 100644 --- a/MainWindow.xaml.cs +++ b/MainWindow.xaml.cs @@ -685,6 +685,9 @@ namespace GARbro.GUI } catch (Exception X) { + // if VFS.FullPath throws an exception, ViewModel becomes inconsistent at this point + // and should be rebuilt + ViewModel = CreateViewModel (VFS.Top.CurrentDirectory, true); SetStatusText (X.Message); return false; }