Skip to content

Commit

Permalink
Fix for SteamCMD update check. Added webhook for server updated.
Browse files Browse the repository at this point in the history
  • Loading branch information
Razzmatazzz committed Mar 29, 2021
1 parent 3c038f5 commit c3d9961
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 16 deletions.
6 changes: 5 additions & 1 deletion DiscordWebhookWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
Expand Down Expand Up @@ -69,7 +70,10 @@
</TextBox.ContextMenu>
</TextBox>
<Button x:Name="btnRandomServerEvent" Content="Test" Tag="OnRandomServerEvent" Margin="0,5,10,5" VerticalAlignment="Center" Grid.Row="10" Grid.Column="2" Click="btnTestWebhook_Click"/>
<Grid Grid.Row="11" Grid.ColumnSpan="3" HorizontalAlignment="Center">
<Label Content="Server Updated" Margin="10,5,5,5" VerticalAlignment="Center" Grid.Row="11" Grid.Column="0"/>
<TextBox x:Name="txtOnUpdated" Tag="OnUpdateEnded" Margin="5" Grid.Row="11" Grid.Column="1" />
<Button x:Name="btnTestUpdated" Content="Test" Tag="OnUpdateEnded" Margin="0,5,10,5" VerticalAlignment="Center" Grid.Row="11" Grid.Column="2" Click="btnTestWebhook_Click"/>
<Grid Grid.Row="12" Grid.ColumnSpan="3" HorizontalAlignment="Center">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="Auto"/>
Expand Down
1 change: 1 addition & 0 deletions DiscordWebhookWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public DiscordWebhookWindow(ValheimServer server)
messageControls.Add(txtOnStarted);
messageControls.Add(txtOnStartFailed);
messageControls.Add(txtOnServerExited);
messageControls.Add(txtOnUpdated);
foreach (var textBox in messageControls)
{
textBox.Text = server.GetWebhookMessage(textBox.Tag.ToString());
Expand Down
23 changes: 16 additions & 7 deletions MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,13 @@ public MainWindow()
Properties.Settings.Default.UpgradeRequired = false;
Properties.Settings.Default.Save();
}
//Width = Properties.Settings.Default.MainWindowWidth;
if (Properties.Settings.Default.MainWindowWidth > 0)
{
Width = Properties.Settings.Default.MainWindowWidth;
}
if (Properties.Settings.Default.MainWindowHeight > 0)
{
//Height = Properties.Settings.Default.MainWindowHeight;
Height = Properties.Settings.Default.MainWindowHeight;
}
LogEntry.NormalColor = ((SolidColorBrush)this.Foreground).Color;
logEntries = new List<LogEntry>();
Expand Down Expand Up @@ -1278,13 +1281,19 @@ private void menuLogClear_Click(object sender, RoutedEventArgs e)

private void chkLog_Checked(object sender, RoutedEventArgs e)
{
bool newValue = chkLog.IsChecked.HasValue ? chkLog.IsChecked.Value : false;
if (newValue & !Properties.Settings.Default.WriteAppLog)
try
{
bool newValue = chkLog.IsChecked.GetValueOrDefault();
if (newValue & !Properties.Settings.Default.WriteAppLog)
{
System.IO.File.WriteAllText(LogPath, DateTime.Now.ToString() + ": Version " + typeof(MainWindow).Assembly.GetName().Version + "\r\n");
}
Properties.Settings.Default.WriteAppLog = newValue;
Properties.Settings.Default.Save();
} catch (Exception ex)
{
System.IO.File.WriteAllText(LogPath, DateTime.Now.ToString() + ": Version " + typeof(MainWindow).Assembly.GetName().Version + "\r\n");
logMessage($"Error changing log option: {ex.Message}");
}
Properties.Settings.Default.WriteAppLog = newValue;
Properties.Settings.Default.Save();
}

