Skip to content

Commit 2762864

Browse files
authored
Immediately hide launcher window when it is closing (SubnauticaNitrox#1354)
* Removed double import of MSTest.TestAdapter.targets in NitroxTest.csproj * Cleaned up how launcher exists Asked user if they are sure when embedded server is running. Fixed double close calling due to close event from WPF. * Immediately hide window when it is closing
1 parent d413589 commit 2762864

5 files changed

Lines changed: 28 additions & 41 deletions

File tree

NitroxLauncher/App.xaml.cs

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,19 +39,10 @@ protected override void OnStartup(StartupEventArgs e)
3939
base.OnStartup(e);
4040
}
4141

42-
protected override void OnExit(ExitEventArgs e)
43-
{
44-
MainWindow window = (MainWindow)Current.MainWindow;
45-
window?.CloseInternalServerAndRemovePatchAsync();
46-
47-
base.OnExit(e);
48-
}
49-
5042
private void Application_DispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
5143
{
52-
// If something went wrong. Close the server
53-
MainWindow window = (MainWindow)Current.MainWindow;
54-
window?.CloseInternalServerAndRemovePatchAsync();
44+
// If something went wrong. Close the server if embedded.
45+
LauncherLogic.Instance.Dispose();
5546
Log.Error(e.Exception.GetBaseException().ToString()); // Gets the exception that was unhandled, not the "dispatched unhandled" exception.
5647
MessageBox.Show(GetExceptionError(e.Exception), "Error", MessageBoxButton.OK, MessageBoxImage.Error);
5748
}

NitroxLauncher/LauncherLogic.cs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,12 @@ public LauncherLogic()
5454

