Allow setting IP address

This commit is contained in:
Mikhail
2024-07-09 17:21:40 -04:00
parent 0219604a1d
commit 84dd7c52d5
5 changed files with 101 additions and 46 deletions

View File

@@ -3,40 +3,56 @@
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:vm="clr-namespace:ServerSelector.ViewModels"
mc:Ignorable="d" d:DesignWidth="370" d:DesignHeight="170"
mc:Ignorable="d" d:DesignWidth="370" d:DesignHeight="200"
x:Class="ServerSelector.Views.MainView"
x:DataType="vm:MainViewModel"
>
<Design.DataContext>
<!-- This only sets the DataContext for the previewer in an IDE,
<Design.DataContext>
<!-- This only sets the DataContext for the previewer in an IDE,
to set the actual DataContext for runtime, set the DataContext property in code (look at App.axaml.cs) -->
<vm:MainViewModel />
</Design.DataContext>
<vm:MainViewModel />
</Design.DataContext>
<StackPanel>
<WrapPanel Margin="5">
<TextBlock VerticalAlignment="Center" Margin="5">Game path: </TextBlock>
<TextBox x:Name="txtGamePath">C:\NIKKE\NIKKE\game</TextBox>
</WrapPanel>
<Grid Margin="5">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="auto"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<WrapPanel Margin="5">
<TextBlock VerticalAlignment="Center" Margin="5">Launcher path: </TextBlock>
<TextBox x:Name="txtLauncherPath">C:\NIKKE\Launcher</TextBox>
</WrapPanel>
<Grid.RowDefinitions>
<RowDefinition Height="auto"></RowDefinition>
<RowDefinition Height="5"></RowDefinition>
<RowDefinition Height="auto"></RowDefinition>
<RowDefinition Height="5"></RowDefinition>
<RowDefinition Height="auto"></RowDefinition>
<RowDefinition Height="5"></RowDefinition>
<RowDefinition Height="auto"></RowDefinition>
<RowDefinition Height="5"></RowDefinition>
<RowDefinition Height="auto"></RowDefinition>
</Grid.RowDefinitions>
<WrapPanel Margin="5">
<TextBlock VerticalAlignment="Center" Margin="5">Server: </TextBlock>
<TextBlock VerticalAlignment="Center" Margin="5" Grid.Row="0" Grid.Column="0">Game path: </TextBlock>
<TextBox x:Name="txtGamePath" Grid.Row="0" Grid.Column="1">C:\NIKKE\NIKKE\game</TextBox>
<ComboBox SelectedIndex="0" x:Name="CmbServerSelection">
<ComboBox.Items>
<ComboBoxItem>Official</ComboBoxItem>
<ComboBoxItem>Local</ComboBoxItem>
</ComboBox.Items>
</ComboBox>
</WrapPanel>
<TextBlock VerticalAlignment="Center" Margin="5" Grid.Row="2" Grid.Column="0">Launcher path: </TextBlock>
<TextBox x:Name="txtLauncherPath" Grid.Row="2" Grid.Column="1">C:\NIKKE\Launcher</TextBox>
<TextBlock VerticalAlignment="Center" Margin="5" Grid.Row="4" Grid.Column="0">Server: </TextBlock>
<ComboBox SelectedIndex="0" x:Name="CmbServerSelection" Grid.Row="4" Grid.Column="1" SelectionChanged="CmbServerSelection_SelectionChanged">
<ComboBox.Items>
<ComboBoxItem>Official</ComboBoxItem>
<ComboBoxItem>Local</ComboBoxItem>
</ComboBox.Items>
</ComboBox>
<TextBlock VerticalAlignment="Center" Margin="5" Grid.Row="6" Grid.Column="0">IP: </TextBlock>
<TextBox x:Name="TxtIpAddress" Grid.Row="6" Grid.Column="1">127.0.0.1</TextBox>
<Button HorizontalAlignment="Right" Margin="5" Click="Save_Click">Save</Button>
</StackPanel>
<Button HorizontalAlignment="Right" Margin="5" Click="Save_Click" Grid.Row="8" Grid.Column="1">Save</Button>
</Grid>
</UserControl>

View File

@@ -1,5 +1,6 @@
using Avalonia.Controls;
using System;
using System.Net;
using System.Runtime.InteropServices;
using System.Security.Principal;
@@ -11,6 +12,8 @@ public partial class MainView : UserControl
{
InitializeComponent();
CmbServerSelection.SelectedIndex = ServerSwitcher.IsUsingOfficalServer() ? 0 : 1;
TxtIpAddress.IsEnabled = !ServerSwitcher.IsUsingOfficalServer();
}
private void Save_Click(object? sender, Avalonia.Interactivity.RoutedEventArgs e)
@@ -21,16 +24,31 @@ public partial class MainView : UserControl
return;
}
if (CmbServerSelection.SelectedIndex == 1)
{
if (!IPAddress.TryParse(TxtIpAddress.Text, out _))
{
ShowWarningMsg("Invalid IP address. The entered IP address should be IPv4 or IPv6.", "Error");
return;
}
}
try
{
ServerSwitcher.SaveCfg(CmbServerSelection.SelectedIndex == 0, txtGamePath.Text, txtLauncherPath.Text);
ServerSwitcher.SaveCfg(CmbServerSelection.SelectedIndex == 0, txtGamePath.Text, txtLauncherPath.Text, TxtIpAddress.Text);
}
catch(Exception ex)
catch (Exception ex)
{
ShowWarningMsg("Failed to save configuration: " + ex.ToString(), "Error");
}
}
private void CmbServerSelection_SelectionChanged(object? sender, SelectionChangedEventArgs e)
{
if (CmbServerSelection != null)
TxtIpAddress.IsEnabled = CmbServerSelection.SelectedIndex == 1;
}
// for some stupid reason avalonia does not support message boxes.
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]

View File

@@ -4,9 +4,9 @@
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:views="clr-namespace:ServerSelector.Views"
mc:Ignorable="d" d:DesignWidth="370" d:DesignHeight="170"
mc:Ignorable="d" d:DesignWidth="370" d:DesignHeight="200"
x:Class="ServerSelector.Views.MainWindow"
Icon="/Assets/avalonia-logo.ico"
Title="Server Switcher Utility" Width="370" Height="170" CanResize="False">
Title="Server Switcher Utility" Width="370" Height="210" CanResize="False">
<views:MainView />
</Window>