-
Notifications
You must be signed in to change notification settings - Fork 803
Expand file tree
/
Copy pathLocalSettingsManager.cs
More file actions
142 lines (117 loc) · 4.08 KB
/
LocalSettingsManager.cs
File metadata and controls
142 lines (117 loc) · 4.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
using log4net;
using System;
using System.IO;
using System.Text.Json;
using System.Text.Json.Serialization;
namespace NETworkManager.Settings;
/// <summary>
/// Manages local application settings that are stored outside the main settings file.
/// This is used for settings that control where the main settings file is located.
/// </summary>
public static class LocalSettingsManager
{
#region Variables
/// <summary>
/// Logger for logging.
/// </summary>
private static readonly ILog Log = LogManager.GetLogger(typeof(LocalSettingsManager));
/// <summary>
/// Settings file name.
/// </summary>
private static string SettingsFileName => "Settings.json";
/// <summary>
/// Settings that are currently loaded.
/// </summary>
public static LocalSettingsInfo Current { get; private set; }
/// <summary>
/// JSON serializer options for consistent serialization/deserialization.
/// </summary>
private static readonly JsonSerializerOptions JsonOptions = new()
{
WriteIndented = true,
PropertyNameCaseInsensitive = true,
DefaultIgnoreCondition = JsonIgnoreCondition.Never,
Converters = { new JsonStringEnumConverter() }
};
#endregion
#region Methods
/// <summary>
/// Method to get the path of the settings folder.
/// </summary>
/// <returns>Path to the settings folder.</returns>
private static string GetSettingsFolderLocation()
{
return Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
AssemblyManager.Current.Name);
}
/// <summary>
/// Method to get the settings file path
/// </summary>
/// <returns>Settings file path.</returns>
private static string GetSettingsFilePath()
{
return Path.Combine(
GetSettingsFolderLocation(),
SettingsFileName);
}
/// <summary>
/// Initialize new settings (<see cref="SettingsInfo" />) and save them (to a file).
/// </summary>
private static void Initialize()
{
Log.Info("Initializing new local settings.");
Current = new LocalSettingsInfo();
Save();
}
/// <summary>
/// Method to load the settings from a file.
/// </summary>
public static void Load()
{
var filePath = GetSettingsFilePath();
if (File.Exists(filePath))
{
try
{
Log.Info($"Loading local settings from: {filePath}");
var jsonString = File.ReadAllText(filePath);
// Treat empty or JSON "null" as "no settings" instead of crashing
if (string.IsNullOrWhiteSpace(jsonString))
{
Log.Info("Local settings file is empty, initializing new local settings.");
}
else
{
Current = JsonSerializer.Deserialize<LocalSettingsInfo>(jsonString, JsonOptions) ?? new LocalSettingsInfo();
Log.Info("Local settings loaded successfully.");
// Reset change tracking
Current.SettingsChanged = false;
return;
}
}
catch (Exception ex)
{
Log.Error($"Failed to load local settings from: {filePath}", ex);
}
}
// Initialize new local settings if file does not exist or loading failed
Initialize();
}
/// <summary>
/// Method to save the current settings to a file.
/// </summary>
public static void Save()
{
// Create the directory if it does not exist
Directory.CreateDirectory(GetSettingsFolderLocation());
// Serialize to file
var filePath = GetSettingsFilePath();
var jsonString = JsonSerializer.Serialize(Current, JsonOptions);
File.WriteAllText(filePath, jsonString);
Log.Info($"Local settings saved to {filePath}");
// Reset change tracking
Current.SettingsChanged = false;
}
#endregion
}