-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #28 from runeberry/jb-bug
Bugfixes
- Loading branch information
Showing
7 changed files
with
129 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters