Skip to content

Commit

Permalink
Added option to write app log to file. Fixed additional crash when at…
Browse files Browse the repository at this point in the history
…tempting to set server path in certain situations.
  • Loading branch information
Razzmatazzz committed Mar 4, 2021
1 parent 504c6c0 commit 613d7ce
Show file tree
Hide file tree
Showing 10 changed files with 105 additions and 49 deletions.
8 changes: 7 additions & 1 deletion LogEntry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public class LogEntry
public static Color NormalColor { get; set; } = Color.FromArgb(System.Drawing.SystemColors.WindowText.A, System.Drawing.SystemColors.WindowText.R, System.Drawing.SystemColors.WindowText.G, System.Drawing.SystemColors.WindowText.B);
public static Color SuccessColor { get; set; } = Color.FromRgb(50, 175, 50);
public static Color ErrorColor { get; set; } = Color.FromRgb(175, 50, 50);
public static Color WarningColor { get; set; } = Color.FromRgb(255, 170, 0);
private string message;
private LogEntryType logMessageType;
private DateTime timeStamp;
Expand Down Expand Up @@ -53,6 +54,10 @@ public Color Color
{
return ErrorColor;
}
else if (Type == LogEntryType.Warning)
{
return WarningColor;
}
return NormalColor;
}
}
Expand Down Expand Up @@ -94,7 +99,8 @@ public enum LogEntryType
{
Normal,
Success,
Error
Error,
Warning
}
public class LoggedMessageEventArgs : EventArgs
{
Expand Down
4 changes: 3 additions & 1 deletion MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
Expand Down Expand Up @@ -120,7 +121,8 @@
</Border>
<CheckBox x:Name="chkAutoCheckUpdate" Content="Automatically check for update" Margin="10,5,0,5" VerticalAlignment="Center" Grid.Row="2" Checked="chkAutoCheckUpdate_Click" Unchecked="chkAutoCheckUpdate_Click"/>
<Button x:Name="btnUpdateCheck" Content="Check Now" Margin="251,5,0,5" VerticalAlignment="Center" Click="btnUpdateCheck_Click" Grid.Row="2"/>
<Label x:Name="lblReportBug" Content="Found a bug? Report it on GitHub." Margin="10" Cursor="Hand" Grid.Row="4" MouseLeftButtonUp="lblReportBug_MouseLeftButtonUp"/>
<CheckBox x:Name="chkLog" Content="Create log file" Margin="10,5" Grid.Row="3" Checked="chkLog_Checked" Unchecked="chkLog_Checked"/>
<Label x:Name="lblReportBug" Content="Found a bug? Report it on GitHub." Margin="10" Cursor="Hand" Grid.Row="5" MouseLeftButtonUp="lblReportBug_MouseLeftButtonUp"/>
</Grid>
</TabItem>
<TabItem Header="Log">
Expand Down
57 changes: 37 additions & 20 deletions MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ public MainWindow()
radThemeLight.IsChecked = true;
radThemeLight_Checked(null, null);
}

