Skip to content

Commit

Permalink
Merge pull request #28 from runeberry/jb-bug
Browse files Browse the repository at this point in the history
Bugfixes
  • Loading branch information
dolphinspired authored May 13, 2021
2 parents 93fdba5 + a429ed1 commit f3600f2
Show file tree
Hide file tree
Showing 7 changed files with 129 additions and 80 deletions.
8 changes: 5 additions & 3 deletions ValheimServerGUI/Forms/DirectoriesForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,11 @@ private bool SavePreferences()

private void RestoreDefaults()
{
this.GamePathField.Value = Environment.ExpandEnvironmentVariables(UserPreferences.Default.ValheimGamePath);
this.ServerPathField.Value = Environment.ExpandEnvironmentVariables(UserPreferences.Default.ValheimServerPath);
this.SaveDataFolderField.Value = Environment.ExpandEnvironmentVariables(UserPreferences.Default.ValheimSaveDataFolder);
var prefs = UserPreferences.GetDefault();

this.GamePathField.Value = Environment.ExpandEnvironmentVariables(prefs.ValheimGamePath);
this.ServerPathField.Value = Environment.ExpandEnvironmentVariables(prefs.ValheimServerPath);
this.SaveDataFolderField.Value = Environment.ExpandEnvironmentVariables(prefs.ValheimSaveDataFolder);
}
}
}
10 changes: 6 additions & 4 deletions ValheimServerGUI/Forms/PreferencesForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,12 @@ private void ButtonCancel_Click(object sender, EventArgs e)

private void ButtonDefaults_Click(object sender, EventArgs e)
{
this.WindowsStartField.Value = UserPreferences.Default.StartWithWindows;
this.ServerStartField.Value = UserPreferences.Default.StartServerAutomatically;
this.StartMinimizedField.Value = UserPreferences.Default.StartMinimized;
this.CheckForUpdatesField.Value = UserPreferences.Default.CheckForUpdates;
var prefs = UserPreferences.GetDefault();

this.WindowsStartField.Value = prefs.StartWithWindows;
this.ServerStartField.Value = prefs.StartServerAutomatically;
this.StartMinimizedField.Value = prefs.StartMinimized;
this.CheckForUpdatesField.Value = prefs.CheckForUpdates;
}
}
}
6 changes: 6 additions & 0 deletions ValheimServerGUI/Forms/SplashForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,12 @@ private void OnExceptionHandled()

private void OnMainFormClosed(object sender, FormClosedEventArgs e)
{
if (this.InvokeRequired)
{
this.Invoke(new Action<object, FormClosedEventArgs>(this.OnMainFormClosed), new object[] { sender, e });
return;
}

this.Close();
}

Expand Down
119 changes: 51 additions & 68 deletions ValheimServerGUI/Game/UserPreferences.cs
Original file line number Diff line number Diff line change
@@ -1,108 +1,91 @@
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq;
using ValheimServerGUI.Properties;

namespace ValheimServerGUI.Game
{
public class UserPreferences
{
[JsonIgnore]
private static readonly int DefaultServerPort = int.Parse(Resources.DefaultServerPort);

[JsonIgnore]
public static UserPreferences Default => new();
public static UserPreferences GetDefault() => new();

[JsonProperty("valheimGamePath")]
public string ValheimGamePath { get; set; } = Resources.DefaultGamePath;

[JsonProperty("valheimServerPath")]
public string ValheimServerPath { get; set; } = Resources.DefaultServerPath;

[JsonProperty("valheimSaveDataFolder")]
public string ValheimSaveDataFolder { get; set; } = Resources.DefaultValheimSaveFolder;

[JsonProperty("startWithWindows")]
public bool StartWithWindows { get; set; }

[JsonProperty("startServerAutomatically")]
public bool StartServerAutomatically { get; set; }

[JsonProperty("startMinimized")]
public bool StartMinimized { get; set; }

[JsonProperty("checkForUpdates")]
public bool CheckForUpdates { get; set; } = true;

[JsonProperty("servers")]
public List<ServerPreferences> Servers { get; set; } = new();
public string ServerName { get; set; }

[JsonIgnore]
public string ServerName
{
get => this.Servers.FirstOrDefault()?.Name;
set => this.SetServerValue(s => s.Name = value);
}
public string ServerPassword { get; set; }

[JsonIgnore]
public string ServerPassword
{
get => this.Servers.FirstOrDefault()?.Password;
set => this.SetServerValue(s => s.Password = value);
}
public string ServerWorldName { get; set; }

[JsonIgnore]
public string ServerWorldName
{
get => this.Servers.FirstOrDefault()?.WorldName;
set => this.SetServerValue(s => s.WorldName = value);
}
public bool ServerPublic { get; set; }

[JsonIgnore]
public bool ServerPublic
{
get => this.Servers.FirstOrDefault()?.CommunityServer ?? default;
set => this.SetServerValue(s => s.CommunityServer = value);
}
public int ServerPort { get; set; } = DefaultServerPort;

[JsonIgnore]
public int ServerPort
public static UserPreferences FromFile(UserPreferencesFile file)
{
get => this.Servers.FirstOrDefault()?.Port ?? DefaultServerPort;
set => this.SetServerValue(s => s.Port = value);
}
var prefs = new UserPreferences();

private void SetServerValue(Action<ServerPreferences> action)
{
if (this.Servers == null) this.Servers = new();
if (file == null) return prefs;

var server = this.Servers.FirstOrDefault();
if (server == null)
prefs.ValheimGamePath = file.ValheimGamePath ?? prefs.ValheimGamePath;
prefs.ValheimServerPath = file.ValheimServerPath ?? prefs.ValheimServerPath;
prefs.ValheimSaveDataFolder = file.ValheimSaveDataFolder ?? prefs.ValheimSaveDataFolder;
prefs.StartWithWindows = file.StartWithWindows ?? prefs.StartWithWindows;
prefs.StartServerAutomatically = file.StartServerAutomatically ?? prefs.StartServerAutomatically;
prefs.StartMinimized = file.StartMinimized ?? prefs.StartMinimized;
prefs.CheckForUpdates = file.CheckForUpdates ?? prefs.CheckForUpdates;

var server = file.Servers?.FirstOrDefault();

if (server != null)
{
server = new ServerPreferences();
this.Servers.Add(server);
prefs.ServerName = server.Name ?? prefs.ServerName;
prefs.ServerPassword = server.Password ?? prefs.ServerPassword;
prefs.ServerWorldName = server.WorldName ?? prefs.ServerWorldName;
prefs.ServerPublic = server.CommunityServer ?? prefs.ServerPublic;
prefs.ServerPort = server.Port ?? prefs.ServerPort;
}

action(server);
return prefs;
}

public class ServerPreferences
public UserPreferencesFile ToFile()
{
[JsonProperty("name")]
public string Name { get; set; }

[JsonProperty("password")]
public string Password { get; set; }

[JsonProperty("world")]
public string WorldName { get; set; }

[JsonProperty("community")]
public bool CommunityServer { get; set; }

[JsonProperty("port")]
public int Port { get; set; } = DefaultServerPort;
var file = new UserPreferencesFile
{
ValheimGamePath = this.ValheimGamePath,
ValheimServerPath = this.ValheimServerPath,
ValheimSaveDataFolder = this.ValheimSaveDataFolder,
StartWithWindows = this.StartWithWindows,
StartServerAutomatically = this.StartServerAutomatically,
StartMinimized = this.StartMinimized,
CheckForUpdates = this.CheckForUpdates,
Servers = new()
{
new()
{
Name = this.ServerName,
Password = this.ServerPassword,
WorldName = this.ServerWorldName,
CommunityServer = this.ServerPublic,
Port = this.ServerPort,
}
}
};

return file;
}
}
}
54 changes: 54 additions & 0 deletions ValheimServerGUI/Game/UserPreferencesFile.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using Newtonsoft.Json;
using System.Collections.Generic;

