Browse Source

Prevent user from loading levels with invalid names.

Signed-off-by: sphrose <[email protected]>
sphrose 2 years ago
parent
commit
f5b948fe14

+ 12 - 0
Code/Editor/LevelFileDialog.cpp

@@ -268,6 +268,12 @@ void CLevelFileDialog::OnTreeSelectionChanged()
     if (!indexes.isEmpty())
     {
         ui->nameLineEdit->setText(NameForIndex(indexes.first()));
+
+        if (m_bOpenDialog)
+        {
+            // Do not allow the user to open a level that haas illegal characters in the name.
+            ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(ui->nameLineEdit->hasAcceptableInput());
+        }
     }
 }
 
@@ -432,6 +438,12 @@ bool CLevelFileDialog::ValidateSaveLevelPath(QString& errorMessage) const
         return false;
     }
 
+    if (!ui->nameLineEdit->hasAcceptableInput())
+    {
+        errorMessage = tr("The level name contains illegal characters.");
+        return false;
+    }
+
     return true;
 }
 

+ 19 - 1
Code/Editor/WelcomeScreen/WelcomeScreenDialog.cpp

@@ -162,6 +162,24 @@ bool WelcomeScreenDialog::eventFilter(QObject *watched, QEvent *event)
     return QDialog::eventFilter(watched, event);
 }
 
+bool WelcomeScreenDialog::IsValidLevelName(const QString& path)
+{
+    QStringList pathParts = Path::SplitIntoSegments(path);
+
+    QString levelName = pathParts.at(pathParts.size() - 1);
+    if (!levelName.endsWith(".prefab", Qt::CaseInsensitive))
+    {
+        return true;
+    }
+
+    QString containerName = pathParts.at(pathParts.size() - 2);
+
+    QRegExpValidator validator(QRegExp("^[a-zA-Z0-9_\\-./]*$"));
+
+    int pos = 0;
+    return validator.validate(containerName, pos);
+}
+
 void WelcomeScreenDialog::SetRecentFileList(RecentFileList* pList)
 {
     if (!pList)
@@ -186,7 +204,7 @@ void WelcomeScreenDialog::SetRecentFileList(RecentFileList* pList)
      for (int i = 0; i < recentListSize; ++i)
     {
         const QString& recentFile = pList->m_arrNames[i];
-        if (recentFile.endsWith(m_levelExtension))
+        if (recentFile.endsWith(m_levelExtension) && IsValidLevelName(recentFile))
         {
             if (CFileUtil::Exists(recentFile, false))
             {

+ 1 - 0
Code/Editor/WelcomeScreen/WelcomeScreenDialog.h

@@ -51,6 +51,7 @@ private:
     const char* m_levelExtension = nullptr;
     bool m_messageScrollReported = false;
 
+    bool IsValidLevelName(const QString& path);
     void RemoveLevelEntry(int index);
 
     void OnShowToolTip(const QModelIndex& index);