33using System ;
44using System . Collections . Generic ;
55using System . IO ;
6+ using System . Linq ;
67using System . Security ;
78
89namespace CENTIS . UnityFileExplorer
@@ -73,15 +74,10 @@ public virtual void FindFile(
7374
7475 // used to navigate to the given startFolder and create all nodes, that are visited during the navigation
7576 var startFolderPath = Environment . GetFolderPath ( ( Environment . SpecialFolder ) startFolder ) ;
76- DirectoryInfo startDir = new ( startFolderPath ) ;
77- VirtualFolderNode startParent = FindParentRecursive ( startDir ) ;
78- FolderNode startNode = new ( _config , startDir . GetNodeInformation ( ) , startParent ) ;
79- startNode . OnSelected += SelectNode ;
80- startNode . OnDeselected += DeselectNode ;
81- startNode . OnActivated += ActivateNode ;
82- startParent . AddChild ( startNode ) ;
83- _hashedNodes . Add ( startNode . ToString ( ) , startNode ) ;
84- NavigateToNode ( startNode ) ;
77+ var startNode = FindStartFolder ( new ( startFolderPath ) ) ;
78+ LoadFolder ( startNode ) ;
79+ _currentFolder = startNode ;
80+ _currentFolder . NavigateTo ( ) ;
8581 UpdatePath ( ) ;
8682 }
8783
@@ -256,48 +252,54 @@ private void GoForward()
256252
257253 private void NavigateToNode ( VirtualFolderNode node )
258254 {
259- try {
260- if ( ! node . IsFolderLoaded )
255+ if ( ! LoadFolder ( node ) ) return ;
256+
257+ _lastVisitedNodes . Add ( _currentFolder ) ;
258+ _lastReturnedFromNodes . Clear ( ) ;
259+ if ( _config . ArrowBackButton != null )
260+ _config . ArrowBackButton . interactable = true ;
261+ if ( _config . ArrowForwardButton != null )
262+ _config . ArrowForwardButton . interactable = false ;
263+
264+ _currentFolder . NavigateFrom ( ) ;
265+ _currentFolder = node ;
266+ _currentFolder . NavigateTo ( ) ;
267+
268+ UpdatePath ( ) ;
269+ }
270+
271+ private bool LoadFolder ( VirtualFolderNode node )
272+ {
273+ if ( node . IsFolderLoaded ) return true ;
274+
275+ try
276+ {
277+ AddDirectories ( node ) ;
278+ AddFiles ( node ) ;
279+ if ( ! node . IsFolderLoaded || node . Children . Count == 0 )
261280 {
262- AddDirectories ( node ) ;
263- AddFiles ( node ) ;
264- if ( ! node . IsFolderLoaded || node . Children . Count == 0 )
265- {
266- EmptyNode emptyNode = new ( _config , node ) ;
267- node . AddChild ( emptyNode ) ;
268- }
281+ EmptyNode emptyNode = new ( _config , node ) ;
282+ node . AddChild ( emptyNode ) ;
269283 }
284+
285+ return true ;
270286 } catch ( Exception e ) {
271287 switch ( e )
272288 {
273289 case SecurityException :
274290 case UnauthorizedAccessException :
275291 node . OnFailedToLoad ( ENodeFailedToLoad . MissingPermissions ) ;
276- return ;
292+ return false ;
277293 case PathTooLongException :
278294 node . OnFailedToLoad ( ENodeFailedToLoad . PathTooLong ) ;
279- return ;
295+ return false ;
280296 case IOException :
281297 node . OnFailedToLoad ( ENodeFailedToLoad . InvalidNode ) ;
282- return ;
298+ return false ;
283299 default :
284- Console . WriteLine ( e ) ;
285300 throw ;
286301 }
287- }
288-
289- _lastVisitedNodes . Add ( _currentFolder ) ;
290- _lastReturnedFromNodes . Clear ( ) ;
291- if ( _config . ArrowBackButton != null )
292- _config . ArrowBackButton . interactable = true ;
293- if ( _config . ArrowForwardButton != null )
294- _config . ArrowForwardButton . interactable = false ;
295-
296- _currentFolder . NavigateFrom ( ) ;
297- _currentFolder = node ;
298- _currentFolder . NavigateTo ( ) ;
299-
300- UpdatePath ( ) ;
302+ }
301303 }
302304
303305 private void AddDirectories ( VirtualFolderNode folder )
@@ -321,17 +323,9 @@ private void AddFiles(VirtualFolderNode folder)
321323 IEnumerable < FileInfo > containedFiles = new DirectoryInfo ( folderPath ) . GetFiles ( ) ;
322324 foreach ( var file in containedFiles )
323325 {
324- var isCorrectType = false ;
325- if ( _fileExtensions != null )
326- {
327- foreach ( var extension in _fileExtensions )
328- {
329- if ( ! file . Name . EndsWith ( extension ) ) continue ;
330- isCorrectType = true ;
331- break ;
332- }
333- }
334- if ( ! isCorrectType && _fileExtensions != null ) continue ;
326+ if ( _fileExtensions != null
327+ && ! _fileExtensions . Any ( extension => file . Name . EndsWith ( extension ) ) )
328+ continue ;
335329
336330 FileNode fileNode = new ( _config , file . GetNodeInformation ( ) , folder ) ;
337331 fileNode . OnSelected += SelectNode ;
@@ -342,19 +336,19 @@ private void AddFiles(VirtualFolderNode folder)
342336 }
343337 }
344338
345- private FolderNode FindParentRecursive ( DirectoryInfo userDir )
339+ private FolderNode FindStartFolder ( DirectoryInfo userDir )
346340 {
347- if ( _hashedNodes . TryGetValue ( userDir . Parent . ToString ( ) , out TreeNode parent ) )
348- return ( FolderNode ) parent ;
349-
350- FolderNode nextParent = FindParentRecursive ( userDir . Parent ) ;
351- FolderNode newNode = new ( _config , userDir . Parent . GetNodeInformation ( ) , nextParent ) ;
352- newNode . OnSelected += SelectNode ;
353- newNode . OnDeselected += DeselectNode ;
354- newNode . OnActivated += ActivateNode ;
355- nextParent . AddChild ( newNode ) ;
356- _hashedNodes . Add ( newNode . ToString ( ) , newNode ) ;
357- return newNode ;
341+ if ( userDir == null )
342+ throw new ( "Something went wrong opening the file explorer!" ) ;
343+
344+ if ( _hashedNodes . TryGetValue ( userDir . ToString ( ) , out var node ) )
345+ return ( FolderNode ) node ;
346+ var parent = FindStartFolder ( userDir . Parent ) ;
347+ LoadFolder ( parent ) ;
348+ if ( _hashedNodes . TryGetValue ( userDir . ToString ( ) , out var newNode ) )
349+ return ( FolderNode ) newNode ;
350+
351+ throw new ( "Something went wrong opening the file explorer!" ) ;
358352 }
359353
360354 #endregion
0 commit comments