if (Properties.Settings.Default.WriteAppLog)
{
System.IO.File.WriteAllText("vswlog.txt", "");
}
txtLog.Document.Blocks.Clear();
logMessage($"Version {typeof(MainWindow).Assembly.GetName().Version}");
CheckServerPath();
Expand All @@ -86,6 +91,7 @@ public MainWindow()
}
cmbServerType.SelectedIndex = Properties.Settings.Default.ServerInstallType;
chkAutoCheckUpdate.IsChecked = Properties.Settings.Default.AutoCheckUpdate;
chkLog.IsChecked = Properties.Settings.Default.WriteAppLog;
if (Properties.Settings.Default.AutoCheckUpdate)
{
checkForUpdate();
Expand Down Expand Up @@ -183,7 +189,7 @@ private void DgServers_ContextMenuOpening(object sender, ContextMenuEventArgs e)
}
ValheimServer server = ((ValheimServer)dgServers.SelectedItem);
serversMenuDetails.IsEnabled = true;
serversMenuLog.IsEnabled = (File.Exists(server.GetLogName()));
serversMenuLog.IsEnabled = (File.Exists(server.LogRawName));
if (server.Running)
{
serversMenuStart.IsEnabled = false;
Expand Down Expand Up @@ -303,29 +309,29 @@ private void CheckServerPath()
}
private void btnServerPath_Click(object sender, RoutedEventArgs e)
{
var serverpath = "";
if (txtServerPath.Text != "") {
serverpath = new FileInfo(txtServerPath.Text).Directory.FullName;
}
var openFolderDialog = new System.Windows.Forms.FolderBrowserDialog();
if (Directory.Exists(serverpath))
if (txtServerPath.Text != "")
{
openFolderDialog.SelectedPath = serverpath;
var serverpath = new FileInfo(txtServerPath.Text).Directory.FullName;
if (Directory.Exists(serverpath))
{
openFolderDialog.SelectedPath = serverpath;
}
}
openFolderDialog.UseDescriptionForTitle = true;
openFolderDialog.Description = "Default server installation folder";
var result = openFolderDialog.ShowDialog();
if (result == System.Windows.Forms.DialogResult.OK)
{
var folderName = openFolderDialog.SelectedPath;
if (folderName.Equals(serverpath))
/*if (folderName+ "\\valheim_server.exe" == txtServerPath.Text)
{
return;
}
}*/
if (!File.Exists($@"{folderName}\valheim_server.exe") && 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($"valheim_server.exe 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)
{
Expand Down Expand Up @@ -656,8 +662,8 @@ private void StartServer(ValheimServer server)
{
if (s.Running)
{
IEnumerable<int> range = Enumerable.Range(s.Port, 3);
if (range.Contains(server.Port) || range.Contains(server.Port + 1) || range.Contains(server.Port + 2))
IEnumerable<int> 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;
Expand Down Expand Up @@ -786,12 +792,12 @@ public void logMessage(LogEntry entry)
}
}
});
/*if (Properties.Settings.Default.CreateLogFile)
if (Properties.Settings.Default.WriteAppLog)
{
StreamWriter writer = System.IO.File.AppendText("log.txt");
writer.WriteLine(DateTime.Now.ToString() + ": " + msg);
StreamWriter writer = System.IO.File.AppendText("vswlog.txt");
writer.WriteLine(entry.TimeStamp+": " +entry.Message);
writer.Close();
}*/
}
}

private void Window_StateChanged(object sender, EventArgs e)
Expand Down Expand Up @@ -1087,7 +1093,7 @@ private void serversMenuLog_Click(object sender, RoutedEventArgs e)
{
ValheimServer server = ((ValheimServer)dgServers.SelectedItem);
if (server == null) return;
if (File.Exists(server.GetLogName()))
if (File.Exists(server.LogRawName))
{
ShowServerLog(server);
}
Expand Down Expand Up @@ -1135,14 +1141,14 @@ private void btnSteamCmdPath_Click(object sender, RoutedEventArgs e)
if (result == System.Windows.Forms.DialogResult.OK)
{
var folderName = openFolderDialog.SelectedPath;
if (folderName.Equals(txtSteamCmdPath.Text))
/*if (folderName.Equals(txtSteamCmdPath.Text))
{
return;
}
}*/
if (!File.Exists($@"{folderName}\steamcmd.exe"))
{
var mmb = new ModernMessageBox(this);
var install = mmb.Show("steamcmd.exe was not found in this folder, do you want to install it?",
var install = mmb.Show($"steamcmd.exe was not found in {folderName}, do you want to install it?",
"Install SteamCMD?", MessageBoxButton.YesNo, MessageBoxImage.Question, MessageBoxResult.No);
if (install == MessageBoxResult.Yes)
{
Expand Down Expand Up @@ -1210,5 +1216,16 @@ private void menuLogClear_Click(object sender, RoutedEventArgs e)
txtLog.Document.Blocks.Clear();
logEntries.Clear();
}

private void chkLog_Checked(object sender, RoutedEventArgs e)
{
bool newValue = chkLog.IsChecked.HasValue ? chkLog.IsChecked.Value : false;
if (newValue & !Properties.Settings.Default.WriteAppLog)
{
System.IO.File.WriteAllText("vswlog.txt", DateTime.Now.ToString() + ": Version " + typeof(MainWindow).Assembly.GetName().Version + "\r\n");
}
Properties.Settings.Default.WriteAppLog = newValue;
Properties.Settings.Default.Save();
}
}
}
12 changes: 12 additions & 0 deletions Properties/Settings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Properties/Settings.settings
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,8 @@
<Setting Name="MainWindowHeight" Type="System.Double" Scope="User">
<Value Profile="(Default)">0</Value>
</Setting>
<Setting Name="WriteAppLog" Type="System.Boolean" Scope="User">
<Value Profile="(Default)">False</Value>
</Setting>
</Settings>
</SettingsFile>
2 changes: 1 addition & 1 deletion ServerDetailsWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@
</Grid>
</Grid>
<TabControl x:Name="tabsServer" Grid.Row="1">
<TabItem Header="Status" Background="White">
<TabItem Header="Status">
<Grid x:Name="gridStatus" Height="Auto">
<Grid.ColumnDefinitions>
<ColumnDefinition/>
Expand Down
26 changes: 18 additions & 8 deletions ServerDetailsWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,8 @@ public void RefreshControls()
{
menuSteamCmdUpdate.Visibility = Visibility.Collapsed;
}
btnLog.IsEnabled = (File.Exists(Server.GetLogName()));
btnLog.Visibility = (File.Exists(Server.GetLogName())) ? Visibility.Visible : Visibility.Hidden;
btnLog.IsEnabled = (File.Exists(Server.LogRawName));
btnLog.Visibility = (File.Exists(Server.LogRawName)) ? Visibility.Visible : Visibility.Hidden;

if (Properties.Settings.Default.SteamCMDPath != null && Properties.Settings.Default.SteamCMDPath.Length > 0 && File.Exists(Properties.Settings.Default.SteamCMDPath) && Server.InstallMethod == ValheimServer.ServerInstallMethod.SteamCMD)
{
Expand Down Expand Up @@ -215,7 +215,14 @@ private void ServerToControls()
txtPassword.Text = Server.Password;
txtSaveDir.Text = Server.SaveDir;
chkPublic.IsChecked = Server.Public;
txtServerDir.Text = Server.InstallPath;
if (Server.InstallPath != null)
{
txtServerDir.Text = Server.InstallPath;
}
else if (Properties.Settings.Default.ServerFilePath != null)
{
txtServerDir.Text = Properties.Settings.Default.ServerFilePath;
}
cmbServerType.SelectedIndex = (int)Server.InstallMethod;
chkAutostart.IsChecked = Server.Autostart;
chkRawLog.IsChecked = Server.RawLog;
Expand Down Expand Up @@ -552,22 +559,25 @@ private void Window_ContentRendered(object sender, EventArgs e)

private void btnServerDir_Click(object sender, RoutedEventArgs e)
{
var serverpath = new FileInfo(txtServerDir.Text).Directory.FullName;
var openFolderDialog = new System.Windows.Forms.FolderBrowserDialog();
if (Directory.Exists(serverpath))
if (txtServerDir.Text != null)
{
openFolderDialog.SelectedPath = serverpath;
var serverpath = new FileInfo(txtServerDir.Text).Directory.FullName;
if (Directory.Exists(serverpath))
{
openFolderDialog.SelectedPath = serverpath;
}
}
openFolderDialog.UseDescriptionForTitle = true;
openFolderDialog.Description = "Server installation folder";
var result = openFolderDialog.ShowDialog();
if (result == System.Windows.Forms.DialogResult.OK)
{
var folderName = openFolderDialog.SelectedPath;
if (folderName.Equals(serverpath))
/*if (folderName.Equals(serverpath))
{
return;
}
}*/
if (!File.Exists($@"{folderName}\valheim_server.exe") && cmbServerType.SelectedIndex == (int)ValheimServer.ServerInstallMethod.SteamCMD && File.Exists(Properties.Settings.Default.SteamCMDPath))
{
var mmb = new ModernMessageBox(this);
Expand Down
6 changes: 3 additions & 3 deletions ServerLogWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,18 @@ public ServerLogWindow(ValheimServer server)
{
InitializeComponent();
_server = server;
this.Title = $"{server.GetLogName()} (does not update live)";
this.Title = $"{server.LogRawName} (does not update live)";
LoadLogText();
}

public void LoadLogText()
{
try
{
if (File.Exists(_server.GetLogName()))
if (File.Exists(_server.LogRawName))
{
txtLog.Document.Blocks.Clear();
Run run = new Run(File.ReadAllText(_server.GetLogName()));
Run run = new Run(File.ReadAllText(_server.LogRawName));
Paragraph paragraph = new Paragraph(run);
paragraph.Margin = new Thickness(0);
txtLog.Document.Blocks.Add(paragraph);
Expand Down
30 changes: 18 additions & 12 deletions ValheimServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -402,18 +402,24 @@ private void RestartTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs
{

}
public string GetLogName()
public string LogRawName
{
string logname = this.DisplayName.Replace(" ", "_");
logname = Regex.Replace(logname, @"[<]", "[");
logname = Regex.Replace(logname, @"[>]", "]");
foreach (var c in Path.GetInvalidFileNameChars()) { logname = logname.Replace(c, '-'); }
//Regex rgx = new Regex("[^a-zA-Z0-9_-]");
//logname = rgx.Replace(logname, "");
//return $"{logname}-{this.startTime.ToString("yyyy-MM-dd_HH-mm-ss")}.log";
return $"{logname}-{this.Port}-{this.World}.log";
get
{
return LogName.Replace(".log", "-raw.log");
}
}
public string LogName
{
get
{
string logname = this.DisplayName.Replace(" ", "_");
logname = Regex.Replace(logname, @"[<]", "[");
logname = Regex.Replace(logname, @"[>]", "]");
foreach (var c in Path.GetInvalidFileNameChars()) { logname = logname.Replace(c, '-'); }
return $"{logname}-{this.Port}-{this.World}.log";
}
}

public double GetMilisecondsUntilRestart()
{
DateTime restartTime = this.startTime.AddHours(this.RestartHours);
Expand Down Expand Up @@ -483,7 +489,7 @@ private void Process_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
try
{
StreamWriter writer = System.IO.File.AppendText(this.GetLogName());
StreamWriter writer = System.IO.File.AppendText(LogRawName);
writer.WriteLine(msg);
writer.Close();
}
Expand Down Expand Up @@ -658,7 +664,7 @@ public void Start()
{
if (this.RawLog)
{
System.IO.File.WriteAllText(this.GetLogName(),"");
System.IO.File.WriteAllText(LogRawName,"");
}
this.startTime = DateTime.Now;
if (this.RestartHours > 0)
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.6</Version>
<AssemblyVersion>0.4.6.0</AssemblyVersion>
<Version>0.4.7</Version>
<AssemblyVersion>0.4.7.0</AssemblyVersion>
<Product>ValheimServerWarden</Product>
<Authors>Razzmatazz</Authors>
<ApplicationIcon>Resources\vsw2.ico</ApplicationIcon>
<PackageId>ValheimServerWarden</PackageId>
<AssemblyName>Valheim Server Warden</AssemblyName>
<FileVersion>0.4.6.0</FileVersion>
<FileVersion>0.4.7.0</FileVersion>
<Company>ValheimServerWarden</Company>
</PropertyGroup>

Expand Down

0 comments on commit 613d7ce

Please sign in to comment.