Skip to content

Commit

Permalink
File browser no longer throws ArgumentException when entered filename…
Browse files Browse the repository at this point in the history
… contains invalid characters; filename input field turns red instead
  • Loading branch information
yasirkula committed Nov 22, 2020
1 parent 52ecce6 commit 0abb61e
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 10 deletions.
57 changes: 48 additions & 9 deletions Plugins/SimpleFileBrowser/Scripts/FileBrowser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,8 @@ private static FileBrowser Instance
private int currentPathIndex = -1;
private readonly List<string> pathsFollowed = new List<string>();

private HashSet<char> invalidFilenameChars;

private bool canvasDimensionsChanged;

// Required in RefreshFiles() function
Expand Down Expand Up @@ -580,6 +582,12 @@ private void Awake()
allFilesFilter = new Filter( ALL_FILES_FILTER_TEXT );
filters.Add( allFilesFilter );

invalidFilenameChars = new HashSet<char>( Path.GetInvalidFileNameChars() )
{
Path.DirectorySeparatorChar,
Path.AltDirectorySeparatorChar
};

window.Initialize( this );
listView.SetAdapter( this );

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
}
}

Expand Down Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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."
}

0 comments on commit 0abb61e

Please sign in to comment.