Skip to content

Commit

Permalink
Merge pull request #1110 from morganwillcock/validate-game-name
Browse files Browse the repository at this point in the history
Editor: Add some validation tools and validate game filename
  • Loading branch information
ivan-mogilko authored Nov 27, 2020
2 parents ab76940 + 4a957f9 commit a52e537
Show file tree
Hide file tree
Showing 6 changed files with 135 additions and 63 deletions.
1 change: 1 addition & 0 deletions Editor/AGS.Editor/AGSEditor.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,7 @@
<Compile Include="GUI\ProjectTree.cs" />
<Compile Include="GUI\ProjectTreeItem.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Utils\Validation.cs" />
<EmbeddedResource Include="GUI\frmMain.resx">
<SubType>Designer</SubType>
<DependentUpon>frmMain.cs</DependentUpon>
Expand Down
3 changes: 2 additions & 1 deletion Editor/AGS.Editor/Components/ScriptsComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Text;
using System.Windows.Forms;
using System.Xml;
using AGS.Editor.Utils;
using AGS.Types;
using WeifenLuo.WinFormsUI.Docking;

Expand Down Expand Up @@ -523,7 +524,7 @@ private void ScriptRenamed(string oldScriptName, ProjectTreeItem renamedItem)
return;
}

if (!Utilities.DoesFileNameContainOnlyValidCharacters(renamedScript.FileName))
if (!Validation.FilenameIsValid(renamedScript.FileName))
{
_guiController.ShowMessage("The file name '" + renamedScript.FileName + "' contains some invalid characters. You cannot use some characters like : and / in script file names.", MessageBoxIcon.Warning);
renamedScript.FileName = oldScriptName;
Expand Down
101 changes: 55 additions & 46 deletions Editor/AGS.Editor/GUI/StartNewGameWizardPage2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.IO;
using System.Text;
using System.Windows.Forms;
using AGS.Editor.Utils;

namespace AGS.Editor
{
Expand Down Expand Up @@ -40,56 +41,64 @@ public string GetFullPath()
}
}

