From 84dd7c52d586e8e130c00169c4e4175376f3b368 Mon Sep 17 00:00:00 2001 From: Mikhail Date: Tue, 9 Jul 2024 17:21:40 -0400 Subject: [PATCH] Allow setting IP address --- .../Properties/launchSettings.json | 8 +++ ServerSelector/ServerSwitcher.cs | 47 ++++++++----- ServerSelector/Views/MainView.axaml | 66 ++++++++++++------- ServerSelector/Views/MainView.axaml.cs | 22 ++++++- ServerSelector/Views/MainWindow.axaml | 4 +- 5 files changed, 101 insertions(+), 46 deletions(-) create mode 100644 ServerSelector.Desktop/Properties/launchSettings.json diff --git a/ServerSelector.Desktop/Properties/launchSettings.json b/ServerSelector.Desktop/Properties/launchSettings.json new file mode 100644 index 0000000..05196a4 --- /dev/null +++ b/ServerSelector.Desktop/Properties/launchSettings.json @@ -0,0 +1,8 @@ +{ + "profiles": { + "ServerSelector.Desktop": { + "commandName": "Project", + "nativeDebugging": false + } + } +} \ No newline at end of file diff --git a/ServerSelector/ServerSwitcher.cs b/ServerSelector/ServerSwitcher.cs index 1702ae1..388888e 100644 --- a/ServerSelector/ServerSwitcher.cs +++ b/ServerSelector/ServerSwitcher.cs @@ -16,7 +16,7 @@ namespace ServerSelector return !hostsFile.Contains("cloud.nikke-kr.com"); } - public static void SaveCfg(bool useOffical, string gamePath, string launcherPath) + public static void SaveCfg(bool useOffical, string gamePath, string launcherPath, string ip) { string sodiumLib = AppDomain.CurrentDomain.BaseDirectory + "sodium.dll"; string gameSodium = gamePath + "/nikke_Data/Plugins/x86_64/sodium.dll"; @@ -37,8 +37,20 @@ namespace ServerSelector try { - int startIdx = txt.IndexOf("127.0.0.1 cloud.nikke-kr.com"); - int endIdx = txt.IndexOf("y.io") + 3; + int startIdx = txt.IndexOf("cloud.nikke-kr.com"); + + // find new line character before start index + for (int i = startIdx - 1; i >= 0; i--) + { + var c = txt[i]; + if (c == '\n') + { + startIdx = i + 1; + break; + } + } + + int endIdx = txt.IndexOf("y.io") + 4; txt = txt.Substring(0, startIdx) + txt.Substring(endIdx); File.WriteAllText(hostsFilePath, txt); @@ -71,25 +83,26 @@ namespace ServerSelector else { // add to hosts file - string hosts = @"127.0.0.1 cloud.nikke-kr.com -127.0.0.1 global-lobby.nikke-kr.com -127.0.0.1 jp-lobby.nikke-kr.com -127.0.0.1 us-lobby.nikke-kr.com -127.0.0.1 kr-lobby.nikke-kr.com -127.0.0.1 sea-lobby.nikke-kr.com -127.0.0.1 hmt-lobby.nikke-kr.com -127.0.0.1 aws-na-dr.intlgame.com -127.0.0.1 sg-vas.intlgame.com -127.0.0.1 aws-na.intlgame.com -127.0.0.1 na-community.playerinfinite.com -127.0.0.1 common-web.intlgame.com -127.0.0.1 li-sg.intlgame.com -127.0.0.1 data-aws-na.intlgame.com + string hosts = $@"{ip} cloud.nikke-kr.com +{ip} global-lobby.nikke-kr.com +{ip} jp-lobby.nikke-kr.com +{ip} us-lobby.nikke-kr.com +{ip} kr-lobby.nikke-kr.com +{ip} sea-lobby.nikke-kr.com +{ip} hmt-lobby.nikke-kr.com +{ip} aws-na-dr.intlgame.com +{ip} sg-vas.intlgame.com +{ip} aws-na.intlgame.com +{ip} na-community.playerinfinite.com +{ip} common-web.intlgame.com +{ip} li-sg.intlgame.com +{ip} data-aws-na.intlgame.com 255.255.221.21 sentry.io"; if (!File.ReadAllText(hostsFilePath).Contains("global-lobby.nikke-kr.com")) { using StreamWriter w = File.AppendText(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.System), "drivers/etc/hosts")); + w.WriteLine(); w.WriteLine(hosts); } diff --git a/ServerSelector/Views/MainView.axaml b/ServerSelector/Views/MainView.axaml index 68778bb..fd1fb60 100644 --- a/ServerSelector/Views/MainView.axaml +++ b/ServerSelector/Views/MainView.axaml @@ -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" > - - - - + + - - - Game path: - C:\NIKKE\NIKKE\game - + + + + + - - Launcher path: - C:\NIKKE\Launcher - + + + + + + + + + + + - - Server: + Game path: + C:\NIKKE\NIKKE\game - - - Official - Local - - - + Launcher path: + C:\NIKKE\Launcher + + + Server: + + + + Official + Local + + + + IP: + 127.0.0.1 - - + + + diff --git a/ServerSelector/Views/MainView.axaml.cs b/ServerSelector/Views/MainView.axaml.cs index ba2171e..76b36bb 100644 --- a/ServerSelector/Views/MainView.axaml.cs +++ b/ServerSelector/Views/MainView.axaml.cs @@ -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)] diff --git a/ServerSelector/Views/MainWindow.axaml b/ServerSelector/Views/MainWindow.axaml index 7f4ea9e..950f0f8 100644 --- a/ServerSelector/Views/MainWindow.axaml +++ b/ServerSelector/Views/MainWindow.axaml @@ -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">