5555
public void Dispose()
5656
{
57+
Application.Current.MainWindow?.Hide();
58+
if (isEmbedded)
59+
{
60+
Instance.SendServerCommand("stop\n");
61+
}
62+
5763
try
5864
{
5965
nitroxEntryPatch.Remove();
@@ -68,13 +74,13 @@ public void Dispose()
6874
serverProcess = null; // Indicate the process is dead now.
6975
}
7076

71-
public async Task WriteToServerAsync(string inputText)
77+
public void WriteToServer(string inputText)
7278
{
7379
if (ServerRunning)
7480
{
7581
try
7682
{
77-
await serverProcess.StandardInput.WriteLineAsync(inputText);
83+
serverProcess.StandardInput.WriteLine(inputText);
7884
}
7985
catch (Exception)
8086
{
@@ -262,14 +268,14 @@ internal Process StartServer(bool standalone)
262268
return serverProcess;
263269
}
264270

265-
internal async Task SendServerCommandAsync(string inputText)
271+
internal void SendServerCommand(string inputText)
266272
{
267273
if (!ServerRunning)
268274
{
269275
return;
270276
}
271277

272-
await WriteToServerAsync(inputText);
278+
WriteToServer(inputText);
273279
}
274280

275281
private void OnEndServer()

NitroxLauncher/MainWindow.xaml.cs

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -98,38 +98,30 @@ protected virtual void OnPropertyChanged([CallerMemberName] string propertyName
9898
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
9999
}
100100

101-
internal async Task CloseInternalServerAndRemovePatchAsync()
102-
{
103-
await LauncherLogic.Instance.SendServerCommandAsync("stop\n");
104-
logic.Dispose();
105-
}
106-
107101
private bool CanClose()
108102
{
109103
if (logic.ServerRunning && isServerEmbedded)
110104
{
111-
MessageBox.Show("The embedded server is still running.", "Close aborted", MessageBoxButton.OK, MessageBoxImage.Warning);
112-
return false;
105+
MessageBoxResult userAnswer = MessageBox.Show("The embedded server is still running. Click yes to close.", "Close aborted", MessageBoxButton.YesNo, MessageBoxImage.Warning);
106+
return userAnswer == MessageBoxResult.Yes;
113107
}
114108

115109
return true;
116110
}
117111

118-
private async void OnClosing(object sender, CancelEventArgs e)
112+
private void OnClosing(object sender, CancelEventArgs e)
119113
{
120114
if (!CanClose())
121115
{
122116
e.Cancel = true;
117+
return;
123118
}
124-
await CloseInternalServerAndRemovePatchAsync();
119+
logic.Dispose();
125120
}
126121

127122
private void Close_Click(object sender, RoutedEventArgs e)
128123
{
129-
if (CanClose())
130-
{
131-
Close();
132-
}
124+
Close();
133125
}
134126

135127
private void Minimze_Click(object sender, RoutedEventArgs e)

NitroxLauncher/Pages/ServerConsolePage.xaml.cs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
using System.Diagnostics;
55
using System.Linq;
66
using System.Runtime.CompilerServices;
7-
using System.Threading.Tasks;
87
using System.Windows;
98
using System.Windows.Input;
109
using NitroxLauncher.Events;
@@ -98,9 +97,9 @@ private void ServerDataReceived(object sender, DataReceivedEventArgs e)
9897
ServerOutput += e.Data + Environment.NewLine;
9998
}
10099

101-
private async Task SendCommandInputToServerAsync()
100+
private void SendCommandInputToServer()
102101
{
103-
await LauncherLogic.Instance.SendServerCommandAsync(CommandInputText);
102+
LauncherLogic.Instance.SendServerCommand(CommandInputText);
104103
ServerOutput += CommandInputText + Environment.NewLine;
105104

106105
// Deduplication of command history
@@ -116,29 +115,29 @@ private void HideCommandHistory()
116115
CommandHistoryIndex = commandLinesHistory.Count;
117116
}
118117

119-
private async void CommandButton_OnClick(object sender, RoutedEventArgs e)
118+
private void CommandButton_OnClick(object sender, RoutedEventArgs e)
120119
{
121-
await SendCommandInputToServerAsync();
120+
SendCommandInputToServer();
122121
}
123122

124-
private async void StopButton_Click(object sender, RoutedEventArgs e)
123+
private void StopButton_Click(object sender, RoutedEventArgs e)
125124
{
126125
// Suggest referencing NitroxServer.ConsoleCommands.ExitCommand.name, but the class is internal
127-
await LauncherLogic.Instance.SendServerCommandAsync("stop");
126+
LauncherLogic.Instance.SendServerCommand("stop");
128127
ServerOutput += $"stop{Environment.NewLine}";
129128

130129
commandLinesHistory.Add("stop");
131130
HideCommandHistory();
132131
}
133132

134-
private async void CommandLine_PreviewKeyDown(object sender, KeyEventArgs e)
133+
private void CommandLine_PreviewKeyDown(object sender, KeyEventArgs e)
135134
{
136135
e.Handled = true;
137136

138137
switch (e.Key)
139138
{
140139
case Key.Enter:
141-
await SendCommandInputToServerAsync();
140+
SendCommandInputToServer();
142141
break;
143142
case Key.Escape:
144143
HideCommandHistory();

NitroxTest/NitroxTest.csproj

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?xml version="1.0" encoding="utf-8"?>
1+
<?xml version="1.0" encoding="utf-8"?>
22
<Project ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
33
<PropertyGroup>
44
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
@@ -145,5 +145,4 @@
145145
<Error Condition="!Exists('$(PkgMSTest_TestAdapter)\build\net45\MSTest.TestAdapter.props')" Text="$([System.String]::Format('$(ErrorText)', '$(PkgMSTest_TestAdapter)\build\net45\MSTest.TestAdapter.props'))" />
146146
<Error Condition="!Exists('$(PkgMSTest_TestAdapter)\build\net45\MSTest.TestAdapter.targets')" Text="$([System.String]::Format('$(ErrorText)', '$(PkgMSTest_TestAdapter)\build\net45\MSTest.TestAdapter.targets'))" />
147147
</Target>
148-
<Import Project="$(PkgMSTest_TestAdapter)\build\net45\MSTest.TestAdapter.targets" Condition="Exists('$(PkgMSTest_TestAdapter)\build\net45\MSTest.TestAdapter.targets')" />
149-
</Project>
148+
</Project>

0 commit comments

Comments
 (0)