private bool VerifyPathContainsValidCharacters()
{
foreach (char c in GetFullPath())
{
foreach (char invalidChar in Path.GetInvalidPathChars())
{
if (invalidChar == c)
{
return false;
}
}
}
if (!txtCreateInFolder.Text.Contains(@"\"))
{
// Path must contain at least one \
return false;
}
if (!txtCreateInFolder.Text.Contains(":"))
{
// Path must contain at least one :
return false;
}
return true;
}

public override bool NextButtonPressed()
{
if ((txtFileName.Text.Length > 0) &&
(txtFriendlyName.Text.Length > 0) &&
(txtCreateInFolder.Text.Length > 0))
if (txtFriendlyName.TextLength == 0)
{
Factory.GUIController.ShowMessage("You must enter a name for your game", MessageBoxIcon.Warning);
return false;
}

if (txtFileName.TextLength == 0)
{
Factory.GUIController.ShowMessage("You must enter a file name for your game", MessageBoxIcon.Warning);
return false;
}

if (txtCreateInFolder.TextLength == 0)
{
Factory.GUIController.ShowMessage("You must choose a directory where the game project will be created", MessageBoxIcon.Warning);
return false;
}

if (!Validation.FilenameIsValid(txtFileName.Text) ||
!Validation.StringIsAsciiCharactersOnly(txtFileName.Text))
{
Factory.GUIController.ShowMessage("The file name you have specified includes some invalid characters. Please use just letters and numbers", MessageBoxIcon.Warning);
return false;
}

if (!Validation.PathIsValid(txtCreateInFolder.Text))
{
Factory.GUIController.ShowMessage("The path to the directory you have specified includes some invalid characters", MessageBoxIcon.Warning);
return false;
}

string fullpath;
try
{
fullpath = Path.Combine(txtCreateInFolder.Text, txtFileName.Text);
}
catch
{
if (!Utilities.DoesFileNameContainOnlyValidCharacters(txtFileName.Text))
{
Factory.GUIController.ShowMessage("The file name you have specified includes some invalid characters. Please use just letters and numbers.", MessageBoxIcon.Warning);
return false;
}
if (!VerifyPathContainsValidCharacters())
{
Factory.GUIController.ShowMessage("The folder name you have specified includes some invalid characters. Please use just letters and numbers.", MessageBoxIcon.Warning);
return false;
}
if (Directory.Exists(GetFullPath()))
{
Factory.GUIController.ShowMessage("The directory '" + GetFullPath() + "', already exists. Please choose another file name.", MessageBoxIcon.Warning);
return false;
}
return true;
Factory.GUIController.ShowMessage("Error combining full project path" , MessageBoxIcon.Error);
return false;
}
Factory.GUIController.ShowMessage("You must type in a file name and a game name to continue.", MessageBoxIcon.Information);
return false;

if (!Validation.PathIsAbsolute(fullpath) ||
!Validation.PathIsAbsoluteDriveLetter(fullpath))
{
Factory.GUIController.ShowMessage("The project directory must be an absolute path that starts with a drive letter", MessageBoxIcon.Warning);
return false;
}

if (!Validation.PathIsAvailable(fullpath))
{
Factory.GUIController.ShowMessage("The chosen project directory already exists", MessageBoxIcon.Warning);
return false;
}

return true;
}

public override string TitleText
Expand Down
15 changes: 0 additions & 15 deletions Editor/AGS.Editor/Utils/Utilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -223,21 +223,6 @@ public static void CopyFileAndSetDestinationWritable(string sourceFileName, stri
File.SetAttributes(destFileName, FileAttributes.Archive);
}

public static bool DoesFileNameContainOnlyValidCharacters(string fileName)
{
foreach (char c in fileName)
{
foreach (char invalidChar in Path.GetInvalidFileNameChars())
{
if (invalidChar == c)
{
return false;
}
}
}
return true;
}

public static void CopyFont(int fromSlot, int toSlot)
{
if (fromSlot == toSlot)
Expand Down
57 changes: 57 additions & 0 deletions Editor/AGS.Editor/Utils/Validation.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using System;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;

namespace AGS.Editor.Utils
{
public class Validation
{
public static bool FilenameIsValid(string filename)
{
return !(filename.IndexOfAny(Path.GetInvalidFileNameChars()) >= 0);
}

public static bool PathIsAvailable(string path)
{
if (File.Exists(path))
return false;

if (Directory.Exists(path))
return false;

return true;
}

public static bool PathIsAbsoluteDriveLetter(string path)
{
return Regex.IsMatch(path, @"^[a-zA-Z]:\\");
}

public static bool PathIsAbsolute(string path)
{
bool rooted = false;

try
{
rooted = Path.IsPathRooted(path);
}
catch (ArgumentException)
{
// pass
}

return rooted;
}

public static bool PathIsValid(string path)
{
return !(path.IndexOfAny(Path.GetInvalidPathChars()) >= 0);
}

public static bool StringIsAsciiCharactersOnly(string fileName)
{
return !fileName.Any(c => c > 127);
}
}
}
21 changes: 20 additions & 1 deletion Editor/AGS.Types/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Xml;
Expand Down Expand Up @@ -159,7 +160,25 @@ public void GenerateNewGameID()
public string GameFileName
{
get { return _gameFileName; }
set { _gameFileName = value; }
set
{
if (string.IsNullOrEmpty(value))
{
throw new ArgumentException("Game file name cannot be empty");
}

if (value.IndexOfAny(Path.GetInvalidFileNameChars()) >= 0)
{
throw new ArgumentException("Game file name contains invalid characters");
}

if (value.Any(c => c > 127))
{
throw new ArgumentException("Game file name should only contain letters and numbers");
}

_gameFileName = value;
}
}

[DisplayName(PROPERTY_GAME_NAME)]
Expand Down

0 comments on commit a52e537

Please sign in to comment.