improve server selector

This commit is contained in:
Mikhail
2024-08-24 17:20:09 -04:00
parent 7ddaef38df
commit d00ab6d185
12 changed files with 269 additions and 107 deletions

View File

@@ -3,17 +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="250"
mc:Ignorable="d" d:DesignWidth="370" d:DesignHeight="300"
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,
to set the actual DataContext for runtime, set the DataContext property in code (look at App.axaml.cs) -->
<vm:MainViewModel />
</Design.DataContext>
<Grid>
<TabControl x:Name="MainUI">
<TabItem Header="PC" x:Name="TabPc">
@@ -34,15 +27,17 @@
<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>
<TextBox x:Name="txtGamePath" Grid.Row="0" Grid.Column="1" TextChanged="GamePath_TextChanged">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>
<TextBox x:Name="txtLauncherPath" Grid.Row="2" Grid.Column="1" TextChanged="LauncherPath_TextChanged">C:\NIKKE\Launcher</TextBox>
<Button x:Name="btnSelectLauncherPath" Grid.Row="2" Grid.Column="2" Content="..." Click="BtnSelectLauncherPath_Click"></Button>
@@ -55,11 +50,13 @@
</ComboBox.Items>
</ComboBox>
<TextBlock VerticalAlignment="Center" Margin="5" Grid.Row="6" Grid.Column="0">IP: </TextBlock>
<TextBlock x:Name="LblIp" 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>
<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>
<CheckBox x:Name="ChkOffline" VerticalAlignment="Center" Margin="5" Grid.Row="8" Grid.Column="0" Grid.ColumnSpan="3" ToolTip.Tip="Enables the ability to run the game offline by making the game download the assets from the server, and not the official server. This only works if enableOffline is enabled on the server. Please note that this should not be enabled on public servers due to copyright issues.">Enable offline mode</CheckBox>
<TextBlock Grid.Row="10" Grid.Column="0" VerticalAlignment="Center" Margin="5" x:Name="LblStatus" ToolTip.Tip="Shows the status of the patches to the game. All patches are reverted when server is set to official.">Status: OK</TextBlock>
<Button HorizontalAlignment="Right" Margin="5" Click="Save_Click" Grid.Row="10" Grid.Column="1" Grid.ColumnSpan="2" Classes="accent">Save</Button>
</Grid>
</TabItem>
<TabItem Header="Android">

View File

