Skip to content

Commit 6852266

Browse files
committed
Feature: Migrate settings to json
1 parent 03be70b commit 6852266

File tree

2 files changed

+56
-25
lines changed

2 files changed

+56
-25
lines changed

Source/NETworkManager.Settings/SettingsManager.cs

Lines changed: 50 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ public static class SettingsManager
2525
/// </summary>
2626
private static string SettingsFolderName => "Settings";
2727

28+
/// <summary>
29+
/// Settings backups directory name.
30+
/// </summary>
31+
private static string BackupFolderName => "Backups";
32+
2833
/// <summary>
2934
/// Settings file name.
3035
/// </summary>
@@ -38,6 +43,7 @@ public static class SettingsManager
3843
/// <summary>
3944
/// Legacy XML settings file extension.
4045
/// </summary>
46+
[Obsolete("Legacy XML settings are no longer used, but the extension is kept for migration purposes.")]
4147
private static string LegacySettingsFileExtension => ".xml";
4248

4349
/// <summary>
@@ -77,6 +83,15 @@ public static string GetSettingsFolderLocation()
7783
AssemblyManager.Current.Name, SettingsFolderName);
7884
}
7985

86+
/// <summary>
87+
/// Method to get the path of the settings backup folder.
88+
/// </summary>
89+
/// <returns>Path to the settings backup folder.</returns>
90+
public static string GetSettingsBackupFolderLocation()
91+
{
92+
return Path.Combine(GetSettingsFolderLocation(), BackupFolderName);
93+
}
94+
8095
/// <summary>
8196
/// Method to get the settings file name.
8297
/// </summary>
@@ -86,6 +101,16 @@ public static string GetSettingsFileName()
86101
return $"{SettingsFileName}{SettingsFileExtension}";
87102
}
88103

104+
/// <summary>
105+
/// Method to get the legacy settings file name.
106+
/// </summary>
107+
/// <returns>Legacy settings file name.</returns>
108+
[Obsolete("Legacy XML settings are no longer used, but the method is kept for migration purposes.")]
109+
public static string GetLegacySettingsFileName()
110+
{
111+
return $"{SettingsFileName}{LegacySettingsFileExtension}";
112+
}
113+
89114
/// <summary>
90115
/// Method to get the settings file path.
91116
/// </summary>
@@ -99,9 +124,10 @@ public static string GetSettingsFilePath()
99124
/// Method to get the legacy XML settings file path.
100125
/// </summary>
101126
/// <returns>Legacy XML settings file path.</returns>
127+
[Obsolete("Legacy XML settings are no longer used, but the method is kept for migration purposes.")]
102128
private static string GetLegacySettingsFilePath()
103129
{
104-
return Path.Combine(GetSettingsFolderLocation(), $"{SettingsFileName}{LegacySettingsFileExtension}");
130+
return Path.Combine(GetSettingsFolderLocation(), GetLegacySettingsFileName());
105131
}
106132

107133
#endregion
@@ -151,16 +177,17 @@ public static void Load()
151177
// Save in new JSON format
152178
Save();
153179

154-
// Backup the old XML file with timestamp to avoid overwriting existing backups
155-
// If a backup with the same timestamp exists (unlikely), it will be overwritten
156-
// since it represents the same migration attempt
157-
var backupFilePath = Path.Combine(GetSettingsFolderLocation(),
158-
$"{SettingsFileName}_{TimestampHelper.GetTimestamp()}{LegacySettingsFileExtension}.backup");
180+
// Create a backup of the legacy XML file and delete the original
181+
Directory.CreateDirectory(GetSettingsBackupFolderLocation());
182+
183+
var backupFilePath = Path.Combine(GetSettingsBackupFolderLocation(),
184+
$"{TimestampHelper.GetTimestamp()}_{GetLegacySettingsFileName()}");
185+
159186
File.Copy(legacyFilePath, backupFilePath, true);
160-
Log.Info($"Legacy XML settings file backed up to: {backupFilePath}");
161187

