server selector remove launcher textbox

This commit is contained in:
Mikhail
2024-08-31 12:39:18 -04:00
parent 1c733dfb5e
commit f199ca63e0
4 changed files with 94 additions and 90 deletions

View File

@@ -15,13 +15,13 @@ namespace ServerSelector
public static bool IsUsingOfficalServer() public static bool IsUsingOfficalServer()
{ {
var hostsFile = File.ReadAllText("C:\\Windows\\System32\\drivers\\etc\\hosts"); var hostsFile = File.ReadAllText(OperatingSystem.IsWindows() ? "C:\\Windows\\System32\\drivers\\etc\\hosts" : "/etc/hosts");
return !hostsFile.Contains("global-lobby.nikke-kr.com"); return !hostsFile.Contains("global-lobby.nikke-kr.com");
} }
public static bool IsOffline() public static bool IsOffline()
{ {
var hostsFile = File.ReadAllText("C:\\Windows\\System32\\drivers\\etc\\hosts"); var hostsFile = File.ReadAllText(OperatingSystem.IsWindows() ? "C:\\Windows\\System32\\drivers\\etc\\hosts" : "/etc/hosts");
return hostsFile.Contains("cloud.nikke-kr.com"); return hostsFile.Contains("cloud.nikke-kr.com");
} }
@@ -51,6 +51,13 @@ namespace ServerSelector
string gameAssembly = gamePath + "/GameAssembly.dll"; string gameAssembly = gamePath + "/GameAssembly.dll";
string sodiumBackup = gameSodium + ".bak"; string sodiumBackup = gameSodium + ".bak";
string hostsFilePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.System), "drivers/etc/hosts"); string hostsFilePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.System), "drivers/etc/hosts");
if (OperatingSystem.IsLinux())
{
// for wine
hostsFilePath = gamePath + "/../../../windows/system32/drivers/etc/hosts";
}
var CAcert = await File.ReadAllTextAsync(AppDomain.CurrentDomain.BaseDirectory + "myCA.pem"); var CAcert = await File.ReadAllTextAsync(AppDomain.CurrentDomain.BaseDirectory + "myCA.pem");
string launcherCertList = launcherPath + "/intl_service/cacert.pem"; string launcherCertList = launcherPath + "/intl_service/cacert.pem";
@@ -75,9 +82,8 @@ namespace ServerSelector
return "OK"; return "OK";
} }
public static async Task RevertHostsFile() public static async Task RevertHostsFile(string hostsFilePath)
{ {
string hostsFilePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.System), "drivers/etc/hosts");
var txt = await File.ReadAllTextAsync(hostsFilePath); var txt = await File.ReadAllTextAsync(hostsFilePath);
// remove stuff // remove stuff
@@ -139,16 +145,30 @@ namespace ServerSelector
string gameCertList = gamePath + "/nikke_Data/Plugins/x86_64/cacert.pem"; string gameCertList = gamePath + "/nikke_Data/Plugins/x86_64/cacert.pem";
if (OperatingSystem.IsLinux())
{
// for wine
hostsFilePath = gamePath + "/../../../windows/system32/drivers/etc/hosts";
}
// TODO: allow changing ip address // TODO: allow changing ip address
if (useOffical) if (useOffical)
{ {
await RevertHostsFile(); await RevertHostsFile(hostsFilePath);
if (OperatingSystem.IsLinux())
{
await RevertHostsFile("/etc/hosts");
}
// remove cert // remove cert
if (OperatingSystem.IsWindows())
{
X509Store store = new X509Store(StoreName.Root, StoreLocation.LocalMachine); X509Store store = new X509Store(StoreName.Root, StoreLocation.LocalMachine);
store.Open(OpenFlags.ReadWrite); store.Open(OpenFlags.ReadWrite);
store.Remove(new X509Certificate2(X509Certificate.CreateFromCertFile(AppDomain.CurrentDomain.BaseDirectory + "myCA.pfx"))); store.Remove(new X509Certificate2(X509Certificate.CreateFromCertFile(AppDomain.CurrentDomain.BaseDirectory + "myCA.pfx")));
store.Close(); store.Close();
}
// restore sodium // restore sodium
if (!File.Exists(sodiumBackup)) if (!File.Exists(sodiumBackup))
@@ -228,20 +248,35 @@ namespace ServerSelector
255.255.221.21 sentry.io 255.255.221.21 sentry.io
{HostsEndMarker}"; {HostsEndMarker}";
await RevertHostsFile(); await RevertHostsFile(hostsFilePath);
if (!(await File.ReadAllTextAsync(hostsFilePath)).Contains("global-lobby.nikke-kr.com")) if (!(await File.ReadAllTextAsync(hostsFilePath)).Contains("global-lobby.nikke-kr.com"))
{ {
using StreamWriter w = File.AppendText(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.System), "drivers/etc/hosts")); using StreamWriter w = File.AppendText(hostsFilePath);
w.WriteLine(); w.WriteLine();
w.Write(hosts); w.Write(hosts);
} }
// Also change /etc/hosts if running on linux
if (OperatingSystem.IsLinux())
{
hostsFilePath = "/etc/hosts";
await RevertHostsFile(hostsFilePath);
if (!(await File.ReadAllTextAsync(hostsFilePath)).Contains("global-lobby.nikke-kr.com"))
{
using StreamWriter w = File.AppendText(hostsFilePath);
w.WriteLine();
w.Write(hosts);
}
}
// trust CA // trust CA
if (OperatingSystem.IsWindows())
{
X509Store store = new X509Store(StoreName.Root, StoreLocation.LocalMachine); X509Store store = new X509Store(StoreName.Root, StoreLocation.LocalMachine);
store.Open(OpenFlags.ReadWrite); store.Open(OpenFlags.ReadWrite);
store.Add(new X509Certificate2(X509Certificate2.CreateFromCertFile(AppDomain.CurrentDomain.BaseDirectory + "myCA.pfx"))); store.Add(new X509Certificate2(X509Certificate2.CreateFromCertFile(AppDomain.CurrentDomain.BaseDirectory + "myCA.pfx")));
store.Close(); store.Close();
}
// update sodium lib // update sodium lib

