@@ -348,6 +348,8 @@ private static FileBrowser Instance
348
348
private int currentPathIndex = - 1 ;
349
349
private readonly List < string > pathsFollowed = new List < string > ( ) ;
350
350
351
+ private HashSet < char > invalidFilenameChars ;
352
+
351
353
private bool canvasDimensionsChanged ;
352
354
353
355
// Required in RefreshFiles() function
@@ -580,6 +582,12 @@ private void Awake()
580
582
allFilesFilter = new Filter ( ALL_FILES_FILTER_TEXT ) ;
581
583
filters . Add ( allFilesFilter ) ;
582
584
585
+ invalidFilenameChars = new HashSet < char > ( Path . GetInvalidFileNameChars ( ) )
586
+ {
587
+ Path . DirectorySeparatorChar ,
588
+ Path . AltDirectorySeparatorChar
589
+ } ;
590
+
583
591
window . Initialize ( this ) ;
584
592
listView . SetAdapter ( this ) ;
585
593
@@ -924,6 +932,13 @@ public void OnSubmitButtonClicked()
924
932
if ( filenameLength == 0 )
925
933
continue ;
926
934
935
+ if ( ! VerifyFilenameInput ( filenameInput , startIndex , filenameLength ) )
936
+ {
937
+ // Filename contains invalid characters or is completely whitespace
938
+ filenameImage . color = wrongFilenameColor ;
939
+ return ;
940
+ }
941
+
927
942
if ( m_acceptNonExistingFilename )
928
943
fileCount ++ ;
929
944
else
@@ -1000,7 +1015,16 @@ public void OnSubmitButtonClicked()
1000
1015
else
1001
1016
#endif
1002
1017
{
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
+ }
1004
1028
}
1005
1029
}
1006
1030
@@ -1894,26 +1918,41 @@ private int FilenameInputToFileEntryIndex( string input, int startIndex, int len
1894
1918
{
1895
1919
for ( int i = 0 ; i < validFileEntries . Count ; i ++ )
1896
1920
{
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 )
1898
1922
return i ;
1899
1923
}
1900
1924
1901
1925
return - 1 ;
1902
1926
}
1903
1927
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 )
1906
1930
{
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 ;
1908
1937
1909
- Font myFont = filterItemTemplate . font ;
1910
- CharacterInfo characterInfo = new CharacterInfo ( ) ;
1938
+ if ( isWhitespace && ! char . IsWhiteSpace ( ch ) )
1939
+ isWhitespace = false ;
1940
+ }
1911
1941
1912
- myFont . RequestCharactersInTexture ( str , filterItemTemplate . fontSize , filterItemTemplate . fontStyle ) ;
1942
+ return ! isWhitespace ;
1943
+ }
1913
1944
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 ;
1914
1952
for ( int i = 0 ; i < str . Length ; i ++ )
1915
1953
{
1916
- if ( ! myFont . GetCharacterInfo ( str [ i ] , out characterInfo , filterItemTemplate . fontSize ) )
1954
+ CharacterInfo characterInfo ;
1955
+ if ( ! font . GetCharacterInfo ( str [ i ] , out characterInfo , filterItemTemplate . fontSize ) )
1917
1956
totalLength += 5 ;
1918
1957
1919
1958
totalLength += characterInfo . advance ;
0 commit comments