Revise flag editor validation

i didn't implement it fully properly last time
This commit is contained in:
pizzaboxer 2024-07-02 15:58:56 +04:00
parent 25866849c0
commit 5624e043b5
No known key found for this signature in database
GPG Key ID: 59D4A1DBAD0F2BA8
3 changed files with 75 additions and 35 deletions

View File

@ -1843,7 +1843,16 @@ namespace Bloxstrap.Resources {
}
/// <summary>
/// Looks up a localized string similar to The name of this flag is not valid as names can only contain letters, numbers, and underscores.
/// Looks up a localized string similar to The entry for &apos;{0}&apos; is not valid as the value must be a boolean (either &apos;True&apos; or &apos;False&apos;).
/// </summary>
public static string Menu_FastFlagEditor_InvalidBoolValue {
get {
return ResourceManager.GetString("Menu.FastFlagEditor.InvalidBoolValue", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to The entry for &apos;{0}&apos; is not valid as the name can only contain letters, numbers, and underscores.
/// </summary>
public static string Menu_FastFlagEditor_InvalidCharacter {
get {
@ -1864,7 +1873,16 @@ namespace Bloxstrap.Resources {
}
/// <summary>
/// Looks up a localized string similar to The name of this flag is not valid as names must start with FFlag, DFInt, etc.
/// Looks up a localized string similar to The entry for &apos;{0}&apos; is not valid as the value must be a number.
/// </summary>
public static string Menu_FastFlagEditor_InvalidNumberValue {
get {
return ResourceManager.GetString("Menu.FastFlagEditor.InvalidNumberValue", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to The entry for &apos;{0}&apos; is not valid as the name must start with FFlag, DFInt, etc.
/// </summary>
public static string Menu_FastFlagEditor_InvalidPrefix {
get {

View File

@ -673,7 +673,7 @@ There are {0} conflicting flag definitions:
<value>Export JSON</value>
</data>
<data name="Menu.FastFlagEditor.InvalidCharacter" xml:space="preserve">
<value>The name of this flag is not valid as names can only contain letters, numbers, and underscores</value>
<value>The entry for '{0}' is not valid as the name can only contain letters, numbers, and underscores</value>
</data>
<data name="Menu.FastFlagEditor.InvalidJSON" xml:space="preserve">
<value>The JSON you've entered does not appear to be valid. Please double check it and try again.
@ -682,7 +682,7 @@ More information:
{0}</value>
</data>
<data name="Menu.FastFlagEditor.InvalidPrefix" xml:space="preserve">
<value>The name of this flag is not valid as names must start with FFlag, DFInt, etc</value>
<value>The entry for '{0}' is not valid as the name must start with FFlag, DFInt, etc</value>
</data>
<data name="Menu.FastFlagEditor.JsonCopiedToClipboard" xml:space="preserve">
<value>Copied to clipboard.</value>
@ -1072,4 +1072,11 @@ Selecting 'No' will ignore this warning and continue installation.</value>
<data name="FileTypes.JSONFiles" xml:space="preserve">
<value>JSON files</value>
</data>
<data name="Menu.FastFlagEditor.InvalidBoolValue" xml:space="preserve">
<value>The entry for '{0}' is not valid as the value must be a boolean (either 'True' or 'False')</value>
<comment>Do not translate 'True' and 'False', those must stay in English</comment>
</data>
<data name="Menu.FastFlagEditor.InvalidNumberValue" xml:space="preserve">
<value>The entry for '{0}' is not valid as the value must be a number</value>
</data>
</root>

View File

@ -6,6 +6,8 @@ using System.Collections.ObjectModel;
using Wpf.Ui.Mvvm.Contracts;
using Bloxstrap.UI.Elements.Dialogs;
using Newtonsoft.Json.Linq;
using System.Xml.Linq;
namespace Bloxstrap.UI.Elements.Menu.Pages
{
@ -96,27 +98,19 @@ namespace Bloxstrap.UI.Elements.Menu.Pages
return;
if (dialog.Tabs.SelectedIndex == 0)
AddEntry(dialog.FlagNameTextBox.Text, dialog.FlagValueTextBox.Text);
AddSingle(dialog.FlagNameTextBox.Text, dialog.FlagValueTextBox.Text);
else if (dialog.Tabs.SelectedIndex == 1)
ImportJSON(dialog.JsonTextBox.Text);
}
private void AddEntry(string name, string value)
private void AddSingle(string name, string value)
{
FastFlag? entry;
if (App.FastFlags.GetValue(name) is null)
{
if (!_validPrefixes.Where(x => name.StartsWith(x)).Any())
if (!ValidateFlagEntry(name, value))
{
Frontend.ShowMessageBox(Bloxstrap.Resources.Strings.Menu_FastFlagEditor_InvalidPrefix, MessageBoxImage.Error);
ShowAddDialog();
return;
}
if (!name.All(x => char.IsLetterOrDigit(x) || x == '_'))
{
Frontend.ShowMessageBox(Bloxstrap.Resources.Strings.Menu_FastFlagEditor_InvalidCharacter, MessageBoxImage.Error);
ShowAddDialog();
return;
}
@ -240,12 +234,38 @@ namespace Bloxstrap.UI.Elements.Menu.Pages
if (App.FastFlags.Prop.ContainsKey(pair.Key) && !overwriteConflicting)
continue;
if (!ValidateFlagEntry(pair.Key, (string)pair.Value))
continue;
App.FastFlags.SetValue(pair.Key, pair.Value);
}
ClearSearch();
}
private bool ValidateFlagEntry(string name, string value)
{
string lowerValue = value.ToLowerInvariant();
string errorMessage = "";
if (!_validPrefixes.Where(x => name.StartsWith(x)).Any())
errorMessage = Bloxstrap.Resources.Strings.Menu_FastFlagEditor_InvalidPrefix;
else if (!name.All(x => char.IsLetterOrDigit(x) || x == '_'))
errorMessage = Bloxstrap.Resources.Strings.Menu_FastFlagEditor_InvalidCharacter;
else if (name[..6].Contains("FFlag") && lowerValue != "true" && lowerValue != "false")
errorMessage = Bloxstrap.Resources.Strings.Menu_FastFlagEditor_InvalidBoolValue;
else if (name[..5].Contains("FInt") && !Int32.TryParse(value, out _))
errorMessage = Bloxstrap.Resources.Strings.Menu_FastFlagEditor_InvalidNumberValue;
if (!String.IsNullOrEmpty(errorMessage))
{
Frontend.ShowMessageBox(String.Format(errorMessage, name), MessageBoxImage.Error);
return false;
}
return true;
}
// refresh list on page load to synchronize with preset page
private void Page_Loaded(object sender, RoutedEventArgs e) => ReloadList();
@ -254,29 +274,16 @@ namespace Bloxstrap.UI.Elements.Menu.Pages
int index = e.Row.GetIndex();
FastFlag entry = _fastFlagList[index];
switch (e.Column.Header)
{
/* case "Enabled":
bool enabled = (bool)((CheckBox)e.EditingElement).IsChecked!;
if (enabled)
{
App.FastFlags.SetValue(entry.Name, entry.Value);
App.FastFlags.SetValue($"Disable{entry.Name}", null);
}
else
{
App.FastFlags.SetValue(entry.Name, null);
App.FastFlags.SetValue($"Disable{entry.Name}", entry.Value);
}
break; */
case "Name":
var textbox = e.EditingElement as TextBox;
if (textbox is null)
return;
switch (e.Column.Header)
{
case "Name":
string oldName = entry.Name;
string newName = textbox!.Text;
string newName = textbox.Text;
if (newName == oldName)
return;
@ -300,7 +307,15 @@ namespace Bloxstrap.UI.Elements.Menu.Pages
break;
case "Value":
string newValue = ((TextBox)e.EditingElement).Text;
string oldValue = entry.Value;
string newValue = textbox.Text;
if (!ValidateFlagEntry(entry.Name, newValue))
{
e.Cancel = true;
textbox.Text = oldValue;
return;
}
App.FastFlags.SetValue(entry.Name, newValue);