private void btnReportBug_Click(object sender, RoutedEventArgs e)
Expand Down
49 changes: 44 additions & 5 deletions ValheimServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ public enum ServerInstallMethod
{"OnPlayerConnected", "{Player.Name} has entered the fray!" },
{"OnPlayerDisconnected", "{Player.Name} has departed." },
{"OnPlayerDied", "{Player.Name} met an untimely demise." },
{"OnRandomServerEvent", "{EventName} are attacking!" }
{"OnRandomServerEvent", "{EventName} are attacking!" },
{"OnUpdateEnded", "Server update complete." }
};
public static Dictionary<string, string> DiscordWebhookDefaultAttackNames { get; } = new Dictionary<string, string>
{
Expand Down Expand Up @@ -92,6 +93,7 @@ struct ServerData
public event EventHandler<EventArgs> Stopping;
public event EventHandler<ServerStoppedEventArgs> StoppedUnexpectedly;
public event EventHandler<ServerErrorEventArgs> ErrorOccurred;
public event EventHandler<UpdateCheckEventArgs> CheckingForUpdate;
public event EventHandler<UpdateCheckEventArgs> CheckedForUpdate;
public event EventHandler<UpdateEndedEventArgs> UpdateEnded;
public event DataReceivedEventHandler OutputDataReceived
Expand Down Expand Up @@ -435,6 +437,8 @@ public double MemoryUsed
return 0;
}
}
//[JsonIgnore]
public string Version { get; set; }
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<string,string> discordmessages, Dictionary<string, string> discordservereventnames, ServerInstallMethod install, string instpath, ProcessPriorityClass processpriority, bool umodupdating)
{
this.data.name = name;
Expand All @@ -454,6 +458,7 @@ public ValheimServer(string name, int port, string world, string password, bool
this.data.autoUpdateuMod = umodupdating;
InstallMethod = install;
InstallPath = instpath;
Version = "Unknown";

this.process = new Process();
this.process.StartInfo.EnvironmentVariables["SteamAppId"] = "892970";
Expand Down Expand Up @@ -575,6 +580,7 @@ public void SendDiscordWebhook(string EventName, Player player, string serverEve
if (message == "" || message == null) return;
message = message.Replace("{Server.Name}", this.DisplayName);
message = message.Replace("{Server.PlayerCount}", this.PlayerCount.ToString());
message = message.Replace("{Server.Version}", this.Version);
if (player != null)
{
message = message.Replace("{Player.Name}", player.Name);
Expand Down Expand Up @@ -737,18 +743,28 @@ private void Process_OutputDataReceived(object sender, DataReceivedEventArgs e)
//army_moder
}

//Monitor for server finishes starting
//Last since it should only happen once per server restart, so more efficient overall to check others first
if (this.Status == ServerStatus.Starting)
{
//Monitor for server version
rx = new Regex(@"Valheim version:(\d+\.\d+\.\d+)", RegexOptions.Compiled | RegexOptions.IgnoreCase);
match = rx.Match(msg);
if (match.Success)
{
Version = match.Groups[1].ToString();
//logMessage($"Server {this.Name}: started", LogType.Success);
return;
}

//Monitor for server finishes starting
//Last since it should only happen once per server restart, so more efficient overall to check others first
rx = new Regex(@"DungeonDB Start \d+", RegexOptions.Compiled | RegexOptions.IgnoreCase);
match = rx.Match(msg);
if (match.Success)
{
OnStarted(new EventArgs());
//logMessage($"Server {this.Name}: started", LogType.Success);
return;
}
return;
}

//Monitor for server fails to start
Expand Down Expand Up @@ -1242,7 +1258,8 @@ private void OnStarted(EventArgs args)
{
OnErrorOccurred(new ServerErrorEventArgs($"Error sending Webhook for server start: {ex.Message}", ex));
}
addToLog($"Server started.", LogEntryType.Success);
var v = Version == "Unknown" ? "" : " (" + Version + ")";
addToLog($"Server started{v}.", LogEntryType.Success);
EventHandler<EventArgs> handler = Started;
if (null != handler) handler(this, args);
}
Expand Down Expand Up @@ -1282,6 +1299,15 @@ private void OnErrorOccurred(ServerErrorEventArgs args)
EventHandler<ServerErrorEventArgs> handler = ErrorOccurred;
if (null != handler) handler(this, args);
}
private void OnCheckingForUpdate(UpdateCheckEventArgs args)
{
if (args.Noisy)
{
addToLog($"Checking for server update...");
}
EventHandler<UpdateCheckEventArgs> handler = CheckingForUpdate;
if (null != handler) handler(this, args);
}
private void OnCheckedForUpdate(UpdateCheckEventArgs args)
{
if (args.Success)
Expand Down Expand Up @@ -1310,6 +1336,14 @@ private void OnUpdateEnded(UpdateEndedEventArgs args)
status = ServerStatus.Stopped;
if (args.Updated)
{
try
{
SendDiscordWebhook(System.Reflection.MethodBase.GetCurrentMethod().Name, null, null);
}
catch (Exception ex)
{
OnErrorOccurred(new ServerErrorEventArgs($"Error sending Webhook for server updated: {ex.Message}", ex));
}
addToLog("Update complete.", LogEntryType.Success);
}
else if (args.Result == UpdateEndedEventArgs.UpdateResults.AlreadyUpToDate)
Expand Down Expand Up @@ -1355,9 +1389,14 @@ public void CheckForUpdate(bool noisy)
OnCheckedForUpdate(new UpdateCheckEventArgs($"SteamCMD was not found at {Properties.Settings.Default.SteamCMDPath}."));
return;
}
OnCheckingForUpdate(new UpdateCheckEventArgs(false, false, noisy));
var steamcmddir = (new FileInfo(Properties.Settings.Default.SteamCMDPath)).Directory.FullName;
if (File.Exists($@"{steamcmddir}\appcache\appinfo.vdf")) File.Delete($@"{steamcmddir}\appcache\appinfo.vdf");
//if (File.Exists($@"{steamcmddir}\appcache\packageinfo.vdf")) File.Delete($@"{steamcmddir}\appcache\packageinfo.vdf");
var process = new Process();
process.StartInfo.FileName = Properties.Settings.Default.SteamCMDPath;
process.StartInfo.Arguments = $"+login anonymous +app_info_update 1 +app_info_print {ValheimServer.SteamID} +quit";
process.StartInfo.WorkingDirectory = new FileInfo(this.InstallPath).Directory.FullName;
process.StartInfo.CreateNoWindow = true;
process.EnableRaisingEvents = true;
process.StartInfo.RedirectStandardOutput = true;
Expand Down
6 changes: 3 additions & 3 deletions ValheimServerWarden.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
<UseWPF>true</UseWPF>
<UseWindowsForms>true</UseWindowsForms>
<StartupObject>ValheimServerWarden.App</StartupObject>
<Version>0.4.15</Version>
<AssemblyVersion>0.4.15.0</AssemblyVersion>
<Version>0.4.16</Version>
<AssemblyVersion>0.4.16.0</AssemblyVersion>
<Product>ValheimServerWarden</Product>
<Authors>Razzmatazz</Authors>
<ApplicationIcon>Resources\vsw2.ico</ApplicationIcon>
<PackageId>ValheimServerWarden</PackageId>
<AssemblyName>Valheim Server Warden</AssemblyName>
<FileVersion>0.4.15.0</FileVersion>
<FileVersion>0.4.16.0</FileVersion>
<Company>ValheimServerWarden</Company>
</PropertyGroup>

Expand Down

0 comments on commit c3d9961

Please sign in to comment.