Skip to content

Commit

Permalink
More JSON autocorrecting (#2733)
Browse files Browse the repository at this point in the history
  • Loading branch information
pizzaboxer committed Aug 27, 2024
1 parent 9a412ea commit cf45d9c
Showing 1 changed file with 19 additions and 12 deletions.
31 changes: 19 additions & 12 deletions Bloxstrap/UI/Elements/Settings/Pages/FastFlagEditorPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ private void AddSingle(string name, string value)
}
else
{
Frontend.ShowMessageBox(Bloxstrap.Resources.Strings.Menu_FastFlagEditor_AlreadyExists, MessageBoxImage.Information);
Frontend.ShowMessageBox(Strings.Menu_FastFlagEditor_AlreadyExists, MessageBoxImage.Information);

bool refresh = false;

Expand Down Expand Up @@ -175,7 +175,14 @@ private void ImportJSON(string json)
json = '{' + json;

if (!json.EndsWith('}'))
json += '}';
{
int lastIndex = json.LastIndexOf('}');

if (lastIndex == -1)
json += '}';
else
json = json.Substring(0, lastIndex+1);
}

try
{
Expand All @@ -193,7 +200,7 @@ private void ImportJSON(string json)
catch (Exception ex)
{
Frontend.ShowMessageBox(
String.Format(Bloxstrap.Resources.Strings.Menu_FastFlagEditor_InvalidJSON, ex.Message),
String.Format(Strings.Menu_FastFlagEditor_InvalidJSON, ex.Message),
MessageBoxImage.Error
);

Expand All @@ -205,7 +212,7 @@ private void ImportJSON(string json)
if (list.Count > 16)
{
var result = Frontend.ShowMessageBox(
Bloxstrap.Resources.Strings.Menu_FastFlagEditor_LargeConfig,
Strings.Menu_FastFlagEditor_LargeConfig,
MessageBoxImage.Warning,
MessageBoxButton.YesNo
);
Expand All @@ -222,7 +229,7 @@ private void ImportJSON(string json)
int count = conflictingFlags.Count();

string message = String.Format(
Bloxstrap.Resources.Strings.Menu_FastFlagEditor_ConflictingImport,
Strings.Menu_FastFlagEditor_ConflictingImport,
count,
String.Join(", ", conflictingFlags.Take(25))
);
Expand Down Expand Up @@ -263,16 +270,16 @@ private bool ValidateFlagEntry(string name, string value)
string errorMessage = "";

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

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

if (!String.IsNullOrEmpty(errorMessage))
{
Expand Down Expand Up @@ -319,7 +326,7 @@ private void DataGrid_CellEditEnding(object sender, DataGridCellEditEndingEventA

if (App.FastFlags.GetValue(newName) is not null)
{
Frontend.ShowMessageBox(Bloxstrap.Resources.Strings.Menu_FastFlagEditor_AlreadyExists, MessageBoxImage.Information);
Frontend.ShowMessageBox(Strings.Menu_FastFlagEditor_AlreadyExists, MessageBoxImage.Information);
e.Cancel = true;
textbox.Text = oldName;
return;
Expand Down Expand Up @@ -387,7 +394,7 @@ private void ExportJSONButton_Click(object sender, RoutedEventArgs e)
{
string json = JsonSerializer.Serialize(App.FastFlags.Prop, new JsonSerializerOptions { WriteIndented = true });
Clipboard.SetDataObject(json);
Frontend.ShowMessageBox(Bloxstrap.Resources.Strings.Menu_FastFlagEditor_JsonCopiedToClipboard, MessageBoxImage.Information);
Frontend.ShowMessageBox(Strings.Menu_FastFlagEditor_JsonCopiedToClipboard, MessageBoxImage.Information);
}

private void SearchTextBox_TextChanged(object sender, TextChangedEventArgs e)
Expand Down

0 comments on commit cf45d9c

Please sign in to comment.