|
1 |
| -using UnityEngine; |
| 1 | +//#define WIN_DIR_CHECK_WITHOUT_TIMEOUT // When uncommented, Directory.Exists won't be wrapped inside a Task/Thread on Windows but we won't be able to set a timeout for unreachable directories/drives |
| 2 | + |
| 3 | +using UnityEngine; |
2 | 4 | using UnityEngine.EventSystems;
|
3 | 5 | using UnityEngine.UI;
|
4 | 6 | using System;
|
@@ -541,6 +543,10 @@ private static FileBrowser Instance
|
541 | 543 | #endif
|
542 | 544 | private int numberOfDriveQuickLinks;
|
543 | 545 |
|
| 546 | +#if !WIN_DIR_CHECK_WITHOUT_TIMEOUT && ( UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN ) |
| 547 | + private readonly List<string> timedOutDirectoryExistsRequests = new List<string>( 2 ); |
| 548 | +#endif |
| 549 | + |
544 | 550 | private bool canvasDimensionsChanged;
|
545 | 551 |
|
546 | 552 | private readonly CompareInfo textComparer = new CultureInfo( "en-US" ).CompareInfo;
|
@@ -1058,6 +1064,11 @@ private void RefreshDriveQuickLinks()
|
1058 | 1064 | bool drivesListHasntChanged = true;
|
1059 | 1065 | for( int i = 0; i < drives.Length; i++ )
|
1060 | 1066 | {
|
| 1067 | +#if !WIN_DIR_CHECK_WITHOUT_TIMEOUT && ( UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN ) |
| 1068 | + if( timedOutDirectoryExistsRequests.Contains( drives[i] ) ) |
| 1069 | + continue; |
| 1070 | +#endif |
| 1071 | + |
1061 | 1072 | if( drives[i] != driveQuickLinks[i] )
|
1062 | 1073 | {
|
1063 | 1074 | drivesListHasntChanged = false;
|
@@ -1175,10 +1186,10 @@ private void RefreshDriveQuickLinks()
|
1175 | 1186 | // Verify that current directory still exists
|
1176 | 1187 | try
|
1177 | 1188 | {
|
1178 |
| - if( !string.IsNullOrEmpty( m_currentPath ) && !Directory.Exists( m_currentPath ) ) |
| 1189 | + if( !string.IsNullOrEmpty( m_currentPath ) && !FileBrowserHelpers.DirectoryExists( m_currentPath ) ) |
1179 | 1190 | {
|
1180 | 1191 | string currentPathRoot = Path.GetPathRoot( m_currentPath );
|
1181 |
| - if( !string.IsNullOrEmpty( currentPathRoot ) && Directory.Exists( currentPathRoot ) ) |
| 1192 | + if( !string.IsNullOrEmpty( currentPathRoot ) && FileBrowserHelpers.DirectoryExists( currentPathRoot ) ) |
1182 | 1193 | CurrentPath = currentPathRoot;
|
1183 | 1194 | else if( allQuickLinks.Count > 0 )
|
1184 | 1195 | CurrentPath = allQuickLinks[0].TargetPath;
|
@@ -2220,7 +2231,11 @@ private bool AddQuickLink( Sprite icon, string name, string path )
|
2220 | 2231 | if( !FileBrowserHelpers.ShouldUseSAFForPath( path ) )
|
2221 | 2232 | #endif
|
2222 | 2233 | {
|
| 2234 | +#if !WIN_DIR_CHECK_WITHOUT_TIMEOUT && ( UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN ) |
| 2235 | + if( !CheckDirectoryExistsWithTimeout( path ) ) |
| 2236 | +#else |
2223 | 2237 | if( !Directory.Exists( path ) )
|
| 2238 | +#endif |
2224 | 2239 | return false;
|
2225 | 2240 |
|
2226 | 2241 | path = GetPathWithoutTrailingDirectorySeparator( path.Trim() );
|
@@ -2623,6 +2638,42 @@ private string GetInitialPath( string initialPath )
|
2623 | 2638 |
|
2624 | 2639 | return initialPath;
|
2625 | 2640 | }
|
| 2641 | + |
| 2642 | +#if !WIN_DIR_CHECK_WITHOUT_TIMEOUT && ( UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN ) |
| 2643 | + private bool CheckDirectoryExistsWithTimeout( string path, int timeout = 750 ) |
| 2644 | + { |
| 2645 | + if( timedOutDirectoryExistsRequests.Contains( path ) ) |
| 2646 | + return false; |
| 2647 | + |
| 2648 | + // Directory.Exists freezes for ~15 seconds for unreachable network drives on Windows, set a timeout using threads |
| 2649 | + bool directoryExists = false; |
| 2650 | + try |
| 2651 | + { |
| 2652 | +#if NET_STANDARD_2_0 || NET_4_6 |
| 2653 | + // Credit: https://stackoverflow.com/a/52661569/2373034 |
| 2654 | + System.Threading.Tasks.Task task = new System.Threading.Tasks.Task( () => directoryExists = Directory.Exists( path ) ); |
| 2655 | + task.Start(); |
| 2656 | + if( !task.Wait( timeout ) ) |
| 2657 | + timedOutDirectoryExistsRequests.Add( path ); |
| 2658 | +#else |
| 2659 | + // Credit: https://stackoverflow.com/q/1232953/2373034 |
| 2660 | + System.Threading.Thread thread = new System.Threading.Thread( new System.Threading.ThreadStart( () => directoryExists = Directory.Exists( path ) ) ); |
| 2661 | + thread.Start(); |
| 2662 | + if( !thread.Join( timeout ) ) |
| 2663 | + { |
| 2664 | + timedOutDirectoryExistsRequests.Add( path ); |
| 2665 | + thread.Abort(); |
| 2666 | + } |
| 2667 | +#endif |
| 2668 | + } |
| 2669 | + catch |
| 2670 | + { |
| 2671 | + directoryExists = Directory.Exists( path ); |
| 2672 | + } |
| 2673 | + |
| 2674 | + return directoryExists; |
| 2675 | + } |
| 2676 | +#endif |
2626 | 2677 | #endregion
|
2627 | 2678 |
|
2628 | 2679 | #region File Browser Functions (static)
|
|
0 commit comments