View File

@@ -33,14 +33,9 @@
<TextBlock VerticalAlignment="Center" Margin="5" Grid.Row="0" Grid.Column="0">Game path: </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" TextChanged="GamePath_TextChanged">C:\NIKKE\NIKKE\game</TextBox> <TextBox x:Name="txtGamePath" Grid.Row="0" Grid.Column="1" TextChanged="GamePath_TextChanged">C:\NIKKE\NIKKE\</TextBox>
<Button x:Name="btnSelectGamePath" Grid.Row="0" Grid.Column="2" Content="..." Click="BtnSelectGamePath_Click"></Button> <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" TextChanged="LauncherPath_TextChanged">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" Grid.ColumnSpan="2" SelectionChanged="CmbServerSelection_SelectionChanged" HorizontalAlignment="Stretch"> <ComboBox SelectedIndex="0" x:Name="CmbServerSelection" Grid.Row="4" Grid.Column="1" Grid.ColumnSpan="2" SelectionChanged="CmbServerSelection_SelectionChanged" HorizontalAlignment="Stretch">
@@ -55,7 +50,7 @@
<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, as assets are downloaded through said server. This is required if you want to use an older version of the game.">Enable offline mode</CheckBox> <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, as assets are downloaded through said server. This is required if you want to use an older version of the game.">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> <TextBlock Grid.Row="10" Grid.Column="0" TextWrapping="WrapWithOverflow" 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> <Button HorizontalAlignment="Right" Margin="5" Click="Save_Click" Grid.Row="10" Grid.Column="1" Grid.ColumnSpan="2" Classes="accent">Save</Button>
</Grid> </Grid>
</TabItem> </TabItem>

View File

