From 0abb61ebeea90b0ece5cdab32a24d85a3d78c0ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=BCleyman=20Yasir=20KULA?= Date: Sun, 22 Nov 2020 13:20:26 +0300 Subject: [PATCH] File browser no longer throws ArgumentException when entered filename contains invalid characters; filename input field turns red instead --- .../SimpleFileBrowser/Scripts/FileBrowser.cs | 57 ++++++++++++++++--- package.json | 2 +- 2 files changed, 49 insertions(+), 10 deletions(-) diff --git a/Plugins/SimpleFileBrowser/Scripts/FileBrowser.cs b/Plugins/SimpleFileBrowser/Scripts/FileBrowser.cs index e90003a..58ecbaa 100644 --- a/Plugins/SimpleFileBrowser/Scripts/FileBrowser.cs +++ b/Plugins/SimpleFileBrowser/Scripts/FileBrowser.cs @@ -348,6 +348,8 @@ private static FileBrowser Instance private int currentPathIndex = -1; private readonly List pathsFollowed = new List(); + private HashSet invalidFilenameChars; + private bool canvasDimensionsChanged; // Required in RefreshFiles() function @@ -580,6 +582,12 @@ private void Awake() allFilesFilter = new Filter( ALL_FILES_FILTER_TEXT ); filters.Add( allFilesFilter ); + invalidFilenameChars = new HashSet( Path.GetInvalidFileNameChars() ) + { + Path.DirectorySeparatorChar, + Path.AltDirectorySeparatorChar + }; + window.Initialize( this ); listView.SetAdapter( this ); @@ -924,6 +932,13 @@ public void OnSubmitButtonClicked() if( filenameLength == 0 ) continue; + if( !VerifyFilenameInput( filenameInput, startIndex, filenameLength ) ) + { + // Filename contains invalid characters or is completely whitespace + filenameImage.color = wrongFilenameColor; + return; + } + if( m_acceptNonExistingFilename ) fileCount++; else @@ -1000,7 +1015,16 @@ public void OnSubmitButtonClicked() else #endif { - result[fileCount++] = Path.Combine( m_currentPath, filename ); + try + { + result[fileCount++] = Path.Combine( m_currentPath, filename ); + } + catch( ArgumentException e ) + { + filenameImage.color = wrongFilenameColor; + Debug.LogException( e ); + return; + } } } @@ -1894,26 +1918,41 @@ private int FilenameInputToFileEntryIndex( string input, int startIndex, int len { for( int i = 0; i < validFileEntries.Count; i++ ) { - if( validFileEntries[i].Name.Length == length && input.IndexOf( validFileEntries[i].Name ) == startIndex ) + if( validFileEntries[i].Name.Length == length && input.IndexOf( validFileEntries[i].Name, startIndex, length ) == startIndex ) return i; } return -1; } - // Credit: http://answers.unity3d.com/questions/898770/how-to-get-the-width-of-ui-text-with-horizontal-ov.html - private int CalculateLengthOfDropdownText( string str ) + // Verifies that filename doesn't contain any invalid characters + private bool VerifyFilenameInput( string input, int startIndex, int length ) { - int totalLength = 0; + bool isWhitespace = true; + for( int i = startIndex, endIndex = startIndex + length; i < endIndex; i++ ) + { + char ch = input[i]; + if( invalidFilenameChars.Contains( ch ) ) + return false; - Font myFont = filterItemTemplate.font; - CharacterInfo characterInfo = new CharacterInfo(); + if( isWhitespace && !char.IsWhiteSpace( ch ) ) + isWhitespace = false; + } - myFont.RequestCharactersInTexture( str, filterItemTemplate.fontSize, filterItemTemplate.fontStyle ); + return !isWhitespace; + } + // Credit: http://answers.unity3d.com/questions/898770/how-to-get-the-width-of-ui-text-with-horizontal-ov.html + private int CalculateLengthOfDropdownText( string str ) + { + Font font = filterItemTemplate.font; + font.RequestCharactersInTexture( str, filterItemTemplate.fontSize, filterItemTemplate.fontStyle ); + + int totalLength = 0; for( int i = 0; i < str.Length; i++ ) { - if( !myFont.GetCharacterInfo( str[i], out characterInfo, filterItemTemplate.fontSize ) ) + CharacterInfo characterInfo; + if( !font.GetCharacterInfo( str[i], out characterInfo, filterItemTemplate.fontSize ) ) totalLength += 5; totalLength += characterInfo.advance; diff --git a/package.json b/package.json index 8f863ab..5b2f390 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "com.yasirkula.simplefilebrowser", "displayName": "Simple File Browser", - "version": "1.3.6", + "version": "1.3.7", "description": "This plugin helps you show save/load dialogs during gameplay with its uGUI based file browser." } \ No newline at end of file