Skip to content

Commit e344ae6

Browse files
committed
Feature: Improve settings load
1 parent 66a6ca7 commit e344ae6

File tree

3 files changed

+52
-59
lines changed

3 files changed

+52
-59
lines changed

Source/NETworkManager.Settings/LocalSettingsManager.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ public static void Load()
115115
}
116116
}
117117

118-
// Initialize new settings if loading failed or file does not exist
118+
// Initialize new local settings if file does not exist or loading failed
119119
Initialize();
120120
}
121121

Source/NETworkManager.Settings/SettingsManager.cs

Lines changed: 46 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -266,43 +266,68 @@ public static void Load()
266266
// Check if JSON file exists
267267
if (File.Exists(filePath))
268268
{
269-
Log.Info($"Loading settings from: {filePath}");
269+
try
270+
{
271+
Log.Info($"Loading settings from: {filePath}");
270272

271-
Current = DeserializeFromFile(filePath);
273+
Current = DeserializeFromFile(filePath);
272274

273-
Log.Info("Settings loaded successfully.");
275+
Log.Info("Settings loaded successfully.");
274276

275-
// Reset change tracking
276-
Current.SettingsChanged = false;
277+
// Reset change tracking
278+
Current.SettingsChanged = false;
277279

278-
return;
279-
}
280+
return;
281+
}
282+
catch (Exception ex)
283+
{
284+
Log.Error($"Failed to load settings from: {filePath}", ex);
285+
286+
Backup(filePath,
287+
GetSettingsFolderLocation(),
288+
$"{TimestampHelper.GetTimestamp()}_corrupted_{Path.GetFileName(filePath)}");
280289

290+
ConfigurationManager.Current.ShowSettingsResetNoteOnStartup = true;
291+
}
292+
}
281293
// Check if legacy XML file exists and migrate it
282-
if (File.Exists(legacyFilePath))
294+
else if (File.Exists(legacyFilePath))
283295
{
284-
Log.Info("Legacy XML settings file found. Migrating to JSON format...");
296+
try
297+
{
298+
Log.Info("Legacy XML settings file found. Migrating to JSON format...");
285299

286-
Current = DeserializeFromXmlFile(legacyFilePath);
300+
Current = DeserializeFromXmlFile(legacyFilePath);
287301

288-
Current.SettingsChanged = false;
302+
Current.SettingsChanged = false;
289303

290-
// Save in new JSON format
291-
Save();
304+
// Save in new JSON format
305+
Save();
292306

293-
// Create a backup of the legacy XML file and delete the original
294-
Backup(legacyFilePath,
295-
GetSettingsBackupFolderLocation(),
296-
TimestampHelper.GetTimestampFilename(GetLegacySettingsFileName()));
307+
// Create a backup of the legacy XML file and delete the original
308+
Backup(legacyFilePath,
309+
GetSettingsBackupFolderLocation(),
310+
TimestampHelper.GetTimestampFilename(GetLegacySettingsFileName()));
297311

298-
File.Delete(legacyFilePath);
312+
File.Delete(legacyFilePath);
299313

300-
Log.Info("Settings migration from XML to JSON completed successfully.");
314+
Log.Info("Settings migration from XML to JSON completed successfully.");
301315

302-
return;
316+
return;
317+
}
318+
catch (Exception ex)
319+
{
320+
Log.Error($"Failed to load legacy settings from: {legacyFilePath}", ex);
321+
322+
Backup(legacyFilePath,
323+
GetSettingsFolderLocation(),
324+
$"{TimestampHelper.GetTimestamp()}_corrupted_{Path.GetFileName(legacyFilePath)}");
325+
326+
ConfigurationManager.Current.ShowSettingsResetNoteOnStartup = true;
327+
}
303328
}
304329

305-
// Initialize the default settings if there is no settings file.
330+
// Initialize new settings if file does not exist or loading failed
306331
Initialize();
307332
}
308333

Source/NETworkManager/App.xaml.cs

Lines changed: 5 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
using System;
22
using System.Diagnostics;
3-
using System.IO;
43
using System.Linq;
5-
using System.Text.Json;
64
using System.Threading;
75
using System.Windows;
86
using System.Windows.Threading;
@@ -88,21 +86,12 @@ by BornToBeRoot
8886

8987
// Load (or initialize) local settings
9088
LocalSettingsManager.Load();
91-
92-
// Load (or initialize) settings
93-
try
94-
{
95-
if (CommandLineManager.Current.ResetSettings)
96-
SettingsManager.Initialize();
97-
else
98-
SettingsManager.Load();
99-
}
100-
catch (Exception ex)
101-
{
102-
Log.Error("Could not load application settings!", ex);
10389

104-
HandleCorruptedSettingsFile();
105-
}
90+
// Load (or initialize) settings
91+
if (CommandLineManager.Current.ResetSettings)
92+
SettingsManager.Initialize();
93+
else
94+
SettingsManager.Load();
10695

10796
// Upgrade settings if necessary
10897
var settingsVersion = Version.Parse(SettingsManager.Current.Version);
@@ -209,27 +198,6 @@ by BornToBeRoot
209198
StartupUri = new Uri("MainWindow.xaml", UriKind.Relative);
210199
}
211200

212-
/// <summary>
213-
/// Handles a corrupted settings file by creating a backup and initializing default settings.
214-
/// </summary>
215-
private void HandleCorruptedSettingsFile()
216-
{
217-
// Create backup of corrupted file
218-
var destinationFile =
219-
$"{TimestampHelper.GetTimestamp()}_corrupted_" + SettingsManager.GetSettingsFileName();
220-
221-
File.Copy(SettingsManager.GetSettingsFilePath(),
222-
Path.Combine(SettingsManager.GetSettingsFolderLocation(), destinationFile));
223-
224-
Log.Info($"A backup of the corrupted settings file has been saved under {destinationFile}");
225-
226-
// Initialize default application settings
227-
Log.Info("Initialize default application settings...");
228-
229-
SettingsManager.Initialize();
230-
ConfigurationManager.Current.ShowSettingsResetNoteOnStartup = true;
231-
}
232-
233201
/// <summary>
234202
/// Handles the tick event of the dispatcher timer to trigger a background job and save data.
235203
/// </summary>

0 commit comments

Comments
 (0)