Fix textboxes

This commit is contained in:
Li 2023-04-25 14:22:34 +12:00
parent 5f047866d9
commit 86dcb439ee
11 changed files with 203 additions and 98 deletions

1
.gitignore vendored
View File

@ -4,6 +4,7 @@
*.7z *.7z
*.pdn *.pdn
*.user *.user
*launchSettings.json
*Thumbs.db *Thumbs.db
PbpResign/bin/* PbpResign/bin/*

View File

@ -42,11 +42,11 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Avalonia" Version="0.10.18" /> <PackageReference Include="Avalonia" Version="0.10.19" />
<PackageReference Include="Avalonia.Desktop" Version="0.10.18" /> <PackageReference Include="Avalonia.Desktop" Version="0.10.19" />
<!--Condition below is needed to remove Avalonia.Diagnostics package from build output in Release configuration.--> <!--Condition below is needed to remove Avalonia.Diagnostics package from build output in Release configuration.-->
<PackageReference Condition="'$(Configuration)' == 'Debug'" Include="Avalonia.Diagnostics" Version="0.10.18" /> <PackageReference Condition="'$(Configuration)' == 'Debug'" Include="Avalonia.Diagnostics" Version="0.10.19" />
<PackageReference Include="XamlNameReferenceGenerator" Version="1.5.1" /> <PackageReference Include="XamlNameReferenceGenerator" Version="1.6.1" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>

View File

@ -0,0 +1,8 @@
<UserControl xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
x:Class="ChovySign_GUI.Global.FilteredTextBox">
<TextBox Name="txtBox" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"/>
</UserControl>

View File

@ -0,0 +1,149 @@
using Avalonia;
using Avalonia.Controls;
using Avalonia.Input;
using Avalonia.Interactivity;
using System;
using System.Text;
using System.Threading.Tasks;
namespace ChovySign_GUI.Global
{
public partial class FilteredTextBox : UserControl
{
private string? allowedChars = null;
public event EventHandler<EventArgs>? TextChanged;
protected virtual void OnTextChanged(EventArgs e)
{
if (TextChanged is not null)
TextChanged(this, e);
}
public int MaxLength
{
get
{
return this.txtBox.MaxLength;
}
set
{
this.txtBox.MaxLength = value;
}
}
public bool Password
{
get
{
return this.txtBox.PasswordChar == default(char);
}
set
{
if (value) this.txtBox.PasswordChar = 'X';
else this.txtBox.PasswordChar = default(char);
}
}
public string Watermark
{
get
{
return this.txtBox.Watermark;
}
set
{
this.txtBox.Watermark = value;
}
}
public string? AllowedChars
{
get
{
if (allowedChars is null) return "";
else return allowedChars;
}
set
{
allowedChars = value;
}
}
public string Text
{
get
{
if (this.txtBox.Text is null) return "";
return this.txtBox.Text;
}
set
{
this.txtBox.Text = value;
}
}
public FilteredTextBox()
{
InitializeComponent();
this.txtBox.PastingFromClipboard += onPaste;
this.txtBox.AddHandler(TextInputEvent, onTxtInput, RoutingStrategies.Tunnel);
}
private string filter(string original)
{
if (allowedChars is not null)
{
StringBuilder str = new StringBuilder();
for (int i = 0; i < original.Length; i++)
{
if (allowedChars.Contains(original[i]))
str.Append(original[i]);
}
return str.ToString();
}
return original;
}
private async Task<bool> setClipboardText(string text)
{
if (Application.Current is null) return false;
if (Application.Current.Clipboard is null) return false;
await Application.Current.Clipboard.SetTextAsync(text);
return true;
}
private async Task<string> getClipboardText()
{
if (Application.Current is null) return "";
if (Application.Current.Clipboard is null) return "";
string? clipboard = await Application.Current.Clipboard.GetTextAsync();
if (clipboard is null) return "";
return clipboard;
}
private async void onPaste(object? sender, RoutedEventArgs e)
{
TextBox? txt = sender as TextBox;
if (txt is null) return;
string clipboard = getClipboardText().Result;
clipboard = filter(clipboard);
_ = setClipboardText(clipboard).Result;
// annoyingly, the text being pasted isnt actually in the textbox yet
// and it wont trigger a textInput event when pasting; t-this really is the best can do
await Task.Delay(100);
OnTextChanged(new EventArgs());
}
private void onTxtInput(object? sender, TextInputEventArgs e)
{
string? newTxt = e.Text;
if (newTxt is null) newTxt = "";
newTxt = filter(newTxt);
e.Text = newTxt;
OnTextChanged(new EventArgs());
}
}
}

View File

@ -2,7 +2,8 @@
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="40" xmlns:Global="clr-namespace:ChovySign_GUI.Global"
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="40"
x:Class="ChovySign_GUI.Global.KeySelector"> x:Class="ChovySign_GUI.Global.KeySelector">
<!-- key selector --> <!-- key selector -->
<Border Padding="10 3" HorizontalAlignment="Stretch" VerticalAlignment="Top"> <Border Padding="10 3" HorizontalAlignment="Stretch" VerticalAlignment="Top">
@ -18,7 +19,7 @@
<ColumnDefinition Width="1*" /> <ColumnDefinition Width="1*" />
</Grid.ColumnDefinitions> </Grid.ColumnDefinitions>
<Label HorizontalAlignment="Left" VerticalAlignment="Center" Content="Rif:" Grid.Column="0"/> <Label HorizontalAlignment="Left" VerticalAlignment="Center" Content="Rif:" Grid.Column="0"/>
<TextBox HorizontalAlignment="Stretch" Watermark="GAME.RIF" Name="zRif" Grid.Column="1"/> <Global:FilteredTextBox Name="zRif" AllowedChars="1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz=+/" HorizontalAlignment="Stretch" Watermark="LICENSE ZRIF" Grid.Column="1"/>
</Grid> </Grid>
<Grid HorizontalAlignment="Stretch" Grid.Column="1"> <Grid HorizontalAlignment="Stretch" Grid.Column="1">
<Grid.ColumnDefinitions> <Grid.ColumnDefinitions>
@ -26,7 +27,7 @@
<ColumnDefinition Width="1*" /> <ColumnDefinition Width="1*" />
</Grid.ColumnDefinitions> </Grid.ColumnDefinitions>
<Label HorizontalAlignment="Left" VerticalAlignment="Center" Content="Key:" Grid.Column="0"/> <Label HorizontalAlignment="Left" VerticalAlignment="Center" Content="Key:" Grid.Column="0"/>
<TextBox HorizontalAlignment="Stretch" MaxLength="32" Watermark="Version Key" Name="vKey" Grid.Column="1"/> <Global:FilteredTextBox AllowedChars="1234567890ABCDEFabcdef" HorizontalAlignment="Stretch" MaxLength="32" Watermark="Version Key" Name="vKey" Grid.Column="1"/>
</Grid> </Grid>
<Button Name="getKeys" Click="getKeysClick" Content="Get Keys" HorizontalAlignment="Center" VerticalAlignment="Center" Grid.Column="2"/> <Button Name="getKeys" Click="getKeysClick" Content="Get Keys" HorizontalAlignment="Center" VerticalAlignment="Center" Grid.Column="2"/>

View File

@ -60,7 +60,6 @@ namespace ChovySign_GUI.Global
if (zRif.Text is null) return false; if (zRif.Text is null) return false;
if (zRif.Text.Length <= 0) return false; if (zRif.Text.Length <= 0) return false;
byte[] key = MathUtil.StringToByteArray(vKey.Text);
byte[] rif = new NpDrmRif(zRif.Text).Rif; byte[] rif = new NpDrmRif(zRif.Text).Rif;
if (rif.Length <= 0) return false; if (rif.Length <= 0) return false;
return (VersionKey is not null && Rif is not null); return (VersionKey is not null && Rif is not null);
@ -245,15 +244,13 @@ namespace ChovySign_GUI.Global
reloadCfg(); reloadCfg();
lastValid = IsValid; lastValid = IsValid;
zRif.KeyDown += onZrifKeyDown; zRif.TextChanged += onZrifChanged;
zRif.KeyUp += onZrifKeyDown; vKey.TextChanged += onVkeyChanged;
vKey.KeyDown += onVkeyKeyDown;
vKey.KeyUp += onVkeyKeyDown;
} }
private void onVkeyKeyDown(object? sender, Avalonia.Input.KeyEventArgs e) private void onVkeyChanged(object? sender, EventArgs e)
{ {
TextBox? txt = sender as TextBox; FilteredTextBox? txt = sender as FilteredTextBox;
if (txt is null) return; if (txt is null) return;
if (lastValid != IsValid) if (lastValid != IsValid)
@ -269,9 +266,9 @@ namespace ChovySign_GUI.Global
catch { }; catch { };
} }
private void onZrifKeyDown(object? sender, KeyEventArgs e) private void onZrifChanged(object? sender, EventArgs e)
{ {
TextBox? txt = sender as TextBox; FilteredTextBox? txt = sender as FilteredTextBox;
if (txt is null) return; if (txt is null) return;
if (lastValid != IsValid) if (lastValid != IsValid)

View File

@ -2,7 +2,8 @@
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="80" xmlns:Global="clr-namespace:ChovySign_GUI.Global"
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="80"
x:Class="ChovySign_GUI.Global.LabeledTextBox" x:Class="ChovySign_GUI.Global.LabeledTextBox"
Height="60"> Height="60">
<Grid> <Grid>
@ -11,6 +12,6 @@
<RowDefinition Height="1*" /> <RowDefinition Height="1*" />
</Grid.RowDefinitions> </Grid.RowDefinitions>
<Label Name="lblTxt" Content="Label:" VerticalAlignment="Bottom" Grid.Row="0"/> <Label Name="lblTxt" Content="Label:" VerticalAlignment="Bottom" Grid.Row="0"/>
<TextBox Name="txtBox" HorizontalAlignment="Stretch" VerticalAlignment="Center" Grid.Row="1"/> <Global:FilteredTextBox Name="txtBox" HorizontalAlignment="Stretch" VerticalAlignment="Center" Grid.Row="1"/>
</Grid> </Grid>
</UserControl> </UserControl>

View File

@ -2,17 +2,16 @@
using Avalonia.Controls; using Avalonia.Controls;
using Avalonia.Input; using Avalonia.Input;
using Avalonia.Interactivity; using Avalonia.Interactivity;
using JetBrains.Annotations;
using Org.BouncyCastle.Asn1.X509;
using System; using System;
using System.Text; using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace ChovySign_GUI.Global namespace ChovySign_GUI.Global
{ {
public partial class LabeledTextBox : UserControl public partial class LabeledTextBox : UserControl
{ {
private string lastTxt;
private string? allowedChars = null;
public event EventHandler<EventArgs>? TextChanged; public event EventHandler<EventArgs>? TextChanged;
protected virtual void OnTextChanged(EventArgs e) protected virtual void OnTextChanged(EventArgs e)
@ -20,7 +19,6 @@ namespace ChovySign_GUI.Global
if (TextChanged is not null) if (TextChanged is not null)
TextChanged(this, e); TextChanged(this, e);
} }
public int MaxLength public int MaxLength
{ {
get get
@ -37,25 +35,11 @@ namespace ChovySign_GUI.Global
{ {
get get
{ {
return this.txtBox.PasswordChar == default(char); return this.txtBox.Password;
} }
set set
{ {
if (value) this.txtBox.PasswordChar = 'X'; this.txtBox.Password = value;
else this.txtBox.PasswordChar = default(char);
}
}
public string Label
{
get
{
string? lbl = this.lblTxt.Content as string;
if (lbl is null) return "";
else return lbl;
}
set
{
this.lblTxt.Content = value;
} }
} }
@ -74,19 +58,17 @@ namespace ChovySign_GUI.Global
{ {
get get
{ {
if (allowedChars is null) return ""; return this.txtBox.AllowedChars;
else return allowedChars;
} }
set set
{ {
allowedChars = value; this.txtBox.AllowedChars = value;
} }
} }
public string Text public string Text
{ {
get get
{ {
if (this.txtBox.Text is null) return "";
return this.txtBox.Text; return this.txtBox.Text;
} }
set set
@ -94,63 +76,29 @@ namespace ChovySign_GUI.Global
this.txtBox.Text = value; this.txtBox.Text = value;
} }
} }
public string Label
{
get
{
string? lbl = this.lblTxt.Content as string;
if (lbl is null) return "";
else return lbl;
}
set
{
this.lblTxt.Content = value;
}
}
public LabeledTextBox() public LabeledTextBox()
{ {
InitializeComponent(); InitializeComponent();
lastTxt = this.txtBox.Text; this.txtBox.TextChanged += onTxtBoxTextChange;
this.txtBox.PastingFromClipboard += onPaste;
this.txtBox.AddHandler(TextInputEvent, onTxtInput, RoutingStrategies.Tunnel);
} }
private string filter(string original) private void onTxtBoxTextChange(object? sender, EventArgs e)
{ {
if (allowedChars is not null) OnTextChanged(e);
{
string newTxt = original.ToUpperInvariant();
for (int i = 0; i < newTxt.Length; i++)
{
if (!allowedChars.Contains(newTxt[i]))
{
newTxt = newTxt.Replace(newTxt[i].ToString(), "");
}
}
return newTxt;
}
else
{
return original;
}
}
private async void onPaste(object? sender, RoutedEventArgs e)
{
TextBox? txt = sender as TextBox;
if (txt is null) return;
if (Application.Current is null) return;
if (Application.Current.Clipboard is null) return;
e.Handled = true;
string? newTxt = await Application.Current.Clipboard.GetTextAsync();
if (newTxt is null) newTxt = "";
/*TextInputEventArgs txtInput = new TextInputEventArgs();
txtInput.Text = filter(newTxt);*/
//txt.RaiseEvent(new RoutedEventArgs(txtInput));
}
private void onTxtInput(object? sender, TextInputEventArgs e)
{
string? newTxt = e.Text;
if (newTxt is null) newTxt = "";
newTxt = filter(newTxt);
e.Text = newTxt;
if (newTxt != lastTxt) OnTextChanged(new EventArgs());
lastTxt = newTxt;
} }
} }
} }