namespace ValheimServerGUI.Game
{
/// <summary>
/// Represents the deserialized user preferences file, and provides the model as it's
/// written to / read from disk.
/// </summary>
public class UserPreferencesFile
{
[JsonProperty("valheimGamePath")]
public string ValheimGamePath { get; set; }

[JsonProperty("valheimServerPath")]
public string ValheimServerPath { get; set; }

[JsonProperty("valheimSaveDataFolder")]
public string ValheimSaveDataFolder { get; set; }

[JsonProperty("startWithWindows")]
public bool? StartWithWindows { get; set; }

[JsonProperty("startServerAutomatically")]
public bool? StartServerAutomatically { get; set; }

[JsonProperty("startMinimized")]
public bool? StartMinimized { get; set; }

[JsonProperty("checkForUpdates")]
public bool? CheckForUpdates { get; set; }

[JsonProperty("servers")]
public List<ServerPreferencesFile> Servers { get; set; }

public class ServerPreferencesFile
{
[JsonProperty("name")]
public string Name { get; set; }

[JsonProperty("password")]
public string Password { get; set; }

[JsonProperty("world")]
public string WorldName { get; set; }

[JsonProperty("community")]
public bool? CommunityServer { get; set; }

[JsonProperty("port")]
public int? Port { get; set; }
}
}
}
10 changes: 6 additions & 4 deletions ValheimServerGUI/Game/UserPreferencesProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ private void SaveInternal(UserPreferences preferences)
{
try
{
this.SaveAsync(this.UserPrefsFilePath, preferences).GetAwaiter().GetResult();
var file = preferences.ToFile();
this.SaveAsync<UserPreferencesFile>(this.UserPrefsFilePath, file).GetAwaiter().GetResult();
this.Logger.LogInformation("User preferences saved");
}
catch (Exception e)
Expand All @@ -81,16 +82,17 @@ public UserPreferences LoadInternal()
}
else
{
return UserPreferences.Default;
return UserPreferences.GetDefault();
}
}

return this.LoadAsync<UserPreferences>(this.UserPrefsFilePath).GetAwaiter().GetResult();
var file = this.LoadAsync<UserPreferencesFile>(this.UserPrefsFilePath).GetAwaiter().GetResult();
return UserPreferences.FromFile(file);
}
catch (Exception e)
{
this.Logger.LogException(e, "Failed to load user preferences");
return UserPreferences.Default;
return UserPreferences.GetDefault();
}
}

Expand Down
2 changes: 1 addition & 1 deletion ValheimServerGUI/ValheimServerGUI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<Description>A simple user interface for running Valheim Dedicated Server on Windows.</Description>
<Copyright>2021</Copyright>
<PackageLicenseExpression>GNU GPLv3</PackageLicenseExpression>
<Version>1.3.0</Version>
<Version>1.3.1</Version>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)'=='Release'">
<SignAssembly>true</SignAssembly>
Expand Down

0 comments on commit f3600f2

Please sign in to comment.