162-
// Note: The original XML file is intentionally not deleted to allow users to revert if needed.
163-
// Users can manually delete Settings.xml after verifying the migration was successful.
188+
File.Delete(legacyFilePath);
189+
190+
Log.Info($"Legacy XML settings file backed up to: {backupFilePath}");
164191

165192
Log.Info("Settings migration from XML to JSON completed successfully.");
166193

@@ -182,11 +209,6 @@ private static SettingsInfo DeserializeFromFile(string filePath)
182209

183210
var settingsInfo = JsonSerializer.Deserialize<SettingsInfo>(jsonString, JsonOptions);
184211

185-
if (settingsInfo == null)
186-
{
187-
throw new InvalidOperationException("Failed to deserialize settings from JSON file. The result was null.");
188-
}
189-
190212
return settingsInfo;
191213
}
192214

@@ -195,6 +217,7 @@ private static SettingsInfo DeserializeFromFile(string filePath)
195217
/// </summary>
196218
/// <param name="filePath">Path to the XML settings file.</param>
197219
/// <returns>Settings as <see cref="SettingsInfo" />.</returns>
220+
[Obsolete("Legacy XML settings are no longer used, but the method is kept for migration purposes.")]
198221
private static SettingsInfo DeserializeFromXmlFile(string filePath)
199222
{
200223
var xmlSerializer = new XmlSerializer(typeof(SettingsInfo));
@@ -203,11 +226,6 @@ private static SettingsInfo DeserializeFromXmlFile(string filePath)
203226

204227
var settingsInfo = xmlSerializer.Deserialize(fileStream) as SettingsInfo;
205228

206-
if (settingsInfo == null)
207-
{
208-
throw new InvalidOperationException("Failed to deserialize settings from XML file. The result was null.");
209-
}
210-
211229
return settingsInfo;
212230
}
213231

@@ -239,6 +257,19 @@ private static void SerializeToFile(string filePath)
239257

240258
#endregion
241259

260+
#region Backup
261+
/*
262+
private static void Backup()
263+
{
264+
Log.Info("Creating settings backup...");
265+
266+
// Create the backup directory if it does not exist
267+
Directory.CreateDirectory(GetSettingsBackupFolderLocation());
268+
}
269+
*/
270+
271+
#endregion
272+
242273
#region Upgrade
243274

244275
/// <summary>

Source/NETworkManager/App.xaml.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -93,15 +93,13 @@ by BornToBeRoot
9393
}
9494
catch (InvalidOperationException ex)
9595
{
96-
Log.Error("Could not load application settings!");
97-
Log.Error(ex.Message + "-" + ex.StackTrace);
98-
96+
Log.Error("Could not load application settings!", ex);
97+
9998
HandleCorruptedSettingsFile();
10099
}
101100
catch (JsonException ex)
102101
{
103-
Log.Error("Could not load application settings! JSON file is corrupted or invalid.");
104-
Log.Error(ex.Message + "-" + ex.StackTrace);
102+
Log.Error("Could not load application settings! JSON file is corrupted or invalid.", ex);
105103

106104
HandleCorruptedSettingsFile();
107105
}
@@ -225,8 +223,10 @@ private void HandleCorruptedSettingsFile()
225223
// Create backup of corrupted file
226224
var destinationFile =
227225
$"{TimestampHelper.GetTimestamp()}_corrupted_" + SettingsManager.GetSettingsFileName();
226+
228227
File.Copy(SettingsManager.GetSettingsFilePath(),
229228
Path.Combine(SettingsManager.GetSettingsFolderLocation(), destinationFile));
229+
230230
Log.Info($"A backup of the corrupted settings file has been saved under {destinationFile}");
231231

232232
// Initialize default application settings
@@ -283,4 +283,4 @@ private void Save()
283283
ProfileManager.Save();
284284
}
285285
}
286-
}
286+
}

0 commit comments

Comments
 (0)