update server selector

This commit is contained in:
Mikhail
2024-08-22 21:09:48 -04:00
parent 107e6b2eed
commit 7ddaef38df
7 changed files with 237 additions and 98 deletions

View File

@@ -3,9 +3,10 @@
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="200"
mc:Ignorable="d" d:DesignWidth="370" d:DesignHeight="250"
x:Class="ServerSelector.Views.MainView"
x:DataType="vm:MainViewModel"
xmlns:ui="using:FluentAvalonia.UI.Controls"
>
<Design.DataContext>
<!-- This only sets the DataContext for the previewer in an IDE,
@@ -13,46 +14,93 @@
<vm:MainViewModel />
</Design.DataContext>
<Grid Margin="5">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="auto"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Grid>
<TabControl x:Name="MainUI">
<TabItem Header="PC" x:Name="TabPc">
<Grid Margin="5">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="auto"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
<ColumnDefinition Width="auto"></ColumnDefinition>
</Grid.ColumnDefinitions>
<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>
<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>
<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>
<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>
<Button x:Name="btnSelectGamePath" Grid.Row="0" Grid.Column="2" Content="..." Click="BtnSelectGamePath_Click"></Button>
<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="2" Grid.Column="0">Launcher path: </TextBlock>
<TextBox x:Name="txtLauncherPath" Grid.Row="2" Grid.Column="1">C:\NIKKE\Launcher</TextBox>
<Button x:Name="btnSelectLauncherPath" Grid.Row="2" Grid.Column="2" Content="..." Click="BtnSelectLauncherPath_Click"></Button>
<TextBlock VerticalAlignment="Center" Margin="5" Grid.Row="4" Grid.Column="0">Server: </TextBlock>
<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>
<ComboBox SelectedIndex="0" x:Name="CmbServerSelection" Grid.Row="4" Grid.Column="1" Grid.ColumnSpan="2" SelectionChanged="CmbServerSelection_SelectionChanged" HorizontalAlignment="Stretch">
<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>
<TextBlock VerticalAlignment="Center" Margin="5" Grid.Row="6" Grid.Column="0">IP: </TextBlock>
<TextBox x:Name="TxtIpAddress" Grid.Row="6" Grid.Column="1" Grid.ColumnSpan="2">127.0.0.1</TextBox>
<Button HorizontalAlignment="Right" Margin="5" Click="Save_Click" Grid.Row="8" Grid.Column="1">Save</Button>
<TextBlock Grid.Row="8" Grid.Column="0" VerticalAlignment="Center" Margin="5" x:Name="LblStatus">Status: OK</TextBlock>
<Button HorizontalAlignment="Right" Margin="5" Click="Save_Click" Grid.Row="8" Grid.Column="1" Grid.ColumnSpan="2" Classes="accent">Save</Button>
</Grid>
</TabItem>
<TabItem Header="Android">
<Grid Margin="5">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="auto"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
<ColumnDefinition Width="auto"></ColumnDefinition>
</Grid.ColumnDefinitions>
<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>
<RowDefinition Height="5"></RowDefinition>
<RowDefinition Height="auto"></RowDefinition>
</Grid.RowDefinitions>
<TextBlock Grid.Row="0" Grid.Column="0" VerticalAlignment="Center" Margin="5">APK path: </TextBlock>
<TextBox x:Name="txtApkPath" Grid.Row="0" Grid.Column="1"></TextBox>
<Button x:Name="btnSelectApkPath" Grid.Row="0" Grid.Column="2" Content="..."></Button>
<TextBlock VerticalAlignment="Center" Grid.Row="2" Grid.Column="0" Margin="5">IP: </TextBlock>
<TextBox x:Name="TxtAndroidIpAddress" Grid.Row="2" Grid.Column="1" Grid.ColumnSpan="2">127.0.0.1</TextBox>
<Button HorizontalAlignment="Right" Margin="5" Click="Save_Click" Grid.Row="4" Grid.Column="1" Grid.ColumnSpan="2" Classes="accent" IsEnabled="False">Coming soon!</Button>
</Grid>
</TabItem>
</TabControl>
<StackPanel VerticalAlignment="Center" HorizontalAlignment="Center" x:Name="LoadingUI" IsVisible="False">
<ui:ProgressRing Height="48" Width="48"></ui:ProgressRing>
<TextBlock>Please wait...</TextBlock>
</StackPanel>
</Grid>
</UserControl>

View File