View File

@ -17,8 +17,8 @@
<RowDefinition Height="1*"/> <RowDefinition Height="1*"/>
</Grid.RowDefinitions> </Grid.RowDefinitions>
<Global:LabeledTextBox Name="idpsInput" MaxLength="32" AllowedChars="1234567890ABCDEF" Label="IDPS / ConsoleID:" Watermark="00000001010200140C00000000000000" Grid.Row="0" VerticalAlignment="Center" HorizontalAlignment="Stretch"/> <Global:LabeledTextBox Name="idpsInput" MaxLength="32" AllowedChars="1234567890ABCDEFabcdef" Label="IDPS / ConsoleID:" Watermark="00000001010200140C00025753578942" Grid.Row="0" VerticalAlignment="Center" HorizontalAlignment="Stretch"/>
<Global:BrowseButton Name="rifFile" Extension="rif" FileTypeName="Rights Information" Label="LICENSE.RIF: (found @ ux0:/pspemu/PSP/LICENSE)" Grid.Row="1" VerticalAlignment="Center" HorizontalAlignment="Stretch"/> <Global:BrowseButton Name="rifFile" Extension="rif" FileTypeName="Rights Information File" Label="LICENSE.RIF: (found @ ux0:/pspemu/PSP/LICENSE)" Grid.Row="1" VerticalAlignment="Center" HorizontalAlignment="Stretch"/>
<Global:BrowseButton Name="actFile" Extension="dat" FileTypeName="Activation Data" Label="ACT.DAT: (found @ tm0:/npdrm/act.dat)" Grid.Row="2" VerticalAlignment="Center" HorizontalAlignment="Stretch"/> <Global:BrowseButton Name="actFile" Extension="dat" FileTypeName="Activation Data" Label="ACT.DAT: (found @ tm0:/npdrm/act.dat)" Grid.Row="2" VerticalAlignment="Center" HorizontalAlignment="Stretch"/>
<Button Name="keyGen" Click="keyGenClick" Content="Generate Keys" Grid.Row="3" HorizontalAlignment="Center"/> <Button Name="keyGen" Click="keyGenClick" Content="Generate Keys" Grid.Row="3" HorizontalAlignment="Center"/>

View File

@ -9,7 +9,7 @@
<Grid> <Grid>
<Grid> <Grid>
<Grid.RowDefinitions> <Grid.RowDefinitions>
<RowDefinition Height="0.2*" /> <RowDefinition Height="40" />
<RowDefinition Height="1.5*" /> <RowDefinition Height="1.5*" />
<RowDefinition Height="1.3*" /> <RowDefinition Height="1.3*" />
<RowDefinition Height="0.6*" /> <RowDefinition Height="0.6*" />

View File

@ -57,7 +57,7 @@ namespace LibChovy.VersionKey
{ {
line = line.ReplaceLineEndings(""); line = line.ReplaceLineEndings("");
string[] data = line.Split(' '); string[] data = line.Split(' ');
if (data.Length != 4) continue; if (data.Length != 5) continue;
if (data[0].Equals(contentId, StringComparison.InvariantCultureIgnoreCase)) if (data[0].Equals(contentId, StringComparison.InvariantCultureIgnoreCase))
return new NpDrmInfo(MathUtil.StringToByteArray(data[1 + keyIndex]), contentId, keyIndex); return new NpDrmInfo(MathUtil.StringToByteArray(data[1 + keyIndex]), contentId, keyIndex);