Skip to content

Commit

Permalink
Merge pull request #2355 from srthMD/place-filter
Browse files Browse the repository at this point in the history
Fix fast flag editor showing an error when trying to add a fast flag with _PlaceFilter or _DataCenterFilter
  • Loading branch information
pizzaboxer committed Jul 29, 2024
2 parents ed5457d + 6275085 commit 6bc1415
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 5 deletions.
10 changes: 9 additions & 1 deletion Bloxstrap/Resources/Strings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Bloxstrap/Resources/Strings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -1021,4 +1021,7 @@ Selecting 'No' will ignore this warning and continue installation.</value>
<data name="Common.SystemDefault" xml:space="preserve">
<value>System default</value>
</data>
<data name="Menu.FastFlagEditor.InvalidPlaceFilter" xml:space="preserve">
<value>The entry for '{0}' is not valid as the place filter is not formatted correctly.</value>
</data>
</root>
29 changes: 25 additions & 4 deletions Bloxstrap/UI/Elements/Menu/Pages/FastFlagEditorPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,15 @@ public partial class FastFlagEditorPage
private readonly ObservableCollection<FastFlag> _fastFlagList = new();
private readonly List<string> _validPrefixes = new()
{
"FFlag", "DFFlag", "SFFlag", "FInt", "DFInt", "FString", "DFString", "FLog", "DFlog"
"FFlag", "DFFlag", "SFFlag", "FInt", "DFInt", "FString", "DFString", "FLog", "DFLog"
};

// values must match the entire string to avoid cases where half the string
// matches but the filter would still be invalid
private readonly Regex _boolFilterPattern = new("(?:true|false)(;[\\d]{1,})+$");
private readonly Regex _intFilterPattern = new("([\\d]{1,})?(;[\\d]{1,})+$");
private readonly Regex _stringFilterPattern = new("^[^;]*(;[\\d]{1,})+$");

private bool _showPresets = false;
private string _searchFilter = "";

Expand Down Expand Up @@ -253,24 +259,39 @@ private bool ValidateFlagEntry(string name, string value)
string lowerValue = value.ToLowerInvariant();
string errorMessage = "";

if (!_validPrefixes.Where(x => name.StartsWith(x)).Any())
if (!_validPrefixes.Any(name.StartsWith))
errorMessage = Bloxstrap.Resources.Strings.Menu_FastFlagEditor_InvalidPrefix;
else if (!name.All(x => char.IsLetterOrDigit(x) || x == '_'))
errorMessage = Bloxstrap.Resources.Strings.Menu_FastFlagEditor_InvalidCharacter;

if (name.EndsWith("_PlaceFilter") || name.EndsWith("_DataCenterFilter"))
errorMessage = !ValidateFilter(name, value) ? Bloxstrap.Resources.Strings.Menu_FastFlagEditor_InvalidPlaceFilter : "";
else if ((name.StartsWith("FInt") || name.StartsWith("DFInt")) && !Int32.TryParse(value, out _))
errorMessage = Bloxstrap.Resources.Strings.Menu_FastFlagEditor_InvalidNumberValue;
else if ((name.StartsWith("FFlag") || name.StartsWith("DFFlag")) && lowerValue != "true" && lowerValue != "false")
errorMessage = Bloxstrap.Resources.Strings.Menu_FastFlagEditor_InvalidBoolValue;

if (!String.IsNullOrEmpty(errorMessage))
{
{
Frontend.ShowMessageBox(String.Format(errorMessage, name), MessageBoxImage.Error);
return false;
}

return true;
}

private bool ValidateFilter(string name, string value)
{
if(name.StartsWith("FFlag") || name.StartsWith("DFFlag"))
return _boolFilterPattern.IsMatch(value);
if (name.StartsWith("FInt") || name.StartsWith("DFInt"))
return _intFilterPattern.IsMatch(value);
if (name.StartsWith("FString") || name.StartsWith("DFString") || name.StartsWith("FLog") || name.StartsWith("DFLog"))
return _stringFilterPattern.IsMatch(value);

return false;
}

// refresh list on page load to synchronize with preset page
private void Page_Loaded(object sender, RoutedEventArgs e) => ReloadList();

Expand Down

0 comments on commit 6bc1415

Please sign in to comment.