diff --git a/DiscordWebhookWindow.xaml b/DiscordWebhookWindow.xaml
index a302c7c..9a67e05 100644
--- a/DiscordWebhookWindow.xaml
+++ b/DiscordWebhookWindow.xaml
@@ -32,29 +32,29 @@
-
-
+
+
-
-
+
+
-
-
+
+
-
-
+
+
-
-
+
+
-
-
+
+
-
-
+
+
-
-
+
+
diff --git a/DiscordWebhookWindow.xaml.cs b/DiscordWebhookWindow.xaml.cs
index 5f00ec0..18492ad 100644
--- a/DiscordWebhookWindow.xaml.cs
+++ b/DiscordWebhookWindow.xaml.cs
@@ -32,24 +32,24 @@ public DiscordWebhookWindow(ValheimServer server)
txtWebhook.Text = _server.DiscordWebhook;
- ContextMenu contextMenu = new ContextMenu();
- MenuItem defaultMenu = new MenuItem();
+ var contextMenu = new ContextMenu();
+ var defaultMenu = new MenuItem();
contextMenu.Items.Add(defaultMenu);
defaultMenu.Header = "Reset to default";
defaultMenu.Click += DefaultMenu_Click;
- messageControls = new List();
- messageControls.Add(OnPlayerConnected);
- messageControls.Add(OnPlayerDisconnected);
- messageControls.Add(OnPlayerDied);
- messageControls.Add(OnFailedPassword);
- messageControls.Add(OnRandomServerEvent);
- messageControls.Add(OnStarted);
- messageControls.Add(OnStartFailed);
- messageControls.Add(OnServerExited);
- foreach (TextBox textBox in messageControls)
+ messageControls = new();
+ messageControls.Add(txtOnPlayerConnected);
+ messageControls.Add(txtOnPlayerDisconnected);
+ messageControls.Add(txtOnPlayerDied);
+ messageControls.Add(txtOnFailedPassword);
+ messageControls.Add(txtOnRandomServerEvent);
+ messageControls.Add(txtOnStarted);
+ messageControls.Add(txtOnStartFailed);
+ messageControls.Add(txtOnServerExited);
+ foreach (var textBox in messageControls)
{
- textBox.Text = server.GetWebhookMessage(textBox.Name);
+ textBox.Text = server.GetWebhookMessage(textBox.Tag.ToString());
textBox.ContextMenu = contextMenu;
textBox.ContextMenuOpening += TextBox_ContextMenuOpening;
}
@@ -62,66 +62,36 @@ private void TextBox_ContextMenuOpening(object sender, ContextMenuEventArgs e)
private void DefaultMenu_Click(object sender, RoutedEventArgs e)
{
- clickedTextBox.Text = _server.DefaultWebhookMessages[clickedTextBox.Name];
- }
-
- private void btnTestConnected_Click(object sender, RoutedEventArgs e)
- {
- _server.SendDiscordWebhook(OnPlayerConnected.Name, new Player("Bjorn", "123456789101112"));
- }
-
- private void btnTestDisconnected_Click(object sender, RoutedEventArgs e)
- {
- _server.SendDiscordWebhook(OnPlayerDisconnected.Name, new Player("Bjorn", "123456789101112"));
- }
-
- private void btnTestDied_Click(object sender, RoutedEventArgs e)
- {
- _server.SendDiscordWebhook(OnPlayerDied.Name, new Player("Bjorn", "123456789101112"));
- }
-
- private void btnFailedPassword_Click(object sender, RoutedEventArgs e)
- {
- _server.SendDiscordWebhook(OnFailedPassword.Name, new Player("Bjorn", "123456789101112"));
- }
-
- private void btnRandomServerEvent_Click(object sender, RoutedEventArgs e)
- {
- _server.SendDiscordWebhook(OnRandomServerEvent.Name, null, "army_bonemass");
+ clickedTextBox.Text = ValheimServer.DiscordWebhookDefaultMessages[clickedTextBox.Tag.ToString()];
}
private void btnSave_Click(object sender, RoutedEventArgs e)
{
_server.DiscordWebhook = txtWebhook.Text;
- foreach (TextBox textBox in messageControls)
+ foreach (var textBox in messageControls)
{
- if (textBox.Text != null && _server.DefaultWebhookMessages.ContainsKey(textBox.Name) && textBox.Text != this._server.DefaultWebhookMessages[textBox.Name])
+ var webHookName = textBox.Tag.ToString();
+ if (textBox.Text != null && ValheimServer.DiscordWebhookDefaultMessages.ContainsKey(webHookName) && textBox.Text != ValheimServer.DiscordWebhookDefaultMessages[webHookName])
{
- _server.DiscordWebhookMessages[textBox.Name] = textBox.Text;
+ _server.DiscordWebhookMessages[webHookName] = textBox.Text;
}
- else if (_server.DefaultWebhookMessages.ContainsKey(textBox.Name) && textBox.Text == this._server.DefaultWebhookMessages[textBox.Name])
+ else if (ValheimServer.DiscordWebhookDefaultMessages.ContainsKey(webHookName) && textBox.Text == ValheimServer.DiscordWebhookDefaultMessages[webHookName])
{
- if (_server.DiscordWebhookMessages.ContainsKey(textBox.Name))
+ if (_server.DiscordWebhookMessages.ContainsKey(webHookName))
{
- _server.DiscordWebhookMessages.Remove(textBox.Name);
+ _server.DiscordWebhookMessages.Remove(webHookName);
}
}
}
DialogResult = true;
Close();
}
-
- private void btnTestServerStarted_Click(object sender, RoutedEventArgs e)
- {
- _server.SendDiscordWebhook(OnStarted.Name, null, null);
- }
- private void btnTestServerStartFailed_Click(object sender, RoutedEventArgs e)
+ private void btnTestWebhook_Click(object sender, RoutedEventArgs e)
{
- _server.SendDiscordWebhook(OnStartFailed.Name, null, null);
- }
- private void btnTestServerStopped_Click(object sender, RoutedEventArgs e)
- {
- _server.SendDiscordWebhook(OnStarted.Name, null, null);
+ var oldUrl = _server.DiscordWebhook;
+ _server.DiscordWebhook = txtWebhook.Text;
+ _server.SendDiscordWebhook(((Button)sender).Tag.ToString(), new Player("Bjorn", "123456789101112"), "army_bonemass");
+ _server.DiscordWebhook = oldUrl;
}
private void btnCancel_Click(object sender, RoutedEventArgs e)
diff --git a/InstallSteamCmdWindow.xaml.cs b/InstallSteamCmdWindow.xaml.cs
index 6bcb7e5..f873494 100644
--- a/InstallSteamCmdWindow.xaml.cs
+++ b/InstallSteamCmdWindow.xaml.cs
@@ -126,7 +126,7 @@ private void txtServerPath_MouseLeftButtonUp(object sender, MouseButtonEventArgs
private void btnInstallServer_Click(object sender, RoutedEventArgs e)
{
- if (!File.Exists(txtServerPath.Text+"\\valheim_server.exe"))
+ if (!File.Exists($@"{txtServerPath.Text}\{ValheimServer.ExecutableName}"))
{
btnInstallServer.IsEnabled = false;
txtServerPath.MouseLeftButtonUp -= txtServerPath_MouseLeftButtonUp;
@@ -142,7 +142,7 @@ private void btnInstallServer_Click(object sender, RoutedEventArgs e)
else
{
Properties.Settings.Default.ServerInstallType = (int)ValheimServer.ServerInstallMethod.SteamCMD;
- Properties.Settings.Default.ServerFilePath = txtServerPath.Text + "\\valheim_server.exe";
+ Properties.Settings.Default.ServerFilePath = txtServerPath.Text + $@"\{ValheimServer.ExecutableName}";
Properties.Settings.Default.SteamCMDPath = txtSteamCMDPath.Text;
Properties.Settings.Default.Save();
DialogResult = true;
@@ -154,10 +154,10 @@ private void SteamCmdProcess_Exited(object sender, EventArgs e)
{
this.Dispatcher.Invoke(() =>
{
- if (File.Exists(txtServerPath.Text + "\\valheim_server.exe"))
+ if (File.Exists($@"{txtServerPath.Text}\{ValheimServer.ExecutableName}"))
{
Properties.Settings.Default.ServerInstallType = (int)ValheimServer.ServerInstallMethod.SteamCMD;
- Properties.Settings.Default.ServerFilePath = txtServerPath.Text + "\\valheim_server.exe";
+ Properties.Settings.Default.ServerFilePath = $@"{txtServerPath.Text}\{ValheimServer.ExecutableName}";
Properties.Settings.Default.SteamCMDPath = txtSteamCMDPath.Text;
Properties.Settings.Default.Save();
DialogResult = true;
diff --git a/MainWindow.xaml.cs b/MainWindow.xaml.cs
index 89d4b62..98afdf2 100644
--- a/MainWindow.xaml.cs
+++ b/MainWindow.xaml.cs
@@ -32,7 +32,7 @@ namespace ValheimServerWarden
///
public partial class MainWindow : Window
{
- private List servers;
+ //private List servers;
private bool suppressLog = false;
private bool editing = false;
private System.Windows.Forms.NotifyIcon notifyIcon;
@@ -112,7 +112,7 @@ public MainWindow()
notifyIcon.ContextMenuStrip = cm;
storedWindowState = WindowState.Normal;
- servers = new List();
+ //servers = new List();
if (File.Exists(this.ServerJsonPath))
{
try
@@ -121,10 +121,10 @@ public MainWindow()
foreach (ValheimServer s in savedServers)
{
attachServerEventListeners(s);
- servers.Add(s);
+ //servers.Add(s);
if (s.Autostart)
{
- StartServer(s);
+ s.Start();
}
}
}
@@ -133,14 +133,14 @@ public MainWindow()
logMessage($"Error reading saved servers: {ex.Message}", LogEntryType.Error);
}
}
- dgServers.ItemsSource = servers;
+ dgServers.ItemsSource = ValheimServer.Servers;//servers;
RefreshDataGrid();
checkForRunningServers();
}
private void NotifyMenuQuit_Click(object sender, EventArgs e)
{
- foreach (var server in servers)
+ foreach (var server in ValheimServer.Servers)
{
if (server.Running)
{
@@ -281,7 +281,7 @@ private void CheckServerPath()
logMessage("Valid path for Valheim dedicated server not set.");
string steampath = @"Program Files (x86)\Steam\steam.exe";
string fullSteamPath = "";
- string filePath = @"Program Files (x86)\Steam\steamapps\common\Valheim dedicated server\valheim_server.exe";
+ string filePath = $@"Program Files (x86)\Steam\steamapps\common\Valheim dedicated server\{ValheimServer.ExecutableName}";
bool serverfound = false;
DriveInfo[] drives = DriveInfo.GetDrives();
foreach (DriveInfo drive in drives)
@@ -379,10 +379,10 @@ private void btnServerPath_Click(object sender, RoutedEventArgs e)
{
return;
}*/
- if (!File.Exists($@"{folderName}\valheim_server.exe") && cmbServerType.SelectedIndex == (int)ValheimServer.ServerInstallMethod.SteamCMD && File.Exists(Properties.Settings.Default.SteamCMDPath))
+ if (!File.Exists($@"{folderName}\{ValheimServer.ExecutableName}") && cmbServerType.SelectedIndex == (int)ValheimServer.ServerInstallMethod.SteamCMD && File.Exists(Properties.Settings.Default.SteamCMDPath))
{
var mmb = new ModernMessageBox(this);
- var install = mmb.Show($"valheim_server.exe was not found in {folderName}, do you want to install it via SteamCMD?",
+ var install = mmb.Show($"{ValheimServer.ExecutableName} was not found in {folderName}, do you want to install it via SteamCMD?",
"Install Valheim dedicated server?", MessageBoxButton.YesNo, MessageBoxImage.Question, MessageBoxResult.No);
if (install == MessageBoxResult.Yes)
{
@@ -402,7 +402,7 @@ private void btnServerPath_Click(object sender, RoutedEventArgs e)
}
}
}
- folderName += "\\valheim_server.exe";
+ folderName += $@"\{ValheimServer.ExecutableName}";
txtServerPath.Text = folderName;
Properties.Settings.Default.ServerFilePath = folderName;
Properties.Settings.Default.Save();
@@ -456,7 +456,7 @@ private void Window_Closed(object sender, EventArgs e)
SaveServers();
notifyIcon.Dispose();
notifyIcon = null;
- foreach (ValheimServer server in servers)
+ foreach (ValheimServer server in ValheimServer.Servers)
{
if (server.Status == ValheimServer.ServerStatus.Running || server.Status == ValheimServer.ServerStatus.Starting)
{
@@ -477,7 +477,7 @@ private void SaveServers()
{
try
{
- File.WriteAllTextAsync(this.ServerJsonPath, JsonSerializer.Serialize(servers));
+ File.WriteAllTextAsync(this.ServerJsonPath, JsonSerializer.Serialize(ValheimServer.Servers));
}
catch (Exception ex)
{
@@ -497,9 +497,11 @@ private void attachServerEventListeners(ValheimServer server)
logMessage(server.DisplayName+": "+e.LogEntry.Message, e.LogEntry.Type);
});
});
- server.Exited += Server_Exited;
+ server.Stopped += Server_Stopped;
server.Started += Server_Started;
+ server.Starting += Server_StartingStopping;
server.StartFailed += Server_StartFailed;
+ server.Stopping += Server_StartingStopping;
server.StopFailed += Server_StopFailed;
server.PlayerConnected += Server_PlayerConnected;
server.PlayerDisconnected += Server_PlayerDisconnected;
@@ -509,7 +511,15 @@ private void attachServerEventListeners(ValheimServer server)
logMessage($"Error attaching event listeners: {ex.Message}", LogEntryType.Error);
}
}
-
+ private void Server_StartingStopping(object sender, EventArgs e)
+ {
+ this.Dispatcher.Invoke(() =>
+ {
+ dgServers.CancelEdit();
+ dgServers.IsReadOnly = true;
+ RefreshDataGrid();
+ });
+ }
private void Server_StopFailed(object sender, ServerErrorEventArgs e)
{
try
@@ -517,6 +527,7 @@ private void Server_StopFailed(object sender, ServerErrorEventArgs e)
var server = (ValheimServer)sender;
this.Dispatcher.Invoke(() =>
{
+ dgServers.IsReadOnly = false;
RefreshDataGrid();
});
}
@@ -533,6 +544,7 @@ private void Server_StartFailed(object sender, ServerErrorEventArgs e)
var server = (ValheimServer)sender;
this.Dispatcher.Invoke(() =>
{
+ dgServers.IsReadOnly = false;
RefreshDataGrid();
});
}
@@ -596,7 +608,7 @@ private void serversMenuAdd_Click(object sender, RoutedEventArgs e)
ValheimServer s = new ValheimServer();
s.InstallMethod = (ValheimServer.ServerInstallMethod)Properties.Settings.Default.ServerInstallType;
attachServerEventListeners(s);
- servers.Add(s);
+ //servers.Add(s);
RefreshDataGrid();
dgServers.SelectedItem = s;
dgServers.BeginEdit();
@@ -618,11 +630,11 @@ private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs
base.OnClosing(e);*/
try
{
- foreach (ValheimServer server in servers)
+ foreach (ValheimServer server in ValheimServer.Servers)
{
if (server.Running)
{
- logMessage($"Server {server.Name} is still running. Please stop all servers before exiting.", LogEntryType.Error);
+ logMessage($"Server {server.DisplayName} is still running. Please stop all servers before exiting.", LogEntryType.Error);
e.Cancel = true;
}
else
@@ -699,50 +711,12 @@ private void dgServers_RowEditEnding(object sender, DataGridRowEditEndingEventAr
}
}
- private void StartServer(ValheimServer server)
- {
- try
- {
- if (server == null) return;
- if (server.Running)
- {
- logMessage($"Server {server.Name} is alread running.", LogEntryType.Error);
- return;
- }
- foreach (ValheimServer s in servers)
- {
- if (s.Running)
- {
- IEnumerable range = Enumerable.Range(s.Port, 2);
- if (range.Contains(server.Port) || range.Contains(server.Port + 1))
- {
- logMessage($"Server {s.Name} is already running on conflicting port {s.Port}.", LogEntryType.Error);
- return;
- }
- if (s.World.Equals(server.World))
- {
- logMessage($"Server {s.Name} is already running using world {s.World}.", LogEntryType.Error);
- return;
- }
- }
- }
- dgServers.CancelEdit();
- dgServers.IsReadOnly = true;
- server.Start();
- RefreshDataGrid();
- }
- catch (Exception ex)
- {
- logMessage($"Error starting server: {ex.Message}", LogEntryType.Error);
- }
- }
-
private void serversMenuStart_Click(object sender, RoutedEventArgs e)
{
try
{
ValheimServer server = ((ValheimServer)dgServers.SelectedItem);
- StartServer(server);
+ server.Start();
}
catch (Exception ex)
{
@@ -755,27 +729,7 @@ private void serversMenuStop_Click(object sender, RoutedEventArgs e)
try
{
ValheimServer server = ((ValheimServer)dgServers.SelectedItem);
- StopServer(server);
- }
- catch (Exception ex)
- {
- logMessage($"Error stopping server: {ex.Message}", LogEntryType.Error);
- }
- }
-
- private void StopServer(ValheimServer server)
- {
- try
- {
- if (server == null) return;
- if (!server.Running)
- {
- return;
- }
- dgServers.CancelEdit();
- dgServers.IsReadOnly = true;
server.Stop();
- RefreshDataGrid();
}
catch (Exception ex)
{
@@ -783,7 +737,7 @@ private void StopServer(ValheimServer server)
}
}
- private void Server_Exited(object sender, ServerExitedEventArgs e)
+ private void Server_Stopped(object sender, ServerStoppedEventArgs e)
{
try
{
@@ -792,10 +746,6 @@ private void Server_Exited(object sender, ServerExitedEventArgs e)
{
dgServers.IsReadOnly = false;
RefreshDataGrid();
- if (server.Autostart && e.ExitCode != 0 && e.ExitCode != -1073741510 && !e.IntentionalExit && !server.Running)
- {
- server.Start();
- }
});
}
catch (Exception ex)
@@ -878,19 +828,6 @@ private void Window_StateChanged(object sender, EventArgs e)
storedWindowState = WindowState;
}
}
-
- private void menuEditPreferences_Click(object sender, RoutedEventArgs e)
- {
- try
- {
- tabsMain.SelectedIndex = 1;
- }
- catch (Exception ex)
- {
- logMessage($"Error opening preferences: {ex.Message}", LogEntryType.Error);
- }
- }
-
private void dgServers_BeginningEdit(object sender, DataGridBeginningEditEventArgs e)
{
editing = true;
@@ -919,21 +856,16 @@ private void serversMenuRemove_Click(object sender, RoutedEventArgs e)
{
ValheimServer server = (ValheimServer)dgServers.SelectedItem;
var mmb = new ModernMessageBox(this);
- var confirmResult = mmb.Show($"Are you sure you want to remove {server.Name}?",
+ var confirmResult = mmb.Show($"Are you sure you want to remove {server.DisplayName}?",
"Remove Server", MessageBoxButton.YesNo, MessageBoxImage.Question, MessageBoxResult.No);
if (confirmResult == MessageBoxResult.Yes)
{
- servers.Remove(server);
+ //servers.Remove(server);
+ server.Dispose();
RefreshDataGrid();
}
}
-
- private void menuFileExit_Click(object sender, RoutedEventArgs e)
- {
- this.Close();
- }
-
private void radThemeDark_Checked(object sender, RoutedEventArgs e)
{
if (!Properties.Settings.Default.AppTheme.Equals("Dark"))
@@ -1091,14 +1023,6 @@ private void serversMenuDetails_Click(object sender, RoutedEventArgs e)
{
this.serverDetailWindows.Remove(window);
});
- window.Starting += ((object sender, ServerEventArgs e) =>
- {
- this.StartServer(e.Server);
- });
- window.Stopping += ((object sender, ServerEventArgs e) =>
- {
- this.StopServer(e.Server);
- });
window.ShowLog += ((object sender, ServerEventArgs e) =>
{
ShowServerLog(e.Server);
@@ -1139,7 +1063,7 @@ private void ShowServerLog(ValheimServer server)
}
catch (Exception ex)
{
- logMessage($"Error showing {server.Name} server log: {ex.Message}", LogEntryType.Error);
+ logMessage($"Error showing {server.DisplayName} server log: {ex.Message}", LogEntryType.Error);
}
}
diff --git a/ServerDetailsWindow.xaml b/ServerDetailsWindow.xaml
index 0b96302..0b965d0 100644
--- a/ServerDetailsWindow.xaml
+++ b/ServerDetailsWindow.xaml
@@ -244,15 +244,20 @@
+
+
+
+
+
@@ -266,6 +271,17 @@
+
+
+
+
+
+
+
+
+
+
+
diff --git a/ServerDetailsWindow.xaml.cs b/ServerDetailsWindow.xaml.cs
index baae2e9..fca4428 100644
--- a/ServerDetailsWindow.xaml.cs
+++ b/ServerDetailsWindow.xaml.cs
@@ -27,8 +27,8 @@ namespace ValheimServerWarden
///
public partial class ServerDetailsWindow : Window
{
- public event EventHandler Starting;
- public event EventHandler Stopping;
+ //public event EventHandler Starting;
+ //public event EventHandler Stopping;
public event EventHandler EditedServer;
public event EventHandler ShowLog;
private ValheimServer _server;
@@ -88,6 +88,9 @@ public ServerDetailsWindow(ValheimServer server)
txtServerLog.Document.Blocks.Add((Paragraph)entry);
}
}
+ cmbPriority.Items.Add(ProcessPriorityClass.Normal);
+ cmbPriority.Items.Add(ProcessPriorityClass.AboveNormal);
+ cmbPriority.Items.Add(ProcessPriorityClass.High);
}
public void RefreshControls()
@@ -159,11 +162,13 @@ public void RefreshControls()
{
btnSteamCmd.Visibility = Visibility.Visible;
chkUpdateOnRestart.Visibility = Visibility.Visible;
+ gridAutoUpdate.Visibility = Visibility.Visible;
}
else
{
btnSteamCmd.Visibility = Visibility.Collapsed;
chkUpdateOnRestart.Visibility = Visibility.Collapsed;
+ gridAutoUpdate.Visibility = Visibility.Collapsed;
}
}
catch (Exception ex)
@@ -172,6 +177,22 @@ public void RefreshControls()
}
});
}
+ private void RefreshControls(object sender, EventArgs e)
+ {
+ RefreshControls();
+ }
+ private void RefreshControls(object sender, ServerStoppedEventArgs e)
+ {
+ RefreshControls();
+ }
+ private void RefreshControls(object sender, PlayerEventArgs e)
+ {
+ RefreshControls();
+ }
+ private void RefreshControls(object sender, ServerErrorEventArgs e)
+ {
+ RefreshControls();
+ }
private void attachServerEventHandlers()
{
Server.LoggedMessage += ((object sender, LoggedMessageEventArgs e) => {
@@ -180,42 +201,16 @@ private void attachServerEventHandlers()
LogMessage(e.LogEntry);
});
});
- Server.Exited += ((object sender, ServerExitedEventArgs e) =>
- {
- RefreshControls();
- });
- Server.PlayerConnected += ((object sender, PlayerEventArgs e) =>
- {
- RefreshControls();
- });
- Server.PlayerDisconnected += ((object sender, PlayerEventArgs e) =>
- {
- RefreshControls();
- });
- Server.PlayerDied += ((object sender, PlayerEventArgs e) =>
- {
- RefreshControls();
- });
- Server.FailedPassword += ((object sender, FailedPasswordEventArgs e) =>
- {
- RefreshControls();
- });
- Server.Starting += ((object sender, EventArgs e) =>
- {
- RefreshControls();
- });
- Server.Stopping += ((object sender, EventArgs e) =>
- {
- RefreshControls();
- });
- Server.Started += ((object sender, EventArgs e) =>
- {
- RefreshControls();
- });
- Server.StartFailed += ((object sender, ServerErrorEventArgs e) =>
- {
- RefreshControls();
- });
+ Server.Stopped += RefreshControls;
+ Server.PlayerConnected += RefreshControls;
+ Server.PlayerDisconnected += RefreshControls;
+ Server.PlayerDied += RefreshControls;
+ Server.FailedPassword += RefreshControls;
+ Server.Starting += RefreshControls;
+ Server.Stopping += RefreshControls;
+ Server.Started += RefreshControls;
+ Server.StartFailed += RefreshControls;
+ Server.StopFailed += RefreshControls;
Server.Updated += ((object sender, UpdatedEventArgs e) =>
{
RefreshControls();
@@ -257,6 +252,13 @@ private void ServerToControls()
txtRestartInterval.IsEnabled = false;
}
chkUpdateOnRestart.IsChecked = Server.UpdateOnRestart;
+ if (Server.UpdateCheckMinutes > 0)
+ {
+ chkAutoUpdate.IsChecked = true;
+ txtUpdateCheckInterval.Text = Server.UpdateCheckMinutes.ToString();
+ txtUpdateCheckInterval.IsEnabled = true;
+ }
+ cmbPriority.SelectedItem = Server.ProcessPriority;
}
catch (Exception ex)
{
@@ -320,9 +322,10 @@ private void btnStart_Click(object sender, RoutedEventArgs e)
{
//btnStart.IsEnabled = false;
//btnStart.Content = FindResource("StartGrey");
- OnStarting(new ServerEventArgs(this.Server));
+ //OnStarting(new ServerEventArgs(this.Server));
+ Server.Start();
}
- private void OnStarting(ServerEventArgs args)
+ /*private void OnStarting(ServerEventArgs args)
{
EventHandler handler = Starting;
if (null != handler) handler(this, args);
@@ -331,7 +334,7 @@ private void OnStopping(ServerEventArgs args)
{
EventHandler handler = Stopping;
if (null != handler) handler(this, args);
- }
+ }*/
private void OnShowLog(ServerEventArgs args)
{
EventHandler handler = ShowLog;
@@ -347,7 +350,8 @@ private void btnStop_Click(object sender, RoutedEventArgs e)
{
//btnStop.IsEnabled = false;
//btnStop.Content = FindResource("StopGrey");
- OnStopping(new ServerEventArgs(this.Server));
+ //OnStopping(new ServerEventArgs(this.Server));
+ Server.Stop();
}
private void btnLog_Click(object sender, RoutedEventArgs e)
@@ -408,6 +412,7 @@ private void btnSave_Click(object sender, RoutedEventArgs e)
Server.InstallMethod = (ValheimServer.ServerInstallMethod)cmbServerType.SelectedIndex;
Server.Autostart = chkAutostart.IsChecked.GetValueOrDefault();
Server.RawLog = chkRawLog.IsChecked.GetValueOrDefault();
+ Server.ProcessPriority = (ProcessPriorityClass)cmbPriority.SelectedItem;
int restartHours = Server.RestartHours;
if (chkAutoRestart.IsChecked.GetValueOrDefault())
{
@@ -427,6 +432,24 @@ private void btnSave_Click(object sender, RoutedEventArgs e)
chkAutoRestart.IsChecked = false;
}
Server.UpdateOnRestart = chkUpdateOnRestart.IsChecked.GetValueOrDefault();
+ int updateCheckMinutes = Server.UpdateCheckMinutes;
+ if (chkAutoUpdate.IsChecked.GetValueOrDefault())
+ {
+ int.TryParse(txtUpdateCheckInterval.Text, out updateCheckMinutes);
+ }
+ else
+ {
+ updateCheckMinutes = 0;
+ }
+ if (updateCheckMinutes > -1)
+ {
+ Server.UpdateCheckMinutes = updateCheckMinutes;
+ }
+ if (updateCheckMinutes == 0)
+ {
+ txtUpdateCheckInterval.Text = "";
+ chkAutoUpdate.IsChecked = false;
+ }
OnEditedServer(new ServerEventArgs(this.Server));
RefreshControls();
if (Server.Running)
@@ -508,6 +531,10 @@ private void menuConnectLink_Click(object sender, RoutedEventArgs e)
private void chkAutoRestart_Checked(object sender, RoutedEventArgs e)
{
txtRestartInterval.IsEnabled = chkAutoRestart.IsChecked.GetValueOrDefault();
+ if (chkAutoRestart.IsChecked.GetValueOrDefault() && txtRestartInterval.Text == "")
+ {
+ txtRestartInterval.Text = "4";
+ }
}
private void btnDiscordWebhook_Click(object sender, RoutedEventArgs e)
@@ -596,10 +623,10 @@ private void btnServerDir_Click(object sender, RoutedEventArgs e)
{
return;
}*/
- if (!File.Exists($@"{folderName}\valheim_server.exe") && cmbServerType.SelectedIndex == (int)ValheimServer.ServerInstallMethod.SteamCMD && File.Exists(Properties.Settings.Default.SteamCMDPath))
+ if (!File.Exists($@"{folderName}\{ValheimServer.ExecutableName}") && cmbServerType.SelectedIndex == (int)ValheimServer.ServerInstallMethod.SteamCMD && File.Exists(Properties.Settings.Default.SteamCMDPath))
{
var mmb = new ModernMessageBox(this);
- var install = mmb.Show("valheim_server.exe was not found in this folder, do you want to install it via SteamCMD?",
+ var install = mmb.Show($"{ValheimServer.ExecutableName} was not found in this folder, do you want to install it via SteamCMD?",
"Install Valheim dedicated server?", MessageBoxButton.YesNo, MessageBoxImage.Question, MessageBoxResult.No);
if (install == MessageBoxResult.Yes)
{
@@ -619,7 +646,7 @@ private void btnServerDir_Click(object sender, RoutedEventArgs e)
}
}
}
- folderName += "\\valheim_server.exe";
+ folderName += $@"\{ValheimServer.ExecutableName}";
txtServerDir.Text = folderName;
}
RefreshControls();
@@ -757,6 +784,15 @@ private void menuSteamCmdCheckUpdate_Click(object sender, RoutedEventArgs e)
{
Server.CheckForUpdate();
}
+
+ private void chkAutoUpdate_Checked(object sender, RoutedEventArgs e)
+ {
+ txtUpdateCheckInterval.IsEnabled = chkAutoUpdate.IsChecked.GetValueOrDefault();
+ if (chkAutoUpdate.IsChecked.GetValueOrDefault() && txtUpdateCheckInterval.Text == "")
+ {
+ txtUpdateCheckInterval.Text = "20";
+ }
+ }
}
public class ServerEventArgs : EventArgs
diff --git a/ValheimServer.cs b/ValheimServer.cs
index bb73d19..2289980 100644
--- a/ValheimServer.cs
+++ b/ValheimServer.cs
@@ -16,7 +16,7 @@
namespace ValheimServerWarden
{
- public class ValheimServer : IEditableObject, IComparable
+ public class ValheimServer : IEditableObject, IComparable, IDisposable
{
public enum ServerStatus
{
@@ -32,6 +32,20 @@ public enum ServerInstallMethod
Steam,
SteamCMD
}
+ public static List Servers { get; } = new List();
+ public static string DefaultSaveDir { get { return $@"{Environment.GetFolderPath(Environment.SpecialFolder.UserProfile)}\AppData\LocalLow\IronGate\Valheim"; } }
+ public static string ExecutableName { get { return "valheim_server.exe"; } }
+ public static Dictionary DiscordWebhookDefaultMessages { get; } = new Dictionary {
+ {"OnStarted", "Server {Server.Name} has started." },
+ {"OnStartFailed", "Server {Server.Name} failed to start." },
+ {"OnServerExited", "Server {Server.Name} has stopped." },
+ {"OnFailedPassword", "User with SteamID {Player.SteamID} tried to join with an invalid password." },
+ {"OnPlayerConnected", "{Player.Name} has entered the fray!" },
+ {"OnPlayerDisconnected", "{Player.Name} has departed." },
+ {"OnPlayerDied", "{Player.Name} met an untimely demise." },
+ {"OnRandomServerEvent", "An {EventName} attack is underway!" }
+ };
+ public static string SteamID { get { return "896660"; } }
struct ServerData
{
internal string name;
@@ -44,7 +58,9 @@ struct ServerData
internal bool rawlog;
internal int restartHours;
internal bool updateOnRestart;
+ internal int updateCheckMinutes;
internal string discordWebhook;
+ internal ProcessPriorityClass processPriority;
}
public event EventHandler Updated;
public event EventHandler FailedPassword;
@@ -57,12 +73,13 @@ struct ServerData
public event EventHandler StartFailed;
public event EventHandler StopFailed;
public event EventHandler ScheduledRestart;
- public event EventHandler Exited;
+ public event EventHandler AutomaticUpdate;
+ public event EventHandler Stopped;
public event EventHandler Stopping;
- public event EventHandler ExitedUnexpectedly;
+ public event EventHandler StoppedUnexpectedly;
public event EventHandler ErrorOccurred;
public event EventHandler CheckedForUpdate;
- public event EventHandler UpdateComplete;
+ public event EventHandler UpdateEnded;
public event DataReceivedEventHandler OutputDataReceived
{
add
@@ -86,8 +103,6 @@ public event DataReceivedEventHandler ErrorDataReceived
}
}
public event EventHandler LoggedMessage;
- public static readonly string DefaultSaveDir = $@"{Environment.GetFolderPath(Environment.SpecialFolder.UserProfile)}\AppData\LocalLow\IronGate\Valheim";
- private string serverExe = "valheim_server.exe";
private bool testMode = false;
private ServerData data;
private Dictionary _discordWebhookMesssages;
@@ -99,24 +114,15 @@ public event DataReceivedEventHandler ErrorDataReceived
private bool intentionalExit;
private List connectingSteamIds;
private bool needsRestart;
+ private bool needsUpdate;
private bool scheduledRestart;
+ private bool automaticUpdate;
private System.Timers.Timer restartTimer;
+ private System.Timers.Timer updateTimer;
private List logEntries;
private bool inTxn = false;
private int stopAttempts;
-
- private static Dictionary _discordWebhookDefaultMessages = new Dictionary {
- {"OnStarted", "Server {Server.Name} has started." },
- {"OnStartFailed", "Server {Server.Name} failed to start." },
- {"OnServerExited", "Server {Server.Name} has stopped." },
- {"OnFailedPassword", "User with SteamID {Player.SteamID} tried to join with an invalid password." },
- {"OnPlayerConnected", "{Player.Name} has entered the fray!" },
- {"OnPlayerDisconnected", "{Player.Name} has departed." },
- {"OnPlayerDied", "{Player.Name} met an untimely demise." },
- {"OnRandomServerEvent", "An {EventName} attack is underway!" }
- };
-
- public static string SteamID { get { return "896660"; } }
+ private bool disposed = false;
public string Name
{
@@ -227,6 +233,27 @@ public int RestartHours
}
}
}
+ public int UpdateCheckMinutes
+ {
+ get
+ {
+ return this.data.updateCheckMinutes;
+ }
+ set
+ {
+ this.data.updateCheckMinutes = value;
+ if (value > 0 && (this.Status == ServerStatus.Running || this.status == ServerStatus.Starting))
+ {
+ updateTimer.Interval = this.GetMilisecondsUntilUpdateCheck();
+ updateTimer.Enabled = true;
+ updateTimer.Start();
+ }
+ else
+ {
+ restartTimer.Enabled = false;
+ }
+ }
+ }
public bool UpdateOnRestart
{
get
@@ -261,6 +288,17 @@ public Dictionary DiscordWebhookMessages
}
}
public string InstallPath { get; set; }
+ public ProcessPriorityClass ProcessPriority
+ {
+ get
+ {
+ return this.data.processPriority;
+ }
+ set
+ {
+ this.data.processPriority = value;
+ }
+ }
public ServerInstallMethod InstallMethod { get; set; }
[JsonIgnore]
public bool Running
@@ -308,14 +346,6 @@ public DateTime StartTime
get { return this.startTime; }
}
[JsonIgnore]
- public Dictionary DefaultWebhookMessages
- {
- get
- {
- return _discordWebhookDefaultMessages;
- }
- }
- [JsonIgnore]
public List LogEntries
{
get
@@ -340,7 +370,7 @@ public string DisplayName
}
}
}
- public ValheimServer(string name, int port, string world, string password, bool pubserver, bool autostart, bool rawlog, int restarthours, bool updateonrestart, string discordwebhook, Dictionary discordmessages, ServerInstallMethod install, string instpath)
+ public ValheimServer(string name, int port, string world, string password, bool pubserver, bool autostart, bool rawlog, int restarthours, bool updateonrestart, int updatecheckminutes, string discordwebhook, Dictionary discordmessages, ServerInstallMethod install, string instpath, ProcessPriorityClass processpriority)
{
this.data.name = name;
this.data.port = 2456;
@@ -353,6 +383,7 @@ public ValheimServer(string name, int port, string world, string password, bool
this.data.restartHours = restarthours;
this.data.updateOnRestart = updateonrestart;
this.data.discordWebhook = discordwebhook;
+ this.data.processPriority = processpriority;
this._discordWebhookMesssages = discordmessages;
InstallMethod = install;
InstallPath = instpath;
@@ -372,21 +403,54 @@ public ValheimServer(string name, int port, string world, string password, bool
restartTimer.AutoReset = false;
restartTimer.Elapsed += RestartTimer_Elapsed;
+ updateTimer = new System.Timers.Timer();
+ updateTimer.AutoReset = false;
+ updateTimer.Elapsed += UpdateTimer_Elapsed;
+
this.players = new PlayerList();
this.status = ServerStatus.Stopped;
this.scheduledRestart = false;
this.needsRestart = false;
+ this.automaticUpdate = false;
+ this.needsUpdate = false;
this.logEntries = new List();
connectingSteamIds = new List();
stopAttempts = 0;
+ Servers.Add(this);
+ }
+
+ private void UpdateTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
+ {
+ CheckedForUpdate += ValheimServer_ScheduledCheckedForUpdate;
+ this.CheckForUpdate();
+ }
+
+ private void ValheimServer_ScheduledCheckedForUpdate(object sender, UpdateCheckEventArgs e)
+ {
+ CheckedForUpdate -= ValheimServer_ScheduledCheckedForUpdate;
+ if (e.UpdateAvailable)
+ {
+ if (this.PlayerCount == 0)
+ {
+ OnAutomaticUpdate(new EventArgs());
+ this.Stop();
+ }
+ else
+ {
+ this.needsUpdate = true;
+ }
+ }
+ else
+ {
+ UpdateCheckMinutes = UpdateCheckMinutes;
+ }
}
private void RestartTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
if (this.PlayerCount == 0)
{
- scheduledRestart = true;
OnScheduledRestart(new EventArgs());
this.Stop();
} else
@@ -395,11 +459,11 @@ private void RestartTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs
}
}
- public ValheimServer() : this("My Server", 2456, "Dedicated", "Secret", false, false, false, 0, false, null, new Dictionary(), ServerInstallMethod.Manual, Properties.Settings.Default.ServerFilePath)
+ public ValheimServer() : this("My Server", 2456, "Dedicated", "Secret", false, false, false, 0, false, 0, null, new Dictionary(), ServerInstallMethod.Manual, Properties.Settings.Default.ServerFilePath, ProcessPriorityClass.Normal)
{
}
- public ValheimServer(string name) : this(name, 2456, "Dedicated", "Secret", false, false, false, 0, false, null, new Dictionary(), ServerInstallMethod.Manual, Properties.Settings.Default.ServerFilePath)
+ public ValheimServer(string name) : this(name, 2456, "Dedicated", "Secret", false, false, false, 0, false, 0, null, new Dictionary(), ServerInstallMethod.Manual, Properties.Settings.Default.ServerFilePath, ProcessPriorityClass.Normal)
{
}
@@ -428,6 +492,12 @@ public double GetMilisecondsUntilRestart()
TimeSpan ts = restartTime - this.startTime;
return ts.TotalMilliseconds;
}
+ public double GetMilisecondsUntilUpdateCheck()
+ {
+ DateTime updateCheckTime = this.startTime.AddMinutes(this.UpdateCheckMinutes);
+ TimeSpan ts = updateCheckTime - this.startTime;
+ return ts.TotalMilliseconds;
+ }
public string GetWebhookMessage(string EventName)
{
@@ -435,9 +505,9 @@ public string GetWebhookMessage(string EventName)
{
return this.DiscordWebhookMessages[EventName];
}
- else if (_discordWebhookDefaultMessages.ContainsKey(EventName))
+ else if (DiscordWebhookDefaultMessages.ContainsKey(EventName))
{
- return _discordWebhookDefaultMessages[EventName];
+ return DiscordWebhookDefaultMessages[EventName];
}
return null;
}
@@ -445,7 +515,7 @@ public string GetWebhookMessage(string EventName)
public void SendDiscordWebhook(string EventName, Player player, string serverEventName)
{
string message = GetWebhookMessage(EventName);
- if (message == "") return;
+ if (message == "" || message == null) return;
message = message.Replace("{Server.Name}", this.DisplayName);
message = message.Replace("{Server.PlayerCount}", this.PlayerCount.ToString());
if (player != null)
@@ -566,10 +636,14 @@ private void Process_OutputDataReceived(object sender, DataReceivedEventArgs e)
OnPlayerDisconnected(new PlayerEventArgs(player));
if (this.needsRestart && this.PlayerCount == 0)
{
- scheduledRestart = true;
OnScheduledRestart(new EventArgs());
this.Stop();
}
+ if (this.needsUpdate && this.PlayerCount == 0)
+ {
+ OnAutomaticUpdate(new EventArgs());
+ this.Stop();
+ }
break;
}
}
@@ -626,11 +700,26 @@ public void Start()
{
if (this.Running)
{
- //logMessage($"Server {this.Name} cannot start since it is already running.", LogType.Error);
- //addToLog("Server cannot start; it is already running.", LogEntryType.Error);
OnStartFailed(new ServerErrorEventArgs("Server cannot start; it is already running."));
return;
}
+ foreach (var s in ValheimServer.Servers)
+ {
+ if (s.Running)
+ {
+ IEnumerable range = Enumerable.Range(s.Port, 2);
+ if (range.Contains(this.Port) || range.Contains(this.Port + 1))
+ {
+ OnStartFailed(new ServerErrorEventArgs($"Server cannot start; server {s.DisplayName} is already running on conflicting port {s.Port}."));
+ return;
+ }
+ if (s.SaveDir == this.SaveDir && s.World == this.World)
+ {
+ OnStartFailed(new ServerErrorEventArgs($"Server cannot start; server {s.DisplayName} is already running using world {s.World}."));
+ return;
+ }
+ }
+ }
string saveDir = this.SaveDir;
if (saveDir == null || saveDir.Length == 0)
{
@@ -639,7 +728,7 @@ public void Start()
string serverpath = InstallPath;//Properties.Settings.Default.ServerFilePath;
if (!File.Exists(serverpath))
{
- OnStartFailed(new ServerErrorEventArgs($"Server cannot start because {this.serverExe} was not found at the server executable path ({serverpath}). Please update the server executable path."));
+ OnStartFailed(new ServerErrorEventArgs($"Server cannot start because {ValheimServer.ExecutableName} was not found at the server executable path ({serverpath}). Please update the server executable path."));
return;
}
string arguments = $"-nographics -batchmode -name \"{this.Name}\" -port {this.Port} -world \"{this.World}\"";
@@ -678,6 +767,16 @@ public void Start()
{
restartTimer.Enabled = false;
}
+ if (this.UpdateCheckMinutes > 0)
+ {
+ updateTimer.Interval = this.GetMilisecondsUntilUpdateCheck();
+ updateTimer.Enabled = true;
+ updateTimer.Start();
+ }
+ else
+ {
+ updateTimer.Enabled = false;
+ }
stopAttempts = 0;
this.status = ServerStatus.Starting;
OnStarting(new EventArgs());
@@ -688,7 +787,10 @@ public void Start()
this.process.Refresh();
this.needsRestart = false;
this.scheduledRestart = false;
+ this.needsUpdate = false;
+ this.automaticUpdate = false;
this.process.Start();
+ this.process.PriorityClass = this.ProcessPriority;
this.process.BeginOutputReadLine();
this.process.WaitForExit();
}).Start();
@@ -715,6 +817,7 @@ public void Stop()
SetConsoleCtrlHandler(null, false);
this.intentionalExit = true;
this.restartTimer.Enabled = false;
+ this.updateTimer.Enabled = false;
}
else
{
@@ -763,7 +866,7 @@ private void Process_Exited(object sender, EventArgs e)
}
else
{
- OnServerExited(new ServerExitedEventArgs(this.process.ExitCode, this.intentionalExit));
+ OnServerStopped(new ServerStoppedEventArgs(this.process.ExitCode, this.intentionalExit));
if (scheduledRestart)
{
if (this.UpdateOnRestart && this.InstallMethod == ServerInstallMethod.SteamCMD)
@@ -775,9 +878,18 @@ private void Process_Exited(object sender, EventArgs e)
{
this.Start();
}
- } else if (unwantedexit)
+ }
+ else if (automaticUpdate)
+ {
+ if (this.InstallMethod == ServerInstallMethod.SteamCMD)
+ {
+ this.UpdateEnded += ValheimServer_UpdateEndedAndStart;
+ this.Update();
+ }
+ }
+ else if (unwantedexit)
{
- OnServerExitedUnexpectedly(new ServerExitedEventArgs(this.process.ExitCode, this.intentionalExit));
+ OnServerStoppedUnexpectedly(new ServerStoppedEventArgs(this.process.ExitCode, this.intentionalExit));
if (this.Autostart)
{
this.Start();
@@ -791,7 +903,7 @@ private void ValheimServer_CheckedForUpdateAndRestart(object sender, UpdateCheck
this.CheckedForUpdate -= ValheimServer_CheckedForUpdateAndRestart;
if (e.UpdateAvailable)
{
- this.UpdateComplete += ValheimServer_UpdateCompleteAndStart;
+ this.UpdateEnded += ValheimServer_UpdateEndedAndStart;
this.Update();
}
else
@@ -800,9 +912,9 @@ private void ValheimServer_CheckedForUpdateAndRestart(object sender, UpdateCheck
}
}
- private void ValheimServer_UpdateCompleteAndStart(object sender, UpdateCompleteEventArgs e)
+ private void ValheimServer_UpdateEndedAndStart(object sender, UpdateEndedEventArgs e)
{
- this.UpdateComplete -= ValheimServer_UpdateCompleteAndStart;
+ this.UpdateEnded -= ValheimServer_UpdateEndedAndStart;
this.Start();
}
@@ -886,7 +998,7 @@ void IEditableObject.EndEdit()
int IComparable.CompareTo(object obj)
{
ValheimServer vs = (ValheimServer)obj;
- int nameCompare = String.Compare(this.Name, vs.Name);
+ int nameCompare = String.Compare(this.DisplayName, vs.DisplayName);
if (nameCompare == 0)
{
return String.Compare(this.Port.ToString(), vs.Port.ToString());
@@ -955,7 +1067,7 @@ private void OnPlayerDied(PlayerEventArgs args)
EventHandler handler = PlayerDied;
if (null != handler) handler(this, args);
}
- private void OnServerExited(ServerExitedEventArgs args)
+ private void OnServerStopped(ServerStoppedEventArgs args)
{
try
{
@@ -973,13 +1085,13 @@ private void OnServerExited(ServerExitedEventArgs args)
{
addToLog($"Server exited with code {args.ExitCode}.", LogEntryType.Error);
}
- EventHandler handler = Exited;
+ EventHandler handler = Stopped;
if (null != handler) handler(this, args);
}
- private void OnServerExitedUnexpectedly(ServerExitedEventArgs args)
+ private void OnServerStoppedUnexpectedly(ServerStoppedEventArgs args)
{
addToLog($"Server stopped unexpectedly.", LogEntryType.Error);
- EventHandler handler = ExitedUnexpectedly;
+ EventHandler handler = StoppedUnexpectedly;
if (null != handler) handler(this, args);
}
private void OnRandomServerEvent(RandomServerEventArgs args)
@@ -1010,7 +1122,7 @@ private void OnStarting(EventArgs args)
private void OnStopping(EventArgs args)
{
addToLog("Server stopping...");
- EventHandler handler = Starting;
+ EventHandler handler = Stopping;
if (null != handler) handler(this, args);
}
private void OnStarted(EventArgs args)
@@ -1041,10 +1153,18 @@ private void OnStopFailed(ServerErrorEventArgs args)
}
private void OnScheduledRestart(EventArgs args)
{
+ scheduledRestart = true;
addToLog($"Initiating scheduled restart...");
EventHandler handler = ScheduledRestart;
if (null != handler) handler(this, args);
}
+ private void OnAutomaticUpdate(EventArgs args)
+ {
+ this.automaticUpdate = true;
+ addToLog($"Initiating automatic update...");
+ EventHandler handler = AutomaticUpdate;
+ if (null != handler) handler(this, args);
+ }
private void OnErrorOccurred(ServerErrorEventArgs args)
{
addToLog(args.Message, LogEntryType.Error);
@@ -1071,13 +1191,13 @@ private void OnCheckedForUpdate(UpdateCheckEventArgs args)
EventHandler handler = CheckedForUpdate;
if (null != handler) handler(this, args);
}
- private void OnUpdateComplete(UpdateCompleteEventArgs args)
+ private void OnUpdateEnded(UpdateEndedEventArgs args)
{
if (args.Updated)
{
addToLog("Update complete.", LogEntryType.Success);
}
- else if (args.Result == UpdateCompleteEventArgs.UpdateResults.AlreadyUpToDate)
+ else if (args.Result == UpdateEndedEventArgs.UpdateResults.AlreadyUpToDate)
{
addToLog("Server already up to date.");
}
@@ -1085,7 +1205,7 @@ private void OnUpdateComplete(UpdateCompleteEventArgs args)
{
addToLog(args.Message, LogEntryType.Error);
}
- EventHandler handler = UpdateComplete;
+ EventHandler handler = UpdateEnded;
if (null != handler) handler(this, args);
}
/*private void OutputReceived(DataReceivedEventArgs args)
@@ -1189,10 +1309,18 @@ public void Update()
{
if (!this.Running)
{
+ foreach (var s in ValheimServer.Servers)
+ {
+ if (s != this && s.Status != ServerStatus.Stopped && s.InstallPath == this.InstallPath)
+ {
+ OnUpdateEnded(new UpdateEndedEventArgs($"Could not update server; server {s.DisplayName} uses the same installation and is running."));
+ return;
+ }
+ }
addToLog($"Updating server...");
if (!File.Exists(Properties.Settings.Default.SteamCMDPath))
{
- OnUpdateComplete(new UpdateCompleteEventArgs($"SteamCMD was not found at {Properties.Settings.Default.SteamCMDPath}."));
+ OnUpdateEnded(new UpdateEndedEventArgs($"SteamCMD was not found at {Properties.Settings.Default.SteamCMDPath}."));
return;
}
var process = new Process();
@@ -1210,24 +1338,24 @@ public void Update()
Match match = rx.Match(output);
if (match.Success)
{
- OnUpdateComplete(new UpdateCompleteEventArgs(UpdateCompleteEventArgs.UpdateResults.AlreadyUpToDate));
+ OnUpdateEnded(new UpdateEndedEventArgs(UpdateEndedEventArgs.UpdateResults.AlreadyUpToDate));
return;
}
rx = new Regex($"App '{SteamID}' fully installed.", RegexOptions.Compiled);
match = rx.Match(output);
if (match.Success)
{
- OnUpdateComplete(new UpdateCompleteEventArgs(UpdateCompleteEventArgs.UpdateResults.Updated));
+ OnUpdateEnded(new UpdateEndedEventArgs(UpdateEndedEventArgs.UpdateResults.Updated));
return;
}
- OnUpdateComplete(new UpdateCompleteEventArgs("Update probably failed. Unrecognized output from SteamCMD."));
+ OnUpdateEnded(new UpdateEndedEventArgs("Update probably failed. Unrecognized output from SteamCMD."));
});
process.Start();
//process.WaitForExit();
}
else
{
- OnUpdateComplete(new UpdateCompleteEventArgs("Please stop the server before updating."));
+ OnUpdateEnded(new UpdateEndedEventArgs("Please stop the server before updating."));
}
}
public static void TerminateAll(Process[] servers)
@@ -1247,6 +1375,45 @@ public static void TerminateAll(Process[] servers)
}
}).Start();
}
+ ~ValheimServer()
+ {
+ Dispose(false);
+ }
+ public void Dispose()
+ {
+ Dispose(true);
+ // This object will be cleaned up by the Dispose method.
+ // Therefore, you should call GC.SupressFinalize to
+ // take this object off the finalization queue
+ // and prevent finalization code for this object
+ // from executing a second time.
+ GC.SuppressFinalize(this);
+ }
+ public void Dispose(bool disposing)
+ {
+ if (!this.disposed)
+ {
+ if (this.Status != ServerStatus.Stopped)
+ {
+ throw new Exception("Server must be stopped before it can be disposed");
+ }
+ // If disposing equals true, dispose all managed
+ // and unmanaged resources.
+ if (disposing)
+ {
+ // Dispose managed resources.
+ //component.Dispose();
+ }
+ if (Servers.Contains(this))
+ {
+ Servers.Remove(this);
+ }
+ // Call the appropriate methods to clean up
+ // unmanaged resources here.
+ //resource.Cleanup()
+ }
+ disposed = true;
+ }
}
public class UpdatedEventArgs : EventArgs
@@ -1290,12 +1457,12 @@ public Player Player
get { return _player; }
}
}
- public class ServerExitedEventArgs
+ public class ServerStoppedEventArgs
{
private readonly int _exitcode;
private readonly bool _intentionalexit;
- public ServerExitedEventArgs(int exitcode, bool intentionalexit)
+ public ServerStoppedEventArgs(int exitcode, bool intentionalexit)
{
_exitcode = exitcode;
_intentionalexit = intentionalexit;
@@ -1388,7 +1555,7 @@ public string Message
get { return _message; }
}
}
- public class UpdateCompleteEventArgs : EventArgs
+ public class UpdateEndedEventArgs : EventArgs
{
public enum UpdateResults
{
@@ -1398,12 +1565,12 @@ public enum UpdateResults
}
private readonly UpdateResults _result;
private readonly string _message;
- public UpdateCompleteEventArgs(UpdateResults result)
+ public UpdateEndedEventArgs(UpdateResults result)
{
_result = result;
_message = "";
}
- public UpdateCompleteEventArgs(string failMessage) : this(UpdateResults.Failed)
+ public UpdateEndedEventArgs(string failMessage) : this(UpdateResults.Failed)
{
_message = failMessage;
}
diff --git a/ValheimServerWarden.csproj b/ValheimServerWarden.csproj
index 324ae90..59ff1f8 100644
--- a/ValheimServerWarden.csproj
+++ b/ValheimServerWarden.csproj
@@ -6,14 +6,14 @@
true
true
ValheimServerWarden.App
- 0.4.8
- 0.4.8.0
+ 0.4.9
+ 0.4.9.0
ValheimServerWarden
Razzmatazz
Resources\vsw2.ico
ValheimServerWarden
Valheim Server Warden
- 0.4.8.0
+ 0.4.9.0
ValheimServerWarden