From 1b0f78c56fe78b0388093fb5413b1998c73963e6 Mon Sep 17 00:00:00 2001 From: dolphinspired Date: Thu, 13 May 2021 18:06:27 -0400 Subject: [PATCH 1/4] Split UserPreferences logic / IO concerns --- ValheimServerGUI/Game/UserPreferences.cs | 119 ++++++++----------- ValheimServerGUI/Game/UserPreferencesFile.cs | 54 +++++++++ 2 files changed, 105 insertions(+), 68 deletions(-) create mode 100644 ValheimServerGUI/Game/UserPreferencesFile.cs diff --git a/ValheimServerGUI/Game/UserPreferences.cs b/ValheimServerGUI/Game/UserPreferences.cs index 7df18f3..3c3e4c6 100644 --- a/ValheimServerGUI/Game/UserPreferences.cs +++ b/ValheimServerGUI/Game/UserPreferences.cs @@ -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 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 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; } } } diff --git a/ValheimServerGUI/Game/UserPreferencesFile.cs b/ValheimServerGUI/Game/UserPreferencesFile.cs new file mode 100644 index 0000000..182ddc6 --- /dev/null +++ b/ValheimServerGUI/Game/UserPreferencesFile.cs @@ -0,0 +1,54 @@ +using Newtonsoft.Json; +using System.Collections.Generic; + +namespace ValheimServerGUI.Game +{ + /// + /// Represents the deserialized user preferences file, and provides the model as it's + /// written to / read from disk. + /// + 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 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; } + } + } +} From d502fb49ff568e491a99659fbc485353fd9e2280 Mon Sep 17 00:00:00 2001 From: dolphinspired Date: Thu, 13 May 2021 18:09:01 -0400 Subject: [PATCH 2/4] Switch over to UserPreferencesFile when saving / loading --- ValheimServerGUI/Forms/DirectoriesForm.cs | 8 +++++--- ValheimServerGUI/Forms/PreferencesForm.cs | 10 ++++++---- ValheimServerGUI/Game/UserPreferencesProvider.cs | 10 ++++++---- 3 files changed, 17 insertions(+), 11 deletions(-) diff --git a/ValheimServerGUI/Forms/DirectoriesForm.cs b/ValheimServerGUI/Forms/DirectoriesForm.cs index 1c4d9ce..bccf4dc 100644 --- a/ValheimServerGUI/Forms/DirectoriesForm.cs +++ b/ValheimServerGUI/Forms/DirectoriesForm.cs @@ -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); } } } diff --git a/ValheimServerGUI/Forms/PreferencesForm.cs b/ValheimServerGUI/Forms/PreferencesForm.cs index f6e9fa3..0aab192 100644 --- a/ValheimServerGUI/Forms/PreferencesForm.cs +++ b/ValheimServerGUI/Forms/PreferencesForm.cs @@ -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; } } } diff --git a/ValheimServerGUI/Game/UserPreferencesProvider.cs b/ValheimServerGUI/Game/UserPreferencesProvider.cs index c677b7b..39579bc 100644 --- a/ValheimServerGUI/Game/UserPreferencesProvider.cs +++ b/ValheimServerGUI/Game/UserPreferencesProvider.cs @@ -60,7 +60,8 @@ private void SaveInternal(UserPreferences preferences) { try { - this.SaveAsync(this.UserPrefsFilePath, preferences).GetAwaiter().GetResult(); + var file = preferences.ToFile(); + this.SaveAsync(this.UserPrefsFilePath, file).GetAwaiter().GetResult(); this.Logger.LogInformation("User preferences saved"); } catch (Exception e) @@ -81,16 +82,17 @@ public UserPreferences LoadInternal() } else { - return UserPreferences.Default; + return UserPreferences.GetDefault(); } } - return this.LoadAsync(this.UserPrefsFilePath).GetAwaiter().GetResult(); + var file = this.LoadAsync(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(); } } From c161e5403e165bf900b263c7306d8cfcb2f7df1a Mon Sep 17 00:00:00 2001 From: dolphinspired Date: Thu, 13 May 2021 19:03:00 -0400 Subject: [PATCH 3/4] Fix cross-thread bug when closing SplashForm --- ValheimServerGUI/Forms/SplashForm.cs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/ValheimServerGUI/Forms/SplashForm.cs b/ValheimServerGUI/Forms/SplashForm.cs index 03b5f95..e456c1c 100644 --- a/ValheimServerGUI/Forms/SplashForm.cs +++ b/ValheimServerGUI/Forms/SplashForm.cs @@ -202,6 +202,12 @@ private void OnExceptionHandled() private void OnMainFormClosed(object sender, FormClosedEventArgs e) { + if (this.InvokeRequired) + { + this.Invoke(new Action(this.OnMainFormClosed), new object[] { sender, e }); + return; + } + this.Close(); } From a429ed17b6a39426c7a6897446142d92dfd5ed17 Mon Sep 17 00:00:00 2001 From: dolphinspired Date: Thu, 13 May 2021 19:04:47 -0400 Subject: [PATCH 4/4] v1.3.1 --- ValheimServerGUI/ValheimServerGUI.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ValheimServerGUI/ValheimServerGUI.csproj b/ValheimServerGUI/ValheimServerGUI.csproj index d9fb835..e523337 100644 --- a/ValheimServerGUI/ValheimServerGUI.csproj +++ b/ValheimServerGUI/ValheimServerGUI.csproj @@ -10,7 +10,7 @@ A simple user interface for running Valheim Dedicated Server on Windows. 2021 GNU GPLv3 - 1.3.0 + 1.3.1 true