@@ -348,6 +348,8 @@ private static FileBrowser Instance
348348 private int currentPathIndex = - 1 ;
349349 private readonly List < string > pathsFollowed = new List < string > ( ) ;
350350
351+ private HashSet < char > invalidFilenameChars ;
352+
351353 private bool canvasDimensionsChanged ;
352354
353355 // Required in RefreshFiles() function
@@ -580,6 +582,12 @@ private void Awake()
580582 allFilesFilter = new Filter ( ALL_FILES_FILTER_TEXT ) ;
581583 filters . Add ( allFilesFilter ) ;
582584
585+ invalidFilenameChars = new HashSet < char > ( Path . GetInvalidFileNameChars ( ) )
586+ {
587+ Path . DirectorySeparatorChar ,
588+ Path . AltDirectorySeparatorChar
589+ } ;
590+
583591 window . Initialize ( this ) ;
584592 listView . SetAdapter ( this ) ;
585593
@@ -924,6 +932,13 @@ public void OnSubmitButtonClicked()
924932 if ( filenameLength == 0 )
925933 continue ;
926934
935+ if ( ! VerifyFilenameInput ( filenameInput , startIndex , filenameLength ) )
936+ {
937+ // Filename contains invalid characters or is completely whitespace
938+ filenameImage . color = wrongFilenameColor ;
939+ return ;
940+ }
941+
927942 if ( m_acceptNonExistingFilename )
928943 fileCount ++ ;
929944 else
@@ -1000,7 +1015,16 @@ public void OnSubmitButtonClicked()
10001015 else
10011016#endif
10021017 {
1003- result [ fileCount ++ ] = Path . Combine ( m_currentPath , filename ) ;
1018+ try
1019+ {
1020+ result [ fileCount ++ ] = Path . Combine ( m_currentPath , filename ) ;
1021+ }
1022+ catch ( ArgumentException e )
1023+ {
1024+ filenameImage . color = wrongFilenameColor ;
1025+ Debug . LogException ( e ) ;
1026+ return ;
1027+ }
10041028 }
10051029 }
10061030
@@ -1894,26 +1918,41 @@ private int FilenameInputToFileEntryIndex( string input, int startIndex, int len
18941918 {
18951919 for ( int i = 0 ; i < validFileEntries . Count ; i ++ )
18961920 {
1897- if ( validFileEntries [ i ] . Name . Length == length && input . IndexOf ( validFileEntries [ i ] . Name ) == startIndex )
1921+ if ( validFileEntries [ i ] . Name . Length == length && input . IndexOf ( validFileEntries [ i ] . Name , startIndex , length ) == startIndex )
18981922 return i ;
18991923 }
19001924
19011925 return - 1 ;
19021926 }
19031927
1904- // Credit: http://answers.unity3d.com/questions/898770/how-to-get-the-width-of-ui-text-with-horizontal-ov.html
1905- private int CalculateLengthOfDropdownText ( string str )
1928+ // Verifies that filename doesn't contain any invalid characters
1929+ private bool VerifyFilenameInput ( string input , int startIndex , int length )
19061930 {
1907- int totalLength = 0 ;
1931+ bool isWhitespace = true ;
1932+ for ( int i = startIndex , endIndex = startIndex + length ; i < endIndex ; i ++ )
1933+ {
1934+ char ch = input [ i ] ;
1935+ if ( invalidFilenameChars . Contains ( ch ) )
1936+ return false ;
19081937
1909- Font myFont = filterItemTemplate . font ;
1910- CharacterInfo characterInfo = new CharacterInfo ( ) ;
1938+ if ( isWhitespace && ! char . IsWhiteSpace ( ch ) )
1939+ isWhitespace = false ;
1940+ }
19111941
1912- myFont . RequestCharactersInTexture ( str , filterItemTemplate . fontSize , filterItemTemplate . fontStyle ) ;
1942+ return ! isWhitespace ;
1943+ }
19131944
1945+ // Credit: http://answers.unity3d.com/questions/898770/how-to-get-the-width-of-ui-text-with-horizontal-ov.html
1946+ private int CalculateLengthOfDropdownText ( string str )
1947+ {
1948+ Font font = filterItemTemplate . font ;
1949+ font . RequestCharactersInTexture ( str , filterItemTemplate . fontSize , filterItemTemplate . fontStyle ) ;
1950+
1951+ int totalLength = 0 ;
19141952 for ( int i = 0 ; i < str . Length ; i ++ )
19151953 {
1916- if ( ! myFont . GetCharacterInfo ( str [ i ] , out characterInfo , filterItemTemplate . fontSize ) )
1954+ CharacterInfo characterInfo ;
1955+ if ( ! font . GetCharacterInfo ( str [ i ] , out characterInfo , filterItemTemplate . fontSize ) )
19171956 totalLength += 5 ;
19181957
19191958 totalLength += characterInfo . advance ;
0 commit comments