@@ -1,8 +1,11 @@
using Avalonia.Controls;
using Avalonia.Platform.Storage;
using FluentAvalonia.UI.Controls;
using System;
using System.IO;
using System.Net;
using System.Runtime.InteropServices;
using System.Security.Principal;
namespace ServerSelector.Views;
@@ -14,6 +17,18 @@ public partial class MainView : UserControl
CmbServerSelection.SelectedIndex = ServerSwitcher.IsUsingOfficalServer() ? 0 : 1;
TxtIpAddress.IsEnabled = !ServerSwitcher.IsUsingOfficalServer();
if (OperatingSystem.IsWindows() && !new WindowsPrincipal(WindowsIdentity.GetCurrent()).IsInRole(WindowsBuiltInRole.Administrator))
{
TabPc.Content = new TextBlock()
{
VerticalAlignment = Avalonia.Layout.VerticalAlignment.Center,
HorizontalAlignment = Avalonia.Layout.HorizontalAlignment.Center,
Text = "Administrator privileges are required to change servers."
};
}
LblStatus.Text = "Status: " + ServerSwitcher.CheckIntegrity();
}
private void Save_Click(object? sender, Avalonia.Interactivity.RoutedEventArgs e)
@@ -58,6 +73,8 @@ public partial class MainView : UserControl
}
if (TxtIpAddress.Text == null) TxtIpAddress.Text = "";
MainUI.IsVisible = false;
LoadingUI.IsVisible = true;
try
{
@@ -67,6 +84,9 @@ public partial class MainView : UserControl
{
ShowWarningMsg("Failed to save configuration: " + ex.ToString(), "Error");
}
LoadingUI.IsVisible = false;
MainUI.IsVisible = true;
}
private void CmbServerSelection_SelectionChanged(object? sender, SelectionChangedEventArgs e)
@@ -75,13 +95,69 @@ public partial class MainView : UserControl
TxtIpAddress.IsEnabled = CmbServerSelection.SelectedIndex == 1;
}
// for some stupid reason avalonia does not support message boxes.
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
public static extern int MessageBox(IntPtr hWnd, String text, String caption, uint type);
public static void ShowWarningMsg(string text, string title)
{
MessageBox(IntPtr.Zero, text, title, 0x00000030);
ContentDialog dlg = new ContentDialog() { Title = title, Content = text, PrimaryButtonText = "OK" };
dlg.ShowAsync();
}
private async void BtnSelectGamePath_Click(object? sender, Avalonia.Interactivity.RoutedEventArgs e)
{
var topLevel = TopLevel.GetTopLevel(this);
if (topLevel != null)
{
// Start async operation to open the dialog.
var files = await topLevel.StorageProvider.OpenFolderPickerAsync(new FolderPickerOpenOptions
{
Title = "Select game path (with game executable)",
AllowMultiple = false
});
if (files.Count >= 1)
{
txtGamePath.Text = files[0].TryGetLocalPath();
// validate if the folder has game exe
if (!string.IsNullOrEmpty(txtGamePath.Text))
{
if (!File.Exists(Path.Combine(txtGamePath.Text, "nikke.exe")))
{
ShowWarningMsg("Game path is invalid. Make sure that nikke.exe exists in the launcher folder", "Error");
return;
}
}
}
}
}
private async void BtnSelectLauncherPath_Click(object? sender, Avalonia.Interactivity.RoutedEventArgs e)
{
var topLevel = TopLevel.GetTopLevel(this);
if (topLevel != null)
{
// Start async operation to open the dialog.
var files = await topLevel.StorageProvider.OpenFolderPickerAsync(new FolderPickerOpenOptions
{
Title = "Select launcher path",
AllowMultiple = false
});
if (files.Count >= 1)
{
txtLauncherPath.Text = files[0].TryGetLocalPath();
// validate if the folder has game exe
if (!string.IsNullOrEmpty(txtLauncherPath.Text))
{
if (!File.Exists(Path.Combine(txtLauncherPath.Text, "nikke_launcher.exe")))
{
ShowWarningMsg("Launcher path is invalid. Make sure that the game executable exists in the launcher folder", "Error");
return;
}
}
}
}
}
}

View File

@@ -4,9 +4,8 @@
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="200"
mc:Ignorable="d" d:DesignWidth="370" d:DesignHeight="250"
x:Class="ServerSelector.Views.MainWindow"
Icon="/Assets/avalonia-logo.ico"
Title="Server Switcher Utility" Width="370" Height="210" CanResize="False">
Title="Server Switcher" Width="370" Height="260" CanResize="False">
<views:MainView />
</Window>