@@ -29,6 +29,15 @@ public partial class MainView : UserControl
Text = "Administrator privileges are required to change servers." Text = "Administrator privileges are required to change servers."
}; };
} }
if (OperatingSystem.IsLinux() && Environment.UserName != "root")
{
TabPc.Content = new TextBlock()
{
VerticalAlignment = Avalonia.Layout.VerticalAlignment.Center,
HorizontalAlignment = Avalonia.Layout.HorizontalAlignment.Center,
Text = "Root is required to change servers."
};
}
UpdateIntegrityLabel(); UpdateIntegrityLabel();
} }
@@ -44,36 +53,34 @@ public partial class MainView : UserControl
txtGamePath.BorderBrush = new SolidColorBrush(Color.FromRgb(255, 0, 0)); 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 string? GamePath
{
get
{
if (txtGamePath.Text == null) return null;
return Path.Combine(txtGamePath.Text, "NIKKE", "game");
}
}
private string? LauncherPath
{
get
{
if (txtGamePath.Text == null) return null;
return Path.Combine(txtGamePath.Text, "Launcher");
}
}
private bool ValidatePaths(bool showMessage) private bool ValidatePaths(bool showMessage)
{ {
if (string.IsNullOrEmpty(txtGamePath.Text)) if (string.IsNullOrEmpty(txtGamePath.Text) || LauncherPath == null)
{ {
SetGamePathValid(false); SetGamePathValid(false);
if (showMessage) if (showMessage)
ShowWarningMsg("Game path is blank", "Error"); ShowWarningMsg("Game path is blank", "Error");
return false; return false;
} }
if (string.IsNullOrEmpty(txtLauncherPath.Text))
{
SetLauncherPathValid(false);
if (showMessage)
ShowWarningMsg("Launcher path is blank", "Error");
return false;
}
if (!Directory.Exists(txtGamePath.Text)) if (!Directory.Exists(GamePath))
{ {
SetGamePathValid(false); SetGamePathValid(false);
if (showMessage) if (showMessage)
@@ -81,36 +88,27 @@ public partial class MainView : UserControl
return false; return false;
} }
if (!Directory.Exists(txtLauncherPath.Text)) if (!File.Exists(Path.Combine(LauncherPath, "nikke_launcher.exe")))
{
SetLauncherPathValid(false);
if (showMessage)
ShowWarningMsg("Launcher path does not exist", "Error");
return false;
}
if (!File.Exists(Path.Combine(txtLauncherPath.Text, "nikke_launcher.exe")))
{ {
SetGamePathValid(false); SetGamePathValid(false);
if (showMessage) if (showMessage)
ShowWarningMsg("Launcher path is invalid. Make sure that the game executable exists in the launcher folder", "Error"); ShowWarningMsg("Launcher path is invalid. Make sure that nikke_launcher.exe exists in the launcher folder", "Error");
return false; return false;
} }
SetGamePathValid(true); SetGamePathValid(true);
SetLauncherPathValid(true);
return true; return true;
} }
private async void UpdateIntegrityLabel() private async void UpdateIntegrityLabel()
{ {
if (!ValidatePaths(false) || txtGamePath.Text == null || txtLauncherPath.Text == null) if (!ValidatePaths(false) || txtGamePath.Text == null || GamePath == null || LauncherPath == null)
return; return;
SetLoadingScreenVisible(true); SetLoadingScreenVisible(true);
LblStatus.Text = "Status: " + await ServerSwitcher.CheckIntegrity(txtGamePath.Text, txtLauncherPath.Text); LblStatus.Text = "Status: " + await ServerSwitcher.CheckIntegrity(GamePath, LauncherPath);
SetLoadingScreenVisible(false); SetLoadingScreenVisible(false);
} }
@@ -130,7 +128,7 @@ public partial class MainView : UserControl
private async void Save_Click(object? sender, Avalonia.Interactivity.RoutedEventArgs e) private async void Save_Click(object? sender, Avalonia.Interactivity.RoutedEventArgs e)
{ {
if (!ValidatePaths(true) || txtGamePath.Text == null || txtLauncherPath.Text == null) if (!ValidatePaths(true) || txtGamePath.Text == null || GamePath == null || LauncherPath == null)
return; return;
if (CmbServerSelection.SelectedIndex == 1) if (CmbServerSelection.SelectedIndex == 1)
@@ -146,7 +144,7 @@ public partial class MainView : UserControl
SetLoadingScreenVisible(true); SetLoadingScreenVisible(true);
try try
{ {
await ServerSwitcher.SaveCfg(CmbServerSelection.SelectedIndex == 0, txtGamePath.Text, txtLauncherPath.Text, TxtIpAddress.Text, ChkOffline.IsChecked ?? false); await ServerSwitcher.SaveCfg(CmbServerSelection.SelectedIndex == 0, GamePath, LauncherPath, TxtIpAddress.Text, ChkOffline.IsChecked ?? false);
} }
catch (Exception ex) catch (Exception ex)
{ {
@@ -192,9 +190,14 @@ public partial class MainView : UserControl
// validate if the folder has game exe // validate if the folder has game exe
if (!string.IsNullOrEmpty(txtGamePath.Text)) if (!string.IsNullOrEmpty(txtGamePath.Text))
{ {
if (!File.Exists(Path.Combine(txtGamePath.Text, "nikke.exe"))) if (!File.Exists(Path.Combine(txtGamePath.Text, "game", "nikke.exe")))
{ {
ShowWarningMsg("Game path is invalid. Make sure that nikke.exe exists in the launcher folder", "Error"); ShowWarningMsg("Game path is invalid. Make sure that nikke.exe exists in the game folder", "Error");
return;
}
if (!File.Exists(Path.Combine(txtGamePath.Text, "Launcher", "nikke_launcher.exe")))
{
ShowWarningMsg("Game path is invalid. Make sure that nikke_launcher.exe exists in the Launcher folder", "Error");
return; return;
} }
} }
@@ -202,36 +205,7 @@ public partial class MainView : UserControl
} }
} }
} }
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;
}
}
UpdateIntegrityLabel();
}
}
}
private void GamePath_TextChanged(object? sender, Avalonia.Controls.TextChangedEventArgs e) private void GamePath_TextChanged(object? sender, Avalonia.Controls.TextChangedEventArgs e)
{ {
UpdateIntegrityLabel(); UpdateIntegrityLabel();

View File

@@ -6,6 +6,6 @@
xmlns:views="clr-namespace:ServerSelector.Views" xmlns:views="clr-namespace:ServerSelector.Views"
mc:Ignorable="d" d:DesignWidth="370" d:DesignHeight="300" mc:Ignorable="d" d:DesignWidth="370" d:DesignHeight="300"
x:Class="ServerSelector.Views.MainWindow" x:Class="ServerSelector.Views.MainWindow"
Title="Server Switcher" Width="370" Height="300" CanResize="False"> Title="Server Switcher" Width="370" Height="300" CanResize="True">
<views:MainView /> <views:MainView />
</Window> </Window>