Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Editor] ASCII check for mod export #345

Open
wants to merge 3 commits into
base: 1.0.6.4
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 19 additions & 13 deletions FrostyEditor/Windows/ModSettingsWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
xmlns:local="clr-namespace:FrostyEditor.Windows"
xmlns:ctrl="clr-namespace:Frosty.Controls;assembly=FrostyControls"
mc:Ignorable="d"
Title="Export Mod" Height="530" Width="600"
ResizeMode="NoResize" WindowStartupLocation="CenterScreen" SnapsToDevicePixels="True">
Title="Export Mod" MinHeight="530" Height="530" MinWidth="600" Width="600"
WindowStartupLocation="CenterScreen" SnapsToDevicePixels="True">

<Grid Background="{StaticResource WindowBackground}">
<Grid.RowDefinitions>
Expand All @@ -17,28 +17,35 @@

<Grid Background="{StaticResource ListBackground}">
<DockPanel LastChildFill="True">
<Grid Margin="5" DockPanel.Dock="Top">
<Label Content="Please fill out each of the following sections. Only the first section is mandatory." FontFamily="Global User Interface" HorizontalAlignment="Center" VerticalAlignment="Center" FontWeight="Bold" FontSize="14"/>
</Grid>
<StackPanel Margin="5" DockPanel.Dock="Top">
<Label HorizontalAlignment="Center" VerticalAlignment="Center">
<TextBlock Text="Please fill out each of the following sections. Only the first 4 fields is mandatory."
FontWeight="Bold" FontSize="14" FontFamily="Global User Interface" TextWrapping="WrapWithOverflow" Foreground="{StaticResource FontColor}"
HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Label>
<Label x:Name="ErrorMessageLabel" HorizontalAlignment="Center" VerticalAlignment="Center">
<TextBlock Text="Test" TextWrapping="WrapWithOverflow" HorizontalAlignment="Center" VerticalAlignment="Center" Foreground="Red"/>
</Label>
</StackPanel>

<Border DockPanel.Dock="Top" Background="{StaticResource ControlBackground}" Height="2"/>
<Border DockPanel.Dock="Top" Background="{StaticResource ControlBackground}" Height="2" HorizontalAlignment="Stretch"/>

<Grid DockPanel.Dock="Top" Margin="5">
<StackPanel>
<DockPanel LastChildFill="True">
<Label Content="Title " FontFamily="Global User Interface" Width="75"/>
<Label Content="Title" FontFamily="Global User Interface" Width="75"/>
<TextBox x:Name="modTitleTextBox" VerticalContentAlignment="Center" Text="Testing" Margin="1" BorderThickness="1"/>
</DockPanel>
<DockPanel LastChildFill="True">
<Label Content="Author " FontFamily="Global User Interface" Width="75"/>
<Label Content="Author" FontFamily="Global User Interface" Width="75"/>
<TextBox x:Name="modAuthorTextBox" VerticalContentAlignment="Center" Text="Testing" Margin="1" BorderThickness="1"/>
</DockPanel>
<DockPanel LastChildFill="True">
<Label Content="Version " FontFamily="Global User Interface" Width="75"/>
<Label Content="Version" FontFamily="Global User Interface" Width="75"/>
<TextBox Grid.Column="1" x:Name="modVersionTextBox" VerticalContentAlignment="Center" Text="Testing" Margin="1" BorderThickness="1"/>
</DockPanel>
<DockPanel LastChildFill="True">
<Label Content="Category " FontFamily="Global User Interface" Width="75"/>
<Label Content="Category" FontFamily="Global User Interface" Width="75"/>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="0.5*"/>
Expand All @@ -57,9 +64,8 @@
</Grid>

<Grid DockPanel.Dock="Top" Margin="5">
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
<ctrl:FrostyImageButton x:Name="iconImageButton" Title="ICON" OnValidate="FrostyImageButton_OnValidate" Margin="0,0,4,0"/>
<Grid Height="88" Width="88" Margin="0,0,16,0"/>
<ctrl:FrostyImageButton x:Name="iconImageButton" HorizontalAlignment="Left" Title="ICON" OnValidate="FrostyImageButton_OnValidate" Margin="16,0,0,0"/>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Right" Margin="0,0,16,0">
<ctrl:FrostyImageButton x:Name="ssImageButton1" Title="IMAGE" OnValidate="FrostyImageButton_OnValidate" Margin="0,0,4,0"/>
<ctrl:FrostyImageButton x:Name="ssImageButton2" Title="IMAGE" OnValidate="FrostyImageButton_OnValidate" Margin="0,0,4,0"/>
<ctrl:FrostyImageButton x:Name="ssImageButton3" Title="IMAGE" OnValidate="FrostyImageButton_OnValidate" Margin="0,0,4,0"/>
Expand Down
122 changes: 117 additions & 5 deletions FrostyEditor/Windows/ModSettingsWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Collections.Generic;
using System;
using System.Linq;
using System.Windows.Controls;