@@ -1,4 +1,5 @@
using Avalonia.Controls;
using Avalonia.Media;
using Avalonia.Platform.Storage;
using FluentAvalonia.UI.Controls;
using System;
@@ -17,6 +18,7 @@ public partial class MainView : UserControl
CmbServerSelection.SelectedIndex = ServerSwitcher.IsUsingOfficalServer() ? 0 : 1;
TxtIpAddress.IsEnabled = !ServerSwitcher.IsUsingOfficalServer();
ChkOffline.IsChecked = ServerSwitcher.IsOffline();
if (OperatingSystem.IsWindows() && !new WindowsPrincipal(WindowsIdentity.GetCurrent()).IsInRole(WindowsBuiltInRole.Administrator))
{
@@ -28,40 +30,108 @@ public partial class MainView : UserControl
};
}
LblStatus.Text = "Status: " + ServerSwitcher.CheckIntegrity();
UpdateIntegrityLabel();
}
private void Save_Click(object? sender, Avalonia.Interactivity.RoutedEventArgs e)
private void SetGamePathValid(bool isValid)
{
if (string.IsNullOrEmpty(txtGamePath.Text) || string.IsNullOrEmpty(txtLauncherPath.Text))
if (isValid)
{
ShowWarningMsg("Game path / launcher path is empty", "Error");
return;
txtGamePath.BorderBrush = null;
}
else
{
txtGamePath.BorderBrush = new SolidColorBrush(Color.FromRgb(255,0,0));
}
}
private void SetLauncherPathValid(bool isValid)
{
if (isValid)
{
txtLauncherPath.BorderBrush = null;
}
else
{
txtLauncherPath.BorderBrush = new SolidColorBrush(Color.FromRgb(255, 0, 0));
}
}
private bool ValidatePaths(bool showMessage)
{
if (string.IsNullOrEmpty(txtGamePath.Text))
{
SetGamePathValid(false);
if (showMessage)
ShowWarningMsg("Game path is blank", "Error");
return false;
}
if (string.IsNullOrEmpty(txtLauncherPath.Text))
{
SetLauncherPathValid(false);
if (showMessage)
ShowWarningMsg("Launcher path is blank", "Error");
return false;
}
if (!Directory.Exists(txtGamePath.Text))
{
ShowWarningMsg("Game path folder does not exist", "Error");
return;
SetGamePathValid(false);
if (showMessage)
ShowWarningMsg("Game path does not exist", "Error");
return false;
}
if (!Directory.Exists(txtLauncherPath.Text))
{
ShowWarningMsg("Launcher folder does not exist", "Error");
return;
SetLauncherPathValid(false);
if (showMessage)
ShowWarningMsg("Launcher path does not exist", "Error");
return false;
}
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;
SetGamePathValid(false);
if (showMessage)
ShowWarningMsg("Launcher path is invalid. Make sure that the game executable exists in the launcher folder", "Error");
return false;
}
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");
SetGamePathValid(true);
SetLauncherPathValid(true);
return true;
}
private async void UpdateIntegrityLabel()
{
if (!ValidatePaths(false) || txtGamePath.Text == null || txtLauncherPath.Text == null)
return;
SetLoadingScreenVisible(true);
LblStatus.Text = "Status: " + await ServerSwitcher.CheckIntegrity(txtGamePath.Text, txtLauncherPath.Text);
SetLoadingScreenVisible(false);
}
private void SetLoadingScreenVisible(bool visible)
{
if (visible)
{
MainUI.IsVisible = false;
LoadingUI.IsVisible = true;
}
else
{
LoadingUI.IsVisible = false;
MainUI.IsVisible = true;
}
}
private async void Save_Click(object? sender, Avalonia.Interactivity.RoutedEventArgs e)
{
if (!ValidatePaths(true) || txtGamePath.Text == null || txtLauncherPath.Text == null)
return;
if (CmbServerSelection.SelectedIndex == 1)
{
@@ -73,26 +143,27 @@ public partial class MainView : UserControl
}
if (TxtIpAddress.Text == null) TxtIpAddress.Text = "";
MainUI.IsVisible = false;
LoadingUI.IsVisible = true;
SetLoadingScreenVisible(true);
try
{
ServerSwitcher.SaveCfg(CmbServerSelection.SelectedIndex == 0, txtGamePath.Text, txtLauncherPath.Text, TxtIpAddress.Text);
await ServerSwitcher.SaveCfg(CmbServerSelection.SelectedIndex == 0, txtGamePath.Text, txtLauncherPath.Text, TxtIpAddress.Text, ChkOffline.IsChecked ?? false);
}
catch (Exception ex)
{
ShowWarningMsg("Failed to save configuration: " + ex.ToString(), "Error");
}
LoadingUI.IsVisible = false;
MainUI.IsVisible = true;
UpdateIntegrityLabel();
SetLoadingScreenVisible(false);
}
private void CmbServerSelection_SelectionChanged(object? sender, SelectionChangedEventArgs e)
{
if (CmbServerSelection != null)
{
TxtIpAddress.IsEnabled = CmbServerSelection.SelectedIndex == 1;
ChkOffline.IsEnabled = CmbServerSelection.SelectedIndex == 1;
LblIp.IsEnabled = CmbServerSelection.SelectedIndex == 1;
}
}
public static void ShowWarningMsg(string text, string title)
@@ -127,6 +198,7 @@ public partial class MainView : UserControl
return;
}
}
UpdateIntegrityLabel();
}
}
}
@@ -156,8 +228,16 @@ public partial class MainView : UserControl
return;
}
}
UpdateIntegrityLabel();
}
}
}
private void GamePath_TextChanged(object? sender, Avalonia.Controls.TextChangedEventArgs e)
{
UpdateIntegrityLabel();
}
private void LauncherPath_TextChanged(object? sender, Avalonia.Controls.TextChangedEventArgs e)
{
UpdateIntegrityLabel();
}
}

View File

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