namespace FrostyEditor.Windows
{
Expand Down Expand Up @@ -38,6 +39,8 @@ public ModSettingsWindow(FrostyProject inProject = null)

private void ModSettingsWindow_Loaded(object sender, System.Windows.RoutedEventArgs e)
{
ErrorMessageLabel.Visibility = System.Windows.Visibility.Collapsed;

modCategoryComboBox.ItemsSource = categories;

modTitleTextBox.Text = ModSettings.Title;
Expand Down Expand Up @@ -72,20 +75,129 @@ private void cancelButton_Click(object sender, System.Windows.RoutedEventArgs e)

private void saveButton_Click(object sender, System.Windows.RoutedEventArgs e)
{
if (modTitleTextBox.Text == "" || modAuthorTextBox.Text == "" || modCategoryTextBox.Text == "" || modVersionTextBox.Text == "")
List<string> errors = new List<string>();

// Check Title
if (String.IsNullOrEmpty(modTitleTextBox.Text))
{
FrostyMessageBox.Show("Title, Author, Category and Version are mandatory fields", "Frosty Editor");
return;
errors.Add("Title are mandatory fields");
modTitleTextBox.BorderBrush = System.Windows.Media.Brushes.Red;
}
else if (modTitleTextBox.Text.Any(c => c > sbyte.MaxValue))
{
errors.Add("Title can only contain ASCII characters.");
modTitleTextBox.BorderBrush = System.Windows.Media.Brushes.Red;
}
else
{
modTitleTextBox.BorderBrush = (System.Windows.Media.Brush)FindResource("ControlBackground");
}

// Check Author
if (String.IsNullOrEmpty(modAuthorTextBox.Text))
{
errors.Add("Author are mandatory fields");
modAuthorTextBox.BorderBrush = System.Windows.Media.Brushes.Red;
}
else if (modAuthorTextBox.Text.Any(c => c > sbyte.MaxValue))
{
errors.Add("Author can only contain ASCII characters.");
modAuthorTextBox.BorderBrush = System.Windows.Media.Brushes.Red;
}
else
{
modAuthorTextBox.BorderBrush = (System.Windows.Media.Brush)FindResource("ControlBackground");
}

// Check Category
if (String.IsNullOrEmpty(modCategoryTextBox.Text))
{
errors.Add("Category are mandatory fields");
modCategoryTextBox.BorderBrush = System.Windows.Media.Brushes.Red;
}
else if (modCategoryTextBox.Text.Any(c => c > sbyte.MaxValue))
{
errors.Add("Category can only contain ASCII characters.");
modCategoryTextBox.BorderBrush = System.Windows.Media.Brushes.Red;
}
else
{
modCategoryTextBox.BorderBrush = (System.Windows.Media.Brush)FindResource("ControlBackground");
}

// Check Version
if (String.IsNullOrEmpty(modVersionTextBox.Text))
{
errors.Add("Version are mandatory fields");
modVersionTextBox.BorderBrush = System.Windows.Media.Brushes.Red;
}
else if (modVersionTextBox.Text.Any(c => c > sbyte.MaxValue))
{
errors.Add("Version can only contain ASCII characters.");
modVersionTextBox.BorderBrush = System.Windows.Media.Brushes.Red;
}
else
{
modVersionTextBox.BorderBrush = (System.Windows.Media.Brush)FindResource("ControlBackground");
}

// Check Link
string[] approvedDomains = { "nexusmods.com", "moddb.com" };
if (!String.IsNullOrEmpty(modPageLinkTextBox.Text))
{
if (!Uri.IsWellFormedUriString(modPageLinkTextBox.Text, UriKind.RelativeOrAbsolute))
{
errors.Add("Link needs to be valid");
modPageLinkTextBox.BorderBrush = System.Windows.Media.Brushes.Red;
}
else if (!approvedDomains.Any(modPageLinkTextBox.Text.Contains))
{
errors.Add("Link needs to be nexusmods.com or moddb.com");
modPageLinkTextBox.BorderBrush = System.Windows.Media.Brushes.Red;
}
else
{
if (Uri.TryCreate(modPageLinkTextBox.Text, UriKind.Absolute, out Uri uriResult))
modPageLinkTextBox.Text = uriResult.AbsoluteUri;

if (modPageLinkTextBox.Text.Any(c => c > sbyte.MaxValue))
{
errors.Add("Link can only contain ASCII characters.");
modPageLinkTextBox.BorderBrush = System.Windows.Media.Brushes.Red;
}
else
{
modPageLinkTextBox.BorderBrush = (System.Windows.Media.Brush)FindResource("ControlBackground");
}
}
}
else
{
modPageLinkTextBox.BorderBrush = (System.Windows.Media.Brush)FindResource("ControlBackground");
}

// Check Description
if (!String.IsNullOrEmpty(modDescriptionTextBox.Text) &&
!modDescriptionTextBox.Text.Any(c => c > sbyte.MaxValue))
{
errors.Add("Description can only contain ASCII characters.");
modDescriptionTextBox.BorderBrush = System.Windows.Media.Brushes.Red;
}
else
{
modDescriptionTextBox.BorderBrush = (System.Windows.Media.Brush)FindResource("ControlBackground");
}

if (modPageLinkTextBox.Text != "" && (!Uri.IsWellFormedUriString(modPageLinkTextBox.Text, UriKind.RelativeOrAbsolute) || !approvedDomains.Any(modPageLinkTextBox.Text.Contains)))
// Check if error exits
if (errors.Any())
{
FrostyMessageBox.Show("Link needs to be valid", "Frosty Editor");
ErrorMessageLabel.Visibility = System.Windows.Visibility.Visible;
(ErrorMessageLabel.Content as TextBlock).Text = String.Join(Environment.NewLine, errors);
return;
}

ErrorMessageLabel.Visibility = System.Windows.Visibility.Collapsed;

ModSettings.Title = modTitleTextBox.Text;
ModSettings.Author = modAuthorTextBox.Text;
ModSettings.Category = modCategoryTextBox.Text;
Expand Down